pytex/PyTeX/tmp/pytex_path.py

72 lines
2.3 KiB
Python

import os
from pathlib import Path, PurePath, PurePosixPath, PureWindowsPath
from PyTeX.build.build import PyTeXBuilder
from PyTeX.build.build.enums import PyTeXRootDirType
class PyTeXPurePath:
pass
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)
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()
}[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()
)
except ValueError as e:
raise NotImplementedError
def root_dir_type(self) -> PyTeXRootDirType:
return self._pytex_root_dir_type
@property
def relative_path(self) -> PyTeXPurePath:
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)
class PyTeXPurePosixPath(PurePosixPath, PyTeXPurePath):
pass
class PyTeXPureWindowsPath(PureWindowsPath, PyTeXPurePath):
pass
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)
class PyTeXPosixPath(PyTeXPath, PyTeXPurePosixPath):
pass
class PyTeXWindowsPath(PyTeXPath, PyTeXPureWindowsPath):
pass