Base Model

class crotal.models.base.Model(**attributes)[source]

This is the model of the data in crotal site. It is a simple ORM in which the database layer is replaced with static file reader. The pattern is designed to go with Django.

Example usage:

We have defined the max length of the title as 8, so the result is:

create()[source]

This function is called after the model has successfully load the documents from the folder indicated, it should be overridden if you want to do some extra operation on the data.

For example, if you want to convert the post content into markdown format, then you should define it in the method create under Post class.

to_db(absolute_path)[source]

Convert the data from object into crotal database format for data persistence and serialization.

Parameters
absolute_path -- Absolute file path of the source file.
Returns
A dictionary to be stored in crotal database.
classmethod load(database, config)[source]

This method loads the static files indicated by cls.PATH of each model, then parse them into objects.

Parameters
  • database -- A crotal database instance.
  • config -- A configuration instance.
Returns

None

classmethod classify_files()[source]

This method classifies the static files into 3 types: unmodified, modified and removed by comparing the last modified time of the file read from file system with the last modified time stored in database.

classmethod single_file_on_change(file_path, event_type, config)[source]

This method is called by the crotal server. If there is a file change when the server is running, the server will call this method to let the model responding to the change of file.

Parameters
  • file_path -- The path of the designated file.
  • event_type -- A string indicating the event type, can be created, modified or deleted.
  • config -- A configuration instance.
Returns

classmethod load_main_items(config)[source]

This method handles 3 types of source file differently, loads the fields of each source file, build objects and add them into the object manager.

If the file is unmodified, then load it from crotal database; If the field is modified, then load it from the file system; If the field is removed, then remove it from the crotal database;

As unmodified file is stored in crotal database which can be accessed from memory, loading an unmodified file can be much faster than loading a modified file from the file system(hard drive). This is also known as incremental build, which makes Crotal fast.

Parameters
config -- A configuration instance.
Returns
classmethod load_extra_items(config)[source]

This method is called after all static files are load. If you want to load some other fields from the files in addition to the main fields, please override this function.

For more information please refer to the implementation of models.Post

Parameters
config -- A configuration instance.
Returns
classmethod load_single_file(file_path, config)[source]

This method is used to load a single file from the file_path.

Parameters
  • file_path -- The path of the designated file.
  • config -- A configuration instance.
Returns

An object of the loaded file.

classmethod remove_single_file(file_path)[source]

This method is used to remove a single file from the object manager and the database.

Parameters
file_path -- The path of the designated file.
classmethod from_db(path, config, row)[source]

This method is used to load a single object directly from the database.

Parameters
  • path -- Path of the designated file, which is not used.
  • config -- A configuration instance.
  • row -- A dictionary which contains the serialized information of the object.
Returns

An object of the designated file.

classmethod from_file(file_path, config)[source]

This method is used to load a single object from the file system.

Parameters
  • file_path -- The path of the designated file.
  • config -- A configuration instance.
Returns

An object of the loaded file.

classmethod from_text(file_path, text, config)[source]

This method is used to load a single object from a string.

Parameters
  • file_path -- The path of the designated file.
  • text -- The string containing object information.
  • config -- A configuration instance.
Returns

The object of the loaded file.

classmethod parse_content(file_path, content, config)[source]

Parse the content from a string and return an model object.

Parameters
  • file_path -- The path of the designated file.
  • content -- The string containing object information.
  • config -- A configuration instance.
Returns

classmethod parse_attributes(file_path, config, attributes)[source]

For each attributes read from the static file, find the corresponding field and parse the attribute by the parse method under the field.

Parameters
  • file_path -- The path of the designated file.
  • config -- A configuration instance.
  • attributes -- Attributes read from the designated file.
Returns

None

static get_file_list(base_dir, path_list)[source]

This method returns the list of all the source files in the directory indicated. Notice that there be multiple paths passed as a list and files started with '.' will be ignored.

Parameters
  • base_dir -- The base directory of the crotal site.
  • path_list -- A list of paths that files will be looked up.
Returns

None