summaryrefslogtreecommitdiffstats
path: root/nova/objects/base.py
diff options
context:
space:
mode:
Diffstat (limited to 'nova/objects/base.py')
-rw-r--r--nova/objects/base.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/nova/objects/base.py b/nova/objects/base.py
index 6f2b6c81f..c6dc8cc1a 100644
--- a/nova/objects/base.py
+++ b/nova/objects/base.py
@@ -353,6 +353,13 @@ class NovaObject(object):
"""
setattr(self, name, value)
+ def __contains__(self, name):
+ """For backwards-compatibility with dict-based objects.
+
+ NOTE(danms): May be removed in the future.
+ """
+ return hasattr(self, get_attrname(name))
+
def get(self, key, value=None):
"""For backwards-compatibility with dict-based objects.
@@ -360,6 +367,14 @@ class NovaObject(object):
"""
return 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():
+ self[key] = value
+
class ObjectListBase(object):
"""Mixin class for lists of objects.
@@ -457,3 +472,20 @@ class NovaObjectSerializer(nova.openstack.common.rpc.serializer.Serializer):
entity = self._process_iterable(context, self.deserialize_entity,
entity)
return entity
+
+
+def obj_to_primitive(obj):
+ """Recrusively turn an object into a python primitive.
+
+ A NovaObject becomes a dict, and anything that implements ObjectListBase
+ becomes a list.
+ """
+ if isinstance(obj, ObjectListBase):
+ return [obj_to_primitive(x) for x in obj]
+ elif isinstance(obj, NovaObject):
+ result = {}
+ for key, value in obj.iteritems():
+ result[key] = obj_to_primitive(value)
+ return result
+ else:
+ return obj