From 58671de54a9bae4a42adb52913001cd10ad63e11 Mon Sep 17 00:00:00 2001 From: Maxim Kurnikov Date: Fri, 26 Apr 2019 00:55:56 +0300 Subject: extract everything file related to upper level --- isort/compat.py | 100 ++++++++++++++++++++++++++++++++++++++++++++------------ isort/isort.py | 84 ++--------------------------------------------- 2 files changed, 83 insertions(+), 101 deletions(-) diff --git a/isort/compat.py b/isort/compat.py index e24f84de..1b3ba7cb 100644 --- a/isort/compat.py +++ b/isort/compat.py @@ -1,8 +1,11 @@ +import locale import os +import sys from typing import Any, Optional from isort import settings -from isort.isort import _SortImports +from isort.format import ask_whether_to_apply_changes_to_file, show_unified_diff +from isort.isort import _SortImports, determine_file_encoding, read_file_contents class SortImports(object): @@ -26,21 +29,86 @@ class SortImports(object): else: _settings_path = os.getcwd() - config = settings.prepare_config(_settings_path, **setting_overrides) + self.config = settings.prepare_config(_settings_path, **setting_overrides) + self.output = None - self.sorted_imports = _SortImports(file_path=file_path, + file_encoding = 'utf-8' + file_name = file_path + + self.skipped = False + + self.file_path = file_path or "" + if file_path: + file_path = os.path.abspath(file_path) + if check_skip: + if run_path and file_path.startswith(run_path): + file_name = os.path.relpath(file_path, run_path) + else: + file_name = file_path + run_path = '' + + if settings.file_should_be_skipped(file_name, self.config, run_path): + self.skipped = True + if self.config['verbose']: + print("WARNING: {0} was skipped as it's listed in 'skip' setting" + " or matches a glob in 'skip_glob' setting".format(file_path)) + file_contents = None + + if not self.skipped and not file_contents: + preferred_encoding = determine_file_encoding(file_path) + + # default encoding for open(mode='r') on the system + fallback_encoding = locale.getpreferredencoding(False) + + file_contents, used_encoding = read_file_contents(file_path, + encoding=preferred_encoding, + fallback_encoding=fallback_encoding) + if used_encoding is None: + self.skipped = True + if self.config['verbose']: + print("WARNING: {} was skipped as it couldn't be opened with the given " + "{} encoding or {} fallback encoding".format(file_path, + file_encoding, + fallback_encoding)) + else: + file_encoding = used_encoding + + if file_contents is None or ("isort:" + "skip_file") in file_contents: + self.skipped = True + # self.output = None + if write_to_stdout and file_contents: + sys.stdout.write(file_contents) + return + + self.sorted_imports = _SortImports(file_path=self.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) + config=self.config) + self.output = self.sorted_imports.output - @property - def config(self): - return self.sorted_imports.config + if show_diff or self.config['show_diff']: + show_unified_diff(file_input=file_contents, file_output=self.output, + file_path=self.file_path) + + elif write_to_stdout: + sys.stdout.write(self.output) + + elif file_name and not check: + if self.output == file_contents: + return + + if ask_to_apply: + show_unified_diff(file_input=file_contents, file_output=self.output, + file_path=self.file_path) + apply_changes = ask_whether_to_apply_changes_to_file(self.file_path) + if not apply_changes: + return + + with open(self.file_path, 'w', encoding=file_encoding, newline='') as output_file: + if not self.config['quiet']: + print("Fixing {0}".format(self.file_path)) + + output_file.write(self.output) @property def sections(self): @@ -50,14 +118,6 @@ class SortImports(object): def incorrectly_sorted(self): return self.sorted_imports.incorrectly_sorted - @property - def skipped(self) -> bool: - return self.sorted_imports.skipped - @property def length_change(self) -> int: return self.sorted_imports.length_change - - @property - def output(self): - return self.sorted_imports.output diff --git a/isort/isort.py b/isort/isort.py index 10b0744a..d955ee2a 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -26,18 +26,12 @@ OTHER DEALINGS IN THE SOFTWARE. """ import copy import itertools -import locale -import os import re -import sys from collections import OrderedDict, namedtuple -from datetime import datetime -from difflib import unified_diff -from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Mapping, Optional, Sequence, Tuple +from typing import Any, Dict, Iterable, List, Mapping, Optional, Sequence, TYPE_CHECKING, Tuple from isort import utils -from isort.format import format_natural, format_simplified, show_unified_diff, ask_whether_to_apply_changes_to_file - +from isort.format import format_natural, format_simplified from . import settings from .finders import FindersManager from .natural import nsorted @@ -60,19 +54,13 @@ if TYPE_CHECKING: class _SortImports(object): incorrectly_sorted = False - skipped = False def __init__( 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: self.config = config @@ -83,50 +71,7 @@ class _SortImports(object): self._section_comments = ["# " + value for key, value in self.config.items() if key.startswith('import_heading') and value] - 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: - if run_path and file_path.startswith(run_path): - file_name = os.path.relpath(file_path, run_path) - else: - file_name = file_path - run_path = '' - - if settings.file_should_be_skipped(file_name, self.config, run_path): - self.skipped = True - if self.config['verbose']: - print("WARNING: {0} was skipped as it's listed in 'skip' setting" - " or matches a glob in 'skip_glob' setting".format(file_path)) - file_contents = None - - if not self.skipped and not file_contents: - preferred_encoding = determine_file_encoding(file_path) - # default encoding for open(mode='r') on the system - fallback_encoding = locale.getpreferredencoding(False) - - file_contents, used_encoding = read_file_contents(file_path, - encoding=preferred_encoding, - fallback_encoding=fallback_encoding) - if used_encoding is None: - self.skipped = True - if self.config['verbose']: - print("WARNING: {} was skipped as it couldn't be opened with the given " - "{} encoding or {} fallback encoding".format(file_path, - self.file_encoding, - fallback_encoding)) - else: - self.file_encoding = used_encoding - - if file_contents is None or ("isort:" + "skip_file") in file_contents: - self.skipped = True - self.output = None - if write_to_stdout and file_contents: - sys.stdout.write(file_contents) - return - + self.file_path = file_path self.line_separator = self.determine_line_separator(file_contents) self.in_lines = file_contents.split(self.line_separator) @@ -193,29 +138,6 @@ 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']: - show_unified_diff(file_input=file_contents, file_output=self.output, - file_path=self.file_path) - - elif write_to_stdout: - sys.stdout.write(self.output) - - elif file_name and not check: - if self.output == file_contents: - return - - if ask_to_apply: - show_unified_diff(file_input=file_contents, file_output=self.output, - file_path=self.file_path) - apply_changes = ask_whether_to_apply_changes_to_file(self.file_path) - if not apply_changes: - return - - 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)) - output_file.write(self.output) - def determine_line_separator(self, file_contents: str) -> str: if self.config['line_ending']: return self.config['line_ending'] -- cgit v1.2.1