summaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
authorDaniel M. Pelt <D.M.Pelt@cwi.nl>2015-06-24 18:36:03 +0200
committerWillem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl>2015-07-23 11:57:15 +0200
commit11af4b554df9a8a5c31d9dcbc1ea849b32394ba3 (patch)
treef9507f1fe22559d73bfa992c960e8b254b4edcc4 /python
parent18b6d25f7e4f0943b3592f3bb4f6ca5ed9c285d3 (diff)
downloadastra-11af4b554df9a8a5c31d9dcbc1ea849b32394ba3.tar.gz
astra-11af4b554df9a8a5c31d9dcbc1ea849b32394ba3.tar.bz2
astra-11af4b554df9a8a5c31d9dcbc1ea849b32394ba3.tar.xz
astra-11af4b554df9a8a5c31d9dcbc1ea849b32394ba3.zip
Better way of passing options to Python plugin using inspect
Diffstat (limited to 'python')
-rw-r--r--python/astra/plugin.py24
1 files changed, 12 insertions, 12 deletions
diff --git a/python/astra/plugin.py b/python/astra/plugin.py
index ccdb2cb..891f6c9 100644
--- a/python/astra/plugin.py
+++ b/python/astra/plugin.py
@@ -26,22 +26,20 @@
from . import plugin_c as p
from . import log
+import inspect
class base(object):
def astra_init(self, cfg):
try:
- try:
- req = self.required_options
- except AttributeError:
- log.warn("Plugin '" + self.__class__.__name__ + "' does not specify required options")
- req = {}
-
- try:
- opt = self.optional_options
- except AttributeError:
- log.warn("Plugin '" + self.__class__.__name__ + "' does not specify optional options")
- opt = {}
+ args, varargs, varkw, defaults = inspect.getargspec(self.initialize)
+ nopt = len(defaults)
+ if nopt>0:
+ req = args[2:-nopt]
+ opt = args[-nopt:]
+ else:
+ req = args[2:]
+ opt = []
try:
optDict = cfg['options']
@@ -60,7 +58,9 @@ class base(object):
if not cfgKeys.issubset(reqKeys | optKeys):
log.warn(self.__class__.__name__ + ": unused configuration option: " + str(list(cfgKeys.difference(reqKeys | optKeys))))
- self.initialize(cfg)
+ args = [optDict[k] for k in req]
+ kwargs = dict((k,optDict[k]) for k in opt if k in optDict)
+ self.initialize(cfg, *args, **kwargs)
except Exception as e:
log.error(str(e))
raise