|
1 | | -import fileinput |
2 | 1 | import logging |
3 | 2 | import pathlib |
4 | 3 | from collections import defaultdict |
|
8 | 7 | import isort |
9 | 8 |
|
10 | 9 | from ni_python_styleguide import _acknowledge_existing_errors |
| 10 | +from ni_python_styleguide import _config_constants |
11 | 11 | from ni_python_styleguide import _format |
12 | 12 | from ni_python_styleguide import _utils |
13 | 13 |
|
@@ -67,42 +67,19 @@ def _split_imports_line(lines: str, *_, **__): |
67 | 67 |
|
68 | 68 | def _sort_imports(file: pathlib.Path, app_import_names): |
69 | 69 | raw = file.read_text() |
| 70 | + isort_config = isort.Config( |
| 71 | + settings_file=str(_config_constants.ISORT_CONFIG_FILE), |
| 72 | + known_first_party=filter(None, app_import_names.split(",")), |
| 73 | + ) |
70 | 74 | output = isort.code( |
71 | 75 | raw, |
72 | | - multi_line_output=3, |
73 | | - line_length=1, |
74 | | - known_first_party=filter(None, app_import_names.split(",")), |
| 76 | + config=isort_config, |
75 | 77 | ) |
76 | 78 | file.write_text(output) |
77 | 79 |
|
78 | 80 |
|
79 | | -def _handle_multiple_import_lines(bad_file: pathlib.Path): |
80 | | - multiline_string_checker = _utils.string_helpers.InMultiLineStringChecker( |
81 | | - lines=bad_file.read_text(encoding=_utils.DEFAULT_ENCODING).splitlines() |
82 | | - ) |
83 | | - with fileinput.FileInput(files=[str(bad_file)], inplace=True) as f: |
84 | | - for line_no, line in enumerate(f): |
85 | | - working_line = line |
86 | | - if multiline_string_checker.in_multiline_string(line_no + 1): |
87 | | - print(working_line, end="") |
88 | | - continue |
89 | | - working_line = _split_imports_line(working_line) |
90 | | - print(working_line, end="") |
91 | | - |
92 | | - |
93 | 81 | def _format_imports(file: pathlib.Path, app_import_names: Iterable[str]) -> None: |
94 | 82 | _sort_imports(file, app_import_names=app_import_names) |
95 | | - start_line, end_line = _utils.code_analysis.find_import_region(file) |
96 | | - file_lines = file.read_text().splitlines() |
97 | | - before = file_lines[:start_line] |
98 | | - import_lines = file_lines[start_line:end_line] |
99 | | - after = file_lines[end_line:] |
100 | | - with _utils.temp_file.multi_access_tempfile(suffix=".py") as temp_py_file: |
101 | | - temp_py_file.write_text("\n".join(import_lines)) |
102 | | - _format.format(temp_py_file, "--line-length=300") # condense any split lines |
103 | | - _handle_multiple_import_lines(temp_py_file) |
104 | | - handled_import_region = temp_py_file.read_text().splitlines() |
105 | | - file.write_text("\n".join(before + handled_import_region + after)) |
106 | 83 | _format.format(file) |
107 | 84 |
|
108 | 85 |
|
|
0 commit comments