Commit 6430d3a0 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Update commands to use pager when --more

Refs: #4279
parent 38db356b
......@@ -32,7 +32,8 @@
# or implied, of GRNET S.A.command
from kamaki.cli.logger import get_logger
from kamaki.cli.utils import print_json, print_items, filter_dicts_by_dict
from kamaki.cli.utils import (
print_json, print_items, filter_dicts_by_dict, stdout)
from kamaki.cli.argument import FlagArgument, ValueArgument
log = get_logger(__name__)
......@@ -219,11 +220,14 @@ class _optional_json(object):
json_output=FlagArgument('show headers in json', ('-j', '--json'))
)
def _print(self, output, print_method=print_items, **print_method_kwargs):
def _print(
self, output,
print_method=print_items, out=stdout,
**print_method_kwargs):
if self['json_output']:
print_json(output)
print_json(output, out=out)
else:
print_method(output, **print_method_kwargs)
print_method(output, out=out, **print_method_kwargs)
class _name_filter(object):
......
......@@ -34,7 +34,7 @@
from kamaki.cli import command
from kamaki.cli.command_tree import CommandTree
from kamaki.cli.utils import (
print_dict, remove_from_items, filter_dicts_by_dict)
print_dict, remove_from_items, filter_dicts_by_dict, pager)
from kamaki.cli.errors import raiseCLIError, CLISyntaxError, CLIBaseUrlError
from kamaki.clients.cyclades import CycladesClient, ClientError
from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument
......@@ -45,6 +45,7 @@ from kamaki.cli.commands import (
from base64 import b64encode
from os.path import exists
from io import StringIO
server_cmds = CommandTree('server', 'Cyclades/Compute API server commands')
......@@ -268,10 +269,13 @@ class server_list(_init_cyclades, _optional_json, _name_filter, _id_filter):
srv.pop(key)
kwargs = dict(with_enumeration=self['enum'])
if self['more']:
kwargs['page_size'] = self['limit'] if self['limit'] else 10
elif self['limit']:
kwargs['out'] = StringIO()
kwargs['title'] = ()
if self['limit']:
servers = servers[:self['limit']]
self._print(servers, **kwargs)
if self['more']:
pager(kwargs['out'].getvalue())
def main(self):
super(self.__class__, self)._run()
......@@ -758,12 +762,13 @@ class flavor_list(_init_cyclades, _optional_json, _name_filter, _id_filter):
for flv in flavors:
for key in set(flv).difference(self.PERMANENTS):
flv.pop(key)
pg_size = 10 if self['more'] and not self['limit'] else self['limit']
kwargs = dict(out=StringIO(), title=()) if self['more'] else {}
self._print(
flavors,
with_redundancy=self['detail'],
page_size=pg_size,
with_enumeration=self['enum'])
with_redundancy=self['detail'], with_enumeration=self['enum'],
**kwargs)
if self['more']:
pager(kwargs['out'].getvalue())
def main(self):
super(self.__class__, self)._run()
......@@ -906,10 +911,13 @@ class network_list(_init_cyclades, _optional_json, _name_filter, _id_filter):
self._add_name(networks, 'tenant_id')
kwargs = dict(with_enumeration=self['enum'])
if self['more']:
kwargs['page_size'] = self['limit'] or 10
elif self['limit']:
kwargs['out'] = StringIO()
kwargs['title'] = ()
if self['limit']:
networks = networks[:self['limit']]
self._print(networks, **kwargs)
if self['more']:
pager(kwargs['out'].getvalue())
def main(self):
super(self.__class__, self)._run()
......
......@@ -34,10 +34,12 @@
from json import load, dumps
from os import path
from logging import getLogger
from io import StringIO
from kamaki.cli import command
from kamaki.cli.command_tree import CommandTree
from kamaki.cli.utils import print_dict, print_json, filter_dicts_by_dict
from kamaki.cli.utils import (
print_dict, print_json, filter_dicts_by_dict, pager)
from kamaki.clients.image import ImageClient
from kamaki.clients.pithos import PithosClient
from kamaki.clients.astakos import AstakosClient
......@@ -267,11 +269,14 @@ class image_list(_init_image, _optional_json, _name_filter, _id_filter):
for key in set(img).difference(self.PERMANENTS):
img.pop(key)
kwargs = dict(with_enumeration=self['enum'])
if self['more']:
kwargs['page_size'] = self['limit'] or 10
elif self['limit']:
if self['limit']:
images = images[:self['limit']]
if self['more']:
kwargs['out'] = StringIO()
kwargs['title'] = ()
self._print(images, **kwargs)
if self['more']:
pager(kwargs['out'].getvalue())
def main(self):
super(self.__class__, self)._run()
......@@ -827,11 +832,14 @@ class image_compute_list(
for key in set(img).difference(self.PERMANENTS):
img.pop(key)
kwargs = dict(with_enumeration=self['enum'])
if self['more']:
kwargs['page_size'] = self['limit'] or 10
elif self['limit']:
if self['limit']:
images = images[:self['limit']]
if self['more']:
kwargs['out'] = StringIO()
kwargs['title'] = ()
self._print(images, **kwargs)
if self['more']:
pager(kwargs['out'].getvalue())
def main(self):
super(self.__class__, self)._run()
......
......@@ -255,7 +255,7 @@ def print_items(
title = sorted(set(title).intersection(item))
pick = item.get if with_redundancy else item.pop
header = u' '.join(u'%s' % pick(key) for key in title)
out.writelines(unicode(bold(header) + '\n'))
out.writelines((unicode(bold(header) if header else '') + '\n'))
print_dict(item, indent=INDENT_TAB, out=out)
elif isinstance(item, list) or isinstance(item, tuple):
print_list(item, indent=INDENT_TAB, out=out)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment