本文整理匯總了Python中oslo_versionedobjects.fields.ObjectField方法的典型用法代碼示例。如果您正苦於以下問題:Python fields.ObjectField方法的具體用法?Python fields.ObjectField怎麽用?Python fields.ObjectField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類oslo_versionedobjects.fields
的用法示例。
在下文中一共展示了fields.ObjectField方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_from_primitive
# 需要導入模塊: from oslo_versionedobjects import fields [as 別名]
# 或者: from oslo_versionedobjects.fields import ObjectField [as 別名]
def test_from_primitive(self):
@obj_base.VersionedObjectRegistry.register
class TestFakeObject(obj_base.VersionedObject):
OBJ_PROJECT_NAMESPACE = 'fake-project'
@obj_base.VersionedObjectRegistry.register
class TestBar(TestFakeObject, obj_base.ComparableVersionedObject):
fields = {
'name': fields.StringField(),
}
@obj_base.VersionedObjectRegistry.register
class TestFoo(TestFakeObject, obj_base.ComparableVersionedObject):
fields = {
'name': fields.StringField(),
'bar': fields.ObjectField('TestBar', nullable=True)
}
bar = TestBar(name='bar')
foo = TestFoo(name='foo', bar=bar)
from_primitive_values = [(foo.obj_to_primitive(), foo), (foo, foo)]
for prim_val, out_val in from_primitive_values:
self.assertEqual(out_val, self.field.from_primitive(
foo, 'attr', prim_val))
示例2: test_changed_with_sub_object
# 需要導入模塊: from oslo_versionedobjects import fields [as 別名]
# 或者: from oslo_versionedobjects.fields import ObjectField [as 別名]
def test_changed_with_sub_object(self):
@base.VersionedObjectRegistry.register
class ParentObject(base.VersionedObject):
fields = {'foo': fields.IntegerField(),
'bar': fields.ObjectField('MyObj'),
}
obj = ParentObject()
self.assertEqual(set(), obj.obj_what_changed())
obj.foo = 1
self.assertEqual(set(['foo']), obj.obj_what_changed())
bar = MyObj()
obj.bar = bar
self.assertEqual(set(['foo', 'bar']), obj.obj_what_changed())
obj.obj_reset_changes()
self.assertEqual(set(), obj.obj_what_changed())
bar.foo = 1
self.assertEqual(set(['bar']), obj.obj_what_changed())
示例3: test_parent_child
# 需要導入模塊: from oslo_versionedobjects import fields [as 別名]
# 或者: from oslo_versionedobjects.fields import ObjectField [as 別名]
def test_parent_child(self):
@base.VersionedObjectRegistry.register
class TestChild(base.VersionedObject):
VERSION = '2.34'
@base.VersionedObjectRegistry.register
class TestObject(base.VersionedObject):
VERSION = '1.23'
fields = {
'child': fields.ObjectField('TestChild'),
}
tree = base.obj_tree_get_versions('TestObject')
self.assertEqual({'TestObject': '1.23',
'TestChild': '2.34'},
tree)
示例4: test_complex
# 需要導入模塊: from oslo_versionedobjects import fields [as 別名]
# 或者: from oslo_versionedobjects.fields import ObjectField [as 別名]
def test_complex(self):
@base.VersionedObjectRegistry.register
class TestChild(base.VersionedObject):
VERSION = '2.34'
@base.VersionedObjectRegistry.register
class TestChildTwo(base.VersionedObject):
VERSION = '4.56'
fields = {
'sibling': fields.ObjectField('TestChild'),
}
@base.VersionedObjectRegistry.register
class TestObject(base.VersionedObject):
VERSION = '1.23'
fields = {
'child': fields.ObjectField('TestChild'),
'childtwo': fields.ListOfObjectsField('TestChildTwo'),
}
tree = base.obj_tree_get_versions('TestObject')
self.assertEqual({'TestObject': '1.23',
'TestChild': '2.34',
'TestChildTwo': '4.56'},
tree)
示例5: obj_make_compatible
# 需要導入模塊: from oslo_versionedobjects import fields [as 別名]
# 或者: from oslo_versionedobjects.fields import ObjectField [as 別名]
def obj_make_compatible(self, primitive, target_version):
"""Make an object representation compatible with a target version.
This is responsible for taking the primitive representation of
an object and making it suitable for the given target_version.
This may mean converting the format of object attributes, removing
attributes that have been added since the target version, etc. In
general:
- If a new version of an object adds a field, this routine
should remove it for older versions.
- If a new version changed or restricted the format of a field, this
should convert it back to something a client knowing only of the
older version will tolerate.
- If an object that this object depends on is bumped, then this
object should also take a version bump. Then, this routine should
backlevel the dependent object (by calling its obj_make_compatible())
if the requested version of this object is older than the version
where the new dependent object was added.
:param primitive: The result of :meth:`obj_to_primitive`
:param target_version: The version string requested by the recipient
of the object
:raises: :exc:`oslo_versionedobjects.exception.UnsupportedObjectError`
if conversion is not possible for some reason
"""
for key, field in self.fields.items():
if not isinstance(field, (obj_fields.ObjectField,
obj_fields.ListOfObjectsField)):
continue
if not self.obj_attr_is_set(key):
continue
self._obj_make_obj_compatible(primitive, target_version, key)
示例6: obj_tree_get_versions
# 需要導入模塊: from oslo_versionedobjects import fields [as 別名]
# 或者: from oslo_versionedobjects.fields import ObjectField [as 別名]
def obj_tree_get_versions(objname, tree=None):
"""Construct a mapping of dependent object versions.
This method builds a list of dependent object versions given a top-
level object with other objects as fields. It walks the tree recursively
to determine all the objects (by symbolic name) that could be contained
within the top-level object, and the maximum versions of each. The result
is a dict like::
{'MyObject': '1.23', ... }
:param objname: The top-level object at which to start
:param tree: Used internally, pass None here.
:returns: A dictionary of object names and versions
"""
if tree is None:
tree = {}
if objname in tree:
return tree
objclass = VersionedObjectRegistry.obj_classes()[objname][0]
tree[objname] = objclass.VERSION
for field_name in objclass.fields:
field = objclass.fields[field_name]
if isinstance(field, obj_fields.ObjectField):
child_cls = field._type._obj_name
elif isinstance(field, obj_fields.ListOfObjectsField):
child_cls = field._type._element_type._type._obj_name
else:
continue
try:
obj_tree_get_versions(child_cls, tree=tree)
except IndexError:
raise exception.UnregisteredSubobject(
child_objname=child_cls, parent_objname=objname)
return tree
示例7: test_get_dependencies
# 需要導入模塊: from oslo_versionedobjects import fields [as 別名]
# 或者: from oslo_versionedobjects.fields import ObjectField [as 別名]
def test_get_dependencies(self):
# Make sure _get_dependencies() generates a correct tree when parsing
# an object
self._add_class(self.obj_classes, MyExtraObject)
MyObject.fields['subob'] = fields.ObjectField('MyExtraObject')
MyExtraObject.VERSION = '1.0'
tree = {}
self.ovc._get_dependencies(tree, MyObject)
expected_tree = {'MyObject': {'MyExtraObject': '1.0'}}
self.assertEqual(expected_tree, tree, "_get_dependencies() did "
"not generate a correct dependency tree.")
示例8: _test_test_relationships_in_order_bad
# 需要導入模塊: from oslo_versionedobjects import fields [as 別名]
# 或者: from oslo_versionedobjects.fields import ObjectField [as 別名]
def _test_test_relationships_in_order_bad(self, fake_rels):
fake = mock.MagicMock()
fake.VERSION = '1.5'
fake.fields = {'foo': fields.ObjectField('bar')}
fake.obj_relationships = fake_rels
checker = fixture.ObjectVersionChecker()
self.assertRaises(AssertionError,
checker._test_relationships_in_order, fake)
示例9: _test_nested_backport
# 需要導入模塊: from oslo_versionedobjects import fields [as 別名]
# 或者: from oslo_versionedobjects.fields import ObjectField [as 別名]
def _test_nested_backport(self, old):
@base.VersionedObjectRegistry.register
class Parent(base.VersionedObject):
VERSION = '1.0'
fields = {
'child': fields.ObjectField('MyObj'),
}
@base.VersionedObjectRegistry.register # noqa
class Parent(base.VersionedObject):
VERSION = '1.1'
fields = {
'child': fields.ObjectField('MyObj'),
}
child = MyObj(foo=1)
parent = Parent(child=child)
prim = parent.obj_to_primitive()
child_prim = prim['versioned_object.data']['child']
child_prim['versioned_object.version'] = '1.10'
ser = base.VersionedObjectSerializer()
with mock.patch.object(base.VersionedObject, 'indirection_api') as a:
if old:
a.object_backport_versions.side_effect = NotImplementedError
ser.deserialize_entity(self.context, prim)
a.object_backport_versions.assert_called_once_with(
self.context, prim, {'Parent': '1.1',
'MyObj': '1.6',
'MyOwnedObject': '1.0'})
if old:
# NOTE(danms): This should be the version of the parent object,
# not the child. If wrong, this will be '1.6', which is the max
# child version in our registry.
a.object_backport.assert_called_once_with(
self.context, prim, '1.1')
示例10: test_missing_referenced
# 需要導入模塊: from oslo_versionedobjects import fields [as 別名]
# 或者: from oslo_versionedobjects.fields import ObjectField [as 別名]
def test_missing_referenced(self):
"""Ensure a missing child object is highlighted."""
@base.VersionedObjectRegistry.register
class TestObjectFoo(base.VersionedObject):
VERSION = '1.23'
fields = {
# note that this object does not exist
'child': fields.ObjectField('TestChildBar'),
}
exc = self.assertRaises(exception.UnregisteredSubobject,
base.obj_tree_get_versions,
'TestObjectFoo')
self.assertIn('TestChildBar is referenced by TestObjectFoo',
exc.format_message())
示例11: obj_reset_changes
# 需要導入模塊: from oslo_versionedobjects import fields [as 別名]
# 或者: from oslo_versionedobjects.fields import ObjectField [as 別名]
def obj_reset_changes(self, fields=None, recursive=False):
"""Reset the list of fields that have been changed.
:param fields: List of fields to reset, or "all" if None.
:param recursive: Call obj_reset_changes(recursive=True) on
any sub-objects within the list of fields
being reset.
This is NOT "revert to previous values".
Specifying fields on recursive resets will only be honored at the top
level. Everything below the top will reset all.
"""
if recursive:
for field in self.obj_get_changes():
# Ignore fields not in requested set (if applicable)
if fields and field not in fields:
continue
# Skip any fields that are unset
if not self.obj_attr_is_set(field):
continue
value = getattr(self, field)
# Don't reset nulled fields
if value is None:
continue
# Reset straight Object and ListOfObjects fields
if isinstance(self.fields[field], ovoo_fields.ObjectField):
value.obj_reset_changes(recursive=True)
elif isinstance(self.fields[field],
ovoo_fields.ListOfObjectsField):
for thing in value:
thing.obj_reset_changes(recursive=True)
if fields:
self._changed_fields -= set(fields)
for field in fields:
self._obj_original_values.pop(field, None)
else:
self._changed_fields.clear()
self._obj_original_values = dict()
示例12: obj_reset_changes
# 需要導入模塊: from oslo_versionedobjects import fields [as 別名]
# 或者: from oslo_versionedobjects.fields import ObjectField [as 別名]
def obj_reset_changes(self, fields=None, recursive=False):
"""Reset the list of fields that have been changed.
:param fields: List of fields to reset, or "all" if None.
:param recursive: Call obj_reset_changes(recursive=True) on
any sub-objects within the list of fields
being reset.
This is NOT "revert to previous values".
Specifying fields on recursive resets will only be honored at the top
level. Everything below the top will reset all.
"""
if recursive:
for field in self.obj_get_changes():
# Ignore fields not in requested set (if applicable)
if fields and field not in fields:
continue
# Skip any fields that are unset
if not self.obj_attr_is_set(field):
continue
value = getattr(self, field)
# Don't reset nulled fields
if value is None:
continue
# Reset straight Object and ListOfObjects fields
if isinstance(self.fields[field], obj_fields.ObjectField):
value.obj_reset_changes(recursive=True)
elif isinstance(self.fields[field],
obj_fields.ListOfObjectsField):
for thing in value:
thing.obj_reset_changes(recursive=True)
if fields:
self._changed_fields -= set(fields)
else:
self._changed_fields.clear()
示例13: obj_reset_changes
# 需要導入模塊: from oslo_versionedobjects import fields [as 別名]
# 或者: from oslo_versionedobjects.fields import ObjectField [as 別名]
def obj_reset_changes(self, fields=None, recursive=False):
"""Reset the list of fields that have been changed.
.. note::
- This is NOT "revert to previous values"
- Specifying fields on recursive resets will only be honored at the
top level. Everything below the top will reset all.
:param fields: List of fields to reset, or "all" if None.
:param recursive: Call obj_reset_changes(recursive=True) on
any sub-objects within the list of fields
being reset.
"""
if recursive:
for field in self.obj_get_changes():
# Ignore fields not in requested set (if applicable)
if fields and field not in fields:
continue
# Skip any fields that are unset
if not self.obj_attr_is_set(field):
continue
value = getattr(self, field)
# Don't reset nulled fields
if value is None:
continue
# Reset straight Object and ListOfObjects fields
if isinstance(self.fields[field], obj_fields.ObjectField):
value.obj_reset_changes(recursive=True)
elif isinstance(self.fields[field],
obj_fields.ListOfObjectsField):
for thing in value:
thing.obj_reset_changes(recursive=True)
if fields:
self._changed_fields -= set(fields)
else:
self._changed_fields.clear()