Mercurial > hg > config
comparison 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 |
comparison
equal
deleted
inserted
replaced
384:5ae5ada91ac8 | 385:6ef0ea2f10e3 |
---|---|
7 | 7 |
8 import optparse | 8 import optparse |
9 import os | 9 import os |
10 import sys | 10 import sys |
11 | 11 |
12 here = os.path.dirname(os.path.realpath(__file__)) | 12 LINE = '|' |
13 ITEM = '+' | |
14 END = '\\' | |
15 # | |
16 LINE = '│' | |
17 ITEM = '├' | |
18 END = '└' | |
13 | 19 |
14 def depth(directory): | 20 def depth(directory): |
15 directory = os.path.abspath(directory) | 21 directory = os.path.abspath(directory) |
16 level = 0 | 22 level = 0 |
17 while True: | 23 while True: |
22 return level | 28 return level |
23 | 29 |
24 def tree(directory): | 30 def tree(directory): |
25 | 31 |
26 sort_key=lambda x: x.lower() | 32 sort_key=lambda x: x.lower() |
27 retval = [directory] | 33 retval = [] |
28 top = depth(directory) | 34 top = depth(directory) |
29 indent = [] | 35 indent = [] |
30 last = {} | 36 last = {} |
31 for dirpath, dirnames, filenames in os.walk(directory, topdown=True): | 37 for dirpath, dirnames, filenames in os.walk(directory, topdown=True): |
32 | 38 |
37 | 43 |
38 # sort articles of interest | 44 # sort articles of interest |
39 for resource in (dirnames, filenames): | 45 for resource in (dirnames, filenames): |
40 resource[:] = sorted(resource, key=sort_key) | 46 resource[:] = sorted(resource, key=sort_key) |
41 | 47 |
42 # set last for current dirpath | 48 files_end = ITEM |
49 dirpath_marker = ITEM | |
50 | |
51 if level > len(indent): | |
52 indent.append(LINE) | |
53 indent = indent[:level] | |
54 | |
43 if dirnames: | 55 if dirnames: |
44 last[os.path.abspath(dirpath)] = dirnames[-1] | 56 files_end = ITEM |
45 if last.get(parent) == os.path.basename(abspath): | |
46 pass | |
47 | 57 |
48 retval.append('%s%s'% (str(level) * level, basename)) | 58 last[abspath] = dirnames[-1] |
49 level += 1 | 59 else: |
50 retval.extend([('%s%s' % (str(level) * level, filename)) | 60 files_end = END |
51 for filename in filenames]) | |
52 | 61 |
53 # retval.append('%s%s%s %s' % ('│' * (indent-1), | 62 if last.get(parent) == os.path.basename(abspath): |
54 # ('├' if basename == basename else '└') if indent else '', | 63 # last directory of parent |
55 # basename)) | 64 dirpath_mark = END |
56 # filenames = sorted(filenames, key=lambda x: x.lower()) | 65 indent[-1] = ' ' |
57 # retval.extend(['%s%s%s' % ('│' * (indent), | 66 elif not indent: |
58 # '├' if (((index < len(filenames) -1)) or dirnames) else '└', | 67 dirpath_mark = '' |
59 # name) | 68 else: |
60 # for index, name in | 69 dirpath_mark = ITEM |
61 # enumerate(filenames) | 70 |
62 # ]) | 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 | |
63 return '\n'.join(retval) | 84 return '\n'.join(retval) |
64 | 85 |
65 def main(args=sys.argv[1:]): | 86 def main(args=sys.argv[1:]): |
66 | 87 |
67 usage = '%prog [options]' | 88 usage = '%prog [options]' |