mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-24 08:13:36 +05:00
feat(ConfigManager): implement own ConfigParser write() method
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -19,7 +19,7 @@ from kiauh.utils.logger import Logger
|
|||||||
class ConfigManager:
|
class ConfigManager:
|
||||||
def __init__(self, cfg_file: str):
|
def __init__(self, cfg_file: str):
|
||||||
self.config_file = cfg_file
|
self.config_file = cfg_file
|
||||||
self.config = configparser.ConfigParser()
|
self.config = CustomConfigParser()
|
||||||
|
|
||||||
def read_config(self) -> None:
|
def read_config(self) -> None:
|
||||||
if not self.config_file:
|
if not self.config_file:
|
||||||
@@ -55,3 +55,27 @@ class ConfigManager:
|
|||||||
|
|
||||||
def set_value(self, section: str, key: str, value: str):
|
def set_value(self, section: str, key: str, value: str):
|
||||||
self.config.set(section, key, value)
|
self.config.set(section, key, value)
|
||||||
|
|
||||||
|
|
||||||
|
class CustomConfigParser(configparser.ConfigParser):
|
||||||
|
"""
|
||||||
|
A custom ConfigParser class overwriting the write() method of configparser.Configparser.
|
||||||
|
Key and value will be delimited by a ": ".
|
||||||
|
Note the whitespace AFTER the colon, which is the whole reason for that overwrite.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def write(self, fp, space_around_delimiters=False):
|
||||||
|
if self._defaults:
|
||||||
|
fp.write("[%s]\n" % configparser.DEFAULTSECT)
|
||||||
|
for key, value in self._defaults.items():
|
||||||
|
fp.write("%s: %s\n" % (key, str(value).replace("\n", "\n\t")))
|
||||||
|
fp.write("\n")
|
||||||
|
for section in self._sections:
|
||||||
|
fp.write("[%s]\n" % section)
|
||||||
|
for key, value in self._sections[section].items():
|
||||||
|
if key == "__name__":
|
||||||
|
continue
|
||||||
|
if (value is not None) or (self._optcre == self.OPTCRE):
|
||||||
|
key = ": ".join((key, str(value).replace("\n", "\n\t")))
|
||||||
|
fp.write("%s\n" % key)
|
||||||
|
fp.write("\n")
|
||||||
|
|||||||
Reference in New Issue
Block a user