From 5d6060cba09fc484dc304fd67d3405d8f493d019 Mon Sep 17 00:00:00 2001 From: Hellow <74311245+HeIIow2@users.noreply.github.com> Date: Wed, 21 Feb 2024 21:31:14 +0100 Subject: [PATCH] implemented basic functionality --- configparser_toml/__init__.py | 42 +++++++++++++++++++++++++++++---- configparser_toml/exceptions.py | 11 +++++++++ configparser_toml/shared.py | 1 + example.toml | 10 ++++++++ 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 configparser_toml/exceptions.py create mode 100644 configparser_toml/shared.py create mode 100644 example.toml diff --git a/configparser_toml/__init__.py b/configparser_toml/__init__.py index a0ba473..929786c 100644 --- a/configparser_toml/__init__.py +++ b/configparser_toml/__init__.py @@ -1,3 +1,4 @@ +from __future__ import annotations from pathlib import Path import toml @@ -12,16 +13,38 @@ I need a config directory, where I can store all config files. """ -CONFIG_DIR = Path.home() / ".config" / "configparser_toml" +class ConfigItem(dict): + """ + recursive config + """ + def __init__(self, data: dict): + self._data: dict = {} + + self._default = data.get("DEFAULT", {}) + + for key, value in data.items(): + self[key] = value + + def __missing__(self, key): + return self._default.get(key, None) + + def __setitem__(self, key, value): + if isinstance(value, dict): + self._data[key] = ConfigItem(value) + else: + self._data[key] = value -class Config: + +class Config(ConfigItem): def __init__(self, appname: str, profile: str = None, config_dir: str = None): - self._config: dict = {} + self.is_root: bool = is_root + self._config: ConfigItem = ConfigItem({}) self.appname: str = appname self.config_dir: Path = Path(config_dir or platformdirs.user_config_path(appname=appname)) + self.config_file: Path = self.load_profile(profile) @@ -31,13 +54,24 @@ class Config: self.config_dir.mkdir(parents=True, exist_ok=True) self.config_file = self.config_dir / f"{profile}.toml" + + self.read() + return self.config_file def read(self): - pass + _raw = {} + with self.config_file.open("r", encoding=ENCODING) as f: + self._config = ConfigItem(toml.load(f)) def write(self): with self.config_file.open("w", encoding=ENCODING) as f: toml.dump(self._config, f) + + def __setitem__(self, key, value): + self._config.__setitem__(key, value) + + def __getitem__(self, key): + return self._config.__getitem__(key) diff --git a/configparser_toml/exceptions.py b/configparser_toml/exceptions.py new file mode 100644 index 0000000..7b1f403 --- /dev/null +++ b/configparser_toml/exceptions.py @@ -0,0 +1,11 @@ +class ConfigParserError(Exception): + """Base class for exceptions in this module.""" + + def __init__(self, msg=''): + self.message = msg + Exception.__init__(self, msg) + + def __repr__(self): + return self.message + + __str__ = __repr__ \ No newline at end of file diff --git a/configparser_toml/shared.py b/configparser_toml/shared.py new file mode 100644 index 0000000..4f471c2 --- /dev/null +++ b/configparser_toml/shared.py @@ -0,0 +1 @@ +ENCODING = "utf-8" diff --git a/example.toml b/example.toml new file mode 100644 index 0000000..276d6bf --- /dev/null +++ b/example.toml @@ -0,0 +1,10 @@ +[foo_bar.DEFAULT] +msg = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." +number = 3.1416 + +[foo_bar.foo] +msg = "ustry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." +number = 42 + +[foo_bar.bar] +msg = "Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their."