mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 03:56:54 +00:00
Move FalconSocial pre-commit-python-sorter to offical pre-commit repo, thanks @Dinoshauer @teeberg @amureki @jeffshek @Tenzer @asottile
This commit is contained in:
parent
0ddb3b8527
commit
2ca5460c30
6 changed files with 130 additions and 0 deletions
69
pre_commit_hooks/sort_python_imports.py
Normal file
69
pre_commit_hooks/sort_python_imports.py
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
"""
|
||||
Migrated from https://github.com/FalconSocial/pre-commit-python-sorter/
|
||||
by @benjaoming
|
||||
|
||||
Thanks to FalconSocial devs for starting this.
|
||||
Original author: Kasper Jacobsen, @Dinoshauer
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Falcon Social
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import os
|
||||
|
||||
from isort import isort
|
||||
|
||||
|
||||
def imports_incorrect(filename, show_diff=False):
|
||||
return isort.SortImports(filename, check=True, show_diff=show_diff).incorrectly_sorted
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*', help='Filenames to run')
|
||||
parser.add_argument('--silent-overwrite', action='store_true', dest='silent', default=False)
|
||||
parser.add_argument('--check-only', action='store_true', dest='check_only', default=False)
|
||||
parser.add_argument('--diff', action='store_true', dest='show_diff', default=False)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
return_value = 0
|
||||
|
||||
for filename in args.filenames:
|
||||
if imports_incorrect(filename, show_diff=args.show_diff):
|
||||
if args.check_only:
|
||||
return_value = 1
|
||||
elif args.silent:
|
||||
isort.SortImports(filename)
|
||||
else:
|
||||
return_value = 1
|
||||
isort.SortImports(filename)
|
||||
print('FIXED: {}'.format(os.path.abspath(filename)))
|
||||
return return_value
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exit(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue