annotate python/tree2.py @ 387:0aee0da6b06b

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