import configparser import os FILENAME = 'settings.cfg' class Config(object): def __init__(self): self.last_mtime = 0 self.config = None def __contains__(self, item): return item in self.config def __getitem__(self, key): return self.config[key] def __getattr__(self, name): return getattr(self.config, name) def reload(self): config = configparser.ConfigParser() config.read(FILENAME) self.config = config def reload_if_needed(self): mtime = os.stat(FILENAME).st_mtime if mtime > self.last_mtime: self.last_mtime = mtime self.reload()