1
1
# -*- coding: utf-8 -*-
2
2
from difflib import Differ
3
3
from isort import SortImports
4
-
from os.path import expanduser
5
4
from testfixtures import OutputCapture
6
5
7
-
import os
8
-
9
-
10
-
try:
11
-
from configparser import ConfigParser as SafeConfigParser
12
-
except ImportError:
13
-
from ConfigParser import SafeConfigParser
14
-
15
6
16
7
class Flake8Isort(object):
17
8
name = 'flake8_isort'
@@ -32,7 +23,6 @@ class Flake8Isort(object):
32
23
'I005 isort found an unexpected missing import'
33
24
)
34
25
35
-
config_file = None
36
26
show_traceback = False
37
27
stdin_display_name = None
38
28
@@ -43,12 +33,6 @@ def __init__(self, tree, filename, lines, search_current=True):
43
33
44
34
@classmethod
45
35
def add_options(cls, parser):
46
-
parser.add_option(
47
-
'--no-isort-config',
48
-
action='store_true',
49
-
parse_from_config=True,
50
-
help='Do not require explicit configuration to be found'
51
-
)
52
36
53
37
parser.add_option(
54
38
'--isort-show-traceback',
@@ -59,93 +43,27 @@ def add_options(cls, parser):
59
43
60
44
@classmethod
61
45
def parse_options(cls, options):
62
-
if options.no_isort_config is None:
63
-
cls.config_file = True
64
-
else:
65
-
cls.config_file = False
66
-
67
46
cls.stdin_display_name = options.stdin_display_name
68
47
cls.show_traceback = options.isort_show_traceback
69
48
70
49
def run(self):
71
-
settings_file = self.search_isort_config()
72
-
if self.config_file and not settings_file:
73
-
yield 0, 0, self.no_config_msg, type(self)
50
+
if self.filename is not self.stdin_display_name:
51
+
file_path = self.filename
74
52
else:
75
-
if self.filename is not self.stdin_display_name:
76
-
file_path = self.filename
77
-
else:
78
-
file_path = None
79
-
with OutputCapture() as buffer:
80
-
sort_result = SortImports(
81
-
file_path=file_path,
82
-
file_contents=''.join(self.lines),
83
-
check=True,
84
-
settings_path=settings_file,
85
-
show_diff=True,
86
-
)
87
-
traceback = self._format_isort_output(buffer)
88
-
89
-
for line_num, message in self.sortimports_linenum_msg(sort_result):
90
-
if self.show_traceback:
91
-
message += traceback
92
-
yield line_num, 0, message, type(self)
93
-
94
-
def search_isort_config(self):
95
-
# type: () -> Optional[str] # noqa: F821
96
-
"""Search for isort configuration all the way up to the root folder
97
-
98
-
Looks for ``.isort.cfg``, ``.editorconfig`` or ``[isort]`` section in
99
-
``setup.cfg``, ``tox.ini``, or ``.flake8`` config files.
100
-
"""
101
-
full_path = os.path.abspath(self.filename)
102
-
split_path = (os.path.dirname(full_path), True)
103
-
while split_path[1]:
104
-
config_on_file = self._search_config_on_path(split_path[0])
105
-
if config_on_file:
106
-
return config_on_file
107
-
split_path = os.path.split(split_path[0])
108
-
109
-
if self.search_current:
110
-
return self.search_isort_config_at_current()
111
-
112
-
# last attempt, check home folder
113
-
home = expanduser('~')
114
-
config_on_home = self._search_config_on_path(home)
115
-
if config_on_home:
116
-
return config_on_home
117
-
118
-
return None
119
-
120
-
def search_isort_config_at_current(self):
121
-
# type: () -> Optional[str] # noqa: F821
122
-
"""Search for isort configuration at current directory"""
123
-
return self._search_config_on_path(os.path.realpath('.'))
124
-
125
-
def _search_config_on_path(self, path):
126
-
# type: (str) -> Optional[str] # noqa: F821
127
-
"""Search for isort configuration files at the specifed path.
128
-
129
-
Args:
130
-
path: The path to search for config files on.
131
-
132
-
Return:
133
-
str: the isort config if found otherwise, None
134
-
"""
135
-
for config_file in ('.isort.cfg', '.editorconfig'):
136
-
config_file_path = os.path.join(path, config_file)
137
-
if os.path.isfile(config_file_path):
138
-
return config_file_path
139
-
140
-
# Check for '[isort]' section in other configuration files.
141
-
for config_file in ('tox.ini', 'setup.cfg', '.flake8'):
142
-
config_file_path = os.path.join(path, config_file)
143
-
config = SafeConfigParser()
144
-
config.read(config_file_path)
145
-
if 'isort' in config.sections():
146
-
return config_file_path
147
-
148
-
return None
53
+
file_path = None
54
+
with OutputCapture() as buffer:
55
+
sort_result = SortImports(
56
+
file_path=file_path,
57
+
file_contents=''.join(self.lines),
58
+
check=True,
59
+
show_diff=True,
60
+
)
61
+
traceback = self._format_isort_output(buffer)
62
+
63
+
for line_num, message in self.sortimports_linenum_msg(sort_result):
64
+
if self.show_traceback:
65
+
message += traceback
66
+
yield line_num, 0, message, type(self)
149
67
150
68
def sortimports_linenum_msg(self, sort_result):
151
69
"""Parses isort.SortImports for line number changes and message
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4