diff options
Diffstat (limited to 'Config.py')
-rw-r--r-- | Config.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/Config.py b/Config.py new file mode 100644 index 0000000..47009f2 --- /dev/null +++ b/Config.py @@ -0,0 +1,35 @@ + +import os +import ConfigParser + +__config = None +SECTION = "main" + +class Error(Exception): + """Configuration error""" + def __init__(self, value): + self.value = value + self.str = repr(self.value) + def __str__(self): + return self.str + +def load(filename): + global __config + conf = ConfigParser.ConfigParser() + conf.read(filename) + if not conf.has_section(SECTION): + raise Error, "invalid or missing config file: %s" % filename + __config = conf + +def require(key): + result = option(key) + if not result: + raise Error, "missing conf option '%s' in section '%s'" % (key, SECTION) + return result + +def option(key, default = None): + assert __config is not None, "configuration not loaded" + if not __config.has_option(SECTION, key): + return default + return __config.get(SECTION, key) + |