From d607fd3904180cb4ed4a7bcd4170f6ae16b0b1cd Mon Sep 17 00:00:00 2001 From: TerryHowe Date: Thu, 16 Apr 2015 06:22:38 -0600 Subject: [PATCH] Add module name to repr string Add the module name to the repr string so we can better identify the resource that created the string. This is also convenient for recreating the object from a string such as: python examples/get.py openstack/compute/v2/server.py --data "openstack.compute.v2.server.Server(attrs={u'id': u'85d8ecf3-3757-43fd-b7a2-eb6636f7af1c', u'name': u'jenkins'}, loaded=True)" Change-Id: I7a8bdabd8150e0bd06ec46deecf81eaa126907ab --- examples/common.py | 9 ++++++++- openstack/resource.py | 5 +++-- openstack/tests/unit/test_resource.py | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/examples/common.py b/examples/common.py index 6980e0db..f203323c 100755 --- a/examples/common.py +++ b/examples/common.py @@ -72,7 +72,14 @@ def get_data_option(opts): iddy = uuid.UUID(opts.data) return {'id': iddy} except ValueError: - return eval(opts.data) + data = opts.data + if data.startswith('openstack.'): + fullname = data.split('(')[0] + classname = fullname.split('.')[-1] + modulename = fullname.replace('.' + classname, '') + data = data.replace('openstack.', + '__import__("' + modulename + '").') + return eval(data) def get_open_fds(): diff --git a/openstack/resource.py b/openstack/resource.py index 42888113..12677352 100644 --- a/openstack/resource.py +++ b/openstack/resource.py @@ -257,8 +257,9 @@ class Resource(collections.MutableMapping): self.update_attrs(attrs) def __repr__(self): - return "%s(attrs=%s, loaded=%s)" % (self.__class__.__name__, - self._attrs, self._loaded) + return "%s.%s(attrs=%s, loaded=%s)" % (self.__module__, + self.__class__.__name__, + self._attrs, self._loaded) @classmethod def get_resource_name(cls): diff --git a/openstack/tests/unit/test_resource.py b/openstack/tests/unit/test_resource.py index a576712e..cf43c185 100644 --- a/openstack/tests/unit/test_resource.py +++ b/openstack/tests/unit/test_resource.py @@ -942,6 +942,7 @@ class ResourceTests(base.TestTransportBase): fr.second = "hi" fr.third = "nah" the_repr = repr(fr) + the_repr = the_repr.replace('openstack.tests.unit.test_resource.', '') result = eval(the_repr) self.assertEqual(fr._loaded, result._loaded) self.assertEqual(fr.first, result.first)