From a4c42d72bc60b6c565674454c72b87faeda710c6 Mon Sep 17 00:00:00 2001 From: Kevin McDonald Date: Fri, 26 Aug 2016 11:37:46 -0500 Subject: [PATCH 1/2] Adds a new 'jsonraw' format that will print JSON without indention This makes it easier to parse the JSON, especially for commands that output multiple things. --- SoftLayer/CLI/core.py | 2 +- SoftLayer/CLI/formatting.py | 4 +++- tests/CLI/helper_tests.py | 40 +++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/SoftLayer/CLI/core.py b/SoftLayer/CLI/core.py index aa384309d..02ef1e0f4 100644 --- a/SoftLayer/CLI/core.py +++ b/SoftLayer/CLI/core.py @@ -31,7 +31,7 @@ 3: logging.DEBUG } -VALID_FORMATS = ['table', 'raw', 'json'] +VALID_FORMATS = ['table', 'raw', 'json', 'jsonraw'] DEFAULT_FORMAT = 'raw' if sys.stdout.isatty(): DEFAULT_FORMAT = 'table' diff --git a/SoftLayer/CLI/formatting.py b/SoftLayer/CLI/formatting.py index 15cb8df81..45e3166dc 100644 --- a/SoftLayer/CLI/formatting.py +++ b/SoftLayer/CLI/formatting.py @@ -28,7 +28,7 @@ def format_output(data, fmt='table'): # pylint: disable=R0911,R0912 :param string fmt (optional): One of: table, raw, json, python """ if isinstance(data, utils.string_types): - if fmt == 'json': + if fmt in ('json', 'jsonraw'): return json.dumps(data) return data @@ -46,6 +46,8 @@ def format_output(data, fmt='table'): # pylint: disable=R0911,R0912 format_output(data, fmt='python'), indent=4, cls=CLIJSONEncoder) + elif fmt == 'jsonraw': + return json.dumps(format_output(data, fmt='python'), CLIJSONEncoder) elif fmt == 'python': return data.to_python() diff --git a/tests/CLI/helper_tests.py b/tests/CLI/helper_tests.py index fd0d31823..57577a442 100644 --- a/tests/CLI/helper_tests.py +++ b/tests/CLI/helper_tests.py @@ -306,6 +306,21 @@ def test_format_output_json(self): ret = formatting.format_output('test', 'json') self.assertEqual('"test"', ret) + def test_format_output_jsonraw(self): + t = formatting.Table(['nothing']) + t.align['nothing'] = 'c' + t.add_row(['testdata']) + t.add_row([formatting.blank()]) + t.sortby = 'nothing' + ret = formatting.format_output(t, 'jsonraw') + # This uses json.dumps due to slight changes in the output between + # py3.3 and py3.4 + expected = json.dumps([{'nothing': 'testdata'}, {'nothing': None}]) + self.assertEqual(expected, ret) + + ret = formatting.format_output('test', 'json') + self.assertEqual('"test"', ret) + def test_format_output_json_keyvaluetable(self): t = formatting.KeyValueTable(['key', 'value']) t.add_row(['nothing', formatting.blank()]) @@ -315,6 +330,21 @@ def test_format_output_json_keyvaluetable(self): "nothing": null }''', ret) + def test_format_output_jsonraw_keyvaluetable(self): + t = formatting.KeyValueTable(['key', 'value']) + t.add_row(['nothing', formatting.blank()]) + t.sortby = 'nothing' + ret = formatting.format_output(t, 'jsonraw') + self.assertEqual('''{"nothing": null}''', ret) + + def test_format_output_json_string(self): + ret = formatting.format_output("test", 'json') + self.assertEqual('"test"', ret) + + def test_format_output_jsonraw_string(self): + ret = formatting.format_output("test", 'jsonraw') + self.assertEqual('"test"', ret) + def test_format_output_formatted_item(self): item = formatting.FormattedItem('test', 'test_formatted') ret = formatting.format_output(item, 'table') @@ -380,6 +410,16 @@ def test_format_output_unicode(self): t = formatting.format_output(item, 'raw') self.assertEqual('raw ☃', t) + def test_format_output_table_invalid_sort(self): + t = formatting.Table(['nothing']) + t.align['nothing'] = 'c' + t.add_row(['testdata']) + t.sortby = 'DOES NOT EXIST' + self.assertRaises( + exceptions.CLIHalt, + formatting.format_output, t, 'table', + ) + class TestTemplateArgs(testing.TestCase): From 6fa7dcbfa8d43d306e5fbc2b4186c68fabc22f66 Mon Sep 17 00:00:00 2001 From: Kevin McDonald Date: Fri, 26 Aug 2016 17:56:46 -0500 Subject: [PATCH 2/2] Fixes trivial style issue --- SoftLayer/CLI/formatting.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SoftLayer/CLI/formatting.py b/SoftLayer/CLI/formatting.py index 45e3166dc..59917b007 100644 --- a/SoftLayer/CLI/formatting.py +++ b/SoftLayer/CLI/formatting.py @@ -47,7 +47,8 @@ def format_output(data, fmt='table'): # pylint: disable=R0911,R0912 indent=4, cls=CLIJSONEncoder) elif fmt == 'jsonraw': - return json.dumps(format_output(data, fmt='python'), CLIJSONEncoder) + return json.dumps(format_output(data, fmt='python'), + CLIJSONEncoder) elif fmt == 'python': return data.to_python()