Skip to content

Commit 332d3ea

Browse files
Merge pull request #2180 from allmightyspiff/issues2165
Fixed a bug when displaying empty tables.
2 parents 5650b08 + 3375140 commit 332d3ea

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

SoftLayer/CLI/formatting.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ def format_output(data, fmt='table', theme=None): # pylint: disable=R0911,R0912
7070
return output
7171

7272
# fallback, convert this odd object to a string
73-
# print(f"Casting this to string {data}")
7473
return str(data)
7574

7675

@@ -318,12 +317,16 @@ def __init__(self, columns, title=None, align=None):
318317
self.sortby = None
319318
self.title = title
320319
# Used to print a message if the table is empty
321-
self.empty_message = None
320+
self.empty_message = "-"
322321

323322
def __bool__(self):
324323
"""Useful for seeing if the table has any rows"""
325324
return len(self.rows) > 0
326325

326+
def __str__(self):
327+
"""A Table should only be cast to a string if its empty"""
328+
return self.empty_message
329+
327330
def set_empty_message(self, message):
328331
"""Sets the empty message for this table for env.fout
329332

SoftLayer/CLI/virt/detail.py

+7-23
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
1919
@click.argument('identifier')
20-
@click.option('--passwords',
21-
is_flag=True,
20+
@click.option('--passwords', is_flag=True,
2221
help='Show passwords (check over your shoulder!)')
2322
@click.option('--price', is_flag=True, help='Show associated prices')
2423
@environment.pass_env
@@ -53,10 +52,7 @@ def cli(env, identifier, passwords=False, price=False):
5352
table.add_row(['active_transaction', formatting.active_txn(result)])
5453
table.add_row(['datacenter', result['datacenter']['name'] or formatting.blank()])
5554
_cli_helper_dedicated_host(env, result, table)
56-
operating_system = utils.lookup(result,
57-
'operatingSystem',
58-
'softwareLicense',
59-
'softwareDescription') or {}
55+
operating_system = utils.lookup(result, 'operatingSystem', 'softwareLicense', 'softwareDescription') or {}
6056
table.add_row(['os', operating_system.get('name', '-')])
6157
table.add_row(['os_version', operating_system.get('version', '-')])
6258
table.add_row(['cores', result['maxCpu']])
@@ -76,10 +72,7 @@ def cli(env, identifier, passwords=False, price=False):
7672

7773
table.add_row(['last_transaction', last_transaction])
7874
table.add_row(['billing', 'Hourly' if result['hourlyBillingFlag'] else 'Monthly'])
79-
table.add_row(['preset', utils.lookup(result, 'billingItem',
80-
'orderItem',
81-
'preset',
82-
'keyName') or '-'])
75+
table.add_row(['preset', utils.lookup(result, 'billingItem', 'orderItem', 'preset', 'keyName') or '-'])
8376

8477
table.add_row(_get_owner_row(result))
8578
table.add_row(_get_vlan_table(result))
@@ -94,9 +87,7 @@ def cli(env, identifier, passwords=False, price=False):
9487
table.add_row(['notes', result.get('notes', '-')])
9588

9689
if price:
97-
total_price = utils.lookup(result,
98-
'billingItem',
99-
'nextInvoiceTotalRecurringAmount') or 0
90+
total_price = utils.lookup(result, 'billingItem', 'nextInvoiceTotalRecurringAmount') or 0
10091
if total_price != 0:
10192
table.add_row(['Prices', _price_table(utils.lookup(result, 'billingItem'), total_price)])
10293
table.add_row(['Price rate', total_price])
@@ -107,10 +98,7 @@ def cli(env, identifier, passwords=False, price=False):
10798
for component in result['softwareComponents']:
10899
for item in component['passwords']:
109100
pass_table.add_row([
110-
utils.lookup(component,
111-
'softwareLicense',
112-
'softwareDescription',
113-
'name'),
101+
utils.lookup(component, 'softwareLicense', 'softwareDescription', 'name'),
114102
item['username'],
115103
item['password'],
116104
])
@@ -122,10 +110,7 @@ def cli(env, identifier, passwords=False, price=False):
122110
# Test to see if this actually has a primary (public) ip address
123111
try:
124112
if not result['privateNetworkOnlyFlag']:
125-
ptr_domains = env.client.call(
126-
'Virtual_Guest', 'getReverseDomainRecords',
127-
id=vs_id,
128-
)
113+
ptr_domains = env.client.call('Virtual_Guest', 'getReverseDomainRecords', id=vs_id)
129114

130115
for ptr_domain in ptr_domains:
131116
for ptr in ptr_domain['resourceRecords']:
@@ -196,8 +181,7 @@ def _get_vlan_table(result):
196181

197182
vlan_table = formatting.Table(['type', 'number', 'id'])
198183
for vlan in result['networkVlans']:
199-
vlan_table.add_row([
200-
vlan['networkSpace'], vlan['vlanNumber'], vlan['id']])
184+
vlan_table.add_row([vlan['networkSpace'], vlan['vlanNumber'], vlan['id']])
201185
return ['vlans', vlan_table]
202186

203187

tests/CLI/formatting_table_tests.py

+20
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ def test_key_value_table(self):
4848
result = capture.get()
4949
self.assertEqual(expected, result)
5050

51+
def test_key_value_table_empty(self):
52+
53+
expected = """┌────────┬───────┐
54+
│ name │ value │
55+
├────────┼───────┤
56+
│ table2 │ - │
57+
└────────┴───────┘
58+
"""
59+
table1 = formatting.KeyValueTable(["name", "value"])
60+
table2 = formatting.Table(["one", "two", "three"])
61+
table1.add_row(["table2", table2])
62+
result = formatting.format_output(table1, "table")
63+
console = Console()
64+
65+
with console.capture() as capture:
66+
to_print = formatting.format_output(table1)
67+
console.print(to_print)
68+
result = capture.get()
69+
self.assertEqual(expected, result)
70+
5171
def test_unrenderable_recovery_table(self):
5272
expected = """│ Sub Table │ [<rich.table.Table object at"""
5373
table = formatting.KeyValueTable(["Key", "Value"])

0 commit comments

Comments
 (0)