Skip to content

Commit d134aa0

Browse files
committedOct 3, 2021
moved trie to utils.py
1 parent 26819c0 commit d134aa0

File tree

4 files changed

+65
-41
lines changed

4 files changed

+65
-41
lines changed
 

‎isort/api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def check_file(
332332
if config.verbose:
333333
print(f"{config_info[0]} used for file {filename}")
334334

335-
file_config = config_info[1]
335+
file_config = Config(**config_info[1])
336336

337337
with io.File.read(filename) as source_file:
338338
return check_stream(
@@ -400,7 +400,7 @@ def sort_file(
400400
if config.verbose:
401401
print(f"{config_info[0]} used for file {filename}")
402402

403-
file_config = config_info[1]
403+
file_config = Config(**config_info[1])
404404

405405
with io.File.read(filename) as source_file:
406406
actual_file_path = file_path or source_file.path

‎isort/main.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from .format import create_terminal_printer
1616
from .logo import ASCII_ART
1717
from .profiles import profiles
18-
from .settings import VALID_PY_TARGETS, Config, Trie, find_all_configs
18+
from .settings import VALID_PY_TARGETS, Config, find_all_configs
19+
from .utils import Trie
1920
from .wrap_modes import WrapModes
2021

2122
DEPRECATED_SINGLE_DASH_ARGS = {

‎isort/settings.py

+2-38
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from .profiles import profiles
4242
from .sections import DEFAULT as SECTION_DEFAULTS
4343
from .sections import FIRSTPARTY, FUTURE, LOCALFOLDER, STDLIB, THIRDPARTY
44+
from .utils import Trie
4445
from .wrap_modes import WrapModes
4546
from .wrap_modes import from_string as wrap_mode_from_string
4647

@@ -726,43 +727,6 @@ def _parse_known_pattern(self, pattern: str) -> List[str]:
726727
return patterns
727728

728729

729-
class TrieNode:
730-
def __init__(self) -> None:
731-
self.nodes: Dict[str, TrieNode] = {}
732-
self.config_info: Tuple[str, Config] = ("", DEFAULT_CONFIG)
733-
734-
735-
class Trie:
736-
def __init__(self) -> None:
737-
self.root: TrieNode = TrieNode()
738-
739-
def _insert(self, config_path: str, config_file: str, config_data: Dict[str, Any]) -> None:
740-
resolved_config_path_as_tuple = Path(config_path).parent.resolve().parts
741-
742-
temp = self.root
743-
744-
for path in resolved_config_path_as_tuple:
745-
if path not in temp.nodes:
746-
temp.nodes[path] = TrieNode()
747-
748-
temp = temp.nodes[path]
749-
750-
temp.config_info = (config_file, Config(**config_data))
751-
752-
def _search(self, filename: str) -> Tuple[str, Config]:
753-
resolved_file_path_as_tuple = Path(filename).resolve().parts
754-
755-
temp = self.root
756-
757-
for path in resolved_file_path_as_tuple:
758-
if path not in temp.nodes:
759-
return temp.config_info
760-
761-
temp = temp.nodes[path]
762-
763-
return temp.config_info
764-
765-
766730
def _get_str_to_type_converter(setting_name: str) -> Union[Callable[[str], Any], Type[Any]]:
767731
type_converter: Union[Callable[[str], Any], Type[Any]] = type(
768732
_DEFAULT_SETTINGS.get(setting_name, "")
@@ -824,7 +788,7 @@ def _find_config(path: str) -> Tuple[str, Dict[str, Any]]:
824788

825789
@lru_cache()
826790
def find_all_configs(src_paths: Tuple[str]) -> Trie:
827-
trie_root = Trie()
791+
trie_root = Trie("default", DEFAULT_CONFIG.__dict__)
828792

829793
for path in src_paths:
830794
for (dirpath, _, _) in os.walk(path):

‎isort/utils.py

+59
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
11
import os
22
import sys
3+
from pathlib import Path
4+
from typing import Any, Dict, Optional, Tuple
5+
6+
7+
class TrieNode:
8+
def __init__(self, config_file: str = "", config_data: Optional[Dict[str, Any]] = None) -> None:
9+
if not config_file:
10+
config_file = ""
11+
12+
if not config_data:
13+
config_data = {}
14+
15+
self.nodes: Dict[str, TrieNode] = {}
16+
self.config_info: Tuple[str, Dict[str, Any]] = (config_file, config_data)
17+
18+
19+
class Trie:
20+
"""
21+
A prefix tree to store the paths of all config files and to search the nearest config
22+
associated with each file
23+
"""
24+
25+
def __init__(self, config_file: str = "", config_data: Optional[Dict[str, Any]] = None) -> None:
26+
self.root: TrieNode = TrieNode(config_file, config_data)
27+
28+
def _insert(self, config_path: str, config_file: str, config_data: Dict[str, Any]) -> None:
29+
resolved_config_path_as_tuple = Path(config_path).parent.resolve().parts
30+
31+
temp = self.root
32+
33+
for path in resolved_config_path_as_tuple:
34+
if path not in temp.nodes:
35+
temp.nodes[path] = TrieNode()
36+
37+
temp = temp.nodes[path]
38+
39+
temp.config_info = (config_file, config_data)
40+
41+
def _search(self, filename: str) -> Tuple[str, Dict[str, Any]]:
42+
"""
43+
Returns the closest config relative to filename by doing a depth
44+
first search on the prefix tree.
45+
"""
46+
resolved_file_path_as_tuple = Path(filename).resolve().parts
47+
48+
temp = self.root
49+
50+
last_stored_config: Tuple[str, Dict[str, Any]] = ("", {})
51+
52+
for path in resolved_file_path_as_tuple:
53+
if temp.config_info[0]:
54+
last_stored_config = temp.config_info
55+
56+
if path not in temp.nodes:
57+
break
58+
59+
temp = temp.nodes[path]
60+
61+
return last_stored_config
362

463

564
def exists_case_sensitive(path: str) -> bool:

0 commit comments

Comments
 (0)
Please sign in to comment.