Merge pull request #8 from pre-commit/autopep8_wrapper

Autopep8 wrapper
This commit is contained in:
Ken Struys 2014-08-22 11:10:23 -07:00
commit dec8e5eecc
6 changed files with 65 additions and 1 deletions

View file

@ -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

View file

@ -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.

View file

@ -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.

View file

@ -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())

View file

@ -24,6 +24,7 @@ setup(
packages=find_packages('.', exclude=('tests*', 'testing*')),
install_requires=[
'argparse',
'autopep8',
'flake8',
'plumbum',
'pyflakes',
@ -32,6 +33,7 @@ setup(
],
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',

View file

@ -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