replace the (optional) tabulate dependency w/ a simple table printer

This commit is contained in:
Ryan Petrello
2019-08-21 15:22:27 -04:00
parent ec5d471640
commit 44ff141c23
3 changed files with 40 additions and 24 deletions

View File

@@ -20,16 +20,14 @@ To print results in YAML, specify ``-f yaml``:
Human-Readable (Tabular) Formatting Human-Readable (Tabular) Formatting
----------------------------------- -----------------------------------
|prog| provides *optional* support for printing results in a human-readable |prog| also provides support for printing results in a human-readable
tabular format, but it requires an additional Python software dependency, ASCII table format:
``tabulate``.
To use ``-f human``, you must install the optional dependency via ``pip install tabulate``.
.. code:: bash .. code:: bash
awx jobs list -f human awx jobs list -f human
awx jobs list -f human --filter name,created,status awx jobs list -f human --filter name,created,status
awx jobs list -f human --filter *
Custom Formatting with jq Custom Formatting with jq

View File

@@ -1,3 +1,4 @@
import locale
import json import json
from distutils.util import strtobool from distutils.util import strtobool
@@ -136,32 +137,49 @@ def format_yaml(output, fmt):
def format_human(output, fmt): def format_human(output, fmt):
lines = []
if fmt == '.': if fmt == '.':
fmt = 'id,name' fmt = 'id,name'
column_names = fmt.split(',') column_names = fmt.split(',')
try:
from tabulate import tabulate
except ImportError:
raise ImportError(
'To use `-f human`, you must install the optional tabulate '
'dependency.\n`pip install tabulate`',
)
if 'count' in output: if 'count' in output:
output = output['results'] output = output['results']
else: else:
output = [output] output = [output]
return tabulate( if fmt == '*' and len(output):
[ column_names = list(output[0].keys())
dict( for k in ('summary_fields', 'related'):
(col, record.get(col, '')) if k in column_names:
for col in column_names column_names.remove(k)
)
for record in output table = [column_names]
], table.extend([
headers='keys', [record.get(col, '') for col in column_names]
tablefmt='rst' for record in output
) ])
col_paddings = []
def format_num(v):
try:
return locale.format("%.*f", (0, int(v)), True)
except (ValueError, TypeError):
return v
# calculate the max width of each column
for i, _ in enumerate(column_names):
max_width = max([len(format_num(row[i])) for row in table])
col_paddings.append(max_width)
# insert a row of === header lines
table.insert(1, ['=' * i for i in col_paddings])
# print each row of the table data, justified based on col_paddings
for row in table:
line = ''
for i, value in enumerate(row):
line += format_num(value).ljust(col_paddings[i] + 1)
lines.append(line)
return '\n'.join(lines)
FORMATTERS = { FORMATTERS = {

View File

@@ -69,7 +69,7 @@ setup(
install_requires=requirements, install_requires=requirements,
python_requires=">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*", python_requires=">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*",
extras_require={ extras_require={
'formatting': ['jq', 'tabulate'], 'formatting': ['jq'],
'websockets': ['websocket-client>0.54.0'], 'websockets': ['websocket-client>0.54.0'],
}, },
entry_points={ entry_points={