summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxim Kurnikov <maxim.kurnikov@gmail.com>2019-04-25 22:15:02 +0300
committerMaxim Kurnikov <maxim.kurnikov@gmail.com>2019-04-25 22:15:02 +0300
commit403fde70b2d3c0898bd437dba92c8375d7f6418f (patch)
tree62aa2088fe174ab54fc50367fe201c457f06b6e7
parent4c20eb67a72a72a5634ca0eda7db39f7a7f6a267 (diff)
downloadisort-403fde70b2d3c0898bd437dba92c8375d7f6418f.tar.gz
extract reading configuration
-rw-r--r--isort/compat.py22
-rw-r--r--isort/isort.py31
2 files changed, 35 insertions, 18 deletions
diff --git a/isort/compat.py b/isort/compat.py
index 07da7371..e24f84de 100644
--- a/isort/compat.py
+++ b/isort/compat.py
@@ -1,5 +1,7 @@
+import os
from typing import Any, Optional
+from isort import settings
from isort.isort import _SortImports
@@ -17,8 +19,24 @@ class SortImports(object):
check_skip: bool = True,
**setting_overrides: Any
):
- self.sorted_imports = _SortImports(file_path, file_contents, write_to_stdout, check, show_diff, settings_path,
- ask_to_apply, run_path, check_skip, **setting_overrides)
+ _settings_path = settings_path
+ if _settings_path is None:
+ if file_path:
+ _settings_path = os.path.dirname(os.path.abspath(file_path))
+ else:
+ _settings_path = os.getcwd()
+
+ config = settings.prepare_config(_settings_path, **setting_overrides)
+
+ self.sorted_imports = _SortImports(file_path=file_path,
+ file_contents=file_contents,
+ write_to_stdout=write_to_stdout,
+ check=check,
+ show_diff=show_diff,
+ ask_to_apply=ask_to_apply,
+ run_path=run_path,
+ check_skip=check_skip,
+ config=config)
@property
def config(self):
diff --git a/isort/isort.py b/isort/isort.py
index 4df1808c..3776bb01 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -61,23 +61,18 @@ class _SortImports(object):
skipped = False
def __init__(
- self,
- file_path: Optional[str] = None,
- file_contents: Optional[str] = None,
- write_to_stdout: bool = False,
- check: bool = False,
- show_diff: bool = False,
- settings_path: Optional[str] = None,
- ask_to_apply: bool = False,
- run_path: str = '',
- check_skip: bool = True,
- **setting_overrides: Any
+ self, *,
+ config: Dict[str, Any],
+ file_path: Optional[str] = None,
+ file_contents: Optional[str] = None,
+ write_to_stdout: bool = False,
+ check: bool = False,
+ show_diff: bool = False,
+ ask_to_apply: bool = False,
+ run_path: str = '',
+ check_skip: bool = True
) -> None:
- if not settings_path and file_path:
- settings_path = os.path.dirname(os.path.abspath(file_path))
- settings_path = settings_path or os.getcwd()
-
- self.config = settings.prepare_config(settings_path, **setting_overrides)
+ self.config = config
self.place_imports = {} # type: Dict[str, List[str]]
self.import_placements = {} # type: Dict[str, str]
@@ -89,6 +84,7 @@ class _SortImports(object):
self.file_encoding = 'utf-8'
file_name = file_path
self.file_path = file_path or ""
+
if file_path:
file_path = os.path.abspath(file_path)
if check_skip:
@@ -188,6 +184,7 @@ class _SortImports(object):
print("ERROR: {0} File contains syntax errors.".format(self.file_path))
return
+
if check:
check_output = self.output
check_against = file_contents
@@ -202,6 +199,7 @@ class _SortImports(object):
print("ERROR: {0} Imports are incorrectly sorted.".format(self.file_path))
self.incorrectly_sorted = True
+
if show_diff or self.config['show_diff']:
self._show_diff(file_contents)
elif write_to_stdout:
@@ -219,6 +217,7 @@ class _SortImports(object):
return
if answer in ('quit', 'q'):
sys.exit(1)
+
with open(self.file_path, 'w', encoding=self.file_encoding, newline='') as output_file:
if not self.config['quiet']:
print("Fixing {0}".format(self.file_path))