in modular-service-cli/modular_service_cli/group/__init__.py [0:0]
def __call__(self, func: Callable) -> Callable:
@wraps(func)
def wrapper(*args, **kwargs):
modular_mode = False
if Path(__file__).parents[
3].name == 'modules': # TODO check some other way
modular_mode = True
json_view = kwargs.pop('json')
verbose = kwargs.pop('verbose')
if verbose:
write_verbose_logs()
ctx = cast(click.Context, click.get_current_context())
self.update_context(ctx)
try:
self._check_context(ctx)
resp: ApiResponse = click.pass_obj(func)(*args, **kwargs)
except click.ClickException as e:
_LOG.info('Click exception has occurred')
resp = ApiResponse.build(e.format_message())
except Exception as e:
_LOG.exception('Unexpected error has occurred')
resp = ApiResponse.build(str(e))
if modular_mode:
_LOG.info('The cli is installed as a module. '
'Returning m3 modular cli response')
formatted = ModularResponseProcessor().format(resp)
return json.dumps(formatted, separators=(',', ':'))
if not json_view: # table view
_LOG.info('Returning table view')
prepared = TableResponseProcessor().format(resp)
trace_id = resp.trace_id
next_token = (resp.data or {}).get(NEXT_TOKEN_ATTR)
try:
printer = TablePrinter(
attributes_order=self._attributes_order
)
table = printer.print(prepared)
except ColumnOverflow as ce:
_LOG.info(f'Awaiting user to respond to - {ce!r}.')
to_revert = click.prompt(
REVERT_TO_JSON_MESSAGE,
type=click.Choice(('y', 'n'))
)
if to_revert == 'n':
table = ce.table
else:
table, json_view = None, True
if table:
if verbose:
click.echo(f'Trace id: \'{trace_id}\'')
if next_token:
click.echo(f'Next token: \'{next_token}\'')
click.echo(table)
_LOG.info(f'Finished request: \'{trace_id}\'')
if json_view:
_LOG.info('Returning json view')
data = JsonResponseProcessor().format(resp)
click.echo(json.dumps(data, indent=2))
return wrapper