mafw.db.model_register

Module for registering and retrieving database models.

This module provides functionality to register database models with table names as key and retrieve them using flexible naming conventions that support prefixes and suffixes.

Added in version v2.0.0.

Classes

ModelRegister()

A registry for database models with support for prefixes and suffixes.

class mafw.db.model_register.ModelRegister[source]

Bases: object

A registry for database models with support for prefixes and suffixes.

This class allows registration of database models with table names and provides flexible retrieval mechanisms that can handle different naming conventions.

The following implemented methods are thread-safe:

Accessing the class underling containers is instead not thread safe.

Added in version v2.0.0.

clear() None[source]

Clear all registered models, prefixes, and suffixes from the registry.

This method removes all entries from the internal dictionaries and lists, effectively resetting the ModelRegister to its initial empty state.

Note

This operation cannot be undone. All previously registered models and naming conventions will be lost after calling this method.

Return type:

None

get_model(name: str) ModelBase[source]

Retrieve a model by name, supporting prefixes and suffixes.

name could be either the table_name or the ModelName.

This method attempts to find a model by the given name, considering registered prefixes and suffixes. It also handles conversion between CamelCase model names and snake_case table names using peewee’s utility.

Parameters:

name (str) – The name to search for

Returns:

The registered peewee Model class

Return type:

peewee.Model

Raises:

KeyError – If no matching model is found or multiple similar models exist

get_model_names() list[str][source]

Get a list of all registered model names.

Returns:

List of registered model names

Return type:

list[str]

get_standard_tables() List[Type[StandardTable]][source]

Retrieve all registered models that are instances of StandardTable.

This method filters the registered models and returns only those that inherit from the StandardTable base class.

This is useful for identifying and working with standard database tables that follow a specific structure or interface.

Since the introduction of the ModelRegister, there is no need any more for a standard table plugin hook, instead the user can use this method to retrieve all standard tables.

Returns:

A list of registered model classes that are standard tables

Return type:

list[peewee.ModelBase]

get_table_names() list[str][source]

Get a list of all registered table names.

Returns:

List of registered table names

Return type:

list[str]

items() list[tuple[str, ModelBase]][source]

Return the items list of the registered models dictionary.

This method provides access to all registered models through a dictionary-like items view, allowing iteration over key-value pairs of table names and their corresponding model classes.

In order to release the thread lock as soon as possible, instead of providing an iterator a list of the current snapshot of the dictionary is provided.

Returns:

An items view of the registered models

Return type:

list[[str, peewee.ModelBase]]

register_model(table_name: str, model: ModelBase) None[source]

Register a model with a specific table name.

If a model with the same table name already exists, it will be replaced with a warning message.

Parameters:
  • table_name (str) – The table name to register the model under

  • model (peewee.Model) – The peewee Model class to register

register_prefix(prefix: str) None[source]

Register a prefix to be used when searching for models.

Parameters:

prefix (str) – The prefix string to register

register_suffix(suffix: str) None[source]

Register a suffix to be used when searching for models.

Parameters:

suffix (str) – The suffix string to register