from pathlib import Path from ..config import GlobalPyTeXConfig from PyTeX.build.enums.enums import PyTeXRootDirType class RelativePath: """ Represents a path that knows of its corresponding root directory """ def __init__(self, pytex_root_dir_type: PyTeXRootDirType, *args, **kwargs): self._path = Path(*args, **kwargs) self._pytex_root_dir_type: PyTeXRootDirType = pytex_root_dir_type self._root_dir: Path = { PyTeXRootDirType.BUILD: GlobalPyTeXConfig.build_root(), PyTeXRootDirType.PYTEX_SOURCE: GlobalPyTeXConfig.source_root(), PyTeXRootDirType.DOC: GlobalPyTeXConfig.doc_root(), PyTeXRootDirType.TEX_SOURCE: GlobalPyTeXConfig.tex_root() }[self._pytex_root_dir_type] 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._pytex_root_dir_type, path) def __rtruediv__(self, other): if isinstance(other, RelativePath): path = self._path.__rtruediv__(other.path) else: path = self._path.__rtruediv__(other) return RelativePath(self._pytex_root_dir_type, path) @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