-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtree.py
More file actions
executable file
·242 lines (206 loc) · 6.79 KB
/
tree.py
File metadata and controls
executable file
·242 lines (206 loc) · 6.79 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/python3
"""
Usage: tree.py [options] [directory]
Options:
-a, --all Include hidden files
-gitignore Exclude files listed in .gitignore.
-sn, --sortbyname Sort files by name.
-r, --reverse Sort files by name in reverse.
-fo, --filesonly Only show files.
-do, --dirsonly Only show directories
-L Max depth the directory tree reaches.
-h, --help Show this help message and exit.
All Rights Reserved
If you have any questions, or you need help please contact the developers:
"""
from os.path import exists, join
from os import walk, getcwd
from sys import argv
from dataclasses import dataclass
V_PIPE = '│'
MID_NODE = '├'
FINAL_NODE = '└'
H_PIPE = '──'
@dataclass
class Flag:
"""
Flag dataclass to hold the values of arguments
passed by the user
"""
all: bool = False
gitignore: bool = False
sortbyname: bool = False
reverse: bool = False
help: bool = False
dirs_only: bool = False
files_only: bool = False
filesfirst: bool = False
depth: int = 0
def __init__(self, **kwargs):
"""
Sets all values once given
whatever is passed in kwargs
"""
for key, value in kwargs.items():
object.__setattr__(self, key, value)
def __setattr__(self, *args):
"""
Disables setting attributes via
item.prop = val or item['prop'] = val
"""
raise TypeError(
'Immutable objects cannot have properties set after init')
def __delattr__(self, *args):
"""
Disables deleting properties
"""
raise TypeError('Immutable objects cannot have properties deleted')
# returns a 2d list of the form [[{is it a directory?}, name],...]
def parse_ls(pwd, flag):
"""
parse list
"""
ls_arr = []
dirs = []
files = []
for (_, dirnames, filenames) in walk(pwd):
dirs = [[False, dir] for dir in dirnames
if flag.all or not dir.startswith('.')]
files = [[True, file] for file in filenames
if flag.all or not file.startswith('.')]
# breaks the walk from yeilding other directory contents...
# we might actually use this to make the whole tool
break
if flag.filesfirst:
ls_arr.extend(files)
ls_arr.extend(dirs)
else:
ls_arr.extend(dirs)
ls_arr.extend(files)
return ls_arr
def printarr(array):
"""
print array
"""
for i in array:
for j in i:
print(j, end='')
print(" ", end='')
def tree(pwd, flags: Flag):
"""
tree
"""
if flags.help:
# basic help prinitng here
print_help()
return
print('.')
def _tree(pwd, arr, depth):
"""
_tree
"""
if not exists(pwd):
print("path doesn't exist")
return
ls_arr = parse_ls(pwd, flags)
if flags.sortbyname:
# sort list by name
if flags.reverse:
ls_arr = sorted(ls_arr, key=lambda x: x[1], reverse=True)
else:
ls_arr = sorted(ls_arr, key=lambda x: x[1])
lslen = len(ls_arr)
arr += [[MID_NODE, H_PIPE]]
depth += 1
for i in range(lslen):
if i >= lslen-1:
arr[-1][0] = FINAL_NODE
if ls_arr[i][0]: # is it a file?
if not flags.dirs_only:
printarr(arr)
print(ls_arr[i][1])
else:
if not flags.files_only:
printarr(arr)
print(ls_arr[i][1])
if flags.depth == 0 or depth < flags.depth:
if i < lslen-1:
_tree(pwd + '/' + ls_arr[i][1],
arr[:-1] + [[V_PIPE, " "]], depth)
else:
_tree(pwd + '/' + ls_arr[i][1],
arr[:-1] + [[" ", " "]], depth)
_tree(pwd, [], depth=0)
def parse_args(argv_list: list, pwd: str):
"""
parse args
"""
all_arg, gitignore, help_arg, sortbyname = False, False, False, False
reverse, files_only, dirs_only, filesfirst = False, False, False, False
depth = 0
is_pwd_set = False
if len(argv_list) <= 1:
pass
else:
for arg in argv_list[1:]:
if arg.startswith('-'):
arg = arg[1:]
if arg in ('a', '-all'):
all_arg = True
elif arg in ('h', '-help'):
help_arg = True
elif arg in ('gitignore', '-gitignore'):
gitignore = True
elif arg in ('sn', '-sortbyname'):
sortbyname = True
elif arg in ('r', '-reverse'):
sortbyname = True
reverse = True
elif arg in ('fo', '-filesonly'):
files_only = True
elif arg in ('do', '-dirsonly'):
dirs_only = True
# this has to be used like this: tree -L5 with the number
# directly next to the arg no space
elif arg.startswith('L'):
depth = int(arg[1:])
else:
help_arg = True
else:
if not is_pwd_set:
pwd = join(pwd, arg)
is_pwd_set = True
flags = Flag(all=all_arg, gitignore=gitignore, sortbyname=sortbyname,
help=help_arg, reverse=reverse, files_only=files_only,
dirs_only=dirs_only, depth=depth, filesfirst=filesfirst)
return (flags, pwd)
def print_help():
"""
print help
"""
print(r"""
_____ ____ U _____ u U _____ u
|_ " _| U | _"\ u \| ___"|/ \| ___"|/
| | \| |_) |/ | _|" | _|"
/| |\ | _ < | |___ | |___
u |_|U |_| \_\ |_____| |_____|
_// \\_ // \\_ << >> << >>
(__) (__) (__) (__) (__) (__) (__) (__) .py
A tree(1) clone written in Python.
Usage: tree.py [options] [directory]
Options:
-a, --all Include hidden files
-gitignore Exclude files listed in .gitignore.
-sn, --sortbyname Sort files by name.
-r, --reverse Sort files by name in reverse.
-fo, --filesonly Only show files.
-do, --dirsonly Only show directories
-L Max depth the directory tree reaches.
-h, --help Show this help message and exit.
All Rights Reserved
If you have any questions, or you need help please contact the developers:
""")
if __name__ == "__main__":
cwd = getcwd()
(arg_flags, cwd) = parse_args(argv, cwd)
tree(cwd, arg_flags)