comparison python/tree2.py @ 389:2d4dfe1dc632

make delimeters flexible
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 27 Jul 2013 20:01:01 -0700
parents 3678770e8c52
children 9d02187611ae
comparison
equal deleted inserted replaced
388:3678770e8c52 389:2d4dfe1dc632
9 import os 9 import os
10 import sys 10 import sys
11 11
12 # ASCII delimeters 12 # ASCII delimeters
13 ascii_delimeters = { 13 ascii_delimeters = {
14 VERTICAL_LINE = '|' 14 'vertical_line' : '|',
15 ITEM = '+' 15 'item_marker' : '+',
16 END = '\\' 16 'last_child' : '\\'
17 } 17 }
18 18
19 # unicode delimiters 19 # unicode delimiters
20 VERTICAL_LINE = '│' 20 unicode_delimeters = {
21 ITEM = '├' 21 'vertical_line' : '│',
22 END = '└' 22 'item_marker' : '├',
23 'last_child' : '└'
24 }
23 25
24 def depth(directory): 26 def depth(directory):
25 """returns the integer depth of a directory or path relative to '/' """ 27 """returns the integer depth of a directory or path relative to '/' """
26 28
27 directory = os.path.abspath(directory) 29 directory = os.path.abspath(directory)
32 if not remainder: 34 if not remainder:
33 break 35 break
34 return level 36 return level
35 37
36 def tree(directory, 38 def tree(directory,
37 item_marker='', 39 item_marker=unicode_delimeters['item_marker'],
40 vertical_line=unicode_delimeters['vertical_line'],
41 last_child=unicode_delimeters['last_child'],
38 sort_key=lambda x: x.lower()): 42 sort_key=lambda x: x.lower()):
39 """ 43 """
40 display tree directory structure for `directory` 44 display tree directory structure for `directory`
41 """ 45 """
42 46
54 58
55 # sort articles of interest 59 # sort articles of interest
56 for resource in (dirnames, filenames): 60 for resource in (dirnames, filenames):
57 resource[:] = sorted(resource, key=sort_key) 61 resource[:] = sorted(resource, key=sort_key)
58 62
59 files_end = ITEM 63 files_end = item_marker
60 dirpath_marker = ITEM 64 dirpath_marker = item_marker
61 65
62 if level > len(indent): 66 if level > len(indent):
63 indent.append(VERTICAL_LINE) 67 indent.append(vertical_line)
64 indent = indent[:level] 68 indent = indent[:level]
65 69
66 if dirnames: 70 if dirnames:
67 files_end = ITEM 71 files_end = item_marker
68 last[abspath] = dirnames[-1] 72 last[abspath] = dirnames[-1]
69 else: 73 else:
70 files_end = END 74 files_end = last_child
71 75
72 if last.get(parent) == os.path.basename(abspath): 76 if last.get(parent) == os.path.basename(abspath):
73 # last directory of parent 77 # last directory of parent
74 dirpath_mark = END 78 dirpath_mark = last_child
75 indent[-1] = ' ' 79 indent[-1] = ' '
76 elif not indent: 80 elif not indent:
77 dirpath_mark = '' 81 dirpath_mark = ''
78 else: 82 else:
79 dirpath_mark = ITEM 83 dirpath_mark = item_marker
80 84
81 # append the directory and piece of tree structure 85 # append the directory and piece of tree structure
82 # if the top-level entry directory, print as passed 86 # if the top-level entry directory, print as passed
83 retval.append('%s%s%s'% (''.join(indent[:-1]), 87 retval.append('%s%s%s'% (''.join(indent[:-1]),
84 dirpath_mark, 88 dirpath_mark,
85 basename if retval else directory)) 89 basename if retval else directory))
86 # add the files 90 # add the files
87 if filenames: 91 if filenames:
88 last_file = filenames[-1] 92 last_file = filenames[-1]
89 retval.extend([('%s%s%s' % (''.join(indent), 93 retval.extend([('%s%s%s' % (''.join(indent),
90 files_end if filename == last_file else ITEM, 94 files_end if filename == last_file else item_marker,
91 filename)) 95 filename))
92 for index, filename in enumerate(filenames)]) 96 for index, filename in enumerate(filenames)])
93 97
94 return '\n'.join(retval) 98 return '\n'.join(retval)
95 99