Skip to content

Commit 7aca9ce

Browse files
authored
Adds a new 'jsonraw' format that will print JSON without indention (softlayer#766)
* 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. * Fixes trivial style issue
1 parent 3aec9ec commit 7aca9ce

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

SoftLayer/CLI/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
3: logging.DEBUG
3232
}
3333

34-
VALID_FORMATS = ['table', 'raw', 'json']
34+
VALID_FORMATS = ['table', 'raw', 'json', 'jsonraw']
3535
DEFAULT_FORMAT = 'raw'
3636
if sys.stdout.isatty():
3737
DEFAULT_FORMAT = 'table'

SoftLayer/CLI/formatting.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def format_output(data, fmt='table'): # pylint: disable=R0911,R0912
2828
:param string fmt (optional): One of: table, raw, json, python
2929
"""
3030
if isinstance(data, utils.string_types):
31-
if fmt == 'json':
31+
if fmt in ('json', 'jsonraw'):
3232
return json.dumps(data)
3333
return data
3434

@@ -46,6 +46,9 @@ def format_output(data, fmt='table'): # pylint: disable=R0911,R0912
4646
format_output(data, fmt='python'),
4747
indent=4,
4848
cls=CLIJSONEncoder)
49+
elif fmt == 'jsonraw':
50+
return json.dumps(format_output(data, fmt='python'),
51+
CLIJSONEncoder)
4952
elif fmt == 'python':
5053
return data.to_python()
5154

tests/CLI/helper_tests.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,21 @@ def test_format_output_json(self):
305305
ret = formatting.format_output('test', 'json')
306306
self.assertEqual('"test"', ret)
307307

308+
def test_format_output_jsonraw(self):
309+
t = formatting.Table(['nothing'])
310+
t.align['nothing'] = 'c'
311+
t.add_row(['testdata'])
312+
t.add_row([formatting.blank()])
313+
t.sortby = 'nothing'
314+
ret = formatting.format_output(t, 'jsonraw')
315+
# This uses json.dumps due to slight changes in the output between
316+
# py3.3 and py3.4
317+
expected = json.dumps([{'nothing': 'testdata'}, {'nothing': None}])
318+
self.assertEqual(expected, ret)
319+
320+
ret = formatting.format_output('test', 'json')
321+
self.assertEqual('"test"', ret)
322+
308323
def test_format_output_json_keyvaluetable(self):
309324
t = formatting.KeyValueTable(['key', 'value'])
310325
t.add_row(['nothing', formatting.blank()])
@@ -314,6 +329,21 @@ def test_format_output_json_keyvaluetable(self):
314329
"nothing": null
315330
}''', ret)
316331

332+
def test_format_output_jsonraw_keyvaluetable(self):
333+
t = formatting.KeyValueTable(['key', 'value'])
334+
t.add_row(['nothing', formatting.blank()])
335+
t.sortby = 'nothing'
336+
ret = formatting.format_output(t, 'jsonraw')
337+
self.assertEqual('''{"nothing": null}''', ret)
338+
339+
def test_format_output_json_string(self):
340+
ret = formatting.format_output("test", 'json')
341+
self.assertEqual('"test"', ret)
342+
343+
def test_format_output_jsonraw_string(self):
344+
ret = formatting.format_output("test", 'jsonraw')
345+
self.assertEqual('"test"', ret)
346+
317347
def test_format_output_formatted_item(self):
318348
item = formatting.FormattedItem('test', 'test_formatted')
319349
ret = formatting.format_output(item, 'table')
@@ -379,6 +409,16 @@ def test_format_output_unicode(self):
379409
t = formatting.format_output(item, 'raw')
380410
self.assertEqual('raw ☃', t)
381411

412+
def test_format_output_table_invalid_sort(self):
413+
t = formatting.Table(['nothing'])
414+
t.align['nothing'] = 'c'
415+
t.add_row(['testdata'])
416+
t.sortby = 'DOES NOT EXIST'
417+
self.assertRaises(
418+
exceptions.CLIHalt,
419+
formatting.format_output, t, 'table',
420+
)
421+
382422

383423
class TestTemplateArgs(testing.TestCase):
384424

0 commit comments

Comments
 (0)