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"
|
2013-02-21 17:07:00 +01:00
|
|
|
# Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors)
|
2013-02-07 16:20:21 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
# ____________________________________________________________
|
2013-04-18 20:26:40 +02:00
|
|
|
from time import time
|
2013-06-12 10:22:50 +02:00
|
|
|
from copy import copy
|
2013-08-28 11:33:43 +02:00
|
|
|
import sys
|
2013-08-27 11:39:32 +02:00
|
|
|
import weakref
|
2013-04-19 20:10:55 +02:00
|
|
|
from tiramisu.error import ConfigError, SlaveError
|
2013-12-09 17:59:39 +01:00
|
|
|
from tiramisu.setting import owners, multitypes, expires_time, undefined
|
2013-04-08 16:05:56 +02:00
|
|
|
from tiramisu.autolib import carry_out_calculation
|
2013-04-13 23:09:05 +02:00
|
|
|
from tiramisu.i18n import _
|
2013-06-13 22:06:49 +02:00
|
|
|
from tiramisu.option import SymLinkOption
|
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()
|
|
|
|
if context is None:
|
|
|
|
raise ConfigError(_('the context does not exist anymore'))
|
|
|
|
return context
|
|
|
|
|
2013-08-14 23:06:31 +02:00
|
|
|
def _getdefault(self, opt):
|
2013-08-21 14:52:48 +02:00
|
|
|
"""
|
|
|
|
actually retrieves the default value
|
|
|
|
|
|
|
|
:param opt: the `option.Option()` object
|
|
|
|
"""
|
2014-01-25 11:20:11 +01:00
|
|
|
meta = self._getcontext().cfgimpl_get_meta()
|
2013-05-02 11:34:57 +02:00
|
|
|
if meta is not None:
|
2013-06-12 10:22:50 +02:00
|
|
|
value = meta.cfgimpl_get_values()[opt]
|
2013-05-02 11:34:57 +02:00
|
|
|
else:
|
2013-06-12 10:22:50 +02:00
|
|
|
value = opt.impl_getdefault()
|
|
|
|
if opt.impl_is_multi():
|
|
|
|
return copy(value)
|
|
|
|
else:
|
|
|
|
return value
|
2013-05-02 11:34:57 +02:00
|
|
|
|
2013-08-21 22:21:50 +02:00
|
|
|
def _getvalue(self, opt, path, validate=True):
|
2013-08-21 14:52:48 +02:00
|
|
|
"""actually retrieves the value
|
|
|
|
|
|
|
|
:param opt: the `option.Option()` object
|
|
|
|
:returns: the option's value (or the default value if not set)
|
|
|
|
"""
|
2013-08-21 22:21:50 +02:00
|
|
|
if not self._p_.hasvalue(path):
|
2013-08-21 17:21:09 +02:00
|
|
|
# if there is no value
|
2013-08-14 23:06:31 +02:00
|
|
|
value = self._getdefault(opt)
|
2013-05-08 18:14:42 +02:00
|
|
|
if opt.impl_is_multi():
|
2013-08-21 22:21:50 +02:00
|
|
|
value = Multi(value, self.context, opt, path, validate)
|
2013-04-08 16:05:56 +02:00
|
|
|
else:
|
2013-08-21 17:21:09 +02:00
|
|
|
# if there is a value
|
2013-08-21 22:21:50 +02:00
|
|
|
value = self._p_.getvalue(path)
|
2013-08-14 23:06:31 +02:00
|
|
|
if opt.impl_is_multi() and not isinstance(value, Multi):
|
2013-08-21 11:09:11 +02:00
|
|
|
# load value so don't need to validate if is not a Multi
|
2013-08-21 22:21:50 +02:00
|
|
|
value = Multi(value, self.context, opt, path, validate=False)
|
2013-04-08 16:05:56 +02:00
|
|
|
return value
|
2013-02-07 16:20:21 +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
|
|
|
|
"""
|
2013-08-21 22:21:50 +02:00
|
|
|
path = self._get_opt_path(opt)
|
2013-08-24 23:10:35 +02:00
|
|
|
return self._contains(path)
|
2013-08-21 22:21:50 +02:00
|
|
|
|
|
|
|
def _contains(self, path):
|
2013-08-24 23:10:35 +02:00
|
|
|
return self._p_.hasvalue(path)
|
2013-08-14 23:06:31 +02:00
|
|
|
|
2013-04-18 23:06:14 +02:00
|
|
|
def __delitem__(self, opt):
|
2013-08-21 14:52:48 +02:00
|
|
|
"""overrides the builtins `del()` instructions"""
|
2013-08-14 23:06:31 +02:00
|
|
|
self.reset(opt)
|
2013-04-18 23:06:14 +02:00
|
|
|
|
2013-08-21 22:21:50 +02:00
|
|
|
def reset(self, opt, path=None):
|
|
|
|
if path is None:
|
|
|
|
path = self._get_opt_path(opt)
|
|
|
|
if self._p_.hasvalue(path):
|
2014-01-25 11:20:11 +01:00
|
|
|
context = self._getcontext()
|
|
|
|
setting = context.cfgimpl_get_settings()
|
2013-09-26 21:56:06 +02:00
|
|
|
opt.impl_validate(opt.impl_getdefault(),
|
2014-01-25 11:20:11 +01:00
|
|
|
context, 'validator' in setting)
|
|
|
|
context.cfgimpl_reset_cache()
|
2013-08-20 12:08:02 +02:00
|
|
|
if (opt.impl_is_multi() and
|
2013-08-20 16:33:32 +02:00
|
|
|
opt.impl_get_multitype() == multitypes.master):
|
2013-06-12 10:22:50 +02:00
|
|
|
for slave in opt.impl_get_master_slaves():
|
2013-08-14 23:06:31 +02:00
|
|
|
self.reset(slave)
|
2013-08-21 22:21:50 +02:00
|
|
|
self._p_.resetvalue(path)
|
2013-02-26 14:56:15 +01:00
|
|
|
|
2013-08-14 23:06:31 +02:00
|
|
|
def _isempty(self, opt, value):
|
2013-02-08 11:50:22 +01:00
|
|
|
"convenience method to know if an option is empty"
|
2013-04-16 22:44:16 +02:00
|
|
|
empty = opt._empty
|
2013-05-08 18:14:42 +02:00
|
|
|
if (not opt.impl_is_multi() and (value is None or value == empty)) or \
|
|
|
|
(opt.impl_is_multi() and (value == [] or
|
|
|
|
None in value or empty in value)):
|
2013-04-16 22:44:16 +02:00
|
|
|
return True
|
2013-02-08 11:50:22 +01:00
|
|
|
return False
|
2013-02-07 16:20:21 +01:00
|
|
|
|
2013-09-19 21:38:46 +02:00
|
|
|
def _getcallback_value(self, opt, index=None, max_len=None):
|
2013-08-21 14:52:48 +02:00
|
|
|
"""
|
|
|
|
retrieves a value for the options that have a callback
|
|
|
|
|
|
|
|
:param opt: the `option.Option()` object
|
|
|
|
:param index: if an option is multi, only calculates the nth value
|
|
|
|
:type index: int
|
|
|
|
:returns: a calculated value
|
|
|
|
"""
|
2013-04-16 12:04:20 +02:00
|
|
|
callback, callback_params = opt._callback
|
2013-04-08 16:05:56 +02:00
|
|
|
if callback_params is None:
|
|
|
|
callback_params = {}
|
2014-01-25 11:20:11 +01:00
|
|
|
return carry_out_calculation(opt, config=self._getcontext(),
|
2013-04-08 16:05:56 +02:00
|
|
|
callback=callback,
|
2013-06-12 10:22:50 +02:00
|
|
|
callback_params=callback_params,
|
2013-09-19 21:38:46 +02:00
|
|
|
index=index, max_len=max_len)
|
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)
|
|
|
|
|
2013-08-21 22:21:50 +02:00
|
|
|
def getitem(self, opt, path=None, validate=True, force_permissive=False,
|
2013-05-17 18:11:14 +02:00
|
|
|
force_properties=None, validate_properties=True):
|
2013-08-21 22:21:50 +02:00
|
|
|
if path is None:
|
|
|
|
path = self._get_opt_path(opt)
|
2013-09-07 17:25:22 +02:00
|
|
|
ntime = None
|
2014-01-25 11:20:11 +01:00
|
|
|
setting = self._getcontext().cfgimpl_get_settings()
|
2013-09-07 17:25:22 +02:00
|
|
|
if 'cache' in setting and self._p_.hascache(path):
|
|
|
|
if 'expire' in setting:
|
|
|
|
ntime = int(time())
|
|
|
|
is_cached, value = self._p_.getcache(path, ntime)
|
2013-08-14 23:06:31 +02:00
|
|
|
if is_cached:
|
2013-08-19 11:01:21 +02:00
|
|
|
if opt.impl_is_multi() and not isinstance(value, Multi):
|
|
|
|
#load value so don't need to validate if is not a Multi
|
2013-08-21 22:21:50 +02:00
|
|
|
value = Multi(value, self.context, opt, path, validate=False)
|
2013-04-18 23:06:14 +02:00
|
|
|
return value
|
2013-09-07 17:25:22 +02:00
|
|
|
val = self._getitem(opt, path, validate, force_permissive,
|
|
|
|
force_properties, validate_properties)
|
|
|
|
if 'cache' in setting and validate and validate_properties and \
|
|
|
|
force_permissive is False and force_properties is None:
|
|
|
|
if 'expire' in setting:
|
|
|
|
if ntime is None:
|
|
|
|
ntime = int(time())
|
|
|
|
ntime = ntime + expires_time
|
|
|
|
self._p_.setcache(path, val, ntime)
|
2013-08-14 23:06:31 +02:00
|
|
|
|
2013-04-18 23:06:14 +02:00
|
|
|
return val
|
|
|
|
|
2013-08-21 22:21:50 +02:00
|
|
|
def _getitem(self, opt, path, validate, force_permissive, force_properties,
|
2013-05-17 18:11:14 +02:00
|
|
|
validate_properties):
|
2013-02-08 11:50:22 +01:00
|
|
|
# options with callbacks
|
2014-01-25 11:20:11 +01:00
|
|
|
context = self._getcontext()
|
|
|
|
setting = context.cfgimpl_get_settings()
|
2013-04-20 17:30:05 +02:00
|
|
|
is_frozen = 'frozen' in setting[opt]
|
2013-09-16 20:51:13 +02:00
|
|
|
# For calculating properties, we need value (ie for mandatory value).
|
|
|
|
# If value is calculating with a PropertiesOptionError's option
|
|
|
|
# _getcallback_value 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 = None
|
|
|
|
force_permissives = None
|
2013-08-20 12:08:02 +02:00
|
|
|
# if value is callback and is not set
|
|
|
|
# or frozen with force_default_on_freeze
|
|
|
|
if opt.impl_has_callback() and (
|
2013-08-21 22:21:50 +02:00
|
|
|
self._is_default_owner(path) or
|
2013-08-20 16:33:32 +02:00
|
|
|
(is_frozen and 'force_default_on_freeze' in setting[opt])):
|
2013-09-19 21:38:46 +02:00
|
|
|
lenmaster = None
|
2013-06-11 15:10:38 +02:00
|
|
|
no_value_slave = False
|
2013-08-20 12:08:02 +02:00
|
|
|
if (opt.impl_is_multi() and
|
2013-08-20 16:33:32 +02:00
|
|
|
opt.impl_get_multitype() == multitypes.slave):
|
|
|
|
masterp = self._get_opt_path(opt.impl_get_master_slaves())
|
2014-01-25 11:20:11 +01:00
|
|
|
mastervalue = getattr(context, masterp)
|
2013-06-11 15:10:38 +02:00
|
|
|
lenmaster = len(mastervalue)
|
|
|
|
if lenmaster == 0:
|
|
|
|
value = []
|
|
|
|
no_value_slave = True
|
|
|
|
|
|
|
|
if not no_value_slave:
|
2013-09-16 15:02:14 +02:00
|
|
|
try:
|
2013-09-19 21:38:46 +02:00
|
|
|
value = self._getcallback_value(opt, max_len=lenmaster)
|
2013-09-22 20:57:52 +02:00
|
|
|
except ConfigError as err:
|
|
|
|
# cannot assign config_err directly in python 3.3
|
|
|
|
config_error = err
|
2013-09-16 15:02:14 +02:00
|
|
|
value = None
|
2013-09-16 20:51:13 +02:00
|
|
|
# should not raise PropertiesOptionError if option is
|
|
|
|
# mandatory
|
|
|
|
force_permissives = set(['mandatory'])
|
2013-09-16 15:02:14 +02:00
|
|
|
else:
|
|
|
|
if (opt.impl_is_multi() and
|
|
|
|
opt.impl_get_multitype() == multitypes.slave):
|
|
|
|
if not isinstance(value, list):
|
|
|
|
value = [value for i in range(lenmaster)]
|
2013-09-17 09:10:08 +02:00
|
|
|
if config_error is None:
|
|
|
|
if opt.impl_is_multi():
|
|
|
|
value = Multi(value, self.context, opt, path, validate)
|
|
|
|
# suppress value if already set
|
|
|
|
self.reset(opt, path)
|
2013-02-08 11:50:22 +01:00
|
|
|
# frozen and force default
|
2013-04-20 17:30:05 +02:00
|
|
|
elif is_frozen and 'force_default_on_freeze' in setting[opt]:
|
2013-08-14 23:06:31 +02:00
|
|
|
value = self._getdefault(opt)
|
2013-05-08 18:14:42 +02:00
|
|
|
if opt.impl_is_multi():
|
2013-08-28 22:50:35 +02:00
|
|
|
value = Multi(value, self.context, opt, path, validate)
|
2013-06-11 15:10:38 +02:00
|
|
|
else:
|
2013-08-21 22:21:50 +02:00
|
|
|
value = self._getvalue(opt, path, validate)
|
2013-09-17 09:10:08 +02:00
|
|
|
if config_error is None and validate:
|
2014-01-25 11:20:11 +01:00
|
|
|
opt.impl_validate(value, context, 'validator' in setting)
|
2013-09-17 09:10:08 +02:00
|
|
|
if config_error is None and self._is_default_owner(path) and \
|
2013-04-20 17:30:05 +02:00
|
|
|
'force_store_value' in setting[opt]:
|
2013-08-21 22:21:50 +02:00
|
|
|
self.setitem(opt, value, path, is_write=False)
|
2013-05-17 18:11:14 +02:00
|
|
|
if validate_properties:
|
2013-08-21 22:21:50 +02:00
|
|
|
setting.validate_properties(opt, False, False, value=value, path=path,
|
2013-05-17 18:11:14 +02:00
|
|
|
force_permissive=force_permissive,
|
2013-09-16 20:51:13 +02:00
|
|
|
force_properties=force_properties,
|
|
|
|
force_permissives=force_permissives)
|
2013-09-16 15:02:14 +02:00
|
|
|
if config_error is not None:
|
2013-09-25 10:22:31 +02:00
|
|
|
raise config_error
|
2013-02-08 11:50:22 +01:00
|
|
|
return value
|
|
|
|
|
2013-04-18 23:06:14 +02:00
|
|
|
def __setitem__(self, opt, value):
|
2013-08-25 18:06:07 +02:00
|
|
|
raise ValueError('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,
|
|
|
|
is_write=True):
|
2013-08-21 11:09:11 +02:00
|
|
|
# is_write is, for example, used with "force_store_value"
|
|
|
|
# user didn't change value, so not write
|
|
|
|
# valid opt
|
2014-01-25 11:20:11 +01:00
|
|
|
context = self._getcontext()
|
|
|
|
opt.impl_validate(value, context,
|
|
|
|
'validator' in context.cfgimpl_get_settings())
|
2013-05-08 18:14:42 +02:00
|
|
|
if opt.impl_is_multi() and not isinstance(value, Multi):
|
2013-09-19 23:02:15 +02:00
|
|
|
value = Multi(value, self.context, opt, path, setitem=True)
|
2013-08-21 22:21:50 +02:00
|
|
|
self._setvalue(opt, path, value, force_permissive=force_permissive,
|
2013-04-23 16:43:31 +02:00
|
|
|
is_write=is_write)
|
2013-02-08 11:50:22 +01:00
|
|
|
|
2013-08-21 22:21:50 +02:00
|
|
|
def _setvalue(self, opt, path, value, force_permissive=False,
|
2013-08-20 12:08:02 +02:00
|
|
|
force_properties=None,
|
2013-05-17 18:11:14 +02:00
|
|
|
is_write=True, validate_properties=True):
|
2014-01-25 11:20:11 +01:00
|
|
|
context = self._getcontext()
|
|
|
|
context.cfgimpl_reset_cache()
|
2013-05-17 18:11:14 +02:00
|
|
|
if validate_properties:
|
2014-01-25 11:20:11 +01:00
|
|
|
setting = context.cfgimpl_get_settings()
|
2013-05-17 18:11:14 +02:00
|
|
|
setting.validate_properties(opt, False, is_write,
|
2013-08-21 22:21:50 +02:00
|
|
|
value=value, path=path,
|
2013-05-17 18:11:14 +02:00
|
|
|
force_permissive=force_permissive,
|
|
|
|
force_properties=force_properties)
|
2014-01-25 11:20:11 +01:00
|
|
|
owner = context.cfgimpl_get_settings().getowner()
|
2013-08-21 22:21:50 +02:00
|
|
|
self._p_.setvalue(path, value, owner)
|
2013-02-08 11:50:22 +01:00
|
|
|
|
|
|
|
def getowner(self, opt):
|
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
|
2013-08-21 11:09:11 +02:00
|
|
|
:returns: a `setting.owners.Owner` object
|
|
|
|
"""
|
2013-06-13 22:06:49 +02:00
|
|
|
if isinstance(opt, SymLinkOption):
|
|
|
|
opt = opt._opt
|
2013-08-21 22:21:50 +02:00
|
|
|
path = self._get_opt_path(opt)
|
|
|
|
return self._getowner(path)
|
|
|
|
|
|
|
|
def _getowner(self, path):
|
|
|
|
owner = self._p_.getowner(path, owners.default)
|
2014-01-25 11:20:11 +01:00
|
|
|
meta = self._getcontext().cfgimpl_get_meta()
|
2013-05-02 11:34:57 +02:00
|
|
|
if owner is owners.default and meta is not None:
|
2013-08-21 22:21:50 +02:00
|
|
|
owner = meta.cfgimpl_get_values()._getowner(path)
|
2013-05-02 11:34:57 +02:00
|
|
|
return owner
|
2013-04-03 12:20:26 +02:00
|
|
|
|
|
|
|
def setowner(self, opt, owner):
|
2013-08-21 11:09:11 +02:00
|
|
|
"""
|
|
|
|
sets a owner to an option
|
2013-08-21 14:52:48 +02:00
|
|
|
|
|
|
|
:param opt: the `option.Option` object
|
2013-08-21 11:09:11 +02:00
|
|
|
:param owner: a valid owner, that is a `setting.owners.Owner` object
|
|
|
|
"""
|
2013-04-03 12:20:26 +02:00
|
|
|
if not isinstance(owner, owners.Owner):
|
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
|
|
|
|
|
|
|
path = self._get_opt_path(opt)
|
|
|
|
self._setowner(path, owner)
|
|
|
|
|
|
|
|
def _setowner(self, path, owner):
|
|
|
|
if self._getowner(path) == owners.default:
|
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))
|
|
|
|
self._p_.setowner(path, owner)
|
2013-04-03 12:20:26 +02:00
|
|
|
|
|
|
|
def is_default_owner(self, opt):
|
|
|
|
"""
|
|
|
|
:param config: *must* be only the **parent** config
|
|
|
|
(not the toplevel config)
|
|
|
|
:return: boolean
|
|
|
|
"""
|
2013-08-21 22:21:50 +02:00
|
|
|
path = self._get_opt_path(opt)
|
|
|
|
return self._is_default_owner(path)
|
|
|
|
|
|
|
|
def _is_default_owner(self, path):
|
|
|
|
return self._getowner(path) == 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-08-14 23:06:31 +02:00
|
|
|
def _get_opt_path(self, opt):
|
2013-08-21 11:09:11 +02:00
|
|
|
"""
|
2013-08-21 14:52:48 +02:00
|
|
|
retrieve the option's path in the config
|
|
|
|
|
|
|
|
:param opt: the `option.Option` object
|
2013-08-21 11:09:11 +02:00
|
|
|
:returns: a string with points like "gc.dummy.my_option"
|
|
|
|
"""
|
2014-01-25 11:20:11 +01:00
|
|
|
return self._getcontext().cfgimpl_get_description().impl_get_path_by_opt(opt)
|
2013-08-14 23:06:31 +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)
|
|
|
|
|
|
|
|
def get_information(self, key, default=None):
|
|
|
|
"""retrieves one information's item
|
|
|
|
|
|
|
|
:param key: the item string (ex: "help")
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self._p_.get_information(key)
|
|
|
|
except ValueError:
|
|
|
|
if default is not None:
|
|
|
|
return default
|
|
|
|
else:
|
|
|
|
raise ValueError(_("information's item"
|
|
|
|
" not found: {0}").format(key))
|
|
|
|
|
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
|
2013-04-03 12:20:26 +02:00
|
|
|
|
|
|
|
|
2013-02-08 11:50:22 +01:00
|
|
|
class Multi(list):
|
|
|
|
"""multi options values container
|
|
|
|
that support item notation for the values of multi options"""
|
2013-08-21 22:21:50 +02:00
|
|
|
__slots__ = ('opt', 'path', 'context')
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-09-19 23:02:15 +02:00
|
|
|
def __init__(self, value, context, opt, path, validate=True,
|
|
|
|
setitem=False):
|
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
|
2013-09-19 23:02:15 +02:00
|
|
|
:param setitem: only if set a value
|
2013-02-08 11:50:22 +01:00
|
|
|
"""
|
|
|
|
self.opt = opt
|
2013-08-21 22:21:50 +02:00
|
|
|
self.path = path
|
2013-08-27 11:39:32 +02: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):
|
|
|
|
value = [value]
|
2013-05-10 15:10:06 +02:00
|
|
|
if validate and self.opt.impl_get_multitype() == multitypes.slave:
|
2013-09-19 23:02:15 +02:00
|
|
|
value = self._valid_slave(value, setitem)
|
2013-09-22 20:57:52 +02:00
|
|
|
elif validate and self.opt.impl_get_multitype() == multitypes.master:
|
2013-04-18 23:06:14 +02:00
|
|
|
self._valid_master(value)
|
|
|
|
super(Multi, self).__init__(value)
|
|
|
|
|
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()
|
|
|
|
if context is None:
|
|
|
|
raise ConfigError(_('the context does not exist anymore'))
|
|
|
|
return context
|
|
|
|
|
2013-09-19 23:02:15 +02:00
|
|
|
def _valid_slave(self, value, setitem):
|
2013-04-18 23:06:14 +02:00
|
|
|
#if slave, had values until master's one
|
2014-01-25 11:20:11 +01:00
|
|
|
context = self._getcontext()
|
|
|
|
values = context.cfgimpl_get_values()
|
|
|
|
masterp = context.cfgimpl_get_description().impl_get_path_by_opt(
|
2013-05-08 18:14:42 +02:00
|
|
|
self.opt.impl_get_master_slaves())
|
2014-01-25 11:20:11 +01:00
|
|
|
mastervalue = getattr(context, masterp)
|
2013-04-18 23:06:14 +02:00
|
|
|
masterlen = len(mastervalue)
|
2013-08-20 16:33:32 +02:00
|
|
|
valuelen = len(value)
|
2013-09-19 23:02:15 +02:00
|
|
|
is_default_owner = not values._is_default_owner(self.path) or setitem
|
2013-08-20 16:33:32 +02:00
|
|
|
if valuelen > masterlen or (valuelen < masterlen and
|
2013-09-19 23:02:15 +02:00
|
|
|
is_default_owner):
|
2013-04-19 20:10:55 +02:00
|
|
|
raise SlaveError(_("invalid len for the slave: {0}"
|
2013-04-18 23:06:14 +02:00
|
|
|
" which has {1} as master").format(
|
|
|
|
self.opt._name, masterp))
|
2013-08-20 16:33:32 +02:00
|
|
|
elif valuelen < masterlen:
|
|
|
|
for num in range(0, masterlen - valuelen):
|
2013-09-19 21:38:46 +02:00
|
|
|
if self.opt.impl_has_callback():
|
|
|
|
# if callback add a value, but this value will not change
|
|
|
|
# anymore automaticly (because this value has owner)
|
|
|
|
index = value.__len__()
|
|
|
|
value.append(values._getcallback_value(self.opt,
|
|
|
|
index=index))
|
|
|
|
else:
|
|
|
|
value.append(self.opt.impl_getdefault_multi())
|
2013-04-18 23:06:14 +02:00
|
|
|
#else: same len so do nothing
|
|
|
|
return value
|
|
|
|
|
|
|
|
def _valid_master(self, value):
|
|
|
|
masterlen = len(value)
|
2014-01-25 11:20:11 +01:00
|
|
|
values = self._getcontext().cfgimpl_get_values()
|
2013-04-18 23:06:14 +02:00
|
|
|
for slave in self.opt._master_slaves:
|
2013-08-21 22:21:50 +02:00
|
|
|
path = values._get_opt_path(slave)
|
|
|
|
if not values._is_default_owner(path):
|
|
|
|
value_slave = values._getvalue(slave, path)
|
2013-04-18 23:06:14 +02:00
|
|
|
if len(value_slave) > masterlen:
|
2013-04-19 20:10:55 +02:00
|
|
|
raise SlaveError(_("invalid len for the master: {0}"
|
2013-04-18 23:06:14 +02:00
|
|
|
" which has {1} as slave with"
|
|
|
|
" greater len").format(
|
|
|
|
self.opt._name, slave._name))
|
|
|
|
elif len(value_slave) < masterlen:
|
|
|
|
for num in range(0, masterlen - len(value_slave)):
|
2013-09-19 21:38:46 +02:00
|
|
|
if slave.impl_has_callback():
|
|
|
|
# if callback add a value, but this value will not
|
|
|
|
# change anymore automaticly (because this value
|
|
|
|
# has owner)
|
|
|
|
index = value_slave.__len__()
|
|
|
|
value_slave.append(
|
|
|
|
values._getcallback_value(slave, index=index),
|
|
|
|
force=True)
|
|
|
|
else:
|
2013-12-09 17:59:39 +01:00
|
|
|
value_slave.append(undefined, force=True)
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-09-28 17:05:01 +02:00
|
|
|
def __setitem__(self, index, value):
|
|
|
|
self._validate(value, index)
|
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)
|
2014-01-25 11:20:11 +01:00
|
|
|
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
2013-02-08 11:50:22 +01:00
|
|
|
|
2013-12-09 17:59:39 +01:00
|
|
|
def append(self, value=undefined, force=False):
|
2013-02-08 11:50:22 +01:00
|
|
|
"""the list value can be updated (appened)
|
|
|
|
only if the option is a master
|
|
|
|
"""
|
2014-01-25 11:20:11 +01:00
|
|
|
context = self._getcontext()
|
2013-02-22 11:09:17 +01:00
|
|
|
if not force:
|
2013-05-08 18:14:42 +02:00
|
|
|
if self.opt.impl_get_multitype() == multitypes.slave:
|
2013-04-19 20:10:55 +02:00
|
|
|
raise SlaveError(_("cannot append a value on a multi option {0}"
|
2013-04-17 21:57:06 +02:00
|
|
|
" which is a slave").format(self.opt._name))
|
2013-05-08 18:14:42 +02:00
|
|
|
elif self.opt.impl_get_multitype() == multitypes.master:
|
2014-01-25 11:20:11 +01:00
|
|
|
values = context.cfgimpl_get_values()
|
2013-12-09 17:59:39 +01:00
|
|
|
if value is undefined and self.opt.impl_has_callback():
|
2013-06-12 10:22:50 +02:00
|
|
|
value = values._getcallback_value(self.opt)
|
|
|
|
#Force None il return a list
|
|
|
|
if isinstance(value, list):
|
|
|
|
value = None
|
2013-09-28 17:05:01 +02:00
|
|
|
index = self.__len__()
|
2013-12-09 17:59:39 +01:00
|
|
|
if value is undefined:
|
|
|
|
value = self.opt.impl_getdefault_multi()
|
2013-09-28 17:05:01 +02:00
|
|
|
self._validate(value, index)
|
2013-02-22 11:09:17 +01:00
|
|
|
super(Multi, self).append(value)
|
2014-01-25 11:20:11 +01:00
|
|
|
context.cfgimpl_get_values()._setvalue(self.opt, self.path,
|
|
|
|
self,
|
|
|
|
validate_properties=not force)
|
2013-06-12 10:22:50 +02:00
|
|
|
if not force and self.opt.impl_get_multitype() == multitypes.master:
|
|
|
|
for slave in self.opt.impl_get_master_slaves():
|
2013-08-21 22:21:50 +02:00
|
|
|
path = values._get_opt_path(slave)
|
|
|
|
if not values._is_default_owner(path):
|
2013-06-12 10:22:50 +02:00
|
|
|
if slave.impl_has_callback():
|
|
|
|
dvalue = values._getcallback_value(slave, index=index)
|
|
|
|
else:
|
|
|
|
dvalue = slave.impl_getdefault_multi()
|
2013-08-25 20:51:29 +02:00
|
|
|
old_value = values.getitem(slave, path,
|
|
|
|
validate_properties=False)
|
|
|
|
if len(old_value) < self.__len__():
|
|
|
|
values.getitem(slave, path,
|
|
|
|
validate_properties=False).append(
|
|
|
|
dvalue, force=True)
|
|
|
|
else:
|
|
|
|
values.getitem(slave, path,
|
|
|
|
validate_properties=False)[
|
|
|
|
index] = dvalue
|
2013-02-22 11:09:17 +01:00
|
|
|
|
2013-06-12 22:48:22 +02:00
|
|
|
def sort(self, cmp=None, key=None, reverse=False):
|
|
|
|
if self.opt.impl_get_multitype() in [multitypes.slave,
|
|
|
|
multitypes.master]:
|
|
|
|
raise SlaveError(_("cannot sort multi option {0} if master or slave"
|
|
|
|
"").format(self.opt._name))
|
2013-08-28 11:33:43 +02:00
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
if cmp is not None:
|
|
|
|
raise ValueError(_('cmp is not permitted in python v3 or greater'))
|
|
|
|
super(Multi, self).sort(key=key, reverse=reverse)
|
|
|
|
else:
|
|
|
|
super(Multi, self).sort(cmp=cmp, key=key, reverse=reverse)
|
2014-01-25 11:20:11 +01:00
|
|
|
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
2013-06-12 22:48:22 +02:00
|
|
|
|
|
|
|
def reverse(self):
|
|
|
|
if self.opt.impl_get_multitype() in [multitypes.slave,
|
|
|
|
multitypes.master]:
|
|
|
|
raise SlaveError(_("cannot reverse multi option {0} if master or "
|
|
|
|
"slave").format(self.opt._name))
|
|
|
|
super(Multi, self).reverse()
|
2014-01-25 11:20:11 +01:00
|
|
|
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
2013-06-12 22:48:22 +02:00
|
|
|
|
|
|
|
def insert(self, index, obj):
|
|
|
|
if self.opt.impl_get_multitype() in [multitypes.slave,
|
|
|
|
multitypes.master]:
|
|
|
|
raise SlaveError(_("cannot insert multi option {0} if master or "
|
|
|
|
"slave").format(self.opt._name))
|
|
|
|
super(Multi, self).insert(index, obj)
|
2014-01-25 11:20:11 +01:00
|
|
|
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
2013-06-12 22:48:22 +02:00
|
|
|
|
|
|
|
def extend(self, iterable):
|
|
|
|
if self.opt.impl_get_multitype() in [multitypes.slave,
|
|
|
|
multitypes.master]:
|
|
|
|
raise SlaveError(_("cannot extend multi option {0} if master or "
|
|
|
|
"slave").format(self.opt._name))
|
|
|
|
super(Multi, self).extend(iterable)
|
2014-01-25 11:20:11 +01:00
|
|
|
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
2013-06-12 22:48:22 +02:00
|
|
|
|
2013-09-28 17:05:01 +02:00
|
|
|
def _validate(self, value, force_index):
|
2013-07-17 20:48:46 +02:00
|
|
|
if value is not None:
|
|
|
|
try:
|
2014-01-25 11:20:11 +01:00
|
|
|
self.opt.impl_validate(value, context=self._getcontext(),
|
2013-09-28 17:05:01 +02:00
|
|
|
force_index=force_index)
|
2013-08-28 11:33:43 +02:00
|
|
|
except ValueError as err:
|
2013-07-17 20:48:46 +02:00
|
|
|
raise ValueError(_("invalid value {0} "
|
2013-08-20 16:33:32 +02:00
|
|
|
"for option {1}: {2}"
|
|
|
|
"").format(str(value),
|
|
|
|
self.opt._name, 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:
|
2013-05-08 18:14:42 +02:00
|
|
|
if self.opt.impl_get_multitype() == multitypes.slave:
|
2013-04-19 20:10:55 +02:00
|
|
|
raise SlaveError(_("cannot pop a value on a multi option {0}"
|
2013-04-17 21:57:06 +02:00
|
|
|
" which is a slave").format(self.opt._name))
|
2013-05-08 18:14:42 +02:00
|
|
|
elif self.opt.impl_get_multitype() == multitypes.master:
|
|
|
|
for slave in self.opt.impl_get_master_slaves():
|
2014-01-25 11:20:11 +01:00
|
|
|
values = context.cfgimpl_get_values()
|
2013-05-17 18:11:14 +02:00
|
|
|
if not values.is_default_owner(slave):
|
|
|
|
#get multi without valid properties
|
2013-08-20 12:08:02 +02:00
|
|
|
values.getitem(slave,
|
|
|
|
validate_properties=False
|
2013-09-22 21:23:12 +02:00
|
|
|
).pop(index, force=True)
|
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)
|
2014-01-25 11:20:11 +01:00
|
|
|
context.cfgimpl_get_values()._setvalue(self.opt, self.path, self, validate_properties=not force)
|
2013-08-14 23:06:31 +02:00
|
|
|
return ret
|