non mandatory api

This commit is contained in:
gwen 2012-09-11 15:18:38 +02:00
parent e2ec79063e
commit 8795180867
3 changed files with 50 additions and 15 deletions

View File

@ -32,9 +32,8 @@ class BaseType(object):
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 self.has_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):

View File

@ -169,6 +169,16 @@ class Config(object):
rootconfig = self._cfgimpl_get_toplevel() rootconfig = self._cfgimpl_get_toplevel()
if 'disabled' in rootconfig._cfgimpl_properties: if 'disabled' in rootconfig._cfgimpl_properties:
rootconfig._cfgimpl_properties.remove('disabled') rootconfig._cfgimpl_properties.remove('disabled')
def cfgimpl_non_mandatory(self):
if self._cfgimpl_parent != None:
raise MethodCallError("this method root_mandatory machin() shall not be"
"used with non-root Confit() object")
rootconfig = self._cfgimpl_get_toplevel()
if 'mandatory' in rootconfig._cfgimpl_properties:
rootconfig._cfgimpl_properties.remove('mandatory')
# ____________________________________________________________ # ____________________________________________________________
def __setattr__(self, name, value): def __setattr__(self, name, value):
if '.' in name: if '.' in name:
@ -195,16 +205,25 @@ class Config(object):
if self._cfgimpl_toplevel._cfgimpl_has_properties() and \ if self._cfgimpl_toplevel._cfgimpl_has_properties() and \
opt_or_descr.has_properties(): opt_or_descr.has_properties():
raise PropertiesOptionError("trying to access" raise PropertiesOptionError("trying to access"
" to an option named: {0}".format(name), " to an option named: {0} with properties"
" {1}".format(name, opt_or_descr.properties),
opt_or_descr.properties) opt_or_descr.properties)
if self._cfgimpl_toplevel._cfgimpl_has_properties() and \ if self._cfgimpl_toplevel._cfgimpl_has_properties() and \
self._cfgimpl_descr.has_properties(): self._cfgimpl_descr.has_properties():
raise PropertiesOptionError("trying to access" raise PropertiesOptionError("trying to access"
" to an option's group named: {0}" " to an option's group named: {0}"
" for option named: {1}".format( " for option named: {1} with properties {2}".format(
self._cfgimpl_descr._name, name), self._cfgimpl_descr._name, name,
opt_or_descr.properties),
self._cfgimpl_descr.properties) self._cfgimpl_descr.properties)
def _is_empty(self, opt):
if (not opt.is_multi() and self._cfgimpl_values[opt._name] == None) or \
(opt.is_multi() and (self._cfgimpl_values[opt._name] == [] or \
None in self._cfgimpl_values[name])):
return True
return False
def __getattr__(self, name): def __getattr__(self, name):
# attribute access by passing a path, # attribute access by passing a path,
# for instance getattr(self, "creole.general.family.adresse_ip_eth0") # for instance getattr(self, "creole.general.family.adresse_ip_eth0")
@ -265,8 +284,14 @@ class Config(object):
homeconfig = self._cfgimpl_get_toplevel() homeconfig = self._cfgimpl_get_toplevel()
mandatory = homeconfig._cfgimpl_mandatory mandatory = homeconfig._cfgimpl_mandatory
if opt_or_descr.is_mandatory() and mandatory: if opt_or_descr.is_mandatory() and mandatory:
if self._cfgimpl_values[name] == None\ if name == 'ip_ssh_eth0':
and opt_or_descr.getdefault() == None: print "c'est bien une mandataire2"
print self._cfgimpl_values[name]
print self._is_empty(opt_or_descr)
print type(opt_or_descr)
print opt_or_descr.is_empty_by_default()
if self._is_empty(opt_or_descr) and \
opt_or_descr.is_empty_by_default():
raise MandatoryError("option: {0} is mandatory " raise MandatoryError("option: {0} is mandatory "
"and shall have a value".format(name)) "and shall have a value".format(name))
# frozen and force default # frozen and force default
@ -523,14 +548,15 @@ class Config(object):
for path in self._cfgimpl_descr.getpaths(include_groups=include_groups): for path in self._cfgimpl_descr.getpaths(include_groups=include_groups):
try: try:
value = getattr(self, path) value = getattr(self, path)
except MandatoryError: except MandatoryError:
if mandatory or allpaths: if mandatory or allpaths:
paths.append(path) paths.append(path)
except Exception, e: except PropertiesOptionError:
if allpaths: if allpaths:
paths.append(path) # hidden or disabled or mandatory option added paths.append(path) # hidden or disabled or mandatory option added
else: else:
paths.append(path) paths.append(path)
return paths return paths
def make_dict(config, flatten=False): def make_dict(config, flatten=False):
@ -552,9 +578,11 @@ def make_dict(config, flatten=False):
def mandatory_warnings(config): def mandatory_warnings(config):
mandatory = config._cfgimpl_get_toplevel()._cfgimpl_mandatory mandatory = config._cfgimpl_get_toplevel()._cfgimpl_mandatory
config._cfgimpl_get_toplevel()._cfgimpl_mandatory = True config._cfgimpl_get_toplevel()._cfgimpl_mandatory = True
for path in config.getpaths(mandatory=True): for path in config._cfgimpl_descr.getpaths(include_groups=True):
try: try:
value = getattr(config, path) value = getattr(config, path)
except MandatoryError: except MandatoryError:
yield path yield path
except PropertiesOptionError:
pass
config._cfgimpl_get_toplevel()._cfgimpl_mandatory = mandatory config._cfgimpl_get_toplevel()._cfgimpl_mandatory = mandatory

