Storage
What it is
Utility helpers for locating a project-level storage/ directory and ensuring module/component data directories exist under a standard layout.
Public API
-
Exception
NoStorageFolderFound: Raised byfind_storage_folder()when nostoragefolder is found up to filesystem root (/).
-
Functions
find_storage_folder(base_path: str, needle: str = "storage") -> str
Recursively searches upward frombase_pathfor a directory namedneedle(default:storage) and returns its path.ensure_data_directory(module_name: str, component: str) -> str
Creates (if needed) and returns the absolute path to:storage/datastore/core/modules/<module_name>/<component>
Configuration/Dependencies
- Uses the standard library
osmodule. ensure_data_directory()creates directories relative to the current working directory.
Usage
from naas_abi_core.utils.Storage import find_storage_folder, ensure_data_directory, NoStorageFolderFound
# Ensure a module/component data directory exists (created relative to CWD)
data_dir = ensure_data_directory("__demo__", "orchestration")
print(data_dir) # absolute path
# Find the nearest "storage" folder by walking up from a starting path
try:
storage_path = find_storage_folder(data_dir) # or any base path within the project
print(storage_path)
except NoStorageFolderFound:
print("No storage folder found")
Caveats
find_storage_folder():- Assumes a Unix-style root (
/) as the stop condition. - Only checks existence of the
needlepath; it does not validate that it is a directory.
- Assumes a Unix-style root (
ensure_data_directory():- Creates directories under
./storage/...based on the current working directory, not based onfind_storage_folder().
- Creates directories under