pytex/PyTeX/build/paths/relative_path.py
Maximilian Keßler 1cdb9964e7 remove some unnecessary dependencies in subclasses.
add PyTeXBuilder class instead of a global config
2022-02-05 21:13:43 +01:00

60 lines
1.5 KiB
Python

from pathlib import Path
from PyTeX.build.enums.enums import PyTeXRootDirType
class RelativePath:
"""
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
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
@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):
try:
return self.relative_to(self._root_dir)
except ValueError as e:
raise NotImplementedError