2022-02-05 20:04:52 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
2022-02-05 20:04:52 +01:00
|
|
|
class RelativePath:
|
2022-02-05 18:31:32 +01:00
|
|
|
"""
|
|
|
|
Represents a path that knows of its corresponding root directory
|
|
|
|
"""
|
2022-02-05 21:13:43 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
root_dir: Path,
|
|
|
|
*args,
|
|
|
|
**kwargs
|
|
|
|
):
|
2022-02-05 20:04:52 +01:00
|
|
|
self._path = Path(*args, **kwargs)
|
2022-02-05 21:13:43 +01:00
|
|
|
self._root_dir = root_dir
|
2022-02-05 18:31:32 +01:00
|
|
|
|
2022-02-05 20:04:52 +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)
|
2022-02-05 21:13:43 +01:00
|
|
|
return RelativePath(self._root_dir, path)
|
2022-02-05 20:04:52 +01:00
|
|
|
|
|
|
|
def __rtruediv__(self, other):
|
|
|
|
if isinstance(other, RelativePath):
|
|
|
|
path = self._path.__rtruediv__(other.path)
|
|
|
|
else:
|
|
|
|
path = self._path.__rtruediv__(other)
|
2022-02-05 21:13:43 +01:00
|
|
|
return RelativePath(self._root_dir, path)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self._path.__str__()
|
2022-02-05 20:04:52 +01:00
|
|
|
|
|
|
|
@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
|