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
This commit is contained in:
TerryHowe
2015-04-16 06:22:38 -06:00
parent 8fa558604a
commit d607fd3904
3 changed files with 12 additions and 3 deletions

View File

@@ -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():

View File

@@ -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):

View File

@@ -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)