Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions simpeg_drivers/uijson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Copyright (c) 2025 Mira Geoscience Ltd. '
# '
# This file is part of simpeg-drivers package. '
# '
# simpeg-drivers is distributed under the terms and conditions of the MIT License '
# (see LICENSE file at the root of this source code package). '
# '
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import json
import logging

from geoh5py.ui_json.ui_json import BaseUIJson
from pydantic import field_validator

import simpeg_drivers


logger = logging.getLogger(__name__)


class CoreUIJson(BaseUIJson):
version: str = simpeg_drivers.__version__

@field_validator("version", mode="before")
@classmethod
def verify_and_update_version(cls, value: str) -> str:
version = simpeg_drivers.__version__
if value != version:
logger.warning(
"Provided ui.json file version %s does not match the the current"
"simpeg-drivers version %s. This may lead to unpredictable"
"behavior.",
value,
version,
)
return value

@classmethod
def write_default(cls):
"""Write the default UIJson file to disk with updated version."""

with open(cls.default_ui_json, encoding="utf-8") as file:
data = json.load(file)
data["version"] = simpeg_drivers.__version__

uijson = cls.model_construct(**data)
data = uijson.model_dump_json(indent=4)
with open(cls.default_ui_json, "w", encoding="utf-8") as file:
file.write(data)
63 changes: 63 additions & 0 deletions tests/uijson_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Copyright (c) 2025 Mira Geoscience Ltd. '
# '
# This file is part of simpeg-drivers package. '
# '
# simpeg-drivers is distributed under the terms and conditions of the MIT License '
# (see LICENSE file at the root of this source code package). '
# '
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import json
import logging
from pathlib import Path
from typing import ClassVar

from geoh5py import Workspace

import simpeg_drivers
from simpeg_drivers.uijson import CoreUIJson


logger = logging.getLogger(__name__)


def test_version_warning(tmp_path, caplog):
workspace = Workspace(tmp_path / "test.geoh5")

with caplog.at_level(logging.WARNING):
_ = CoreUIJson(
version="0.2.0",
title="My app",
geoh5=str(workspace.h5file),
run_command="myapp.driver",
monitoring_directory="",
conda_environment="my-app",
workspace_geoh5="",
)


def test_write_default(tmp_path):
default_path = tmp_path / "default.ui.json"
data = {
"version": "0.1.0",
"title": "My app",
"geoh5": "",
"run_command": "myapp.driver",
"monitoring_directory": "",
"conda_environment": "my-app",
"workspace_geoh5": "",
}
with open(default_path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)

class MyUIJson(CoreUIJson):
default_ui_json: ClassVar[Path] = default_path
version: str = "0.2.0"

MyUIJson.write_default()

with open(default_path, encoding="utf-8") as f:
data = json.load(f)

assert data["version"] == simpeg_drivers.__version__