remove some unnecessary dependencies in subclasses.

add PyTeXBuilder class instead of a global config
This commit is contained in:
Maximilian Keßler 2022-02-05 21:13:43 +01:00
parent 4cf09b71a2
commit 1cdb9964e7
7 changed files with 82 additions and 76 deletions

View file

@ -1 +1 @@
from .global_config import GlobalPyTeXConfig
from .global_config import PyTeXBuilder, BuildDirSpecification

View file

@ -5,67 +5,66 @@ from PyTeX.format.formatting_config import FormattingConfig
from ..enums import PyTeXRootDirType
class GlobalPyTeXConfig:
_source_root: Optional[Path] = None
_build_root: Optional[Path] = None
_doc_root: Optional[Path] = None
_tex_root: Optional[Path] = None
_wrapper_dir: Optional[PurePath] = None
_default_formatting_config: Optional[FormattingConfig] = None
_recursive: bool = True
_overwrite_existing_files: bool = False
_clean_old_files: True
_allow_dirty: bool = False
_build_target: PyTeXRootDirType = None
@classmethod
def init(
cls,
class BuildDirSpecification:
def __init__(
self,
source_root: Optional[Path] = None,
build_root: Optional[Path] = None,
doc_root: Optional[Path] = None,
tex_root: Optional[Path] = None,
wrapper_dir: Optional[PurePath] = None
):
cls._source_root = source_root
cls._build_root = build_root
cls._doc_root = doc_root
cls._tex_root = tex_root
cls._wrapper_dir = wrapper_dir
self._source_root: Optional[Path] = source_root
self._build_root: Optional[Path] = build_root
self._doc_root: Optional[Path] = doc_root
self._tex_root: Optional[Path] = tex_root
self._wrapper_dir: Optional[PurePath] = wrapper_dir
@classmethod
def build_root(cls) -> Path:
return cls._build_root
self._build_target_dir_type: PyTeXRootDirType = None
@classmethod
def doc_root(cls) -> Path:
return cls._doc_root
@property
def build_root(self) -> Path:
return self._build_root
@classmethod
def source_root(cls) -> Path:
return cls._source_root
@property
def doc_root(self) -> Path:
return self._doc_root
@classmethod
def tex_root(cls) -> Path:
return cls._tex_root
@property
def source_root(self) -> Path:
return self._source_root
@classmethod
def wrapper_dir(cls) -> PurePath:
@property
def tex_root(self) -> Path:
return self._tex_root
@property
def target_root(self) -> Path:
return {
PyTeXRootDirType.BUILD: self.build_root,
PyTeXRootDirType.DOC: self.doc_root,
PyTeXRootDirType.TEX_SOURCE: self.tex_root,
PyTeXRootDirType.PYTEX_SOURCE: self.source_root
}[self._build_target_dir_type]
@property
def wrapper_dir(self) -> PurePath:
raise NotImplementedError
@classmethod
def build_mode(cls) -> PyTeXRootDirType:
return cls._build_target
@classmethod
def target_root(cls) -> Path:
return {
PyTeXRootDirType.BUILD: cls.build_root(),
PyTeXRootDirType.DOC: cls.doc_root(),
PyTeXRootDirType.TEX_SOURCE: cls.tex_root(),
PyTeXRootDirType.PYTEX_SOURCE: cls.source_root()
}[cls._build_target]
class PyTeXBuilder:
def __init__(
self,
build_dir_spec: BuildDirSpecification
):
self._build_spec: BuildDirSpecification = None
self._default_formatting_config: Optional[FormattingConfig] = None
self._recursive: bool = True
self._overwrite_existing_files: bool = False
self._clean_old_files: True
self._allow_dirty: bool = False

View file

@ -1,6 +1,5 @@
from pathlib import Path
from ..config import GlobalPyTeXConfig
from PyTeX.build.enums.enums import PyTeXRootDirType
@ -8,15 +7,14 @@ class RelativePath:
"""
Represents a path that knows of its corresponding root directory
"""
def __init__(self, pytex_root_dir_type: PyTeXRootDirType, *args, **kwargs):
def __init__(
self,
root_dir: Path,
*args,
**kwargs
):
self._path = Path(*args, **kwargs)
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]
self._root_dir = root_dir
def __getattr__(self, attr):
ret = getattr(self._path, attr)
@ -30,14 +28,17 @@ class RelativePath:
path = self._path.__truediv__(other.path)
else:
path = self._path.__truediv__(other)
return RelativePath(self._pytex_root_dir_type, path)
return RelativePath(self._root_dir, path)
def __rtruediv__(self, other):
if isinstance(other, RelativePath):
path = self._path.__rtruediv__(other.path)
else:
path = self._path.__rtruediv__(other)
return RelativePath(self._pytex_root_dir_type, path)
return RelativePath(self._root_dir, path)
def __str__(self):
return self._path.__str__()
@property
def path(self) -> Path:

