Merge branch 'master' into metaconfig

This commit is contained in:
2014-02-02 18:21:22 +01:00
25 changed files with 1453 additions and 670 deletions

View File

@@ -20,15 +20,16 @@
# ____________________________________________________________
"enables us to carry out a calculation and return an option's value"
from tiramisu.error import PropertiesOptionError, ConfigError
from tiramisu.setting import multitypes
from tiramisu.i18n import _
# ____________________________________________________________
def carry_out_calculation(name, config, callback, callback_params,
def carry_out_calculation(option, config, callback, callback_params,
index=None, max_len=None):
"""a function that carries out a calculation for an option's value
:param name: the option name (`opt._name`)
:param name: the option
:param config: the context config in order to have
the whole options available
:param callback: the name of the callback function
@@ -49,13 +50,13 @@ def carry_out_calculation(name, config, callback, callback_params,
Values could have multiple values only when key is ''.
* if no callback_params:
=> calculate()
=> calculate(<function func at 0x2092320>, [], {})
* if callback_params={'': ('yes',)}
=> calculate('yes')
=> calculate(<function func at 0x2092320>, ['yes'], {})
* if callback_params={'value': ('yes',)}
=> calculate(value='yes')
=> calculate(<function func at 0x165b320>, [], {'value': 'yes'})
* if callback_params={'': ('yes', 'no')}
=> calculate('yes', 'no')
@@ -63,58 +64,71 @@ def carry_out_calculation(name, config, callback, callback_params,
* if callback_params={'value': ('yes', 'no')}
=> ValueError()
* if callback_params={'': (['yes', 'no'],)}
=> calculate(<function func at 0x176b320>, ['yes', 'no'], {})
* if callback_params={'value': ('yes', 'no')}
=> raises ValueError()
* if callback_params={'': ((opt1, False),)}
- a simple option:
opt1 == 11
=> calculate(11)
=> calculate(<function func at 0x1cea320>, [11], {})
- a multi option:
- a multi option and not master/slave:
opt1 == [1, 2, 3]
=> calculate(1)
=> calculate(2)
=> calculate(3)
=> calculate(<function func at 0x223c320>, [[1, 2, 3]], {})
- option is master or slave of opt1:
opt1 == [1, 2, 3]
=> calculate(<function func at 0x223c320>, [1], {})
=> calculate(<function func at 0x223c320>, [2], {})
=> calculate(<function func at 0x223c320>, [3], {})
- opt is a master or slave but not related to option:
opt1 == [1, 2, 3]
=> calculate(<function func at 0x11b0320>, [[1, 2, 3]], {})
* if callback_params={'value': ((opt1, False),)}
- a simple option:
opt1 == 11
=> calculate(value=11)
=> calculate(<function func at 0x17ff320>, [], {'value': 11})
- a multi option:
opt1 == [1, 2, 3]
=> calculate(value=1)
=> calculate(value=2)
=> calculate(value=3)
=> calculate(<function func at 0x1262320>, [], {'value': [1, 2, 3]})
* if callback_params={'': ((opt1, False), (opt2, False))}
- two single options
opt1 = 11
opt2 = 12
=> calculate(<function func at 0x217a320>, [11, 12], {})
- a multi option with a simple option
opt1 == [1, 2, 3]
opt2 == 11
=> calculate(1, 11)
=> calculate(2, 11)
=> calculate(3, 11)
opt2 == 12
=> calculate(<function func at 0x2153320>, [[1, 2, 3], 12], {})
- a multi option with an other multi option but with same length
opt1 == [1, 2, 3]
opt2 == [11, 12, 13]
=> calculate(1, 11)
=> calculate(2, 12)
=> calculate(3, 13)
=> calculate(<function func at 0x1981320>, [[1, 2, 3], [11, 12, 13]], {})
- a multi option with an other multi option but with different length
opt1 == [1, 2, 3]
opt2 == [11, 12]
=> ConfigError()
=> calculate(<function func at 0x2384320>, [[1, 2, 3], [11, 12]], {})
- a multi option without value with a simple option
opt1 == []
opt2 == 11
=> []
=> calculate(<function func at 0xb65320>, [[], 12], {})
* if callback_params={'value': ((opt1, False), (opt2, False))}
=> ConfigError()
=> raises ValueError()
If index is not None, return a value, otherwise return:
@@ -133,29 +147,36 @@ def carry_out_calculation(name, config, callback, callback_params,
for callbk in callbacks:
if isinstance(callbk, tuple):
# callbk is something link (opt, True|False)
option, force_permissive = callbk
opt, force_permissive = callbk
path = config.cfgimpl_get_description().impl_get_path_by_opt(
option)
opt)
# get value
try:
value = config._getattr(path, force_permissive=True)
value = config._getattr(path, force_permissive=True, validate=False)
# convert to list, not modifie this multi
if value.__class__.__name__ == 'Multi':
value = list(value)
except PropertiesOptionError as err:
if force_permissive:
continue
raise ConfigError(_('unable to carry out a calculation, '
'option {0} has properties: {1} for: '
'{2}').format(option._name,
'{2}').format(opt._name,
err.proptype,
name))
is_multi = option.impl_is_multi()
option._name))
is_multi = False
if opt.impl_is_multi():
#opt is master, search if option is a slave
if opt.impl_get_multitype() == multitypes.master:
if option in opt.impl_get_master_slaves():
is_multi = True
#opt is slave, search if option is an other slaves
elif opt.impl_get_multitype() == multitypes.slave:
if option in opt.impl_get_master_slaves().impl_get_master_slaves():
is_multi = True
if is_multi:
len_value = len(value)
if len_multi is not None and len_multi != len_value:
raise ConfigError(_('unable to carry out a '
'calculation, option value with'
' multi types must have same '
'length for: {0}').format(name))
len_multi = len_value
len_multi = len(value)
one_is_multi = True
tcparams.setdefault(key, []).append((value, is_multi))
else:
@@ -168,16 +189,9 @@ def carry_out_calculation(name, config, callback, callback_params,
if one_is_multi:
ret = []
if index:
if index < len_multi:
range_ = [index]
else:
range_ = []
ret = None
range_ = [index]
else:
if max_len and max_len < len_multi:
range_ = range(max_len)
else:
range_ = range(len_multi)
range_ = range(len_multi)
for incr in range_:
args = []
kwargs = {}
@@ -196,10 +210,7 @@ def carry_out_calculation(name, config, callback, callback_params,
if index:
ret = calc
else:
if isinstance(calc, list):
ret.extend(calc)
else:
ret.append(calc)
ret.append(calc)
return ret
else:
# no value is multi
@@ -213,7 +224,18 @@ def carry_out_calculation(name, config, callback, callback_params,
args.append(couple[0])
else:
kwargs[key] = couple[0]
return calculate(callback, args, kwargs)
ret = calculate(callback, args, kwargs)
if callback_params != {}:
if isinstance(ret, list) and max_len:
ret = ret[:max_len]
if len(ret) < max_len:
ret = ret + [None] * (max_len - len(ret))
if isinstance(ret, list) and index:
if len(ret) < index + 1:
ret = None
else:
ret = ret[index]
return ret
def calculate(callback, args, kwargs):

View File

@@ -156,7 +156,15 @@ class SubConfig(object):
__repr__ = __str__
def _cfgimpl_get_context(self):
return self._impl_context()
"""context could be None, we need to test it
context is None only if all reference to `Config` object is deleted
(for example we delete a `Config` and we manipulate a reference to
old `SubConfig`, `Values`, `Multi` or `Settings`)
"""
context = self._impl_context()
if context is None:
raise ConfigError(_('the context does not exist anymore'))
return context
def cfgimpl_get_description(self):
if self._impl_descr is None:
@@ -250,7 +258,8 @@ class SubConfig(object):
force_properties=force_properties,
force_permissive=force_permissive)
def find(self, bytype=None, byname=None, byvalue=None, type_='option'):
def find(self, bytype=None, byname=None, byvalue=None, type_='option',
check_properties=True):
"""
finds a list of options recursively in the config
@@ -262,11 +271,11 @@ class SubConfig(object):
return self._cfgimpl_get_context()._find(bytype, byname, byvalue,
first=False,
type_=type_,
_subpath=self.cfgimpl_get_path()
)
_subpath=self.cfgimpl_get_path(),
check_properties=check_properties)
def find_first(self, bytype=None, byname=None, byvalue=None,
type_='option', display_error=True):
type_='option', display_error=True, check_properties=True):
"""
finds an option recursively in the config
@@ -277,7 +286,8 @@ class SubConfig(object):
"""
return self._cfgimpl_get_context()._find(
bytype, byname, byvalue, first=True, type_=type_,
_subpath=self.cfgimpl_get_path(), display_error=display_error)
_subpath=self.cfgimpl_get_path(), display_error=display_error,
check_properties=check_properties)
def _find(self, bytype, byname, byvalue, first, type_='option',
_subpath=None, check_properties=True, display_error=True):
@@ -734,4 +744,6 @@ def mandatory_warnings(config):
except PropertiesOptionError as err:
if err.proptype == ['mandatory']:
yield path
except ConfigError:
pass
config.cfgimpl_reset_cache(only=('values',))

