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.InsertionErrorRaised when a CBRGraph insertion resulted unsuccessful.
-
exception
cookbase.db.exceptions.CBRInsertionError(partial_result)[source]¶ Bases:
cookbase.db.exceptions.InsertionErrorRaised when a CBR insertion resulted unsuccessful.
-
exception
cookbase.db.exceptions.DBClientConnectionError(db_id: str)[source]¶ Bases:
cookbase.db.exceptions.DBHandlerExceptionRaised 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:
ExceptionBase class for
cookbase.db.handlererrors.-
db_handler_class_name= 'cookbase.db.handler.DBHandler'¶
-
-
exception
cookbase.db.exceptions.DBNotRegisteredError(db_id: str)[source]¶ Bases:
cookbase.db.exceptions.DBHandlerExceptionRaised 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.DBHandlerExceptionBase class for exceptions raised when an insertion operation resulted unsuccessful.
-
exception
cookbase.db.exceptions.InvalidDBTypeError(invalid_db_type: str)[source]¶ Bases:
cookbase.db.exceptions.DBHandlerExceptionRaised 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:
objectA 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: - DBClientConnectionError – The database connection could not be established
- InvalidDBTypeError – The given database type is not registered as a valid database type
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:
objectHelper class registering the different types of databases that are handled by
cookbase.db.handler.DBHandler.-
MONGODB= 'mongodb'¶
-
-
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 toNoneReturns: The requested database client
Return type: Any
Raises: - cookbase.db.exceptions.InvalidDBTypeError – The database type is not valid
- cookbase.db.exceptions.DBNotRegisteredError – The database client is not registered
-
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: Returns: A
InsertCBRResultobject holding the insertion results.Return type: Raises: - CBRInsertionError – The CBR could not be stored
- CBRGraphInsertionError – The CBRGraph could not be stored
- pymongo.errors.PyMongoError – Database error produced during insertion
-
class
cookbase.db.handler.InsertCBRResult(cbr_id: Optional[bson.objectid.ObjectId] = None, cbrgraph_id: Optional[bson.objectid.ObjectId] = None)[source]¶ Bases:
objectA 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
Noneif the insertion was not performed, defaults toNone - cbrgraph_id (ObjectId, optional) – Field taking the database identifier of the inserted
Cookbase Recipe Graph (CBRGraph), or
Noneif the insertion was not performed, defaults toNone
- cbr_id (ObjectId, optional) – Field taking the database identifier of the inserted Cookbase
Recipe (CBR), or
-
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) aDBHandlerobject 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 toFalse), 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
DBHandlerinstance connected to the default databaseReturn type: