sanskrit_data.schema.common

A module containing some data container base classes.

class sanskrit_data.schema.common.JsonObject[source]

Bases: object

The base class of all Json-serializable data container classes, with many utility methods.

delete_in_collection(db_interface)[source]
dump_to_file(filename)[source]
equals_ignore_id(other)[source]
classmethod from_id(id, db_interface)[source]

Returns None if nothing is found.

classmethod get_json_map_list(some_list)[source]
classmethod get_jsonpickle_typeid()[source]
classmethod get_wire_typeid()[source]
classmethod make_from_dict(input_dict)[source]

Defines our canonical way of constructing a JSON object from a dict.

All other deserialization methods should use this.

Note that this assumes that json_class_index is populated properly!

  • from sanskrit_data.schema import * before using this should take care of it.
Parameters:input_dict
Returns:A subclass of JsonObject
classmethod make_from_dict_list(input_dict_list)[source]
classmethod make_from_pickledstring(pickle)[source]
classmethod read_from_file(filename)[source]
schema = {'required': ['jsonClass'], 'type': 'object', 'properties': {'jsonClass': {'type': 'string'}}}
set_from_dict(input_dict)[source]
set_from_id(db_interface, id)[source]
set_jsonpickle_type_recursively()[source]
set_type()[source]
set_type_recursively()[source]
to_json_map()[source]

One convenient way of ‘serializing’ the object.

So, the type must be properly set. Many functions accept such json maps, just as they accept strings.

update_collection(db_interface)[source]

Do JSON validation and write to database.

validate(db_interface=None)[source]

Validate the JSON serialization of this object using the schema member. Called before database writes.

Parameters:db_interface – Potentially useful in subclasses to perform validations (eg. is the target_id valid). This value may not be available: for example when called from the from_details methods.
Returns:a boolean.
validate_schema()[source]
class sanskrit_data.schema.common.JsonObjectNode[source]

Bases: sanskrit_data.schema.common.JsonObject

Represents a tree (not a general Directed Acyclic Graph) of JsonObjectWithTargets.

A video describing its use.

delete_in_collection(db_interface)[source]
fill_descendents(db_interface, depth=10, entity_type=None)[source]
classmethod from_details(content, children=None)[source]
schema = {'required': ['jsonClass'], 'type': 'object', 'properties': {'content': {'required': ['jsonClass'], 'type': 'object', 'properties': {'jsonClass': {'type': 'string'}}}, 'children': {'items': {'type': 'object', '$ref': 'JsonObjectNode'}, 'type': 'array'}, 'jsonClass': {'enum': ['JsonObjectNode'], 'type': 'string'}}, 'id': 'JsonObjectNode'}

Recursively valdiate target-types.

update_collection(db_interface)[source]
validate(db_interface=None)[source]
validate_children_types()[source]
class sanskrit_data.schema.common.JsonObjectWithTarget[source]

Bases: sanskrit_data.schema.common.JsonObject

A JsonObject with a target field.

classmethod get_allowed_target_classes()[source]
get_targetting_entities(db_interface, entity_type=None)[source]
schema = {'required': ['jsonClass'], 'type': 'object', 'description': 'A JsonObject with a target field.', 'properties': {'targets': {'items': {'required': ['container_id', 'jsonClass'], 'type': 'object', 'properties': {'container_id': {'type': 'string'}, 'jsonClass': {'enum': ['Target'], 'type': 'string'}}}, 'type': 'array', 'description': 'This field lets us define a directed graph involving JsonObjects stored in a database.'}, 'jsonClass': {'type': 'string'}}}
target_class

alias of Target

validate(db_interface=None)[source]
validate_targets(targets, allowed_types, db_interface)[source]
class sanskrit_data.schema.common.ScriptRendering[source]

Bases: sanskrit_data.schema.common.JsonObject

classmethod from_details(text, encoding_scheme=None)[source]
schema = {'required': ['text', 'jsonClass'], 'type': 'object', 'properties': {'encoding_scheme': {'type': 'string'}, 'text': {'type': 'string'}, 'jsonClass': {'enum': ['ScriptRendering'], 'type': 'string'}}}
class sanskrit_data.schema.common.Target[source]

Bases: sanskrit_data.schema.common.JsonObject

classmethod from_containers(containers)[source]
classmethod from_details(container_id)[source]
classmethod from_ids(container_ids)[source]
get_target_entity(db_interface)[source]

Returns null if db_interface doesnt have any such entity.

schema = {'required': ['container_id', 'jsonClass'], 'type': 'object', 'properties': {'container_id': {'type': 'string'}, 'jsonClass': {'enum': ['Target'], 'type': 'string'}}}
exception sanskrit_data.schema.common.TargetValidationError(allowed_types, target_obj, targetting_obj)[source]

Bases: exceptions.Exception

class sanskrit_data.schema.common.Text[source]

Bases: sanskrit_data.schema.common.JsonObject

classmethod from_details(script_renderings, language_code=None)[source]
schema = {'required': ['jsonClass'], 'type': 'object', 'properties': {'jsonClass': {'enum': ['Text'], 'type': 'string'}, 'script_renderings': {'items': {'type': {'required': ['text', 'jsonClass'], 'type': 'object', 'properties': {'encoding_scheme': {'type': 'string'}, 'text': {'type': 'string'}, 'jsonClass': {'enum': ['ScriptRendering'], 'type': 'string'}}}}, 'type': 'array'}, 'language_code': {'type': 'string'}}}
sanskrit_data.schema.common.check_class(obj, allowed_types)[source]
sanskrit_data.schema.common.check_list_item_types(some_list, allowed_types)[source]
sanskrit_data.schema.common.get_schemas(module_in)[source]
sanskrit_data.schema.common.json_class_index = {'JsonObjectWithTarget': <class 'sanskrit_data.schema.common.JsonObjectWithTarget'>, 'Target': <class 'sanskrit_data.schema.common.Target'>, 'JsonObject': <class 'sanskrit_data.schema.common.JsonObject'>, 'BookMetadata': <class 'sanskrit_data.schema.books.BookMetadata'>, 'TargetValidationError': <class 'sanskrit_data.schema.common.TargetValidationError'>, 'BookPortion': <class 'sanskrit_data.schema.books.BookPortion'>, 'ScriptRendering': <class 'sanskrit_data.schema.common.ScriptRendering'>, 'JsonObjectNode': <class 'sanskrit_data.schema.common.JsonObjectNode'>, 'Text': <class 'sanskrit_data.schema.common.Text'>, 'PublicationDetails': <class 'sanskrit_data.schema.books.PublicationDetails'>, 'BookPositionTarget': <class 'sanskrit_data.schema.books.BookPositionTarget'>}

Maps jsonClass values to the containing Python module object. Useful for (de)serialization. Updated using update_json_class_index() calls at the end of each module file (such as this one) whose classes may be serialized.

sanskrit_data.schema.common.recursively_merge(a, b)[source]
sanskrit_data.schema.common.update_json_class_index(module_in)[source]

Call this function to enable (de)serialization.

Usage example: common.update_json_class_index(sys.modules[__name__]).