Eclectic Media Git ptree / b9ec292
Added long level truncation, sanitized function UPDATES TO V0.1 Needs testing! Ariana Giroux 1 year, 8 months ago
1 changed file(s) with 60 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
77
88 from os import listdir, sep
99 from os.path import abspath, basename, isdir
10 from sys import argv
1110
12 def tree(dir, padding, print_files=False):
13 print(padding[:-1] + '+-' + basename(abspath(dir)) + '/')
11 from argparse import ArgumentParser
12
13
14 def tree(origin_path, padding, print_files=False, truncate_files=False, truncate_dirs=False):
15 """ :truncate_dirs: If true, don't follow dir depths deeper than 3. """
16 print(padding[:-1] + '+-' + basename(abspath(origin_path)) + '/')
1417 padding = padding + ' '
1518 files = []
19
20 dirs = [x for x in listdir(origin_path) if isdir(origin_path + sep + x)]
21 files = [x for x in listdir(origin_path) if not isdir(origin_path + sep + x)]
22
1623 if print_files:
17 files = listdir(dir)
18 else:
19 files = [x for x in listdir(dir) if isdir(dir + sep + x)]
20 count = 0
21 for file in files:
22 count += 1
23 print(padding + '|')
24 path = dir + sep + file
25 if isdir(path):
26 if count == len(files):
27 tree(path, padding + ' ', print_files)
28 else:
29 tree(path, padding + '|', print_files)
30 else:
24 padding += '| '
25
26 for i, file in enumerate(files):
3127 print(padding + '+-' + file)
3228
33 def usage():
34 return '''Usage: %s [-f] <PATH>
35 Print tree structure of path specified.
36 Options:
37 -f Print files as well as directories
38 PATH Path to process''' % basename(argv[0])
29 if len(files) > 3 and i + 1 == 3 and truncate_files:
30 print(padding + '+- ...')
31 break
32
33 padding = padding[:-2]
34
35 # count = 0
36 for i, dir in enumerate(dirs):
37
38 path = origin_path + sep + dir
39
40 if truncate_dirs:
41 if len(dirs) < 3:
42 tree(path, padding + '|', print_files, truncate_files, truncate_dirs)
43 else:
44 print(padding + '+-' + basename(abspath(dir)) + '/')
45
46 if i + 1 == 3:
47 print(padding + '+- ...')
48 break
49
50 else:
51 tree(path, padding + '|', print_files, truncate_files, truncate_dirs)
52
3953
4054 def main():
41 if len(argv) == 1:
42 print(usage())
43 elif len(argv) == 2:
44 # print just directories
45 path = argv[1]
46 if isdir(path):
47 tree(path, ' ')
48 else:
49 print('ERROR: \'' + path + '\' is not a directory')
50 elif len(argv) == 3 and argv[1] == '-f':
51 # print directories and files
52 path = argv[2]
53 if isdir(path):
54 tree(path, ' ', True)
55 else:
56 print('ERROR: \'' + path + '\' is not a directory')
55 parser = ArgumentParser()
56
57 parser.add_argument('path', default='./', type=str,
58 help='The path to map.')
59
60 parser.add_argument('-f', '--print_files', action='store_true',
61 help='Output files alongside directories.')
62
63 parser.add_argument('-F', '--truncate_files', action='store_true',
64 help='Only output 3 files of any given level. '
65 '(Implies `-f`)')
66
67 parser.add_argument('-D', '--truncate_directories', action='store_true',
68 help='Only output 3 directories of any given level.')
69
70 parsed = parser.parse_args()
71
72 if parsed.truncate_files:
73 parsed.print_files = True
74
75 if isdir(parsed.path):
76 tree(parsed.path, ' ', parsed.print_files, parsed.truncate_files, parsed.truncate_directories)
5777 else:
58 print(usage())
78 print('%s is not a valid path...' % parsed.path)
5979
6080 if __name__ == '__main__':
6181 main()