Skip to content

Commit

Permalink
MAINT: Use validated config as input to case metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
tnatt committed Jan 13, 2025
1 parent c9329d6 commit 9b9d9a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
30 changes: 18 additions & 12 deletions src/fmu/dataio/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ._logging import null_logger
from ._metadata import CaseMetadataExport
from ._model import global_configuration
from ._model.fields import Access, Case, Masterdata, Model, User
from ._model.fields import Case, User

logger: Final = null_logger(__name__)

Expand Down Expand Up @@ -63,12 +63,6 @@ def __post_init__(self) -> None:
self._casepath = Path(self.rootfolder)
self._metafile = self._casepath / "share/metadata/fmu_case.yml"

# For this class, the global config must be valid; hence error if not
try:
global_configuration.GlobalConfiguration.model_validate(self.config)
except ValidationError as e:
global_configuration.validation_error_warning(e)
raise
logger.info("Ran __post_init__ for CreateCaseMetadata")

def _establish_metadata_files(self) -> bool:
Expand Down Expand Up @@ -106,6 +100,16 @@ def generate_metadata(self) -> dict:
A dictionary with case metadata or an empty dictionary if the metadata
already exists.
"""

# For this class, the global config must be valid; hence error if not
try:
config = global_configuration.GlobalConfiguration.model_validate(
self.config
)
except ValidationError as e:
global_configuration.validation_error_warning(e)
raise

if not self._establish_metadata_files():
exists_warning = (
"The case metadata file already exists and will not be overwritten. "
Expand All @@ -116,13 +120,15 @@ def generate_metadata(self) -> dict:
warnings.warn(exists_warning, UserWarning)
return {}

# TODO: Change to direct use of config.access when version 3.0 is released
self._metadata = CaseMetadataExport(
masterdata=Masterdata.model_validate(self.config["masterdata"]),
access=Access.model_validate(self.config["access"]),
masterdata=config.masterdata,
access=fields.Access(
asset=config.access.asset,
classification=config.access.classification,
),
fmu=fields.FMUBase(
model=Model.model_validate(
self.config["model"],
),
model=config.model,
case=Case(
name=self.casename,
uuid=self._case_uuid(),
Expand Down
3 changes: 2 additions & 1 deletion tests/test_units/test_initialize_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,15 @@ def test_create_case_metadata_export_with_norsk_alphabet(
monkeypatch.chdir(fmurun)
caseroot = fmurun.parent.parent

globalconfig2["masterdata"]["smda"]["field"][0]["identifier"] = "æøå"

icase = CreateCaseMetadata(
config=globalconfig2,
rootfolder=caseroot,
casename="MyCaseName_with_Æ",
caseuser="MyUser",
description="Søme description",
)
globalconfig2["masterdata"]["smda"]["field"][0]["identifier"] = "æøå"

fmu_case_yml = Path(icase.export())
assert fmu_case_yml.exists()
Expand Down

0 comments on commit 9b9d9a1

Please sign in to comment.