comparison python/tree2.py @ 383:8d1ad56761b0

this still, somehow, eludes my tired brain
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 26 Jul 2013 05:56:18 -0700
parents 9ac9561b6574
children 5ae5ada91ac8
comparison
equal deleted inserted replaced
382:9ac9561b6574 383:8d1ad56761b0
20 if not remainder: 20 if not remainder:
21 break 21 break
22 return level 22 return level
23 23
24 def tree(directory): 24 def tree(directory):
25 retval = [] 25
26 level = depth(directory) 26 sort_key=lambda x: x.lower())
27 pre = [] 27 retval = [directory]
28 directories = {} 28 top = depth(directory)
29 lvlndctr = [] 29 indent = []
30 last = {}
31 passed_last = {}
32 columns = []
33 lastdepth = depth
34 indent = 0
35 for dirpath, dirnames, filenames in os.walk(directory, topdown=True): 30 for dirpath, dirnames, filenames in os.walk(directory, topdown=True):
36 basename = os.path.basename(dirpath)
37 parent = os.path.abspath(os.path.dirname(dirpath))
38 indent = depth(dirpath) - level
39 import pdb; pdb.set_trace()
40 dirnames[:] = sorted(dirnames, key=lambda x: x.lower())
41 last[os.path.abspath(dirpath)] = dirnames and dirnames[-1] or None
42 directories[dirpath] = dirnames
43 31
44 retval.append('%s%s%s %s' % ('│' * (indent-1), 32 abspath = os.path.abspath(dirpath)
45 ('├' if basename == basename else '└') if indent else '', 33 parent = os.path.dirname(abspath))
46 basename)) 34 level = depth(abspath) - top
47 filenames = sorted(filenames, key=lambda x: x.lower()) 35
48 retval.extend(['%s%s%s' % ('│' * (indent), 36 # sort articles of interest
49 '├' if (((index < len(filenames) -1)) or dirnames) else '└', 37 for resource in (dirnames, filenames):
50 name) 38 resource[:] = sorted(resource, key=sort_key)
51 for index, name in 39
52 enumerate(filenames) 40 # set last for current dirpath
53 ]) 41 if dirnames:
42 last[os.path.abspath(dirpath)] = dirnames[-1]
43 if last.get(parent) == os.path.basename(abspath):
44 pass
45
46 # retval.append('%s%s%s %s' % ('│' * (indent-1),
47 # ('├' if basename == basename else '└') if indent else '',
48 # basename))
49 # filenames = sorted(filenames, key=lambda x: x.lower())
50 # retval.extend(['%s%s%s' % ('│' * (indent),
51 # '├' if (((index < len(filenames) -1)) or dirnames) else '└',
52 # name)
53 # for index, name in
54 # enumerate(filenames)
55 # ])
54 return '\n'.join(retval) 56 return '\n'.join(retval)
55 57
56 def main(args=sys.argv[1:]): 58 def main(args=sys.argv[1:]):
57 59
58 usage = '%prog [options]' 60 usage = '%prog [options]'