api for mandatory

This commit is contained in:
gwen 2012-09-10 11:49:52 +02:00
parent 3e8d16ece6
commit 5907f3e663
3 changed files with 38 additions and 29 deletions

View File

@ -24,32 +24,32 @@
class BaseType(object):
def has_properties(self):
return bool(len(self.properties))
def has_property(self, propname):
return propname in self.properties
def get_properties(self):
return self.properties
def add_property(self, propname):
if not propname in self.properties:
self.properties.append(propname)
def del_property(self, propname):
if not self.has_property(propname):
raise TypeError("option has no property " + propname)
self.properties.remove(propname)
def has_property(self, propname):
return propname in self.properties
def get_properties(self):
return self.properties
def add_property(self, propname):
if not propname in self.properties:
self.properties.append(propname)
def del_property(self, propname):
if not self.has_property(propname):
raise TypeError("option has no property " + propname)
self.properties.remove(propname)
class HiddenBaseType(BaseType):
def hide(self):
self.add_property('hidden')
self.add_property('hidden')
def show(self):
self.del_property('hidden')
self.del_property('hidden')
def _is_hidden(self):
# dangerous method: how an Option() can determine its status by itself ?
return self.has_property('hidden')
return self.has_property('hidden')
class DisabledBaseType(BaseType):
def disable(self):
self.add_property('disabled')
self.add_property('disabled')
def enable(self):
self.del_property('disabled')
self.del_property('disabled')
def _is_disabled(self):
return self.has_property('disabled')
return self.has_property('disabled')

View File

@ -178,9 +178,13 @@ class Config(object):
if name.startswith('_cfgimpl_'):
self.__dict__[name] = value
return
if self._cfgimpl_frozen and getattr(self, name) != value:
if self.is_frozen() and getattr(self, name) != value:
raise TypeError("trying to change a value in a frozen config"
": {0} {1}".format(name, value))
if self.is_mandatory() and value == None:
raise MandatoryError("trying to reset option: {0} wich lives in a"
" mandatory group: {1}".format(name,
self._cfgimpl_descr._name))
if type(getattr(self._cfgimpl_descr, name)) != SymLinkOption:
self._validate(name, getattr(self._cfgimpl_descr, name))
self.setoption(name, value, self._cfgimpl_owner)
@ -329,14 +333,6 @@ class Config(object):
else:
newowner = who
if type(child) != SymLinkOption:
if child.is_mandatory() and value is None:
raise MandatoryError('cannot override value to %s for '
'option %s' % (value, name))
if name not in self._cfgimpl_values:
raise AttributeError('unknown option %s' % (name,))
if child.has_callback() or child.isfrozen():
raise ConflictConfigError('cannot override value to %s for '
'option %s' % (value, name))
# if oldowner == who:
# oldvalue = getattr(self, name)
# if oldvalue == value:
@ -442,6 +438,10 @@ class Config(object):
rootconfig = self._cfgimpl_get_toplevel()
return rootconfig.__dict__['_cfgimpl_frozen']
def is_mandatory(self):
rootconfig = self._cfgimpl_get_toplevel()
return rootconfig.__dict__['_cfgimpl_mandatory']
def cfgimpl_read_only(self):
# hung up on freeze, hidden and disabled concepts
self.cfgimpl_freeze()

View File

@ -184,8 +184,17 @@ class Option(HiddenBaseType, DisabledBaseType):
def setoption(self, config, value, who):
"who is **not necessarily** a owner because it cannot be a list"
name = self._name
if self._frozen:
raise TypeError('trying to change a frozen option object: %s' % name)
if config.is_mandatory() and child.is_mandatory() and \
((self.is_multi() and value == []) or
(not self.is_multi() and value is None)):
raise MandatoryError('cannot override value to %s for '
'option %s' % (value, name))
if name not in config._cfgimpl_values:
raise AttributeError('unknown option %s' % (name))
if config.is_frozen() and (child.has_callback() or child.isfrozen()):
raise ConflictConfigError('cannot override value to %s for '
'option %s' % (value, name))
# we want the possibility to reset everything
if who == "default" and value is None:
self.default = None