diff options
| author | Dan Smith <danms@us.ibm.com> | 2013-06-05 11:52:53 -0700 |
|---|---|---|
| committer | Dan Smith <danms@us.ibm.com> | 2013-06-07 11:43:37 -0700 |
| commit | 65f6c536fecd3c788b2e0dfa9d66ecd24ca550e1 (patch) | |
| tree | ba7d2c9f6ed06d998d94c979fbf3b6214eec5331 /nova/objects | |
| parent | 91ac85ce82bc6d050b1152dd7af8851b43b01ea5 (diff) | |
| download | nova-65f6c536fecd3c788b2e0dfa9d66ecd24ca550e1.tar.gz nova-65f6c536fecd3c788b2e0dfa9d66ecd24ca550e1.tar.xz nova-65f6c536fecd3c788b2e0dfa9d66ecd24ca550e1.zip | |
Add base mixin class for object lists
This provides a base class that provides the bulk of the common
functionality that objects implementing a list of objects will
need.
Related to blueprint unified-object-model
Change-Id: I5bc4e64cc8fe2d780251cbc43f2ee45f4e0f820a
Diffstat (limited to 'nova/objects')
| -rw-r--r-- | nova/objects/base.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/nova/objects/base.py b/nova/objects/base.py index b263b14e4..625dd48d1 100644 --- a/nova/objects/base.py +++ b/nova/objects/base.py @@ -357,6 +357,58 @@ class NovaObject(object): return self[key] +class ObjectListBase(object): + """Mixin class for lists of objects. + + This mixin class can be added as a base class for an object that + is implementing a list of objects. It adds a single field of 'objects', + which is the list store, and behaves like a list itself. It supports + serialization of the list of objects automatically. + """ + fields = { + 'objects': list, + } + + def __iter__(self): + """List iterator interface.""" + return iter(self.objects) + + def __len__(self): + """List length.""" + return len(self.objects) + + def __getitem__(self, index): + """List index access.""" + if isinstance(index, slice): + new_obj = self.__class__() + new_obj.objects = self.objects[index] + # NOTE(danms): We must be mixed in with a NovaObject! + new_obj.obj_reset_changes() + new_obj._context = self._context + return new_obj + return self.objects[index] + + def __contains__(self, value): + """List membership test.""" + return value in self.objects + + def count(self, value): + """List count of value occurrences.""" + return self.objects.count(value) + + def index(self, value): + """List index of value.""" + return self.objects.index(value) + + def _attr_objects_to_primitive(self): + """Serialization of object list.""" + return [x.obj_to_primitive() for x in self.objects] + + def _attr_objects_from_primitive(self, value): + """Deserialization of object list.""" + return [NovaObject.obj_from_primitive(x) for x in value] + + class NovaObjectSerializer(nova.openstack.common.rpc.serializer.Serializer): """A NovaObject-aware Serializer. |
