cookbase.db

Module contents

A package implementing connection handlers to third-party DBMS.

The most common way to interact with this package is through the handler.get_handler() method, which provides a handler.DBHandler object with database communication functionalities.

Submodules

cookbase.db.exceptions

exception cookbase.db.exceptions.CBRGraphInsertionError(partial_result)[source]

Bases: cookbase.db.exceptions.InsertionError

Raised when a CBRGraph insertion resulted unsuccessful.

exception cookbase.db.exceptions.CBRInsertionError(partial_result)[source]

Bases: cookbase.db.exceptions.InsertionError

Raised when a CBR insertion resulted unsuccessful.

exception cookbase.db.exceptions.DBClientConnectionError(db_id: str)[source]

Bases: cookbase.db.exceptions.DBHandlerException

Raised when trying to access a database results in a connection error.

Variables:db_id (str) – The database id
exception cookbase.db.exceptions.DBHandlerException[source]

Bases: Exception

Base class for cookbase.db.handler errors.

db_handler_class_name = 'cookbase.db.handler.DBHandler'
exception cookbase.db.exceptions.DBNotRegisteredError(db_id: str)[source]

Bases: cookbase.db.exceptions.DBHandlerException

Raised when trying to access a non-registered database.

Variables:db_id (str) – The database identifier
exception cookbase.db.exceptions.InsertionError[source]

Bases: cookbase.db.exceptions.DBHandlerException

Base class for exceptions raised when an insertion operation resulted unsuccessful.

exception cookbase.db.exceptions.InvalidDBTypeError(invalid_db_type: str)[source]

Bases: cookbase.db.exceptions.DBHandlerException

Raised when trying to use an invalid database type.

Variables:invalid_db_type (str) – The invalid database type

cookbase.db.handler

class cookbase.db.handler.DBHandler(mongodb_url: str, db_type: str = 'mongodb', db_name: str = 'cookbase')[source]

Bases: object

A class that handles connections to database instances in order to store and retrieve the different Cookbase Data Model (CBDM) elements.

Parameters:
  • mongodb_url (str) – A MongoDB connection URI to use as default database
  • db_type (str) – An identifier of the database connection to use, defaults to DBTypes.MONGODB
  • db_name (str) – The name of the database to connect to, defaults to 'cookbase'
Raises:
Variables:
  • _default_db_id (str) – The default database identifier with the form 'db_type:db_name', defaults to 'mongodb:cookbase'
  • _connections (dict[str, Any]) – A dictionary containing the data about all the handled connections
  • _default_db (Any) – The default database client
class DBTypes[source]

Bases: object

Helper class registering the different types of databases that are handled by cookbase.db.handler.DBHandler.

MONGODB = 'mongodb'
close_connections()[source]

Closes all connections registered by this DBHandler object.

get_cba(cba_id: int) → Dict[str, Any][source]

Retrieves a Cookbase Appliance (CBA) from database.

Parameters:cba_id (int) – CBA identifier
Returns:The requested CBA
Return type:dict[str, Any]
get_cbi(cbi_id: int) → Dict[str, Any][source]

Retrieves a Cookbase Ingredient (CBI) from database.

Parameters:cbi_id (int) – CBI identifier
Returns:The requested CBI
Return type:dict[str, Any]
get_cbp(cbp_id: int) → Dict[str, Any][source]

Retrieves a Cookbase Process (CBP) from database.

Parameters:cbp_id (int) – CBP identifier
Returns:The requested CBP
Return type:dict[str, Any]
get_cbr(query: Dict[str, Any]) → Dict[str, Any][source]

Retrieves a CBR from database.

Parameters:query (dict[str, Any]) – A dictionary specifying the query
Returns:The requested CBR
Return type:dict[str, Any]
get_db_client(db_id: str = 'mongodb:cookbase') → Any[source]

Retrieves the requested database client.

Parameters:

db_id (str, optional) – A database identifier with the form 'db_type:db_name', defaults to None

Returns:

The requested database client

Return type:

Any

Raises:
insert_cbr(cbr: Dict[str, Any], cbrgraph: Optional[cookbase.graph.cbrgraph.CBRGraph] = None) → cookbase.db.handler.InsertCBRResult[source]

Inserts a CBR into database with its CBRGraph (if given).

Parameters:
  • cbr (dict[str, Any]) – A dictionary representing the CBR
  • cbrgraph (dict[str, Any], optional) – A dictionary representing the CBRGraph
Returns:

A InsertCBRResult object holding the insertion results.

Return type:

InsertCBRResult

Raises:
class cookbase.db.handler.InsertCBRResult(cbr_id: Optional[bson.objectid.ObjectId] = None, cbrgraph_id: Optional[bson.objectid.ObjectId] = None)[source]

Bases: object

A class containing the results from the DBHandler.insert_cbr() method.

Parameters:
  • cbr_id (ObjectId, optional) – Field taking the database identifier of the inserted Cookbase Recipe (CBR), or None if the insertion was not performed, defaults to None
  • cbrgraph_id (ObjectId, optional) – Field taking the database identifier of the inserted Cookbase Recipe Graph (CBRGraph), or None if the insertion was not performed, defaults to None
cookbase.db.handler.get_handler(credentials_path: Optional[str] = None, force_new_instance: bool = False)[source]

Provides the database handler instance.

The first time this function is called (or if the force_new_instance flag is set to True) a DBHandler object is instantiated and returned according to the credentials provided in the file located at credentials_path; if called after the first time (and being the force_new_instance flag set to False), it returns the already available instance, disregarding the credentials_path argument.

Parameters:
  • credentials_path (str or None, optional) – Path to the file containing the connection credentials
  • force_new_instance (bool, optional) – A flag indicating whether a new database handler instance must be initialized, defaults to False
Returns:

A DBHandler instance connected to the default database

Return type:

DBHandler

cookbase.db.utils

cookbase.db.utils.demongofy(f: Callable)[source]

Decorator function that processes a JSON document retrieved from MongoDB.

Parameters:f (Callable) – The decorated function
cookbase.db.utils.deunderscore_id(o)[source]
cookbase.db.utils.underscore_id(o)[source]