From 337514079aa53f6ee1d30d0a5a8fa50b07ce5823 Mon Sep 17 00:00:00 2001 From: Christopher Gallo Date: Fri, 9 Aug 2024 15:39:53 -0500 Subject: [PATCH] Fixed a bug when displaying empty tables. Fixed #2165 --- SoftLayer/CLI/formatting.py | 7 +++++-- SoftLayer/CLI/virt/detail.py | 30 +++++++---------------------- tests/CLI/formatting_table_tests.py | 20 +++++++++++++++++++ 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/SoftLayer/CLI/formatting.py b/SoftLayer/CLI/formatting.py index 0e51eb308..b9eca571e 100644 --- a/SoftLayer/CLI/formatting.py +++ b/SoftLayer/CLI/formatting.py @@ -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) @@ -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 diff --git a/SoftLayer/CLI/virt/detail.py b/SoftLayer/CLI/virt/detail.py index 958c865b1..041375d86 100644 --- a/SoftLayer/CLI/virt/detail.py +++ b/SoftLayer/CLI/virt/detail.py @@ -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 @@ -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']]) @@ -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)) @@ -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]) @@ -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'], ]) @@ -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']: @@ -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] diff --git a/tests/CLI/formatting_table_tests.py b/tests/CLI/formatting_table_tests.py index 4d62a742b..f59764231 100644 --- a/tests/CLI/formatting_table_tests.py +++ b/tests/CLI/formatting_table_tests.py @@ -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 │ [