View file

@ -1,7 +1,5 @@
from typing import Optional, List, Dict, Tuple
from PyTeX.build.build_info import BasicBuildInfo
from PyTeX.build.config import GlobalPyTeXConfig
from typing import Optional, List, Dict, Tuple, Union
from pathlib import Path
from .enums import PyTeXFileType
from PyTeX.format.formatter import Formatter
from PyTeX.build.paths import RelativePath
@ -12,7 +10,6 @@ class PyTeXSourceFile:
self._pytex_file_type: PyTeXFileType = None
self._relative_path: RelativePath = None
self._formatter: Formatter = None
self._build_info: Optional[BasicBuildInfo] = None
@property
def relative_path(self):
@ -31,9 +28,11 @@ class PyTeXSourceFile:
]
return files
def format(self) -> Optional[List[Tuple[RelativePath, Dict]]]:
def format(self, target_root: Union[Path, RelativePath]) -> Optional[List[Tuple[RelativePath, Dict]]]:
try:
configs = self._formatter.format(GlobalPyTeXConfig.target_root())
configs = self._formatter.format(
target_root.path if isinstance(target_root, RelativePath) else target_root
)
except Exception as e:
raise NotImplementedError
configs = [

0
PyTeX/tmp/__init__.py Normal file
View file

View file

@ -1,5 +1,5 @@
from pathlib import Path, PurePath, PurePosixPath, PureWindowsPath
from PyTeX.build.config.global_config import GlobalPyTeXConfig
from PyTeX.build.config.global_config import PyTeXBuilder
from PyTeX.build.enums.enums import PyTeXRootDirType
import os
@ -19,17 +19,17 @@ class PyTeXPurePath(PurePath):
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()
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 / GlobalPyTeXConfig.wrapper_dir()
self._root_dir / PyTeXBuilder.wrapper_dir()
)
except ValueError as e:
raise NotImplementedError
@ -45,7 +45,7 @@ class PyTeXPurePath(PurePath):
if self._pytex_root_dir_type == PyTeXRootDirType.PYTEX_SOURCE:
return super().__truediv__(other)
else:
return super().__truediv__(GlobalPyTeXConfig.wrapper_dir()).__truediv__(other)
return super().__truediv__(PyTeXBuilder.wrapper_dir()).__truediv__(other)
class PyTeXPurePosixPath(PurePosixPath, PyTeXPurePath):

7
PyTeX/tmp/relpath.py Normal file
View file

@ -0,0 +1,7 @@
self._pytex_root_dir_type: PyTeXRootDirType = pytex_root_dir_type
self._root_dir: Path = {
PyTeXRootDirType.BUILD: build_dir_spec.build_root,
PyTeXRootDirType.PYTEX_SOURCE: build_dir_spec.source_root,
PyTeXRootDirType.DOC: build_dir_spec.doc_root,
PyTeXRootDirType.TEX_SOURCE: build_dir_spec.tex_root
}[self._pytex_root_dir_type]