View File

@@ -40,9 +40,7 @@ forbidden_names = ('iter_all', 'iter_group', 'find', 'find_first',
def valid_name(name):
"an option's name is a str and does not start with 'impl' or 'cfgimpl'"
try:
name = str(name)
except:
if not isinstance(name, str):
return False
if re.match(name_regexp, name) is None and not name.startswith('_') \
and name not in forbidden_names \
@@ -413,7 +411,7 @@ class Option(BaseOption):
else:
validator_params = {'': (val,)}
# Raise ValueError if not valid
carry_out_calculation(self._name, config=context,
carry_out_calculation(self, config=context,
callback=self._validator[0],
callback_params=validator_params)
@@ -421,7 +419,11 @@ class Option(BaseOption):
if _value is None:
return
# option validation
self._validate(_value)
try:
self._validate(_value)
except ValueError as err:
raise ValueError(_('invalid value for option {0}: {1}'
'').format(self._name, err))
try:
# valid with self._validator
val_validator(_value)
@@ -430,8 +432,8 @@ class Option(BaseOption):
descr._valid_consistency(self, _value, context, _index)
self._second_level_validation(_value)
except ValueError as err:
msg = _("invalid value {0} for option {1}: {2}").format(
_value, self._name, err)
msg = _("invalid value for option {0}: {1}").format(
self._name, err)
if self._warnings_only:
warnings.warn_explicit(ValueWarning(msg, self),
ValueWarning,
@@ -447,17 +449,13 @@ class Option(BaseOption):
do_validation(value, force_index)
else:
if not isinstance(value, list):
raise ValueError(_("which must be a list").format(value,
self._name))
raise ValueError(_("invalid value {0} for option {1} which must be a list").format(value, self._name))
for index, val in enumerate(value):
do_validation(val, index)
def impl_getdefault(self, default_multi=False):
def impl_getdefault(self):
"accessing the default value"
if not default_multi or not self.impl_is_multi():
return self._default
else:
return self.getdefault_multi()
return self._default
def impl_getdefault_multi(self):
"accessing the default value for a multi"
@@ -677,7 +675,7 @@ class BoolOption(Option):
def _validate(self, value):
if not isinstance(value, bool):
raise ValueError(_('value must be a boolean'))
raise ValueError(_('invalid boolean'))
class IntOption(Option):
@@ -687,7 +685,7 @@ class IntOption(Option):
def _validate(self, value):
if not isinstance(value, int):
raise ValueError(_('value must be an integer'))
raise ValueError(_('invalid integer'))
class FloatOption(Option):
@@ -697,7 +695,7 @@ class FloatOption(Option):
def _validate(self, value):
if not isinstance(value, float):
raise ValueError(_('value must be a float'))
raise ValueError(_('invalid float'))
class StrOption(Option):
@@ -707,12 +705,11 @@ class StrOption(Option):
def _validate(self, value):
if not isinstance(value, str):
raise ValueError(_('value must be a string, not '
'{0}').format(type(value)))
raise ValueError(_('invalid string'))
if sys.version_info[0] >= 3:
#UnicodeOption is same has StrOption in python 3+
#UnicodeOption is same as StrOption in python 3+
class UnicodeOption(StrOption):
__slots__ = tuple()
pass
@@ -725,7 +722,7 @@ else:
def _validate(self, value):
if not isinstance(value, unicode):
raise ValueError(_('value must be an unicode'))
raise ValueError(_('invalid unicode'))
class SymLinkOption(BaseOption):
@@ -783,17 +780,23 @@ class IPOption(Option):
warnings_only=warnings_only)
def _validate(self, value):
# sometimes an ip term starts with a zero
# but this does not fit in some case, for example bind does not like it
for val in value.split('.'):
if val.startswith("0") and len(val) > 1:
raise ValueError(_('invalid IP'))
# 'standard' validation
try:
IP('{0}/32'.format(value))
except ValueError:
raise ValueError(_('invalid IP {0}').format(self._name))
raise ValueError(_('invalid IP'))
def _second_level_validation(self, value):
ip = IP('{0}/32'.format(value))
if not self._allow_reserved and ip.iptype() == 'RESERVED':
raise ValueError(_("IP mustn't not be in reserved class"))
raise ValueError(_("invalid IP, mustn't not be in reserved class"))
if self._private_only and not ip.iptype() == 'PRIVATE':
raise ValueError(_("IP must be in private class"))
raise ValueError(_("invalid IP, must be in private class"))
class PortOption(Option):
@@ -853,16 +856,17 @@ class PortOption(Option):
if self._allow_range and ":" in str(value):
value = str(value).split(':')
if len(value) != 2:
raise ValueError('range must have two values only')
raise ValueError('invalid part, range must have two values '
'only')
if not value[0] < value[1]:
raise ValueError('first port in range must be'
raise ValueError('invalid port, first port in range must be'
' smaller than the second one')
else:
value = [value]
for val in value:
if not self._min_value <= int(val) <= self._max_value:
raise ValueError('port must be an between {0} and {1}'
raise ValueError('invalid port, must be an between {0} and {1}'
''.format(self._min_value, self._max_value))
@@ -875,12 +879,12 @@ class NetworkOption(Option):
try:
IP(value)
except ValueError:
raise ValueError(_('invalid network address {0}').format(self._name))
raise ValueError(_('invalid network address'))
def _second_level_validation(self, value):
ip = IP(value)
if ip.iptype() == 'RESERVED':
raise ValueError(_("network shall not be in reserved class"))
raise ValueError(_("invalid network address, must not be in reserved class"))
class NetmaskOption(Option):
@@ -892,7 +896,7 @@ class NetmaskOption(Option):
try:
IP('0.0.0.0/{0}'.format(value))
except ValueError:
raise ValueError(_('invalid netmask address {0}').format(self._name))
raise ValueError(_('invalid netmask address'))
def _cons_network_netmask(self, opts, vals):
#opts must be (netmask, network) options
@@ -921,21 +925,21 @@ class NetmaskOption(Option):
except ValueError:
if not make_net:
msg = _("invalid network {0} ({1}) "
"with netmask {2} ({3}),"
"with netmask {2},"
" this network is an IP")
else:
if make_net:
msg = _("invalid IP {0} ({1}) with netmask {2} ({3}),"
msg = _("invalid IP {0} ({1}) with netmask {2},"
" this IP is a network")
except ValueError:
if make_net:
msg = _("invalid IP {0} ({1}) with netmask {2} ({3})")
msg = _('invalid IP {0} ({1}) with netmask {2}')
else:
msg = _("invalid network {0} ({1}) with netmask {2} ({3})")
msg = _('invalid network {0} ({1}) with netmask {2}')
if msg is not None:
raise ValueError(msg.format(val_ipnetwork, opts[1]._name,
val_netmask, self._name))
val_netmask))
class BroadcastOption(Option):
@@ -946,7 +950,7 @@ class BroadcastOption(Option):
try:
IP('{0}/32'.format(value))
except ValueError:
raise ValueError(_('invalid broadcast address {0}').format(self._name))
raise ValueError(_('invalid broadcast address'))
def _cons_broadcast(self, opts, vals):
if len(vals) != 3:
@@ -968,20 +972,39 @@ class DomainnameOption(Option):
domainname:
fqdn: with tld, not supported yet
"""
__slots__ = ('_type', '_allow_ip')
__slots__ = ('_type', '_allow_ip', '_allow_without_dot', '_domain_re')
_opt_type = 'domainname'
def __init__(self, name, doc, default=None, default_multi=None,
requires=None, multi=False, callback=None,
callback_params=None, validator=None, validator_params=None,
properties=None, allow_ip=False, type_='domainname',
warnings_only=False):
warnings_only=False, allow_without_dot=False):
if type_ not in ['netbios', 'hostname', 'domainname']:
raise ValueError(_('unknown type_ {0} for hostname').format(type_))
self._type = type_
if allow_ip not in [True, False]:
raise ValueError(_('allow_ip must be a boolean'))
if allow_without_dot not in [True, False]:
raise ValueError(_('allow_without_dot must be a boolean'))
self._allow_ip = allow_ip
self._allow_without_dot = allow_without_dot
end = ''
extrachar = ''
extrachar_mandatory = ''
if self._type == 'netbios':
length = 14
elif self._type == 'hostname':
length = 62
elif self._type == 'domainname':
length = 62
if allow_without_dot is False:
extrachar_mandatory = '\.'
else:
extrachar = '\.'
end = '+[a-z]*'
self._domain_re = re.compile(r'^(?:[a-z][a-z\d\-{0}]{{,{1}}}{2}){3}$'
''.format(extrachar, length, extrachar_mandatory, end))
super(DomainnameOption, self).__init__(name, doc, default=default,
default_multi=default_multi,
callback=callback,
@@ -1000,29 +1023,82 @@ class DomainnameOption(Option):
return
except ValueError:
pass
if self._type == 'netbios':
length = 15
extrachar = ''
elif self._type == 'hostname':
length = 63
extrachar = ''
elif self._type == 'domainname':
length = 255
extrachar = '\.'
if '.' not in value:
raise ValueError(_("invalid value for {0}, must have dot"
"").format(self._name))
if len(value) > length:
raise ValueError(_("invalid domainname's length for"
" {0} (max {1})").format(self._name, length))
if len(value) == 1:
raise ValueError(_("invalid domainname's length for {0} (min 2)"
"").format(self._name))
regexp = r'^[a-z]([a-z\d{0}-])*[a-z\d]$'.format(extrachar)
if re.match(regexp, value) is None:
if self._type == 'domainname' and not self._allow_without_dot and \
'.' not in value:
raise ValueError(_("invalid domainname, must have dot"))
if len(value) > 255:
raise ValueError(_("invalid domainname's length (max 255)"))
if len(value) < 2:
raise ValueError(_("invalid domainname's length (min 2)"))
if not self._domain_re.search(value):
raise ValueError(_('invalid domainname'))
class EmailOption(DomainnameOption):
__slots__ = tuple()
_opt_type = 'email'
username_re = re.compile(r"^[\w!#$%&'*+\-/=?^`{|}~.]+$")
def _validate(self, value):
splitted = value.split('@', 1)
try:
username, domain = splitted
except ValueError:
raise ValueError(_('invalid email address, should contains one @'
))
if not self.username_re.search(username):
raise ValueError(_('invalid username in email address'))
super(EmailOption, self)._validate(domain)
class URLOption(DomainnameOption):
__slots__ = tuple()
_opt_type = 'url'
proto_re = re.compile(r'(http|https)://')
path_re = re.compile(r"^[a-z0-9\-\._~:/\?#\[\]@!%\$&\'\(\)\*\+,;=]+$")
def _validate(self, value):
match = self.proto_re.search(value)
if not match:
raise ValueError(_('invalid url, should start with http:// or '
'https://'))
value = value[len(match.group(0)):]
# get domain/files
splitted = value.split('/', 1)
try:
domain, files = splitted
except ValueError:
domain = value
files = None
# if port in domain
splitted = domain.split(':', 1)
try:
domain, port = splitted
except ValueError:
domain = splitted[0]
port = 0
if not 0 <= int(port) <= 65535:
raise ValueError(_('invalid url, port must be an between 0 and '
'65536'))
# validate domainname
super(URLOption, self)._validate(domain)
# validate file
if files is not None and files != '' and not self.path_re.search(files):
raise ValueError(_('invalid url, should ends with filename'))
class FilenameOption(Option):
__slots__ = tuple()
_opt_type = 'file'
path_re = re.compile(r"^[a-zA-Z0-9\-\._~/+]+$")
def _validate(self, value):
match = self.path_re.search(value)
if not match:
raise ValueError(_('invalid filename'))
class OptionDescription(BaseOption):
"""Config's schema (organisation, group) and container of Options
The `OptionsDescription` objects lives in the `tiramisu.config.Config`.
@@ -1177,7 +1253,6 @@ class OptionDescription(BaseOption):
self._group_type = group_type
if isinstance(group_type, groups.MasterGroupType):
#if master (same name has group) is set
identical_master_child_name = False
#for collect all slaves
slaves = []
master = None
@@ -1194,7 +1269,6 @@ class OptionDescription(BaseOption):
": this option is not a multi"
"").format(child._name, self._name))
if child._name == self._name:
identical_master_child_name = True
child._multitype = multitypes.master
master = child
else:
@@ -1203,14 +1277,18 @@ class OptionDescription(BaseOption):
raise ValueError(_('master group with wrong'
' master name for {0}'
).format(self._name))
if master._callback is not None and master._callback[1] is not None:
for key, callbacks in master._callback[1].items():
for callbk in callbacks:
if isinstance(callbk, tuple):
if callbk[0] in slaves:
raise ValueError(_("callback of master's option shall "
"not refered a slave's ones"))
master._master_slaves = tuple(slaves)
for child in self.impl_getchildren():
if child != master:
child._master_slaves = master
child._multitype = multitypes.slave
if not identical_master_child_name:
raise ValueError(_("no child has same nom has master group"
" for: {0}").format(self._name))
else:
raise ValueError(_('group_type: {0}'
' not allowed').format(group_type))

View File

@@ -24,7 +24,7 @@ from time import time
from copy import copy
import weakref
from tiramisu.error import (RequirementError, PropertiesOptionError,
ConstError)
ConstError, ConfigError)
from tiramisu.i18n import _
@@ -242,6 +242,14 @@ multitypes = MultiTypeModule()
populate_multitypes()
# ____________________________________________________________
class Undefined():
pass
undefined = Undefined()
# ____________________________________________________________
class Property(object):
"a property is responsible of the option's value access rules"
@@ -254,6 +262,11 @@ class Property(object):
self._properties = prop
def append(self, propname):
"""Appends a property named propname
:param propname: a predefined or user defined property name
:type propname: string
"""
if self._opt is not None and self._opt._calc_properties is not None \
and propname in self._opt._calc_properties:
raise ValueError(_('cannot append {0} property for option {1}: '
@@ -263,12 +276,29 @@ class Property(object):
self._setting._setproperties(self._properties, self._opt, self._path)
def remove(self, propname):
"""Removes a property named propname
:param propname: a predefined or user defined property name
:type propname: string
"""
if propname in self._properties:
self._properties.remove(propname)
self._setting._setproperties(self._properties, self._opt,
self._path)
def extend(self, propnames):
"""Extends properties to the existing properties
:param propnames: an iterable made of property names
:type propnames: iterable of string
"""
for propname in propnames:
self.append(propname)
def reset(self):
"""resets the properties (does not **clear** the properties,
default properties are still present)
"""
self._setting.reset(_path=self._path)
def __contains__(self, propname):
@@ -280,7 +310,7 @@ class Property(object):
#____________________________________________________________
class Settings(object):
"``Config()``'s configuration options"
"``config.Config()``'s configuration options settings"
__slots__ = ('context', '_owner', '_p_', '__weakref__')
def __init__(self, context, storage):
@@ -298,6 +328,17 @@ class Settings(object):
self.context = weakref.ref(context)
self._p_ = storage
def _getcontext(self):
"""context could be None, we need to test it
context is None only if all reference to `Config` object is deleted
(for example we delete a `Config` and we manipulate a reference to
old `SubConfig`, `Values`, `Multi` or `Settings`)
"""
context = self.context()
if context is None:
raise ConfigError(_('the context does not exist anymore'))
return context
#____________________________________________________________
# properties methods
def __contains__(self, propname):
@@ -322,12 +363,12 @@ class Settings(object):
raise ValueError(_('opt and all_properties must not be set '
'together in reset'))
if all_properties:
self._p_.reset_all_propertives()
self._p_.reset_all_properties()
else:
if opt is not None and _path is None:
_path = self._get_path_by_opt(opt)
self._p_.reset_properties(_path)
self.context().cfgimpl_reset_cache()
self._getcontext().cfgimpl_reset_cache()
def _getproperties(self, opt=None, path=None, is_apply_req=True):
if opt is None:
@@ -380,7 +421,7 @@ class Settings(object):
self._p_.reset_properties(path)
else:
self._p_.setproperties(path, properties)
self.context().cfgimpl_reset_cache()
self._getcontext().cfgimpl_reset_cache()
#____________________________________________________________
def validate_properties(self, opt_or_descr, is_descr, is_write, path,
@@ -406,10 +447,12 @@ class Settings(object):
"""
# opt properties
properties = copy(self._getproperties(opt_or_descr, path))
self_properties = copy(self._getproperties())
# remove opt permissive
# permissive affect option's permission with or without permissive
# global property
properties -= self._p_.getpermissive(path)
# remove global permissive if need
self_properties = copy(self._getproperties())
if force_permissive is True or 'permissive' in self_properties:
properties -= self._p_.getpermissive()
if force_permissives is not None:
@@ -426,7 +469,7 @@ class Settings(object):
properties -= frozenset(('mandatory', 'frozen'))
else:
if 'mandatory' in properties and \
not self.context().cfgimpl_get_values()._isempty(
not self._getcontext().cfgimpl_get_values()._isempty(
opt_or_descr, value):
properties.remove('mandatory')
if is_write and 'everything_frozen' in self_properties:
@@ -549,6 +592,7 @@ class Settings(object):
# filters the callbacks
calc_properties = set()
context = self._getcontext()
for requires in opt._requires:
for require in requires:
option, expected, action, inverse, \
@@ -560,8 +604,7 @@ class Settings(object):
" '{0}' with requirement on: "
"'{1}'").format(path, reqpath))
try:
value = self.context()._getattr(reqpath,
force_permissive=True)
value = context._getattr(reqpath, force_permissive=True)
except PropertiesOptionError as err:
if not transitive:
continue
@@ -590,7 +633,7 @@ class Settings(object):
:param opt: `Option`'s object
:returns: path
"""
return self.context().cfgimpl_get_description().impl_get_path_by_opt(opt)
return self._getcontext().cfgimpl_get_description().impl_get_path_by_opt(opt)
def get_modified_properties(self):
return self._p_.get_modified_properties()

View File

@@ -31,7 +31,7 @@ class Settings(Cache):
self._permissives = {}
super(Settings, self).__init__(storage)
# propertives
# properties
def setproperties(self, path, properties):
self._properties[path] = properties
@@ -41,7 +41,7 @@ class Settings(Cache):
def hasproperties(self, path):
return path in self._properties
def reset_all_propertives(self):
def reset_all_properties(self):
self._properties.clear()
def reset_properties(self, path):

View File

@@ -33,7 +33,7 @@ class Settings(Sqlite3DB):
self._storage.execute(settings_table, commit=False)
self._storage.execute(permissives_table)
# propertives
# properties
def setproperties(self, path, properties):
path = self._sqlite_encode_path(path)
self._storage.execute("DELETE FROM property WHERE path = ?", (path,),
@@ -56,7 +56,7 @@ class Settings(Sqlite3DB):
return self._storage.select("SELECT properties FROM property WHERE "
"path = ?", (path,)) is not None
def reset_all_propertives(self):
def reset_all_properties(self):
self._storage.execute("DELETE FROM property")
def reset_properties(self, path):

View File

@@ -22,7 +22,7 @@ from copy import copy
import sys
import weakref
from tiramisu.error import ConfigError, SlaveError
from tiramisu.setting import owners, multitypes, expires_time
from tiramisu.setting import owners, multitypes, expires_time, undefined
from tiramisu.autolib import carry_out_calculation
from tiramisu.i18n import _
from tiramisu.option import SymLinkOption
@@ -46,13 +46,24 @@ class Values(object):
# the storage type is dictionary or sqlite3
self._p_ = storage
def _getcontext(self):
"""context could be None, we need to test it
context is None only if all reference to `Config` object is deleted
(for example we delete a `Config` and we manipulate a reference to
old `SubConfig`, `Values`, `Multi` or `Settings`)
"""
context = self.context()
if context is None:
raise ConfigError(_('the context does not exist anymore'))
return context
def _getdefault(self, opt):
"""
actually retrieves the default value
:param opt: the `option.Option()` object
"""
meta = self.context().cfgimpl_get_meta()
meta = self._getcontext().cfgimpl_get_meta()
if meta is not None:
value = meta.cfgimpl_get_values()[opt]
else:
@@ -62,7 +73,7 @@ class Values(object):
else:
return value
def _getvalue(self, opt, path, validate=True):
def _getvalue(self, opt, path):
"""actually retrieves the value
:param opt: the `option.Option()` object
@@ -71,14 +82,9 @@ class Values(object):
if not self._p_.hasvalue(path):
# if there is no value
value = self._getdefault(opt)
if opt.impl_is_multi():
value = Multi(value, self.context, opt, path, validate)
else:
# if there is a value
value = self._p_.getvalue(path)
if opt.impl_is_multi() and not isinstance(value, Multi):
# load value so don't need to validate if is not a Multi
value = Multi(value, self.context, opt, path, validate=False)
return value
def get_modified_values(self):
@@ -105,11 +111,11 @@ class Values(object):
if path is None:
path = self._get_opt_path(opt)
if self._p_.hasvalue(path):
setting = self.context().cfgimpl_get_settings()
context = self._getcontext()
setting = context.cfgimpl_get_settings()
opt.impl_validate(opt.impl_getdefault(),
self.context(),
'validator' in setting)
self.context().cfgimpl_reset_cache()
context, 'validator' in setting)
context.cfgimpl_reset_cache()
if (opt.impl_is_multi() and
opt.impl_get_multitype() == multitypes.master):
for slave in opt.impl_get_master_slaves():
@@ -137,7 +143,7 @@ class Values(object):
callback, callback_params = opt._callback
if callback_params is None:
callback_params = {}
return carry_out_calculation(opt._name, config=self.context(),
return carry_out_calculation(opt, config=self._getcontext(),
callback=callback,
callback_params=callback_params,
index=index, max_len=max_len)
@@ -151,7 +157,7 @@ class Values(object):
if path is None:
path = self._get_opt_path(opt)
ntime = None
setting = self.context().cfgimpl_get_settings()
setting = self._getcontext().cfgimpl_get_settings()
if 'cache' in setting and self._p_.hascache(path):
if 'expire' in setting:
ntime = int(time())
@@ -176,7 +182,8 @@ class Values(object):
def _getitem(self, opt, path, validate, force_permissive, force_properties,
validate_properties):
# options with callbacks
setting = self.context().cfgimpl_get_settings()
context = self._getcontext()
setting = context.cfgimpl_get_settings()
is_frozen = 'frozen' in setting[opt]
# For calculating properties, we need value (ie for mandatory value).
# If value is calculating with a PropertiesOptionError's option
@@ -186,7 +193,7 @@ class Values(object):
# ConfigError if properties did not raise.
config_error = None
force_permissives = None
# if value is callback and is not set
# if value has callback and is not set
# or frozen with force_default_on_freeze
if opt.impl_has_callback() and (
self._is_default_owner(path) or
@@ -196,7 +203,7 @@ class Values(object):
if (opt.impl_is_multi() and
opt.impl_get_multitype() == multitypes.slave):
masterp = self._get_opt_path(opt.impl_get_master_slaves())
mastervalue = getattr(self.context(), masterp)
mastervalue = context._getattr(masterp, validate=validate)
lenmaster = len(mastervalue)
if lenmaster == 0:
value = []
@@ -228,9 +235,12 @@ class Values(object):
if opt.impl_is_multi():
value = Multi(value, self.context, opt, path, validate)
else:
value = self._getvalue(opt, path, validate)
value = self._getvalue(opt, path)
if opt.impl_is_multi():
# load value so don't need to validate if is not a Multi
value = Multi(value, self.context, opt, path, validate=validate)
if config_error is None and validate:
opt.impl_validate(value, self.context(), 'validator' in setting)
opt.impl_validate(value, context, 'validator' in setting)
if config_error is None and self._is_default_owner(path) and \
'force_store_value' in setting[opt]:
self.setitem(opt, value, path, is_write=False)
@@ -251,24 +261,45 @@ class Values(object):
# is_write is, for example, used with "force_store_value"
# user didn't change value, so not write
# valid opt
opt.impl_validate(value, self.context(),
'validator' in self.context().cfgimpl_get_settings())
if opt.impl_is_multi() and not isinstance(value, Multi):
context = self._getcontext()
opt.impl_validate(value, context,
'validator' in context.cfgimpl_get_settings())
if opt.impl_is_multi():
value = Multi(value, self.context, opt, path, setitem=True)
# Save old value
if opt.impl_get_multitype() == multitypes.master and \
self._p_.hasvalue(path):
old_value = self._p_.getvalue(path)
old_owner = self._p_.getowner(path, None)
else:
old_value = undefined
old_owner = undefined
self._setvalue(opt, path, value, force_permissive=force_permissive,
is_write=is_write)
if opt.impl_is_multi() and opt.impl_get_multitype() == multitypes.master:
try:
value._valid_master()
except Exception, err:
if old_value is not undefined:
self._p_.setvalue(path, old_value, old_owner)
else:
self._p_.resetvalue(path)
raise err
def _setvalue(self, opt, path, value, force_permissive=False,
force_properties=None,
is_write=True, validate_properties=True):
self.context().cfgimpl_reset_cache()
context = self._getcontext()
context.cfgimpl_reset_cache()
if validate_properties:
setting = self.context().cfgimpl_get_settings()
setting = context.cfgimpl_get_settings()
setting.validate_properties(opt, False, is_write,
value=value, path=path,
force_permissive=force_permissive,
force_properties=force_properties)
owner = self.context().cfgimpl_get_settings().getowner()
owner = context.cfgimpl_get_settings().getowner()
if isinstance(value, Multi):
value = list(value)
self._p_.setvalue(path, value, owner)
def getowner(self, opt):
@@ -285,7 +316,7 @@ class Values(object):
def _getowner(self, path):
owner = self._p_.getowner(path, owners.default)
meta = self.context().cfgimpl_get_meta()
meta = self._getcontext().cfgimpl_get_meta()
if owner is owners.default and meta is not None:
owner = meta.cfgimpl_get_values()._getowner(path)
return owner
@@ -337,7 +368,7 @@ class Values(object):
:param opt: the `option.Option` object
:returns: a string with points like "gc.dummy.my_option"
"""
return self.context().cfgimpl_get_description().impl_get_path_by_opt(opt)
return self._getcontext().cfgimpl_get_description().impl_get_path_by_opt(opt)
# information
def set_information(self, key, value):
@@ -389,6 +420,8 @@ class Multi(list):
:param opt: the option object that have this Multi value
:param setitem: only if set a value
"""
if isinstance(value, Multi):
raise ValueError(_('{0} is already a Multi ').format(opt._name))
self.opt = opt
self.path = path
if not isinstance(context, weakref.ReferenceType):
@@ -398,21 +431,32 @@ class Multi(list):
value = [value]
if validate and self.opt.impl_get_multitype() == multitypes.slave:
value = self._valid_slave(value, setitem)
elif validate and self.opt.impl_get_multitype() == multitypes.master:
self._valid_master(value)
elif not setitem and validate and \
self.opt.impl_get_multitype() == multitypes.master:
self._valid_master()
super(Multi, self).__init__(value)
def _getcontext(self):
"""context could be None, we need to test it
context is None only if all reference to `Config` object is deleted
(for example we delete a `Config` and we manipulate a reference to
old `SubConfig`, `Values`, `Multi` or `Settings`)
"""
context = self.context()
if context is None:
raise ConfigError(_('the context does not exist anymore'))
return context
def _valid_slave(self, value, setitem):
#if slave, had values until master's one
values = self.context().cfgimpl_get_values()
masterp = self.context().cfgimpl_get_description().impl_get_path_by_opt(
context = self._getcontext()
values = context.cfgimpl_get_values()
masterp = context.cfgimpl_get_description().impl_get_path_by_opt(
self.opt.impl_get_master_slaves())
mastervalue = getattr(self.context(), masterp)
mastervalue = context._getattr(masterp, validate=False)
masterlen = len(mastervalue)
valuelen = len(value)
is_default_owner = not values._is_default_owner(self.path) or setitem
if valuelen > masterlen or (valuelen < masterlen and
is_default_owner):
if valuelen > masterlen or (valuelen < masterlen and setitem):
raise SlaveError(_("invalid len for the slave: {0}"
" which has {1} as master").format(
self.opt._name, masterp))
@@ -429,59 +473,43 @@ class Multi(list):
#else: same len so do nothing
return value
def _valid_master(self, value):
masterlen = len(value)
values = self.context().cfgimpl_get_values()
def _valid_master(self):
#masterlen = len(value)
values = self._getcontext().cfgimpl_get_values()
for slave in self.opt._master_slaves:
path = values._get_opt_path(slave)
if not values._is_default_owner(path):
value_slave = values._getvalue(slave, path)
if len(value_slave) > masterlen:
raise SlaveError(_("invalid len for the master: {0}"
" which has {1} as slave with"
" greater len").format(
self.opt._name, slave._name))
elif len(value_slave) < masterlen:
for num in range(0, masterlen - len(value_slave)):
if slave.impl_has_callback():
# if callback add a value, but this value will not
# change anymore automaticly (because this value
# has owner)
index = value_slave.__len__()
value_slave.append(
values._getcallback_value(slave, index=index),
force=True)
else:
value_slave.append(slave.impl_getdefault_multi(),
force=True)
Multi(values._getvalue(slave, path), self.context, slave, path)
def __setitem__(self, index, value):
self._validate(value, index)
#assume not checking mandatory property
super(Multi, self).__setitem__(index, value)
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
def append(self, value, force=False):
def append(self, value=undefined, force=False):
"""the list value can be updated (appened)
only if the option is a master
"""
context = self._getcontext()
if not force:
if self.opt.impl_get_multitype() == multitypes.slave:
raise SlaveError(_("cannot append a value on a multi option {0}"
" which is a slave").format(self.opt._name))
elif self.opt.impl_get_multitype() == multitypes.master:
values = self.context().cfgimpl_get_values()
if value is None and self.opt.impl_has_callback():
values = context.cfgimpl_get_values()
if value is undefined and self.opt.impl_has_callback():
value = values._getcallback_value(self.opt)
#Force None il return a list
if isinstance(value, list):
value = None
index = self.__len__()
if value is undefined:
value = self.opt.impl_getdefault_multi()
self._validate(value, index)
super(Multi, self).append(value)
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path,
self,
validate_properties=not force)
context.cfgimpl_get_values()._setvalue(self.opt, self.path,
self,
validate_properties=not force)
if not force and self.opt.impl_get_multitype() == multitypes.master:
for slave in self.opt.impl_get_master_slaves():
path = values._get_opt_path(slave)
@@ -490,16 +518,15 @@ class Multi(list):
dvalue = values._getcallback_value(slave, index=index)
else:
dvalue = slave.impl_getdefault_multi()
old_value = values.getitem(slave, path,
old_value = values.getitem(slave, path, validate=False,
validate_properties=False)
if len(old_value) < self.__len__():
values.getitem(slave, path,
validate_properties=False).append(
dvalue, force=True)
else:
values.getitem(slave, path,
validate_properties=False)[
index] = dvalue
if len(old_value) + 1 != self.__len__():
raise SlaveError(_("invalid len for the slave: {0}"
" which has {1} as master").format(
self.opt._name, self.__len__()))
values.getitem(slave, path, validate=False,
validate_properties=False).append(
dvalue, force=True)
def sort(self, cmp=None, key=None, reverse=False):
if self.opt.impl_get_multitype() in [multitypes.slave,
@@ -512,7 +539,7 @@ class Multi(list):
super(Multi, self).sort(key=key, reverse=reverse)
else:
super(Multi, self).sort(cmp=cmp, key=key, reverse=reverse)
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
def reverse(self):
if self.opt.impl_get_multitype() in [multitypes.slave,
@@ -520,7 +547,7 @@ class Multi(list):
raise SlaveError(_("cannot reverse multi option {0} if master or "
"slave").format(self.opt._name))
super(Multi, self).reverse()
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
def insert(self, index, obj):
if self.opt.impl_get_multitype() in [multitypes.slave,
@@ -528,7 +555,7 @@ class Multi(list):
raise SlaveError(_("cannot insert multi option {0} if master or "
"slave").format(self.opt._name))
super(Multi, self).insert(index, obj)
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
def extend(self, iterable):
if self.opt.impl_get_multitype() in [multitypes.slave,
@@ -536,12 +563,12 @@ class Multi(list):
raise SlaveError(_("cannot extend multi option {0} if master or "
"slave").format(self.opt._name))
super(Multi, self).extend(iterable)
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
def _validate(self, value, force_index):
if value is not None:
try:
self.opt.impl_validate(value, context=self.context(),
self.opt.impl_validate(value, context=self._getcontext(),
force_index=force_index)
except ValueError as err:
raise ValueError(_("invalid value {0} "
@@ -559,19 +586,20 @@ class Multi(list):
:type force: boolean
:returns: item at index
"""
context = self._getcontext()
if not force:
if self.opt.impl_get_multitype() == multitypes.slave:
raise SlaveError(_("cannot pop a value on a multi option {0}"
" which is a slave").format(self.opt._name))
elif self.opt.impl_get_multitype() == multitypes.master:
if self.opt.impl_get_multitype() == multitypes.master:
for slave in self.opt.impl_get_master_slaves():
values = self.context().cfgimpl_get_values()
values = context.cfgimpl_get_values()
if not values.is_default_owner(slave):
#get multi without valid properties
values.getitem(slave,
values.getitem(slave, validate=False,
validate_properties=False
).pop(index, force=True)
#set value without valid properties
ret = super(Multi, self).pop(index)
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self, validate_properties=not force)
context.cfgimpl_get_values()._setvalue(self.opt, self.path, self, validate_properties=not force)
return ret