Skip to content

Commit 7e6f219

Browse files
committed
Add documentation to glob tree etc.
1 parent f45aa5f commit 7e6f219

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

basic_features/glob_tree.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@
1616
def glob_tree(
1717
file_tree: mobase.IFileTree, pattern: str, path: str = ""
1818
) -> Iterable[tuple[str, mobase.FileTreeEntry]]:
19-
parts = parse_pattern(pattern)
19+
"""Find paths/entries matching a unix style glob pattern.
20+
21+
If `glob_pattern` ends with a slash ('/' or '\\'), only directories are yielded.
22+
23+
Args:
24+
file_tree: `IFileTree`
25+
_pattern: Glob pattern to match the entries path.
26+
path (optional): Root path for the given file tree. Defaults to "".
27+
28+
Yields:
29+
(path, entry)
30+
"""
31+
parts = parse_glob_pattern(pattern)
2032
if path and not path.endswith("/"):
2133
path += "/"
2234
if pattern.endswith(("/", "\\")):
@@ -27,7 +39,7 @@ def glob_tree(
2739
yield from _glob_tree(file_tree, path, parts)
2840

2941

30-
def parse_pattern(pattern: str) -> list[PatternPart]:
42+
def parse_glob_pattern(pattern: str) -> list[PatternPart]:
3143
if not pattern:
3244
raise ValueError(f"Unacceptable pattern: {pattern}")
3345
if pattern.startswith(("/", "\\")):
@@ -49,14 +61,14 @@ def parse_pattern(pattern: str) -> list[PatternPart]:
4961
)
5062
elif ".." in part:
5163
raise ValueError(f".. parent selector not supported: {pattern}")
52-
elif has_glob_pattern(part):
64+
elif has_wildcards(part):
5365
res.append(re.compile(fnmatch.translate(part)))
5466
else:
5567
res.append(part)
5668
return res
5769

5870

59-
def has_glob_pattern(test_str: str):
71+
def has_wildcards(test_str: str):
6072
return _glob_pattern_matcher.search(test_str)
6173

6274

@@ -122,6 +134,15 @@ def _glob_tree(
122134
def all_tree_entries(
123135
file_tree: mobase.IFileTree, path_prefix: str = ""
124136
) -> Iterable[tuple[str, mobase.FileTreeEntry]]:
137+
"""Get all tree entries recursively.
138+
139+
Args:
140+
file_tree: `IFileTree`
141+
path_prefix (optional): Prepend to each returned path. Defaults to "".
142+
143+
Yields:
144+
(path, entry)
145+
"""
125146
for entry in file_tree:
126147
sub_path = path_prefix + entry.name()
127148
yield sub_path, entry

0 commit comments

Comments
 (0)