├── .gitignore ├── .pre-commit-hooks.yaml ├── .travis.yml ├── LICENSE.txt ├── README.md ├── hooks.yaml ├── pre_commit_hook ├── __init__.py └── sort.py ├── requirements-dev.txt ├── setup.cfg ├── setup.py └── tests ├── __init__.py └── sort_test.py /.gitignore: -------------------------------------------------------------------------------- 1 | .coverage 2 | *.egg-info 3 | -------------------------------------------------------------------------------- /.pre-commit-hooks.yaml: -------------------------------------------------------------------------------- 1 | - id: python-import-sorter 2 | name: Sort python imports 3 | description: This hook sorts python imports. 4 | entry: python-import-sorter 5 | language: python 6 | files: \.py$ 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "2.6" 4 | - "2.7" 5 | - "3.3" 6 | - "3.4" 7 | install: 8 | - "pip install -r requirements-dev.txt" 9 | - "pip install coveralls" 10 | script: "py.test --cov pre_commit_hook tests/" 11 | after_success: 12 | - coveralls 13 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Falcon Social 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | | :exclamation: This project is no longer maintained, and should be replaced with a different project such as isort. | 2 | |---------------------------------------------------------------------------------------------------------------------| 3 | 4 | [![Build Status](https://travis-ci.org/FalconSocial/pre-commit-python-sorter.svg?branch=master)](https://travis-ci.org/FalconSocial/pre-commit-python-sorter) 5 | [![Coverage Status](https://img.shields.io/coveralls/FalconSocial/pre-commit-python-sorter.svg)](https://coveralls.io/r/FalconSocial/pre-commit-python-sorter) 6 | 7 | 8 | Pre-commit python module sorter 9 | =============================== 10 | 11 | This is a [pre-commit](https://github.com/pre-commit) hook that will sort your 12 | imports for you (or show you how it should be done). 13 | 14 | * [pre-commit](https://github.com/pre-commit) 15 | * [isort](https://github.com/timothycrosley/isort) 16 | 17 | 18 | Add this to your ``.pre-commit-config.yaml`` file 19 | 20 | - repo: git://github.com/FalconSocial/pre-commit-python-sorter 21 | sha: 1.0.4 22 | hooks: 23 | - id: python-import-sorter 24 | args: ['--silent-overwrite'] 25 | 26 | Available flags: 27 | 28 | * ``--silent-overwrite``: The hook won't fail if it has to change files. It will 29 | just do it. 30 | * ``--check-only``: The hook will not change any files. 31 | * ``--diff``: If imports are not ordered correctly, print a diff of required 32 | changes to fix the import order. 33 | 34 | The hook supports [isort's configuration files](https://github.com/timothycrosley/isort#configuring-isort) - Please refer to the isort documentation for reference 35 | 36 | Development: ``pip install -r requirements-dev.txt`` 37 | 38 | Testing: ``py.test --cov pre_commit_hook tests/`` 39 | -------------------------------------------------------------------------------- /hooks.yaml: -------------------------------------------------------------------------------- 1 | - id: python-import-sorter 2 | name: Sort python imports 3 | description: This hook sorts python imports. 4 | entry: python-import-sorter 5 | language: python 6 | files: \.py$ 7 | -------------------------------------------------------------------------------- /pre_commit_hook/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FalconSocial/pre-commit-python-sorter/b57ce61dbbeff813cc8786584d3c98c885ac3e14/pre_commit_hook/__init__.py -------------------------------------------------------------------------------- /pre_commit_hook/sort.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import argparse 4 | import os 5 | 6 | from isort import isort 7 | 8 | 9 | def imports_incorrect(filename, show_diff=False): 10 | return isort.SortImports(filename, check=True, show_diff=show_diff).incorrectly_sorted 11 | 12 | 13 | def main(argv=None): 14 | 15 | parser = argparse.ArgumentParser() 16 | parser.add_argument('filenames', nargs='*', help='Filenames to run') 17 | parser.add_argument('--silent-overwrite', action='store_true', dest='silent', default=False) 18 | parser.add_argument('--check-only', action='store_true', dest='check_only', default=False) 19 | parser.add_argument('--diff', action='store_true', dest='show_diff', default=False) 20 | args = parser.parse_args(argv) 21 | 22 | return_value = 0 23 | 24 | for filename in args.filenames: 25 | if imports_incorrect(filename, show_diff=args.show_diff): 26 | if args.check_only: 27 | return_value = 1 28 | elif args.silent: 29 | isort.SortImports(filename) 30 | else: 31 | return_value = 1 32 | isort.SortImports(filename) 33 | print('FIXED: {0}'.format(os.path.abspath(filename))) 34 | return return_value 35 | 36 | if __name__ == '__main__': 37 | exit(main()) 38 | -------------------------------------------------------------------------------- /requirements-dev.txt: -------------------------------------------------------------------------------- 1 | -e . 2 | pytest 3 | pytest-cov 4 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [wheel] 2 | universal = 1 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from setuptools import find_packages 3 | from setuptools import setup 4 | 5 | install_requires = ['isort>=4.1.1,<5'] 6 | if sys.version_info < (2, 7): 7 | install_requires.append('argparse') 8 | 9 | 10 | setup( 11 | name='pre_commit_python_sort', 12 | description='A pre-commit hook to sort your python imports.', 13 | url='https://github.com/FalconSocial/pre-commit-python-sorter', 14 | version='1.0.3', 15 | 16 | author='Kasper Jacobsen', 17 | author_email='k@mackwerk.dk', 18 | 19 | platforms='linux', 20 | classifiers=[ 21 | 'License :: OSI Approved :: MIT License', 22 | 'Programming Language :: Python :: 2', 23 | 'Programming Language :: Python :: 2.6', 24 | 'Programming Language :: Python :: 2.7', 25 | 'Programming Language :: Python :: 3', 26 | 'Programming Language :: Python :: 3.3', 27 | 'Programming Language :: Python :: 3.4', 28 | 'Programming Language :: Python :: Implementation :: CPython', 29 | 'Programming Language :: Python :: Implementation :: PyPy', 30 | ], 31 | 32 | packages=find_packages('.', exclude=('tests*', 'testing*')), 33 | install_requires=install_requires, 34 | entry_points={ 35 | 'console_scripts': [ 36 | 'python-import-sorter = pre_commit_hook.sort:main', 37 | ], 38 | }, 39 | ) 40 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FalconSocial/pre-commit-python-sorter/b57ce61dbbeff813cc8786584d3c98c885ac3e14/tests/__init__.py -------------------------------------------------------------------------------- /tests/sort_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from isort.isort import stdout 3 | 4 | from pre_commit_hook.sort import main 5 | 6 | 7 | def write_file(filename, contents): 8 | with open(filename, 'w') as file_obj: 9 | file_obj.write(contents) 10 | 11 | 12 | @pytest.fixture 13 | def tmpfiles(tmpdir): 14 | write_file(tmpdir.join('correct_1.py').strpath, 'import json\nimport sys\n') 15 | write_file(tmpdir.join('incorrect_1.py').strpath, 'import sys\n\n\nimport json\n') 16 | write_file(tmpdir.join('incorrect_2.py').strpath, 'import sys\n\n\nimport json\n') 17 | return tmpdir 18 | 19 | 20 | def test_sort(tmpfiles): 21 | assert main([tmpfiles.join('correct_1.py').strpath]) == 0 22 | assert main([tmpfiles.join('incorrect_1.py').strpath]) == 1 23 | assert main([tmpfiles.join('incorrect_2.py').strpath, '--check-only']) == 1 24 | assert main([tmpfiles.join('incorrect_2.py').strpath, '--silent-overwrite']) == 0 25 | 26 | 27 | def test_sort_with_diff(tmpfiles): 28 | filename = tmpfiles.join('incorrect_1.py').strpath 29 | main(['--diff', '--check-only', filename]) 30 | 31 | stdout.seek(0) 32 | lines = stdout.read().splitlines() 33 | # Skip diff header 34 | lines = lines[4:] 35 | assert lines == [ 36 | '+import json', 37 | ' import sys', 38 | '-', 39 | '-', 40 | '-import json', 41 | ] 42 | --------------------------------------------------------------------------------