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
2 changes: 1 addition & 1 deletion h2integrate/core/inputs/driver_schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ properties:
type: object
properties:
folder_output:
type: string
type: [string, Path]
description: Name of the folder for output files
default: output
create_om_reports:
Expand Down
20 changes: 16 additions & 4 deletions h2integrate/core/inputs/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,37 @@ def convert(value):
return value.item() # convert numpy primitives to python primitive underlying
elif isinstance(value, float | int | bool | str):
return value # this should be the end case
elif isinstance(value, Path):
return str(Path(value).resolve())
else:
return ""

return convert(indict)


def is_path(checker, instance):
return isinstance(instance, Path)


# ---------------------
# See: https://python-jsonschema.readthedocs.io/en/stable/faq/#why-doesn-t-my-schema-s-default-property-set-the-default-on-my-instance
def extend_with_default(validator_class):
validate_properties = validator_class.VALIDATORS["properties"]
type_checker = validator_class.TYPE_CHECKER.redefine("Path", is_path)

def set_defaults(validator, properties, instance, schema):
for prop, subschema in properties.items():
if "default" in subschema:
instance.setdefault(prop, subschema["default"])
if isinstance(instance, dict):
for prop, subschema in properties.items():
if "default" in subschema:
instance.setdefault(prop, subschema["default"])

yield from validate_properties(validator, properties, instance, schema)

return json.validators.extend(validator_class, {"properties": set_defaults})
return json.validators.extend(
validator_class,
{"properties": set_defaults},
type_checker=type_checker,
)


DefaultValidatingDraft7Validator = extend_with_default(json.Draft7Validator)
Expand Down
17 changes: 16 additions & 1 deletion h2integrate/core/test/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
import openmdao.api as om
from attrs import field, define

from h2integrate import ROOT_DIR, EXAMPLE_DIR, RESOURCE_DEFAULT_DIR, load_tech_yaml
from h2integrate import (
ROOT_DIR,
EXAMPLE_DIR,
RESOURCE_DEFAULT_DIR,
load_tech_yaml,
load_driver_yaml,
)
from h2integrate.core.utilities import BaseConfig, build_time_series_from_plant_config
from h2integrate.core.dict_utils import check_inputs, dict_to_yaml_formatting
from h2integrate.core.file_utils import (
Expand All @@ -23,6 +29,15 @@
from h2integrate.core.supported_models import supported_models


@pytest.mark.unit
def test_driver_schema_path_validator(subtests):
driver_config = load_yaml(EXAMPLE_DIR / "16_natural_gas" / "driver_config.yaml")
driver_config["general"]["folder_output"] = EXAMPLE_DIR / "16_natural_gas" / "outputs"
driver_config_validated = load_driver_yaml(driver_config)
with subtests.test("folder output is a path object"):
assert isinstance(driver_config_validated["general"]["folder_output"], Path)


@pytest.fixture(scope="function")
def temp_resource_dir_env():
"""Temporarily set the `RESOURCE_DIR` environment variable to example 11's weather folder."""
Expand Down
Loading