Mercurial > hg > config
annotate python/tree2.py @ 384:5ae5ada91ac8
simple numbering
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 26 Jul 2013 09:45:46 -0700 |
parents | 8d1ad56761b0 |
children | 6ef0ea2f10e3 |
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 | |
12 here = os.path.dirname(os.path.realpath(__file__)) | |
13 | |
14 def depth(directory): | |
15 directory = os.path.abspath(directory) | |
16 level = 0 | |
17 while True: | |
18 directory, remainder = os.path.split(directory) | |
19 level += 1 | |
20 if not remainder: | |
21 break | |
22 return level | |
23 | |
24 def tree(directory): | |
383
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
25 |
384 | 26 sort_key=lambda x: x.lower() |
383
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
27 retval = [directory] |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
28 top = depth(directory) |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
29 indent = [] |
384 | 30 last = {} |
382 | 31 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
|
32 |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
33 abspath = os.path.abspath(dirpath) |
384 | 34 basename = os.path.basename(abspath) |
35 parent = os.path.dirname(abspath) | |
383
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
36 level = depth(abspath) - top |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
37 |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
38 # sort articles of interest |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
39 for resource in (dirnames, filenames): |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
40 resource[:] = sorted(resource, key=sort_key) |
382 | 41 |
383
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
42 # set last for current dirpath |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
43 if dirnames: |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
44 last[os.path.abspath(dirpath)] = dirnames[-1] |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
45 if last.get(parent) == os.path.basename(abspath): |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
46 pass |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
47 |
384 | 48 retval.append('%s%s'% (str(level) * level, basename)) |
49 level += 1 | |
50 retval.extend([('%s%s' % (str(level) * level, filename)) | |
51 for filename in filenames]) | |
52 | |
383
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
53 # retval.append('%s%s%s %s' % ('│' * (indent-1), |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
54 # ('├' if basename == basename else '└') if indent else '', |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
55 # basename)) |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
56 # filenames = sorted(filenames, key=lambda x: x.lower()) |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
57 # retval.extend(['%s%s%s' % ('│' * (indent), |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
58 # '├' if (((index < len(filenames) -1)) or dirnames) else '└', |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
59 # name) |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
60 # for index, name in |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
61 # enumerate(filenames) |
8d1ad56761b0
this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents:
382
diff
changeset
|
62 # ]) |
382 | 63 return '\n'.join(retval) |
64 | |
65 def main(args=sys.argv[1:]): | |
66 | |
67 usage = '%prog [options]' | |
68 parser = optparse.OptionParser(usage=usage, description=__doc__) | |
69 options, args = parser.parse_args(args) | |
70 if not args: | |
71 args = ['.'] | |
72 | |
73 not_directory = [arg for arg in args | |
74 if not os.path.isdir(arg)] | |
75 if not_directory: | |
76 parser.error("Not a directory: %s" % (', '.join(not_directory))) | |
77 | |
78 for arg in args: | |
79 print (tree(arg)) | |
80 | |
81 if __name__ == '__main__': | |
82 main() |