def parse_resource(client, skip_deprecated=False): subparsers = client.parser.add_subparsers( dest='resource', metavar='resource', ) # check if the user is running a custom command for command in CustomCommand.__subclasses__(): client.subparsers[command.name] = subparsers.add_parser( command.name, help=command.help_text) if hasattr(client, 'v2'): for k in client.v2.json.keys(): if k in ('dashboard', ): # the Dashboard API is deprecated and not supported continue # argparse aliases are *only* supported in Python3 (not 2.7) kwargs = {} if not skip_deprecated: if k in DEPRECATED_RESOURCES: kwargs['aliases'] = [DEPRECATED_RESOURCES[k]] client.subparsers[k] = subparsers.add_parser(k, help='', **kwargs) resource = client.parser.parse_known_args()[0].resource if resource in DEPRECATED_RESOURCES.values(): client.argv[client.argv.index( resource)] = DEPRECATED_RESOURCES_REVERSE[resource] resource = DEPRECATED_RESOURCES_REVERSE[resource] if resource in CustomCommand.registry: parser = client.subparsers[resource] command = CustomCommand.registry[resource]() response = command.handle(client, parser) if response: _filter = client.get_config('filter') if (resource == 'config' and client.get_config('format') == 'human'): response = { 'count': len(response), 'results': [{ 'key': k, 'value': v } for k, v in response.items()] } _filter = 'key, value' try: connection = client.root.connection except AttributeError: connection = None formatted = format_response(Page.from_json(response, connection=connection), fmt=client.get_config('format'), filter=_filter) print(formatted) raise SystemExit() else: return resource
def parse_resource(client, skip_deprecated=False): subparsers = client.parser.add_subparsers( dest='resource', metavar='resource', ) # check if the user is running a custom command for command in CustomCommand.__subclasses__(): client.subparsers[command.name] = subparsers.add_parser( command.name, help=command.help_text ) if hasattr(client, 'v2'): for k in client.v2.json.keys(): if k in ('dashboard',): # the Dashboard API is deprecated and not supported continue # argparse aliases are *only* supported in Python3 (not 2.7) kwargs = {} if not skip_deprecated and PY3: if k in DEPRECATED_RESOURCES: kwargs['aliases'] = [DEPRECATED_RESOURCES[k]] client.subparsers[k] = subparsers.add_parser( k, help='', **kwargs ) try: resource = client.parser.parse_known_args()[0].resource except SystemExit: if PY3: raise else: # Unfortunately, argparse behavior between py2 and py3 # changed in a notable way when required subparsers # have invalid (or missing) arguments specified # see: https://github.com/python/cpython/commit/f97c59aaba2d93e48cbc6d25f7ff9f9c87f8d0b2 # In py2, this raises a SystemExit; which we want to _ignore_ resource = None if resource in DEPRECATED_RESOURCES.values(): client.argv[ client.argv.index(resource) ] = DEPRECATED_RESOURCES_REVERSE[resource] resource = DEPRECATED_RESOURCES_REVERSE[resource] if resource in CustomCommand.registry: parser = client.subparsers[resource] command = CustomCommand.registry[resource]() response = command.handle(client, parser) if response: formatted = format_response( Page.from_json(response), fmt=client.get_config('format'), filter=client.get_config('filter'), ) print(formatted) raise SystemExit() else: return resource
def test_yaml_list(): users = { 'results': [ { 'username': '******' }, { 'username': '******' }, { 'username': '******' }, ] } page = Users.from_json(users) formatted = format_response(page, fmt='yaml') assert yaml.safe_load(formatted) == users
def test_json_list(): users = { 'results': [ { 'username': '******' }, { 'username': '******' }, { 'username': '******' }, ] } page = Users.from_json(users) formatted = format_response(page) assert json.loads(formatted) == users
def parse_resource(client, skip_deprecated=False): subparsers = client.parser.add_subparsers( dest='resource', metavar='resource', ) # check if the user is running a custom command for command in CustomCommand.__subclasses__(): client.subparsers[command.name] = subparsers.add_parser( command.name, help=command.help_text) if hasattr(client, 'v2'): for k in client.v2.json.keys(): if k in ('dashboard', ): # the Dashboard API is deprecated and not supported continue aliases = [] if not skip_deprecated: if k in DEPRECATED_RESOURCES: aliases = [DEPRECATED_RESOURCES[k]] client.subparsers[k] = subparsers.add_parser(k, help='', aliases=aliases) resource = client.parser.parse_known_args()[0].resource if resource in DEPRECATED_RESOURCES.values(): client.argv[client.argv.index( resource)] = DEPRECATED_RESOURCES_REVERSE[resource] resource = DEPRECATED_RESOURCES_REVERSE[resource] if resource in CustomCommand.registry: parser = client.subparsers[resource] command = CustomCommand.registry[resource]() response = command.handle(client, parser) if response: formatted = format_response( Page.from_json(response), fmt=client.get_config('format'), filter=client.get_config('filter'), ) print(formatted) raise SystemExit() else: return resource
def test_yaml_empty_list(): page = Page.from_json({'results': []}) formatted = format_response(page, fmt='yaml') assert yaml.safe_load(formatted) == {'results': []}
def test_json_empty_list(): page = Page.from_json({'results': []}) formatted = format_response(page) assert json.loads(formatted) == {'results': []}