From 48a017ea17bfebd0194f99f407d0ab4cd3277fff Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Tue, 14 Apr 2015 12:40:21 -0700 Subject: [PATCH] Start the conversion to oslo.versionedobjects This is a first commit to make a mostly trivial step towards using oslo.versionedobjects in Nova. It substitutes our dict compat class for the one in the library, and officially makes the library a requirement. Related to blueprint use-oslo-objects Change-Id: Ie23108123ac774b421a050c3aa5d1c29b27d6eb3 --- nova/objects/base.py | 76 +------------------------ nova/tests/unit/objects/test_objects.py | 8 --- requirements.txt | 1 + 3 files changed, 4 insertions(+), 81 deletions(-) diff --git a/nova/objects/base.py b/nova/objects/base.py index ce4df5b512f8..e2436a5487f3 100644 --- a/nova/objects/base.py +++ b/nova/objects/base.py @@ -25,6 +25,7 @@ import netaddr from oslo_log import log as logging import oslo_messaging as messaging from oslo_utils import timeutils +from oslo_versionedobjects import base as ovoo_base import six from nova import context @@ -39,10 +40,6 @@ from nova import utils LOG = logging.getLogger('object') -class NotSpecifiedSentinel(object): - pass - - def get_attrname(name): """Return the mangled name of the attribute's underlying storage.""" return '_' + name @@ -649,75 +646,8 @@ class NovaObject(object): self._context = original_context -class NovaObjectDictCompat(object): - """Mix-in to provide dictionary key access compat - - If an object needs to support attribute access using - dictionary items instead of object attributes, inherit - from this class. This should only be used as a temporary - measure until all callers are converted to use modern - attribute access. - - NOTE(berrange) This class will eventually be deleted. - """ - - # dictish syntactic sugar - def iteritems(self): - """For backwards-compatibility with dict-based objects. - - NOTE(danms): May be removed in the future. - """ - for name in self.obj_fields: - if (self.obj_attr_is_set(name) or - name in self.obj_extra_fields): - yield name, getattr(self, name) - - items = lambda self: list(self.iteritems()) - - def __getitem__(self, name): - """For backwards-compatibility with dict-based objects. - - NOTE(danms): May be removed in the future. - """ - return getattr(self, name) - - def __setitem__(self, name, value): - """For backwards-compatibility with dict-based objects. - - NOTE(danms): May be removed in the future. - """ - setattr(self, name, value) - - def __contains__(self, name): - """For backwards-compatibility with dict-based objects. - - NOTE(danms): May be removed in the future. - """ - try: - return self.obj_attr_is_set(name) - except AttributeError: - return False - - def get(self, key, value=NotSpecifiedSentinel): - """For backwards-compatibility with dict-based objects. - - NOTE(danms): May be removed in the future. - """ - if key not in self.obj_fields: - raise AttributeError("'%s' object has no attribute '%s'" % ( - self.__class__, key)) - if value != NotSpecifiedSentinel and not self.obj_attr_is_set(key): - return value - else: - return getattr(self, key) - - def update(self, updates): - """For backwards-compatibility with dict-base objects. - - NOTE(danms): May be removed in the future. - """ - for key, value in updates.items(): - setattr(self, key, value) +class NovaObjectDictCompat(ovoo_base.VersionedObjectDictCompat): + pass class NovaTimestampObject(object): diff --git a/nova/tests/unit/objects/test_objects.py b/nova/tests/unit/objects/test_objects.py index 37be4d29a0ff..94ebb2c8bdc2 100644 --- a/nova/tests/unit/objects/test_objects.py +++ b/nova/tests/unit/objects/test_objects.py @@ -499,14 +499,6 @@ class _TestObject(object): obj.foo = 'a' self.assertRaises(ValueError, fail) - def test_object_dict_syntax(self): - obj = MyObj(foo=123, bar='bar') - self.assertEqual(obj['foo'], 123) - self.assertEqual(sorted(obj.items(), key=lambda x: x[0]), - [('bar', 'bar'), ('foo', 123)]) - self.assertEqual(sorted(list(obj.iteritems()), key=lambda x: x[0]), - [('bar', 'bar'), ('foo', 123)]) - def test_load(self): obj = MyObj() self.assertEqual(obj.bar, 'loaded!') diff --git a/requirements.txt b/requirements.txt index ea99b9743997..fd76a2dd5cc4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -41,3 +41,4 @@ oslo.i18n>=1.5.0 # Apache-2.0 rfc3986>=0.2.0 # Apache-2.0 oslo.middleware>=1.0.0 # Apache-2.0 psutil>=1.1.1,<2.0.0 +oslo.versionedobjects>=0.1.1