View File

@ -120,7 +120,7 @@ class Option(HiddenBaseType, DisabledBaseType):
"for option {1}".format(str(default), name)) "for option {1}".format(str(default), name))
self.default = default self.default = default
self.properties = [] # 'hidden', 'disabled'... self.properties = [] # 'hidden', 'disabled'...
def validate(self, value): def validate(self, value):
if self.multi == False: if self.multi == False:
# None allows the reset of the value # None allows the reset of the value
@ -141,6 +141,12 @@ class Option(HiddenBaseType, DisabledBaseType):
def getdefault(self): def getdefault(self):
return self.default return self.default
def is_empty_by_default(self):
if ((not self.is_multi() and self.default == None) or
(self.is_multi() and self.default == []) or None in self.default):
return True
return False
def force_default(self): def force_default(self):
self._force_default_on_freeze = True self._force_default_on_freeze = True
@ -194,9 +200,11 @@ class Option(HiddenBaseType, DisabledBaseType):
if self.is_mandatory(): if self.is_mandatory():
# value shall not be '' for a mandatory option # value shall not be '' for a mandatory option
# so '' is considered as being None # so '' is considered as being None
if value == '': if not self.is_multi() and value == '':
value = None value = None
if config.is_mandatory() and ((self.is_multi() and value == []) or if self.is_multi() and '' in value:
value = Multi([{'': None}.get(i, i) for i in value], config, self)
if config.is_mandatory() and ((self.is_multi() and value == []) or \
(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))
@ -531,8 +539,8 @@ def apply_requires(opt, config):
if action not in available_actions: if action not in available_actions:
raise RequiresError("malformed requirements" raise RequiresError("malformed requirements"
" for option: {0}".format(opt._name)) " for option: {0}".format(opt._name))
# FIXME generic programming opt.property_launch(action, False)
getattr(opt, action)() #.hide() or show() or... getattr(opt, action)() #.hide() or show() or...
# FIXME generic programming opt.property_launch(action, False)
matches = True matches = True
else: # option doesn't exist ! should not happen... else: # option doesn't exist ! should not happen...
raise NotFoundError("required option not found: " raise NotFoundError("required option not found: "