Skip to content

Commit

Permalink
Merge pull request #2180 from allmightyspiff/issues2165
Browse files Browse the repository at this point in the history
Fixed a bug when displaying empty tables.
  • Loading branch information
allmightyspiff authored Aug 9, 2024
2 parents 5650b08 + 3375140 commit 332d3ea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
7 changes: 5 additions & 2 deletions SoftLayer/CLI/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def format_output(data, fmt='table', theme=None): # pylint: disable=R0911,R0912
return output

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


Expand Down Expand Up @@ -318,12 +317,16 @@ def __init__(self, columns, title=None, align=None):
self.sortby = None
self.title = title
# Used to print a message if the table is empty
self.empty_message = None
self.empty_message = "-"

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

def __str__(self):
"""A Table should only be cast to a string if its empty"""
return self.empty_message

def set_empty_message(self, message):
"""Sets the empty message for this table for env.fout
Expand Down
30 changes: 7 additions & 23 deletions SoftLayer/CLI/virt/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@click.option('--passwords',
is_flag=True,
@click.option('--passwords', is_flag=True,
help='Show passwords (check over your shoulder!)')
@click.option('--price', is_flag=True, help='Show associated prices')
@environment.pass_env
Expand Down Expand Up @@ -53,10 +52,7 @@ def cli(env, identifier, passwords=False, price=False):
table.add_row(['active_transaction', formatting.active_txn(result)])
table.add_row(['datacenter', result['datacenter']['name'] or formatting.blank()])
_cli_helper_dedicated_host(env, result, table)
operating_system = utils.lookup(result,
'operatingSystem',
'softwareLicense',
'softwareDescription') or {}
operating_system = utils.lookup(result, 'operatingSystem', 'softwareLicense', 'softwareDescription') or {}
table.add_row(['os', operating_system.get('name', '-')])
table.add_row(['os_version', operating_system.get('version', '-')])
table.add_row(['cores', result['maxCpu']])
Expand All @@ -76,10 +72,7 @@ def cli(env, identifier, passwords=False, price=False):

table.add_row(['last_transaction', last_transaction])
table.add_row(['billing', 'Hourly' if result['hourlyBillingFlag'] else 'Monthly'])
table.add_row(['preset', utils.lookup(result, 'billingItem',
'orderItem',
'preset',
'keyName') or '-'])
table.add_row(['preset', utils.lookup(result, 'billingItem', 'orderItem', 'preset', 'keyName') or '-'])

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

if price:
total_price = utils.lookup(result,
'billingItem',
'nextInvoiceTotalRecurringAmount') or 0
total_price = utils.lookup(result, 'billingItem', 'nextInvoiceTotalRecurringAmount') or 0
if total_price != 0:
table.add_row(['Prices', _price_table(utils.lookup(result, 'billingItem'), total_price)])
table.add_row(['Price rate', total_price])
Expand All @@ -107,10 +98,7 @@ def cli(env, identifier, passwords=False, price=False):
for component in result['softwareComponents']:
for item in component['passwords']:
pass_table.add_row([
utils.lookup(component,
'softwareLicense',
'softwareDescription',
'name'),
utils.lookup(component, 'softwareLicense', 'softwareDescription', 'name'),
item['username'],
item['password'],
])
Expand All @@ -122,10 +110,7 @@ def cli(env, identifier, passwords=False, price=False):
# Test to see if this actually has a primary (public) ip address
try:
if not result['privateNetworkOnlyFlag']:
ptr_domains = env.client.call(
'Virtual_Guest', 'getReverseDomainRecords',
id=vs_id,
)
ptr_domains = env.client.call('Virtual_Guest', 'getReverseDomainRecords', id=vs_id)

for ptr_domain in ptr_domains:
for ptr in ptr_domain['resourceRecords']:
Expand Down Expand Up @@ -196,8 +181,7 @@ def _get_vlan_table(result):

vlan_table = formatting.Table(['type', 'number', 'id'])
for vlan in result['networkVlans']:
vlan_table.add_row([
vlan['networkSpace'], vlan['vlanNumber'], vlan['id']])
vlan_table.add_row([vlan['networkSpace'], vlan['vlanNumber'], vlan['id']])
return ['vlans', vlan_table]


Expand Down
20 changes: 20 additions & 0 deletions tests/CLI/formatting_table_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ def test_key_value_table(self):
result = capture.get()
self.assertEqual(expected, result)

def test_key_value_table_empty(self):

expected = """┌────────┬───────┐
│ name │ value │
├────────┼───────┤
│ table2 │ - │
└────────┴───────┘
"""
table1 = formatting.KeyValueTable(["name", "value"])
table2 = formatting.Table(["one", "two", "three"])
table1.add_row(["table2", table2])
result = formatting.format_output(table1, "table")
console = Console()

with console.capture() as capture:
to_print = formatting.format_output(table1)
console.print(to_print)
result = capture.get()
self.assertEqual(expected, result)

def test_unrenderable_recovery_table(self):
expected = """│ Sub Table │ [<rich.table.Table object at"""
table = formatting.KeyValueTable(["Key", "Value"])
Expand Down

0 comments on commit 332d3ea

Please sign in to comment.