From 12b7a2b54f4e3c36d49dec2b2620826aa7029a3e Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Mon, 12 Mar 2018 21:05:51 +0800 Subject: format output JIRA: RELENG-348 Change-Id: I8f6edae6ed70542f5dde45d81601c35d32af96d9 Signed-off-by: SerenaFeng --- .../testapi-client/testapiclient/utils/command.py | 44 +++++++++++++++------- 1 file changed, 30 insertions(+), 14 deletions(-) (limited to 'testapi/testapi-client/testapiclient/utils/command.py') diff --git a/testapi/testapi-client/testapiclient/utils/command.py b/testapi/testapi-client/testapiclient/utils/command.py index f9c75a7..c99a3f3 100644 --- a/testapi/testapi-client/testapiclient/utils/command.py +++ b/testapi/testapi-client/testapiclient/utils/command.py @@ -1,9 +1,27 @@ +import abc +import logging + from cliff import command +from cliff import lister +from cliff import show +import six +from testapiclient import utils from testapiclient.utils import url_parse +class CommandMeta(abc.ABCMeta): + + def __new__(mcs, name, bases, cls_dict): + if 'log' not in cls_dict: + cls_dict['log'] = logging.getLogger( + cls_dict['__module__'] + '.' + name) + return super(CommandMeta, mcs).__new__(mcs, name, bases, cls_dict) + + +@six.add_metaclass(CommandMeta) class Command(command.Command): + def get_parser(self, prog_name): parser = super(Command, self).get_parser(prog_name) parser.add_argument('-u', @@ -12,17 +30,14 @@ class Command(command.Command): parser.add_argument('-p', type=str, help='Password for authentication') - return parser - def show(self, request, response): - print ' '.join([request, - 'success' if response.status_code < 300 - else 'failed: {}'.format(response.reason)]) - + def run(self, parsed_args): + self.log.debug('run(%s)', parsed_args) + return super(Command, self).run(parsed_args) -class Lister(command.Command): +class Lister(Command, lister.Lister): @staticmethod def filter_by_name(url, parsed_args): def query_url(): @@ -30,12 +45,13 @@ class Lister(command.Command): return query_url() if parsed_args.name else url - def show(self, response): - print response.json() if response.status_code < 300 \ - else 'Get failed: {}'.format(response.reason) + @staticmethod + def format_output(columns, data): + return (columns, + (utils.get_item_properties(s, columns) for s in data)) -class ShowOne(command.Command): - def show(self, response): - print response.json() if response.status_code < 300 \ - else 'Get failed: {}'.format(response.reason) +class ShowOne(Command, show.ShowOne): + @staticmethod + def format_output(body): + return zip(*sorted(six.iteritems(body))) -- cgit 1.2.3-korg