diff options
Diffstat (limited to 'RepSys/ConfigParser.py')
-rw-r--r-- | RepSys/ConfigParser.py | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/RepSys/ConfigParser.py b/RepSys/ConfigParser.py index 0f219b9..4dc3e3c 100644 --- a/RepSys/ConfigParser.py +++ b/RepSys/ConfigParser.py @@ -348,6 +348,7 @@ import os class Config: def __init__(self): self._config = ConfigParser() + self._wrapped = {} conffiles = [] conffiles.append("/etc/repsys.conf") repsys_conf = os.environ.get("REPSYS_CONF") @@ -358,6 +359,14 @@ class Config: if os.path.isfile(file): self._config.read(file) + def wrap(self, section, handler, option=None): + """Set one wrapper for a given section + + The wrapper must be a function + f(section, option=None, default=None, walk=False). + """ + self._wrapped[section] = handler + def sections(self): try: return self._config.sections() @@ -373,12 +382,20 @@ class Config: def set(self, section, option, value): return self._config.set(section, option, value) - def walk(self, section): - return self._config.walk(section) - - def get(self, section, option, default=None): + def walk(self, section, option=None, raw=0, vars=None): + handler = self._wrapped.get(section) + if handler: + return handler(section, option, walk=True) + return self._config.walk(section, option, raw, vars) + + def get(self, section, option, default=None, raw=False, wrap=True): + if wrap: + handler = self._wrapped.get(section) + if handler: + handler = self._wrapped.get(section) + return handler(section, option, default) try: - return self._config.get(section, option) + return self._config.get(section, option, raw=raw) except Error: return default @@ -395,4 +412,22 @@ class Config: return states[ret.lower()] return default +def test(): + config = Config() + def handler(section, option=None, default=None, walk=False): + d = {"fulano": "ciclano", + "foolano": "ceeclano"} + if walk: + return d.items() + elif option in d: + return d[option] + else: + return config.get(section, option, default, wrap=False) + config.wrap("users", handler=handler) + print config.get("users", "fulano") # found in wrapper + print config.get("users", "andreas") # found in repsys.conf + print config.walk("users") + +if __name__ == "__main__": + test() # vim:ts=4:sw=4:et |