epymorph.cache
epymorph's file caching utilities.
CACHE_PATH
module-attribute
The root directory for epymorph's cached files.
FileTree
module-attribute
Nodes in a file tree are either directories or files.
FileError
Bases: Exception
Error during a file operation.
FileMissingError
Bases: FileError
Error loading a file, as it does not exist.
FileWriteError
Bases: FileError
Error writing a file.
FileReadError
Bases: FileError
Error loading a file.
FileVersionError
Bases: FileError
Error loading a file due to unmet version requirements.
CacheMissError
Bases: FileError
Raised on a cache-miss (for any reason) during a load-from-cache operation.
CacheWarning
Bases: Warning
Warning issued when we are unable to interact with the file cache but in a situation where program execution can continue, even if less optimally. For example: if we successfully load data from an external source but are unable to cache it for later, this is a warning because we assume the data is valid and that it could always be loaded again from the same source at a later time. The warning is issued to give the user the opportunity to fix it for next time.
Directory
Bases: NamedTuple
A directory.
File
Bases: NamedTuple
A file.
module_cache_path
Return the cache directory to use for the given module.
When epymorph modules need to store files in the cache, they should use a
subdirectory within the application's cache path. The path should correspond to the
module's path within epymorph. e.g.: module epymorph.adrio.acs5
will store files
in $CACHE_PATH/adrio/acs5
.
Parameters:
-
name
(str
) –The name of the module, typically given as
__name__
.
Returns:
-
Path
–The cache directory to use, as a relative path since the cache functions require that.
Examples:
save_file
load_file
Load a single file.
Parameters:
Returns:
-
BytesIO
–The bytes of the file.
Raises:
-
FileReadError
–If the file cannot be loaded for any reason.
save_bundle
Save a bundle of files in our tar format with an associated version number.
Parameters:
-
to_path
(str | PathLike[str]
) –An absolute or relative path to store the file. Relative paths will be resolved against the current working directory. Folders in the path which do not exist will be created automatically.
-
version
(int
) –The version number for the archive.
-
files
(dict[str, BytesIO]
) –The files to store, with a file name for each (its name within the archive).
load_bundle
Load a bundle of files in our tar format, optionally enforcing a minimum version.
Parameters:
-
from_path
(str | PathLike[str]
) –The path of the bundle.
-
version_at_least
(int
, default:-1
) –The minimally accepted version number to load. -1 to accept any version.
Returns:
-
dict[str, BytesIO]
–A dictionary of the contained files, mapping the internal file names to the bytes of the file.
Raises:
-
FileMissingError
–If the file doesn't exist.
-
FileReadError
–If the file cannot be read or is not valid.
-
FileVersionError
–If there is an existing file but the version is below the minimum allowed.
check_file_in_cache
save_file_to_cache
Save a single file to the cache (overwriting the existing file, if any).
This is a low-level building block.
Parameters:
-
to_path
(str | PathLike[str]
) –A path to store the file, relative to the cache folder.
-
file
(BytesIO
) –The file bytes to store.
Raises:
-
FileWriteError
–If the file cannot be saved for any reason.
load_file_from_cache
load_or_fetch
Attempt to load a file from the cache.
If the file isn't already in the cache, the provided fetch
method is used to load
the file and then it is cached for next time. Any exceptions raised by fetch
will
not be caught in this method. If the file was successfully loaded but failed to save
to the cache, a warning is raised.
This is a higher-level but still fairly generic building block.
Parameters:
-
cache_path
(Path
) –The path to the file, relative to the cache folder.
-
fetch
(Callable[[], BytesIO]
) –The function to load the file if it's not in the cache.
Returns:
-
BytesIO
–The file bytes.
load_or_fetch_url
Attempt to load a file from the cache.
If the file isn't already in the cache, the provided url
is used to load
the file and then it is cached for next time. Any exceptions raised by fetch
will
not be caught in this method. If the file was successfully loaded but failed to save
to the cache, a warning is raised. URLs must use the http or https protocol.
This is a higher-level but still fairly generic building block.
Parameters:
-
url
(str
) –The url to locate the file if it's not in the cache.
-
cache_path
(Path
) –The path to the file, relative to the cache folder.
Returns:
-
BytesIO
–The file bytes.
save_bundle_to_cache
save_bundle_to_cache(
to_path: str | PathLike[str],
version: int,
files: dict[str, BytesIO],
) -> None
Save a tar bundle of files to the cache (overwriting the existing file, if any).
The tar includes the sha256 checksums of every content file, and a version file indicating which application version was responsible for writing the file (thus allowing the application to decide if a cached file is still valid when reading it).
Parameters:
load_bundle_from_cache
load_bundle_from_cache(
from_path: str | PathLike[str],
version_at_least: int = -1,
) -> dict[str, BytesIO]
Load a tar bundle of files from the cache.
Parameters:
-
from_path
(str | PathLike[str]
) –The path of the bundle, relative to the cache folder.
-
version_at_least
(int
, default:-1
) –The minimally accepted version number to load. -1 to accept any version.
Raises:
-
CacheMissError
–If the bundle cannot be loaded.
format_file_size
cache_remove_confirmation
Create a function which removes a directory or file from the cache.
Also returns the resolved path to the thing that will be removed; this allows the application to confirm the removal.
Parameters:
Returns: