Eclectic Media Git ptree / 6a92ac3
Added an "Excluded Paths" argument UPDATES TO TAG V0.2 Ariana Giroux 1 year, 10 months ago
1 changed file(s) with 20 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
1111 from argparse import ArgumentParser
1212
1313
14 def tree(origin_path, padding, print_files=False, truncate_files=False, truncate_dirs=False):
14 def tree(origin_path, padding, print_files=False, exclude_path=None, truncate_files=False, truncate_dirs=False):
1515 """ Traverses the file system, outputting file structure as it goes.
1616
1717 :origin_path: The path to start the traversal from.
2424 files = []
2525
2626 dirs = [x for x in listdir(origin_path) if isdir(origin_path + sep + x)]
27 if exclude_path is not None:
28 for path in exclude_path:
29 if dirs.count(path):
30 dirs.remove(path)
31
2732 files = [x for x in listdir(origin_path) if not isdir(origin_path + sep + x)]
2833
2934 if print_files:
4449
4550 if truncate_dirs:
4651 if len(dirs) < 3:
47 tree(path, padding + '|', print_files, truncate_files, truncate_dirs)
52 tree(path, padding + '|', print_files, exclude_path, truncate_files, truncate_dirs)
4853 else:
4954 print(padding + '+-' + basename(abspath(dir)) + '/')
5055
5358 break
5459
5560 else:
56 tree(path, padding + '|', print_files, truncate_files, truncate_dirs)
61 tree(path, padding + '|', print_files, exclude_path, truncate_files, truncate_dirs)
5762
5863
5964 def _main():
7075 parser.add_argument('-f', '--print_files', action='store_true',
7176 help='Output files alongside directories.')
7277
78 parser.add_argument('-e', '--exclude_paths', type=str,
79 help='A comma separated list of paths to exclude from the'
80 'traversal. (Where \'.git,__pycache__\' yields a list with '
81 '\'.git\' and \'__pycache__\' as separate paths)')
82
7383 parser.add_argument('-F', '--truncate_files', action='store_true',
7484 help='Only output 3 files of any given level. '
7585 '(Implies `-f`)')
8292 if parsed.truncate_files:
8393 parsed.print_files = True
8494
95 exclude_path = parsed.exclude_paths.split(',')
96
8597 if isdir(parsed.path):
86 tree(parsed.path, ' ', parsed.print_files, parsed.truncate_files, parsed.truncate_directories)
98 tree(parsed.path, ' ', parsed.print_files, exclude_path, parsed.truncate_files, parsed.truncate_directories)
8799 else:
88100 print('%s is not a valid path...' % parsed.path)
101
102
103 if __name__ == '__main__':
104 _main()