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 # automatic Option object
special_owners = ['auto', 'fill'] special_owners = ['auto', 'fill']
def special_owner_factory(name, owner, default=None, def special_owner_factory(name, owner, value,
callback=None, config=None): callback, callback_params=None, config=None):
# auto behavior: carries out a calculation if owner == 'fill' and value != None:
if owner == 'auto': return value
return auto_factory(name, callback, config) # in case of an 'auto' and a 'fill' without a value,
# fill behavior: carries out a calculation only if a default value isn't set # we have to carry out a calculation
if owner == 'fill': return calc_factory(name, callback, callback_params, config)
if default == None:
return auto_factory(name, callback, config)
else:
return default
g = globals() 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: try:
#return getattr(autolib, callback)(name, config) #return getattr(autolib, callback)(name, config)
return g[callback](name, config) return g[callback](name, config, **callback_params)
except AttributeError: except AttributeError:
raise SpecialOwnersError("callback: {0} not found for " raise SpecialOwnersError("callback: {0} not found for "
"option: {1}".format(callback, name)) "option: {1}".format(callback, name))

View file

@ -192,9 +192,10 @@ class Config(object):
# special owners # special owners
if owner in special_owners: if owner in special_owners:
return special_owner_factory(name, owner, return special_owner_factory(name, owner,
default=opt_or_descr.getdefault(), value=self._cfgimpl_values[name],
callback=opt_or_descr.getcallback(), callback=opt_or_descr.getcallback(),
config=self) callback_params=opt_or_descr.getcallback_params(),
config=self)
# mandatory options # mandatory options
if not isinstance(opt_or_descr, OptionDescription): if not isinstance(opt_or_descr, OptionDescription):
homeconfig = self._cfgimpl_get_toplevel() 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 #reminder: an Option object is **not** a container for the value
_frozen = False _frozen = False
def __init__(self, name, doc, default=None, requires=None, 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._name = name
self.doc = doc self.doc = doc
self._requires = requires self._requires = requires
self._mandatory = mandatory self._mandatory = mandatory
self.multi = multi self.multi = multi
self.callback = callback 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: if mode not in modes:
raise ConfigError("mode {0} not available".format(mode)) raise ConfigError("mode {0} not available".format(mode))
self.mode = mode self.mode = mode
@ -81,6 +86,9 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
def getcallback(self): def getcallback(self):
return self.callback return self.callback
def getcallback_params(self):
return self.callback_params
def setowner(self, config, who): def setowner(self, config, who):
name = self._name name = self._name
if self._frozen: if self._frozen:
@ -146,9 +154,11 @@ class ChoiceOption(Option):
opt_type = 'string' opt_type = 'string'
def __init__(self, name, doc, values, default=None, requires=None, 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 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) requires=requires, multi=multi, mandatory=mandatory)
def setoption(self, config, value, who): def setoption(self, config, value, who):
@ -161,8 +171,6 @@ class ChoiceOption(Option):
class BoolOption(Option): class BoolOption(Option):
opt_type = 'bool' opt_type = 'bool'
def __init__(self, *args, **kwargs):
super(BoolOption, self).__init__(*args, **kwargs)
# def __init__(self, name, doc, default=None, requires=None, # def __init__(self, name, doc, default=None, requires=None,
# validator=None, multi=False, mandatory=False): # validator=None, multi=False, mandatory=False):
# super(BoolOption, self).__init__(name, doc, default=default, # super(BoolOption, self).__init__(name, doc, default=default,
@ -183,9 +191,6 @@ class BoolOption(Option):
class IntOption(Option): class IntOption(Option):
opt_type = 'int' opt_type = 'int'
def __init__(self, *args, **kwargs):
super(IntOption, self).__init__(*args, **kwargs)
def _validate(self, value): def _validate(self, value):
try: try:
int(value) int(value)
@ -202,9 +207,6 @@ class IntOption(Option):
class FloatOption(Option): class FloatOption(Option):
opt_type = 'float' opt_type = 'float'
def __init__(self, *args, **kwargs):
super(FloatOption, self).__init__(*args, **kwargs)
def _validate(self, value): def _validate(self, value):
try: try:
float(value) float(value)
@ -221,9 +223,6 @@ class FloatOption(Option):
class StrOption(Option): class StrOption(Option):
opt_type = 'string' opt_type = 'string'
def __init__(self, *args, **kwargs):
super(StrOption, self).__init__(*args, **kwargs)
def _validate(self, value): def _validate(self, value):
return isinstance(value, str) return isinstance(value, str)
@ -249,9 +248,6 @@ class SymLinkOption(object): #(HiddenBaseType, DisabledBaseType):
class IPOption(Option): class IPOption(Option):
opt_type = 'ip' opt_type = 'ip'
def __init__(self, *args, **kwargs):
super(IPOption, self).__init__(*args, **kwargs)
def _validate(self, value): def _validate(self, value):
# by now the validation is nothing but a string, use IPy instead # by now the validation is nothing but a string, use IPy instead
return isinstance(value, str) return isinstance(value, str)
@ -265,9 +261,6 @@ class IPOption(Option):
class NetmaskOption(Option): class NetmaskOption(Option):
opt_type = 'netmask' opt_type = 'netmask'
def __init__(self, *args, **kwargs):
super(NetmaskOption, self).__init__(*args, **kwargs)
def _validate(self, value): def _validate(self, value):
# by now the validation is nothing but a string, use IPy instead # by now the validation is nothing but a string, use IPy instead
return isinstance(value, str) return isinstance(value, str)