Skip to content

Commit 7b9e2ce

Browse files
finalizing color for tables, fixing all the unit tests I broke, and all the other little things that required changing
1 parent 7628bdd commit 7b9e2ce

File tree

13 files changed

+47
-65
lines changed

13 files changed

+47
-65
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
strategy:
1212
matrix:
13-
python-version: [3.6,3.7,3.8,3.9]
13+
python-version: [3.7,3.8,3.9,3.10]
1414

1515
steps:
1616
- uses: actions/checkout@v2

SoftLayer/CLI/dns/zone_import.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from SoftLayer.CLI.command import SLCommand as SLCommand
99
from SoftLayer.CLI import environment
1010
from SoftLayer.CLI import exceptions
11-
from SoftLayer.CLI import formatting
1211
from SoftLayer.CLI import helpers
1312

1413
RECORD_REGEX = re.compile(r"""^((?P<domain>(([\w-]+|\*)(\.)?)*|\@)?\s+

SoftLayer/CLI/environment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def out(self, output):
4949
try:
5050
self.console.print_json(output)
5151
# Tried to print not-json, so just print it out normally...
52-
except JSONDecodeError as ex:
52+
except JSONDecodeError:
5353
click.echo(output)
5454
elif self.format == 'jsonraw':
5555
# Using Rich here is problematic because in the unit tests it thinks the terminal is 80 characters wide
@@ -66,7 +66,7 @@ def out(self, output):
6666
def err(self, output, newline=True):
6767
"""Outputs an error string to the console (stderr)."""
6868

69-
self.err_console.print(output, overflow='ignore', new_line_start=newline)
69+
self.err_console.print(output, new_line_start=newline)
7070

7171
def fmt(self, output, fmt=None):
7272
"""Format output based on current the environment format."""

SoftLayer/CLI/formatting.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def format_output(data, fmt='table'): # pylint: disable=R0911,R0912
3030
elif fmt == 'jsonraw':
3131
return json.dumps(data, cls=CLIJSONEncoder)
3232

33-
if isinstance(data, str) or isinstance(data, rTable):
33+
if isinstance(data, str) or isinstance(data, rTable):
3434
return data
3535

3636
# responds to .prettytable()
@@ -228,7 +228,7 @@ class Table(object):
228228
:param list columns: a list of column names
229229
"""
230230

231-
def __init__(self, columns, title=None, align={}):
231+
def __init__(self, columns, title=None, align=None):
232232
duplicated_cols = [col for col, count
233233
in collections.Counter(columns).items()
234234
if count > 1]
@@ -238,13 +238,10 @@ def __init__(self, columns, title=None, align={}):
238238

239239
self.columns = columns
240240
self.rows = []
241-
self.align = align
241+
self.align = align or {}
242242
self.sortby = None
243243
self.title = title
244244

245-
def __str__(self):
246-
print("OK")
247-
248245
def add_row(self, row):
249246
"""Add a row to the table.
250247
@@ -264,6 +261,16 @@ def to_python(self):
264261
def prettytable(self):
265262
"""Returns a RICH table instance."""
266263
table = rTable(title=self.title, box=box.SQUARE, header_style="bright_cyan")
264+
if self.sortby:
265+
try:
266+
# https://docs.python.org/3/howto/sorting.html#key-functions
267+
sort_index = self.columns.index(self.sortby)
268+
# All the values in `rows` are strings, so we need to cast to int for sorting purposes.
269+
self.rows.sort(key=lambda the_row: the_row[sort_index] if not the_row[sort_index].isdigit()
270+
else int(the_row[sort_index]))
271+
except ValueError as ex:
272+
msg = "Column (%s) doesn't exist to sort by" % self.sortby
273+
raise exceptions.CLIAbort(msg) from ex
267274

268275
for col in self.columns:
269276
justify = "center"
@@ -322,7 +329,7 @@ def __str__(self):
322329
"""returns the formatted value."""
323330
# If the original value is None, represent this as 'NULL'
324331
if self.original is None:
325-
return self.formatted or "NULL"
332+
return "NULL"
326333

327334
try:
328335
return str(self.original)

SoftLayer/CLI/virt/bandwidth.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,11 @@ def cli(env, identifier, start_date, end_date, summary_period, quite_summary):
3232
3333
Example::
3434
35-
slcli hw bandwidth 1234 -s 2019-05-01T00:01 -e 2019-05-02T00:00:01.00000-12:00
35+
slcli vs bandwidth 1234 -s 2019-05-01T00:01 -e 2019-05-02T00:00:01.00000-12:00
3636
"""
3737
vsi = SoftLayer.VSManager(env.client)
3838
vsi_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS')
3939

40-
# Summary period is broken for virtual guests, check VIRT-11733 for a resolution.
41-
# For now, we are going to ignore summary_period and set it to the default the API imposes
42-
if summary_period != 300:
43-
click.secho("""The Summary Period option is currently set to the 300s as the backend API will throw an exception
44-
any other value. This should be resolved in the next version of the slcli.""", fg='yellow')
45-
summary_period = 300
4640
data = vsi.get_bandwidth_data(vsi_id, start_date, end_date, None, None)
4741

4842
title = "Bandwidth Report: %s - %s" % (start_date, end_date)

SoftLayer/utils.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88
import collections
99
import datetime
10-
from json import JSONDecoder, JSONDecodeError
10+
from json import JSONDecoder
1111
import re
1212
import time
1313

@@ -437,6 +437,8 @@ def clean_dict(dictionary):
437437

438438

439439
NOT_WHITESPACE = re.compile(r'[^\s]')
440+
441+
440442
def decode_stacked(document, pos=0, decoder=JSONDecoder()):
441443
"""Used for converting CLI output to JSON datastructures. Specially for unit tests
442444
@@ -452,10 +454,6 @@ def decode_stacked(document, pos=0, decoder=JSONDecoder()):
452454
if not match:
453455
return
454456
pos = match.start()
455-
456-
try:
457-
obj, pos = decoder.raw_decode(document, pos)
458-
except JSONDecodeError:
459-
# do something sensible if there's some error
460-
raise
461-
yield obj
457+
obj, pos = decoder.raw_decode(document, pos)
458+
459+
yield obj

tests/CLI/environment_tests.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"""
77

88
import click
9-
from rich.console import Console
109
from unittest import mock as mock
1110

1211
from SoftLayer.CLI import environment

tests/CLI/helper_tests.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import tempfile
1212

1313
import click
14+
from rich.table import Table
1415
from unittest import mock as mock
1516

1617
from SoftLayer.CLI import core
@@ -266,19 +267,6 @@ def test_format_output_string(self):
266267
t = formatting.format_output('just a string', 'raw')
267268
self.assertEqual('just a string', t)
268269

269-
t = formatting.format_output(b'just a string', 'raw')
270-
self.assertEqual(b'just a string', t)
271-
272-
def test_format_output_raw(self):
273-
t = formatting.Table(['nothing'])
274-
t.align['nothing'] = 'c'
275-
t.add_row(['testdata'])
276-
t.sortby = 'nothing'
277-
ret = formatting.format_output(t, 'raw')
278-
279-
self.assertNotIn('nothing', str(ret))
280-
self.assertIn('testdata', str(ret))
281-
282270
def test_format_output_json(self):
283271
t = formatting.Table(['nothing'])
284272
t.align['nothing'] = 'c'
@@ -342,21 +330,19 @@ def test_format_output_formatted_item(self):
342330
def test_format_output_list(self):
343331
item = ['this', 'is', 'a', 'list']
344332
ret = formatting.format_output(item, 'table')
345-
self.assertEqual(os.linesep.join(item), ret)
333+
self.assertEqual("['this', 'is', 'a', 'list']", str(ret))
346334

347335
def test_format_output_table(self):
348336
t = formatting.Table(['nothing'])
349337
t.align['nothing'] = 'c'
350338
t.add_row(['testdata'])
351339
t.sortby = 'nothing'
352340
ret = formatting.format_output(t, 'table')
353-
354-
self.assertIn('nothing', str(ret))
355-
self.assertIn('testdata', str(ret))
341+
self.assertIsInstance(ret, Table)
356342

357343
def test_unknown(self):
358344
t = formatting.format_output({}, 'raw')
359-
self.assertEqual({}, t)
345+
self.assertEqual('{}', t)
360346

361347
def test_sequentialoutput(self):
362348
# specifying the separator prevents windows from using \n\r
@@ -380,7 +366,7 @@ def test_format_output_python(self):
380366
self.assertEqual(['just a string'], t)
381367

382368
t = formatting.format_output({'test_key': 'test_value'}, 'python')
383-
self.assertEqual({'test_key': 'test_value'}, t)
369+
self.assertEqual("{'test_key': 'test_value'}", t)
384370

385371
def test_format_output_python_keyvaluetable(self):
386372
t = formatting.KeyValueTable(['key', 'value'])

tests/CLI/modules/block_tests.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
:license: MIT, see LICENSE for more details.
66
"""
77
from SoftLayer.CLI import exceptions
8-
from SoftLayer.CLI import formatting
98
from SoftLayer import SoftLayerAPIError
109
from SoftLayer import testing
1110

tests/CLI/modules/call_api_tests.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,11 @@ def test_call_api_orderBy(self):
314314
'value': ['DESC']}]},
315315
'typeId': {'operation': 1}}
316316
})
317+
317318
def test_very_verbose(self):
318-
result = self.run_command(['call-api', '-vvv', 'Account', 'getObject'])
319+
result = self.run_command(['-vvv', 'call-api', 'Account', 'getObject'])
319320
self.assert_no_fail(result)
320321
self.assert_called_with('SoftLayer_Account', 'getObject')
321-
print(result.output)
322-
self.assertEqual(result.output, "soething")
322+
self.assertIn("ORIGIN_PULL", result.output)
323+
self.assertIn("python_version", result.output)
324+
self.assertIn("offset", result.output)

0 commit comments

Comments
 (0)