ogstools.core.storage module#

class ogstools.core.storage.StorageBase[source]#

Bases: ABC

Abstract base class for saveable objects in OGSTools.

Provides infrastructure for: - Persistent storage with automatic ID generation - Symlink-based efficient data management - Backup and overwrite protection - Archive creation (materializing symlinks)

Subclasses must implement: - _propagate_target(): Propagate save target to child objects - _save_impl(): Actual save logic - save(): Public save method (usually delegates to base implementation)

Initialize a StorageBase object.

Parameters:
  • class_id (str) – Type identifier for this saveable object (e.g., “Model”, “Simulation”).

  • file_ext (str) – File extension if this is a file (e.g., “yaml”, “prj”). Empty string for directories.

  • default_target (Path | str | None) – Default path for saving if none is specified.

  • id (str | None) – Unique identifier. If None, generates a timestamp-based ID.

Userpath = PosixPath('/tmp/tmpr_ma5f_f/organized')#
Temppath = PosixPath('/tmp/ogstools_root')#
Backup = False#
DefaultOverwrite = False#
__init__(class_id, file_ext='', default_target=None, id=None)[source]#

Initialize a StorageBase object.

Parameters:
  • class_id (str) – Type identifier for this saveable object (e.g., “Model”, “Simulation”).

  • file_ext (str) – File extension if this is a file (e.g., “yaml”, “prj”). Empty string for directories.

  • default_target (Path | str | None) – Default path for saving if none is specified.

  • id (str | None) – Unique identifier. If None, generates a timestamp-based ID.

class_id: str#
user_specified_target: bool#
property is_file: bool#

Check if this saveable represents a file (vs. directory).

property id: str#

Get the unique identifier of this object.

property is_saved: bool#

Check if this object has been saved to disk.

property active_target: Path | None#

Get the path where this object is currently saved, or None if not saved.

property next_target: Path#

Get the path where this object will be saved next.

Replace symlinks with actual copies of their targets.

Parameters:
  • root (Path | str) – Path to process.

  • recursive (bool) – If True, recursively traverse the directory tree and materialize all symlinks. If False, only materialize the root path itself.

Raises:

FileNotFoundError – If the root path does not exist.

Return type:

None

Create a symlink to the saved data at a new location.

Efficiently creates a reference to existing saved data without duplicating files.

Parameters:
  • new_target (Path) – Path where the symlink should be created.

  • dry_run (bool) – If True, simulate without creating the link.

Return type:

None

static saving_path()[source]#

Get the base path for saving objects.

Return type:

Path

Returns:

Resolved user path or system temp directory.

copy(target=None, id=None, deep=True)[source]#

Performs a deepcopy and reassign the associated files/ids. Use this function together with the constructor to tell, that it is your intent to protect the file, you used for creating the object. e.g. prj1 = Project(file1).copy(id=”prj1”), here you want to protect the original file1.

Parameters:
  • target (Path | str | None) – If provided, the target is the location for the next call of save(). Mutually exclusive with id.

  • id (str | None) – If provided, set the id for the next call of save().

  • deep (bool) – switch to choose between deep (default) and shallow (self.copy(deep=False)) copy.

Return type:

Self

Returns:

A deep copy of this object associated to a new file/folder or id