-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f404005
commit 2e683fd
Showing
4 changed files
with
94 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,95 @@ | ||
import sys | ||
from pathlib import Path, PosixPath, WindowsPath | ||
from typing import Any | ||
from os import PathLike | ||
from pathlib import Path | ||
from typing import Any, Type, TypeVar, Union | ||
from warnings import warn | ||
|
||
if sys.platform == "win32": | ||
path_type = WindowsPath | ||
else: | ||
path_type = PosixPath | ||
from pydantic_core import core_schema | ||
|
||
PathWrapperT = TypeVar("PathWrapperT", bound="PathWrapper") | ||
|
||
class _DeprecatedResolvedPath(path_type): | ||
"""Class to support the deprecated .resolve() and .raw() methods.""" | ||
|
||
def resolve(self, *args: Any, **kwargs: Any) -> Path: # type: ignore | ||
"""Return absolute path.""" | ||
class PathWrapper(PathLike[str]): | ||
"""Custom path for settings that can be absolute or relative to another setting.""" | ||
|
||
_path: Path | ||
|
||
def __init__(self, path: Union[str, Path, "PathWrapper"]) -> None: | ||
"""Create a new resolved path instance.""" | ||
if isinstance(path, str): | ||
path = Path(path) | ||
elif isinstance(path, PathWrapper): | ||
path = path._path | ||
self._path = path | ||
|
||
def __fspath__(self) -> str: | ||
"""Return the file system path representation.""" | ||
return self._path.__fspath__() | ||
|
||
def __truediv__(self, other: str | PathLike[str]) -> Path: | ||
"""Return a joined path on the basis of `/`.""" | ||
return self._path.__truediv__(other) | ||
|
||
def __str__(self) -> str: | ||
"""Return a string rendering of the resolved path.""" | ||
return self._path.as_posix() | ||
|
||
def __repr__(self) -> str: | ||
"""Return a representation string of the resolved path.""" | ||
return f'{self.__class__.__name__}("{self}")' | ||
|
||
def __eq__(self, other: Any) -> bool: | ||
"""Return true for two PathWrappers with equal paths.""" | ||
if isinstance(other, PathWrapper): | ||
return self._path.__eq__(other._path) | ||
raise TypeError(f"Can't compare {type(other)} with {type(self)}") | ||
|
||
def is_absolute(self) -> bool: | ||
"""True if the underlying path is absolute.""" | ||
return self._path.is_absolute() | ||
|
||
def resolve(self) -> Path: | ||
"""Return the resolved path which is the underlying path.""" | ||
warn("deprecated", DeprecationWarning) | ||
return self._path | ||
|
||
def raw(self) -> Path: | ||
"""Return the raw underlying path without resolving it.""" | ||
warn("deprecated", DeprecationWarning) | ||
return super().resolve(*args, **kwargs) | ||
return self._path | ||
|
||
@classmethod | ||
def __get_pydantic_core_schema__(cls, _source: Type[Any]) -> core_schema.CoreSchema: | ||
"""Set schema to str schema.""" | ||
from_str_schema = core_schema.chain_schema( | ||
[ | ||
core_schema.str_schema(), | ||
core_schema.no_info_plain_validator_function( | ||
cls.validate, | ||
), | ||
] | ||
) | ||
from_anything_schema = core_schema.chain_schema( | ||
[ | ||
core_schema.no_info_plain_validator_function(cls.validate), | ||
core_schema.is_instance_schema(PathWrapper), | ||
] | ||
) | ||
return core_schema.json_or_python_schema( | ||
json_schema=from_str_schema, | ||
python_schema=from_anything_schema, | ||
) | ||
|
||
@classmethod | ||
def validate(cls: type[PathWrapperT], value: Any) -> PathWrapperT: | ||
"""Convert a string value to a Text instance.""" | ||
if isinstance(value, (str, Path, PathWrapper)): | ||
return cls(value) | ||
raise ValueError(f"Cannot parse {type(value)} as {cls.__name__}") | ||
|
||
|
||
class AssetsPath( | ||
_DeprecatedResolvedPath | ||
): # TODO: inherit from path_type instead after removal of deprecated class | ||
class AssetsPath(PathWrapper): | ||
"""Custom path for settings that can be absolute or relative to `assets_dir`.""" | ||
|
||
|
||
class WorkPath( | ||
_DeprecatedResolvedPath | ||
): # TODO: inherit from path_type instead after removal of deprecated class | ||
class WorkPath(PathWrapper): | ||
"""Custom path for settings that can be absolute or relative to `work_dir`.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters