8000 Multiple refactors on DirectoryTree and _TreeGenerator. · realpython/rptree@f729e5f · GitHub
[go: up one dir, main page]

Skip to content

Commit f729e5f

Browse files
committed
Multiple refactors on DirectoryTree and _TreeGenerator.
- Add a missing import - Add a new module-level constant to provide a new prefix On DirectoryTree - Swap the order of the argument in the class initializer - Update functions calls and rename item to entry On _TreeGenerator - Rename the first argument of the initializer to be more consistent with the current API - Turn .dir_only into a non-public attribute - Use a deque instead of a list to optimize the insertions of item at the beginning of the data structure - Rename the dir argument to directory so we can avoid name collisions with the built-in dir() function - Remove the path local variable joining - Rename path to entry so we maintain a uniform naming - Move the path argument to the beginning of the argument in _add_file(). Also, it was renamed from path to file
1 parent 6772f93 commit f729e5f

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

rptree/rptree.py

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,82 @@
44
import pathlib
55
import sys
66

7+
from collections import deque
8+
79
PIPE = "│"
810
ELBOW = "└──"
911
TEE = "├──"
1012
PIPE_PREFIX = "│ "
13+
SPACE_PREFIX = " "
1114

1215

1316
class DirectoryTree:
14-
def __init__(self, root_dir, output_file=sys.stdout, dir_only=False):
17+
def __init__(self, root_dir, dir_only=False, output_file=sys.stdout):
1518
self._output_file = output_file
1619
self._generator = _TreeGenerator(root_dir, dir_only)
1720

1821
def generate(self):
19-
tree = self._generator.generate_tree()
22+
tree = self._generator.build_tree()
2023
if self._output_file != sys.stdout:
24+
# Wrap the tree in a markdown code block
2125
tree.appendleft("```")
2226
tree.append("```")
2327
self._output_file = open(
2428
self._output_file, mode="w", encoding="UTF-8"
2529
)
2630
with self._output_file as stream:
27-
for item in tree:
28-
print(item, file=stream)
31+
for entry in tree:
32+
print(entry, file=stream)
2933

3034

3135
class _TreeGenerator:
32-
def __init__(self, root, dir_only=False):
33-
self._root = pathlib.Path(root)
34-
self.dir_only = dir_only
35-
self._tree = []
36+
def __init__(self, root_dir, dir_only=False):
37+
self._root_dir = pathlib.Path(root_dir)
38+
self._dir_only = dir_only
39+
self._tree = deque()
3640

37-
def generate_tree(self):
41+
def build_tree(self):
3842
self._tree_head()
39-
self._tree_tail(self._root)
43+
self._tree_tail(self._root_dir)
4044
return self._tree
4145

4246
def _tree_head(self):
43-
self._tree.append(f"{self._root}{os.sep}")
47+
self._tree.append(f"{self._root_dir}{os.sep}")
4448
self._tree.append(PIPE)
4549

46-
def _tree_tail(self, dir, prefix=""):
47-
entries = self._prepare_entries(dir)
50+
def _tree_tail(self, directory, prefix=""):
51+
entries = self._prepare_entries(directory)
4852
entries_count = len(entries)
4953
for index, entry in enumerate(entries):
5054
connector = ELBOW if index == entries_count - 1 else TEE
51-
path = dir.joinpath(entry)
52-
if path.is_dir():
55+
if entry.is_dir():
5356
self._add_directory(
5457
entry, index, entries_count, prefix, connector
5558
)
5659
else:
57-
self._add_file(prefix, connector, path)
60+
self._add_file(entry, prefix, connector)
5861

59-
def _prepare_entries(self, dir):
60-
entries = dir.iterdir()
61-
if self.dir_only:
62-
entries = [e for e in entries if dir.joinpath(e).is_dir()]
62+
def _prepare_entries(self, directory):
63+
entries = directory.iterdir()
64+
if self._dir_only:
65+
entries = [entry for entry in entries if entry.is_dir()]
6366
return entries
64-
entries = sorted(entries, key=lambda e: dir.joinpath(e).is_file())
67+
entries = sorted(entries, key=lambda entry: entry.is_file())
6568
return entries
6669

67-
def _add_directory(self, entry, index, entries_count, prefix, connector):
68-
self._tree.append(f"{prefix}{connector}{entry.name}{os.sep}")
70+
def _add_directory(
71+
self, directory, index, entries_count, prefix, connector
72+
):
73+
self._tree.append(f"{prefix}{connector} {directory.name}{os.sep}")
6974
if index != entries_count - 1:
7075
prefix += PIPE_PREFIX
76+
else:
77+
prefix += SPACE_PREFIX
7178
self._tree_tail(
72-
dir=entry,
79+
directory=directory,
7380
prefix=prefix,
7481
)
75-
self._tree.append(prefix)
82+
self._tree.append(prefix.rstrip())
7683

77-
def _add_file(self, prefix, connector, path):
78-
self._tree.append(f"{prefix}{connector} {path.name}")
84+
def _add_file(self, file, prefix, connector):
85+
self._tree.append(f"{prefix}{connector} {file.name}")

0 commit comments

Comments
 (0)
0