From 66a7e007d2d303234654928a982fdfddc111984b Mon Sep 17 00:00:00 2001 From: slangeveld Date: Tue, 6 Aug 2024 15:43:02 +0200 Subject: [PATCH] ENH: Create enum for tracklog event type --- schema/definitions/0.8.0/schema/fmu_meta.json | 20 +++++++++++-------- src/fmu/dataio/_metadata.py | 6 +++--- src/fmu/dataio/_model/enums.py | 8 ++++++++ src/fmu/dataio/_model/fields.py | 4 ++-- src/fmu/dataio/preprocessed.py | 4 ++-- tests/test_units/test_metadata_class.py | 11 +++++----- 6 files changed, 33 insertions(+), 20 deletions(-) diff --git a/schema/definitions/0.8.0/schema/fmu_meta.json b/schema/definitions/0.8.0/schema/fmu_meta.json index c3c395b1e..398e1f162 100644 --- a/schema/definitions/0.8.0/schema/fmu_meta.json +++ b/schema/definitions/0.8.0/schema/fmu_meta.json @@ -8430,6 +8430,16 @@ "title": "Timestamp", "type": "object" }, + "TrackLogEventType": { + "description": "The type of event being logged", + "enum": [ + "created", + "updated", + "merged" + ], + "title": "TrackLogEventType", + "type": "string" + }, "TracklogEvent": { "description": "The ``tracklog`` block contains a record of events recorded on these data.\nThis data object describes a tracklog event.", "properties": { @@ -8442,13 +8452,7 @@ "type": "string" }, "event": { - "examples": [ - "created", - "updated", - "merged" - ], - "title": "Event", - "type": "string" + "$ref": "#/$defs/TrackLogEventType" }, "sysinfo": { "anyOf": [ @@ -9463,4 +9467,4 @@ } }, "title": "Root" -} +} \ No newline at end of file diff --git a/src/fmu/dataio/_metadata.py b/src/fmu/dataio/_metadata.py index 9b3824c2a..00d00c931 100644 --- a/src/fmu/dataio/_metadata.py +++ b/src/fmu/dataio/_metadata.py @@ -10,13 +10,13 @@ import os import platform from datetime import timezone -from typing import TYPE_CHECKING, Final, Literal +from typing import TYPE_CHECKING, Final from pydantic import AnyHttpUrl, TypeAdapter from ._definitions import SCHEMA, SOURCE, VERSION from ._logging import null_logger -from ._model import fields, internal +from ._model import enums, fields, internal from .exceptions import InvalidMetadataError from .providers._filedata import FileDataProvider from .providers.objectdata._provider import objectdata_provider_factory @@ -32,7 +32,7 @@ def generate_meta_tracklog( - event: Literal["created", "merged"] = "created", + event: enums.TrackLogEventType = enums.TrackLogEventType.created, ) -> list[fields.TracklogEvent]: """Initialize the tracklog with the 'created' event only.""" return [ diff --git a/src/fmu/dataio/_model/enums.py b/src/fmu/dataio/_model/enums.py index 6a0d55f1e..38cb34013 100644 --- a/src/fmu/dataio/_model/enums.py +++ b/src/fmu/dataio/_model/enums.py @@ -99,3 +99,11 @@ class DomainReference(str, Enum): msl = "msl" sb = "sb" rkb = "rkb" + + +class TrackLogEventType(str, Enum): + """The type of event being logged""" + + created = "created" + updated = "updated" + merged = "merged" diff --git a/src/fmu/dataio/_model/fields.py b/src/fmu/dataio/_model/fields.py index c0d7b5f6f..60e7c823e 100644 --- a/src/fmu/dataio/_model/fields.py +++ b/src/fmu/dataio/_model/fields.py @@ -418,8 +418,8 @@ class TracklogEvent(BaseModel): ) """A datetime representation recording when the event occurred.""" - event: str = Field(examples=["created", "updated", "merged"]) - """A string containing a reference to the type of event being logged.""" + event: enums.TrackLogEventType + """The type of event being logged. See :class:`enums.TrackLogEventType`.""" user: User """The user who caused the event to happen. See :class:`User`.""" diff --git a/src/fmu/dataio/preprocessed.py b/src/fmu/dataio/preprocessed.py index ecafddb1d..3f622a387 100644 --- a/src/fmu/dataio/preprocessed.py +++ b/src/fmu/dataio/preprocessed.py @@ -11,7 +11,7 @@ from ._logging import null_logger from ._metadata import generate_meta_tracklog -from ._model import internal +from ._model import enums, internal from ._model.enums import FMUContext from ._model.fields import File from ._utils import export_metadata_file, md5sum @@ -185,7 +185,7 @@ def _get_updated_metadata(self, meta_existing: dict, objfile: Path) -> dict: meta_existing["file"] = self._get_meta_file(objfile, checksum_md5_file) # update the tracklog block - tracklog_entry = generate_meta_tracklog(event="merged") + tracklog_entry = generate_meta_tracklog(enums.TrackLogEventType.merged) meta_existing["tracklog"].extend(tracklog_entry) try: diff --git a/tests/test_units/test_metadata_class.py b/tests/test_units/test_metadata_class.py index 83169885b..3cab2abde 100644 --- a/tests/test_units/test_metadata_class.py +++ b/tests/test_units/test_metadata_class.py @@ -11,6 +11,7 @@ VERSION, generate_export_metadata, ) +from fmu.dataio._model import enums from fmu.dataio._model.fields import ( OperatingSystem, TracklogEvent, @@ -47,10 +48,10 @@ def test_generate_meta_tracklog_fmu_dataio_version(regsurf, edataobj1): tracklog = mymeta.tracklog assert isinstance(tracklog, list) - assert len(tracklog) == 1 # assume "created" + assert len(tracklog) == 1 # assume enums.TrackLogEventType.created parsed = TracklogEvent.model_validate(tracklog[0]) - assert parsed.event == "created" + assert parsed.event == enums.TrackLogEventType.created # datetime in tracklog shall include time zone offset assert parsed.datetime.tzinfo is not None @@ -70,10 +71,10 @@ def test_generate_meta_tracklog_komodo_version(edataobj1, regsurf, monkeypatch): tracklog = mymeta.tracklog assert isinstance(tracklog, list) - assert len(tracklog) == 1 # assume "created" + assert len(tracklog) == 1 # assume enums.TrackLogEventType.created parsed = TracklogEvent.model_validate(tracklog[0]) - assert parsed.event == "created" + assert parsed.event == enums.TrackLogEventType.created # datetime in tracklog shall include time zone offset assert parsed.datetime.tzinfo is not None @@ -90,7 +91,7 @@ def test_generate_meta_tracklog_operating_system(edataobj1, regsurf): tracklog = mymeta.tracklog assert isinstance(tracklog, list) - assert len(tracklog) == 1 # assume "created" + assert len(tracklog) == 1 # assume enums.TrackLogEventType.created parsed = TracklogEvent.model_validate(tracklog[0]) assert isinstance(