print_items can print results in pages, flavorlist
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Tue, 8 Jan 2013 11:11:09 +0000 (13:11 +0200)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Tue, 8 Jan 2013 11:11:09 +0000 (13:11 +0200)
if called with page_size a possitive int, print_items holds every page_size
results and waits for user input to continue.

This is used in flavor_list for the moment, but is going to be a std option
for all *_list commands

kamaki/cli/commands/cyclades_cli.py
kamaki/cli/utils.py

index 45deca5..8911dec 100644 (file)
@@ -37,7 +37,7 @@ from kamaki.cli.utils import print_dict, print_list, print_items, bold
 from kamaki.cli.errors import raiseCLIError, CLISyntaxError
 from kamaki.clients.cyclades import CycladesClient, ClientError
 from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument
-from kamaki.cli.argument import ProgressBarArgument, DateArgument
+from kamaki.cli.argument import ProgressBarArgument, DateArgument, IntArgument
 from kamaki.cli.commands import _command_init
 
 from base64 import b64encode
@@ -609,10 +609,14 @@ class server_wait(_init_cyclades):
 
 @command(flavor_cmds)
 class flavor_list(_init_cyclades):
-    """List flavors"""
+    """List available hardware flavors"""
 
     arguments = dict(
-        detail=FlagArgument('show detailed output', '-l')
+        detail=FlagArgument('show detailed output', '-l'),
+        limit=IntArgument('limit the number of flavors to list', '-n'),
+        more=FlagArgument(
+            'output results in pages (-n to set items per page, defaul is 10)',
+            '--more')
     )
 
     def main(self):
@@ -621,7 +625,16 @@ class flavor_list(_init_cyclades):
             flavors = self.client.list_flavors(self['detail'])
         except Exception as err:
             raiseCLIError(err)
-        print_items(flavors, with_redundancy=self['detail'])
+        if self['more']:
+            print_items(
+                flavors,
+                with_redundancy=self['detail'],
+                page_size=self['limit'] if self['limit'] else 10)
+        else:
+            print_items(
+                flavors,
+                with_redundancy=self['detail'],
+                page_size=self['limit'])
 
 
 @command(flavor_cmds)
index 8eb7af7..72bf4ac 100644 (file)
@@ -31,7 +31,7 @@
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from sys import stdout
+from sys import stdout, stdin
 from re import compile as regex_compile
 from kamaki.cli.errors import raiseCLIError
 
@@ -194,7 +194,8 @@ def print_list(l,
 def print_items(items,
     title=('id', 'name'),
     with_enumeration=False,
-    with_redundancy=False):
+    with_redundancy=False,
+    page_size=0):
     """print dict or list items in a list, using some values as title
     Objects of next level don't inherit enumeration (default: off) or titles
 
@@ -202,7 +203,15 @@ def print_items(items,
     :param title: (tuple) keys to use their values as title
     :param with_enumeration: (boolean) enumerate items (order id on title)
     :param with_redundancy: (boolean) values in title also appear on body
+    :param page_size: (int) show results in pages of page_size items, enter to
+        continue
     """
+    try:
+        page_size = int(page_size) if int(page_size) > 0 else len(items)
+    except:
+        page_size = len(items)
+    num_of_pages = len(items) // page_size
+    num_of_pages += 1 if len(items) % page_size else 0
     for i, item in enumerate(items):
         if with_enumeration:
             stdout.write('%s. ' % (i + 1))
@@ -221,6 +230,12 @@ def print_items(items,
             print_list(item, ident=1)
         else:
             print(' %s' % item)
+        if num_of_pages and len(items) > (i + 1) and 0 == (i + 1) % page_size:
+            num_of_pages -= 1
+            print('(%s listed - %s more - "enter" to continue)' % (
+                i + 1,
+                len(items) - (i + 1)))
+            stdin.read(1)
 
 
 def format_size(size):