comparison python/tree.py @ 671:eeb38dfa17d0

display directories only
author Jeff Hammel <k0scist@gmail.com>
date Tue, 29 Apr 2014 15:06:48 -0700
parents 93dc0507ab3b
children
comparison
equal deleted inserted replaced
670:93dc0507ab3b 671:eeb38dfa17d0
115 115
116 abspath = os.path.abspath(dirpath) 116 abspath = os.path.abspath(dirpath)
117 basename = os.path.basename(abspath) 117 basename = os.path.basename(abspath)
118 parent = os.path.dirname(abspath) 118 parent = os.path.dirname(abspath)
119 level = depth(abspath) - top 119 level = depth(abspath) - top
120
121 # omit files if specified
122 if not display_files:
123 filenames = []
120 124
121 # sort articles of interest 125 # sort articles of interest
122 for resource in (dirnames, filenames): 126 for resource in (dirnames, filenames):
123 resource[:] = sorted(resource, key=sort_key) 127 resource[:] = sorted(resource, key=sort_key)
124 128
157 filename)) 161 filename))
158 for index, filename in enumerate(filenames)]) 162 for index, filename in enumerate(filenames)])
159 163
160 return '\n'.join(retval) 164 return '\n'.join(retval)
161 165
166
162 def main(args=sys.argv[1:]): 167 def main(args=sys.argv[1:]):
163 """CLI""" 168 """CLI"""
164 169
165 # parse command line options 170 # parse command line options
166 parser = argparse.ArgumentParser(description=__doc__) 171 parser = argparse.ArgumentParser(description=__doc__)
167 parser.add_argument('-a', '--ascii', dest='use_ascii', 172 parser.add_argument('-a', '--ascii', dest='use_ascii',
168 action='store_true', default=False, 173 action='store_true', default=False,
169 help="use ascii delimeters ({})".format(', '.join(ascii_delimeters.values()))) 174 help="use ascii delimeters ({})".format(', '.join(ascii_delimeters.values())))
175 parser.add_argument('-d', '--no-files', dest='display_files',
176 action='store_false', default=True,
177 help='only display directories')
170 parser.add_argument('path', nargs='*', 178 parser.add_argument('path', nargs='*',
171 help="paths to display the tree of") 179 help="directory paths to display the tree of")
172 options = parser.parse_args(args) 180 options = parser.parse_args(args)
173 181
174 # get paths to operate on 182 # get paths to operate on
175 paths = options.path 183 paths = options.path
176 if not paths: 184 if not paths:
177 paths = ['.'] 185 paths = ['.']
178 186
179 # sanity check 187 # ensure each path is a directory
180 not_directory = [arg for arg in paths 188 not_directory = [arg for arg in paths
181 if not os.path.isdir(arg)] 189 if not os.path.isdir(arg)]
182 if not_directory: 190 if not_directory:
183 parser.error("Not a directory: %s" % (', '.join(not_directory))) 191 parser.error("Not a directory: {}".format(', '.join(not_directory)))
184 192
185 delimeters = unicode_delimeters 193 delimeters = unicode_delimeters
186 if options.use_ascii: 194 if options.use_ascii:
187 delimeters = ascii_delimeters 195 delimeters = ascii_delimeters
188 196
197 # build function arguments
198 kw = delimeters.copy()
199 kw['display_files'] = options.display_files
200
189 # print the tree 201 # print the tree
190 for arg in paths: 202 for arg in paths:
191 print (tree(arg, **delimeters)) 203 print (tree(arg, **kw))
192 204
193 if __name__ == '__main__': 205 if __name__ == '__main__':
194 main() 206 main()