Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit bcb154d

Browse files
authored
Merge pull request #18 from ESSS/work_with_isort_cfg
Making the fix format tool work with an .isort.cfg in the filesystem …
2 parents c2a854d + 009222a commit bcb154d

File tree

3 files changed

+44
-45
lines changed

3 files changed

+44
-45
lines changed

.isort.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[settings]
2+
line_length=100
3+
multi_line_output=4
4+
use_parentheses=true
5+
known_standard_library=Bastion,CGIHTTPServer,DocXMLRPCServer,HTMLParser,MimeWriter,SimpleHTTPServer,UserDict,UserList,UserString,aifc,antigravity,ast,audiodev,bdb,binhex,cgi,chunk,code,codeop,colorsys,cookielib,copy_reg,dummy_thread,dummy_threading,formatter,fpformat,ftplib,genericpath,htmlentitydefs,htmllib,httplib,ihooks,imghdr,imputil,keyword,macpath,macurl2path,mailcap,markupbase,md5,mimetools,mimetypes,mimify,modulefinder,multifile,mutex,netrc,new,nntplib,ntpath,nturl2path,numbers,opcode,os2emxpath,pickletools,popen2,poplib,posixfile,posixpath,pty,py_compile,quopri,repr,rexec,rfc822,runpy,sets,sgmllib,sha,sndhdr,sre,sre_compile,sre_constants,sre_parse,ssl,stat,statvfs,stringold,stringprep,sunau,sunaudio,symbol,symtable,telnetlib,this,toaiff,token,tokenize,tty,types,user,uu,wave,xdrlib,xmllib
6+
known_third_party=six,six.moves,sip

esss_fix_format/cli.py

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,6 @@
66

77
import click
88

9-
10-
# This is a complete list of all modules in our stdlib which are not already known to isort
11-
# This is a workaround for https://github.com/timothycrosley/isort/issues/464
12-
all_stdlib_modules = ["Bastion", "CGIHTTPServer", "DocXMLRPCServer", "HTMLParser", "MimeWriter",
13-
"SimpleHTTPServer", "UserDict", "UserList", "UserString", "aifc",
14-
"antigravity", "ast",
15-
"audiodev", "bdb", "binhex", "cgi", "chunk", "code", "codeop", "colorsys",
16-
"cookielib", "copy_reg", "email",
17-
"dummy_thread", "dummy_threading", "formatter", "fpformat", "ftplib",
18-
"genericpath",
19-
"htmlentitydefs", "htmllib", "httplib", "ihooks", "imghdr", "imputil",
20-
"keyword", "macpath", "macurl2path",
21-
"mailcap", "markupbase", "md5", "mimetools", "mimetypes", "mimify",
22-
"modulefinder", "multifile", "mutex",
23-
"netrc", "new", "nntplib", "ntpath", "nturl2path", "numbers", "opcode",
24-
"os2emxpath", "pickletools", "popen2", "poplib", "posixfile", "posixpath",
25-
"pty",
26-
"py_compile", "quopri", "repr", "rexec", "rfc822", "runpy", "sets", "sgmllib",
27-
"sha", "sndhdr", "sre",
28-
"sre_compile", "sre_constants", "sre_parse", "ssl", "stat", "statvfs",
29-
"stringold",
30-
"stringprep", "sunau", "sunaudio", "symbol", "symtable", "telnetlib", "this",
31-
"toaiff", "token",
32-
"tokenize", "tty", "types", "user", "uu", "wave", "xdrlib", "xmllib"]
33-
34-
ISORT_CONFIG = {
35-
'line_length': 100,
36-
'multi_line_output': 4, # 4-vert-grid
37-
'use_parentheses': True,
38-
# This is a workaround for https://github.com/timothycrosley/isort/issues/464
39-
'known_standard_library': all_stdlib_modules,
40-
'known_third_party': ["six", "six.moves", "sip"],
41-
}
42-
439
PATTERNS = {
4410
'*.py',
4511
'*.cpp',
@@ -74,7 +40,7 @@ def should_format(filename):
7440
help='use modified files from git')
7541
def main(files_or_directories, check, stdin, commit):
7642
"""Fixes and checks formatting according to ESSS standards."""
77-
import isort
43+
import isort.settings
7844
if stdin:
7945
files = [x.strip() for x in click.get_text_stream('stdin').readlines()]
8046
elif commit:
@@ -119,7 +85,17 @@ def main(files_or_directories, check, stdin, commit):
11985
extension = os.path.normcase(os.path.splitext(filename)[1])
12086

12187
if extension == '.py':
122-
sorter = isort.SortImports(file_contents=new_contents, **ISORT_CONFIG)
88+
settings_path = os.path.dirname(filename)
89+
settings_loaded = isort.settings.from_path(settings_path)
90+
if settings_loaded['line_length'] < 80:
91+
# The default isort configuration has 79 chars, so, if the passed
92+
# does not have more than that, complain that .isort.cfg is not configured.
93+
msg = ': ERROR .isort.cfg not available in repository (or line_length config < 80).'
94+
error_msg = click.format_filename(filename) + msg
95+
click.secho(error_msg, fg='red')
96+
errors.append(error_msg)
97+
98+
sorter = isort.SortImports(file_contents=new_contents, settings_path=settings_path)
12399
# strangely, if the entire file is skipped by an "isort:skip_file"
124100
# instruction in the docstring, SortImports doesn't even contain an
125101
# "output" attribute

