8000 Fixed a bug when displaying empty tables. by allmightyspiff · Pull Request #2180 · softlayer/softlayer-python · GitHub
[go: up one dir, main page]

Skip to content

Fixed a bug when displaying empty tables. #2180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading
0