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
@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):
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)
# 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
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
: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))
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):