pytex/PyTeX/build/paths/relative_path.py

61 lines
1.5 KiB
Python
Raw Normal View History

from pathlib import Path
2022-02-05 18:31:32 +01:00
2022-02-05 19:36:42 +01:00
from PyTeX.build.enums.enums import PyTeXRootDirType
2022-02-05 18:31:32 +01:00
class RelativePath:
2022-02-05 18:31:32 +01:00
"""
Represents a path that knows of its corresponding root directory
"""
def __init__(
self,
root_dir: Path,
*args,
**kwargs
):
self._path = Path(*args, **kwargs)
self._root_dir = root_dir
2022-02-05 18:31:32 +01:00
def __getattr__(self, attr):
ret = getattr(self._path, attr)
if isinstance(ret, Path):
return RelativePath(self._pytex_root_dir_type, ret)
else:
return ret
def __truediv__(self, other):
if isinstance(other, RelativePath):
path = self._path.__truediv__(other.path)
else:
path = self._path.__truediv__(other)
return RelativePath(self._root_dir, path)
def __rtruediv__(self, other):
if isinstance(other, RelativePath):
path = self._path.__rtruediv__(other.path)
else:
path = self._path.__rtruediv__(other)
return RelativePath(self._root_dir, path)
def __str__(self):
return self._path.__str__()
@property
def path(self) -> Path:
return self._path
2022-02-05 18:31:32 +01:00
@property
def root_dir_type(self) -> PyTeXRootDirType:
return self._pytex_root_dir_type
@property
def root_dir(self) -> Path:
return self._root_dir
@property
def relative_path(self):
2022-02-05 19:38:42 +01:00
try:
return self.relative_to(self._root_dir)
except ValueError as e:
raise NotImplementedError