def print_as_table(query_result): """Given a collection of (cluster, token) pairs, formats a table showing the most relevant token fields""" cluster_token_pairs = query_result_to_cluster_token_pairs(query_result) rows = [collections.OrderedDict([("Cluster", cluster), ("Owner", token['owner']), ("Token", token['token']), ("Updated", format_timestamp_string(token['last-update-time']))]) for (cluster, token) in cluster_token_pairs] token_table = tabulate(rows, headers='keys', tablefmt='plain') print(token_table)
def tabulate_token(cluster_name, token, token_name, services, token_etag): """Given a token, returns a string containing tables for the fields""" table = [['Owner', token['owner']]] if token.get('name'): table.append(['Name', token['name']]) if token.get('cpus'): table.append(['CPUs', token['cpus']]) if token.get('mem'): table.append(['Memory', format_mem_field(token)]) if token.get('ports'): table.append(['Ports requested', token['ports']]) if token.get('cmd-type'): table.append(['Command type', token['cmd-type']]) if token.get('health-check-url'): table.append(['Health check endpoint', token['health-check-url']]) if token.get('permitted-user'): table.append(['Permitted user(s)', token['permitted-user']]) explicit_keys = ('cmd', 'cmd-type', 'cpus', 'env', 'health-check-url', 'last-update-time', 'last-update-user', 'mem', 'name', 'owner', 'permitted-user', 'ports') ignored_keys = ('cluster', 'previous', 'root') for key, value in token.items(): if key not in (explicit_keys + ignored_keys): table.append([format_field_name(key), value]) command = token.get('cmd') if command: token_command = f'Command:\n{command}' else: token_command = '<No command specified>' if token.get('env') and len(token['env']) > 0: environment = '\n\nEnvironment:\n%s' % '\n'.join( ['%s=%s' % (k, v) for k, v in token['env'].items()]) else: environment = '' table_text = tabulate(table, tablefmt='plain') last_update_time = format_timestamp_string(token['last-update-time']) last_update_user = f' ({token["last-update-user"]})' if 'last-update-user' in token else '' service_table = tabulate_token_services(services, token_etag, token_name) return f'\n' \ f'=== {terminal.bold(cluster_name)} / {terminal.bold(token_name)} ===\n' \ f'\n' \ f'Last Updated: {last_update_time}{last_update_user}\n' \ f'\n' \ f'{table_text}\n' \ f'\n' \ f'{token_command}' \ f'{environment}' \ f'{service_table}'