diff --git a/PyTeX/format/config.py b/PyTeX/format/config.py new file mode 100644 index 0000000..e1029de --- /dev/null +++ b/PyTeX/format/config.py @@ -0,0 +1,91 @@ +import json +from pathlib import Path +from typing import Dict, Optional, Union + +import yaml + + +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 + + +class Config: + def merge_with(self, other, strict: bool = False): + """ + Merges the other config into this one + :param other: + :param strict: whether conflicting options are allowed or not + :return: self + """ + for var in vars(self): + if not getattr(self, var): + setattr(self, var, getattr(other, var)) + else: + if strict and getattr(other, var) is not None and getattr(self, var) != getattr(other, var): + 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 + + +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 \ No newline at end of file diff --git a/PyTeX/format/formatterif.py b/PyTeX/format/formatterif.py index 0065bd6..bc85302 100644 --- a/PyTeX/format/formatterif.py +++ b/PyTeX/format/formatterif.py @@ -1,93 +1,7 @@ -from typing import List, Optional, Dict, Tuple, Union -import json -import yaml +from typing import List, Optional, Dict, Tuple 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): - """ - Merges the other config into this one - :param other: - :param strict: whether conflicting options are allowed or not - :return: self - """ - for var in vars(self): - if not getattr(self, var): - setattr(self, var, getattr(other, var)) - else: - if strict and getattr(other, var) is not None and getattr(self, var) != getattr(other, var): - 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 +from .config import Config class FormatterIF: diff --git a/PyTeX/format/formatting_config.py b/PyTeX/format/formatting_config.py index 362be27..2888b0f 100644 --- a/PyTeX/format/formatting_config.py +++ b/PyTeX/format/formatting_config.py @@ -1,10 +1,8 @@ -from typing import List, Optional, Union, Dict -from pathlib import Path -import yaml -import json +from typing import List, Optional, Dict + +from .config import Config, recursive_merge_dictionaries from .enums import NamingScheme from .generic_text import GenericText -from .formatterif import Config, recursive_merge_dictionaries from .git_version_info import GitVersionInfo from .constants import * from .enums import TeXType, TeXFlavour