Mercurial > hg > config
comparison python/tree.py @ 670:93dc0507ab3b
to argparse
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Tue, 29 Apr 2014 15:01:40 -0700 |
| parents | fbb5b143349a |
| children | eeb38dfa17d0 |
comparison
equal
deleted
inserted
replaced
| 669:bcf5781b615e | 670:93dc0507ab3b |
|---|---|
| 5 tree in python | 5 tree in python |
| 6 """ | 6 """ |
| 7 | 7 |
| 8 # TODO: script2package | 8 # TODO: script2package |
| 9 | 9 |
| 10 import optparse | 10 import argparse |
| 11 import os | 11 import os |
| 12 import sys | 12 import sys |
| 13 | 13 |
| 14 # ASCII delimeters | 14 # ASCII delimeters |
| 15 ascii_delimeters = { | 15 ascii_delimeters = { |
| 98 | 98 |
| 99 def tree(directory, | 99 def tree(directory, |
| 100 item_marker=unicode_delimeters['item_marker'], | 100 item_marker=unicode_delimeters['item_marker'], |
| 101 vertical_line=unicode_delimeters['vertical_line'], | 101 vertical_line=unicode_delimeters['vertical_line'], |
| 102 last_child=unicode_delimeters['last_child'], | 102 last_child=unicode_delimeters['last_child'], |
| 103 display_files=True, | |
| 103 sort_key=lambda x: x.lower()): | 104 sort_key=lambda x: x.lower()): |
| 104 """ | 105 """ |
| 105 display tree directory structure for `directory` | 106 display tree directory structure for `directory` |
| 106 """ | 107 """ |
| 107 | 108 |
| 157 for index, filename in enumerate(filenames)]) | 158 for index, filename in enumerate(filenames)]) |
| 158 | 159 |
| 159 return '\n'.join(retval) | 160 return '\n'.join(retval) |
| 160 | 161 |
| 161 def main(args=sys.argv[1:]): | 162 def main(args=sys.argv[1:]): |
| 163 """CLI""" | |
| 162 | 164 |
| 163 # parse command line options | 165 # parse command line options |
| 164 usage = '%prog [options]' | 166 parser = argparse.ArgumentParser(description=__doc__) |
| 165 parser = optparse.OptionParser(usage=usage, description=__doc__) | 167 parser.add_argument('-a', '--ascii', dest='use_ascii', |
| 166 parser.add_option('-a', '--ascii', dest='use_ascii', | 168 action='store_true', default=False, |
| 167 action='store_true', default=False, | 169 help="use ascii delimeters ({})".format(', '.join(ascii_delimeters.values()))) |
| 168 help="use ascii delimeters (%s)" % ascii_delimeters) | 170 parser.add_argument('path', nargs='*', |
| 169 options, args = parser.parse_args(args) | 171 help="paths to display the tree of") |
| 170 if not args: | 172 options = parser.parse_args(args) |
| 171 args = ['.'] | 173 |
| 174 # get paths to operate on | |
| 175 paths = options.path | |
| 176 if not paths: | |
| 177 paths = ['.'] | |
| 172 | 178 |
| 173 # sanity check | 179 # sanity check |
| 174 not_directory = [arg for arg in args | 180 not_directory = [arg for arg in paths |
| 175 if not os.path.isdir(arg)] | 181 if not os.path.isdir(arg)] |
| 176 if not_directory: | 182 if not_directory: |
| 177 parser.error("Not a directory: %s" % (', '.join(not_directory))) | 183 parser.error("Not a directory: %s" % (', '.join(not_directory))) |
| 178 | 184 |
| 179 delimeters = unicode_delimeters | 185 delimeters = unicode_delimeters |
| 180 if options.use_ascii: | 186 if options.use_ascii: |
| 181 delimeters = ascii_delimeters | 187 delimeters = ascii_delimeters |
| 182 | 188 |
| 183 # print the tree | 189 # print the tree |
| 184 for arg in args: | 190 for arg in paths: |
| 185 print (tree(arg, **delimeters)) | 191 print (tree(arg, **delimeters)) |
| 186 | 192 |
| 187 if __name__ == '__main__': | 193 if __name__ == '__main__': |
| 188 main() | 194 main() |
