tiramisu/tiramisu/value.py

979 lines
46 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"
2017-07-04 19:59:42 +02:00
# Copyright (C) 2013-2017 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 _
2017-07-24 20:39:01 +02:00
from .option import 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()
2017-02-11 17:22:50 +01:00
if context is None:
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
2017-07-16 23:11:12 +02:00
def _getdefaultvalue(self, opt, path, with_meta, index, submulti_index, validate,
_orig_context=undefined):
if _orig_context is undefined:
_orig_context = self._getcontext()
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,
2017-07-16 23:11:12 +02:00
from_masterslave=True, _orig_context=_orig_context)
2016-01-03 21:18:52 +01:00
if isinstance(value, Exception):
2017-02-11 17:22:50 +01:00
if not isinstance(value, PropertiesOptionError): # pragma: no cover
2016-01-03 21:18:52 +01:00
raise value
else:
if isinstance(value, Multi):
2017-07-21 18:03:34 +02:00
new_value = []
for val in value:
if isinstance(val, SubMulti):
val = list(val)
new_value.append(val)
value = new_value
del new_value
return value
2017-07-16 23:11:12 +02:00
# if value has callback and is not set
if opt.impl_has_callback():
callback, callback_params = opt.impl_get_callback()
value = carry_out_calculation(opt, context=_orig_context,
callback=callback,
callback_params=callback_params,
index=index, validate=validate)
_orig_context.cfgimpl_reset_cache(opt=opt, path=path, only=('values',))
2017-07-16 23:11:12 +02:00
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
if not opt.impl_is_submulti() and len(value) > index:
return value[index]
else:
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,
2017-07-16 23:11:12 +02:00
with_meta, masterlen, session, validate, _orig_context):
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
2017-04-19 21:47:12 +02:00
if index is None or not opt.impl_is_master_slaves('slave'):
_index = None
else:
_index = index
2017-07-11 22:31:58 +02:00
owner, value = self._p_.getowner(path, owners.default, session, only_default=True,
index=_index, with_value=True)
is_default = owner == owners.default
2015-11-19 22:25:00 +01:00
if not is_default and not force_default:
2017-07-11 22:31:58 +02:00
if index is not None and not opt.impl_is_master_slaves('slave'):
if len(value) > index:
return value[index]
#value is smaller than expected
#so return default value
2015-11-19 22:25:00 +01:00
else:
2017-07-11 22:31:58 +02:00
return value
return self._getdefaultvalue(opt, path, with_meta, index,
2017-07-16 23:11:12 +02:00
submulti_index, validate, _orig_context)
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
2017-07-16 23:11:12 +02:00
def reset(self, opt, path=None, validate=True, _setting_properties=None, _commit=True):
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)
2016-10-14 22:20:14 +02:00
ret = fake_value._get_cached_value(opt, path,
setting_properties=_setting_properties,
check_frozen=True)
if isinstance(ret, Exception):
raise ret
if opt.impl_is_master_slaves('master'):
2017-07-16 23:11:12 +02:00
opt.impl_get_master_slaves().reset(opt, self, _setting_properties, _commit=_commit)
if hasvalue:
if 'force_store_value' in setting._getproperties(opt=opt,
path=path,
setting_properties=_setting_properties,
read_write=False,
apply_requires=False):
2017-01-26 21:01:54 +01:00
value = self._getdefaultvalue(opt, path, True, undefined, undefined, validate)
2017-02-11 17:22:50 +01:00
if isinstance(value, Exception): # pragma: no cover
2016-10-14 22:20:14 +02:00
raise value
2017-07-16 23:11:12 +02:00
self._setvalue(opt, path, value, force_owner=owners.forced, commit=_commit)
else:
2017-07-16 23:11:12 +02:00
self._p_.resetvalue(path, session, _commit)
2017-07-13 22:04:06 +02:00
context.cfgimpl_reset_cache(opt=opt, path=path, only=('values', 'properties'))
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 = value is None or (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"
2016-11-20 14:32:06 +01:00
return self._get_cached_value(opt)
2013-04-18 23:06:14 +02:00
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,
2017-07-16 23:11:12 +02:00
session=None, display_warnings=True, _orig_context=undefined):
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)
2017-07-16 23:11:12 +02:00
if 'cache' in setting_properties and self._p_.hascache(path, index) and \
_orig_context is undefined:
if 'expire' in setting_properties:
2013-09-07 17:25:22 +02:00
ntime = int(time())
is_cached, value = self._p_.getcache(path, ntime, None)
if index:
value = value[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-10-14 22:20:14 +02:00
return 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,
2017-05-05 21:40:44 +02:00
self_properties=self_properties,
check_frozen=check_frozen)
else:
val = self._get_validated_value(opt, path, validate,
force_permissive,
validate_properties,
setting_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,
session=session,
2017-07-16 23:11:12 +02:00
display_warnings=display_warnings,
_orig_context=_orig_context)
2016-01-06 22:37:11 +01:00
if isinstance(val, Exception):
2016-10-14 22:20:14 +02:00
return val
# cache doesn't work with SubMulti yet
2017-10-14 13:33:25 +02:00
if index is None and not isinstance(val, SubMulti) and 'cache' in setting_properties and \
2015-12-17 22:41:57 +01:00
validate and validate_properties and force_permissive is False \
2017-07-16 23:11:12 +02:00
and trusted_cached_properties is True and _orig_context is undefined:
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, None)
2013-04-18 23:06:14 +02:00
return val
def _get_validated_value(self, opt, path, validate, force_permissive,
validate_properties, setting_properties,
2016-11-19 19:16:31 +01:00
self_properties,
index=None, submulti_index=undefined,
with_meta=True,
masterlen=undefined,
2016-11-19 19:16:31 +01:00
check_frozen=False,
2017-07-16 23:11:12 +02:00
session=None, display_warnings=True,
_orig_context=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()
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,
2017-07-16 23:11:12 +02:00
with_meta, masterlen, session, validate, _orig_context)
2016-01-03 21:18:52 +01:00
if isinstance(value, Exception):
2016-11-20 14:32:06 +01:00
value_error = True
2016-01-03 21:18:52 +01:00
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
2017-02-11 17:22:50 +01:00
else: # pragma: no cover
2016-01-03 21:18:52 +01:00
raise value
2015-11-19 22:25:00 +01:00
else:
2016-11-20 14:32:06 +01:00
value_error = False
if opt.impl_is_multi():
2017-02-11 17:22:50 +01:00
if 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,
2017-02-11 17:22:50 +01:00
index)
2014-04-25 22:57:08 +02:00
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,
2017-02-11 17:22:50 +01:00
force_index=index,
force_submulti_index=force_submulti_index,
2016-11-20 14:32:06 +01:00
display_error=True,
2017-07-11 22:31:58 +02:00
display_warnings=False,
setting_properties=setting_properties)
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-10-14 22:20:14 +02:00
return props
2016-11-20 14:32:06 +01:00
if not value_error and validate and display_warnings:
opt.impl_validate(value, context,
'validator' in setting_properties,
2017-02-11 17:22:50 +01:00
force_index=index,
2016-11-20 14:32:06 +01:00
force_submulti_index=force_submulti_index,
display_error=False,
2017-07-04 19:59:42 +02:00
display_warnings=display_warnings,
2017-07-11 22:31:58 +02:00
setting_properties=setting_properties)
if config_error is not None:
2016-10-14 22:20:14 +02:00
return config_error
return value
2017-02-11 17:22:50 +01:00
def __setitem__(self, opt, value):
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,
2017-07-11 22:31:58 +02:00
check_frozen=True, not_raises=False, index=None,
2017-07-16 23:11:12 +02:00
_setting_properties=undefined, _commit=True):
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()
2017-07-11 22:31:58 +02:00
if 'validator' in _setting_properties:
session = context.cfgimpl_get_values()._p_.getsession()
2017-07-16 10:57:43 +02:00
if opt._has_consistencies():
fake_context = context._gen_fake_values(session)
fake_values = fake_context.cfgimpl_get_values()
fake_values._setvalue(opt, path, value, index=index)
else:
fake_context = context
fake_values = self
props = fake_values.validate(opt, value, path,
check_frozen=check_frozen,
force_permissive=force_permissive,
2017-07-11 22:31:58 +02:00
setting_properties=_setting_properties,
2017-05-31 19:22:22 +02:00
session=session, not_raises=not_raises,
2017-10-14 13:33:25 +02:00
index=index, setitem=True)
2015-12-31 18:35:31 +01:00
if props and not_raises:
return props
2017-07-11 22:31:58 +02:00
err = opt.impl_validate(value, fake_context, display_warnings=False, force_index=index,
setting_properties=_setting_properties)
if err:
if not_raises:
return err
raise err
2017-07-11 22:31:58 +02:00
opt.impl_validate(value, fake_context, display_error=False,
setting_properties=_setting_properties)
2017-07-16 23:11:12 +02:00
self._setvalue(opt, path, value, index=index, commit=_commit)
2015-12-31 18:20:36 +01:00
2017-07-16 23:11:12 +02:00
def _setvalue(self, opt, path, value, force_owner=undefined, index=None, commit=True):
context = self._getcontext()
2017-07-13 22:04:06 +02:00
context.cfgimpl_reset_cache(opt=opt, path=path, only=('values', 'properties'))
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):
2017-05-31 19:22:22 +02:00
if not opt.impl_is_master_slaves('slave') or index is None:
value = list(value)
if opt.impl_is_submulti():
for idx, val in enumerate(value):
if isinstance(val, SubMulti):
value[idx] = list(val)
else:
value = value[index]
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:
2017-07-16 23:11:12 +02:00
self._p_.setvalue(path, value, owner, index, session, commit)
else:
2017-07-16 23:11:12 +02:00
self._p_.resetvalue(path, session, commit)
for idx, val in enumerate(value):
2017-07-16 23:11:12 +02:00
self._p_.setvalue(path, val, owner, idx, session, commit)
2015-11-19 22:25:00 +01:00
else:
2017-07-16 23:11:12 +02:00
self._p_.setvalue(path, value, owner, None, session, commit)
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,
2017-10-14 13:33:25 +02:00
not_raises=False, session=None, index=None, setitem=False):
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()
2017-10-14 13:33:25 +02:00
if opt.impl_is_master_slaves('master'):
masterlen = len(value)
slavelen = None
else:
masterlen = None
slavelen = len(value)
2017-07-21 18:03:34 +02:00
setitem = True
2017-10-14 13:33:25 +02:00
opt.impl_get_master_slaves().impl_validate(self._getcontext(), force_permissive,
setting_properties, masterlen=masterlen,
slavelen=slavelen, opt=opt, setitem=True)
#val = opt.impl_get_master_slaves().impl_validate(self, opt, len_value, path, session, setitem=setitem)
#if isinstance(val, Exception):
# return val
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,
2017-05-31 19:22:22 +02:00
setting_properties=setting_properties,
index=index)
2015-12-31 18:20:36 +01:00
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
2017-10-14 13:33:25 +02:00
def _is_meta(self, opt, path, session=None, force_permissive=False):
context = self._getcontext()
2017-10-14 13:33:25 +02:00
if context.cfgimpl_get_meta() is None:
return False
setting = context.cfgimpl_get_settings()
2015-10-29 09:03:13 +01:00
self_properties = setting._getproperties(opt, path, read_write=False)
2017-10-14 13:33:25 +02:00
if session is None:
session = self._p_.getsession()
return self.is_default_owner(opt, path=path, validate_properties=True,
validate_meta=False, index=None,
force_permissive=force_permissive)
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
"""
2017-07-24 20:39:01 +02:00
if opt._is_symlinkoption() and \
2014-06-19 23:22:39 +02:00
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:
2016-10-14 22:20:14 +02:00
value = self._get_cached_value(opt, path, True, force_permissive, None, True,
2017-04-19 21:47:12 +02:00
self_properties=self_properties, session=session,
index=index)
2016-10-14 22:20:14 +02:00
if isinstance(value, Exception):
raise value
2016-11-19 19:16:31 +01:00
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)
2017-10-14 13:33:25 +02:00
validate_meta = self._is_meta(master, masterp, session)
else:
validate_meta = True
2017-10-14 13:33:25 +02:00
if validate_meta and owner is owners.default:
meta = context.cfgimpl_get_meta()
2017-10-14 13:33:25 +02:00
if 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
"""
2017-02-11 17:22:50 +01:00
if not isinstance(owner, owners.Owner):
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()
2017-02-11 17:22:50 +01:00
if not self._p_.hasvalue(path, session):
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
2017-10-14 13:33:25 +02:00
def is_default_owner(self, opt, path=None, 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
"""
2017-10-14 13:33:25 +02:00
if path is None:
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):
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
# 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)
def del_information(self, key, raises=True):
self._p_.del_information(key, raises)
def mandatory_warnings(self, force_permissive=True):
"""convenience function to trace Options that are mandatory and
where no value has been set
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.update(['mandatory', 'empty'])
def _is_properties_option(err, path):
if not isinstance(err, Exception):
pass
elif isinstance(err, PropertiesOptionError):
if err.proptype == ['mandatory']:
return path
elif isinstance(err, ConfigError):
#assume that uncalculated value is an empty value
return path
else:
raise err
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=True,
2015-12-30 22:32:07 +01:00
setting_properties=setting_properties):
2015-10-29 09:03:13 +01:00
for path in _mandatory_warnings(opt, currpath + [name]):
yield path
else:
2017-07-24 20:39:01 +02:00
if opt._is_symlinkoption() and \
2015-10-29 09:03:13 +01:00
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 or 'empty' 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=True,
2016-01-03 21:18:52 +01:00
setting_properties=setting_properties,
self_properties=self_properties,
2016-10-14 22:20:14 +02:00
validate=True,
display_warnings=False)
if opt.impl_is_master_slaves('slave') and isinstance(err, list):
for val in err:
ret = _is_properties_option(val, path)
if ret is not None:
yield ret
break
2016-01-03 21:18:52 +01:00
else:
ret = _is_properties_option(err, path)
if ret is not None:
yield ret
2015-10-29 09:03:13 +01:00
2017-06-16 18:25:01 +02:00
descr = context.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'))
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)
2017-02-11 17:22:50 +01:00
if isinstance(err, Exception) and not isinstance(err, PropertiesOptionError): # pragma: no cover
2016-01-03 21:18:52 +01:00
raise err
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 = []
2017-02-11 17:22:50 +01:00
if not opt.impl_is_submulti() and isinstance(value, Multi):
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
2017-02-11 17:22:50 +01:00
if not isinstance(context, weakref.ReferenceType):
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].refmulti = 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()
2017-02-11 17:22:50 +01:00
if context is None:
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)
2017-05-26 14:07:43 +02:00
fake_multi = Multi(list(self), weakref.ref(fake_context), self.opt, self.path)
2015-04-18 22:53:45 +02:00
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
2017-03-17 21:27:42 +01:00
def __delitem__(self, index):
return self.pop(index)
2016-10-10 21:41:22 +02:00
def _getdefaultvalue(self, index):
2014-04-25 22:57:08 +02:00
values = self._getcontext().cfgimpl_get_values()
2016-10-10 21:41:22 +02:00
value = values._getdefaultvalue(self.opt, self.path, True, index,
2017-01-26 21:01:54 +01:00
undefined, True)
2016-10-10 21:41:22 +02:00
if self.opt.impl_is_submulti():
value = SubMulti(value, self.context, self.opt, self.path, index)
return value
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
"""
2017-02-11 17:22:50 +01:00
if not force and self.opt.impl_is_master_slaves('slave'):
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:
2016-10-10 21:41:22 +02:00
value = self._getdefaultvalue(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)
2017-05-31 19:22:22 +02:00
fake_multi = Multi(list(self), weakref.ref(fake_context), self.opt, self.path)
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.refmulti = 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()))
2017-02-11 17:22:50 +01:00
if sys.version_info[0] >= 3: # pragma: no cover
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)
2017-05-31 19:22:22 +02:00
fake_multi = Multi(list(self), weakref.ref(fake_context), self.opt, self.path)
2015-04-18 22:53:45 +02:00
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)
2017-05-31 19:22:22 +02:00
fake_multi = Multi(list(self), weakref.ref(fake_context), self.opt, self.path)
2016-11-19 19:16:31 +01:00
if index is None:
fake_multi.extend(iterable, validate=False)
self._validate(fake_multi, fake_context, index)
else:
fake_multi[index].extend(iterable, validate=False)
self._validate(fake_multi[index], 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,
2016-11-19 19:16:31 +01:00
force_index=force_index,
multi=self)
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:
2017-02-11 17:22:50 +01:00
if self.opt.impl_is_master_slaves('slave'):
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
2017-05-17 22:13:05 +02:00
def remove(self, value):
idx = self.index(value)
return self.pop(idx)
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
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', 'refmulti')
2014-04-25 22:57:08 +02:00
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)
multi = self.refmulti()
if multi is None:
multi = values._get_cached_value(self.opt, path=self.path)
multi[self._index] = self
values._setvalue(self.opt, self.path, multi)
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,
2016-11-19 19:16:31 +01:00
force_submulti_index=force_index,
multi=self)
if err:
raise err
2014-04-25 22:57:08 +02:00
2016-10-10 21:41:22 +02:00
def _getdefaultvalue(self, index):
2014-04-25 22:57:08 +02:00
values = self._getcontext().cfgimpl_get_values()
2016-10-10 21:41:22 +02:00
return values._getdefaultvalue(self.opt, self.path, True, index,
2017-01-26 21:01:54 +01:00
self._index, True)