fix some bugs with reading config and merging dictionaries
This commit is contained in:
parent
d1a87b5356
commit
51bf833e1f
2 changed files with 31 additions and 2 deletions
|
@ -42,6 +42,20 @@ def clean_dict(dictionary: Dict) -> Optional[Dict]:
|
||||||
return aux2 if aux2 != {} else None
|
return aux2 if aux2 != {} else None
|
||||||
|
|
||||||
|
|
||||||
|
def recursive_merge_dictionaries(dict1: Dict, dict2: Dict) -> Dict:
|
||||||
|
|
||||||
|
aux1 = {
|
||||||
|
k: v for k, v in dict1.items() if type(v) == dict
|
||||||
|
}
|
||||||
|
aux2 = {
|
||||||
|
k: v for k, v in dict2.items() if type(v) == dict
|
||||||
|
}
|
||||||
|
merged = {
|
||||||
|
k: recursive_merge_dictionaries(v, aux2[k]) for k, v in aux1.items() if k in aux2.keys()
|
||||||
|
}
|
||||||
|
return dict1 | dict2 | merged
|
||||||
|
|
||||||
|
|
||||||
class FormattingConfig(Config):
|
class FormattingConfig(Config):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._naming_scheme: Optional[Union[NamingScheme, str]] = None
|
self._naming_scheme: Optional[Union[NamingScheme, str]] = None
|
||||||
|
@ -73,7 +87,10 @@ class FormattingConfig(Config):
|
||||||
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: Dict):
|
||||||
content = FormattingConfig().to_json() | content # Fill with none values
|
content = recursive_merge_dictionaries(
|
||||||
|
FormattingConfig().to_json(),
|
||||||
|
content
|
||||||
|
)
|
||||||
|
|
||||||
info = content[YAML_INFO]
|
info = content[YAML_INFO]
|
||||||
self._author = info[YAML_AUTHOR]
|
self._author = info[YAML_AUTHOR]
|
||||||
|
|
14
main.py
14
main.py
|
@ -5,7 +5,7 @@ from PyTeX.build.enums import *
|
||||||
from PyTeX.build.paths import RelativePath
|
from PyTeX.build.paths import RelativePath
|
||||||
|
|
||||||
from PyTeX.format.formatting_config import FormattingConfig
|
from PyTeX.format.formatting_config import FormattingConfig
|
||||||
|
from PyTeX.format.generic_text import GenericText
|
||||||
|
|
||||||
spec = BuildDirSpecification(
|
spec = BuildDirSpecification(
|
||||||
source_root=Path('src'),
|
source_root=Path('src'),
|
||||||
|
@ -55,7 +55,19 @@ dump = config.to_json()
|
||||||
|
|
||||||
config.naming_scheme = 'prepend-author'
|
config.naming_scheme = 'prepend-author'
|
||||||
|
|
||||||
|
|
||||||
|
config.extra_header = GenericText(Path('header.txt'))
|
||||||
|
config.extra_header = GenericText(['hello', 'world'])
|
||||||
config.dump_as_yaml(Path('test.yaml'))
|
config.dump_as_yaml(Path('test.yaml'))
|
||||||
|
|
||||||
|
json = config.to_json()
|
||||||
|
|
||||||
|
config2 = FormattingConfig()
|
||||||
|
config2.set_from_json(json)
|
||||||
|
|
||||||
|
|
||||||
|
config3 = FormattingConfig.from_yaml(Path('test.yaml'))
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue