import os
from configparser import ConfigParser


class Config:
    parser: ConfigParser

    def __init__(self):
        """
        Config parser reading config.ini

        Attributes:
            self.parser: ConfigParser holding list of sections and options
            self.__filename: Path and name to the config file
        """
        self.parser = ConfigParser()

        self.config_file_name = "config.ini"
        self.root_path = os.path.dirname(os.path.abspath(__file__))
        self.config_file = os.path.join(self.root_path, self.config_file_name)

        self._load()

    def _save(self):
        with open(self.config_file, 'w') as outfile:
            self.parser.write(outfile)

    def _load(self):
        self.parser.read(self.config_file)

    def add_section(self, section):
        self._load()
        self.parser.add_section(section)
        self._save()

    def set(self, section: str, option: str, value: str):
        self._load()
        self.parser.set(section, option, value)
        self._save()

    def get(self, section: str, option: str):
        self._load()
        return self.parser.get(section, option)
