diff options
author | Bryan Jones <jonesbr@us.ibm.com> | 2015-12-09 20:58:36 +0000 |
---|---|---|
committer | Bryan Jones <jonesbr@us.ibm.com> | 2015-12-10 20:55:31 +0000 |
commit | 9c610d6ef5d720d94b555632cc780603d6e7ac4c (patch) | |
tree | 4f489e42ba45add03bd4be85281292d3ac8f8154 | |
parent | dbf34861a89602abbf371721b5ca1d3676394922 (diff) | |
download | python-heatclient-9c610d6ef5d720d94b555632cc780603d6e7ac4c.tar.gz |
Add JsonFormat display option
JsonFormat is a base class from which commands that expect to dump
json output can inherit. This class defaults to the cliff json_format
formatter, and will not produce any output if no data is provided.
Change-Id: I49abb2172afab802fa1b311bac9e1c66c0373803
-rw-r--r-- | heatclient/common/format_utils.py | 53 | ||||
-rw-r--r-- | heatclient/tests/unit/test_format_utils.py | 91 |
2 files changed, 144 insertions, 0 deletions
diff --git a/heatclient/common/format_utils.py b/heatclient/common/format_utils.py new file mode 100644 index 0000000..2b02fc9 --- /dev/null +++ b/heatclient/common/format_utils.py @@ -0,0 +1,53 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# Copyright 2015 IBM Corp. + +from cliff import show + + +class RawFormat(show.ShowOne): + + def produce_output(self, parsed_args, column_names, data): + if data is None: + return + + self.formatter.emit_one(column_names, data, + self.app.stdout, parsed_args) + + +class JsonFormat(RawFormat): + + @property + def formatter_default(self): + return 'json' + + +class YamlFormat(RawFormat): + + @property + def formatter_default(self): + return 'yaml' + + +class ShellFormat(RawFormat): + + @property + def formatter_default(self): + return 'shell' + + +class ValueFormat(RawFormat): + + @property + def formatter_default(self): + return 'value' diff --git a/heatclient/tests/unit/test_format_utils.py b/heatclient/tests/unit/test_format_utils.py new file mode 100644 index 0000000..8e948ce --- /dev/null +++ b/heatclient/tests/unit/test_format_utils.py @@ -0,0 +1,91 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# Copyright 2015 IBM Corp. + +import json +import yaml + +from heatclient.common import format_utils +from heatclient.tests.unit.osc import utils + + +columns = ['col1', 'col2', 'col3'] +data = ['abcde', ['fg', 'hi', 'jk'], {'lmnop': 'qrstu'}] + + +class ShowJson(format_utils.JsonFormat): + def take_action(self, parsed_args): + return columns, data + + +class ShowYaml(format_utils.YamlFormat): + def take_action(self, parsed_args): + return columns, data + + +class ShowShell(format_utils.ShellFormat): + def take_action(self, parsed_args): + return columns, data + + +class ShowValue(format_utils.ValueFormat): + def take_action(self, parsed_args): + return columns, data + + +class TestFormats(utils.TestCommand): + + def test_json_format(self): + self.cmd = ShowJson(self.app, None) + parsed_args = self.check_parser(self.cmd, [], []) + expected = json.dumps(dict(zip(columns, data)), indent=2) + + self.cmd.run(parsed_args) + + self.assertEqual(expected, self.app.stdout.make_string()) + + def test_yaml_format(self): + self.cmd = ShowYaml(self.app, None) + parsed_args = self.check_parser(self.cmd, [], []) + expected = yaml.safe_dump(dict(zip(columns, data)), + default_flow_style=False) + + self.cmd.run(parsed_args) + + self.assertEqual(expected, self.app.stdout.make_string()) + + def test_shell_format(self): + self.cmd = ShowShell(self.app, None) + parsed_args = self.check_parser(self.cmd, [], []) + expected = '''\ +col1="abcde" +col2="['fg', 'hi', 'jk']" +col3="{'lmnop': 'qrstu'}" +''' + + self.cmd.run(parsed_args) + + self.assertEqual(expected, self.app.stdout.make_string()) + + def test_value_format(self): + self.cmd = ShowValue(self.app, None) + parsed_args = self.check_parser(self.cmd, [], []) + expected = '''\ +abcde +['fg', 'hi', 'jk'] +{'lmnop': 'qrstu'} +''' + + self.cmd.run(parsed_args) + + self.assertEqual(expected, self.app.stdout.make_string()) |