start rework of relative paths

This commit is contained in:
Maximilian Keßler 2022-02-04 21:15:56 +01:00
parent 6d158ebea9
commit ada4b6865c
4 changed files with 95 additions and 34 deletions

View file

@ -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)

2
PyTeX/paths/__init__.py Normal file
View file

@ -0,0 +1,2 @@
from .enums import PyTeXRootDirType
from .relative_paths import PyTeXPath

8
PyTeX/paths/enums.py Normal file
View file

@ -0,0 +1,8 @@
from enum import Enum
class PyTeXRootDirType(Enum):
BUILD = 1
PYTEX_SOURCE = 2
DOC = 3
TEX_SOURCE = 4

View file

@ -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