move some methods of FormattingConfig to general Config type
This commit is contained in:
parent
225cd19788
commit
2f335e39fa
2 changed files with 73 additions and 51 deletions
|
@ -1,7 +1,35 @@
|
|||
from typing import List, Optional, Dict, Tuple
|
||||
from typing import List, Optional, Dict, Tuple, Union
|
||||
import json
|
||||
import yaml
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def clean_dict(dictionary: Dict) -> Optional[Dict]:
|
||||
aux = {
|
||||
k: clean_dict(v) for k, v in dictionary.items() if type(v) == dict
|
||||
} | {
|
||||
k: v for k, v in dictionary.items() if type(v) != dict
|
||||
}
|
||||
aux2 = {
|
||||
k: v for k, v in aux.items() if v is not 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 Config:
|
||||
def merge_with(self, other, strict: bool = False):
|
||||
"""
|
||||
|
@ -18,6 +46,49 @@ class Config:
|
|||
raise NotImplementedError
|
||||
return self
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, content: Union[Path, Dict]):
|
||||
if isinstance(content, Path):
|
||||
with open(content, 'r') as config:
|
||||
content: Dict = json.load(config)
|
||||
config = cls()
|
||||
config.set_from_json(content)
|
||||
return config
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, path: Path):
|
||||
with open(path, 'r') as config:
|
||||
path: Dict = yaml.safe_load(config)
|
||||
return cls.from_json(path)
|
||||
|
||||
def set_from_json(self, content: Dict):
|
||||
raise NotImplementedError
|
||||
|
||||
def to_json(self) -> Dict:
|
||||
raise NotImplementedError
|
||||
|
||||
def dump_as_yaml(self, filename: Path, clean_none_entries: bool = True):
|
||||
with filename.open('w') as file:
|
||||
if clean_none_entries:
|
||||
simple_dict = clean_dict(self.to_json())
|
||||
else:
|
||||
simple_dict = self.to_json()
|
||||
if simple_dict is not None:
|
||||
yaml.dump(simple_dict, file)
|
||||
else:
|
||||
pass # TODO
|
||||
|
||||
def dump_as_json(self, filename: Path, clean_none_entries: bool = True):
|
||||
with open(filename, 'w') as config:
|
||||
if clean_none_entries:
|
||||
simple_dict = clean_dict(self.to_json())
|
||||
else:
|
||||
simple_dict = self.to_json()
|
||||
if simple_dict is not None:
|
||||
json.dump(simple_dict, config)
|
||||
else:
|
||||
pass # TODO
|
||||
|
||||
|
||||
class FormatterIF:
|
||||
"""
|
||||
|
|
|
@ -4,7 +4,7 @@ import yaml
|
|||
import json
|
||||
from .enums import NamingScheme
|
||||
from .generic_text import GenericText
|
||||
from .formatterif import Config
|
||||
from .formatterif import Config, recursive_merge_dictionaries
|
||||
from .git_version_info import GitVersionInfo
|
||||
from .constants import *
|
||||
from .enums import TeXType, TeXFlavour
|
||||
|
@ -30,32 +30,6 @@ class VersionInfo:
|
|||
return self._repo_version
|
||||
|
||||
|
||||
def clean_dict(dictionary: Dict) -> Optional[Dict]:
|
||||
aux = {
|
||||
k: clean_dict(v) for k, v in dictionary.items() if type(v) == dict
|
||||
} | {
|
||||
k: v for k, v in dictionary.items() if type(v) != dict
|
||||
}
|
||||
aux2 = {
|
||||
k: v for k, v in aux.items() if v is not 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):
|
||||
def __init__(self):
|
||||
self._naming_scheme: Optional[Union[NamingScheme, str]] = None
|
||||
|
@ -133,29 +107,6 @@ class FormattingConfig(Config):
|
|||
self._include_ins = docstrip[YAML_INCLUDE_INS]
|
||||
self._docstrip_guards = docstrip[YAML_DOCSTRIP_GUARDS]
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, content: Path):
|
||||
with open(content, 'r') as config:
|
||||
content: Dict = yaml.safe_load(config)
|
||||
return cls.from_json(content)
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, content: Union[Path, Dict]):
|
||||
if isinstance(content, Path):
|
||||
with open(content, 'r') as config:
|
||||
content: Dict = json.load(config)
|
||||
config = FormattingConfig()
|
||||
config.set_from_json(content)
|
||||
return config
|
||||
|
||||
def dump_as_yaml(self, filename: Path):
|
||||
with filename.open('w') as file:
|
||||
simple_dict = clean_dict(self.to_json())
|
||||
if simple_dict is not None:
|
||||
yaml.dump(simple_dict, file)
|
||||
else:
|
||||
pass # TODO
|
||||
|
||||
def to_json(self) -> Dict:
|
||||
return {
|
||||
YAML_INFO: {
|
||||
|
|
Loading…
Reference in a new issue