From 1370241c3bfa18b48c270a09de0b9cca604e8b7e Mon Sep 17 00:00:00 2001 From: mferrera Date: Mon, 23 Dec 2024 09:22:05 +0100 Subject: [PATCH] MAINT: Template schema version and add dev schema url --- .../definitions/0.8.0/schema/fmu_results.json | 2 +- src/fmu/dataio/_definitions.py | 20 +++++++++++++++---- src/fmu/dataio/_metadata.py | 6 +++--- src/fmu/dataio/_model/root.py | 4 +++- src/fmu/dataio/_model/schema.py | 6 +++--- tests/test_units/test_metadata_class.py | 12 ++++------- tools/update_schema | 9 +++++---- 7 files changed, 35 insertions(+), 24 deletions(-) diff --git a/schema/definitions/0.8.0/schema/fmu_results.json b/schema/definitions/0.8.0/schema/fmu_results.json index 35ce99019..cfdfa223f 100644 --- a/schema/definitions/0.8.0/schema/fmu_results.json +++ b/schema/definitions/0.8.0/schema/fmu_results.json @@ -10879,7 +10879,7 @@ "type": "object" } }, - "$id": "fmu_results.json", + "$id": "https://main-fmu-schemas-prod.radix.equinor.com/schemas/0.8.0/fmu_results.json", "$schema": "https://json-schema.org/draft/2020-12/schema", "discriminator": { "propertyName": "class" diff --git a/src/fmu/dataio/_definitions.py b/src/fmu/dataio/_definitions.py index c3182e6d6..47cde02f8 100644 --- a/src/fmu/dataio/_definitions.py +++ b/src/fmu/dataio/_definitions.py @@ -5,10 +5,22 @@ from enum import Enum from typing import Final -SCHEMA: Final = ( - "https://main-fmu-schemas-prod.radix.equinor.com/schemas/0.8.0/fmu_results.json" -) -VERSION: Final = "0.8.0" + +class FmuResultsSchema: + DEV_ROOT: Final[str] = "https://main-fmu-schemas-dev.radix.equinor.com/schemas" + PROD_ROOT: Final[str] = "https://main-fmu-schemas-prod.radix.equinor.com/schemas" + VERSION: Final[str] = "0.8.0" + FILENAME: Final[str] = "fmu_results.json" + DEV_URL: Final[str] = f"{DEV_ROOT}/{VERSION}/{FILENAME}" + PROD_URL: Final[str] = f"{PROD_ROOT}/{VERSION}/{FILENAME}" + + @staticmethod + def url() -> str: + """This method is meant to return the `PROD_URL` or `DEV_URL` under relevant + circumstances.""" + return FmuResultsSchema.PROD_URL + + SOURCE: Final = "fmu" diff --git a/src/fmu/dataio/_metadata.py b/src/fmu/dataio/_metadata.py index 1082b24d5..8542e137c 100644 --- a/src/fmu/dataio/_metadata.py +++ b/src/fmu/dataio/_metadata.py @@ -9,7 +9,7 @@ from pydantic import AnyHttpUrl, TypeAdapter -from ._definitions import SCHEMA, SOURCE, VERSION +from ._definitions import SOURCE, FmuResultsSchema from ._logging import null_logger from ._model import fields, schema from ._model.global_configuration import GlobalConfiguration @@ -104,8 +104,8 @@ def generate_export_metadata( objdata = objectdata_provider_factory(obj, dataio) return schema.InternalObjectMetadata( - schema_=TypeAdapter(AnyHttpUrl).validate_strings(SCHEMA), # type: ignore[call-arg] - version=VERSION, + schema_=TypeAdapter(AnyHttpUrl).validate_strings(FmuResultsSchema.url()), # type: ignore[call-arg] + version=FmuResultsSchema.VERSION, source=SOURCE, class_=objdata.classname, fmu=_get_meta_fmu(fmudata) if fmudata else None, diff --git a/src/fmu/dataio/_model/root.py b/src/fmu/dataio/_model/root.py index 627f5d152..bcac17d02 100644 --- a/src/fmu/dataio/_model/root.py +++ b/src/fmu/dataio/_model/root.py @@ -12,6 +12,8 @@ from pydantic.json_schema import GenerateJsonSchema from typing_extensions import Annotated +from fmu.dataio._definitions import FmuResultsSchema + from .data import AnyData from .enums import FMUClass from .fields import ( @@ -268,7 +270,7 @@ def generate( ) -> dict[str, Any]: json_schema = super().generate(schema, mode=mode) json_schema["$schema"] = self.schema_dialect - json_schema["$id"] = "fmu_results.json" + json_schema["$id"] = FmuResultsSchema.url() json_schema["$contractual"] = self.contractual # sumo-core's validator does not recognize these. diff --git a/src/fmu/dataio/_model/schema.py b/src/fmu/dataio/_model/schema.py index e9923b2ba..e1e6414ce 100644 --- a/src/fmu/dataio/_model/schema.py +++ b/src/fmu/dataio/_model/schema.py @@ -20,7 +20,7 @@ model_validator, ) -from fmu.dataio._definitions import SCHEMA, SOURCE, VERSION +from fmu.dataio._definitions import SOURCE, FmuResultsSchema from . import data, enums, fields @@ -102,9 +102,9 @@ def _validate_input(cls, values: dict) -> dict: class JsonSchemaMetadata(BaseModel, populate_by_name=True): schema_: AnyHttpUrl = Field( alias="$schema", - default=TypeAdapter(AnyHttpUrl).validate_python(SCHEMA), + default=TypeAdapter(AnyHttpUrl).validate_python(FmuResultsSchema.PROD_URL), ) - version: str = Field(default=VERSION) + version: str = Field(default=FmuResultsSchema.VERSION) source: str = Field(default=SOURCE) diff --git a/tests/test_units/test_metadata_class.py b/tests/test_units/test_metadata_class.py index 03232b896..a7e6aee7c 100644 --- a/tests/test_units/test_metadata_class.py +++ b/tests/test_units/test_metadata_class.py @@ -6,12 +6,8 @@ import pytest import fmu.dataio as dio -from fmu.dataio._metadata import ( - SCHEMA, - SOURCE, - VERSION, - generate_export_metadata, -) +from fmu.dataio._definitions import SOURCE, FmuResultsSchema +from fmu.dataio._metadata import generate_export_metadata from fmu.dataio._model import enums from fmu.dataio._model.fields import ( OperatingSystem, @@ -34,8 +30,8 @@ def test_metadata_dollars(edataobj1, regsurf): mymeta = edataobj1.generate_metadata(obj=regsurf) - assert mymeta["version"] == VERSION - assert mymeta["$schema"] == SCHEMA + assert mymeta["version"] == FmuResultsSchema.VERSION + assert mymeta["$schema"] == FmuResultsSchema.PROD_URL assert mymeta["source"] == SOURCE diff --git a/tools/update_schema b/tools/update_schema index 46ba4c762..b1829498e 100755 --- a/tools/update_schema +++ b/tools/update_schema @@ -9,6 +9,7 @@ import sys from pathlib import Path from typing import Any, Final +from fmu.dataio._definitions import FmuResultsSchema from fmu.dataio._model import dump GREEN = "\033[32m" @@ -34,14 +35,14 @@ def _get_parser() -> argparse.ArgumentParser: "-v", type=str, help=f"The version of the schema being output. Default is {SCHEMA_VERSION}", - default=SCHEMA_VERSION, + default=FmuResultsSchema.VERSION, ) parser.add_argument( "--filename", "-f", type=str, help=f"The filename of the schema being output. Default is {SCHEMA_FILENAME}.", - default=SCHEMA_FILENAME, + default=FmuResultsSchema.FILENAME, ) parser.add_argument( "--diff", @@ -75,8 +76,8 @@ def _load_json(filepath: Path) -> dict[str, Any]: return json.load(f) except json.JSONDecodeError as json_decode_error: print( - f"{FAILURE} Parsing existing json schema failed: The json is malformed. " - "If you know why, re-run the command with argument '--force' " + f"{FAILURE} Parsing existing json schema failed: The json is malformed." + " If you know why, re-run the command with argument '--force' " "to overwrite with the new schema.\n" f"{FAILURE} Json parsing error: '{json_decode_error.msg}.'" )