tests/test_esss_fix_format.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@
1515

1616

1717
@pytest.fixture
18-
def input_file(tmpdir):
18+
def sort_cfg_to_tmpdir(tmpdir):
19+
import shutil
20+
shutil.copyfile(
21+
os.path.join(os.path.dirname(__file__), '..', '.isort.cfg'),
22+
str(tmpdir.join('.isort.cfg')))
23+
24+
25+
@pytest.fixture
26+
def input_file(tmpdir, sort_cfg_to_tmpdir):
1927
# imports out-of-order included in example so isort detects as necessary to change
2028
source = textwrap.dedent(
2129
'''\
@@ -34,6 +42,7 @@ def input_file(tmpdir):
3442
)
3543
filename = tmpdir.join('test.py')
3644
filename.write(source)
45+
3746
return filename
3847

3948

@@ -106,7 +115,7 @@ def test_fix_whitespace(input_file):
106115
assert obtained == expected
107116

108117

109-
def test_imports(tmpdir):
118+
def test_imports(tmpdir, sort_cfg_to_tmpdir):
110119
source = textwrap.dedent('''\
111120
import pytest
112121
import sys
@@ -146,7 +155,7 @@ def test_unknown_extension(input_file):
146155
output.fnmatch_lines(str(new_filename) + ': Unknown file type')
147156

148157

149-
def test_filename_without_wildcard(tmpdir):
158+
def test_filename_without_wildcard(tmpdir, sort_cfg_to_tmpdir):
150159
filename = tmpdir.join('CMakeLists.txt')
151160
filename.write('\t#\n')
152161
output = run([str(filename)], expected_exit=0)
@@ -176,7 +185,7 @@ def check_output(cmd, *args, **kwargs):
176185
]
177186

178187

179-
def test_input_invalid_codec(tmpdir):
188+
def test_input_invalid_codec(tmpdir, sort_cfg_to_tmpdir):
180189
"""Display error summary when we fail to open a file"""
181190
filename = tmpdir.join('test.py')
182191
filename.write(u'hello world'.encode('UTF-16'), 'wb')
@@ -186,14 +195,14 @@ def test_input_invalid_codec(tmpdir):
186195
output.fnmatch_lines(str(filename) + ': ERROR (Unicode*')
187196

188197

189-
def test_empty_file(tmpdir):
198+
def test_empty_file(tmpdir, sort_cfg_to_tmpdir):
190199
"""Ensure files with a single empty line do not raise an error"""
191200
filename = tmpdir.join('test.py')
192201
filename.write(u'\r\n', 'w')
193202
run([str(filename)], expected_exit=0)
194203

195204

196-
def test_skip_entire_file(tmpdir):
205+
def test_skip_entire_file(tmpdir, sort_cfg_to_tmpdir):
197206
"""Check that a module-level isort:skip_file correctly skips that file"""
198207
source = textwrap.dedent('''\
199208
"""
@@ -208,7 +217,7 @@ def test_skip_entire_file(tmpdir):
208217
assert filename.read() == source
209218

210219

211-
def test_isort_bug_with_comment_headers(tmpdir):
220+
def test_isort_bug_with_comment_headers(tmpdir, sort_cfg_to_tmpdir):
212221
source = textwrap.dedent("""\
213222
'''
214223
See README.md for usage.
@@ -231,7 +240,7 @@ def Ask(question, answers):
231240
check_valid_file(filename)
232241

233242

234-
def test_missing_builtins(tmpdir):
243+
def test_missing_builtins(tmpdir, sort_cfg_to_tmpdir):
235244
source = textwrap.dedent("""\
236245
import thirdparty
237246
import os
@@ -253,7 +262,7 @@ def test_missing_builtins(tmpdir):
253262
""")
254263

255264

256-
def test_force_parentheses(tmpdir):
265+
def test_force_parentheses(tmpdir, sort_cfg_to_tmpdir):
257266
source = (
258267
'from shutil import copyfileobj, copyfile, copymode, copystat,\\\n'
259268
' copymode, ignore_patterns, copytree, rmtree, move'
@@ -271,6 +280,14 @@ def test_force_parentheses(tmpdir):
271280
assert obtained == expected
272281

273282

283+
def test_no_isort_cfg(tmpdir):
284+
filename = tmpdir.join('test.py')
285+
filename.write('import os', 'w')
286+
output = run([str(filename)], expected_exit=1)
287+
output.fnmatch_lines(
288+
r'*ERROR .isort.cfg not available in repository (or line_length config < 80).')
289+
290+
274291
def run(args, expected_exit):
275292
from _pytest.pytester import LineMatcher
276293
runner = CliRunner()

0 commit comments

Comments
 (0)