generic properties api

This commit is contained in:
gwen 2012-09-14 11:55:32 +02:00
parent 9604fd15ec
commit 934d011847
2 changed files with 35 additions and 32 deletions

View File

@ -27,9 +27,12 @@ from tiramisu.error import (PropertiesOptionError, ConfigError, NotFoundError,
from tiramisu.option import (OptionDescription, Option, SymLinkOption, from tiramisu.option import (OptionDescription, Option, SymLinkOption,
group_types, Multi, apply_requires) group_types, Multi, apply_requires)
from tiramisu.autolib import carry_out_calculation from tiramisu.autolib import carry_out_calculation
import traceback
# ______________________________________________________________________ # ______________________________________________________________________
# generic owner. 'default' is the general config owner after init time # generic owner. 'default' is the general config owner after init time
default_owner = 'user' default_owner = 'user'
DEBUG = False
# ____________________________________________________________ # ____________________________________________________________
class Config(object): class Config(object):
_cfgimpl_properties = ['hidden', 'disabled'] _cfgimpl_properties = ['hidden', 'disabled']
@ -145,7 +148,7 @@ class Config(object):
if self._cfgimpl_parent != None: if self._cfgimpl_parent != None:
raise MethodCallError("this method root_hide() shall not be" raise MethodCallError("this method root_hide() shall not be"
"used with non-root Config() object") "used with non-root Config() object")
if propname not in in self._cfgimpl_properties: if propname not in self._cfgimpl_properties:
self._cfgimpl_properties.append(propname) self._cfgimpl_properties.append(propname)
def cfgimpl_disable_property(self, propname): def cfgimpl_disable_property(self, propname):
@ -153,7 +156,7 @@ class Config(object):
raise MethodCallError("this method root_hide() shall not be" raise MethodCallError("this method root_hide() shall not be"
"used with non-root Config() object") "used with non-root Config() object")
if self._cfgimpl_has_property(propname): if self._cfgimpl_has_property(propname):
self.properties.remove(propname) self._cfgimpl_properties.remove(propname)
def cfgimpl_non_mandatory(self): def cfgimpl_non_mandatory(self):
if self._cfgimpl_parent != None: if self._cfgimpl_parent != None:
@ -184,21 +187,20 @@ class Config(object):
def _validate(self, name, opt_or_descr): def _validate(self, name, opt_or_descr):
apply_requires(opt_or_descr, self) apply_requires(opt_or_descr, self)
if not type(opt_or_descr) == OptionDescription: if not isinstance(opt_or_descr, Option) and \
if self._cfgimpl_toplevel._cfgimpl_has_properties() and \ not isinstance(opt_or_descr, OptionDescription):
opt_or_descr.has_properties(): if DEBUG:
raise PropertiesOptionError("trying to access" traceback.print_exc()
" to an option named: {0} with properties" raise TypeError('Unexpected object: {0}'.format(repr(opt_or_descr)))
" {1}".format(name, opt_or_descr.properties), properties = opt_or_descr.properties
opt_or_descr.properties) for proper in properties:
if self._cfgimpl_toplevel._cfgimpl_has_properties() and \ if not self._cfgimpl_toplevel._cfgimpl_has_property(proper):
self._cfgimpl_descr.has_properties(): properties.remove(proper)
raise PropertiesOptionError("trying to access" if properties != []:
" to an option's group named: {0}" raise PropertiesOptionError("trying to access"
" for option named: {1} with properties {2}".format( " to an option named: {0} with properties"
self._cfgimpl_descr._name, name, " {1}".format(name, str(properties)),
opt_or_descr.properties), properties)
self._cfgimpl_descr.properties)
def _is_empty(self, opt): def _is_empty(self, opt):
if (not opt.is_multi() and self._cfgimpl_values[opt._name] == None) or \ if (not opt.is_multi() and self._cfgimpl_values[opt._name] == None) or \
@ -217,6 +219,9 @@ class Config(object):
# symlink options # symlink options
if type(opt_or_descr) == SymLinkOption: if type(opt_or_descr) == SymLinkOption:
return getattr(self, opt_or_descr.path) return getattr(self, opt_or_descr.path)
if name not in self._cfgimpl_values:
raise AttributeError("%s object has no attribute %s" %
(self.__class__, name))
self._validate(name, opt_or_descr) self._validate(name, opt_or_descr)
# special attributes # special attributes
if name.startswith('_cfgimpl_'): if name.startswith('_cfgimpl_'):
@ -224,11 +229,11 @@ class Config(object):
return self.__dict__[name] return self.__dict__[name]
raise AttributeError("%s object has no attribute %s" % raise AttributeError("%s object has no attribute %s" %
(self.__class__, name)) (self.__class__, name))
if name not in self._cfgimpl_values:
raise AttributeError("%s object has no attribute %s" %
(self.__class__, name))
if not isinstance(opt_or_descr, OptionDescription): if not isinstance(opt_or_descr, OptionDescription):
# options with callbacks (fill or auto) # options with callbacks (fill or auto)
if name == 'interface_gw':
print "pouet"
print opt_or_descr.has_callback()
if opt_or_descr.has_callback(): if opt_or_descr.has_callback():
value = self._cfgimpl_values[name] value = self._cfgimpl_values[name]
if (not opt_or_descr.is_frozen() or \ if (not opt_or_descr.is_frozen() or \
@ -448,16 +453,16 @@ class Config(object):
# hung up on freeze, hidden and disabled concepts # hung up on freeze, hidden and disabled concepts
self.cfgimpl_freeze() self.cfgimpl_freeze()
rootconfig = self._cfgimpl_get_toplevel() rootconfig = self._cfgimpl_get_toplevel()
rootconfig.cfgimpl_hide() rootconfig.cfgimpl_disable_property('hidden')
rootconfig.cfgimpl_disable() rootconfig.cfgimpl_enable_property('disabled')
rootconfig._cfgimpl_mandatory = True rootconfig._cfgimpl_mandatory = True
def cfgimpl_read_write(self): def cfgimpl_read_write(self):
# hung up on freeze, hidden and disabled concepts # hung up on freeze, hidden and disabled concepts
self.cfgimpl_unfreeze() self.cfgimpl_unfreeze()
rootconfig = self._cfgimpl_get_toplevel() rootconfig = self._cfgimpl_get_toplevel()
rootconfig.cfgimpl_hide() rootconfig.cfgimpl_enable_property('hidden')
rootconfig.cfgimpl_disable() rootconfig.cfgimpl_disable_property('disabled')
rootconfig._cfgimpl_mandatory = False rootconfig._cfgimpl_mandatory = False
# ____________________________________________________________ # ____________________________________________________________
def getkey(self): def getkey(self):

View File

@ -190,11 +190,12 @@ class Option(HiddenBaseType, DisabledBaseType):
def setoption(self, config, value, who): def setoption(self, config, value, who):
"who is **not necessarily** a owner because it cannot be a list" "who is **not necessarily** a owner because it cannot be a list"
name = self._name name = self._name
# the value cannot be changed if a callback is defined
if self.has_callback():
raise TypeError("trying to change an option with callback: %s" % name)
# we want the possibility to reset everything # we want the possibility to reset everything
if not (who == "default" and value is None) and not self.validate(value): if not (who == "default" and value is None) and not self.validate(value):
self.default = None
return
if not self.validate(value):
raise ConfigError('invalid value %s for option %s' % (value, name)) raise ConfigError('invalid value %s for option %s' % (value, name))
if self.is_mandatory(): if self.is_mandatory():
# value shall not be '' for a mandatory option # value shall not be '' for a mandatory option
@ -207,12 +208,9 @@ class Option(HiddenBaseType, DisabledBaseType):
(not self.is_multi() and value is None)): (not self.is_multi() and value is None)):
raise MandatoryError('cannot override value to %s for ' raise MandatoryError('cannot override value to %s for '
'option %s' % (value, name)) 'option %s' % (value, name))
if who == "default" and value is None:
self.default = None
return
if name not in config._cfgimpl_values: if name not in config._cfgimpl_values:
raise AttributeError('unknown option %s' % (name)) raise AttributeError('unknown option %s' % (name))
if config.is_frozen() and (self.has_callback() or self.isfrozen()): if config.is_frozen() and self.isfrozen():
raise ConflictConfigError('cannot override value to %s for ' raise ConflictConfigError('cannot override value to %s for '
'option %s' % (value, name)) 'option %s' % (value, name))
if who == "default": if who == "default":