annotate python/tree.py @ 751:753b1c2bd64c

match is NOT POSIX
author Jeff Hammel <k0scist@gmail.com>
date Tue, 30 Jun 2015 15:35:34 -0700
parents eeb38dfa17d0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 tree in python
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 """
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
425
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
8 # TODO: script2package
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
9
670
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
10 import argparse
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 import os
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 import sys
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
387
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
14 # ASCII delimeters
388
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
15 ascii_delimeters = {
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
16 'vertical_line' : '|',
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
17 'item_marker' : '+',
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
18 'last_child' : '\\'
388
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
19 }
387
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
20
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
21 # unicode delimiters
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
22 unicode_delimeters = {
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
23 'vertical_line' : '│',
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
24 'item_marker' : '├',
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
25 'last_child' : '└'
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
26 }
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27
425
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
28
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 def depth(directory):
387
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
30 """returns the integer depth of a directory or path relative to '/' """
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
31
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 directory = os.path.abspath(directory)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 level = 0
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 while True:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 directory, remainder = os.path.split(directory)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 level += 1
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 if not remainder:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 break
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 return level
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40
425
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
41
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
42 ### stuff for tree generalization
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
43
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
44 class Tree(object):
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
45 """tree structure in python"""
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
46
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
47 def __init__(self, parent=None):
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
48 self.parent = parent
426
866c5b1bc4e8 stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 425
diff changeset
49 self._children = []
425
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
50
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
51 def children(self):
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
52 """returns children of the tree"""
426
866c5b1bc4e8 stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 425
diff changeset
53 return self._children # base implementation
425
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
54
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
55 def add(self, item):
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
56 """add a child to the tree root"""
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
57
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
58 def update(self, tree):
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
59 """add a subtree to the tree"""
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
60 self.add(tree)
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
61 tree.parent = self # XXX .add should probably do this for scary reasons
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
62
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
63 def output(self, serializer):
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
64 """output the tree via the given serializer"""
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
65 # XXX or should this method exist at all and instead the
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
66 # __call__ method of serializers take a Tree object?
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
67
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
68 class DirectoryTree(Tree):
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
69 """directory structure as a tree"""
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
70
429
9b7f5e31b465 python/tree.py
Jeff Hammel <jhammel@mozilla.com>
parents: 426
diff changeset
71 def __init__(self, directory):
9b7f5e31b465 python/tree.py
Jeff Hammel <jhammel@mozilla.com>
parents: 426
diff changeset
72 self.directory = directory
9b7f5e31b465 python/tree.py
Jeff Hammel <jhammel@mozilla.com>
parents: 426
diff changeset
73 self._return_type = os.path.abspath
9b7f5e31b465 python/tree.py
Jeff Hammel <jhammel@mozilla.com>
parents: 426
diff changeset
74
9b7f5e31b465 python/tree.py
Jeff Hammel <jhammel@mozilla.com>
parents: 426
diff changeset
75 def children(self):
9b7f5e31b465 python/tree.py
Jeff Hammel <jhammel@mozilla.com>
parents: 426
diff changeset
76 return os.listdir(self.directory) # -> self._return_type
9b7f5e31b465 python/tree.py
Jeff Hammel <jhammel@mozilla.com>
parents: 426
diff changeset
77
9b7f5e31b465 python/tree.py
Jeff Hammel <jhammel@mozilla.com>
parents: 426
diff changeset
78 ## Serializers
9b7f5e31b465 python/tree.py
Jeff Hammel <jhammel@mozilla.com>
parents: 426
diff changeset
79
443
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
80 def tree2html(tree):
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
81 """
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
82 .
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
83 ├── a8e.py
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
84 ├── abstract.py
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
85 ├── accentuate.py
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
86
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
87 ->
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
88
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
89 <ul>
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
90 <li>a8e.py</li>
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
91 ...
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
92 """
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
93
fbb5b143349a notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 429
diff changeset
94
429
9b7f5e31b465 python/tree.py
Jeff Hammel <jhammel@mozilla.com>
parents: 426
diff changeset
95 # How to serialize a tree -> JSON?
9b7f5e31b465 python/tree.py
Jeff Hammel <jhammel@mozilla.com>
parents: 426
diff changeset
96
425
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
97 ###
5417eb6364ee stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 391
diff changeset
98
388
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
99 def tree(directory,
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
100 item_marker=unicode_delimeters['item_marker'],
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
101 vertical_line=unicode_delimeters['vertical_line'],
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
102 last_child=unicode_delimeters['last_child'],
670
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
103 display_files=True,
388
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
104 sort_key=lambda x: x.lower()):
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
105 """
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
106 display tree directory structure for `directory`
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
107 """
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
108
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
109 retval = []
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
110 indent = []
384
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
111 last = {}
386
bea5f2fe4ea4 it works even more!
Jeff Hammel <jhammel@mozilla.com>
parents: 385
diff changeset
112 top = depth(directory)
bea5f2fe4ea4 it works even more!
Jeff Hammel <jhammel@mozilla.com>
parents: 385
diff changeset
113
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
114 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
115
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
116 abspath = os.path.abspath(dirpath)
384
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
117 basename = os.path.basename(abspath)
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
118 parent = os.path.dirname(abspath)
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
119 level = depth(abspath) - top
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
120
671
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
121 # omit files if specified
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
122 if not display_files:
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
123 filenames = []
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
124
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
125 # sort articles of interest
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
126 for resource in (dirnames, filenames):
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
127 resource[:] = sorted(resource, key=sort_key)
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
128
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
129 files_end = item_marker
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
130 dirpath_marker = item_marker
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
131
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
132 if level > len(indent):
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
133 indent.append(vertical_line)
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
134 indent = indent[:level]
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
135
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
136 if dirnames:
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
137 files_end = item_marker
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
138 last[abspath] = dirnames[-1]
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
139 else:
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
140 files_end = last_child
384
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
141
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
142 if last.get(parent) == os.path.basename(abspath):
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
143 # last directory of parent
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
144 dirpath_mark = last_child
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
145 indent[-1] = ' '
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
146 elif not indent:
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
147 dirpath_mark = ''
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
148 else:
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
149 dirpath_mark = item_marker
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
150
388
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
151 # append the directory and piece of tree structure
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
152 # if the top-level entry directory, print as passed
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
153 retval.append('%s%s%s'% (''.join(indent[:-1]),
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
154 dirpath_mark,
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
155 basename if retval else directory))
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
156 # add the files
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
157 if filenames:
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
158 last_file = filenames[-1]
387
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
159 retval.extend([('%s%s%s' % (''.join(indent),
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
160 files_end if filename == last_file else item_marker,
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
161 filename))
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
162 for index, filename in enumerate(filenames)])
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
163
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
164 return '\n'.join(retval)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
165
671
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
166
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
167 def main(args=sys.argv[1:]):
670
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
168 """CLI"""
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
169
390
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
170 # parse command line options
670
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
171 parser = argparse.ArgumentParser(description=__doc__)
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
172 parser.add_argument('-a', '--ascii', dest='use_ascii',
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
173 action='store_true', default=False,
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
174 help="use ascii delimeters ({})".format(', '.join(ascii_delimeters.values())))
671
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
175 parser.add_argument('-d', '--no-files', dest='display_files',
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
176 action='store_false', default=True,
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
177 help='only display directories')
670
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
178 parser.add_argument('path', nargs='*',
671
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
179 help="directory paths to display the tree of")
670
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
180 options = parser.parse_args(args)
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
181
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
182 # get paths to operate on
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
183 paths = options.path
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
184 if not paths:
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
185 paths = ['.']
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
186
671
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
187 # ensure each path is a directory
670
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
188 not_directory = [arg for arg in paths
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
189 if not os.path.isdir(arg)]
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
190 if not_directory:
671
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
191 parser.error("Not a directory: {}".format(', '.join(not_directory)))
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
192
390
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
193 delimeters = unicode_delimeters
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
194 if options.use_ascii:
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
195 delimeters = ascii_delimeters
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
196
671
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
197 # build function arguments
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
198 kw = delimeters.copy()
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
199 kw['display_files'] = options.display_files
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
200
390
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
201 # print the tree
670
93dc0507ab3b to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 443
diff changeset
202 for arg in paths:
671
eeb38dfa17d0 display directories only
Jeff Hammel <k0scist@gmail.com>
parents: 670
diff changeset
203 print (tree(arg, **kw))
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
204
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
205 if __name__ == '__main__':
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
206 main()