tiramisu/tiramisu/value.py

900 lines
41 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
def _getdefaultvalue(self, opt, path, with_meta, index, submulti_index):
# 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,
index=index)
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:
try:
value = meta.cfgimpl_get_values(
)._get_cached_value(opt, path, index=index, from_masterslave=True)
if isinstance(value, Multi):
if index is not None:
value = value[index]
else:
value = list(value)
return value
except PropertiesOptionError:
pass
# 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:
try:
value = value[index]
except IndexError:
value = opt.impl_getdefault_multi()
return value
2013-02-07 16:20:21 +01:00
def _getvalue(self, opt, path, is_default, self_properties,
index=undefined, submulti_index=undefined, with_meta=True,
2015-11-19 22:25:00 +01:00
masterlen=undefined):
"""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
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, index)
2015-11-19 22:25:00 +01:00
else:
value = self._p_.getvalue(path)
if index is not None:
2015-11-19 22:25:00 +01:00
try:
return value[index]
except IndexError:
#value is smaller than expected
#so return default value
pass
else:
return value
return self._getdefaultvalue(opt, path, with_meta, index, submulti_index)
2015-11-19 22:25:00 +01:00
2013-08-14 23:06:31 +02:00
def get_modified_values(self):
context = self._getcontext()
if context._impl_descr is not None:
2014-07-06 15:31:57 +02:00
for opt, path in context.cfgimpl_get_description(
)._get_force_store_value():
self._getowner(opt, path, force_permissive=True)
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):
return self._p_.hasvalue(path)
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
def reset(self, opt, path=None, validate=True):
if path is None:
2014-06-19 23:22:39 +02:00
path = opt.impl_getpath(self._getcontext())
context = self._getcontext()
if validate:
context.cfgimpl_get_settings().validate_properties(opt, False,
True, path)
hasvalue = self._contains(path)
2015-05-03 09:56:03 +02:00
setting = context.cfgimpl_get_settings()
setting_properties = setting._getproperties(read_write=False)
if 'validator' in setting_properties and validate and hasvalue:
fake_context = context._gen_fake_values()
fake_value = fake_context.cfgimpl_get_values()
fake_value.reset(opt, path, validate=False)
opt.impl_validate(getattr(fake_context, path),
fake_context, 'validator' in setting_properties)
context.cfgimpl_reset_cache()
if opt.impl_is_master_slaves('master'):
opt.impl_get_master_slaves().reset(opt, self)
if hasvalue:
self._p_.resetvalue(path)
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,
force_permissive=False, force_properties=None,
validate_properties=True,
setting_properties=undefined, self_properties=undefined,
index=None, from_masterslave=False, with_meta=True,
masterlen=undefined):
2015-10-29 09:03:13 +01:00
untrusted_cached_properties = force_properties is None
context = self._getcontext()
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-10-29 09:03:13 +01:00
setting_properties = context.cfgimpl_get_settings(
2015-05-03 09:56:03 +02:00
)._getproperties(read_write=False)
2015-10-29 09:03:13 +01:00
if self_properties is undefined:
self_properties = context.cfgimpl_get_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-10-29 09:03:13 +01:00
if not untrusted_cached_properties:
# revalidate properties (because not default properties)
context.cfgimpl_get_settings().validate_properties(opt, False, False, value=value,
path=path,
force_permissive=force_permissive,
force_properties=force_properties,
setting_properties=setting_properties,
self_properties=self_properties,
index=index)
2013-04-18 23:06:14 +02:00
return value
if not from_masterslave and opt.impl_is_master_slaves():
val = opt.impl_get_master_slaves().getitem(self, opt, path,
validate,
force_permissive,
force_properties,
validate_properties,
setting_properties=setting_properties,
index=index,
self_properties=self_properties)
else:
val = self._get_validated_value(opt, path, validate,
force_permissive,
force_properties,
validate_properties,
setting_properties=setting_properties,
self_properties=self_properties,
with_meta=with_meta,
masterlen=masterlen,
index=index)
# cache doesn't work with SubMulti yet
if not isinstance(val, SubMulti) and 'cache' in setting_properties and validate and validate_properties \
and force_permissive is False and force_properties is None:
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,
force_properties, validate_properties,
index=None, submulti_index=undefined,
2015-05-03 09:56:03 +02:00
with_meta=True, setting_properties=undefined,
2015-11-19 22:25:00 +01:00
self_properties=undefined, masterlen=undefined):
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)
is_default = self._is_default_owner(opt, path,
validate_properties=False,
validate_meta=False,
2015-11-19 22:25:00 +01:00
self_properties=self_properties,
index=index)
try:
value = self._getvalue(opt, path, is_default, self_properties,
index=index, submulti_index=submulti_index,
with_meta=with_meta,
2015-11-19 22:25:00 +01:00
masterlen=masterlen)
config_error = None
except ConfigError as err:
# 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.
# cannot assign config_err directly in python 3.3
config_error = err
# value is not set, for 'undefined' (cannot set None because of
# mandatory property)
value = undefined
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
2015-04-18 23:46:37 +02:00
try:
opt.impl_validate(value, context,
'validator' in setting_properties,
force_index=force_index,
force_submulti_index=force_submulti_index)
except ValueError, err:
config_error = err
value = None
2015-10-29 09:03:13 +01:00
if is_default and 'force_store_value' in self_properties:
2014-04-18 21:33:15 +02:00
if isinstance(value, Multi):
item = list(value)
else:
item = value
self.setitem(opt, item, path, is_write=False,
force_permissive=force_permissive)
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
setting.validate_properties(opt, False, False, value=val_props,
path=path,
force_permissive=force_permissive,
force_properties=force_properties,
2015-10-29 09:03:13 +01:00
setting_properties=setting_properties,
2015-11-19 22:25:00 +01:00
self_properties=self_properties,
index=index)
if config_error is not None:
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,
is_write=True):
2013-08-21 11:09:11 +02:00
# is_write is, for example, used with "force_store_value"
# 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:
fake_context = context._gen_fake_values()
fake_context.cfgimpl_get_values()._setitem(opt, value, path,
force_permissive,
is_write,
setting_properties)
opt.impl_validate(value, fake_context)
2015-04-18 22:53:45 +02:00
self._setitem(opt, value, path, force_permissive, is_write,
2015-05-03 09:56:03 +02:00
setting_properties, validate_properties=False)
2015-04-18 22:53:45 +02:00
def _setitem(self, opt, value, path, force_permissive, is_write,
2015-05-03 09:56:03 +02:00
setting_properties, validate_properties=True):
if opt.impl_is_master_slaves():
opt.impl_get_master_slaves().setitem(self, opt, value, path)
self._setvalue(opt, path, value, force_permissive=force_permissive,
is_write=is_write,
2015-05-03 09:56:03 +02:00
setting_properties=setting_properties,
validate_properties=validate_properties)
def _setvalue(self, opt, path, value, force_permissive=False,
is_write=True, validate_properties=True,
2015-11-19 22:25:00 +01:00
setting_properties=undefined, index=None):
context = self._getcontext()
context.cfgimpl_reset_cache()
if validate_properties:
setting = context.cfgimpl_get_settings()
setting.validate_properties(opt, False, is_write,
value=value, path=path,
force_permissive=force_permissive,
2015-10-29 09:03:13 +01:00
setting_properties=setting_properties)
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)
2015-05-03 09:56:03 +02:00
owner = context.cfgimpl_get_settings().getowner()
2015-11-19 22:25:00 +01:00
if opt.impl_is_master_slaves('slave') and index is None:
try:
self._p_.resetvalue(path)
except ValueError:
pass
for idx, val in enumerate(value):
self._p_.setvalue(path, val, owner, idx)
else:
self._p_.setvalue(path, value, owner, index)
def _is_meta(self, opt, path):
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
2015-11-19 22:25:00 +01:00
if self._p_.getowner(path, owners.default, only_default=True) is not owners.default:
return False
if context.cfgimpl_get_meta() is not None:
return True
return False
2015-11-19 22:25:00 +01:00
def getowner(self, opt, index=None, force_permissive=False):
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())
2015-11-19 22:25:00 +01:00
return self._getowner(opt, path, index=index, force_permissive=force_permissive)
def _getowner(self, opt, path, 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 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)
2015-11-19 22:25:00 +01:00
owner = self._p_.getowner(path, owners.default, 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)
else:
validate_meta = True
if validate_meta:
meta = context.cfgimpl_get_meta()
if owner is owners.default and meta is not None:
2015-11-19 22:25:00 +01:00
owner = meta.cfgimpl_get_values()._getowner(opt, path,
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
def setowner(self, opt, owner):
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())
if not self._p_.hasvalue(path): # 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))
self._getcontext().cfgimpl_get_settings().validate_properties(opt,
False,
True,
path)
self._p_.setowner(path, owner)
2013-04-03 12:20:26 +02:00
def is_default_owner(self, opt, validate_properties=True,
2015-11-19 22:25:00 +01:00
validate_meta=True, index=None):
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,
validate_properties=validate_properties,
2015-11-19 22:25:00 +01:00
validate_meta=validate_meta, index=index)
def _is_default_owner(self, opt, path, validate_properties=True,
2015-11-19 22:25:00 +01:00
validate_meta=True, self_properties=undefined,
index=None):
if not opt.impl_is_master_slaves('slave'):
index = None
d = self._getowner(opt, path, validate_properties,
validate_meta=validate_meta,
self_properties=self_properties, only_default=True,
index=index)
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")
"""
try:
return self._p_.get_information(key)
2014-06-19 23:22:39 +02:00
except ValueError: # pragma: optional cover
if default is not undefined:
return default
else:
raise ValueError(_("information's item"
" not found: {0}").format(key))
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()
setting_properties = context.cfgimpl_get_settings()._getproperties(
read_write=False)
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():
try:
2015-10-29 09:03:13 +01:00
settings.validate_properties(opt, True, False, path=path,
force_permissive=force_permissive,
setting_properties=setting_properties)
except PropertiesOptionError as err:
2015-10-29 09:03:13 +01:00
pass
else:
for path in _mandatory_warnings(opt, currpath + [name]):
yield path
else:
if isinstance(opt, SymLinkOption) and \
not isinstance(opt, DynSymLinkOption):
true_opt = opt._impl_getopt()
true_path = descr.impl_get_path_by_opt(true_opt)
else:
true_opt = opt
true_path = path
#FIXME attention c'est réutilisé donc jamais complet ??
self_properties = settings._getproperties(true_opt, true_path,
read_write=False,
setting_properties=setting_properties)
if 'mandatory' in self_properties:
try:
self._get_cached_value(true_opt, path=true_path,
force_properties=frozenset(('mandatory',)),
force_permissive=force_permissive,
setting_properties=setting_properties,
self_properties=self_properties,
validate=validate)
2015-10-29 09:03:13 +01:00
except PropertiesOptionError as err:
if err.proptype == ['mandatory']:
yield path
except ConfigError as err:
if validate:
raise err
else:
#assume that uncalculated value is an empty value
yield path
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):
try:
context.getattr(path)
except PropertiesOptionError:
pass
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 'validator' in setting_properties and validate:
fake_context = context._gen_fake_values()
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)
2015-11-19 22:25:00 +01:00
self._store()
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,
True, False, None, True,
index=index)
2014-04-17 18:47:48 +02:00
2015-04-18 22:53:45 +02:00
def append(self, value=undefined, force=False, setitem=True, validate=True):
"""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:
fake_context = context._gen_fake_values()
fake_multi = fake_context.cfgimpl_get_values()._get_cached_value(
self.opt, path=self.path, validate=False)
fake_multi.append(value, validate=False, force=True)
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:
fake_context = context._gen_fake_values()
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()))
2014-04-25 22:57:08 +02:00
try:
index = self._index
except:
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:
fake_context = context._gen_fake_values()
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):
self.opt.impl_validate(value, context=fake_context,
force_index=force_index)
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):
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path,
self,
validate_properties=not force)
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):
#force is unused here
self._getcontext().cfgimpl_get_values()._setvalue(self.opt,
self.path,
self.submulti())
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:
2015-04-18 22:53:45 +02:00
self.opt.impl_validate(value, context=fake_context,
2014-04-25 22:57:08 +02:00
force_index=self._index,
force_submulti_index=force_index)
def _get_validated_value(self, index):
values = self._getcontext().cfgimpl_get_values()
return values._get_validated_value(self.opt, self.path,
True, False, None, True,
index=index,
submulti_index=self._index)