Mercurial > hg > config
annotate python/tree2.py @ 385:6ef0ea2f10e3
its alive
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 26 Jul 2013 13:14:19 -0700 |
parents | 5ae5ada91ac8 |
children | bea5f2fe4ea4 |
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 | |
30 def tree(directory): | |
383
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
31 |
384 | 32 sort_key=lambda x: x.lower() |
385 | 33 retval = [] |
383
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
34 top = depth(directory) |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
35 indent = [] |
384 | 36 last = {} |
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 | |
72 # if basename == 'bin': | |
73 # import pdb; pdb.set_trace() | |
74 | |
75 str_indent = ''.join(indent) | |
76 retval.append('%s%s%s'% (''.join(indent[:-1]), dirpath_mark, basename)) | |
77 if filenames: | |
78 last_file = filenames[-1] | |
79 retval.extend([('%s%s%s' % (str_indent, | |
80 files_end if filename == last_file else ITEM, | |
81 filename)) | |
82 for index, filename in enumerate(filenames)]) | |
83 | |
382 | 84 return '\n'.join(retval) |
85 | |
86 def main(args=sys.argv[1:]): | |
87 | |
88 usage = '%prog [options]' | |
89 parser = optparse.OptionParser(usage=usage, description=__doc__) | |
90 options, args = parser.parse_args(args) | |
91 if not args: | |
92 args = ['.'] | |
93 | |
94 not_directory = [arg for arg in args | |
95 if not os.path.isdir(arg)] | |
96 if not_directory: | |
97 parser.error("Not a directory: %s" % (', '.join(not_directory))) | |
98 | |
99 for arg in args: | |
100 print (tree(arg)) | |
101 | |
102 if __name__ == '__main__': | |
103 main() |