aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBogdano Arendartchuk <bogdano@mandriva.org>2007-05-02 17:39:28 +0000
committerBogdano Arendartchuk <bogdano@mandriva.org>2007-05-02 17:39:28 +0000
commit0b5ebe2e1b3becfd57c15dc3d585ffcd22b47076 (patch)
tree7e434f732783b2398fdb44d01a1a61a9df5ca3d9
parent91700a68924f4b2e97a18f2c3787bd885c36640c (diff)
downloadmgarepo-0b5ebe2e1b3becfd57c15dc3d585ffcd22b47076.tar
mgarepo-0b5ebe2e1b3becfd57c15dc3d585ffcd22b47076.tar.gz
mgarepo-0b5ebe2e1b3becfd57c15dc3d585ffcd22b47076.tar.bz2
mgarepo-0b5ebe2e1b3becfd57c15dc3d585ffcd22b47076.tar.xz
mgarepo-0b5ebe2e1b3becfd57c15dc3d585ffcd22b47076.zip
Merged work on plugins support, including the possibility to wrap
configuration sections.
-rw-r--r--RepSys/ConfigParser.py43
-rw-r--r--RepSys/plugins/__init__.py27
-rw-r--r--RepSys/plugins/sample.py.txt14
-rwxr-xr-xrepsys21
4 files changed, 100 insertions, 5 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)
diff --git a/repsys b/repsys
index bddbf71..b266321 100755
--- a/repsys
+++ b/repsys
@@ -1,5 +1,5 @@
#!/usr/bin/python
-from RepSys import Error
+from RepSys import Error, plugins
from RepSys.command import *
import getopt
import sys
@@ -22,14 +22,32 @@ Useful commands:
Run "repsys COMMAND --help" for more information.
+Run "repsys --help-plugin" for help on loaded plugins.
+
Written by Gustavo Niemeyer <gustavo@niemeyer.net>
"""
+def plugin_help(opt, val, parser, mode):
+ if parser is None:
+ prog = sys.argv[0]
+ print "Use %s --help-plugin <plugin name>" % prog
+ print "Available plugins:"
+ print
+ for name in plugins.list():
+ print name
+ else:
+ print plugins.help(parser)
+ raise SystemExit
+
def parse_options():
parser = OptionParser(help=HELP, version="%prog "+VERSION)
parser.disable_interspersed_args()
parser.add_option("--debug", action="store_true")
+ parser.add_option("--help-plugins", action="callback", callback=plugin_help)
+ parser.add_option("--help-plugin", type="string", dest="__ignore",
+ action="callback", callback=plugin_help)
opts, args = parser.parse_args()
+ del opts.__ignore
if len(args) < 1:
parser.print_help(sys.stderr)
sys.exit(1)
@@ -52,6 +70,7 @@ def dispatch_command(command, argv, debug=0):
command_module.main()
if __name__ == "__main__":
+ plugins.load()
do_command(parse_options, dispatch_command)
# vim:et:ts=4:sw=4