diff options
author | pravindalve | 2020-02-14 13:04:30 +0530 |
---|---|---|
committer | GitHub | 2020-02-14 13:04:30 +0530 |
commit | a80b6726f5f70d9a2ec1cbf361e7f607849343bf (patch) | |
tree | 333d34f58255003939e70b800d2cd57e40253b6b /venv/Lib/site-packages/isort/hooks.py | |
parent | 8189de7d424964aac11b81c8297b7af7fcedd2b8 (diff) | |
parent | df141f35dccc6b21fcfa575707c6435a39d0002f (diff) | |
download | Chemical-Simulator-GUI-a80b6726f5f70d9a2ec1cbf361e7f607849343bf.tar.gz Chemical-Simulator-GUI-a80b6726f5f70d9a2ec1cbf361e7f607849343bf.tar.bz2 Chemical-Simulator-GUI-a80b6726f5f70d9a2ec1cbf361e7f607849343bf.zip |
Merge pull request #2 from pravindalve/master
Code restructured, some ui improvizations, undo redo implementation and Binary envelops utility
Diffstat (limited to 'venv/Lib/site-packages/isort/hooks.py')
-rw-r--r-- | venv/Lib/site-packages/isort/hooks.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/venv/Lib/site-packages/isort/hooks.py b/venv/Lib/site-packages/isort/hooks.py new file mode 100644 index 0000000..16a16e1 --- /dev/null +++ b/venv/Lib/site-packages/isort/hooks.py @@ -0,0 +1,91 @@ +"""isort.py. + +Defines a git hook to allow pre-commit warnings and errors about import order. + +usage: + exit_code = git_hook(strict=True|False, modify=True|False) + +Copyright (C) 2015 Helen Sherwood-Taylor + +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. + +""" +import subprocess + +from isort import SortImports + + +def get_output(command): + """ + Run a command and return raw output + + :param str command: the command to run + :returns: the stdout output of the command + """ + return subprocess.check_output(command.split()) + + +def get_lines(command): + """ + Run a command and return lines of output + + :param str command: the command to run + :returns: list of whitespace-stripped lines output by command + """ + stdout = get_output(command) + return [line.strip().decode('utf-8') for line in stdout.splitlines()] + + +def git_hook(strict=False, modify=False): + """ + Git pre-commit hook to check staged files for isort errors + + :param bool strict - if True, return number of errors on exit, + causing the hook to fail. If False, return zero so it will + just act as a warning. + :param bool modify - if True, fix the sources if they are not + sorted properly. If False, only report result without + modifying anything. + + :return number of errors if in strict mode, 0 otherwise. + """ + + # Get list of files modified and staged + diff_cmd = "git diff-index --cached --name-only --diff-filter=ACMRTUXB HEAD" + files_modified = get_lines(diff_cmd) + + errors = 0 + for filename in files_modified: + if filename.endswith('.py'): + # Get the staged contents of the file + staged_cmd = "git show :%s" % filename + staged_contents = get_output(staged_cmd) + + sort = SortImports( + file_path=filename, + file_contents=staged_contents.decode(), + check=True + ) + + if sort.incorrectly_sorted: + errors += 1 + if modify: + SortImports( + file_path=filename, + file_contents=staged_contents.decode(), + check=False, + ) + + return errors if strict else 0 |