cubeserver_common.models.utils package

Submodules

cubeserver_common.models.utils.complexdictcodec module

For encoding/decoding “complex dictionaries” into bson

  • “complex dictionary” meaning a dictionary with non-bson-compatible types

class cubeserver_common.models.utils.complexdictcodec.ComplexDictCodec(key_codec: ~bson.codec_options.TypeCodec = <class 'cubeserver_common.models.utils.dummycodec.DummyCodec'>, value_codec: ~bson.codec_options.TypeCodec = <class 'cubeserver_common.models.utils.dummycodec.DummyCodec'>)

Bases: TypeCodec

Codec for encoding a complex dictionary into bson

property bson_type

The BSON type to be converted into our own type.

property python_type

The Python type to be converted into something serializable.

transform_bson(value) Enum

Convert the given BSON value into our own type.

transform_python(value: dict)

Convert the given Python object into something serializable.

cubeserver_common.models.utils.dummycodec module

A dummy, fallback codec that effectively does nothing.

class cubeserver_common.models.utils.dummycodec.DummyCodec(type_class: ~typing.Type[~typing.Any] = <class 'str'>)

Bases: TypeCodec

Dummy Codec. Does nothing as far as encoding/decoding. Returns what was given. No questions asked.

property bson_type

The BSON type to be converted into our own type.

property python_type

The Python type to be converted into something serializable.

transform_bson(value)

Convert the given BSON value into our own type.

transform_python(value)

Convert the given Python object into something serializable.

cubeserver_common.models.utils.enumcodec module

For encoding/decoding Enums into bson

Limitations: Only works with Enums that have bson-compatible typed values Enum values must all be of the same type

class cubeserver_common.models.utils.enumcodec.EnumCodec(enum_class: Type[Enum], value_class: type = Ellipsis)

Bases: TypeCodec

Codec for encoding a generic Enum into bson This assumes the Enum only contains primitive/built-in/bson-compatible types

property bson_type

The BSON type to be converted into our own type.

property python_type

The Python type to be converted into something serializable.

transform_bson(value) Enum

Convert the given BSON value into our own type.

transform_python(value: Enum)

Convert the given Python object into something serializable.

cubeserver_common.models.utils.listcodec module

For encoding/decoding lists

Limitations: Assumes the type of the list’s elements is uniform

class cubeserver_common.models.utils.listcodec.ListCodec(type_codec: Optional[TypeCodec] = None)

Bases: TypeCodec

Codec for encoding a list into bson

bson_type

alias of list

python_type

alias of list

transform_bson(value) list

Convert the given BSON value into our own type.

transform_python(value: list)

Convert the given Python object into something serializable.

cubeserver_common.models.utils.modelutils module

Some utility classes to help with object mapping

class cubeserver_common.models.utils.modelutils.Encodable

Bases: _Encoder

An abstract class for classes that contain codec data

abstract classmethod decode(value: dict) _Encoder

Decodes a dictionary into an Encodable object

abstract encode() dict

Encodes an Encodable object into a plain old, bson-able dictionary

class cubeserver_common.models.utils.modelutils.EncodableCodec(encodable: Type[_Encoder])

Bases: TypeCodec

A TypeCodec for PyMongoModel objects

bson_type

alias of dict

property python_type: Type[_Encoder]

The Python type to be converted into something serializable.

transform_bson(value: dict) _Encoder

Decodes a dictionary into a PyMongoModel object

transform_python(value: _Encoder) dict

Encodes a PyMongoModel object into a plain old, bson-able dictionary

class cubeserver_common.models.utils.modelutils.PyMongoModel

Bases: Encodable

A class for easy object-mapping to bson. Extend this class for any classes that describe a type of document.

property collection

Define the Mongodb collection in your class. Use the PyMongoModel.model_type_registry as the type registry.

classmethod decode(value: Optional[Mapping[str, Any]]) Optional[Encodable]

Populates an object from a dictionary of the document This returns None only if the bson value given is None

encode() dict

Encodes this into a dictionary for BSON to be happy

classmethod find(*args, **kwargs)

Finds documents from the collection Arguments are the same as those for PyMongo.collection’s find().

classmethod find_by_id(identifier)

Finds a document from the collection, given the id

classmethod find_codec(field_name: str, field_type_name: str) TypeCodec

Finds a codec for a given field (w/ name and type name specified)

classmethod find_one(*args, **kwargs)

Finds a document from the collection Arguments are the same as those for PyMongo’s find_one().

classmethod find_safe(*args, **kwargs)
find_self()

Returns the database’s version of self

classmethod find_sorted(*args, key: str = Ellipsis, order=1, **kwargs)

Same a find(), but with sorting!

property id

The internal document identifier

property id_secondary

A dummy property; always equal to id Used to have multiple id-driven columns in a Flask-tables table

ignore_attribute(attr_name: str)

Forces an attribute to be ignored as a document field

locate_codec(data_type: type) Optional[TypeCodec]

Tries to find a TypeCodec for the specified type if possible.

property model_type_registry

A TypeRegistry to be used when getting the collection from the database

mongo: Optional[MongoClient] = None
register_codec(type_codec: TypeCodec, replace=False)

Register a TypeCodec for use in the PyMongoModelCodec Specify whether to replace an existing one if applicable, with the default being False.

register_field(attr_name: str, value: Optional[Any] = None, custom_codec: Optional[TypeCodec] = None)

Register each attribute of the model for the database. Optionally specify with a custom codec prior to setting the attribute. If a codec is specified optionally here, it will not be automatically registered for use in encoding/decoding attributes of the same type unless register_codec() is used also. The value does not need to be specified ONLY IF a custom_codec is provided. This method returns immediately if a field with the given name is already registered. The force parameter allows you to force the registration of a field despite checks failing or the type appearing to be bson-compatible

remove()

Removes this document from the collection

save()

Saves this document to the collection

set_attr_from_string(field_name: str, value: str)

Decodes and updates a single string value to the document object

classmethod set_collection_name(collection_name: str)

Define the Mongodb collection in your class. Use the PyMongoModel.model_type_registry as the type registry.

classmethod update_mongo_client(mongo_client: Optional[MongoClient])

Sets the MongoClient reference in PyMongoModel, which is then used by any models that extend this class.

Module contents

Classes to allow for easy object-mapping to bson for MongoDb