diff options
Diffstat (limited to 'testapi/testapi-client/testapiclient/utils/command.py')
-rw-r--r-- | testapi/testapi-client/testapiclient/utils/command.py | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/testapi/testapi-client/testapiclient/utils/command.py b/testapi/testapi-client/testapiclient/utils/command.py index f9c75a7..9614acf 100644 --- a/testapi/testapi-client/testapiclient/utils/command.py +++ b/testapi/testapi-client/testapiclient/utils/command.py @@ -1,9 +1,26 @@ +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,30 +29,21 @@ 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(): - return url_parse.query_join(url, name=parsed_args.name) - - return query_url() if parsed_args.name else url + def format_output(columns, data): + return (columns, + (utils.get_item_properties(s, columns) for s in data)) - def show(self, response): - print response.json() if response.status_code < 300 \ - else 'Get failed: {}'.format(response.reason) - -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))) |