changeset 671:eeb38dfa17d0

display directories only
author Jeff Hammel <k0scist@gmail.com>
date Tue, 29 Apr 2014 15:06:48 -0700
parents 93dc0507ab3b
children 0eff3f3658ed
files python/tree.py
diffstat 1 files changed, 16 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/python/tree.py	Tue Apr 29 15:01:40 2014 -0700
+++ b/python/tree.py	Tue Apr 29 15:06:48 2014 -0700
@@ -118,6 +118,10 @@
         parent = os.path.dirname(abspath)
         level = depth(abspath) - top
 
+        # omit files if specified
+        if not display_files:
+            filenames = []
+
         # sort articles of interest
         for resource in (dirnames, filenames):
             resource[:] = sorted(resource, key=sort_key)
@@ -159,6 +163,7 @@
 
     return '\n'.join(retval)
 
+
 def main(args=sys.argv[1:]):
     """CLI"""
 
@@ -167,8 +172,11 @@
     parser.add_argument('-a', '--ascii', dest='use_ascii',
                         action='store_true', default=False,
                         help="use ascii delimeters ({})".format(', '.join(ascii_delimeters.values())))
+    parser.add_argument('-d', '--no-files', dest='display_files',
+                        action='store_false', default=True,
+                        help='only display directories')
     parser.add_argument('path', nargs='*',
-                        help="paths to display the tree of")
+                        help="directory paths to display the tree of")
     options = parser.parse_args(args)
 
     # get paths to operate on
@@ -176,19 +184,23 @@
     if not paths:
         paths = ['.']
 
-    # sanity check
+    # ensure each path is a directory
     not_directory = [arg for arg in paths
                      if not os.path.isdir(arg)]
     if not_directory:
-        parser.error("Not a directory: %s" % (', '.join(not_directory)))
+        parser.error("Not a directory: {}".format(', '.join(not_directory)))
 
     delimeters = unicode_delimeters
     if options.use_ascii:
         delimeters = ascii_delimeters
 
+    # build function arguments
+    kw = delimeters.copy()
+    kw['display_files'] = options.display_files
+
     # print the tree
     for arg in paths:
-        print (tree(arg, **delimeters))
+        print (tree(arg, **kw))
 
 if __name__ == '__main__':
     main()