mirror of
https://github.com/ansible/awx.git
synced 2026-03-01 08:48:46 -03:30
replace the (optional) tabulate dependency w/ a simple table printer
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import locale
|
||||
import json
|
||||
from distutils.util import strtobool
|
||||
|
||||
@@ -136,32 +137,49 @@ def format_yaml(output, fmt):
|
||||
|
||||
|
||||
def format_human(output, fmt):
|
||||
lines = []
|
||||
if fmt == '.':
|
||||
fmt = 'id,name'
|
||||
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:
|
||||
output = output['results']
|
||||
else:
|
||||
output = [output]
|
||||
|
||||
return tabulate(
|
||||
[
|
||||
dict(
|
||||
(col, record.get(col, ''))
|
||||
for col in column_names
|
||||
)
|
||||
for record in output
|
||||
],
|
||||
headers='keys',
|
||||
tablefmt='rst'
|
||||
)
|
||||
if fmt == '*' and len(output):
|
||||
column_names = list(output[0].keys())
|
||||
for k in ('summary_fields', 'related'):
|
||||
if k in column_names:
|
||||
column_names.remove(k)
|
||||
|
||||
table = [column_names]
|
||||
table.extend([
|
||||
[record.get(col, '') for col in column_names]
|
||||
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 = {
|
||||
|
||||
Reference in New Issue
Block a user