fields

fields

class oslo_versionedobjects.fields.BaseEnumField(**kwargs)

Base class for all enum field types

This class should not be directly instantiated. Instead subclass it and set AUTO_TYPE to be a SomeEnum() where SomeEnum is a subclass of Enum.

valid_values

Return the list of valid values for the field.

class oslo_versionedobjects.fields.CoercedDict(*args, **kwargs)

Dict which coerces its values

Dict implementation which overrides all element-adding methods and coercing the element(s) being added to the required element type

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) → None. Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

class oslo_versionedobjects.fields.CoercedList(*args, **kwargs)

List which coerces its elements

List implementation which overrides all element-adding methods and coercing the element(s) being added to the required element type

append(object) → None -- append object to end
extend(iterable) → None -- extend list by appending elements from the iterable
insert(i, x)

L.insert(index, object) – insert object before index

class oslo_versionedobjects.fields.CoercedSet(*args, **kwargs)

Set which coerces its values

Dict implementation which overrides all element-adding methods and coercing the element(s) being added to the required element type

add(value)

Add an element to a set.

This has no effect if the element is already present.

symmetric_difference_update(values)

Update a set with the symmetric difference of itself and another.

update(values)

Update a set with the union of itself and others.

class oslo_versionedobjects.fields.DictProxyField(dict_field_name, key_type=<class 'int'>)

Descriptor allowing us to assign pinning data as a dict of key_types

This allows us to have an object field that will be a dict of key_type keys, allowing that will convert back to string-keyed dict.

This will take care of the conversion while the dict field will make sure that we store the raw json-serializable data on the object.

key_type should return a type that unambiguously responds to six.text_type so that calling key_type on it yields the same thing.

exception oslo_versionedobjects.fields.ElementTypeError(expected, key, value)
class oslo_versionedobjects.fields.EnumField(valid_values, **kwargs)

Anonymous enum field type

This class allows for anonymous enum types to be declared, simply by passing in a list of valid values to its constructor. It is generally preferable though, to create an explicit named enum type by sub-classing the BaseEnumField type directly.

exception oslo_versionedobjects.fields.KeyTypeError(expected, value)
class oslo_versionedobjects.fields.SensitiveString

A string field type that may contain sensitive (password) information.

Passwords in the string value are masked when stringified.

stringify(value)

Returns a short stringified version of a value.

class oslo_versionedobjects.fields.SensitiveStringField(**kwargs)

Field type that masks passwords when the field is stringified.

class oslo_versionedobjects.fields.StateMachine(valid_values, **kwargs)

A mixin that can be applied to an EnumField to enforce a state machine

e.g: Setting the code below on a field will ensure an object cannot transition from ERROR to ACTIVE

Example
class FakeStateMachineField(fields.EnumField, fields.StateMachine):

    ACTIVE = 'ACTIVE'
    PENDING = 'PENDING'
    ERROR = 'ERROR'
    DELETED = 'DELETED'

    ALLOWED_TRANSITIONS = {
        ACTIVE: {
            PENDING,
            ERROR,
            DELETED,
        },
        PENDING: {
            ACTIVE,
            ERROR
        },
        ERROR: {
            PENDING,
        },
        DELETED: {}  # This is a terminal state
    }

    _TYPES = (ACTIVE, PENDING, ERROR, DELETED)

    def __init__(self, **kwargs):
        super(FakeStateMachineField, self).__init__(
        self._TYPES, **kwargs)
coerce(obj, attr, value)

Coerce a value to a suitable type.

This is called any time you set a value on an object, like:

foo.myint = 1

and is responsible for making sure that the value (1 here) is of the proper type, or can be sanely converted.

This also handles the potentially nullable or defaultable nature of the field and calls the coerce() method on a FieldType to actually do the coercion.

:param:obj: The object being acted upon :param:attr: The name of the attribute/field being set :param:value: The value being set :returns: The properly-typed value

class oslo_versionedobjects.fields.UUIDField(**kwargs)

UUID Field Type

Warning

This class does not actually validate UUIDs. This will happen in a future major version of oslo.versionedobjects

To validate that you have valid UUIDs you need to do the following in your own objects/fields.py

Example
import oslo_versionedobjects.fields as ovo_fields

class UUID(ovo_fields.UUID):
     def coerce(self, obj, attr, value):
        uuid.UUID(value)
        return str(value)


class UUIDField(ovo_fields.AutoTypedField):
    AUTO_TYPE = UUID()

and then in your objects use <your_projects>.object.fields.UUIDField.

This will become default behaviour in the future.

Creative Commons Attribution 3.0 License

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.