From 68cb4d53385821c3ffdc40c299a77d11a7f98f27 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Mon, 24 Jun 2013 12:54:05 -0700 Subject: Add obj_to_primitive() to recursively primitiveize objects Occasionally we need to recursively process a NovaObject into a representative dict or list. This patch adds a function to do that recursively, accounting for nested objects and lists of objects. Related to blueprint unified-object-model Change-Id: I6adb84f9e5a0564ec4af14200e26e838b4a5bb6e --- nova/objects/base.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'nova/objects') diff --git a/nova/objects/base.py b/nova/objects/base.py index 147d55c23..4f973de0c 100644 --- a/nova/objects/base.py +++ b/nova/objects/base.py @@ -464,3 +464,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 -- cgit