1
from typing import Dict, List
3
from .glob_group import GlobGroup, GlobPattern
9
"""A file structure representation. Organized as Directory nodes that have lists of
10
their Directory children. Directories for a package are created by calling
11
:meth:`PackageImporter.file_structure`."""
13
def __init__(self, name: str, is_dir: bool):
16
self.children: Dict[str, Directory] = {}
18
def _get_dir(self, dirs: List[str]) -> "Directory":
19
"""Builds path of Directories if not yet built and returns last directory
23
dirs (List[str]): List of directory names that are treated like a path.
26
:class:`Directory`: The last Directory specified in the dirs list.
31
if dir_name not in self.children:
32
self.children[dir_name] = Directory(dir_name, True)
33
return self.children[dir_name]._get_dir(dirs[1:])
35
def _add_file(self, file_path: str):
36
"""Adds a file to a Directory.
39
file_path (str): Path of file to add. Last element is added as a file while
40
other paths items are added as directories.
42
*dirs, file = file_path.split("/")
43
dir = self._get_dir(dirs)
44
dir.children[file] = Directory(file, False)
46
def has_file(self, filename: str) -> bool:
47
"""Checks if a file is present in a :class:`Directory`.
50
filename (str): Path of file to search for.
52
bool: If a :class:`Directory` contains the specified file.
54
lineage = filename.split("/", maxsplit=1)
56
grandchildren = lineage[1] if len(lineage) > 1 else None
57
if child in self.children.keys():
58
if grandchildren is None:
61
return self.children[child].has_file(grandchildren)
65
str_list: List[str] = []
66
self._stringify_tree(str_list)
67
return "".join(str_list)
70
self, str_list: List[str], preamble: str = "", dir_ptr: str = "─── "
72
"""Recursive method to generate print-friendly version of a Directory."""
79
str_list.append(f"{preamble}{dir_ptr}{self.name}\n")
83
preamble = preamble + branch
85
preamble = preamble + space
87
file_keys: List[str] = []
88
dir_keys: List[str] = []
89
for key, val in self.children.items():
95
for index, key in enumerate(sorted(dir_keys)):
96
if (index == len(dir_keys) - 1) and len(file_keys) == 0:
97
self.children[key]._stringify_tree(str_list, preamble, last)
99
self.children[key]._stringify_tree(str_list, preamble, tee)
100
for index, file in enumerate(sorted(file_keys)):
101
pointer = last if (index == len(file_keys) - 1) else tee
102
str_list.append(f"{preamble}{pointer}{file}\n")
105
def _create_directory_from_file_list(
107
file_list: List[str],
108
include: "GlobPattern" = "**",
109
exclude: "GlobPattern" = (),
111
"""Return a :class:`Directory` file structure representation created from a list of files.
114
filename (str): The name given to the top-level directory that will be the
115
relative root for all file paths found in the file_list.
117
file_list (List[str]): List of files to add to the top-level directory.
119
include (Union[List[str], str]): An optional pattern that limits what is included from the file_list to
120
files whose name matches the pattern.
122
exclude (Union[List[str], str]): An optional pattern that excludes files whose name match the pattern.
125
:class:`Directory`: a :class:`Directory` file structure representation created from a list of files.
127
glob_pattern = GlobGroup(include, exclude=exclude, separator="/")
129
top_dir = Directory(filename, True)
130
for file in file_list:
131
if glob_pattern.matches(file):
132
top_dir._add_file(file)