Skip to content

Commit fee3b70

Browse files
Update docstrings in local_storage.py (#13993)
1 parent ed77de3 commit fee3b70

1 file changed

Lines changed: 137 additions & 18 deletions

File tree

src/ert/storage/local_storage.py

Lines changed: 137 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@
3737
def open_storage(
3838
path: str | os.PathLike[str], mode: ModeLiteral | Mode = "r"
3939
) -> ert.storage.Storage:
40+
"""
41+
Opens the local storage at the given path.
42+
43+
Parameters
44+
----------
45+
path : {str, path-like}
46+
The file system path to the storage.
47+
mode : {ModeLiteral, Mode}
48+
The access mode for the storage ("r" for read, "w" for write).
49+
Defaults to read.
50+
51+
Returns
52+
-------
53+
storage : Storage
54+
The opened storage.
55+
56+
Raises
57+
------
58+
ErtStoragePermissionError
59+
If the storage cannot be accessed due to insufficient permissions.
60+
ErtStorageException
61+
If the storage cannot be opened for any other reason.
62+
"""
4063
_ = LocalStorage.check_migration_needed(Path(path))
4164

4265
try:
@@ -97,6 +120,16 @@ def __init__(
97120
The access mode for the storage (read/write).
98121
stage_for_migration : bool
99122
Whether to avoid reloading storage to allow migration
123+
124+
Raises
125+
------
126+
TimeoutError
127+
If the storage lock cannot be acquired in write mode.
128+
ValueError
129+
If no index.json is found but other storage components exist.
130+
RuntimeError
131+
If the storage is opened in read-only mode but its version is too
132+
old and requires migration.
100133
"""
101134

102135
self.path = Path(path).absolute()
@@ -139,6 +172,25 @@ def __init__(
139172

140173
@staticmethod
141174
def check_migration_needed(storage_dir: Path) -> bool:
175+
"""
176+
Checks whether the storage at the given path needs to be migrated.
177+
178+
Parameters
179+
----------
180+
storage_dir : Path
181+
The file system path to the storage.
182+
183+
Returns
184+
-------
185+
migration_needed : bool
186+
True if the storage version is older than the current version and
187+
must be migrated, False otherwise.
188+
189+
Raises
190+
------
191+
ErtStorageException
192+
If the storage version is newer than the current version.
193+
"""
142194
try:
143195
version = _storage_version(storage_dir)
144196
except FileNotFoundError:
@@ -155,6 +207,21 @@ def check_migration_needed(storage_dir: Path) -> bool:
155207

156208
@staticmethod
157209
def perform_migration(path: Path) -> None:
210+
"""
211+
Migrates the storage at the given path to the current version.
212+
213+
Does nothing if the storage is already up-to-date.
214+
215+
Parameters
216+
----------
217+
path : Path
218+
The file system path to the storage.
219+
220+
Raises
221+
------
222+
ErtStorageException
223+
If the storage version is newer than the current version.
224+
"""
158225
if LocalStorage.check_migration_needed(path):
159226
with LocalStorage(path, Mode("w"), stage_for_migration=True) as storage:
160227
storage._migrate(storage.version)
@@ -189,21 +256,29 @@ def get_experiment(self, uuid: UUID) -> LocalExperiment:
189256
-------
190257
local_experiment : LocalExperiment
191258
The experiment associated with the given UUID.
259+
260+
Raises
261+
------
262+
KeyError
263+
If no experiment with the given UUID is found.
192264
"""
193265

194266
return self._experiments[uuid]
195267

196268
def get_experiment_by_name(self, name: str) -> LocalExperiment:
197269
"""
198270
Retrieves an experiment by name.
271+
199272
Parameters
200273
----------
201274
name : str
202275
The name of the experiment to retrieve.
276+
203277
Returns
204278
-------
205279
local_experiment : LocalExperiment
206280
The experiment associated with the given name.
281+
207282
Raises
208283
------
209284
KeyError
@@ -220,12 +295,20 @@ def get_ensemble(self, uuid: UUID | str) -> LocalEnsemble:
220295
221296
Parameters
222297
----------
223-
uuid : UUID
298+
uuid : {UUID, str}
224299
The UUID of the ensemble to retrieve.
225300
226301
Returns
302+
-------
227303
local_ensemble : LocalEnsemble
228304
The ensemble associated with the given UUID.
305+
306+
Raises
307+
------
308+
ValueError
309+
If uuid is a string that is not a valid UUID.
310+
KeyError
311+
If no ensemble with the given UUID is found.
229312
"""
230313
if isinstance(uuid, str):
231314
uuid = UUID(uuid)
@@ -309,6 +392,15 @@ def __exit__(
309392

310393
@require_write
311394
def _acquire_lock(self) -> None:
395+
"""
396+
Acquires the exclusive file lock for the storage.
397+
398+
Raises
399+
------
400+
TimeoutError
401+
If the lock cannot be acquired within ``LOCK_TIMEOUT`` seconds,
402+
typically because another ERT process is using the same ENSPATH.
403+
"""
312404
self._lock = FileLock(self.path / "storage.lock")
313405
try:
314406
self._lock.acquire(timeout=self.LOCK_TIMEOUT)
@@ -349,23 +441,23 @@ def create_experiment(
349441
350442
Parameters
351443
----------
352-
parameters : list of ParameterConfig, optional
353-
The parameters for the experiment.
354-
responses : list of ResponseConfig, optional
355-
The responses for the experiment.
356-
observations : dict of str to observation datasets, optional
357-
The observations for the experiment.
358-
simulation_arguments : SimulationArguments, optional
359-
The simulation arguments for the experiment.
444+
experiment_config : ExperimentConfig, optional
445+
The configuration for the experiment, holding parameters,
446+
responses, observations and other experiment settings. An empty
447+
configuration is used if none is provided.
360448
name : str, optional
361-
The name of the experiment.
362-
templates : list of tuple[str, str], optional
363-
Run templates for the experiment. Defaults to None.
449+
The name of the experiment. If None, the current date in ISO
450+
format (YYYY-MM-DD) is used.
364451
365452
Returns
366453
-------
367454
local_experiment : LocalExperiment
368455
The newly created experiment.
456+
457+
Raises
458+
------
459+
FileExistsError
460+
If an experiment directory with the generated id already exists.
369461
"""
370462
if experiment_config is None:
371463
experiment_config = ExperimentConfig()
@@ -398,9 +490,6 @@ def create_ensemble(
398490
"""
399491
Creates a new ensemble in the storage.
400492
401-
Raises a ValueError if the ensemble size is larger than the prior
402-
ensemble.
403-
404493
Parameters
405494
----------
406495
experiment : {LocalExperiment, UUID}
@@ -418,6 +507,13 @@ def create_ensemble(
418507
-------
419508
local_ensemble : LocalEnsemble
420509
The newly created ensemble.
510+
511+
Raises
512+
------
513+
ValueError
514+
If the ensemble size is larger than the prior ensemble.
515+
FileExistsError
516+
If an ensemble directory with the generated id already exists.
421517
"""
422518

423519
experiment_id = experiment if isinstance(experiment, UUID) else experiment.id
@@ -694,6 +790,27 @@ def _to_parquet_transaction(
694790

695791

696792
def _storage_version(path: Path) -> int:
793+
"""
794+
Determines the storage version at the given path.
795+
796+
Parameters
797+
----------
798+
path : Path
799+
The file system path to the storage.
800+
801+
Returns
802+
-------
803+
version : int
804+
The storage version. Returns the current version if the path does not
805+
exist, and 0 if the path holds legacy block storage.
806+
807+
Raises
808+
------
809+
FileNotFoundError
810+
If the path exists but contains neither an index.json nor block storage.
811+
NotImplementedError
812+
If the index.json does not contain a version key.
813+
"""
697814
if not path.exists():
698815
return _LOCAL_STORAGE_VERSION
699816
if not (path / "index.json").exists():
@@ -736,13 +853,15 @@ def local_storage_get_ert_config() -> ErtConfig:
736853
This function should be called after `local_storage_set_ert_config` has
737854
been used to set the ErtConfig instance.
738855
739-
Raises an AssertionError uf the ErtConfig has not been set before calling
740-
this function.
741-
742856
Returns
743857
-------
744858
ert_config : ErtConfig
745859
The ErtConfig instance.
860+
861+
Raises
862+
------
863+
AssertionError
864+
If the ErtConfig has not been set before calling this function.
746865
"""
747866

748867
assert _migration_ert_config is not None, (

0 commit comments

Comments
 (0)