pytex/PyTeX/build/build/relative_path.py

70 lines
1.7 KiB
Python
Raw Normal View History

2022-02-09 01:22:25 +01:00
from ctypes import Union
from pathlib import Path
2022-02-05 18:31:32 +01:00
from PyTeX.build.build.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
"""
2022-02-07 18:36:30 +01:00
def __init__(
self,
root_dir: Path,
*args,
**kwargs
):
2022-02-09 01:22:25 +01:00
pass
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
2022-02-08 23:34:04 +01:00
def relative_path(self) -> Path:
2022-02-05 19:38:42 +01:00
try:
return self.relative_to(self._root_dir)
except ValueError as e:
raise NotImplementedError
2022-02-09 01:22:25 +01:00
def with_root(self, root: Path):
return RelativePath(
root,
root / self.relative_path
)