add auto and fill wrapper for eosfuncs

This commit is contained in:
gwen 2012-07-03 17:19:55 +02:00
parent f3a9a96714
commit 3de8a7ed33
3 changed files with 42 additions and 36 deletions

View File

@ -23,24 +23,36 @@
# automatic Option object
special_owners = ['auto', 'fill']
def special_owner_factory(name, owner, default=None,
callback=None, config=None):
# auto behavior: carries out a calculation
if owner == 'auto':
return auto_factory(name, callback, config)
# fill behavior: carries out a calculation only if a default value isn't set
if owner == 'fill':
if default == None:
return auto_factory(name, callback, config)
else:
return default
def special_owner_factory(name, owner, value,
callback, callback_params=None, config=None):
if owner == 'fill' and value != None:
return value
# in case of an 'auto' and a 'fill' without a value,
# we have to carry out a calculation
return calc_factory(name, callback, callback_params, config)
g = globals()
def auto_factory(name, callback, config):
def calc_factory(name, callback, callback_params, config):
# FIXME we have to know the exact status of the config
# not to disrupt it
# config.freeze()
if callback_params is None:
callback_params = {}
for key, value in callback_params.items():
if type(value) == tuple:
path, check_disabled = value
try:
opt_value = getattr(config, path)
except DisabledOptionError, e:
if chek_disabled:
del(callback_params[key])
continue
raise DisabledOptionError(e)
callback_params[key] = opt_value
try:
#return getattr(autolib, callback)(name, config)
return g[callback](name, config)
return g[callback](name, config, **callback_params)
except AttributeError:
raise SpecialOwnersError("callback: {0} not found for "
"option: {1}".format(callback, name))

View File

@ -192,9 +192,10 @@ class Config(object):
# special owners
if owner in special_owners:
return special_owner_factory(name, owner,
default=opt_or_descr.getdefault(),
callback=opt_or_descr.getcallback(),
config=self)
value=self._cfgimpl_values[name],
callback=opt_or_descr.getcallback(),
callback_params=opt_or_descr.getcallback_params(),
config=self)
# mandatory options
if not isinstance(opt_or_descr, OptionDescription):
homeconfig = self._cfgimpl_get_toplevel()

View File

@ -35,13 +35,18 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
#reminder: an Option object is **not** a container for the value
_frozen = False
def __init__(self, name, doc, default=None, requires=None,
mandatory=False, multi=False, callback=None, mode='normal'):
mandatory=False, multi=False, callback=None,
callback_params=None, mode='normal'):
self._name = name
self.doc = doc
self._requires = requires
self._mandatory = mandatory
self.multi = multi
self.callback = callback
if self.callback is None and callback_params is not None:
raise ConfigError("params defined for a callback function but"
" no callback defined yet for option {0}".format(name))
self.callback_params = callback_params
if mode not in modes:
raise ConfigError("mode {0} not available".format(mode))
self.mode = mode
@ -81,6 +86,9 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
def getcallback(self):
return self.callback
def getcallback_params(self):
return self.callback_params
def setowner(self, config, who):
name = self._name
if self._frozen:
@ -146,9 +154,11 @@ class ChoiceOption(Option):
opt_type = 'string'
def __init__(self, name, doc, values, default=None, requires=None,
multi=False, mandatory=False):
callback=None, callback_params=None, multi=False,
mandatory=False):
self.values = values
super(ChoiceOption, self).__init__(name, doc, default=default,
super(ChoiceOption, self).__init__(name, doc, default=default,
callback=callback, callback_params=callback_params,
requires=requires, multi=multi, mandatory=mandatory)
def setoption(self, config, value, who):
@ -161,8 +171,6 @@ class ChoiceOption(Option):
class BoolOption(Option):
opt_type = 'bool'
def __init__(self, *args, **kwargs):
super(BoolOption, self).__init__(*args, **kwargs)
# def __init__(self, name, doc, default=None, requires=None,
# validator=None, multi=False, mandatory=False):
# super(BoolOption, self).__init__(name, doc, default=default,
@ -183,9 +191,6 @@ class BoolOption(Option):
class IntOption(Option):
opt_type = 'int'
def __init__(self, *args, **kwargs):
super(IntOption, self).__init__(*args, **kwargs)
def _validate(self, value):
try:
int(value)
@ -202,9 +207,6 @@ class IntOption(Option):
class FloatOption(Option):
opt_type = 'float'
def __init__(self, *args, **kwargs):
super(FloatOption, self).__init__(*args, **kwargs)
def _validate(self, value):
try:
float(value)
@ -221,9 +223,6 @@ class FloatOption(Option):
class StrOption(Option):
opt_type = 'string'
def __init__(self, *args, **kwargs):
super(StrOption, self).__init__(*args, **kwargs)
def _validate(self, value):
return isinstance(value, str)
@ -249,9 +248,6 @@ class SymLinkOption(object): #(HiddenBaseType, DisabledBaseType):
class IPOption(Option):
opt_type = 'ip'
def __init__(self, *args, **kwargs):
super(IPOption, self).__init__(*args, **kwargs)
def _validate(self, value):
# by now the validation is nothing but a string, use IPy instead
return isinstance(value, str)
@ -265,9 +261,6 @@ class IPOption(Option):
class NetmaskOption(Option):
opt_type = 'netmask'
def __init__(self, *args, **kwargs):
super(NetmaskOption, self).__init__(*args, **kwargs)
def _validate(self, value):
# by now the validation is nothing but a string, use IPy instead
return isinstance(value, str)