From 1e8711af353bb6c761bbc52aca28e1893a50b2d5 Mon Sep 17 00:00:00 2001 From: Sergio Alonso Date: Thu, 9 Mar 2017 01:58:07 +0100 Subject: [PATCH 1/2] I002 no configuration found when flake8-isort is used with flake8 git hook. Files with changes are moved to a temporal directory when flake8 git hook is used. $ flake8 --install-hook git flake8-isort should look for configuration files in the current directory while checking the files in the temporary directory. Before fix, no configuration is found. $ git commit -m "Commit message" [DEBUG] full_path = /tmp/tmpl7f83uys/home/user/src/project/script.py script.py:0:1: I002 no configuration found (.isort.cfg or [isort] on setup.cfg) After fix, project configuration is found. $ git commit -m "Commit message" [DEBUG] full_path = /home/user/src/project/script.py script.py:1:1: D200 One-line docstring should fit on one line with quotes --- flake8_isort.py | 25 ++++++++++++++++++++++++- run_tests.py | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/flake8_isort.py b/flake8_isort.py index 821edd7..4336d34 100644 --- a/flake8_isort.py +++ b/flake8_isort.py @@ -35,8 +35,9 @@ class Flake8Isort(object): config_file = None - def __init__(self, tree, filename): + def __init__(self, tree, filename, search_current=True): self.filename = filename + self.search_current = search_current @classmethod def add_options(cls, parser): @@ -98,8 +99,30 @@ def search_isort_config(self): if 'isort' in config.sections(): return True + if self.search_current: + return self.search_isort_config_at_current() + return False + def search_isort_config_at_current(self): + """Search for isort configuration at current directory + """ + isort_file = '{0}{1}.isort.cfg'.format(os.path.realpath('.'), os.sep) + if os.path.exists(isort_file): + return True + + # If the setup file exists and has an "isort" section, + # then we've found the configuration. + setup_file = '{0}{1}setup.cfg'.format(os.path.realpath('.'), os.sep) + if os.path.exists(setup_file): + config = ConfigParser() + config.read(setup_file) + if 'isort' in config.sections(): + return True + + return False + + def sortimports_linenum_msg(self, sort_result): """Parses isort.SortImports for line number changes and message diff --git a/run_tests.py b/run_tests.py index bacd47d..b036ae8 100644 --- a/run_tests.py +++ b/run_tests.py @@ -205,7 +205,7 @@ def test_isortcfg_not_found(self): os.remove(isortcfg_path) with OutputCapture(): - checker = Flake8Isort(None, file_path) + checker = Flake8Isort(None, file_path, search_current=False) checker.config_file = True ret = list(checker.run()) self.assertEqual(len(ret), 1) From a36bd7f4f639cff78c704a97d5d0efc475f7d5e5 Mon Sep 17 00:00:00 2001 From: Sergio Alonso Date: Mon, 13 Mar 2017 21:41:36 +0100 Subject: [PATCH 2/2] Update flake8_isort.py --- flake8_isort.py | 1 - 1 file changed, 1 deletion(-) diff --git a/flake8_isort.py b/flake8_isort.py index 4336d34..04de46e 100644 --- a/flake8_isort.py +++ b/flake8_isort.py @@ -122,7 +122,6 @@ def search_isort_config_at_current(self): return False - def sortimports_linenum_msg(self, sort_result): """Parses isort.SortImports for line number changes and message