Skip to content

Commit

Permalink
Ensure root is absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeNaccarato committed Aug 2, 2024
1 parent 52661d6 commit 9198bfb
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/boilercore/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class CreatePathsModel(DefaultPathsModel):
@classmethod
def create_directories(cls, data: dict[str, Any]) -> dict[str, Any]:
"""Create directories for directory paths."""
root = data.get("root", cls.model_fields[ROOT].default)
root = get_root(cls, ROOT, data)
for field, value in cls.model_fields.items():
if field == ROOT:
continue
Expand All @@ -206,7 +206,7 @@ def create_directories(cls, data: dict[str, Any]) -> dict[str, Any]:
)
data[field] = apply_to_path_or_paths(
value.default,
partial(make_absolute_and_create, other=root, metadata=metadata),
partial(make_absolute_and_create, root=root, metadata=metadata),
)
return data

Expand Down Expand Up @@ -237,11 +237,16 @@ def get_field_type(field: FieldInfo) -> type:

def get_root(typ: type[DefaultPathsModel], field: str, init_kwargs: dict[str, Any]):
"""Get the root path of a model."""
return (
root = (
root
if (init := init_kwargs.get(field)) and (root := init.get(ROOT))
else typ.model_fields[ROOT].default
)
if not root.is_absolute():
raise ValueError(
f"Root path must be absolute in {typ}, derived from {DefaultPathsModel}."
)
return root


def get_default_paths(
Expand Down Expand Up @@ -280,9 +285,9 @@ def check_pathlike(model: type[BaseModel], field: str, annotation: type | Generi
)


def make_absolute_and_create(path, other: Path, metadata: list[Any]):
def make_absolute_and_create(path, root: Path, metadata: list[Any]):
"""Create directories for directory paths."""
absolute = other / path
absolute = root / path
if PathType("dir") in metadata:
absolute.mkdir(parents=True, exist_ok=True)
return absolute
Expand Down

0 comments on commit 9198bfb

Please sign in to comment.