Skip to content

Commit

Permalink
#365 add support for str in filemodel, add related tests in filemodel…
Browse files Browse the repository at this point in the history
… test
  • Loading branch information
MRVermeulenDeltares committed Mar 16, 2023
1 parent 721d8f0 commit 1652710
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
25 changes: 21 additions & 4 deletions hydrolib/core/basemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Tuple,
Type,
TypeVar,
Union,
)
from weakref import WeakValueDictionary

Expand Down Expand Up @@ -673,16 +674,17 @@ class FileModel(BaseModel, ABC):
# Absolute anchor is used to resolve the save location when the filepath is relative.
_absolute_anchor_path: Path = PrivateAttr(default_factory=Path.cwd)

def __new__(cls, filepath: Optional[Path] = None, *args, **kwargs):
def __new__(cls, filepath: Optional[Union[Path,str]] = None, *args, **kwargs):
"""Create a new model.
If the file at the provided file path was already parsed, this instance is returned.
Args:
filepath (Optional[Path], optional): The file path to the file. Defaults to None.
filepath (Optional[Union[Path,str]], optional): The file path to the file. Defaults to None.
Returns:
FileModel: A file model.
"""
filepath = FileModel._change_to_path(filepath)
with file_load_context() as context:
if (file_model := context.retrieve_model(filepath)) is not None:
return file_model
Expand All @@ -691,7 +693,7 @@ def __new__(cls, filepath: Optional[Path] = None, *args, **kwargs):

def __init__(
self,
filepath: Optional[Path] = None,
filepath: Optional[Union[Path,str]] = None,
resolve_casing: bool = False,
recurse: bool = True,
*args,
Expand All @@ -704,14 +706,16 @@ def __init__(
If the filepath is provided, it is read from disk.
Args:
filepath (Optional[Path], optional): The file path. Defaults to None.
filepath (Optional[Union[Path,str]], optional): The file path. Defaults to None.
resolve_casing (bool, optional): Whether or not to resolve the file name references so that they match the case with what is on disk. Defaults to False.
recurse (bool, optional): Whether or not to recursively load the model. Defaults to True.
"""
if not filepath:
super().__init__(*args, **kwargs)
return

filepath = FileModel._change_to_path(filepath)

with file_load_context() as context:
context.initialize_load_settings(recurse, resolve_casing)

Expand Down Expand Up @@ -1011,6 +1015,19 @@ def _load(self, filepath: Path) -> Dict:

def __str__(self) -> str:
return str(self.filepath if self.filepath else "")

@staticmethod
def _change_to_path(filepath):
if filepath is None:
return filepath
if isinstance(filepath, Path):
return filepath
else:
return Path(filepath)

@validator("filepath")
def _conform_filepath_to_hydrolib_standard(cls, value):
return FileModel._change_to_path(value)


class SerializerConfig(BaseModel, ABC):
Expand Down
43 changes: 43 additions & 0 deletions tests/test_basemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,49 @@ def test_initialize_model_with_resolve_casing_updates_file_references_recursivel
)


@pytest.mark.parametrize(
("given_path", "expected_path"),
[
pytest.param
(
Path("test/path"), Path("test/path")
),
pytest.param
(
"test/path", Path("test/path")
),
pytest.param
(
None,None
),
],
)
def test_setting_filepath(self, given_path, expected_path):
model = FMModel()
model.filepath = given_path
assert model.filepath == expected_path

@pytest.mark.parametrize(
("given_path", "expected_path"),
[
pytest.param
(
f"{test_input_dir}/file_load_test/fm.mdu", Path(f"{test_input_dir}/file_load_test/fm.mdu")
),
pytest.param
(
Path(f"{test_input_dir}/file_load_test/fm.mdu"), Path(f"{test_input_dir}/file_load_test/fm.mdu")
),
pytest.param
(
None,None
),
],
)
def test_constuctor_filepath(self, given_path, expected_path):
model = FMModel(given_path)
assert model.filepath == expected_path

class TestContextManagerFileLoadContext:
def test_context_is_created_and_disposed_properly(self):
assert context_file_loading.get(None) is None
Expand Down

0 comments on commit 1652710

Please sign in to comment.