Skip to content

Commit a2da710

Browse files
authored
Merge pull request #154 from MiraGeoscience/GEOPY-2002
GEOPY-2002: Add version validation and write_default method to update version in …
2 parents 87fbd89 + ebd32b9 commit a2da710

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

simpeg_drivers/uijson.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
2+
# Copyright (c) 2025 Mira Geoscience Ltd. '
3+
# '
4+
# This file is part of simpeg-drivers package. '
5+
# '
6+
# simpeg-drivers is distributed under the terms and conditions of the MIT License '
7+
# (see LICENSE file at the root of this source code package). '
8+
# '
9+
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
10+
11+
import json
12+
import logging
13+
14+
from geoh5py.ui_json.ui_json import BaseUIJson
15+
from pydantic import field_validator
16+
17+
import simpeg_drivers
18+
19+
20+
logger = logging.getLogger(__name__)
21+
22+
23+
class CoreUIJson(BaseUIJson):
24+
version: str = simpeg_drivers.__version__
25+
26+
@field_validator("version", mode="before")
27+
@classmethod
28+
def verify_and_update_version(cls, value: str) -> str:
29+
version = simpeg_drivers.__version__
30+
if value != version:
31+
logger.warning(
32+
"Provided ui.json file version %s does not match the the current"
33+
"simpeg-drivers version %s. This may lead to unpredictable"
34+
"behavior.",
35+
value,
36+
version,
37+
)
38+
return value
39+
40+
@classmethod
41+
def write_default(cls):
42+
"""Write the default UIJson file to disk with updated version."""
43+
44+
with open(cls.default_ui_json, encoding="utf-8") as file:
45+
data = json.load(file)
46+
data["version"] = simpeg_drivers.__version__
47+
48+
uijson = cls.model_construct(**data)
49+
data = uijson.model_dump_json(indent=4)
50+
with open(cls.default_ui_json, "w", encoding="utf-8") as file:
51+
file.write(data)

tests/uijson_test.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
2+
# Copyright (c) 2025 Mira Geoscience Ltd. '
3+
# '
4+
# This file is part of simpeg-drivers package. '
5+
# '
6+
# simpeg-drivers is distributed under the terms and conditions of the MIT License '
7+
# (see LICENSE file at the root of this source code package). '
8+
# '
9+
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
10+
11+
import json
12+
import logging
13+
from pathlib import Path
14+
from typing import ClassVar
15+
16+
from geoh5py import Workspace
17+
18+
import simpeg_drivers
19+
from simpeg_drivers.uijson import CoreUIJson
20+
21+
22+
logger = logging.getLogger(__name__)
23+
24+
25+
def test_version_warning(tmp_path, caplog):
26+
workspace = Workspace(tmp_path / "test.geoh5")
27+
28+
with caplog.at_level(logging.WARNING):
29+
_ = CoreUIJson(
30+
version="0.2.0",
31+
title="My app",
32+
geoh5=str(workspace.h5file),
33+
run_command="myapp.driver",
34+
monitoring_directory="",
35+
conda_environment="my-app",
36+
workspace_geoh5="",
37+
)
38+
39+
40+
def test_write_default(tmp_path):
41+
default_path = tmp_path / "default.ui.json"
42+
data = {
43+
"version": "0.1.0",
44+
"title": "My app",
45+
"geoh5": "",
46+
"run_command": "myapp.driver",
47+
"monitoring_directory": "",
48+
"conda_environment": "my-app",
49+
"workspace_geoh5": "",
50+
}
51+
with open(default_path, "w", encoding="utf-8") as f:
52+
json.dump(data, f, ensure_ascii=False, indent=4)
53+
54+
class MyUIJson(CoreUIJson):
55+
default_ui_json: ClassVar[Path] = default_path
56+
version: str = "0.2.0"
57+
58+
MyUIJson.write_default()
59+
60+
with open(default_path, encoding="utf-8") as f:
61+
data = json.load(f)
62+
63+
assert data["version"] == simpeg_drivers.__version__

0 commit comments

Comments
 (0)