store_manager module

Senju Database Management Module

A database interaction layer for the Senju haiku management system.

This module implements a lightweight document database abstraction using TinyDB for persistent storage of haiku poems. It provides a clean interface for storing, retrieving, updating, and managing haiku entries in the system.

Classes

StoreManager

The primary class responsible for all database operations. Handles connection management, CRUD operations, and query capabilities for haiku data.

Functions

utility_function

Provides simple arithmetic operations to support database functionalities.

Constants

DEFAULT_DB_PATH

The default filesystem location for the TinyDB database file (/var/lib/senju.json).

Dependencies

  • future.annotations: Enhanced type hint support

  • logging.Logger: Diagnostic and error logging capabilities

  • pathlib.Path: Cross-platform filesystem path handling

  • typing.Optional: Type annotations for nullable values

  • tinydb.TinyDB: Lightweight document database implementation

  • tinydb.QueryImpl: Query builder for database searches

  • senju.haiku.Haiku: Data model for haiku representation

Implementation Details

The module uses TinyDB as its storage engine, providing a JSON-based document storage solution that balances simplicity with functionality. The StoreManager abstracts all database operations behind a clean API, handling connection lifecycle and providing methods for common operations on haiku data.

exception store_manager.BadStoreManagerFileError(msg: str, *args: object)

Bases: Exception

Parameters:
  • msg (str)

  • args (object)

Return type:

None

class store_manager.StoreManager(path_to_db: Path = PosixPath('/var/lib/senju.json'))

Bases: object

Manages the storage and retrieval of haiku data using TinyDB.

This class provides an interface for saving and loading haikus from a TinyDB database file.

Variables:
  • _db – Database instance for storing haiku data.

  • logger – Logger for tracking operations and errors.

Parameters:

path_to_db (Path)

count_entries() int

Query the store how many Haikus are stored.

Returns:

Number of stored haikus.

Return type:

int

get_id_of_latest_haiku() int | None

Get the ID of the most recently added haiku.

Returns:

The ID of the latest haiku if any exists, None otherwise.

Return type:

Optional[int]

Note

Logs an error if the database is empty.

load_haiku(key: int | None) Haiku

Load a haiku by its ID.

Parameters:

key (int) – The ID of the haiku to load.

Returns:

A Haiku object if found, None otherwise.

Return type:

Optional[Haiku]

logger: Logger
save_haiku(data: Haiku) int

Save a haiku to the database.

Parameters:

data (Haiku) – The Haiku object to save.

Returns:

The document ID of the saved haiku.

Return type:

int