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): class BaseType(object):
def has_properties(self): def has_properties(self):
return bool(len(self.properties)) return bool(len(self.properties))
def has_property(self, propname): def has_property(self, propname):
return propname in self.properties return propname in self.properties
def get_properties(self): def get_properties(self):
return self.properties return self.properties
def add_property(self, propname): def add_property(self, propname):
if not propname in self.properties: if not propname in self.properties:
self.properties.append(propname) self.properties.append(propname)
def del_property(self, propname): def del_property(self, propname):
if not self.has_property(propname): if not self.has_property(propname):
raise TypeError("option has no property " + propname) raise TypeError("option has no property " + propname)
self.properties.remove(propname) self.properties.remove(propname)
class HiddenBaseType(BaseType): class HiddenBaseType(BaseType):
def hide(self): def hide(self):
self.add_property('hidden') self.add_property('hidden')
def show(self): def show(self):
self.del_property('hidden') self.del_property('hidden')
def _is_hidden(self): def _is_hidden(self):
# dangerous method: how an Option() can determine its status by itself ? # dangerous method: how an Option() can determine its status by itself ?
return self.has_property('hidden') return self.has_property('hidden')
class DisabledBaseType(BaseType): class DisabledBaseType(BaseType):
def disable(self): def disable(self):
self.add_property('disabled') self.add_property('disabled')
def enable(self): def enable(self):
self.del_property('disabled') self.del_property('disabled')
def _is_disabled(self): 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_'): if name.startswith('_cfgimpl_'):
self.__dict__[name] = value self.__dict__[name] = value
return 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" raise TypeError("trying to change a value in a frozen config"
": {0} {1}".format(name, value)) ": {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: if type(getattr(self._cfgimpl_descr, name)) != SymLinkOption:
self._validate(name, getattr(self._cfgimpl_descr, name)) self._validate(name, getattr(self._cfgimpl_descr, name))
self.setoption(name, value, self._cfgimpl_owner) self.setoption(name, value, self._cfgimpl_owner)
@ -329,14 +333,6 @@ class Config(object):
else: else:
newowner = who newowner = who
if type(child) != SymLinkOption: 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: # if oldowner == who:
# oldvalue = getattr(self, name) # oldvalue = getattr(self, name)
# if oldvalue == value: # if oldvalue == value:
@ -442,6 +438,10 @@ class Config(object):
rootconfig = self._cfgimpl_get_toplevel() rootconfig = self._cfgimpl_get_toplevel()
return rootconfig.__dict__['_cfgimpl_frozen'] return rootconfig.__dict__['_cfgimpl_frozen']
def is_mandatory(self):
rootconfig = self._cfgimpl_get_toplevel()
return rootconfig.__dict__['_cfgimpl_mandatory']
def cfgimpl_read_only(self): def cfgimpl_read_only(self):
# hung up on freeze, hidden and disabled concepts # hung up on freeze, hidden and disabled concepts
self.cfgimpl_freeze() self.cfgimpl_freeze()

View file

@ -184,8 +184,17 @@ 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
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 # we want the possibility to reset everything
if who == "default" and value is None: if who == "default" and value is None:
self.default = None self.default = None