suppression of the override
This commit is contained in:
@ -25,11 +25,12 @@ from tiramisu.error import (PropertiesOptionError, ConfigError, NotFoundError,
|
||||
AmbigousOptionError, ConflictConfigError, NoMatchingOptionFound,
|
||||
MandatoryError, MethodCallError, NoValueReturned)
|
||||
from tiramisu.option import (OptionDescription, Option, SymLinkOption,
|
||||
group_types, Multi, apply_requires)
|
||||
group_types, Multi, apply_requires, Owner)
|
||||
|
||||
# ______________________________________________________________________
|
||||
# generic owner. 'default' is the general config owner after init time
|
||||
default_owner = 'user'
|
||||
|
||||
# ____________________________________________________________
|
||||
class Config(object):
|
||||
"properties attribute: the name of a property enables this property"
|
||||
@ -41,12 +42,10 @@ class Config(object):
|
||||
_cfgimpl_owner = default_owner
|
||||
_cfgimpl_toplevel = None
|
||||
|
||||
def __init__(self, descr, parent=None, **overrides):
|
||||
def __init__(self, descr, parent=None):
|
||||
""" Configuration option management master class
|
||||
:param descr: describes the configuration schema
|
||||
:type descr: an instance of ``option.OptionDescription``
|
||||
:param overrides: can be used to set different default values
|
||||
(see method ``override``)
|
||||
:param parent: is None if the ``Config`` is root parent Config otherwise
|
||||
:type parent: ``Config``
|
||||
"""
|
||||
@ -61,7 +60,7 @@ class Config(object):
|
||||
self._cfgimpl_toplevel = self._cfgimpl_get_toplevel()
|
||||
'`freeze()` allows us to carry out this calculation again if necessary'
|
||||
self._cfgimpl_frozen = self._cfgimpl_toplevel._cfgimpl_frozen
|
||||
self._cfgimpl_build(overrides)
|
||||
self._cfgimpl_build()
|
||||
|
||||
def _validate_duplicates(self, children):
|
||||
"""duplicates Option names in the schema
|
||||
@ -75,11 +74,10 @@ class Config(object):
|
||||
raise ConflictConfigError('duplicate option name: '
|
||||
'{0}'.format(dup._name))
|
||||
|
||||
def _cfgimpl_build(self, overrides):
|
||||
def _cfgimpl_build(self):
|
||||
"""
|
||||
- builds the config object from the schema
|
||||
- settles various default values for options
|
||||
:param overrides: dict of options name:default values
|
||||
"""
|
||||
self._validate_duplicates(self._cfgimpl_descr._children)
|
||||
for child in self._cfgimpl_descr._children:
|
||||
@ -99,7 +97,7 @@ class Config(object):
|
||||
elif isinstance(child, OptionDescription):
|
||||
self._validate_duplicates(child._children)
|
||||
self._cfgimpl_values[child._name] = Config(child, parent=self)
|
||||
self.override(overrides)
|
||||
# self.override(overrides)
|
||||
|
||||
def cfgimpl_set_permissive(self, permissive):
|
||||
if not isinstance(permissive, list):
|
||||
@ -126,14 +124,14 @@ class Config(object):
|
||||
if child._name not in self._cfgimpl_values:
|
||||
self._cfgimpl_values[child._name] = Config(child, parent=self)
|
||||
|
||||
def override(self, overrides):
|
||||
"""
|
||||
overrides default values. This marks the overridden values as defaults.
|
||||
:param overrides: is a dictionary of path strings to values.
|
||||
"""
|
||||
for name, value in overrides.iteritems():
|
||||
homeconfig, name = self._cfgimpl_get_home_by_path(name)
|
||||
homeconfig.setoption(name, value, 'default')
|
||||
# def override(self, overrides):
|
||||
# """
|
||||
# overrides default values. This marks the overridden values as defaults.
|
||||
# :param overrides: is a dictionary of path strings to values.
|
||||
# """
|
||||
# for name, value in overrides.iteritems():
|
||||
# homeconfig, name = self._cfgimpl_get_home_by_path(name)
|
||||
# homeconfig.setoption(name, value, 'default')
|
||||
|
||||
def cfgimpl_set_owner(self, owner):
|
||||
":param owner: sets the default value for owner at the Config level"
|
||||
@ -369,35 +367,22 @@ class Config(object):
|
||||
:type who: string
|
||||
"""
|
||||
child = getattr(self._cfgimpl_descr, name)
|
||||
if who == None:
|
||||
if child.is_multi():
|
||||
newowner = [self._cfgimpl_owner for i in range(len(value))]
|
||||
else:
|
||||
newowner = self._cfgimpl_owner
|
||||
else:
|
||||
if type(child) != SymLinkOption:
|
||||
if child.is_multi():
|
||||
if type(value) != Multi:
|
||||
if type(value) == list:
|
||||
value = Multi(value, self, child)
|
||||
else:
|
||||
raise ConfigError("invalid value for option:"
|
||||
" {0} that is set to multi".format(name))
|
||||
newowner = [who for i in range(len(value))]
|
||||
else:
|
||||
newowner = who
|
||||
if type(child) != SymLinkOption:
|
||||
#if child.has_callback() and who=='default':
|
||||
# raise TypeError("trying to set a default value to an option "
|
||||
# "which has a callback: {0}".format(name))
|
||||
if who == None:
|
||||
who = self._cfgimpl_owner
|
||||
if child.is_multi():
|
||||
if type(value) != Multi:
|
||||
if type(value) == list:
|
||||
value = Multi(value, self, child)
|
||||
else:
|
||||
raise ConfigError("invalid value for option:"
|
||||
" {0} that is set to multi".format(name))
|
||||
newowner = [who for i in range(len(value))]
|
||||
else:
|
||||
newowner = who
|
||||
child.setoption(self, value, who)
|
||||
if (value is None and who != 'default' and not child.is_multi()):
|
||||
child.setowner(self, 'default')
|
||||
self._cfgimpl_values[name] = copy(child.getdefault())
|
||||
elif (value == [] and who != 'default' and child.is_multi()):
|
||||
child.setowner(self, ['default' for i in range(len(child.getdefault()))])
|
||||
self._cfgimpl_values[name] = Multi(copy(child.getdefault()),
|
||||
config=self, child=child)
|
||||
if child.is_multi() and value == [] and who != 'default':
|
||||
child.setowner(self, Owner(who))
|
||||
else:
|
||||
child.setowner(self, newowner)
|
||||
else:
|
||||
@ -757,4 +742,3 @@ def mandatory_warnings(config):
|
||||
except PropertiesOptionError:
|
||||
pass
|
||||
config._cfgimpl_get_toplevel()._cfgimpl_mandatory = mandatory
|
||||
|
||||
|
@ -43,8 +43,19 @@ master~slave group, the name of the group and the name of the
|
||||
master option are identical.
|
||||
"""
|
||||
group_types = ['default', 'family', 'group', 'master']
|
||||
|
||||
# ____________________________________________________________
|
||||
# multi types
|
||||
|
||||
class Owner(str):
|
||||
"an owner just for a multi Option that have no value set"
|
||||
# we need a string that cannot be iterable
|
||||
def __iter__(self):
|
||||
raise StopIteration
|
||||
|
||||
def __len__(self):
|
||||
return 0
|
||||
|
||||
class Multi(list):
|
||||
"container that support items for the values of list (multi) options"
|
||||
def __init__(self, lst, config, child):
|
||||
@ -58,23 +69,16 @@ class Multi(list):
|
||||
def append(self, value):
|
||||
self.setoption(value)
|
||||
|
||||
def setoption(self, value, key=None):
|
||||
#owners = self.child.getowner(self.config)
|
||||
# None is replaced by default_multi
|
||||
if value == None:
|
||||
defval = self.child.getdefault()
|
||||
if key is not None and len(defval) > key:
|
||||
value = defval[key]
|
||||
else:
|
||||
value = self.child.default_multi
|
||||
who = 'default'
|
||||
else:
|
||||
def setoption(self, value, key=None, who=None):
|
||||
if who is None:
|
||||
who = self.config._cfgimpl_owner
|
||||
if not self.child._validate(value):
|
||||
raise ConfigError("invalid value {0} "
|
||||
"for option {1}".format(str(value), self.child._name))
|
||||
if not self.child._validate(value):
|
||||
raise ConfigError("invalid value {0} "
|
||||
"for option {1}".format(str(value), self.child._name))
|
||||
oldvalue = list(self)
|
||||
oldowner = self.child.getowner(self.config)
|
||||
if isinstance(oldowner, Owner):
|
||||
oldowner = []
|
||||
if key is None:
|
||||
ret = super(Multi, self).append(value)
|
||||
oldvalue.append(None)
|
||||
@ -105,6 +109,11 @@ class Option(HiddenBaseType, DisabledBaseType):
|
||||
def __init__(self, name, doc, default=None, default_multi=None,
|
||||
requires=None, mandatory=False, multi=False, callback=None,
|
||||
callback_params=None):
|
||||
"""
|
||||
:param default: ['bla', 'bla', 'bla']
|
||||
:param default_multi: 'bla' (used in case of a reset to default only at
|
||||
a given index)
|
||||
"""
|
||||
self._name = name
|
||||
self.doc = doc
|
||||
self._requires = requires
|
||||
@ -211,7 +220,7 @@ class Option(HiddenBaseType, DisabledBaseType):
|
||||
"""
|
||||
name = self._name
|
||||
if self.is_multi():
|
||||
if not type(owner) == list:
|
||||
if not type(owner) == list and not isinstance(owner, Owner):
|
||||
raise ConfigError("invalid owner for multi "
|
||||
"option: {0}".format(name))
|
||||
config._cfgimpl_value_owners[name] = owner
|
||||
@ -220,6 +229,27 @@ class Option(HiddenBaseType, DisabledBaseType):
|
||||
"config *must* be only the **parent** config (not the toplevel config)"
|
||||
return config._cfgimpl_value_owners[self._name]
|
||||
|
||||
def reset(self, config, idx=None):
|
||||
"""resets the default value and owner
|
||||
:param idx: if not None, resets only the element at index idx
|
||||
"""
|
||||
if self.is_multi():
|
||||
if idx is not None:
|
||||
defval = self.getdefault()
|
||||
# if the default is ['a', 'b', 'c']
|
||||
if len(defval) > idx:
|
||||
# and idx = 4 -> there is actually no such value in the default
|
||||
value.setoption(default_multi, idx, who='default')
|
||||
else:
|
||||
# and idx = 2 -> there is a value in the default
|
||||
value.setoption(defval[idx], idx, who='default')
|
||||
else:
|
||||
value = Multi(self.getdefault(), config, self)
|
||||
config.setoption(self._name, value, 'default')
|
||||
else:
|
||||
value = self.getdefault()
|
||||
config.setoption(self._name, value, 'default')
|
||||
|
||||
def is_default_owner(self, config, all_default=True, at_index=None):
|
||||
"""
|
||||
:param config: *must* be only the **parent** config
|
||||
@ -274,29 +304,7 @@ class Option(HiddenBaseType, DisabledBaseType):
|
||||
if config.is_frozen() and self.is_frozen():
|
||||
raise TypeError('cannot change the value to %s for '
|
||||
'option %s' % (str(value), name))
|
||||
if who == "default":
|
||||
# changes the default value (and therefore resets the previous value)
|
||||
if self._validate(value):
|
||||
self.default = value
|
||||
else:
|
||||
raise ConfigError("invalid value %s for option %s" % (value, name))
|
||||
apply_requires(self, config)
|
||||
# FIXME put the validation for the multi somewhere else
|
||||
# # it is a multi **and** it has requires
|
||||
# if self.multi == True:
|
||||
# if type(value) != list:
|
||||
# raise TypeError("value {0} must be a list".format(value))
|
||||
# if self._requires is not None:
|
||||
# for reqname in self._requires:
|
||||
# # FIXME : verify that the slaves are all multi
|
||||
# #option = getattr(config._cfgimpl_descr, reqname)
|
||||
# # if not option.multi == True:
|
||||
# # raise ConflictConfigError("an option with requires "
|
||||
# # "has to be a list type : {0}".format(name))
|
||||
# if len(config._cfgimpl_values[reqname]) != len(value):
|
||||
# raise ConflictConfigError("an option with requires "
|
||||
# "has not the same length of the others "
|
||||
# "in the group : {0}".format(reqname))
|
||||
if type(config._cfgimpl_values[name]) == Multi:
|
||||
config._cfgimpl_previous_values[name] = list(config._cfgimpl_values[name])
|
||||
else:
|
||||
|
Reference in New Issue
Block a user