Mercurial > hg > config
annotate python/tree2.py @ 386:bea5f2fe4ea4
it works even more!
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 26 Jul 2013 14:58:18 -0700 |
parents | 6ef0ea2f10e3 |
children | 0aee0da6b06b |
rev | line source |
---|---|
382 | 1 #!/usr/bin/env python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 tree in python | |
6 """ | |
7 | |
8 import optparse | |
9 import os | |
10 import sys | |
11 | |
385 | 12 LINE = '|' |
13 ITEM = '+' | |
14 END = '\\' | |
15 # | |
16 LINE = '│' | |
17 ITEM = '├' | |
18 END = '└' | |
382 | 19 |
20 def depth(directory): | |
21 directory = os.path.abspath(directory) | |
22 level = 0 | |
23 while True: | |
24 directory, remainder = os.path.split(directory) | |
25 level += 1 | |
26 if not remainder: | |
27 break | |
28 return level | |
29 | |
386 | 30 def tree(directory, sort_key=lambda x: x.lower()): |
383
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
31 |
385 | 32 retval = [] |
383
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
33 indent = [] |
384 | 34 last = {} |
386 | 35 top = depth(directory) |
36 | |
382 | 37 for dirpath, dirnames, filenames in os.walk(directory, topdown=True): |
383
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
38 |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
39 abspath = os.path.abspath(dirpath) |
384 | 40 basename = os.path.basename(abspath) |
41 parent = os.path.dirname(abspath) | |
383
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
42 level = depth(abspath) - top |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
43 |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
44 # sort articles of interest |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
45 for resource in (dirnames, filenames): |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
46 resource[:] = sorted(resource, key=sort_key) |
382 | 47 |
385 | 48 files_end = ITEM |
49 dirpath_marker = ITEM | |
50 | |
51 if level > len(indent): | |
52 indent.append(LINE) | |
53 indent = indent[:level] | |
54 | |
383
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
55 if dirnames: |
385 | 56 files_end = ITEM |
383
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
57 |
385 | 58 last[abspath] = dirnames[-1] |
59 else: | |
60 files_end = END | |
384 | 61 |
385 | 62 if last.get(parent) == os.path.basename(abspath): |
63 # last directory of parent | |
64 dirpath_mark = END | |
65 indent[-1] = ' ' | |
66 elif not indent: | |
67 dirpath_mark = '' | |
68 else: | |
69 dirpath_mark = ITEM | |
70 | |
71 retval.append('%s%s%s'% (''.join(indent[:-1]), dirpath_mark, basename)) | |
72 if filenames: | |
73 last_file = filenames[-1] | |
74 retval.extend([('%s%s%s' % (str_indent, | |
75 files_end if filename == last_file else ITEM, | |
76 filename)) | |
77 for index, filename in enumerate(filenames)]) | |
78 | |
382 | 79 return '\n'.join(retval) |
80 | |
81 def main(args=sys.argv[1:]): | |
82 | |
83 usage = '%prog [options]' | |
84 parser = optparse.OptionParser(usage=usage, description=__doc__) | |
85 options, args = parser.parse_args(args) | |
86 if not args: | |
87 args = ['.'] | |
88 | |
89 not_directory = [arg for arg in args | |
90 if not os.path.isdir(arg)] | |
91 if not_directory: | |
92 parser.error("Not a directory: %s" % (', '.join(not_directory))) | |
93 | |
94 for arg in args: | |
95 print (tree(arg)) | |
96 | |
97 if __name__ == '__main__': | |
98 main() |