Skip to content

Commit

Permalink
feat(FileLocation): set and validate in one (public-ish) method
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Sep 26, 2024
1 parent 2f93b12 commit 8e857d3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
10 changes: 4 additions & 6 deletions pkg-py/src/brand_yaml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,10 @@ def resolve_paths(self):
recurse_dicts_and_models(
self,
pred=lambda value: isinstance(value, FileLocation),
modify=lambda value: value._update_root_dir(path.parent),
)
recurse_dicts_and_models(
self,
pred=lambda value: isinstance(value, FileLocation),
modify=lambda value: value._validate_path_exists(),
modify=lambda value: value.set_root_dir(
path.parent,
validate_path=True,
),
)
return self

Expand Down
19 changes: 11 additions & 8 deletions pkg-py/src/brand_yaml/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,32 @@

class FileLocation(RootModel):
root: HttpUrl | Path
_root_dir: Path
_root_dir: Path | None = None

def __init__(self, path: str | Path | HttpUrl):
super().__init__(path)
self._root_dir = Path(".").absolute()

def __call__(self) -> Path | HttpUrl:
if self._root_dir is None:
return self.root

if isinstance(self.root, Path):
if self.root.is_absolute():
return self.root
return self._root_dir / self.root

return self.root

def _update_root_dir(self, root_dir: Path) -> bool:
def set_root_dir(self, root_dir: Path, validate_path: bool = False) -> None:
self._root_dir = root_dir
return False

def _validate_path_exists(self) -> bool:
if validate_path:
self._validate_path_exists()

def _validate_path_exists(self) -> None:
path = self()
if not path or not isinstance(path, Path):
return False
return

if not path.exists():
raise FileNotFoundError(f"File not found: {path}")

return False
4 changes: 2 additions & 2 deletions pkg-py/src/brand_yaml/_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from pathlib import Path
from typing import Any, Callable, Dict, List
from typing import Any, Callable, Dict, List, Union

from pydantic import BaseModel

Expand Down Expand Up @@ -34,7 +34,7 @@ def find_project_brand_yaml(dir_: Path) -> Path:


PredicateFuncType = Callable[[Any], bool]
ModifyFuncType = Callable[[Any], bool]
ModifyFuncType = Callable[[Any], Union[bool, None]]


def recurse_dicts_and_models(
Expand Down

0 comments on commit 8e857d3

Please sign in to comment.