pytex/PyTeX/tmp/pytex_path.py

72 lines
2.3 KiB
Python
Raw Normal View History

from pathlib import Path, PurePath, PurePosixPath, PureWindowsPath
2022-02-05 21:44:52 +01:00
from PyTeX.build.build import PyTeXBuilder
2022-02-05 19:36:42 +01:00
from PyTeX.build.enums.enums import PyTeXRootDirType
2022-02-04 21:15:56 +01:00
import os
2022-02-05 18:17:00 +01:00
class PyTeXPurePath:
2022-02-04 21:15:56 +01:00
pass
2022-02-05 18:17:00 +01:00
class PyTeXPurePath(PurePath):
def __new__(cls, pytex_root_dir_type: PyTeXRootDirType, *args):
if cls is PyTeXPurePath:
cls = PyTeXPureWindowsPath if os.name == 'nt' else PyTeXPurePosixPath
self = cls._from_parts(args)
2022-02-04 21:15:56 +01:00
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: PyTeXBuilder.build_root(),
PyTeXRootDirType.PYTEX_SOURCE: PyTeXBuilder.source_root(),
PyTeXRootDirType.DOC: PyTeXBuilder.doc_root(),
PyTeXRootDirType.TEX_SOURCE: PyTeXBuilder.tex_root()
2022-02-04 21:15:56 +01:00
}[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 / PyTeXBuilder.wrapper_dir()
2022-02-04 21:15:56 +01:00
)
2022-02-05 18:17:00 +01:00
except ValueError as e:
2022-02-05 19:05:50 +01:00
raise NotImplementedError
2022-02-04 21:15:56 +01:00
def root_dir_type(self) -> PyTeXRootDirType:
return self._pytex_root_dir_type
@property
2022-02-05 18:17:00 +01:00
def relative_path(self) -> PyTeXPurePath:
2022-02-04 21:15:56 +01:00
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__(PyTeXBuilder.wrapper_dir()).__truediv__(other)
2022-02-04 21:15:56 +01:00
2022-02-05 18:17:00 +01:00
class PyTeXPurePosixPath(PurePosixPath, PyTeXPurePath):
pass
2022-02-04 21:15:56 +01:00
2022-02-05 18:17:00 +01:00
class PyTeXPureWindowsPath(PureWindowsPath, PyTeXPurePath):
pass
2022-02-04 21:15:56 +01:00
2022-02-05 18:17:00 +01:00
class PyTeXPath(Path, PyTeXPurePath):
def __new__(cls, pytex_root_dir_type: PyTeXRootDirType, *args, **kwargs):
if cls is PyTeXPath:
cls = PyTeXWindowsPath if os.name == 'nt' else PyTeXPosixPath
return super().__new__(cls, *args, **kwargs)
2022-02-04 21:15:56 +01:00
2022-02-05 18:17:00 +01:00
class PyTeXPosixPath(PyTeXPath, PyTeXPurePosixPath):
pass
2022-02-04 21:15:56 +01:00
2022-02-05 18:17:00 +01:00
class PyTeXWindowsPath(PyTeXPath, PyTeXPureWindowsPath):
pass