diff --git a/neko_configparser/modules/writer.py b/neko_configparser/modules/writer.py index 677b0d9..109ed11 100644 --- a/neko_configparser/modules/writer.py +++ b/neko_configparser/modules/writer.py @@ -1,5 +1,6 @@ import toml from typing import Dict, Any +from collections import UserDict def _update_dicts(dict1, dict2): @@ -7,13 +8,15 @@ def _update_dicts(dict1, dict2): Ensures that dict1 exists in dict2 or merges them in right way """ for key in dict1: - if key not in dict2 and not isinstance(dict1[key], WriteTomlConfig): + if (key not in dict2) and not isinstance(dict1[key], dict): dict2[key] = dict1[key] - elif isinstance(dict1[key], dict) and isinstance(dict2[key], dict): + elif key not in dict2: + dict2[key] = {} + elif isinstance(dict1[key], dict) and isinstance(dict2[key], WriteTomlConfig): _update_dicts(dict1[key], dict2[key]) -class WriteTomlConfig(dict): +class WriteTomlConfig(UserDict): def __init__(self, filename: str = 'config.neko.toml'): self.__filename = filename self.__parent = None @@ -28,6 +31,8 @@ class WriteTomlConfig(dict): for key, value in data.items(): if isinstance(value, dict): d[key] = cls.__create_subsidiary(value, parent=parent) + else: + d.data[key] = value return d diff --git a/neko_configparser/neko/interfaces.py b/neko_configparser/neko/interfaces.py index 10b77ab..ee03f0e 100644 --- a/neko_configparser/neko/interfaces.py +++ b/neko_configparser/neko/interfaces.py @@ -1,23 +1,33 @@ +import dataclasses from ..modules.toml_parser import TomlConfig from ..modules.writer import WriteTomlConfig from typing import Dict, Any +@dataclasses.dataclass +class __HiddenStorage: + nekomata_folder: str = '' + + +_storage = __HiddenStorage() + + class ConfigParserInterface: @staticmethod - def parse_config(config_path: str = 'config.neko.toml') -> TomlConfig: + def parse_config(config_path: str = None) -> TomlConfig: """ Get toml configuration in our handy representation :param config_path: :return: """ - return TomlConfig(config_path) + return TomlConfig(config_path if config_path + else _storage.nekomata_folder + '/config.neko.toml') @staticmethod def ensure_config( partition: str, module_config: Dict[str, Any], - config_path: str = 'config.neko.toml' + config_path: str = None ) -> None: """ Validates your module config and adds values if needed @@ -27,4 +37,14 @@ class ConfigParserInterface: be added and verified in config :param config_path: Path of config, if you want to use custom """ - WriteTomlConfig(config_path).ensure({partition: module_config}) + WriteTomlConfig( + config_path if config_path + else _storage.nekomata_folder + '/config.neko.toml' + ).ensure({partition: module_config}) + + @staticmethod + def get_nekomata_folder(): + return _storage.nekomata_folder + + +__all__ = [ConfigParserInterface]