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)