diff --git a/src/config.py b/src/config.py index 56a05f0..7d85462 100644 --- a/src/config.py +++ b/src/config.py @@ -1,11 +1,12 @@ +import configparser import os -from configparser import ConfigParser +from configparser import ConfigParser, DuplicateSectionError class Config: parser: ConfigParser - def __init__(self): + def __init__(self, path: str = None, filename: str = "config.ini"): """ Config parser reading config.ini @@ -14,12 +15,18 @@ class Config: self.__filename: Path and name to the config file """ self.parser = ConfigParser() + self.path = path + self.filename = filename + + if self.path is None: + home_path = os.environ["HOME"] + full_path = os.path.join(home_path, ".config", "brovski-adress-etiketten" ) + else: + full_path = self.path - home_path = os.environ["HOME"] - full_path = os.path.join(home_path, ".config", "brovski-adress-etiketten" ) if not os.path.exists(full_path): os.makedirs(full_path) - self.config_file = os.path.join(full_path, "config.ini") + self.config_file = os.path.join(full_path, self.filename) self._load() @@ -32,11 +39,15 @@ class Config: def add_section(self, section): self._load() - self.parser.add_section(section) + try: + self.parser.add_section(section) + except DuplicateSectionError: + pass self._save() def set(self, section: str, option: str, value: str): self._load() + self.add_section(section) self.parser.set(section, option, value) self._save()