Add autopep8-wrapper hook.
diff --git a/README.md b/README.md
index 32770a9..a2d38a7 100644
--- a/README.md
+++ b/README.md
@@ -22,6 +22,7 @@
### Hooks available
+- `autopep8-wrapper` - Runs autopep8 over python source.
- `check-json` - Attempts to load all json files to verify syntax.
- `check-yaml` - Attempts to load all yaml files to verify syntax.
- `debug-statements` - Check for pdb / ipdb / pudb statements in code.
diff --git a/hooks.yaml b/hooks.yaml
index 7b69d48..84685f2 100644
--- a/hooks.yaml
+++ b/hooks.yaml
@@ -1,3 +1,10 @@
+- id: autopep8-wrapper
+ name: autopep8 wrapper
+ description: Runs autopep8 over python source.
+ entry: autopep8-wrapper
+ language: python
+ files: \.py$
+ args: [-i]
- id: check-json
name: Check JSON
description: This hook checks json files for parseable syntax.
diff --git a/pre_commit_hooks/autopep8_wrapper.py b/pre_commit_hooks/autopep8_wrapper.py
new file mode 100644
index 0000000..c2f5ee7
--- /dev/null
+++ b/pre_commit_hooks/autopep8_wrapper.py
@@ -0,0 +1,28 @@
+from __future__ import absolute_import
+from __future__ import print_function
+from __future__ import unicode_literals
+
+import autopep8
+import io
+import sys
+
+
+def main(argv=None):
+ argv = argv if argv is not None else sys.argv[1:]
+ args = autopep8.parse_args(argv)
+
+ retv = 0
+ for filename in args.files:
+ original_contents = io.open(filename).read()
+ new_contents = autopep8.fix_code(original_contents, args)
+ if original_contents != new_contents:
+ print('Fixing {0}'.format(filename))
+ retv = 1
+ with io.open(filename, 'w') as output_file:
+ output_file.write(new_contents)
+
+ return retv
+
+
+if __name__ == '__main__':
+ exit(main())
diff --git a/setup.py b/setup.py
index d69c730..588736b 100644
--- a/setup.py
+++ b/setup.py
@@ -24,6 +24,7 @@
packages=find_packages('.', exclude=('tests*', 'testing*')),
install_requires=[
'argparse',
+ 'autopep8',
'flake8',
'plumbum',
'pyflakes',
@@ -32,6 +33,7 @@
],
entry_points={
'console_scripts': [
+ 'autopep8-wrapper = pre_commit_hooks.autopep8_wrapper:main',
'check-json = pre_commit_hooks.check_json:check_json',
'check-yaml = pre_commit_hooks.check_yaml:check_yaml',
'debug-statement-hook = pre_commit_hooks.debug_statement_hook:debug_statement_hook',
diff --git a/tests/autopep8_wrapper_test.py b/tests/autopep8_wrapper_test.py
new file mode 100644
index 0000000..0e4d7f0
--- /dev/null
+++ b/tests/autopep8_wrapper_test.py
@@ -0,0 +1,24 @@
+from __future__ import absolute_import
+from __future__ import unicode_literals
+
+import io
+import os.path
+import pytest
+
+from pre_commit_hooks.autopep8_wrapper import main
+
+
+@pytest.mark.parametrize(
+ ('input_src', 'expected_ret', 'output_src'),
+ (
+ ('print(1 + 2)\n', 1, 'print(1 + 2)\n'),
+ ('print(1 + 2)\n', 0, 'print(1 + 2)\n'),
+ ),
+)
+def test_main_failing(tmpdir, input_src, expected_ret, output_src):
+ filename = os.path.join(tmpdir.strpath, 'test.py')
+ with io.open(filename, 'w') as file_obj:
+ file_obj.write(input_src)
+ ret = main([filename, '-i', '-v'])
+ assert ret == expected_ret
+ assert io.open(filename).read() == output_src