Fix writer and load folder

This commit is contained in:
hhh
2024-02-11 15:41:07 +02:00
parent 90ed21c4a5
commit c4b197346d
2 changed files with 32 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
import toml import toml
from typing import Dict, Any from typing import Dict, Any
from collections import UserDict
def _update_dicts(dict1, dict2): 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 Ensures that dict1 exists in dict2 or merges them in right way
""" """
for key in dict1: 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] 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]) _update_dicts(dict1[key], dict2[key])
class WriteTomlConfig(dict): class WriteTomlConfig(UserDict):
def __init__(self, filename: str = 'config.neko.toml'): def __init__(self, filename: str = 'config.neko.toml'):
self.__filename = filename self.__filename = filename
self.__parent = None self.__parent = None
@@ -28,6 +31,8 @@ class WriteTomlConfig(dict):
for key, value in data.items(): for key, value in data.items():
if isinstance(value, dict): if isinstance(value, dict):
d[key] = cls.__create_subsidiary(value, parent=parent) d[key] = cls.__create_subsidiary(value, parent=parent)
else:
d.data[key] = value
return d return d

View File

@@ -1,23 +1,33 @@
import dataclasses
from ..modules.toml_parser import TomlConfig from ..modules.toml_parser import TomlConfig
from ..modules.writer import WriteTomlConfig from ..modules.writer import WriteTomlConfig
from typing import Dict, Any from typing import Dict, Any
@dataclasses.dataclass
class __HiddenStorage:
nekomata_folder: str = ''
_storage = __HiddenStorage()
class ConfigParserInterface: class ConfigParserInterface:
@staticmethod @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 Get toml configuration in our handy representation
:param config_path: :param config_path:
:return: :return:
""" """
return TomlConfig(config_path) return TomlConfig(config_path if config_path
else _storage.nekomata_folder + '/config.neko.toml')
@staticmethod @staticmethod
def ensure_config( def ensure_config(
partition: str, partition: str,
module_config: Dict[str, Any], module_config: Dict[str, Any],
config_path: str = 'config.neko.toml' config_path: str = None
) -> None: ) -> None:
""" """
Validates your module config and adds values if needed Validates your module config and adds values if needed
@@ -27,4 +37,14 @@ class ConfigParserInterface:
be added and verified in config be added and verified in config
:param config_path: Path of config, if you want to use custom :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]