-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path,tree
More file actions
executable file
·129 lines (107 loc) · 3.55 KB
/
,tree
File metadata and controls
executable file
·129 lines (107 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
__author__ = "Dilawar Singh"
__email__ = "[email protected]"
from pathlib import Path
import functools
# CREDIT: https://stackoverflow.com/a/49912639/1805129
class DisplayablePath(object):
display_filename_prefix_middle = "├──"
display_filename_prefix_last = "└──"
display_parent_prefix_middle = " "
display_parent_prefix_last = "│ "
def __init__(self, path, parent_path, is_last):
self.path = Path(str(path))
self.parent = parent_path
self.is_last = is_last
if self.parent:
self.depth = self.parent.depth + 1
else:
self.depth = 0
@classmethod
def make_tree(
cls,
root,
*,
dir_only: bool = False,
parent=None,
is_last=False,
max_depth: int = -1,
):
root = Path(str(root))
criteria = functools.partial(
cls._default_criteria, dir_only=dir_only, max_depth=max_depth
)
displayable_root = cls(root, parent, is_last)
yield displayable_root
children = sorted(
list(path for path in root.iterdir() if criteria(path)),
key=lambda s: str(s).lower(),
)
count = 1
for path in children:
is_last = count == len(children)
try:
if path.is_dir():
yield from cls.make_tree(
path,
parent=displayable_root,
is_last=is_last,
dir_only=dir_only,
max_depth=max_depth,
)
else:
yield cls(path, displayable_root, is_last)
count += 1
except Exception as e:
# its rare but can happen.
pass
@classmethod
def _default_criteria(cls, path, dir_only: bool = False, max_depth: int = -1):
if dir_only:
return path.is_dir()
if max_depth > 0 and path.depth >= max_depth:
return False
return True
@property
def displayname(self):
if self.path.is_dir():
return self.path.name + "/"
return self.path.name
def displayable(self):
if self.parent is None:
return self.displayname
_filename_prefix = (
self.display_filename_prefix_last
if self.is_last
else self.display_filename_prefix_middle
)
parts = ["{!s} {!s}".format(_filename_prefix, self.displayname)]
parent = self.parent
while parent and parent.parent is not None:
parts.append(
self.display_parent_prefix_middle
if parent.is_last
else self.display_parent_prefix_last
)
parent = parent.parent
return "".join(reversed(parts))
def main():
import argparse
parser = argparse.ArgumentParser(
description="Like GNU tree (barebone) but in Python"
)
parser.add_argument("DIR", nargs="?", default=Path("."), help="directory")
parser.add_argument(
"--dir-only", "-d", action="store_true", help="List only subdirectories."
)
parser.add_argument(
"--max-depth", "-n", type=int, default=-1, help="Maximum recursion depth."
)
args = parser.parse_args()
tree = DisplayablePath.make_tree(
args.DIR, dir_only=args.dir_only, max_depth=args.max_depth
)
for path in tree:
print(path.displayable())
if __name__ == "__main__":
main()