config.py 969 Bytes
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_config(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
    def get(self, section, key):
        return self.conf.get(section, key)
29

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

33

34
config = Config()