From 1ba36cb21f431b2c0a4b6d8fad384e46cf78767f Mon Sep 17 00:00:00 2001 From: Maxim Kurnikov Date: Tue, 26 Feb 2019 02:20:09 +0300 Subject: extract config preparation in separate function --- isort/isort.py | 39 +++++++-------------------------------- isort/settings.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/isort/isort.py b/isort/isort.py index ca28d56d..0fd9ae82 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -34,6 +34,8 @@ from datetime import datetime from difflib import unified_diff from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Mapping, Optional, Sequence, Tuple +from isort import utils + from . import settings from .finders import FindersManager from .natural import nsorted @@ -46,7 +48,6 @@ if TYPE_CHECKING: 'straight': Dict[str, Any], 'from': Dict[str, Any] }) - CommentsDict = TypedDict('CommentsDict', { 'from': Dict[str, Any], 'straight': Dict[str, Any], @@ -74,41 +75,14 @@ class SortImports(object): settings_path = os.path.dirname(os.path.abspath(file_path)) settings_path = settings_path or os.getcwd() - self.config = settings.from_path(settings_path).copy() - for key, value in setting_overrides.items(): - access_key = key.replace('not_', '').lower() - # The sections config needs to retain order and can't be converted to a set. - if access_key != 'sections' and type(self.config.get(access_key)) in (list, tuple): - if key.startswith('not_'): - self.config[access_key] = list(set(self.config[access_key]).difference(value)) - else: - self.config[access_key] = list(set(self.config[access_key]).union(value)) - else: - self.config[key] = value - - if self.config['force_alphabetical_sort']: - self.config.update({'force_alphabetical_sort_within_sections': True, - 'no_sections': True, - 'lines_between_types': 1, - 'from_first': True}) - - indent = str(self.config['indent']) - if indent.isdigit(): - indent = " " * int(indent) - else: - indent = indent.strip("'").strip('"') - if indent.lower() == "tab": - indent = "\t" - self.config['indent'] = indent - - self.config['comment_prefix'] = self.config['comment_prefix'].strip("'").strip('"') + self.config = settings.prepare_config(settings_path, **setting_overrides) self.place_imports = {} # type: Dict[str, List[str]] self.import_placements = {} # type: Dict[str, str] self.remove_imports = [self._format_simplified(removal) for removal in self.config['remove_imports']] self.add_imports = [self._format_natural(addition) for addition in self.config['add_imports']] - self._section_comments = ["# " + value for key, value in self.config.items() if - key.startswith('import_heading') and value] + 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 @@ -143,6 +117,7 @@ class SortImports(object): self.line_separator = '\r' else: self.line_separator = '\n' + self.in_lines = file_contents.split(self.line_separator) self.original_length = len(self.in_lines) if (self.original_length > 1 or self.in_lines[:1] not in ([], [""])) or self.config['force_adds']: @@ -535,7 +510,7 @@ class SortImports(object): import_statement = new_import_statement line_length -= 1 new_import_statement = formatter(import_start, copy.copy(from_imports), - dynamic_indent, indent, line_length, comments) + dynamic_indent, indent, line_length, comments) lines = new_import_statement.split(self.line_separator) if import_statement.count(self.line_separator) == 0: return self._wrap(import_statement) diff --git a/isort/settings.py b/isort/settings.py index c1d17802..9c577556 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -184,6 +184,38 @@ def from_path(path: str) -> Dict[str, Any]: return computed_settings +def prepare_config(settings_path: str, **setting_overrides: Any) -> Dict[str, Any]: + config = from_path(settings_path).copy() + for key, value in setting_overrides.items(): + access_key = key.replace('not_', '').lower() + # The sections config needs to retain order and can't be converted to a set. + if access_key != 'sections' and type(config.get(access_key)) in (list, tuple): + if key.startswith('not_'): + config[access_key] = list(set(config[access_key]).difference(value)) + else: + config[access_key] = list(set(config[access_key]).union(value)) + else: + config[key] = value + + if config['force_alphabetical_sort']: + config.update({'force_alphabetical_sort_within_sections': True, + 'no_sections': True, + 'lines_between_types': 1, + 'from_first': True}) + + indent = str(config['indent']) + if indent.isdigit(): + indent = " " * int(indent) + else: + indent = indent.strip("'").strip('"') + if indent.lower() == "tab": + indent = "\t" + config['indent'] = indent + + config['comment_prefix'] = config['comment_prefix'].strip("'").strip('"') + return config + + def _update_settings_with_config( path: str, name: str, -- cgit v1.2.1