diff --git a/geoapps_utils/base.py b/geoapps_utils/base.py index 26515ce..fa899d9 100644 --- a/geoapps_utils/base.py +++ b/geoapps_utils/base.py @@ -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 @@ -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) @@ -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. @@ -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): """ @@ -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: diff --git a/tests/driver_test.py b/tests/driver_test.py index ae19a77..947196b 100644 --- a/tests/driver_test.py +++ b/tests/driver_test.py @@ -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 @@ -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") @@ -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 @@ -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