rework relative paths

This commit is contained in:
Maximilian Keßler 2022-02-05 18:17:00 +01:00
parent 6278a0df5c
commit ce0cd5794d

View file

@ -5,23 +5,15 @@ from .enums import PyTeXRootDirType
import os import os
class PyTeXPurePath:
pass
class PyTeXPurePath(PurePath): class PyTeXPurePath(PurePath):
def __new__(cls, *args): def __new__(cls, pytex_root_dir_type: PyTeXRootDirType, *args):
if cls is PyTeXPurePath: if cls is PyTeXPurePath:
cls = PyTeXPureWindowsPath if os.name == 'nt' else PyTeXPurePosixPath cls = PyTeXPureWindowsPath if os.name == 'nt' else PyTeXPurePosixPath
self = cls._from_parts(args)
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) self.__init(pytex_root_dir_type)
return self return self
@ -40,14 +32,14 @@ class PyTeXPath(Path):
self._relative_path = self.relative_to( self._relative_path = self.relative_to(
self._root_dir / GlobalPyTeXConfig.wrapper_dir() self._root_dir / GlobalPyTeXConfig.wrapper_dir()
) )
except ValueError: except ValueError as e:
raise ValueError # TODO raise ValueError # TODO
def root_dir_type(self) -> PyTeXRootDirType: def root_dir_type(self) -> PyTeXRootDirType:
return self._pytex_root_dir_type return self._pytex_root_dir_type
@property @property
def relative_path(self) -> Path: def relative_path(self) -> PyTeXPurePath:
return self._relative_path return self._relative_path
def __truediv__(self, other): def __truediv__(self, other):
@ -57,29 +49,24 @@ class PyTeXPath(Path):
return super().__truediv__(GlobalPyTeXConfig.wrapper_dir()).__truediv__(other) return super().__truediv__(GlobalPyTeXConfig.wrapper_dir()).__truediv__(other)
class PyTeXPurePosixPath(PurePosixPath, PyTeXPurePath):
pass
class PureRelativePath: class PyTeXPureWindowsPath(PureWindowsPath, PyTeXPurePath):
def __init__(self, relative_path: Union[str, PurePath]): pass
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): class PyTeXPath(Path, PyTeXPurePath):
def __init__(self, path: Path, pytex_root_dir: PyTeXRootDirType): def __new__(cls, pytex_root_dir_type: PyTeXRootDirType, *args, **kwargs):
self._absolute_path: Path = path if cls is PyTeXPath:
cls = PyTeXWindowsPath if os.name == 'nt' else PyTeXPosixPath
return super().__new__(cls, *args, **kwargs)
def absolute_path(self) -> Path:
return self._absolute_path class PyTeXPosixPath(PyTeXPath, PyTeXPurePosixPath):
pass
class PyTeXWindowsPath(PyTeXPath, PyTeXPureWindowsPath):
pass