tiramisu/tiramisu/value.py

949 lines
44 KiB
Python
Raw Normal View History

2013-02-07 16:20:21 +01:00
# -*- coding: utf-8 -*-
"takes care of the option's values and multi values"
2014-06-19 23:22:39 +02:00
# Copyright (C) 2013-2014 Team tiramisu (see AUTHORS for all contributors)
2013-02-07 16:20:21 +01:00
#
2013-09-22 22:33:09 +02:00
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
2013-02-07 16:20:21 +01:00
#
2013-09-22 22:33:09 +02:00
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
2013-02-07 16:20:21 +01:00
#
2013-09-22 22:33:09 +02:00
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2013-02-07 16:20:21 +01:00
# ____________________________________________________________
from time import time
import sys
import weakref
from .error import ConfigError, SlaveError, PropertiesOptionError
from .setting import owners, expires_time, undefined
from .autolib import carry_out_calculation
from .i18n import _
from .option import SymLinkOption, DynSymLinkOption, Option
2013-02-07 16:20:21 +01:00
2013-04-03 12:20:26 +02:00
2013-08-20 09:47:12 +02:00
class Values(object):
2013-05-23 17:51:50 +02:00
"""The `Config`'s root is indeed in charge of the `Option()`'s values,
but the values are physicaly located here, in `Values`, wich is also
responsible of a caching utility.
2013-05-23 14:55:52 +02:00
"""
__slots__ = ('context', '_p_', '__weakref__')
2013-04-03 12:20:26 +02:00
2013-08-20 22:45:11 +02:00
def __init__(self, context, storage):
2013-02-21 17:07:00 +01:00
"""
Initializes the values's dict.
2013-04-03 12:20:26 +02:00
:param context: the context is the home config's values
2013-05-23 14:55:52 +02:00
2013-02-21 17:07:00 +01:00
"""
self.context = weakref.ref(context)
2013-08-21 14:52:48 +02:00
# the storage type is dictionary or sqlite3
self._p_ = storage
2013-08-20 09:47:12 +02:00
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()
2014-06-19 23:22:39 +02:00
if context is None: # pragma: optional cover
raise ConfigError(_('the context does not exist anymore'))
return context
2015-11-19 22:25:00 +01:00
def _get_multi(self, opt, path):
return Multi([], self.context, opt, path)
2015-05-03 09:56:03 +02:00
2016-01-03 21:18:52 +01:00
def _getdefaultvalue(self, opt, path, with_meta, index, submulti_index,
returns_raise):
# if value has callback and is not set
if opt.impl_has_callback():
callback, callback_params = opt.impl_get_callback()
2015-04-18 22:53:45 +02:00
value = carry_out_calculation(opt, context=self._getcontext(),
callback=callback,
callback_params=callback_params,
2016-01-03 21:18:52 +01:00
index=index,
returns_raise=returns_raise)
if isinstance(value, list) and index is not None:
#if return a list and index is set, return value only if
#it's a submulti without submulti_index and without list of list
if opt.impl_is_submulti() and submulti_index is undefined and \
(len(value) == 0 or not isinstance(value[0], list)):
return value
else:
return value
if with_meta:
meta = self._getcontext().cfgimpl_get_meta()
if meta is not None:
2016-01-03 21:18:52 +01:00
value = meta.cfgimpl_get_values(
)._get_cached_value(opt, path, index=index, submulti_index=submulti_index,
from_masterslave=True, returns_raise=True)
if isinstance(value, Exception):
if not isinstance(value, PropertiesOptionError):
raise value
else:
if isinstance(value, Multi):
if index is not None:
value = value[index]
else:
value = list(value)
return value
# now try to get default value
value = opt.impl_getdefault()
if opt.impl_is_multi() and index is not None:
if value == []:
value = opt.impl_getdefault_multi()
else:
2015-12-28 22:00:46 +01:00
if len(value) > index:
value = value[index]
2015-12-28 22:00:46 +01:00
else:
value = opt.impl_getdefault_multi()
return value
2013-02-07 16:20:21 +01:00
def _getvalue(self, opt, path, self_properties, index, submulti_index,
with_meta, masterlen, session):
2015-11-19 22:25:00 +01:00
"""actually retrieves the value
:param opt: the `option.Option()` object
:returns: the option's value (or the default value if not set)
"""
force_default = 'frozen' in self_properties and \
'force_default_on_freeze' in self_properties
# not default value
is_default = self._is_default_owner(opt, path, session,
validate_properties=False,
validate_meta=False,
self_properties=self_properties,
index=index)
2015-11-19 22:25:00 +01:00
if not is_default and not force_default:
if opt.impl_is_master_slaves('slave'):
return self._p_.getvalue(path, session, index)
2015-11-19 22:25:00 +01:00
else:
value = self._p_.getvalue(path, session)
if index is not None:
2015-12-28 22:00:46 +01:00
if len(value) > index:
2015-11-19 22:25:00 +01:00
return value[index]
2015-12-28 22:00:46 +01:00
#value is smaller than expected
#so return default value
2015-11-19 22:25:00 +01:00
else:
return value
return self._getdefaultvalue(opt, path, with_meta, index,
submulti_index, True)
2015-11-19 22:25:00 +01:00
2013-08-14 23:06:31 +02:00
def get_modified_values(self):
2013-08-20 09:47:12 +02:00
return self._p_.get_modified_values()
2013-08-14 23:06:31 +02:00
def __contains__(self, opt):
2013-08-21 14:52:48 +02:00
"""
implements the 'in' keyword syntax in order provide a pythonic way
to kow if an option have a value
:param opt: the `option.Option()` object
"""
2014-06-19 23:22:39 +02:00
path = opt.impl_getpath(self._getcontext())
return self._contains(path)
def _contains(self, path, session=None):
if session is None:
session = self._p_.getsession()
return self._p_.hasvalue(path, session)
2013-08-14 23:06:31 +02:00
2013-04-18 23:06:14 +02:00
def __delitem__(self, opt):
2013-08-21 14:52:48 +02:00
"""overrides the builtins `del()` instructions"""
2013-08-14 23:06:31 +02:00
self.reset(opt)
2013-04-18 23:06:14 +02:00
2015-12-17 22:41:57 +01:00
def reset(self, opt, path=None, validate=True, _setting_properties=None):
context = self._getcontext()
2015-12-17 22:41:57 +01:00
setting = context.cfgimpl_get_settings()
if path is None:
path = opt.impl_getpath(context)
if _setting_properties is None:
_setting_properties = setting._getproperties(read_write=False)
session = self._p_.getsession()
hasvalue = self._contains(path, session)
2015-12-17 22:41:57 +01:00
if validate and hasvalue and 'validator' in _setting_properties:
session = context.cfgimpl_get_values()._p_.getsession()
fake_context = context._gen_fake_values(session)
fake_value = fake_context.cfgimpl_get_values()
fake_value.reset(opt, path, validate=False)
2015-12-17 22:41:57 +01:00
fake_value._get_cached_value(opt, path,
setting_properties=_setting_properties,
check_frozen=True)
if opt.impl_is_master_slaves('master'):
2015-12-17 22:41:57 +01:00
opt.impl_get_master_slaves().reset(opt, self, _setting_properties)
if hasvalue:
if 'force_store_value' in setting._getproperties(opt=opt,
path=path,
setting_properties=_setting_properties,
read_write=False,
apply_requires=False):
value = self._getdefaultvalue(opt, path, True, undefined, undefined, False)
self._setvalue(opt, path, value, force_owner=owners.forced)
else:
self._p_.resetvalue(path, session)
2015-12-17 22:41:57 +01:00
context.cfgimpl_reset_cache()
2013-02-26 14:56:15 +01:00
2015-11-19 22:25:00 +01:00
def _isempty(self, opt, value, force_allow_empty_list=False, index=None):
"convenience method to know if an option is empty"
if value is undefined:
return False
else:
empty = opt._empty
2015-11-19 22:25:00 +01:00
if index in [None, undefined] and opt.impl_is_multi():
if force_allow_empty_list:
allow_empty_list = True
else:
allow_empty_list = opt.impl_allow_empty_list()
if allow_empty_list is undefined:
if opt.impl_is_master_slaves('slave'):
allow_empty_list = True
else:
allow_empty_list = False
isempty = (not allow_empty_list and value == []) or \
None in value or empty in value
else:
isempty = value is None or value == empty
return isempty
2013-04-18 23:06:14 +02:00
def __getitem__(self, opt):
"enables us to use the pythonic dictionary-like access to values"
2013-04-18 23:06:14 +02:00
return self.getitem(opt)
def getitem(self, opt, validate=True, force_permissive=False):
"""
"""
return self._get_cached_value(opt, validate=validate,
force_permissive=force_permissive)
def _get_cached_value(self, opt, path=None, validate=True,
2015-12-14 23:37:15 +01:00
force_permissive=False, trusted_cached_properties=True,
validate_properties=True,
setting_properties=undefined, self_properties=undefined,
index=None, submulti_index=undefined, from_masterslave=False,
2016-01-03 21:18:52 +01:00
with_meta=True, masterlen=undefined, check_frozen=False,
returns_raise=False, session=None):
2015-10-29 09:03:13 +01:00
context = self._getcontext()
2015-12-17 22:41:57 +01:00
settings = context.cfgimpl_get_settings()
if path is None:
2015-10-29 09:03:13 +01:00
path = opt.impl_getpath(context)
2013-09-07 17:25:22 +02:00
ntime = None
if setting_properties is undefined:
2015-12-17 22:41:57 +01:00
setting_properties = settings._getproperties(read_write=False)
2015-10-29 09:03:13 +01:00
if self_properties is undefined:
2015-12-17 22:41:57 +01:00
self_properties = settings._getproperties(opt, path,
read_write=False,
setting_properties=setting_properties,
index=index)
if 'cache' in setting_properties and self._p_.hascache(path, index):
if 'expire' in setting_properties:
2013-09-07 17:25:22 +02:00
ntime = int(time())
is_cached, value = self._p_.getcache(path, ntime, index)
2013-08-14 23:06:31 +02:00
if is_cached:
if opt.impl_is_multi() and not isinstance(value, Multi) and index is None:
value = Multi(value, self.context, opt, path)
2015-12-14 23:37:15 +01:00
if not trusted_cached_properties:
2015-12-17 22:41:57 +01:00
# revalidate properties (because of not default properties)
2015-12-30 22:32:07 +01:00
props = settings.validate_properties(opt, False, False, value=value,
path=path,
force_permissive=force_permissive,
setting_properties=setting_properties,
self_properties=self_properties,
index=index)
if props:
2016-01-03 21:18:52 +01:00
if returns_raise:
return props
else:
raise props
2013-04-18 23:06:14 +02:00
return value
if session is None:
session = self._p_.getsession()
if not from_masterslave and opt.impl_is_master_slaves():
val = opt.impl_get_master_slaves().getitem(self, opt, path,
validate,
force_permissive,
2015-12-14 23:37:15 +01:00
trusted_cached_properties,
validate_properties,
session,
setting_properties=setting_properties,
index=index,
2016-01-03 21:18:52 +01:00
self_properties=self_properties,
returns_raise=returns_raise)
else:
val = self._get_validated_value(opt, path, validate,
force_permissive,
validate_properties,
setting_properties=setting_properties,
self_properties=self_properties,
with_meta=with_meta,
masterlen=masterlen,
2015-12-17 22:41:57 +01:00
index=index,
submulti_index=submulti_index,
2016-01-03 21:18:52 +01:00
check_frozen=check_frozen,
returns_raise=returns_raise,
session=session)
2016-01-06 22:37:11 +01:00
if isinstance(val, Exception):
if returns_raise:
2016-01-03 21:18:52 +01:00
return val
2016-01-06 22:37:11 +01:00
else:
raise val
# cache doesn't work with SubMulti yet
2015-12-17 22:41:57 +01:00
if not isinstance(val, SubMulti) and 'cache' in setting_properties and \
validate and validate_properties and force_permissive is False \
and trusted_cached_properties is True:
if 'expire' in setting_properties:
2013-09-07 17:25:22 +02:00
if ntime is None:
ntime = int(time())
ntime = ntime + expires_time
self._p_.setcache(path, val, ntime, index)
2013-04-18 23:06:14 +02:00
return val
def _get_validated_value(self, opt, path, validate, force_permissive,
2015-12-14 23:37:15 +01:00
validate_properties,
index=None, submulti_index=undefined,
2015-05-03 09:56:03 +02:00
with_meta=True, setting_properties=undefined,
2015-12-17 22:41:57 +01:00
self_properties=undefined, masterlen=undefined,
check_frozen=False, returns_raise=False,
session=None):
2014-04-25 22:57:08 +02:00
"""same has getitem but don't touch the cache
index is None for slave value, if value returned is not a list, just return []
"""
context = self._getcontext()
setting = context.cfgimpl_get_settings()
2015-05-03 09:56:03 +02:00
if setting_properties is undefined:
setting_properties = setting._getproperties(read_write=False)
2015-10-29 09:03:13 +01:00
if self_properties is undefined:
self_properties = setting._getproperties(opt, path, read_write=False, index=index)
2016-01-03 21:18:52 +01:00
config_error = None
if session is None:
session = self._p_.getsession()
value = self._getvalue(opt, path, self_properties, index, submulti_index,
with_meta, masterlen, session)
2016-01-03 21:18:52 +01:00
if isinstance(value, Exception):
if isinstance(value, ConfigError):
# For calculating properties, we need value (ie for mandatory
# value).
# If value is calculating with a PropertiesOptionError's option
# _getvalue raise a ConfigError.
# We can not raise ConfigError if this option should raise
# PropertiesOptionError too. So we get config_error and raise
# ConfigError if properties did not raise.
config_error = value
# value is not set, for 'undefined' (cannot set None because of
# mandatory property)
value = undefined
else:
raise value
2015-11-19 22:25:00 +01:00
else:
if index is undefined:
force_index = None
else:
force_index = index
if opt.impl_is_multi():
if force_index is None:
value = Multi(value, self.context, opt, path)
2014-04-25 22:57:08 +02:00
elif opt.impl_is_submulti() and submulti_index is undefined:
value = SubMulti(value, self.context, opt, path,
force_index)
if validate:
2014-04-25 22:57:08 +02:00
if submulti_index is undefined:
force_submulti_index = None
else:
force_submulti_index = submulti_index
err = opt.impl_validate(value, context,
'validator' in setting_properties,
force_index=force_index,
force_submulti_index=force_submulti_index)
if err:
2015-04-18 23:46:37 +02:00
config_error = err
value = None
if validate_properties:
2015-10-29 09:03:13 +01:00
if config_error is not None:
# should not raise PropertiesOptionError if option is
# mandatory
val_props = undefined
else:
val_props = value
2015-12-30 22:32:07 +01:00
props = setting.validate_properties(opt, False, check_frozen, value=val_props,
path=path,
force_permissive=force_permissive,
setting_properties=setting_properties,
self_properties=self_properties,
index=index)
if props:
2016-01-03 21:18:52 +01:00
if returns_raise:
return props
else:
raise props
if config_error is not None:
2016-01-03 21:18:52 +01:00
if returns_raise:
return config_error
else:
raise config_error
return value
2014-06-19 23:22:39 +02:00
def __setitem__(self, opt, value): # pragma: optional cover
raise ConfigError(_('you should only set value with config'))
2013-04-18 23:06:14 +02:00
def setitem(self, opt, value, path, force_permissive=False,
2015-12-31 18:35:31 +01:00
check_frozen=True, not_raises=False):
2015-12-17 22:41:57 +01:00
# check_frozen is, for example, used with "force_store_value"
2013-08-21 11:09:11 +02:00
# user didn't change value, so not write
# valid opt
context = self._getcontext()
2015-05-03 09:56:03 +02:00
setting_properties = context.cfgimpl_get_settings()._getproperties(read_write=False)
if 'validator' in setting_properties:
session = context.cfgimpl_get_values()._p_.getsession()
fake_context = context._gen_fake_values(session)
2015-12-31 18:20:36 +01:00
fake_values = fake_context.cfgimpl_get_values()
fake_values._setvalue(opt, path, value)
props = fake_values.validate(opt, value, path,
check_frozen=check_frozen,
force_permissive=force_permissive,
setting_properties=setting_properties,
session=session, not_raises=not_raises)
2015-12-31 18:35:31 +01:00
if props and not_raises:
return
err = opt.impl_validate(value, fake_context)
if err:
raise err
2015-12-31 18:20:36 +01:00
self._setvalue(opt, path, value)
def _setvalue(self, opt, path, value, force_owner=undefined, index=None):
context = self._getcontext()
context.cfgimpl_reset_cache()
if force_owner is undefined:
owner = context.cfgimpl_get_settings().getowner()
else:
owner = force_owner
2015-12-31 18:20:36 +01:00
# in storage, value must not be a multi
if isinstance(value, Multi):
value = list(value)
2014-04-25 22:57:08 +02:00
if opt.impl_is_submulti():
for idx, val in enumerate(value):
if isinstance(val, SubMulti):
value[idx] = list(val)
session = self._p_.getsession()
2015-12-30 22:32:07 +01:00
#FIXME pourquoi là et pas dans masterslaves ??
2015-12-31 18:20:36 +01:00
if opt.impl_is_master_slaves('slave'):
if index is not None:
self._p_.setvalue(path, value[index], owner, index, session)
else:
self._p_.resetvalue(path, session)
for idx, val in enumerate(value):
self._p_.setvalue(path, val, owner, idx, session)
2015-11-19 22:25:00 +01:00
else:
self._p_.setvalue(path, value, owner, None, session)
del(session)
2015-12-31 18:20:36 +01:00
def validate(self, opt, value, path, check_frozen=True, force_permissive=False,
2015-12-31 18:35:31 +01:00
setting_properties=undefined, valid_masterslave=True,
not_raises=False, returns_raise=False, session=None):
2015-12-31 18:20:36 +01:00
if valid_masterslave and opt.impl_is_master_slaves():
if session is None:
session = self._p_.getsession()
opt.impl_get_master_slaves().validate(self, opt, value, path, returns_raise, session)
2015-12-31 18:20:36 +01:00
props = self._getcontext().cfgimpl_get_settings().validate_properties(opt,
False,
check_frozen,
value=value,
path=path,
force_permissive=force_permissive,
setting_properties=setting_properties)
if props:
2015-12-31 18:35:31 +01:00
if not_raises:
return props
2015-12-31 18:20:36 +01:00
raise props
def _is_meta(self, opt, path, session):
context = self._getcontext()
setting = context.cfgimpl_get_settings()
2015-10-29 09:03:13 +01:00
self_properties = setting._getproperties(opt, path, read_write=False)
if 'frozen' in self_properties and 'force_default_on_freeze' in self_properties:
return False
if self._p_.getowner(path, owners.default, session, only_default=True) is not owners.default:
return False
if context.cfgimpl_get_meta() is not None:
return True
return False
def getowner(self, opt, index=None, force_permissive=False, session=None):
2013-08-21 11:09:11 +02:00
"""
retrieves the option's owner
2013-08-21 14:52:48 +02:00
:param opt: the `option.Option` object
:param force_permissive: behaves as if the permissive property
was present
2013-08-21 11:09:11 +02:00
:returns: a `setting.owners.Owner` object
"""
2014-06-19 23:22:39 +02:00
if isinstance(opt, SymLinkOption) and \
not isinstance(opt, DynSymLinkOption):
2014-11-10 09:13:44 +01:00
opt = opt._impl_getopt()
2014-06-19 23:22:39 +02:00
path = opt.impl_getpath(self._getcontext())
return self._getowner(opt, path, session, index=index, force_permissive=force_permissive)
def _getowner(self, opt, path, session, validate_properties=True,
force_permissive=False, validate_meta=undefined,
2015-11-19 22:25:00 +01:00
self_properties=undefined, only_default=False,
index=None):
2015-05-03 09:56:03 +02:00
"""get owner of an option
"""
if session is None:
session = self._p_.getsession()
if not isinstance(opt, Option) and not isinstance(opt,
DynSymLinkOption):
raise ConfigError(_('owner only avalaible for an option'))
context = self._getcontext()
2015-10-29 09:03:13 +01:00
if self_properties is undefined:
self_properties = context.cfgimpl_get_settings()._getproperties(
2015-05-03 09:56:03 +02:00
opt, path, read_write=False)
2015-10-29 09:03:13 +01:00
if 'frozen' in self_properties and 'force_default_on_freeze' in self_properties:
return owners.default
if validate_properties:
self._get_cached_value(opt, path, True, force_permissive, None, True,
self_properties=self_properties, session=session)
owner = self._p_.getowner(path, owners.default, session, only_default=only_default, index=index)
if validate_meta is undefined:
if opt.impl_is_master_slaves('slave'):
master = opt.impl_get_master_slaves().getmaster(opt)
masterp = master.impl_getpath(context)
validate_meta = self._is_meta(opt, masterp, session)
else:
validate_meta = True
if validate_meta:
meta = context.cfgimpl_get_meta()
if owner is owners.default and meta is not None:
owner = meta.cfgimpl_get_values()._getowner(opt, path, session,
2015-11-19 22:25:00 +01:00
validate_properties=validate_properties,
force_permissive=force_permissive,
self_properties=self_properties,
only_default=only_default, index=index)
2013-05-02 11:34:57 +02:00
return owner
2013-04-03 12:20:26 +02:00
2016-01-25 16:22:28 +01:00
def setowner(self, opt, owner, index=None):
2013-08-21 11:09:11 +02:00
"""
sets a owner to an option
2013-08-21 14:52:48 +02:00
:param opt: the `option.Option` object
2013-08-21 11:09:11 +02:00
:param owner: a valid owner, that is a `setting.owners.Owner` object
"""
2014-06-19 23:22:39 +02:00
if not isinstance(owner, owners.Owner): # pragma: optional cover
2013-04-13 23:09:05 +02:00
raise TypeError(_("invalid generic owner {0}").format(str(owner)))
2014-06-19 23:22:39 +02:00
path = opt.impl_getpath(self._getcontext())
session = self._p_.getsession()
if not self._p_.hasvalue(path, session): # pragma: optional cover
2013-08-14 23:06:31 +02:00
raise ConfigError(_('no value for {0} cannot change owner to {1}'
'').format(path, owner))
2015-12-30 22:32:07 +01:00
props = self._getcontext().cfgimpl_get_settings().validate_properties(opt,
False,
True,
2016-01-25 16:22:28 +01:00
path,
index=index)
2015-12-30 22:32:07 +01:00
if props:
raise props
self._p_.setowner(path, owner, session, index=index)
2013-04-03 12:20:26 +02:00
def is_default_owner(self, opt, validate_properties=True,
validate_meta=True, index=None,
force_permissive=False):
2013-04-03 12:20:26 +02:00
"""
:param config: *must* be only the **parent** config
(not the toplevel config)
:return: boolean
"""
2014-06-19 23:22:39 +02:00
path = opt.impl_getpath(self._getcontext())
return self._is_default_owner(opt, path, session=None,
validate_properties=validate_properties,
validate_meta=validate_meta, index=index,
force_permissive=force_permissive)
def _is_default_owner(self, opt, path, session, validate_properties=True,
2015-11-19 22:25:00 +01:00
validate_meta=True, self_properties=undefined,
index=None, force_permissive=False):
2015-11-19 22:25:00 +01:00
if not opt.impl_is_master_slaves('slave'):
index = None
d = self._getowner(opt, path, session, validate_properties=validate_properties,
2015-11-19 22:25:00 +01:00
validate_meta=validate_meta,
self_properties=self_properties, only_default=True,
index=index, force_permissive=force_permissive)
2015-11-19 22:25:00 +01:00
return d == owners.default
2013-02-21 17:07:00 +01:00
def reset_cache(self, only_expired):
2013-08-21 11:09:11 +02:00
"""
clears the cache if necessary
"""
if only_expired:
2013-09-07 17:25:22 +02:00
self._p_.reset_expired_cache(int(time()))
else:
2013-09-07 17:25:22 +02:00
self._p_.reset_all_cache()
2013-04-18 23:06:14 +02:00
# information
def set_information(self, key, value):
"""updates the information's attribute
:param key: information's key (ex: "help", "doc"
:param value: information's value (ex: "the help string")
"""
self._p_.set_information(key, value)
def get_information(self, key, default=undefined):
"""retrieves one information's item
:param key: the item string (ex: "help")
"""
2015-12-28 22:00:46 +01:00
return self._p_.get_information(key, default)
2015-10-29 09:03:13 +01:00
def mandatory_warnings(self, force_permissive=False, validate=True):
"""convenience function to trace Options that are mandatory and
where no value has been set
2015-10-29 09:03:13 +01:00
:param force_permissive: do raise with permissives properties
:type force_permissive: `bool`
:param validate: validate value when calculating properties
:type validate: `bool`
2015-10-29 09:03:13 +01:00
:returns: generator of mandatory Option's path
"""
2015-10-29 09:03:13 +01:00
context = self._getcontext()
settings = context.cfgimpl_get_settings()
2015-12-14 23:37:15 +01:00
setting_properties = context.cfgimpl_get_settings()._getproperties()
setting_properties.add('mandatory')
2015-10-29 09:03:13 +01:00
def _mandatory_warnings(description, currpath=None):
if currpath is None:
currpath = []
2015-05-03 09:56:03 +02:00
for opt in description._impl_getchildren(context=context):
2015-10-29 09:03:13 +01:00
name = opt.impl_getname()
path = '.'.join(currpath + [name])
2014-06-19 23:22:39 +02:00
if opt.impl_is_optiondescription():
2015-12-30 22:32:07 +01:00
if not settings.validate_properties(opt, True, False, path=path,
force_permissive=force_permissive,
setting_properties=setting_properties):
2015-10-29 09:03:13 +01:00
for path in _mandatory_warnings(opt, currpath + [name]):
yield path
else:
if isinstance(opt, SymLinkOption) and \
not isinstance(opt, DynSymLinkOption):
continue
self_properties = settings._getproperties(opt, path,
2015-10-29 09:03:13 +01:00
read_write=False,
setting_properties=setting_properties)
if 'mandatory' in self_properties:
err = self._get_cached_value(opt, path=path,
2016-01-03 21:18:52 +01:00
trusted_cached_properties=False,
force_permissive=force_permissive,
setting_properties=setting_properties,
self_properties=self_properties,
validate=validate, returns_raise=True)
if not isinstance(err, Exception):
pass
elif isinstance(err, PropertiesOptionError):
2015-10-29 09:03:13 +01:00
if err.proptype == ['mandatory']:
yield path
2016-01-03 21:18:52 +01:00
elif isinstance(err, ConfigError):
2015-10-29 09:03:13 +01:00
if validate:
raise err
else:
#assume that uncalculated value is an empty value
yield path
2016-01-03 21:18:52 +01:00
else:
raise err
2015-10-29 09:03:13 +01:00
descr = self._getcontext().cfgimpl_get_description()
2015-10-29 09:03:13 +01:00
for path in _mandatory_warnings(descr):
yield path
def force_cache(self):
"""parse all option to force data in cache
"""
context = self.context()
if not 'cache' in context.cfgimpl_get_settings():
raise ConfigError(_('can force cache only if cache '
'is actived in config'))
#remove all cached properties and value to update "expired" time
context.cfgimpl_reset_cache()
for path in context.cfgimpl_get_description().impl_getpaths(
include_groups=True):
2016-01-03 21:18:52 +01:00
err = context.getattr(path, returns_raise=True)
if isinstance(err, Exception) and not isinstance(err, PropertiesOptionError):
raise err
2013-09-22 20:57:52 +02:00
def __getstate__(self):
return {'_p_': self._p_}
def _impl_setstate(self, storage):
self._p_._storage = storage
def __setstate__(self, states):
self._p_ = states['_p_']
2013-09-24 23:19:20 +02:00
# ____________________________________________________________
# multi types
class Multi(list):
"""multi options values container
that support item notation for the values of multi options"""
2014-04-25 22:57:08 +02:00
__slots__ = ('opt', 'path', 'context', '__weakref__')
2013-04-03 12:20:26 +02:00
def __init__(self, value, context, opt, path):
"""
2013-04-18 23:06:14 +02:00
:param value: the Multi wraps a list value
2013-04-03 12:20:26 +02:00
:param context: the home config that has the values
:param opt: the option object that have this Multi value
2014-04-25 22:57:08 +02:00
:param path: path of the option
"""
2014-04-25 22:57:08 +02:00
if value is None:
value = []
2014-06-19 23:22:39 +02:00
if not opt.impl_is_submulti() and isinstance(value, Multi): # pragma: optional cover
2014-04-25 22:57:08 +02:00
raise ValueError(_('{0} is already a Multi ').format(
opt.impl_getname()))
self.opt = opt
self.path = path
2014-06-19 23:22:39 +02:00
if not isinstance(context, weakref.ReferenceType): # pragma: optional cover
raise ValueError('context must be a Weakref')
2013-04-03 12:20:26 +02:00
self.context = context
2013-04-18 23:06:14 +02:00
if not isinstance(value, list):
2014-04-25 22:57:08 +02:00
if not '_index' in self.__slots__ and opt.impl_is_submulti():
value = [[value]]
else:
value = [value]
elif value != [] and not '_index' in self.__slots__ and \
opt.impl_is_submulti() and not isinstance(value[0], list):
2013-04-18 23:06:14 +02:00
value = [value]
super(Multi, self).__init__(value)
2014-04-25 22:57:08 +02:00
if opt.impl_is_submulti():
if not '_index' in self.__slots__:
for idx, val in enumerate(self):
if not isinstance(val, SubMulti):
super(Multi, self).__setitem__(idx, SubMulti(val,
context,
opt, path,
idx))
self[idx].submulti = weakref.ref(self)
2013-04-18 23:06:14 +02:00
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()
2014-06-19 23:22:39 +02:00
if context is None: # pragma: optional cover
raise ConfigError(_('the context does not exist anymore'))
return context
def __setitem__(self, index, value):
2014-04-25 22:57:08 +02:00
self._setitem(index, value)
2015-04-18 22:53:45 +02:00
def _setitem(self, index, value, validate=True):
2015-05-03 09:56:03 +02:00
context = self._getcontext()
setting = context.cfgimpl_get_settings()
setting_properties = setting._getproperties(read_write=False)
if index < 0:
index = self.__len__() + index
2015-05-03 09:56:03 +02:00
if 'validator' in setting_properties and validate:
session = context.cfgimpl_get_values()._p_.getsession()
fake_context = context._gen_fake_values(session)
fake_multi = fake_context.cfgimpl_get_values()._get_cached_value(
2015-04-18 22:53:45 +02:00
self.opt, path=self.path, validate=False)
fake_multi._setitem(index, value, validate=False)
self._validate(value, fake_context, index, True)
#assume not checking mandatory property
super(Multi, self).__setitem__(index, value)
self._store(index=index)
2014-04-25 22:57:08 +02:00
#def __repr__(self, *args, **kwargs):
# return super(Multi, self).__repr__(*args, **kwargs)
2014-04-17 18:47:48 +02:00
def __getitem__(self, index):
value = super(Multi, self).__getitem__(index)
if isinstance(value, PropertiesOptionError):
raise value
return value
2014-04-25 22:57:08 +02:00
def _get_validated_value(self, index):
values = self._getcontext().cfgimpl_get_values()
return values._get_validated_value(self.opt, self.path,
2015-12-14 23:37:15 +01:00
True, False, True,
2014-04-25 22:57:08 +02:00
index=index)
2014-04-17 18:47:48 +02:00
2016-01-25 16:22:28 +01:00
def append(self, value=undefined, force=False, setitem=True, validate=True,
force_permissive=False):
"""the list value can be updated (appened)
only if the option is a master
"""
if not force and self.opt.impl_is_master_slaves('slave'): # pragma: optional cover
raise SlaveError(_("cannot append a value on a multi option {0}"
" which is a slave").format(self.opt.impl_getname()))
index = self.__len__()
if value is undefined:
value = self._get_validated_value(index)
if validate and value not in [None, undefined]:
context = self._getcontext()
setting = context.cfgimpl_get_settings()
setting_properties = setting._getproperties(read_write=False)
if 'validator' in setting_properties:
session = context.cfgimpl_get_values()._p_.getsession()
fake_context = context._gen_fake_values(session)
fake_multi = fake_context.cfgimpl_get_values()._get_cached_value(
2016-01-25 16:22:28 +01:00
self.opt, path=self.path, validate=False,
force_permissive=force_permissive)
fake_multi.append(value, validate=False, force=True,
setitem=setitem)
self._validate(value, fake_context, index, True)
2014-04-25 22:57:08 +02:00
if not '_index' in self.__slots__ and self.opt.impl_is_submulti():
if not isinstance(value, SubMulti):
value = SubMulti(value, self.context, self.opt, self.path, index)
value.submulti = weakref.ref(self)
2013-02-22 11:09:17 +01:00
super(Multi, self).append(value)
if setitem:
2015-11-19 22:25:00 +01:00
self._store(force=force)
2013-02-22 11:09:17 +01:00
def append_properties_error(self, err):
super(Multi, self).append(err)
def sort(self, cmp=None, key=None, reverse=False):
if self.opt.impl_is_master_slaves():
raise SlaveError(_("cannot sort multi option {0} if master or slave"
"").format(self.opt.impl_getname()))
if sys.version_info[0] >= 3:
if cmp is not None:
raise ValueError(_('cmp is not permitted in python v3 or '
'greater'))
super(Multi, self).sort(key=key, reverse=reverse)
else:
super(Multi, self).sort(cmp=cmp, key=key, reverse=reverse)
2014-04-25 22:57:08 +02:00
self._store()
def reverse(self):
if self.opt.impl_is_master_slaves():
raise SlaveError(_("cannot reverse multi option {0} if master or "
"slave").format(self.opt.impl_getname()))
super(Multi, self).reverse()
2014-04-25 22:57:08 +02:00
self._store()
2015-04-18 22:53:45 +02:00
def insert(self, index, value, validate=True):
if self.opt.impl_is_master_slaves():
raise SlaveError(_("cannot insert multi option {0} if master or "
"slave").format(self.opt.impl_getname()))
2015-05-03 09:56:03 +02:00
context = self._getcontext()
setting = setting = context.cfgimpl_get_settings()
setting_properties = setting._getproperties(read_write=False)
if 'validator' in setting_properties and validate and value is not None:
session = context.cfgimpl_get_values()._p_.getsession()
fake_context = context._gen_fake_values(session)
fake_multi = fake_context.cfgimpl_get_values()._get_cached_value(
2015-04-18 22:53:45 +02:00
self.opt, path=self.path, validate=False)
fake_multi.insert(index, value, validate=False)
self._validate(value, fake_context, index, True)
super(Multi, self).insert(index, value)
2014-04-25 22:57:08 +02:00
self._store()
2015-04-18 22:53:45 +02:00
def extend(self, iterable, validate=True):
if self.opt.impl_is_master_slaves():
raise SlaveError(_("cannot extend multi option {0} if master or "
"slave").format(self.opt.impl_getname()))
2015-12-28 22:00:46 +01:00
index = getattr(self, '_index', None)
2015-05-03 09:56:03 +02:00
context = self._getcontext()
setting = context.cfgimpl_get_settings()
setting_properties = setting._getproperties(read_write=False)
if 'validator' in setting_properties and validate:
session = context.cfgimpl_get_values()._p_.getsession()
fake_context = context._gen_fake_values(session)
fake_multi = fake_context.cfgimpl_get_values()._get_cached_value(
2015-04-18 22:53:45 +02:00
self.opt, path=self.path, validate=False)
fake_multi.extend(iterable, validate=False)
self._validate(iterable, fake_context, index)
super(Multi, self).extend(iterable)
2014-04-25 22:57:08 +02:00
self._store()
2015-04-18 22:53:45 +02:00
def _validate(self, value, fake_context, force_index, submulti=False):
err = self.opt.impl_validate(value, context=fake_context,
force_index=force_index)
if err:
raise err
2013-02-22 11:09:17 +01:00
def pop(self, index, force=False):
"""the list value can be updated (poped)
only if the option is a master
:param index: remove item a index
:type index: int
:param force: force pop item (withoud check master/slave)
:type force: boolean
:returns: item at index
"""
context = self._getcontext()
2013-02-22 11:09:17 +01:00
if not force:
2014-06-19 23:22:39 +02:00
if self.opt.impl_is_master_slaves('slave'): # pragma: optional cover
2013-04-19 20:10:55 +02:00
raise SlaveError(_("cannot pop a value on a multi option {0}"
" which is a slave").format(self.opt.impl_getname()))
if self.opt.impl_is_master_slaves('master'):
2014-06-19 23:22:39 +02:00
self.opt.impl_get_master_slaves().pop(self.opt,
context.cfgimpl_get_values(), index)
#set value without valid properties
ret = super(Multi, self).pop(index)
2015-11-19 22:25:00 +01:00
self._store(force=force)
2013-08-14 23:06:31 +02:00
return ret
2014-04-25 22:57:08 +02:00
def _store(self, force=False, index=None):
2015-12-31 18:20:36 +01:00
values = self._getcontext().cfgimpl_get_values()
if not force:
#FIXME could get properties an pass it
2016-01-06 22:37:11 +01:00
values.validate(self.opt, self, self.path,
valid_masterslave=False)
values._setvalue(self.opt, self.path, self, index=index)
2014-04-25 22:57:08 +02:00
class SubMulti(Multi):
__slots__ = ('_index', 'submulti')
def __init__(self, value, context, opt, path, index):
"""
:param index: index (only for slave with submulti)
:type index: `int`
"""
self._index = index
super(SubMulti, self).__init__(value, context, opt, path)
def append(self, value=undefined):
super(SubMulti, self).append(value, force=True)
def pop(self, index):
return super(SubMulti, self).pop(index, force=True)
def __setitem__(self, index, value):
self._setitem(index, value)
def _store(self, force=False, index=None):
2014-04-25 22:57:08 +02:00
#force is unused here
2015-12-31 18:20:36 +01:00
values = self._getcontext().cfgimpl_get_values()
values.validate(self.opt, self, self.path, valid_masterslave=False)
values._setvalue(self.opt, self.path, self.submulti())
2014-04-25 22:57:08 +02:00
2015-04-18 22:53:45 +02:00
def _validate(self, value, fake_context, force_index, submulti=False):
2014-04-25 22:57:08 +02:00
if value is not None:
if submulti is False:
2015-04-18 22:53:45 +02:00
super(SubMulti, self)._validate(value, fake_context,
force_index, submulti)
2014-04-25 22:57:08 +02:00
else:
err = self.opt.impl_validate(value, context=fake_context,
force_index=self._index,
force_submulti_index=force_index)
if err:
raise err
2014-04-25 22:57:08 +02:00
def _get_validated_value(self, index):
values = self._getcontext().cfgimpl_get_values()
return values._get_validated_value(self.opt, self.path,
2015-12-14 23:37:15 +01:00
True, False, True,
2014-04-25 22:57:08 +02:00
index=index,
submulti_index=self._index)