diff --git a/PyTeX/pathlib/relative_paths.py b/PyTeX/pathlib/relative_paths.py deleted file mode 100644 index 6a425f2..0000000 --- a/PyTeX/pathlib/relative_paths.py +++ /dev/null @@ -1,34 +0,0 @@ -from pathlib import Path -from enum import Enum -from PyTeX.config.global_config import GlobalPyTeXConfig - - -class PyTeXRootDir(Enum): - BUILD = 1 - SOURCE = 2 - DOC = 3 - TEX = 4 - - -class RelativePath: - def __init__(self, path: Path): - self._path: Path = path - - @property - def path(self) -> Path: - return self._path - - def absolute_path(self, pytex_root_dir: PyTeXRootDir) -> Path: - root: Path = { - PyTeXRootDir.BUILD: GlobalPyTeXConfig.build_root(), - PyTeXRootDir.SOURCE: GlobalPyTeXConfig.source_root(), - PyTeXRootDir.DOC: GlobalPyTeXConfig.doc_root(), - PyTeXRootDir.TEX: GlobalPyTeXConfig.tex_root() - }[pytex_root_dir] - return root / self._path - - def __eq__(self, other): - return self.path == other.path - - def __str__(self): - return str(self.path) diff --git a/PyTeX/paths/__init__.py b/PyTeX/paths/__init__.py new file mode 100644 index 0000000..1972148 --- /dev/null +++ b/PyTeX/paths/__init__.py @@ -0,0 +1,2 @@ +from .enums import PyTeXRootDirType +from .relative_paths import PyTeXPath diff --git a/PyTeX/paths/enums.py b/PyTeX/paths/enums.py new file mode 100644 index 0000000..34376c6 --- /dev/null +++ b/PyTeX/paths/enums.py @@ -0,0 +1,8 @@ +from enum import Enum + + +class PyTeXRootDirType(Enum): + BUILD = 1 + PYTEX_SOURCE = 2 + DOC = 3 + TEX_SOURCE = 4 diff --git a/PyTeX/paths/relative_paths.py b/PyTeX/paths/relative_paths.py new file mode 100644 index 0000000..4cc5661 --- /dev/null +++ b/PyTeX/paths/relative_paths.py @@ -0,0 +1,85 @@ +from pathlib import Path, PurePath, PosixPath, PurePosixPath, PureWindowsPath, WindowsPath +from typing import Union +from PyTeX.config.global_config import GlobalPyTeXConfig +from .enums import PyTeXRootDirType +import os + + +class PyTeXPurePath(PurePath): + def __new__(cls, *args): + if cls is PyTeXPurePath: + cls = PyTeXPureWindowsPath if os.name == 'nt' else PyTeXPurePosixPath + + +class PyTeXPurePosixPath(PurePosixPath, PyTeXPurePath): + pass + + +class PyTeXPureWindowsPath(PureWindowsPath, PyTeXPurePath): + pass + + +class PyTeXPath(Path): + def __new__(cls, pytex_root_dir_type: PyTeXRootDirType, *args, **kwargs): + self = super().__new__(cls, *args, **kwargs) + self.__init(pytex_root_dir_type) + return self + + def __init(self, pytex_root_dir_type: PyTeXRootDirType): + 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] + try: + if self._pytex_root_dir_type == PyTeXRootDirType.PYTEX_SOURCE: + self._relative_path = self.relative_to(self._root_dir) + else: + self._relative_path = self.relative_to( + self._root_dir / GlobalPyTeXConfig.wrapper_dir() + ) + except ValueError: + raise ValueError # TODO + + def root_dir_type(self) -> PyTeXRootDirType: + return self._pytex_root_dir_type + + @property + def relative_path(self) -> Path: + return self._relative_path + + def __truediv__(self, other): + if self._pytex_root_dir_type == PyTeXRootDirType.PYTEX_SOURCE: + return super().__truediv__(other) + else: + return super().__truediv__(GlobalPyTeXConfig.wrapper_dir()).__truediv__(other) + + + + +class PureRelativePath: + def __init__(self, relative_path: Union[str, PurePath]): + if type(relative_path) == PurePath: + self._relative_path: PurePath = relative_path + else: + self._relative_path: PurePath = PurePath(relative_path) + + @property + def path(self) -> PurePath: + return self._relative_path + + def __eq__(self, other): + return self.path == other.path + + def __str__(self): + return str(self.path) + + +class RelativePath(PureRelativePath, PyTeXPath): + def __init__(self, path: Path, pytex_root_dir: PyTeXRootDirType): + self._absolute_path: Path = path + + def absolute_path(self) -> Path: + return self._absolute_path