sanskrit_data.schema.common

A module containing some data container base classes.

class sanskrit_data.schema.common.DataSource[source]

Bases: sanskrit_data.schema.common.JsonObject

classmethod from_details(source_type, id)[source]
infer_by_admin(db_interface=None, user=None)[source]
is_id_impersonated_by_non_admin(db_interface=None, user=None)[source]

A None user is assumed to be a valid authorized backend script.

schema = {'description': 'Source of the json-data which contains this node. Eg. Uploader details in case of books, annotator in case of annotations. Consider naming the field that contains this object `source` to make querying uniform.', 'jsonClass': {'enum': ['DataSource']}, 'properties': {'source_type': {'default': 'system_inferred', 'enum': ['system_inferred', 'user_supplied'], 'type': 'string', 'description': 'Does this data come from a machine, or a human? source_ prefix avoids keyword conflicts in some languages.'}, 'jsonClass': {'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}, 'id': {'type': 'string', 'description': 'Something to identify the particular data source.'}, 'by_admin': {'type': 'boolean', 'description': 'Was the creator of this data an admin at the time it was created or updated?'}}, 'required': ['source_type', 'jsonClass'], 'type': 'object'}
setup_source(db_interface=None, user=None)[source]
validate(db_interface=None, user=None)[source]

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

Parameters:
  • user
  • 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.

class sanskrit_data.schema.common.JsonObject[source]

Bases: object

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

classmethod add_indexes(db_interface)[source]
delete_in_collection(db_interface, user=None)[source]

To delete referrent items also, use appropriate method in JsonObjectNode. :param db_interface: :param user: :return:

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

Returns None if nothing is found.

get_external_storage_path(db_interface)[source]

Get the directory path where files associated with this object are to be stored.

classmethod get_json_map_list(some_list)[source]
classmethod get_jsonpickle_typeid()[source]
classmethod get_wire_typeid()[source]
list_files(db_interface, suffix_pattern='*')[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 = {'properties': {'jsonClass': {'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}}, 'required': ['jsonClass'], 'type': 'object'}
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, user=None)[source]

Do JSON validation and write to database.

validate(db_interface=None, user=None)[source]

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

Parameters:
  • user
  • 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_deletion(db_interface, user=None)[source]
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 UllekhanamJsonObject.

A video describing its use.

affected_user_ids()[source]
delete_in_collection(db_interface, user=None)[source]

To delete referrent items also, use appropriate method in JsonObjectNode. :param db_interface: :param user: :return:

fill_descendents(db_interface, depth=10, entity_type=None)[source]
classmethod from_details(content, children=None)[source]
recursively_delete_attr(field_name)[source]

Rarely useful method: example when the schema of a Class changes to omit a field.

Limitation: Only useful with direct members.

schema = {'id': 'JsonObjectNode', 'properties': {'content': {'required': ['jsonClass'], 'type': 'object', 'properties': {'jsonClass': {'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}}}, 'children': {'items': {'type': 'object', '$ref': 'JsonObjectNode'}, 'type': 'array'}, 'jsonClass': {'enum': ['JsonObjectNode'], 'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}}, 'required': ['jsonClass'], 'type': 'object'}
setup_source(source)[source]
update_collection(db_interface, user=None)[source]

Special info: Mutates this object.

validate(db_interface=None, user=None)[source]

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

Parameters:
  • user
  • 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_children_types()[source]

Recursively valdiate target-types.

validate_deletion(db_interface, user=None)[source]
class sanskrit_data.schema.common.NamedEntity[source]

Bases: sanskrit_data.schema.common.JsonObject

The same name written in different languages have different spellings - oft due to differing case endings and conventions: kAlidAsaH vs Kalidasa. Hence this container.

classmethod from_details(names)[source]
classmethod from_name_string(name, language_code=None, encoding_scheme=None)[source]
schema = {'properties': {'names': {'minItems': 1, 'items': {'required': ['jsonClass'], 'type': 'object', 'properties': {'search_strings': {'items': {'type': 'string'}, 'type': 'array', 'description': 'Search strings which should match this text. It could be derived from script_renderings - by a simple copy (intended for use with a text index) or some intelligent tokenization thereof.'}, 'language_code': {'type': 'string'}, 'script_renderings': {'minItems': 1, 'items': {'required': ['text', 'jsonClass'], 'type': 'object', 'properties': {'encoding_scheme': {'type': 'string'}, 'text': {'type': 'string'}, 'jsonClass': {'enum': ['ScriptRendering'], 'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}}}, 'type': 'array'}, 'jsonClass': {'enum': ['Text'], 'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}}}, 'type': 'array'}, 'jsonClass': {'enum': ['NamedEntity'], 'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}}, 'required': ['jsonClass'], 'type': 'object'}
class sanskrit_data.schema.common.ScriptRendering[source]

Bases: sanskrit_data.schema.common.JsonObject

classmethod from_details(text, encoding_scheme=None)[source]
schema = {'properties': {'encoding_scheme': {'type': 'string'}, 'text': {'type': 'string'}, 'jsonClass': {'enum': ['ScriptRendering'], 'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}}, 'required': ['text', 'jsonClass'], 'type': 'object'}
class sanskrit_data.schema.common.Target[source]

Bases: sanskrit_data.schema.common.JsonObject

check_target_class(db_interface, allowed_types, targeting_obj)[source]
classmethod check_target_classes(targets_to_check, db_interface, allowed_types, targeting_obj)[source]
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 = {'properties': {'container_id': {'type': 'string'}, 'jsonClass': {'enum': ['Target'], 'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}}, 'required': ['container_id', 'jsonClass'], 'type': 'object'}
exception sanskrit_data.schema.common.TargetValidationError(allowed_types, target_obj, targeting_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]
classmethod from_text_string(text_string, language_code=None, encoding_scheme=None)[source]
schema = {'properties': {'search_strings': {'items': {'type': 'string'}, 'type': 'array', 'description': 'Search strings which should match this text. It could be derived from script_renderings - by a simple copy (intended for use with a text index) or some intelligent tokenization thereof.'}, 'jsonClass': {'enum': ['Text'], 'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}, 'script_renderings': {'minItems': 1, 'items': {'required': ['text', 'jsonClass'], 'type': 'object', 'properties': {'encoding_scheme': {'type': 'string'}, 'text': {'type': 'string'}, 'jsonClass': {'enum': ['ScriptRendering'], 'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}}}, 'type': 'array'}, 'language_code': {'type': 'string'}}, 'required': ['jsonClass'], 'type': 'object'}
class sanskrit_data.schema.common.UllekhanamJsonObject[source]

Bases: sanskrit_data.schema.common.JsonObject

The archetype JsonObject for use with the Ullekhanam project. See description.schema field

classmethod add_indexes(db_interface)[source]
detect_illegal_takeover(db_interface=None, user=None)[source]
classmethod get_allowed_target_classes()[source]
get_targetting_entities(db_interface, entity_type=None)[source]
is_editable_by_others()[source]
schema = {'description': 'Some JsonObject which can be saved as a document in the ullekhanam database.', 'properties': {'source': {'properties': {'source_type': {'default': 'system_inferred', 'enum': ['system_inferred', 'user_supplied'], 'type': 'string', 'description': 'Does this data come from a machine, or a human? source_ prefix avoids keyword conflicts in some languages.'}, 'by_admin': {'type': 'boolean', 'description': 'Was the creator of this data an admin at the time it was created or updated?'}, 'id': {'type': 'string', 'description': 'Something to identify the particular data source.'}, 'jsonClass': {'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}}, 'required': ['source_type', 'jsonClass'], 'type': 'object', 'description': 'Source of the json-data which contains this node. Eg. Uploader details in case of books, annotator in case of annotations. Consider naming the field that contains this object `source` to make querying uniform.', 'jsonClass': {'enum': ['DataSource']}}, 'editable_by_others': {'default': True, 'type': 'boolean', 'description': 'Can this annotation be taken over by others for wiki-style editing or deleting?'}, 'targets': {'items': {'required': ['container_id', 'jsonClass'], 'type': 'object', 'properties': {'container_id': {'type': 'string'}, 'jsonClass': {'enum': ['Target'], 'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}}}, 'type': 'array', 'description': 'This field lets us define a directed graph involving JsonObjects stored in a database.'}, 'jsonClass': {'type': 'string', 'description': 'A hint used by json libraries to deserialize json data to an object of the appropriate type. This is necessary for sub-objects to have as well (to ensure that the deserialization functions as expected).'}}, 'required': ['jsonClass'], 'type': 'object'}
target_class

alias of Target

update_collection(db_interface, user=None)[source]

Do JSON validation and write to database.

validate(db_interface=None, user=None)[source]

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

Parameters:
  • user
  • 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_deletion(db_interface, user=None)[source]
validate_deletion_ignoring_targetters(db_interface, user=None)[source]
validate_targets(db_interface)[source]
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 = {'BookPortion': <class 'sanskrit_data.schema.books.BookPortion'>, 'BookPositionTarget': <class 'sanskrit_data.schema.books.BookPositionTarget'>, 'CreationDetails': <class 'sanskrit_data.schema.books.CreationDetails'>, 'DataSource': <class 'sanskrit_data.schema.common.DataSource'>, 'JsonObject': <class 'sanskrit_data.schema.common.JsonObject'>, 'JsonObjectNode': <class 'sanskrit_data.schema.common.JsonObjectNode'>, 'NamedEntity': <class 'sanskrit_data.schema.common.NamedEntity'>, 'PublicationDetails': <class 'sanskrit_data.schema.books.PublicationDetails'>, 'SchemaError': <class 'jsonschema.exceptions.SchemaError'>, 'ScriptRendering': <class 'sanskrit_data.schema.common.ScriptRendering'>, 'Target': <class 'sanskrit_data.schema.common.Target'>, 'TargetValidationError': <class 'sanskrit_data.schema.common.TargetValidationError'>, 'Text': <class 'sanskrit_data.schema.common.Text'>, 'UllekhanamJsonObject': <class 'sanskrit_data.schema.common.UllekhanamJsonObject'>, 'ValidationError': <class 'jsonschema.exceptions.ValidationError'>}

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_json_schemas(a, b, json_path='')[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__]).