Skip to content

Commit

Permalink
fix(filemodel): ResolveRelativeMode is incorrectly set when reading a…
Browse files Browse the repository at this point in the history
… model with 'pathsRelativeToParent' set to false (#259)

* Add separate relative mode deduction when reading a model

* Add additional documentation to FileModel init

* Fix typo in comment

* Add additional information detailing the data param

* autoformat: isort & black

* Docstring link in documentation only.

Co-authored-by: Arthur van Dam <arthurvd@gmail.com>
  • Loading branch information
BeardedPlatypus and arthurvd committed Jun 27, 2022
1 parent cd83c7c commit 024f58c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
30 changes: 28 additions & 2 deletions hydrolib/core/basemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,13 +546,19 @@ def __init__(self, filepath: Optional[Path] = None, *args, **kwargs):
self._absolute_anchor_path = context.get_current_parent()
loading_path = context.resolve(filepath)

context.push_new_parent(filepath.parent, self._relative_mode)
logger.info(f"Loading data from {filepath}")

data = self._load(loading_path)
data["filepath"] = filepath
kwargs.update(data)

# Note: the relative mode needs to be obtained from the data directly
# because self._relative_mode has not been resolved yet (this is done as
# part of the __init__), however during the __init__ we need to already
# have pushed the new parent. As such we cannot move this call later.
relative_mode = self._get_relative_mode_from_data(data)
context.push_new_parent(filepath.parent, relative_mode)

super().__init__(*args, **kwargs)
self._post_init_load()

Expand Down Expand Up @@ -778,6 +784,26 @@ def _relative_mode(self) -> ResolveRelativeMode:
"""Gets the ResolveRelativeMode of this FileModel.
Returns:
ResolveRelativeMode: The ResolveRelativ
ResolveRelativeMode: The ResolveRelativeMode of this FileModel
"""
return ResolveRelativeMode.ToParent

@classmethod
def _get_relative_mode_from_data(cls, data: Dict[str, Any]) -> ResolveRelativeMode:
"""Gets the ResolveRelativeMode of this FileModel based on the provided data.
Note that by default, data is not used, and FileModels are always relative to
the parent. In exceptional cases, the relative mode can be dependent on the
data (i.e. the unvalidated/parsed dictionary fed into the pydantic basemodel).
As such the data is provided for such classes where the relative mode is
dependent on the state (e.g. the [FMModel][hydrolib.core.io.mdu.models.FMModel]).
Args:
data (Dict[str, Any]):
The unvalidated/parsed data which is fed to the pydantic base model,
used to determine the ResolveRelativeMode.
Returns:
ResolveRelativeMode: The ResolveRelativeMode of this FileModel
"""
return ResolveRelativeMode.ToParent
27 changes: 26 additions & 1 deletion hydrolib/core/io/mdu/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Dict, List, Literal, Optional, Union
from typing import Any, Dict, List, Literal, Optional, Union

from pydantic import Field

Expand Down Expand Up @@ -552,3 +552,28 @@ def _relative_mode(self) -> ResolveRelativeMode:
return ResolveRelativeMode.ToParent
else:
return ResolveRelativeMode.ToAnchor

@classmethod
def _get_relative_mode_from_data(cls, data: Dict[str, Any]) -> ResolveRelativeMode:
"""Gets the ResolveRelativeMode of this FileModel based on the provided data.
The ResolveRelativeMode of the FMModel is determined by the
'pathsRelativeToParent' property of the 'General' category.
Args:
data (Dict[str, Any]):
The unvalidated/parsed data which is fed to the pydantic base model,
used to determine the ResolveRelativeMode.
Returns:
ResolveRelativeMode: The ResolveRelativeMode of this FileModel
"""
if not (general := data.get("general", None)):
return ResolveRelativeMode.ToParent
if not (relative_to_parent := general.get("pathsrelativetoparent", None)):
return ResolveRelativeMode.ToParent

if relative_to_parent == "0":
return ResolveRelativeMode.ToAnchor
else:
return ResolveRelativeMode.ToParent

0 comments on commit 024f58c

Please sign in to comment.