Autopep8 wrapper by asottile · Pull Request #8 · pre-commit/pre-commit-hooks · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions hooks.yaml
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
28 changes: 28 additions & 0 deletions pre_commit_hooks/autopep8_wrapper.py
Original file line number Diff line number Diff line change
@@ -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())
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
packages=find_packages('.', exclude=('tests*', 'testing*')),
install_requires=[
'argparse',
'autopep8',
'flake8',
'plumbum',
'pyflakes',
Expand All @@ -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',
Expand Down
24 changes: 24 additions & 0 deletions tests/autopep8_wrapper_test.py