From 4bb55ce75dc76d84027941159e944fa97ddf5117 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Wed, 4 Nov 2020 11:18:47 +0000 Subject: [PATCH] formatters: Cast columns before outputting Libraries like 'novaclient' return non-primitive types for many operations. These are typically wrappers around primitive types, e.g. 'OrderedDict', but they all break YAML output formatting since PyYAML is only compatible with primitive types when operating in safe mode. The solution is rather simple: simply cast the values for these to their expected format. Change-Id: Ib7db6a25f2352a013cb2ce603e60ca48b6cc70e6 Signed-off-by: Stephen Finucane Story: #2008320 Task: #41217 --- osc_lib/cli/format_columns.py | 6 ++++++ osc_lib/tests/cli/test_format_columns.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/osc_lib/cli/format_columns.py b/osc_lib/cli/format_columns.py index ef2bcc5..7968592 100644 --- a/osc_lib/cli/format_columns.py +++ b/osc_lib/cli/format_columns.py @@ -26,6 +26,9 @@ class DictColumn(columns.FormattableColumn): def human_readable(self): return utils.format_dict(self._value) + def machine_readable(self): + return dict(self._value or {}) + class DictListColumn(columns.FormattableColumn): """Format column for dict, key is string, value is list""" @@ -33,6 +36,9 @@ class DictListColumn(columns.FormattableColumn): def human_readable(self): return utils.format_dict_of_list(self._value) + def machine_readable(self): + return dict(self._value or {}) + class ListColumn(columns.FormattableColumn): """Format column for list content""" diff --git a/osc_lib/tests/cli/test_format_columns.py b/osc_lib/tests/cli/test_format_columns.py index df44500..9df47dc 100644 --- a/osc_lib/tests/cli/test_format_columns.py +++ b/osc_lib/tests/cli/test_format_columns.py @@ -13,6 +13,8 @@ # under the License. # +import collections + from osc_lib.cli import format_columns from osc_lib.tests import utils @@ -28,6 +30,13 @@ class TestDictColumn(utils.TestCase): self.assertEqual(dict_content, col.machine_readable()) self.assertEqual("key1='value1', key2='value2'", col.human_readable()) + def test_complex_object(self): + """Non-primitive objects should be converted to a dict.""" + dict_content = collections.OrderedDict( + [('key1', 'value1'), ('key2', 'value2')]) + col = format_columns.DictColumn(dict_content) + self.assertIsInstance(col._value, dict) + class TestDictListColumn(utils.TestCase): @@ -40,6 +49,13 @@ class TestDictListColumn(utils.TestCase): 'public=172.24.4.6, 2001:db8::8', col.human_readable()) + def test_complex_object(self): + """Non-primitive objects should be converted to a dict.""" + dict_content = collections.OrderedDict( + [('key1', ['value1']), ('key2', ['value2'])]) + col = format_columns.DictListColumn(dict_content) + self.assertIsInstance(col._value, dict) + class TestListColumn(utils.TestCase):