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
class PyTeXPurePath:
pass
class PyTeXPurePath(PurePath):
def __new__(cls, *args):
def __new__(cls, pytex_root_dir_type: PyTeXRootDirType, *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 = cls._from_parts(args)
self.__init(pytex_root_dir_type)
return self
@ -40,14 +32,14 @@ class PyTeXPath(Path):
self._relative_path = self.relative_to(
self._root_dir / GlobalPyTeXConfig.wrapper_dir()
)
except ValueError:
except ValueError as e:
raise ValueError # TODO
def root_dir_type(self) -> PyTeXRootDirType:
return self._pytex_root_dir_type
@property
def relative_path(self) -> Path:
def relative_path(self) -> PyTeXPurePath:
return self._relative_path
def __truediv__(self, other):
@ -57,29 +49,24 @@ class PyTeXPath(Path):
return super().__truediv__(GlobalPyTeXConfig.wrapper_dir()).__truediv__(other)
class PyTeXPurePosixPath(PurePosixPath, PyTeXPurePath):
pass
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 PyTeXPureWindowsPath(PureWindowsPath, PyTeXPurePath):
pass
class RelativePath(PureRelativePath, PyTeXPath):
def __init__(self, path: Path, pytex_root_dir: PyTeXRootDirType):
self._absolute_path: Path = path
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)
def absolute_path(self) -> Path:
return self._absolute_path
class PyTeXPosixPath(PyTeXPath, PyTeXPurePosixPath):
pass
class PyTeXWindowsPath(PyTeXPath, PyTeXPureWindowsPath):
pass