From b6926bb8e5e07b425809beb5773aa39b64feda98 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Mon, 8 May 2017 09:48:11 -0400 Subject: [PATCH] Add pprint and pformat helper methods Because of munch - using pprint directly is weird. Make a helper method that does what the user expects. Change-Id: I617b75034a59118c170895ad20b86fb8a1eab386 --- shade/_utils.py | 10 ++++++++++ shade/openstackcloud.py | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/shade/_utils.py b/shade/_utils.py index 76b9f2ed3..340b63c08 100644 --- a/shade/_utils.py +++ b/shade/_utils.py @@ -95,6 +95,16 @@ def _make_unicode(input): return str(input) +def _dictify_resource(resource): + if isinstance(resource, list): + return [_dictify_resource(r) for r in resource] + else: + if hasattr(resource, 'toDict'): + return resource.toDict() + else: + return resource + + def _filter_list(data, name_or_id, filters): """Filter a list by name/ID and arbitrary meta data. diff --git a/shade/openstackcloud.py b/shade/openstackcloud.py index aa845383c..8a5ca5457 100644 --- a/shade/openstackcloud.py +++ b/shade/openstackcloud.py @@ -528,6 +528,20 @@ class OpenStackCloud(_normalize.Normalizer): self._raw_clients['volume'] = self._get_raw_client('volume') return self._raw_clients['volume'] + def pprint(self, resource): + """Wrapper aroud pprint that groks munch objects""" + # import late since this is a utility function + import pprint + new_resource = _utils._dictify_resource(resource) + pprint.pprint(new_resource) + + def pformat(self, resource): + """Wrapper aroud pformat that groks munch objects""" + # import late since this is a utility function + import pprint + new_resource = _utils._dictify_resource(resource) + return pprint.pformat(new_resource) + @property def nova_client(self): if self._nova_client is None: