diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9288d5e2..f0f974d8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,8 +1,10 @@ - repo: git@github.com:pre-commit/pre-commit-hooks - sha: 243fe50bc119bc5f72be76fc4e3de260ee6f64f1 + sha: 86b1c9da8e917a77c975b14666513d6bd45a3b03 hooks: - id: trailing-whitespace - id: end-of-file-fixer + - id: autopep8-wrapper + args: ['-i', '--ignore=E265,E309,E501', '-v'] - id: check-json - id: check-yaml - id: debug-statements diff --git a/README.md b/README.md index 32770a91..a2d38a79 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Add this to your `.pre-commit-config.yaml` ### 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 7b69d483..84685f26 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 00000000..c2f5ee76 --- /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 d69c7303..588736b9 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 00000000..0e4d7f07 --- /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