diff --git a/PyTeX/build/config/__init__.py b/PyTeX/build/config/__init__.py index 3abc0f7..10e4aa3 100644 --- a/PyTeX/build/config/__init__.py +++ b/PyTeX/build/config/__init__.py @@ -1 +1 @@ -from .global_config import GlobalPyTeXConfig +from .global_config import PyTeXBuilder, BuildDirSpecification diff --git a/PyTeX/build/config/global_config.py b/PyTeX/build/config/global_config.py index 2d1f63e..ffb9c5c 100644 --- a/PyTeX/build/config/global_config.py +++ b/PyTeX/build/config/global_config.py @@ -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 + + diff --git a/PyTeX/build/paths/relative_path.py b/PyTeX/build/paths/relative_path.py index c0f9475..e19f14b 100644 --- a/PyTeX/build/paths/relative_path.py +++ b/PyTeX/build/paths/relative_path.py @@ -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: diff --git a/PyTeX/build/pytex_file/pytex_file.py b/PyTeX/build/pytex_file/pytex_file.py index 5d78007..97be3db 100644 --- a/PyTeX/build/pytex_file/pytex_file.py +++ b/PyTeX/build/pytex_file/pytex_file.py @@ -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 = [ diff --git a/PyTeX/tmp/__init__.py b/PyTeX/tmp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/PyTeX/build/paths/pytex_path.py b/PyTeX/tmp/pytex_path.py similarity index 79% rename from PyTeX/build/paths/pytex_path.py rename to PyTeX/tmp/pytex_path.py index 2ee34e2..35a2a51 100644 --- a/PyTeX/build/paths/pytex_path.py +++ b/PyTeX/tmp/pytex_path.py @@ -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): diff --git a/PyTeX/tmp/relpath.py b/PyTeX/tmp/relpath.py new file mode 100644 index 0000000..80e0915 --- /dev/null +++ b/PyTeX/tmp/relpath.py @@ -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]