add i/o methods to build dir specification / make it a config

This commit is contained in:
Maximilian Keßler 2022-02-07 17:43:23 +01:00
parent bcae59052c
commit 4557f282ce
8 changed files with 49 additions and 15 deletions

View file

@ -1,2 +1,2 @@
from .build_dir_spec import BuildDirSpecification
from .build_dir_spec import BuildDirConfig
from .builder import PyTeXBuilder

View file

@ -1,8 +1,10 @@
from pathlib import Path, PurePath
from typing import Optional
from typing import Optional, Dict
from .constants import *
from ...format.config import Config, recursive_merge_dictionaries, clean_dict
class BuildDirSpecification:
class BuildDirConfig(Config):
def __init__(
self,
source_root: Optional[Path] = None,
@ -17,6 +19,24 @@ class BuildDirSpecification:
self._tex_source_root: Optional[Path] = tex_root
self._wrapper_dir: Optional[PurePath] = wrapper_dir
def set_from_json(self, content: Dict):
content = self._fill_keys(content)
self._tex_source_root = content[YAML_TEX_SOURCE_ROOT]
self._pytex_source_root = content[YAML_PYTEX_SOURCE_ROOT]
self._build_root = content[YAML_BUILD_ROOT]
self._doc_root = content[YAML_DOC_ROOT]
self._wrapper_dir = content[YAML_WRAPPER_DIR]
def to_json(self) -> Dict:
return {
YAML_WRAPPER_DIR: self._wrapper_dir,
YAML_BUILD_ROOT: self._build_root,
YAML_DOC_ROOT: self._doc_root,
YAML_TEX_SOURCE_ROOT: self._tex_source_root,
YAML_PYTEX_SOURCE_ROOT: self._pytex_source_root
}
@property
def build_root(self) -> Path:
if self._build_root is None:

View file

@ -1,7 +1,7 @@
from typing import Optional
from pathlib import Path
from PyTeX.build.build import BuildDirSpecification
from PyTeX.build.build import BuildDirConfig
from PyTeX.build.enums import PyTeXRootDirType
from ...logger import logger
from .pytex_config import PyTeXConfig
@ -10,7 +10,7 @@ from .pytex_config import PyTeXConfig
class PyTeXBuilder:
def __init__(
self,
build_dir_spec: BuildDirSpecification
build_dir_spec: BuildDirConfig
):
self._build_target_type: Optional[PyTeXRootDirType] = None

View file

@ -0,0 +1,10 @@
YAML_BUILD_ROOT = 'build'
YAML_PYTEX_SOURCE_ROOT = 'source'
YAML_TEX_SOURCE_ROOT = 'tex'
YAML_DOC_ROOT = 'doc'
YAML_RECURSIVE = 'recursive'
YAML_CLEAN_OLD_FILES = 'clean'
YAML_OVERWRITE_FILES = 'overwrite'
YAML_ALLOW_DIRTY_BUILD = 'dirty'
YAML_FORCE_MODE = 'force'
YAML_WRAPPER_DIR = 'wrapper'

View file

@ -1,15 +1,15 @@
from typing import Optional
from PyTeX.build.build import BuildDirSpecification
from PyTeX.build.build import BuildDirConfig
from PyTeX.format.formatting_config import FormattingConfig
class PyTeXConfig:
def __init__(
self,
build_dir_spec: Optional[BuildDirSpecification] = None
build_dir_spec: Optional[BuildDirConfig] = None
):
self._build_dir_specification: Optional[BuildDirSpecification] = build_dir_spec
self._build_dir_specification: Optional[BuildDirConfig] = build_dir_spec
self._default_formatting_config: Optional[FormattingConfig] = None
@ -21,7 +21,7 @@ class PyTeXConfig:
@property
def build_dir_specification(self):
if self._build_dir_specification is None:
return BuildDirSpecification()
return BuildDirConfig()
else:
return self._build_dir_specification

View file

@ -76,6 +76,13 @@ class Config:
else:
pass # TODO
@classmethod
def _fill_keys(cls, dictionary: Dict):
return recursive_merge_dictionaries(
cls().to_json(),
dictionary
)
def recursive_merge_dictionaries(dict1: Dict, dict2: Dict) -> Dict:

View file

@ -59,10 +59,7 @@ class FormattingConfig(Config):
self._tex_flavour: Optional[TeXFlavour] = None
def set_from_json(self, content: Dict):
content = recursive_merge_dictionaries(
FormattingConfig().to_json(),
content
)
content = self._fill_keys(content)
info = content[YAML_INFO]
self._author = info[YAML_AUTHOR]

View file

@ -1,13 +1,13 @@
from pathlib import Path
from PyTeX.build.build import PyTeXBuilder
from PyTeX.build.build.build_dir_spec import BuildDirSpecification
from PyTeX.build.build.build_dir_spec import BuildDirConfig
from PyTeX.build.enums import *
from PyTeX.build.paths import RelativePath
from PyTeX.format.formatting_config import FormattingConfig
from PyTeX.format.generic_text import GenericText
spec = BuildDirSpecification(
spec = BuildDirConfig(
source_root=Path('src'),
tex_root=Path('build/source'),
build_root=Path('build'),