New store_list with -l (detail) and bold
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Wed, 8 Aug 2012 14:04:56 +0000 (17:04 +0300)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Wed, 8 Aug 2012 14:04:56 +0000 (17:04 +0300)
kamaki/cli.py
kamaki/clients/pithos_cli.py
kamaki/utils.py

index 93862eb..01d0ab9 100644 (file)
@@ -216,10 +216,13 @@ def main():
         return parser
 
     def find_term_in_args(arg_list, term_list):
+        """find an arg_list term in term_list. All other terms up to found
+        term are rearanged at the end of arg_list, preserving relative order
+        """
         arg_tail = []
         while len(arg_list) > 0:
             group = arg_list.pop(0)
-            if group.startswith('-') or group not in term_list:
+            if group not in term_list:
                 arg_tail.append(group)
             else:
                 arg_list += arg_tail
@@ -256,7 +259,7 @@ def main():
     if not group:
         parser.print_help()
         print_groups()
-        exit(1)
+        exit(0)
 
     parser.prog = '%s %s <command>' % (exe, group)
     command = find_term_in_args(argv, _commands[group])
index d5d2a1b..7a103a0 100644 (file)
 # or implied, of GRNET S.A.command
 
 from kamaki.cli import command, set_api_description
+from kamaki.utils import format_size
 set_api_description('store', 'Pithos+ storage commands')
 from .pithos import PithosClient, ClientError
 from .cli_utils import raiseCLIError
-from kamaki.utils import print_dict
+from kamaki.utils import print_dict, pretty_keys, print_list
+from colors import bold
 
 from progress.bar import IncrementalBar
 
@@ -106,10 +108,52 @@ class store_list(_store_container_command):
     """List containers, object trees or objects in a directory
     """
 
+    def update_parser(self, parser):
+        super(self.__class__, self).update_parser(parser)
+        parser.add_argument('-l', action='store_true', dest='detail', default=False,
+            help='show detailed output')
+        """
+        parser.add_argument('-n', action='store', dest='limit', default=10000,
+            help='show limited output')
+        parser.add_argument('--marker', action='store', dest='marker', default=None,
+            help='show output greater then marker')
+        parser.add_argument('--prefix', action='store', dest='prefix', default=None,
+            help='show output starting with prefix')
+        parser.add_argument('--delimiter', action='store', dest='delimiter', default=None, 
+            help='show output up to the delimiter')
+        parser.add_argument('--path', action='store', dest='path', default=None, 
+            help='show output starting with prefix up to /')
+        parser.add_argument('--meta', action='store', dest='meta', default=None, 
+            help='show output having the specified meta keys')
+        parser.add_argument('--if-modified-since', action='store', dest='if_modified_since', 
+            default=None, help='show output if modified since then')
+        parser.add_argument('--if-unmodified-since', action='store', dest='if_unmodified_since',
+            default=None, help='show output if not modified since then')
+        parser.add_argument('--until', action='store', dest='until', default=None,
+            help='show metadata until that date')
+        parser.add_argument('--format', action='store', dest='format', default='%d/%m/%Y %H:%M:%S',
+            help='format to parse until date (default: %d/%m/%Y %H:%M:%S)')
+        parser.add_argument('--shared', action='store_true', dest='shared', default=False,
+            help='show only shared')
+        parser.add_argument('--public', action='store_true', dest='public', default=False,
+            help='show only public')
+        """
+
     def print_objects(self, object_list):
         for obj in object_list:
-            size = format_size(obj['bytes']) if 0 < obj['bytes'] else 'D'
-            print('%6s %s' % (size, obj['name']))
+            if obj['content_type'] == 'application/directory':
+                size = ''
+            else:
+                size = format_size(obj['bytes'])
+            oname = bold(obj['name'])
+            if getattr(self.args, 'detail'):
+                print(oname)
+                print_dict(pretty_keys(obj), exclude=('name'), ident=18)
+                print
+            else:
+                oname = '%6s %s'%(size, oname)
+                oname += '/' if len(size) == 0 else ''
+                print(oname)
 
     def print_containers(self, container_list):
         for container in container_list:
index 8014208..5a699a4 100644 (file)
 # or implied, of GRNET S.A.
 from .cli import CLIError
 
+def pretty_keys(d, delim='_', recurcive=False):
+    """Transform keys of a dict from the form
+    str1_str2_..._strN to the form strN
+    where _ is the delimeter
+    """
+    new_d = {}
+    for key, val in d.items():
+        new_key = key.split(delim)[-1]
+        if recurcive and isinstance(val, dict):
+            new_val = pretty_keys(val, delim, recurcive) 
+        else:
+            new_val = val
+        new_d[new_key] = new_val
+    return new_d
+
 def print_dict(d, exclude=(), ident= 0):
     if not isinstance(d, dict):
         raise CLIError(message='Cannot dict_print a non-dict object')
@@ -96,13 +111,10 @@ def format_size(size):
         size = float(size)
     except ValueError:
         raise CLIError(message='Cannot format %s in bytes'%size)
-    print('right now size is %s'%size)
     for unit in units:
-        print('right now size is %s'%size)
         if size < 1024:
             break
         size /= 1024
-    print('right now size is %s'%size)
     s = ('%.1f' % size)
     if '.0' == s[-2:]:
         s = s[:-2]