2013-02-07 16:20:21 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-02-08 11:50:22 +01:00
|
|
|
"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
|
|
|
# ____________________________________________________________
|
2013-04-18 20:26:40 +02:00
|
|
|
from time import time
|
2013-08-28 11:33:43 +02:00
|
|
|
import sys
|
2013-08-27 11:39:32 +02:00
|
|
|
import weakref
|
2015-11-29 23:03:08 +01:00
|
|
|
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
|
|
|
"""
|
2013-09-26 21:56:06 +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
|
|
|
"""
|
2013-08-27 11:39:32 +02:00
|
|
|
self.context = weakref.ref(context)
|
2013-08-21 14:52:48 +02:00
|
|
|
# the storage type is dictionary or sqlite3
|
2013-09-06 23:15:28 +02:00
|
|
|
self._p_ = storage
|
2013-08-20 09:47:12 +02:00
|
|
|
|
2014-01-25 11:20:11 +01: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
|
2014-01-25 11:20:11 +01:00
|
|
|
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):
|
2014-04-12 11:53:58 +02:00
|
|
|
# if value has callback and is not set
|
|
|
|
if opt.impl_has_callback():
|
2014-04-13 10:30:42 +02:00
|
|
|
callback, callback_params = opt.impl_get_callback()
|
2015-04-18 22:53:45 +02:00
|
|
|
value = carry_out_calculation(opt, context=self._getcontext(),
|
2014-04-12 11:53:58 +02:00
|
|
|
callback=callback,
|
|
|
|
callback_params=callback_params,
|
2016-01-03 21:18:52 +01:00
|
|
|
index=index,
|
|
|
|
returns_raise=returns_raise)
|
2015-11-29 23:03:08 +01:00
|
|
|
if isinstance(value, list) and index is not None:
|
2015-11-24 10:58:19 +01:00
|
|
|
#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:
|
2014-04-12 11:53:58 +02:00
|
|
|
return value
|
2014-12-01 21:49:50 +01:00
|
|
|
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:
|
2014-12-01 21:49:50 +01:00
|
|
|
if isinstance(value, Multi):
|
2015-11-29 23:03:08 +01:00
|
|
|
if index is not None:
|
2014-12-01 21:49:50 +01:00
|
|
|
value = value[index]
|
|
|
|
else:
|
|
|
|
value = list(value)
|
|
|
|
return value
|
2014-04-12 11:53:58 +02:00
|
|
|
# now try to get default value
|
|
|
|
value = opt.impl_getdefault()
|
2015-11-29 23:03:08 +01:00
|
|
|
if opt.impl_is_multi() and index is not None:
|
2015-11-24 10:58:19 +01:00
|
|
|
if value == []:
|
2014-04-12 11:53:58 +02:00
|
|
|
value = opt.impl_getdefault_multi()
|
|
|
|
else:
|
2015-12-28 22:00:46 +01:00
|
|
|
if len(value) > index:
|
2014-04-12 11:53:58 +02:00
|
|
|
value = value[index]
|
2015-12-28 22:00:46 +01:00
|
|
|
else:
|
2014-04-12 11:53:58 +02:00
|
|
|
value = opt.impl_getdefault_multi()
|
2013-04-08 16:05:56 +02:00
|
|
|
return value
|
2013-02-07 16:20:21 +01:00
|
|
|
|
2016-03-07 16:13:41 +01:00
|
|
|
def _getvalue(self, opt, path, self_properties, index, submulti_index,
|
2016-03-29 09:31:00 +02:00
|
|
|
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
|
2015-11-24 10:58:19 +01:00
|
|
|
# not default value
|
2016-04-28 11:31:04 +02:00
|
|
|
is_default = self._is_default_owner(opt, path, session,
|
2016-03-07 16:13:41 +01:00
|
|
|
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'):
|
2016-03-29 09:31:00 +02:00
|
|
|
return self._p_.getvalue(path, session, index)
|
2015-11-19 22:25:00 +01:00
|
|
|
else:
|
2016-03-29 09:31:00 +02:00
|
|
|
value = self._p_.getvalue(path, session)
|
2015-11-29 23:03:08 +01:00
|
|
|
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
|
2016-03-07 16:13:41 +01:00
|
|
|
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())
|
2013-08-24 23:10:35 +02:00
|
|
|
return self._contains(path)
|
2013-08-21 22:21:50 +02:00
|
|
|
|
2016-03-29 09:31:00 +02:00
|
|
|
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):
|
2014-10-26 09:38:17 +01:00
|
|
|
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)
|
2016-03-29 09:31:00 +02:00
|
|
|
session = self._p_.getsession()
|
|
|
|
hasvalue = self._contains(path, session)
|
2015-04-19 09:37:46 +02:00
|
|
|
|
2015-12-17 22:41:57 +01:00
|
|
|
if validate and hasvalue and 'validator' in _setting_properties:
|
2016-03-29 09:31:00 +02:00
|
|
|
session = context.cfgimpl_get_values()._p_.getsession()
|
|
|
|
fake_context = context._gen_fake_values(session)
|
2015-04-19 09:37:46 +02:00
|
|
|
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)
|
2015-04-19 09:37:46 +02:00
|
|
|
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)
|
2015-04-19 09:37:46 +02:00
|
|
|
if hasvalue:
|
2016-03-07 16:13:41 +01:00
|
|
|
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:
|
2016-03-29 09:31:00 +02:00
|
|
|
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):
|
2013-02-08 11:50:22 +01:00
|
|
|
"convenience method to know if an option is empty"
|
2015-07-26 18:55:21 +02:00
|
|
|
if value is undefined:
|
|
|
|
return False
|
2014-04-12 11:53:58 +02:00
|
|
|
else:
|
2015-07-26 18:55:21 +02:00
|
|
|
empty = opt._empty
|
2015-11-19 22:25:00 +01:00
|
|
|
if index in [None, undefined] and opt.impl_is_multi():
|
2015-07-26 18:55:21 +02:00
|
|
|
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-08 16:05:56 +02:00
|
|
|
|
2013-04-18 23:06:14 +02:00
|
|
|
def __getitem__(self, opt):
|
2013-08-26 21:48:42 +02:00
|
|
|
"enables us to use the pythonic dictionary-like access to values"
|
2013-04-18 23:06:14 +02:00
|
|
|
return self.getitem(opt)
|
|
|
|
|
2014-04-12 21:37:20 +02:00
|
|
|
def getitem(self, opt, validate=True, force_permissive=False):
|
2014-04-12 11:53:58 +02:00
|
|
|
"""
|
|
|
|
"""
|
2015-11-29 23:03:08 +01:00
|
|
|
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,
|
2015-11-29 23:03:08 +01:00
|
|
|
validate_properties=True,
|
|
|
|
setting_properties=undefined, self_properties=undefined,
|
2016-01-03 13:23:15 +01:00
|
|
|
index=None, submulti_index=undefined, from_masterslave=False,
|
2016-01-03 21:18:52 +01:00
|
|
|
with_meta=True, masterlen=undefined, check_frozen=False,
|
2016-03-29 09:31:00 +02:00
|
|
|
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()
|
2013-08-21 22:21:50 +02:00
|
|
|
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
|
2014-12-01 21:49:50 +01:00
|
|
|
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)
|
2015-11-29 23:03:08 +01:00
|
|
|
if 'cache' in setting_properties and self._p_.hascache(path, index):
|
2014-12-01 21:49:50 +01:00
|
|
|
if 'expire' in setting_properties:
|
2013-09-07 17:25:22 +02:00
|
|
|
ntime = int(time())
|
2015-11-29 23:03:08 +01:00
|
|
|
is_cached, value = self._p_.getcache(path, ntime, index)
|
2013-08-14 23:06:31 +02:00
|
|
|
if is_cached:
|
2015-11-29 23:03:08 +01:00
|
|
|
if opt.impl_is_multi() and not isinstance(value, Multi) and index is None:
|
2014-04-12 11:53:58 +02:00
|
|
|
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
|
2016-03-29 09:31:00 +02:00
|
|
|
if session is None:
|
|
|
|
session = self._p_.getsession()
|
2015-11-29 23:03:08 +01:00
|
|
|
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,
|
2015-11-29 23:03:08 +01:00
|
|
|
validate_properties,
|
2016-03-29 09:31:00 +02:00
|
|
|
session,
|
2015-11-29 23:03:08 +01:00
|
|
|
setting_properties=setting_properties,
|
|
|
|
index=index,
|
2016-01-03 21:18:52 +01:00
|
|
|
self_properties=self_properties,
|
|
|
|
returns_raise=returns_raise)
|
2015-11-29 23:03:08 +01:00
|
|
|
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,
|
2016-01-03 13:23:15 +01:00
|
|
|
submulti_index=submulti_index,
|
2016-01-03 21:18:52 +01:00
|
|
|
check_frozen=check_frozen,
|
2016-03-29 09:31:00 +02:00
|
|
|
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
|
2015-11-29 23:03:08 +01:00
|
|
|
# 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:
|
2014-12-01 21:49:50 +01:00
|
|
|
if 'expire' in setting_properties:
|
2013-09-07 17:25:22 +02:00
|
|
|
if ntime is None:
|
|
|
|
ntime = int(time())
|
|
|
|
ntime = ntime + expires_time
|
2015-11-29 23:03:08 +01:00
|
|
|
self._p_.setcache(path, val, ntime, index)
|
2013-04-18 23:06:14 +02:00
|
|
|
return val
|
|
|
|
|
2014-04-12 11:53:58 +02:00
|
|
|
def _get_validated_value(self, opt, path, validate, force_permissive,
|
2015-12-14 23:37:15 +01:00
|
|
|
validate_properties,
|
2015-11-29 23:03:08 +01:00
|
|
|
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,
|
2016-03-29 09:31:00 +02:00
|
|
|
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 []
|
|
|
|
"""
|
2014-01-25 11:20:11 +01:00
|
|
|
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:
|
2015-11-29 23:03:08 +01:00
|
|
|
self_properties = setting._getproperties(opt, path, read_write=False, index=index)
|
2016-01-03 21:18:52 +01:00
|
|
|
config_error = None
|
2016-03-29 09:31:00 +02:00
|
|
|
if session is None:
|
|
|
|
session = self._p_.getsession()
|
2016-03-07 16:13:41 +01:00
|
|
|
value = self._getvalue(opt, path, self_properties, index, submulti_index,
|
2016-03-29 09:31:00 +02:00
|
|
|
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:
|
2014-04-12 11:53:58 +02:00
|
|
|
if index is undefined:
|
|
|
|
force_index = None
|
|
|
|
else:
|
|
|
|
force_index = index
|
2014-02-02 18:20:01 +01:00
|
|
|
if opt.impl_is_multi():
|
2014-04-12 11:53:58 +02:00
|
|
|
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)
|
|
|
|
|
2014-04-12 11:53:58 +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
|
2016-01-03 13:23:15 +01:00
|
|
|
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
|
|
|
|
|
2013-05-17 18:11:14 +02:00
|
|
|
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
|
2013-09-16 15:02:14 +02:00
|
|
|
if config_error is not None:
|
2016-01-03 21:18:52 +01:00
|
|
|
if returns_raise:
|
|
|
|
return config_error
|
|
|
|
else:
|
|
|
|
raise config_error
|
2013-02-08 11:50:22 +01:00
|
|
|
return value
|
|
|
|
|
2014-06-19 23:22:39 +02:00
|
|
|
def __setitem__(self, opt, value): # pragma: optional cover
|
2014-10-26 10:26:23 +01:00
|
|
|
raise ConfigError(_('you should only set value with config'))
|
2013-04-18 23:06:14 +02:00
|
|
|
|
2013-08-21 22:21:50 +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
|
2014-01-25 11:20:11 +01:00
|
|
|
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:
|
2016-03-29 09:31:00 +02:00
|
|
|
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()
|
2016-01-03 13:23:15 +01:00
|
|
|
fake_values._setvalue(opt, path, value)
|
2016-03-29 09:31:00 +02:00
|
|
|
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
|
2016-01-03 13:23:15 +01:00
|
|
|
err = opt.impl_validate(value, fake_context)
|
|
|
|
if err:
|
|
|
|
raise err
|
2015-12-31 18:20:36 +01:00
|
|
|
self._setvalue(opt, path, value)
|
|
|
|
|
2016-03-08 23:18:42 +01:00
|
|
|
def _setvalue(self, opt, path, value, force_owner=undefined, index=None):
|
2014-01-25 11:20:11 +01:00
|
|
|
context = self._getcontext()
|
|
|
|
context.cfgimpl_reset_cache()
|
2016-03-07 16:13:41 +01:00
|
|
|
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
|
2014-02-02 18:20:01 +01:00
|
|
|
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)
|
2016-03-29 09:31:00 +02:00
|
|
|
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'):
|
2016-03-08 23:18:42 +01:00
|
|
|
if index is not None:
|
2016-03-29 09:31:00 +02:00
|
|
|
self._p_.setvalue(path, value[index], owner, index, session)
|
2016-03-08 23:18:42 +01:00
|
|
|
else:
|
2016-03-29 09:31:00 +02:00
|
|
|
self._p_.resetvalue(path, session)
|
2016-03-08 23:18:42 +01:00
|
|
|
for idx, val in enumerate(value):
|
2016-03-29 09:31:00 +02:00
|
|
|
self._p_.setvalue(path, val, owner, idx, session)
|
2015-11-19 22:25:00 +01:00
|
|
|
else:
|
2016-03-29 09:31:00 +02:00
|
|
|
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,
|
2016-03-29 09:31:00 +02:00
|
|
|
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():
|
2016-03-29 09:31:00 +02:00
|
|
|
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
|
2013-02-08 11:50:22 +01:00
|
|
|
|
2016-03-29 09:31:00 +02:00
|
|
|
def _is_meta(self, opt, path, session):
|
2014-12-01 21:49:50 +01:00
|
|
|
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:
|
2014-12-01 21:49:50 +01:00
|
|
|
return False
|
2016-03-29 09:31:00 +02:00
|
|
|
if self._p_.getowner(path, owners.default, session, only_default=True) is not owners.default:
|
2014-12-01 21:49:50 +01:00
|
|
|
return False
|
|
|
|
if context.cfgimpl_get_meta() is not None:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-03-29 09:31:00 +02:00
|
|
|
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
|
2014-03-29 20:31:56 +01:00
|
|
|
: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())
|
2016-03-29 09:31:00 +02:00
|
|
|
return self._getowner(opt, path, session, index=index, force_permissive=force_permissive)
|
2013-08-21 22:21:50 +02:00
|
|
|
|
2016-03-29 09:31:00 +02:00
|
|
|
def _getowner(self, opt, path, session, validate_properties=True,
|
2014-12-01 21:49:50 +01:00
|
|
|
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
|
|
|
|
"""
|
2016-03-29 09:31:00 +02:00
|
|
|
if session is None:
|
|
|
|
session = self._p_.getsession()
|
2014-12-01 21:49:50 +01:00
|
|
|
if not isinstance(opt, Option) and not isinstance(opt,
|
|
|
|
DynSymLinkOption):
|
2014-10-26 08:51:45 +01:00
|
|
|
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:
|
2014-10-26 08:51:45 +01:00
|
|
|
return owners.default
|
2014-03-29 20:31:56 +01:00
|
|
|
if validate_properties:
|
2015-11-29 23:03:08 +01:00
|
|
|
self._get_cached_value(opt, path, True, force_permissive, None, True,
|
2016-03-29 09:31:00 +02:00
|
|
|
self_properties=self_properties, session=session)
|
|
|
|
owner = self._p_.getowner(path, owners.default, session, only_default=only_default, index=index)
|
2014-12-01 21:49:50 +01:00
|
|
|
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)
|
2016-03-29 09:31:00 +02:00
|
|
|
validate_meta = self._is_meta(opt, masterp, session)
|
2014-12-01 21:49:50 +01:00
|
|
|
else:
|
|
|
|
validate_meta = True
|
2014-04-12 11:53:58 +02:00
|
|
|
if validate_meta:
|
2014-10-26 08:51:45 +01:00
|
|
|
meta = context.cfgimpl_get_meta()
|
2014-04-12 11:53:58 +02:00
|
|
|
if owner is owners.default and meta is not None:
|
2016-03-29 09:31:00 +02:00
|
|
|
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)))
|
2013-08-21 22:21:50 +02:00
|
|
|
|
2014-06-19 23:22:39 +02:00
|
|
|
path = opt.impl_getpath(self._getcontext())
|
2016-03-29 09:31:00 +02:00
|
|
|
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}'
|
2013-08-21 22:21:50 +02:00
|
|
|
'').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
|
2016-03-29 09:31:00 +02:00
|
|
|
self._p_.setowner(path, owner, session, index=index)
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2014-04-12 11:53:58 +02:00
|
|
|
def is_default_owner(self, opt, validate_properties=True,
|
2016-03-07 16:34:36 +01:00
|
|
|
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())
|
2016-04-28 11:31:04 +02:00
|
|
|
return self._is_default_owner(opt, path, session=None,
|
2014-04-12 11:53:58 +02:00
|
|
|
validate_properties=validate_properties,
|
2016-03-07 16:34:36 +01:00
|
|
|
validate_meta=validate_meta, index=index,
|
|
|
|
force_permissive=force_permissive)
|
2013-08-21 22:21:50 +02:00
|
|
|
|
2016-03-29 09:31:00 +02:00
|
|
|
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,
|
2016-03-07 16:34:36 +01:00
|
|
|
index=None, force_permissive=False):
|
2015-11-19 22:25:00 +01:00
|
|
|
if not opt.impl_is_master_slaves('slave'):
|
|
|
|
index = None
|
2016-03-29 09:31:00 +02:00
|
|
|
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,
|
2016-03-07 16:34:36 +01:00
|
|
|
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
|
|
|
|
2013-04-19 22:42:33 +02:00
|
|
|
def reset_cache(self, only_expired):
|
2013-08-21 11:09:11 +02:00
|
|
|
"""
|
|
|
|
clears the cache if necessary
|
|
|
|
"""
|
2013-04-19 22:42:33 +02:00
|
|
|
if only_expired:
|
2013-09-07 17:25:22 +02:00
|
|
|
self._p_.reset_expired_cache(int(time()))
|
2013-04-19 22:42:33 +02:00
|
|
|
else:
|
2013-09-07 17:25:22 +02:00
|
|
|
self._p_.reset_all_cache()
|
2013-04-18 23:06:14 +02:00
|
|
|
|
2013-09-03 10:38:28 +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)
|
|
|
|
|
2014-04-12 21:55:22 +02:00
|
|
|
def get_information(self, key, default=undefined):
|
2013-09-03 10:38:28 +02:00
|
|
|
"""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)
|
2013-09-03 10:38:28 +02:00
|
|
|
|
2016-04-28 11:31:04 +02:00
|
|
|
def del_information(self, key, raises=True):
|
|
|
|
self._p_.del_information(key, raises)
|
|
|
|
|
2015-10-29 09:03:13 +01:00
|
|
|
def mandatory_warnings(self, force_permissive=False, validate=True):
|
2014-03-09 20:06:44 +01:00
|
|
|
"""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`
|
2014-03-09 20:06:44 +01:00
|
|
|
|
2015-10-29 09:03:13 +01:00
|
|
|
:returns: generator of mandatory Option's path
|
2014-03-09 20:06:44 +01:00
|
|
|
"""
|
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):
|
2016-03-16 22:33:28 +01:00
|
|
|
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:
|
2016-03-16 22:33:28 +01:00
|
|
|
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
|
|
|
|
2014-04-12 21:37:20 +02:00
|
|
|
descr = self._getcontext().cfgimpl_get_description()
|
2015-10-29 09:03:13 +01:00
|
|
|
for path in _mandatory_warnings(descr):
|
|
|
|
yield path
|
2014-03-09 20:06:44 +01:00
|
|
|
|
|
|
|
def force_cache(self):
|
|
|
|
"""parse all option to force data in cache
|
|
|
|
"""
|
|
|
|
context = self.context()
|
|
|
|
if not 'cache' in context.cfgimpl_get_settings():
|
2014-03-09 20:22:29 +01:00
|
|
|
raise ConfigError(_('can force cache only if cache '
|
|
|
|
'is actived in config'))
|
2014-03-09 20:06:44 +01:00
|
|
|
#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
|
2014-03-09 20:06:44 +01:00
|
|
|
|
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-03 10:38:28 +02:00
|
|
|
|
2013-09-24 23:19:20 +02:00
|
|
|
|
2013-02-08 11:50:22 +01: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
|
|
|
|
2014-04-12 11:53:58 +02:00
|
|
|
def __init__(self, value, context, opt, path):
|
2013-02-08 11:50:22 +01:00
|
|
|
"""
|
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
|
2013-02-08 11:50:22 +01:00
|
|
|
:param opt: the option object that have this Multi value
|
2014-04-25 22:57:08 +02:00
|
|
|
:param path: path of the option
|
2013-02-08 11:50:22 +01:00
|
|
|
"""
|
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()))
|
2013-02-08 11:50:22 +01:00
|
|
|
self.opt = opt
|
2013-08-21 22:21:50 +02:00
|
|
|
self.path = path
|
2014-06-19 23:22:39 +02:00
|
|
|
if not isinstance(context, weakref.ReferenceType): # pragma: optional cover
|
2013-08-27 11:39:32 +02:00
|
|
|
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
|
|
|
|
2014-01-25 11:20:11 +01: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
|
2014-01-25 11:20:11 +01:00
|
|
|
raise ConfigError(_('the context does not exist anymore'))
|
|
|
|
return context
|
|
|
|
|
2013-09-28 17:05:01 +02:00
|
|
|
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)
|
2016-03-21 17:06:38 +01:00
|
|
|
if index < 0:
|
|
|
|
index = self.__len__() + index
|
2015-05-03 09:56:03 +02:00
|
|
|
if 'validator' in setting_properties and validate:
|
2016-03-29 09:31:00 +02:00
|
|
|
session = context.cfgimpl_get_values()._p_.getsession()
|
|
|
|
fake_context = context._gen_fake_values(session)
|
2015-11-29 23:03:08 +01:00
|
|
|
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)
|
2013-04-18 20:26:40 +02:00
|
|
|
#assume not checking mandatory property
|
2013-09-28 17:05:01 +02:00
|
|
|
super(Multi, self).__setitem__(index, value)
|
2016-03-08 23:18:42 +01:00
|
|
|
self._store(index=index)
|
2013-02-08 11:50:22 +01:00
|
|
|
|
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
|
|
|
|
2015-11-29 23:03:08 +01: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):
|
2013-02-08 11:50:22 +01:00
|
|
|
"""the list value can be updated (appened)
|
|
|
|
only if the option is a master
|
|
|
|
"""
|
2015-03-08 12:03:49 +01:00
|
|
|
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()))
|
2013-09-28 17:05:01 +02:00
|
|
|
index = self.__len__()
|
2013-12-09 17:59:39 +01:00
|
|
|
if value is undefined:
|
2015-11-24 10:58:19 +01:00
|
|
|
value = self._get_validated_value(index)
|
2015-11-29 23:03:08 +01:00
|
|
|
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:
|
2016-03-29 09:31:00 +02:00
|
|
|
session = context.cfgimpl_get_values()._p_.getsession()
|
|
|
|
fake_context = context._gen_fake_values(session)
|
2015-11-29 23:03:08 +01:00
|
|
|
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)
|
2016-03-08 23:18:42 +01:00
|
|
|
fake_multi.append(value, validate=False, force=True,
|
|
|
|
setitem=setitem)
|
2015-11-29 23:03:08 +01:00
|
|
|
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)
|
2014-04-12 11:53:58 +02:00
|
|
|
if setitem:
|
2015-11-19 22:25:00 +01:00
|
|
|
self._store(force=force)
|
2013-02-22 11:09:17 +01:00
|
|
|
|
2015-11-29 23:03:08 +01:00
|
|
|
def append_properties_error(self, err):
|
|
|
|
super(Multi, self).append(err)
|
|
|
|
|
2013-06-12 22:48:22 +02:00
|
|
|
def sort(self, cmp=None, key=None, reverse=False):
|
2014-04-12 11:53:58 +02:00
|
|
|
if self.opt.impl_is_master_slaves():
|
2013-06-12 22:48:22 +02:00
|
|
|
raise SlaveError(_("cannot sort multi option {0} if master or slave"
|
2013-11-23 23:34:17 +01:00
|
|
|
"").format(self.opt.impl_getname()))
|
2013-08-28 11:33:43 +02:00
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
if cmp is not None:
|
2014-04-12 11:53:58 +02:00
|
|
|
raise ValueError(_('cmp is not permitted in python v3 or '
|
|
|
|
'greater'))
|
2013-08-28 11:33:43 +02:00
|
|
|
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()
|
2013-06-12 22:48:22 +02:00
|
|
|
|
|
|
|
def reverse(self):
|
2014-04-12 11:53:58 +02:00
|
|
|
if self.opt.impl_is_master_slaves():
|
2013-06-12 22:48:22 +02:00
|
|
|
raise SlaveError(_("cannot reverse multi option {0} if master or "
|
2013-11-23 23:34:17 +01:00
|
|
|
"slave").format(self.opt.impl_getname()))
|
2013-06-12 22:48:22 +02:00
|
|
|
super(Multi, self).reverse()
|
2014-04-25 22:57:08 +02:00
|
|
|
self._store()
|
2013-06-12 22:48:22 +02:00
|
|
|
|
2015-04-18 22:53:45 +02:00
|
|
|
def insert(self, index, value, validate=True):
|
2014-04-12 11:53:58 +02:00
|
|
|
if self.opt.impl_is_master_slaves():
|
2013-06-12 22:48:22 +02:00
|
|
|
raise SlaveError(_("cannot insert multi option {0} if master or "
|
2013-11-23 23:34:17 +01:00
|
|
|
"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:
|
2016-03-29 09:31:00 +02:00
|
|
|
session = context.cfgimpl_get_values()._p_.getsession()
|
|
|
|
fake_context = context._gen_fake_values(session)
|
2015-11-29 23:03:08 +01:00
|
|
|
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()
|
2013-06-12 22:48:22 +02:00
|
|
|
|
2015-04-18 22:53:45 +02:00
|
|
|
def extend(self, iterable, validate=True):
|
2014-04-12 11:53:58 +02:00
|
|
|
if self.opt.impl_is_master_slaves():
|
2013-06-12 22:48:22 +02:00
|
|
|
raise SlaveError(_("cannot extend multi option {0} if master or "
|
2013-11-23 23:34:17 +01:00
|
|
|
"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:
|
2016-03-29 09:31:00 +02:00
|
|
|
session = context.cfgimpl_get_values()._p_.getsession()
|
|
|
|
fake_context = context._gen_fake_values(session)
|
2015-11-29 23:03:08 +01:00
|
|
|
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)
|
2013-06-12 22:48:22 +02:00
|
|
|
super(Multi, self).extend(iterable)
|
2014-04-25 22:57:08 +02:00
|
|
|
self._store()
|
2013-06-12 22:48:22 +02:00
|
|
|
|
2015-04-18 22:53:45 +02:00
|
|
|
def _validate(self, value, fake_context, force_index, submulti=False):
|
2016-01-03 13:23:15 +01:00
|
|
|
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
|
|
|
|
2013-09-22 21:23:12 +02:00
|
|
|
def pop(self, index, force=False):
|
2013-02-08 11:50:22 +01:00
|
|
|
"""the list value can be updated (poped)
|
|
|
|
only if the option is a master
|
|
|
|
|
2013-09-22 21:23:12 +02:00
|
|
|
: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
|
2013-02-08 11:50:22 +01:00
|
|
|
"""
|
2014-01-25 11:20:11 +01:00
|
|
|
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}"
|
2013-11-23 23:34:17 +01:00
|
|
|
" which is a slave").format(self.opt.impl_getname()))
|
2014-04-12 11:53:58 +02:00
|
|
|
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)
|
2013-05-17 18:11:14 +02:00
|
|
|
#set value without valid properties
|
2013-09-22 21:23:12 +02:00
|
|
|
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
|
|
|
|
2016-03-08 23:18:42 +01: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)
|
2016-03-08 23:18:42 +01:00
|
|
|
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)
|
|
|
|
|
2016-03-08 23:18:42 +01:00
|
|
|
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:
|
2016-01-03 13:23:15 +01:00
|
|
|
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)
|