config.py 1.03 KB
Newer Older
1
import configparser
2 3 4
import logging

logger = logging.getLogger(__name__)
5 6


7 8 9
class Config:
    def __init__(self):
        self.default_config()
10

11 12 13 14 15 16
    def default_config(self):
        self.conf = configparser.ConfigParser()
        self.conf.add_section("settings")
        self.conf.set("settings", "volatile_dir", "/tmp")
        self.conf.set("settings", "persistent_dir", "/srv")
        self.conf.set("settings", "plugin_dir", "plugins")
17
        self.conf.set("settings", "cmd_timeout", "10")
18

19
    def load_from_file(self, filename):
20 21 22 23
        try:
            with open(filename, 'r') as f:
                self.conf.read_file(f)
        except FileNotFoundError:
24
            logger.warning("Failed to open config file '{}'".format(filename))
25
        # TODO: handle configparser exceptions
26

27 28 29
    def load_from_dict(self, dictionary):
        self.conf.read_dict(dictionary)

30 31
    def get(self, section, key):
        return self.conf.get(section, key)
32

33 34 35
    def getint(self, section, key):
        return self.conf.getint(section, key)

36

37
config = Config()