comparison 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
comparison
equal deleted inserted replaced
386:bea5f2fe4ea4 387:0aee0da6b06b
7 7
8 import optparse 8 import optparse
9 import os 9 import os
10 import sys 10 import sys
11 11
12 LINE = '|' 12 # ASCII delimeters
13 VERTICAL_LINE = '|'
13 ITEM = '+' 14 ITEM = '+'
14 END = '\\' 15 END = '\\'
15 # 16
16 LINE = '│' 17 # unicode delimiters
18 VERTICAL_LINE = '│'
17 ITEM = '├' 19 ITEM = '├'
18 END = '└' 20 END = '└'
19 21
20 def depth(directory): 22 def depth(directory):
23 """returns the integer depth of a directory or path relative to '/' """
24
21 directory = os.path.abspath(directory) 25 directory = os.path.abspath(directory)
22 level = 0 26 level = 0
23 while True: 27 while True:
24 directory, remainder = os.path.split(directory) 28 directory, remainder = os.path.split(directory)
25 level += 1 29 level += 1
47 51
48 files_end = ITEM 52 files_end = ITEM
49 dirpath_marker = ITEM 53 dirpath_marker = ITEM
50 54
51 if level > len(indent): 55 if level > len(indent):
52 indent.append(LINE) 56 indent.append(VERTICAL_LINE)
53 indent = indent[:level] 57 indent = indent[:level]
54 58
55 if dirnames: 59 if dirnames:
56 files_end = ITEM 60 files_end = ITEM
57
58 last[abspath] = dirnames[-1] 61 last[abspath] = dirnames[-1]
59 else: 62 else:
60 files_end = END 63 files_end = END
61 64
62 if last.get(parent) == os.path.basename(abspath): 65 if last.get(parent) == os.path.basename(abspath):
69 dirpath_mark = ITEM 72 dirpath_mark = ITEM
70 73
71 retval.append('%s%s%s'% (''.join(indent[:-1]), dirpath_mark, basename)) 74 retval.append('%s%s%s'% (''.join(indent[:-1]), dirpath_mark, basename))
72 if filenames: 75 if filenames:
73 last_file = filenames[-1] 76 last_file = filenames[-1]
74 retval.extend([('%s%s%s' % (str_indent, 77 retval.extend([('%s%s%s' % (''.join(indent),
75 files_end if filename == last_file else ITEM, 78 files_end if filename == last_file else ITEM,
76 filename)) 79 filename))
77 for index, filename in enumerate(filenames)]) 80 for index, filename in enumerate(filenames)])
78 81
79 return '\n'.join(retval) 82 return '\n'.join(retval)