aboutsummaryrefslogtreecommitdiffstats
path: root/RepSys
diff options
context:
space:
mode:
Diffstat (limited to 'RepSys')
-rw-r--r--RepSys/ConfigParser.py43
-rw-r--r--RepSys/plugins/__init__.py27
-rw-r--r--RepSys/plugins/sample.py.txt14
3 files changed, 80 insertions, 4 deletions
diff --git a/RepSys/ConfigParser.py b/RepSys/ConfigParser.py
index c10b3a7..d995a57 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,10 +382,18 @@ class Config:
def set(self, section, option, value):
return self._config.set(section, option, value)
- def walk(self, section, *args, **kwargs):
- return self._config.walk(section, *args, **kwargs)
-
- 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, 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)
except Error:
@@ -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
diff --git a/RepSys/plugins/__init__.py b/RepSys/plugins/__init__.py
new file mode 100644
index 0000000..8bf4521
--- /dev/null
+++ b/RepSys/plugins/__init__.py
@@ -0,0 +1,27 @@
+import os
+
+loaded = {}
+
+def load():
+ # based on smart's plugin system
+ pluginsdir = os.path.dirname(__file__)
+ for entry in os.listdir(pluginsdir):
+ if entry != "__init__.py" and entry.endswith(".py"):
+ name = entry[:-3]
+ loaded[name] = __import__("RepSys.plugins."+name,
+ fromlist=[name])
+ elif os.path.isdir(entry):
+ initfile = os.path.join(entry, "__init__.py")
+ if os.path.isfile(initfile):
+ loaded[entry] = __import__("RepSys.plugins."+entry,
+ fromlist=[entry])
+
+def list():
+ return loaded.keys()
+
+def help(name):
+ from RepSys import Error
+ try:
+ return loaded[name].__doc__
+ except KeyError:
+ raise Error, "plugin %s not found" % name
diff --git a/RepSys/plugins/sample.py.txt b/RepSys/plugins/sample.py.txt
new file mode 100644
index 0000000..9877f3c
--- /dev/null
+++ b/RepSys/plugins/sample.py.txt
@@ -0,0 +1,14 @@
+# Sample repsys plugin. In order to test it, rename to sample.py
+# vim:ft=python
+from RepSys import config
+
+def users_wrapper(section, option=None, default=None, walk=False):
+ d = {"foolano": "Foolano De Tal <foolano@bla.com>",
+ "ceeclano": "Ceeclano Algumacoisa <ceeclano@bli.com>",
+ "beltrano": "Beltrano Bla <beltrano@mail.ru>"}
+ if walk:
+ return d.items()
+
+ return d.get(option, default)
+
+config.wrap("users", handler=users_wrapper)