hermes.model.hermes_cache

Classes

HermesCache

The HermesCache supplies the user with easy (read and write) access to the JSON files in the cache.

HermesCacheManager

The HermesCacheManager supplies the user with easy access to the HERMES cache.

Module Contents

class hermes.model.hermes_cache.HermesCache(cache_dir: pathlib.Path)

The HermesCache supplies the user with easy (read and write) access to the JSON files in the cache.

A HermesCache object should only be obtained from an HermesCacheManager object because this ensures the correct cache directory is used.

_cache_dir

The directory the cache is located at.

Type:

Path

_cached_data

The cache of the files in the cache. The key is the filename.

Type:

dict[str, Any]

__enter__() None

Caches all files in the cache_dir.

Return type:

None

__getitem__(item: str) Any

Loads a file if necessary or returns the cached value.

Parameters:

item (str) – The name of the file.

Returns:

The JSON value in the given file.

Return type:

Any

__setitem__(key: str, value: Any) None

Writes a value into the cache.

Note that the files isn’t immediately updated only the cache is.

Parameters:
  • key (str) – The filename the data is written too.

  • value (Any) – The JSON value for the file.

Return type:

None

__exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: types.TracebackType | None) None

Updates the files from the cache.

Parameters:
  • exc_type (type[BaseException] | None) – The type of the exception.

  • exc_val – (BaseException | None): Unused

  • exc_tb – (TracebackType | None): Unused

Return type:

None

class hermes.model.hermes_cache.HermesCacheManager(project_dir: pathlib.Path = Path.cwd())

The HermesCacheManager supplies the user with easy access to the HERMES cache.

project_dir

The directory the project is located in.

Type:

Path

cache_dir

The cache directory inside the project_dir.

Type:

Path

_current_step

The list of steps (i.e. cache names).

Type:

list[str]

CACHE_DIR_NAME

(class attribute) The relative directory all HERMES caches are located in.

Type:

str

prepare_step(step: str) None

Add another cache dir to the list of steps.

Parameters:

step (str) – The new cache dir.

Return type:

None

finalize_step(step: str) None

Remove the step from the list of steps if it is the last one.

Parameters:

step (str) – The cache dir that is removed.

Return type:

None

Raises:
  • ValueError – If no step can be removed.

  • ValueError – If the given step is not the last one.

__getitem__(source_name: str) HermesCache

Return the HERMES cache at the current cache dir and the given sub dir (source_name).

Parameters:

source_name (str) – The name of the sub dir of the current cache dir.

Returns:

The HermesCache object of the cache.

Return type:

HermesCache

Raises:

HermesCacheError – If no step has been prepared (i.e. no current cache dir is set).