Merge "Start the conversion to oslo.versionedobjects"
This commit is contained in:
commit
3eafa2c05a
|
@ -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):
|
||||
|
|
|
@ -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!')
|
||||
|
|
|
@ -41,3 +41,4 @@ oslo.i18n>=1.5.0 # Apache-2.0
|
|||
rfc3986>=0.2.0 # Apache-2.0
|
||||
oslo.middleware>=1.2.0 # Apache-2.0
|
||||
psutil>=1.1.1,<2.0.0
|
||||
oslo.versionedobjects>=0.1.1
|
||||
|
|
Loading…
Reference in New Issue