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 from ..enums import PyTeXRootDirType
class GlobalPyTeXConfig: class BuildDirSpecification:
_source_root: Optional[Path] = None def __init__(
_build_root: Optional[Path] = None self,
_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,
source_root: Optional[Path] = None, source_root: Optional[Path] = None,
build_root: Optional[Path] = None, build_root: Optional[Path] = None,
doc_root: Optional[Path] = None, doc_root: Optional[Path] = None,
tex_root: Optional[Path] = None, tex_root: Optional[Path] = None,
wrapper_dir: Optional[PurePath] = None wrapper_dir: Optional[PurePath] = None
): ):
cls._source_root = source_root self._source_root: Optional[Path] = source_root
cls._build_root = build_root self._build_root: Optional[Path] = build_root
cls._doc_root = doc_root self._doc_root: Optional[Path] = doc_root
cls._tex_root = tex_root self._tex_root: Optional[Path] = tex_root
cls._wrapper_dir = wrapper_dir self._wrapper_dir: Optional[PurePath] = wrapper_dir
@classmethod self._build_target_dir_type: PyTeXRootDirType = None
def build_root(cls) -> Path:
return cls._build_root
@classmethod @property
def doc_root(cls) -> Path: def build_root(self) -> Path:
return cls._doc_root return self._build_root
@classmethod @property
def source_root(cls) -> Path: def doc_root(self) -> Path:
return cls._source_root return self._doc_root
@classmethod @property
def tex_root(cls) -> Path: def source_root(self) -> Path:
return cls._tex_root return self._source_root
@classmethod @property
def wrapper_dir(cls) -> PurePath: 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 raise NotImplementedError
@classmethod
def build_mode(cls) -> PyTeXRootDirType:
return cls._build_target
@classmethod class PyTeXBuilder:
def target_root(cls) -> Path: def __init__(
return { self,
PyTeXRootDirType.BUILD: cls.build_root(), build_dir_spec: BuildDirSpecification
PyTeXRootDirType.DOC: cls.doc_root(), ):
PyTeXRootDirType.TEX_SOURCE: cls.tex_root(), self._build_spec: BuildDirSpecification = None
PyTeXRootDirType.PYTEX_SOURCE: cls.source_root()
}[cls._build_target] 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 pathlib import Path
from ..config import GlobalPyTeXConfig
from PyTeX.build.enums.enums import PyTeXRootDirType from PyTeX.build.enums.enums import PyTeXRootDirType
@ -8,15 +7,14 @@ class RelativePath:
""" """
Represents a path that knows of its corresponding root directory 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._path = Path(*args, **kwargs)
self._pytex_root_dir_type: PyTeXRootDirType = pytex_root_dir_type self._root_dir = root_dir
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]
def __getattr__(self, attr): def __getattr__(self, attr):
ret = getattr(self._path, attr) ret = getattr(self._path, attr)
@ -30,14 +28,17 @@ class RelativePath:
path = self._path.__truediv__(other.path) path = self._path.__truediv__(other.path)
else: else:
path = self._path.__truediv__(other) path = self._path.__truediv__(other)
return RelativePath(self._pytex_root_dir_type, path) return RelativePath(self._root_dir, path)
def __rtruediv__(self, other): def __rtruediv__(self, other):
if isinstance(other, RelativePath): if isinstance(other, RelativePath):
path = self._path.__rtruediv__(other.path) path = self._path.__rtruediv__(other.path)
else: else:
path = self._path.__rtruediv__(other) 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 @property
def path(self) -> Path: def path(self) -> Path:

View file

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

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

View file

@ -1,5 +1,5 @@
from pathlib import Path, PurePath, PurePosixPath, PureWindowsPath 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 from PyTeX.build.enums.enums import PyTeXRootDirType
import os import os
@ -19,17 +19,17 @@ class PyTeXPurePath(PurePath):
def __init(self, pytex_root_dir_type: PyTeXRootDirType): def __init(self, pytex_root_dir_type: PyTeXRootDirType):
self._pytex_root_dir_type: PyTeXRootDirType = pytex_root_dir_type self._pytex_root_dir_type: PyTeXRootDirType = pytex_root_dir_type
self._root_dir: Path = { self._root_dir: Path = {
PyTeXRootDirType.BUILD: GlobalPyTeXConfig.build_root(), PyTeXRootDirType.BUILD: PyTeXBuilder.build_root(),
PyTeXRootDirType.PYTEX_SOURCE: GlobalPyTeXConfig.source_root(), PyTeXRootDirType.PYTEX_SOURCE: PyTeXBuilder.source_root(),
PyTeXRootDirType.DOC: GlobalPyTeXConfig.doc_root(), PyTeXRootDirType.DOC: PyTeXBuilder.doc_root(),
PyTeXRootDirType.TEX_SOURCE: GlobalPyTeXConfig.tex_root() PyTeXRootDirType.TEX_SOURCE: PyTeXBuilder.tex_root()
}[self._pytex_root_dir_type] }[self._pytex_root_dir_type]
try: try:
if self._pytex_root_dir_type == PyTeXRootDirType.PYTEX_SOURCE: if self._pytex_root_dir_type == PyTeXRootDirType.PYTEX_SOURCE:
self._relative_path = self.relative_to(self._root_dir) self._relative_path = self.relative_to(self._root_dir)
else: else:
self._relative_path = self.relative_to( self._relative_path = self.relative_to(
self._root_dir / GlobalPyTeXConfig.wrapper_dir() self._root_dir / PyTeXBuilder.wrapper_dir()
) )
except ValueError as e: except ValueError as e:
raise NotImplementedError raise NotImplementedError
@ -45,7 +45,7 @@ class PyTeXPurePath(PurePath):
if self._pytex_root_dir_type == PyTeXRootDirType.PYTEX_SOURCE: if self._pytex_root_dir_type == PyTeXRootDirType.PYTEX_SOURCE:
return super().__truediv__(other) return super().__truediv__(other)
else: else:
return super().__truediv__(GlobalPyTeXConfig.wrapper_dir()).__truediv__(other) return super().__truediv__(PyTeXBuilder.wrapper_dir()).__truediv__(other)
class PyTeXPurePosixPath(PurePosixPath, PyTeXPurePath): 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]