adjust set_from_json method and make GitVersionInfo a config object

This commit is contained in:
Maximilian Keßler 2022-02-07 21:42:31 +01:00
parent c83f24798e
commit b3eb6f2a90
6 changed files with 31 additions and 13 deletions

View file

@ -20,7 +20,7 @@ class BuildDirConfig(Config):
self._tex_source_root: Optional[Path] = tex_root self._tex_source_root: Optional[Path] = tex_root
self._wrapper_dir: Optional[PurePath] = wrapper_dir self._wrapper_dir: Optional[PurePath] = wrapper_dir
def set_from_json(self, content: Dict): def set_from_json(self, content: Optional[Dict]):
content = self._fill_keys(content) content = self._fill_keys(content)
self._tex_source_root = content[YAML_TEX_SOURCE_ROOT] self._tex_source_root = content[YAML_TEX_SOURCE_ROOT]

View file

@ -38,7 +38,7 @@ class PyTeXConfig(Config):
YAML_CONFIGS: self._configs YAML_CONFIGS: self._configs
} }
def set_from_json(self, content: Dict): def set_from_json(self, content: Optional[Dict]):
content = self._fill_keys(content) content = self._fill_keys(content)
build = content[YAML_BUILD] build = content[YAML_BUILD]

View file

@ -48,7 +48,7 @@ class Config:
path: Dict = yaml.safe_load(config) path: Dict = yaml.safe_load(config)
return cls.from_json(path) return cls.from_json(path)
def set_from_json(self, content: Dict): def set_from_json(self, content: Optional[Dict]):
raise NotImplementedError raise NotImplementedError
def to_json(self) -> Dict: def to_json(self) -> Dict:
@ -77,11 +77,14 @@ class Config:
pass # TODO pass # TODO
@classmethod @classmethod
def _fill_keys(cls, dictionary: Dict): def _fill_keys(cls, dictionary: Optional[Dict]):
return recursive_merge_dictionaries( if dictionary is None:
cls().to_json(), return cls().to_json()
dictionary else:
) return recursive_merge_dictionaries(
cls().to_json(),
dictionary
)
def recursive_merge_dictionaries(dict1: Dict, dict2: Dict) -> Dict: def recursive_merge_dictionaries(dict1: Dict, dict2: Dict) -> Dict:

View file

@ -31,4 +31,4 @@ YAML_TEX_TYPE = 'type'
YAML_TEXT = 'text' YAML_TEXT = 'text'
YAML_REPO = 'repo' YAML_REPO = 'repo'
YAML_PYTEX = 'pytex' YAML_PYTEX = 'pytex'
YAML_DOCSTRIP = 'docstrip' YAML_DOCSTRIP = 'docstrip'

View file

@ -1,18 +1,33 @@
from typing import List, Optional, Dict, Union from typing import List, Optional, Dict, Union
from .config import Config
from .constants import * from .constants import *
from .enums import NamingScheme from .enums import NamingScheme
from .enums import TeXType, TeXFlavour from .enums import TeXType, TeXFlavour
from .generic_text import GenericText from .generic_text import GenericText
from .git_version_info import GitVersionInfo from .git_version_info import GitVersionInfo
from .config import Config
class VersionInfo: class VersionInfo(Config):
def __init__(self): def __init__(self):
self._repo_version: Optional[GitVersionInfo] = None self._repo_version: Optional[GitVersionInfo] = None
self._pytex_version: Optional[GitVersionInfo] = None self._pytex_version: Optional[GitVersionInfo] = None
def set_from_json(self, content: Optional[Dict]):
content = self._fill_keys(content)
self._repo_version = GitVersionInfo.from_json(
content[YAML_REPO]
)
self._pytex_version = GitVersionInfo.from_json(
content[YAML_PYTEX]
)
def to_json(self) -> Dict:
return {
YAML_PYTEX: self._pytex_version.to_json(),
YAML_REPO: self._repo_version.to_json()
}
@property @property
def pytex_version(self) -> Optional[GitVersionInfo]: def pytex_version(self) -> Optional[GitVersionInfo]:
if self._pytex_version is None: if self._pytex_version is None:
@ -58,7 +73,7 @@ class FormattingConfig(Config):
self._tex_type: Optional[TeXType] = None self._tex_type: Optional[TeXType] = None
self._tex_flavour: Optional[TeXFlavour] = None self._tex_flavour: Optional[TeXFlavour] = None
def set_from_json(self, content: Dict): def set_from_json(self, content: Optional[Dict]):
content = self._fill_keys(content) content = self._fill_keys(content)
info = content[YAML_INFO] info = content[YAML_INFO]

View file

@ -12,7 +12,7 @@ class GitVersionInfo(Config):
self._branch: Optional[str] = None self._branch: Optional[str] = None
self._version: Optional[str] = None self._version: Optional[str] = None
def set_from_json(self, content: Dict): def set_from_json(self, content: Optional[Dict]):
content = self._fill_keys(content) content = self._fill_keys(content)
self._branch = content[JSON_BRANCH] self._branch = content[JSON_BRANCH]
self._dirty = content[JSON_DIRTY] self._dirty = content[JSON_DIRTY]