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
22 changes: 18 additions & 4 deletions geoapps_utils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from geoh5py.groups import UIJsonGroup
from geoh5py.objects import ObjectBase
from geoh5py.shared.utils import stringify
from geoh5py.ui_json import InputFile, monitored_directory_copy
from geoh5py.ui_json import BaseUIJson, InputFile, monitored_directory_copy
from geoh5py.ui_json.utils import fetch_active_workspace
from pydantic import BaseModel, ConfigDict, ValidationError

Expand Down Expand Up @@ -107,7 +107,6 @@ def start(cls, filepath: str | Path | InputFile, mode="r+", **kwargs) -> Self:
:param filepath: Path to valid ui.json file for the application driver.
:param kwargs: Additional keyword arguments for InputFile read_ui_json.
"""

ifile = (
cls.read_ui_json(filepath, **kwargs)
if isinstance(filepath, str | Path)
Expand Down Expand Up @@ -168,7 +167,7 @@ def update_monitoring_directory(
)

@classmethod
def get_default_ui_json(cls) -> Path | None:
def get_default_ui_json_path(cls) -> Path | None:
"""
Get the default ui.json file path for the application.

Expand All @@ -178,6 +177,22 @@ def get_default_ui_json(cls) -> Path | None:
return cls._params_class.default_ui_json
return None

@classmethod
def get_default_ui_json(cls) -> BaseUIJson:
"""
Load the driver's default ui.json template from disk
with no parameters filled in.

:return: The default ui.json configuration.
"""
ui_json_path = cls.get_default_ui_json_path()

if ui_json_path is None or not ui_json_path.exists():
raise ValueError(f"Driver {cls} does not have a default ui.json.")

ui_json = BaseUIJson.read(ui_json_path)
return ui_json


class Options(BaseModel):
"""
Expand Down Expand Up @@ -264,7 +279,6 @@ def build(cls, input_data: InputFile | dict | None = None, **kwargs) -> Self:

data.update(kwargs)
options = cls.collect_input_from_dict(cls, data) # type: ignore

try:
out = cls(**options)
except ValidationError as errors:
Expand Down
16 changes: 13 additions & 3 deletions tests/driver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from geoh5py import Workspace
from geoh5py.groups import UIJsonGroup
from geoh5py.objects import Points
from geoh5py.ui_json import InputFile
from geoh5py.ui_json import BaseUIJson, InputFile
from geoh5py.ui_json.templates import group_parameter, object_parameter

from geoapps_utils.base import Options, get_logger
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_base_driver(tmp_path):

driver = TestParamsDriver(params)

assert TestParamsDriver.get_default_ui_json() is None
assert TestParamsDriver.get_default_ui_json_path() is None

driver.start(tmp_path / "test_ifile.ui.json")

Expand Down Expand Up @@ -120,7 +120,7 @@ def test_base_options(tmp_path):

driver = TestOptionsDriver(options)

assert TestOptionsDriver.get_default_ui_json().exists() # type: ignore
assert TestOptionsDriver.get_default_ui_json_path().exists() # type: ignore

assert isinstance(driver.params, TestOptions)
assert driver.params_class == TestOptions
Expand All @@ -141,6 +141,16 @@ def test_base_options(tmp_path):
assert json_dict.get("client", None) == "{" + str(pts.uid) + "}"


def test_get_empty_ui_json():
# Driver with BaseParams has no default ui.json path
with pytest.raises(ValueError, match="does not have a default"):
TestParamsDriver.get_default_ui_json()

# Driver with Options subclass that has a default_ui_json returns a BaseUIJson
ui_json = TestOptionsDriver.get_default_ui_json()
assert isinstance(ui_json, BaseUIJson)


def test_params_errors():
with pytest.raises(TypeError, match="'input_data' must be "):
BaseParams.build(input_data="bidon") # type: ignore
Expand Down
Loading