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-04-19 20:10:55 +02:00
|
|
|
from tiramisu.error import ConfigError, SlaveError
|
2013-04-18 20:26:40 +02:00
|
|
|
from tiramisu.setting import owners, multitypes, expires_time
|
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-02-07 16:20:21 +01:00
|
|
|
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-04-18 23:06:14 +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-04-18 23:06:14 +02:00
|
|
|
__slots__ = ('context', '_values', '_cache')
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-02-08 11:50:22 +01:00
|
|
|
def __init__(self, context):
|
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-05-23 14:55:52 +02:00
|
|
|
|
2013-02-08 11:50:22 +01:00
|
|
|
self.context = context
|
2013-04-18 20:26:40 +02:00
|
|
|
self._values = {}
|
2013-04-18 23:06:14 +02:00
|
|
|
self._cache = {}
|
2013-04-18 20:26:40 +02:00
|
|
|
super(Values, self).__init__()
|
2013-02-07 16:20:21 +01:00
|
|
|
|
2013-05-02 11:34:57 +02:00
|
|
|
def _get_default(self, opt):
|
|
|
|
meta = self.context.cfgimpl_get_meta()
|
|
|
|
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-05-10 22:32:42 +02:00
|
|
|
def _get_value(self, opt, validate=True):
|
2013-04-08 16:05:56 +02:00
|
|
|
"return value or default value if not set"
|
|
|
|
#if no value
|
2013-04-18 20:26:40 +02:00
|
|
|
if opt not in self._values:
|
2013-05-02 11:34:57 +02:00
|
|
|
value = self._get_default(opt)
|
2013-05-08 18:14:42 +02:00
|
|
|
if opt.impl_is_multi():
|
2013-05-10 22:32:42 +02:00
|
|
|
value = Multi(value, self.context, opt, validate)
|
2013-04-08 16:05:56 +02:00
|
|
|
else:
|
|
|
|
#if value
|
2013-04-18 20:26:40 +02:00
|
|
|
value = self._values[opt][1]
|
2013-04-08 16:05:56 +02:00
|
|
|
return value
|
2013-02-07 16:20:21 +01:00
|
|
|
|
2013-04-18 23:06:14 +02:00
|
|
|
def __delitem__(self, opt):
|
|
|
|
self._reset(opt)
|
|
|
|
|
2013-04-18 20:26:40 +02:00
|
|
|
def _reset(self, opt):
|
|
|
|
if opt in self._values:
|
2013-05-08 18:14:42 +02:00
|
|
|
setting = self.context.cfgimpl_get_settings()
|
2013-05-10 22:32:42 +02:00
|
|
|
opt.impl_validate(opt.impl_getdefault(), self.context,
|
|
|
|
'validator' in setting)
|
2013-04-19 22:42:33 +02:00
|
|
|
self.context.cfgimpl_reset_cache()
|
2013-06-12 10:22:50 +02:00
|
|
|
if opt.impl_is_multi() and opt.impl_get_multitype() == multitypes.master:
|
|
|
|
for slave in opt.impl_get_master_slaves():
|
|
|
|
self._reset(slave)
|
2013-04-18 20:26:40 +02:00
|
|
|
del(self._values[opt])
|
2013-02-26 14:56:15 +01:00
|
|
|
|
2013-04-08 16:05:56 +02:00
|
|
|
def _is_empty(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-06-12 10:22:50 +02:00
|
|
|
def _getcallback_value(self, opt, index=None):
|
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 = {}
|
|
|
|
return carry_out_calculation(opt._name, config=self.context,
|
|
|
|
callback=callback,
|
2013-06-12 10:22:50 +02:00
|
|
|
callback_params=callback_params,
|
|
|
|
index=index)
|
2013-04-08 16:05:56 +02:00
|
|
|
|
2013-04-18 23:06:14 +02:00
|
|
|
def __getitem__(self, opt):
|
|
|
|
return self.getitem(opt)
|
|
|
|
|
2013-04-23 11:41:16 +02:00
|
|
|
def get_modified_values(self):
|
|
|
|
return self._values
|
|
|
|
|
2013-04-18 23:06:14 +02:00
|
|
|
def getitem(self, opt, validate=True, force_permissive=False,
|
2013-05-17 18:11:14 +02:00
|
|
|
force_properties=None, validate_properties=True):
|
2013-04-18 23:06:14 +02:00
|
|
|
if opt in self._cache:
|
2013-04-19 22:42:33 +02:00
|
|
|
exp = time()
|
2013-04-18 23:06:14 +02:00
|
|
|
value, created = self._cache[opt]
|
2013-04-19 22:42:33 +02:00
|
|
|
if exp < created:
|
2013-04-18 23:06:14 +02:00
|
|
|
return value
|
2013-05-17 18:11:14 +02:00
|
|
|
val = self._getitem(opt, validate, force_permissive, force_properties,
|
|
|
|
validate_properties)
|
|
|
|
if validate and validate_properties and force_permissive is False and \
|
|
|
|
force_properties is None:
|
2013-05-08 18:14:42 +02:00
|
|
|
self._set_cache(opt, val)
|
2013-04-18 23:06:14 +02:00
|
|
|
return val
|
|
|
|
|
2013-05-17 18:11:14 +02:00
|
|
|
def _getitem(self, opt, validate, force_permissive, force_properties,
|
|
|
|
validate_properties):
|
2013-02-08 11:50:22 +01:00
|
|
|
# options with callbacks
|
2013-04-04 11:24:00 +02:00
|
|
|
setting = self.context.cfgimpl_get_settings()
|
2013-04-20 17:30:05 +02:00
|
|
|
is_frozen = 'frozen' in setting[opt]
|
2013-06-11 15:10:38 +02:00
|
|
|
#if value is callback and is not set or frozen with force_default_on_freeze
|
|
|
|
if opt.impl_has_callback() and (self.is_default_owner(opt) or
|
|
|
|
(is_frozen and
|
|
|
|
'force_default_on_freeze' in setting[opt])):
|
|
|
|
no_value_slave = False
|
2013-06-11 15:31:20 +02:00
|
|
|
if opt.impl_is_multi() and opt.impl_get_multitype() == multitypes.slave:
|
2013-06-11 15:10:38 +02:00
|
|
|
masterp = self.context.cfgimpl_get_description().impl_get_path_by_opt(
|
|
|
|
opt.impl_get_master_slaves())
|
|
|
|
mastervalue = getattr(self.context, masterp)
|
|
|
|
lenmaster = len(mastervalue)
|
|
|
|
if lenmaster == 0:
|
|
|
|
value = []
|
|
|
|
no_value_slave = True
|
|
|
|
|
|
|
|
if not no_value_slave:
|
2013-04-13 22:50:55 +02:00
|
|
|
value = self._getcallback_value(opt)
|
2013-06-11 15:31:20 +02:00
|
|
|
if opt.impl_is_multi() and opt.impl_get_multitype() == multitypes.slave:
|
2013-06-12 10:22:50 +02:00
|
|
|
if not isinstance(value, list):
|
|
|
|
value = [value for i in range(lenmaster)]
|
2013-06-11 15:10:38 +02:00
|
|
|
if opt.impl_is_multi():
|
|
|
|
value = Multi(value, self.context, opt, validate)
|
|
|
|
#suppress value if already set
|
|
|
|
self._reset(opt)
|
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-05-02 11:34:57 +02:00
|
|
|
value = self._get_default(opt)
|
2013-05-08 18:14:42 +02:00
|
|
|
if opt.impl_is_multi():
|
2013-05-10 15:10:06 +02:00
|
|
|
value = Multi(value, self.context, opt, validate)
|
2013-06-11 15:10:38 +02:00
|
|
|
else:
|
|
|
|
value = self._get_value(opt, validate)
|
2013-05-08 18:14:42 +02:00
|
|
|
if validate:
|
|
|
|
opt.impl_validate(value, self.context, 'validator' in setting)
|
2013-04-16 09:45:42 +02:00
|
|
|
if self.is_default_owner(opt) and \
|
2013-04-20 17:30:05 +02:00
|
|
|
'force_store_value' in setting[opt]:
|
2013-04-23 16:43:31 +02:00
|
|
|
self.setitem(opt, value, is_write=False)
|
2013-05-17 18:11:14 +02:00
|
|
|
if validate_properties:
|
|
|
|
setting.validate_properties(opt, False, False, value=value,
|
|
|
|
force_permissive=force_permissive,
|
|
|
|
force_properties=force_properties)
|
2013-02-08 11:50:22 +01:00
|
|
|
return value
|
|
|
|
|
2013-04-18 23:06:14 +02:00
|
|
|
def __setitem__(self, opt, value):
|
|
|
|
self.setitem(opt, value)
|
|
|
|
|
2013-04-23 16:43:31 +02:00
|
|
|
def setitem(self, opt, value, force_permissive=False, is_write=True):
|
|
|
|
#is_write is, for example, used with "force_store_value"
|
|
|
|
#user didn't change value, so not write
|
2013-04-17 21:33:34 +02:00
|
|
|
#valid opt
|
2013-05-08 18:14:42 +02:00
|
|
|
opt.impl_validate(value, self.context,
|
|
|
|
'validator' in self.context.cfgimpl_get_settings())
|
|
|
|
if opt.impl_is_multi() and not isinstance(value, Multi):
|
2013-04-18 23:06:14 +02:00
|
|
|
value = Multi(value, self.context, opt)
|
2013-04-18 20:26:40 +02:00
|
|
|
self._setvalue(opt, 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-05-17 18:11:14 +02:00
|
|
|
def _setvalue(self, opt, value, force_permissive=False, force_properties=None,
|
|
|
|
is_write=True, validate_properties=True):
|
2013-04-19 22:42:33 +02:00
|
|
|
self.context.cfgimpl_reset_cache()
|
2013-04-18 20:26:40 +02:00
|
|
|
setting = self.context.cfgimpl_get_settings()
|
2013-05-17 18:11:14 +02:00
|
|
|
if validate_properties:
|
|
|
|
setting.validate_properties(opt, False, is_write,
|
|
|
|
value=value,
|
|
|
|
force_permissive=force_permissive,
|
|
|
|
force_properties=force_properties)
|
2013-04-18 20:26:40 +02:00
|
|
|
self._values[opt] = (setting.getowner(), value)
|
2013-02-08 11:50:22 +01:00
|
|
|
|
|
|
|
def getowner(self, opt):
|
2013-05-02 11:34:57 +02:00
|
|
|
owner = self._values.get(opt, (owners.default, None))[0]
|
|
|
|
meta = self.context.cfgimpl_get_meta()
|
|
|
|
if owner is owners.default and meta is not None:
|
|
|
|
owner = meta.cfgimpl_get_values().getowner(opt)
|
|
|
|
return owner
|
2013-04-03 12:20:26 +02:00
|
|
|
|
|
|
|
def setowner(self, opt, owner):
|
2013-04-18 20:26:40 +02:00
|
|
|
if opt not in self._values:
|
2013-06-10 17:12:57 +02:00
|
|
|
raise ConfigError(_('no value for {0} cannot change owner to {1}').format(opt._name, owner))
|
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-04-18 20:26:40 +02:00
|
|
|
self._values[opt] = (owner, self._values[opt][1])
|
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
|
|
|
|
"""
|
|
|
|
return self.getowner(opt) == owners.default
|
2013-02-21 17:07:00 +01:00
|
|
|
|
2013-04-18 23:06:14 +02:00
|
|
|
def _set_cache(self, opt, val):
|
2013-04-20 17:30:05 +02:00
|
|
|
if 'expire' in self.context.cfgimpl_get_settings():
|
2013-04-19 20:23:34 +02:00
|
|
|
self._cache[opt] = (val, time() + expires_time)
|
2013-04-18 23:06:14 +02:00
|
|
|
|
2013-04-19 22:42:33 +02:00
|
|
|
def reset_cache(self, only_expired):
|
|
|
|
if only_expired:
|
|
|
|
exp = time()
|
|
|
|
keys = self._cache.keys()
|
|
|
|
for key in keys:
|
|
|
|
val, created = self._cache[key]
|
|
|
|
if exp > created:
|
|
|
|
del(self._cache[key])
|
|
|
|
else:
|
|
|
|
self._cache.clear()
|
2013-04-18 23:06:14 +02:00
|
|
|
|
2013-04-18 20:26:40 +02:00
|
|
|
def __contains__(self, opt):
|
|
|
|
return opt in self._values
|
|
|
|
|
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-04-03 12:20:26 +02:00
|
|
|
__slots__ = ('opt', 'context')
|
|
|
|
|
2013-05-10 15:10:06 +02:00
|
|
|
def __init__(self, value, context, opt, validate=True):
|
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
|
|
|
|
"""
|
|
|
|
self.opt = opt
|
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-04-18 23:06:14 +02:00
|
|
|
value = self._valid_slave(value)
|
2013-05-08 18:14:42 +02:00
|
|
|
elif self.opt.impl_get_multitype() == multitypes.master:
|
2013-04-18 23:06:14 +02:00
|
|
|
self._valid_master(value)
|
|
|
|
super(Multi, self).__init__(value)
|
|
|
|
|
|
|
|
def _valid_slave(self, value):
|
|
|
|
#if slave, had values until master's one
|
2013-05-08 18:14:42 +02:00
|
|
|
masterp = self.context.cfgimpl_get_description().impl_get_path_by_opt(
|
|
|
|
self.opt.impl_get_master_slaves())
|
2013-04-18 23:06:14 +02:00
|
|
|
mastervalue = getattr(self.context, masterp)
|
|
|
|
masterlen = len(mastervalue)
|
|
|
|
if len(value) > masterlen or (len(value) < masterlen and
|
|
|
|
not self.context.cfgimpl_get_values().is_default_owner(self.opt)):
|
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))
|
|
|
|
elif len(value) < masterlen:
|
|
|
|
for num in range(0, masterlen - len(value)):
|
2013-05-08 18:14:42 +02:00
|
|
|
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)
|
|
|
|
values = self.context.cfgimpl_get_values()
|
|
|
|
for slave in self.opt._master_slaves:
|
|
|
|
if not values.is_default_owner(slave):
|
|
|
|
value_slave = values._get_value(slave)
|
|
|
|
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-05-08 18:14:42 +02:00
|
|
|
value_slave.append(slave.impl_getdefault_multi(), force=True)
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-02-08 11:50:22 +01:00
|
|
|
def __setitem__(self, key, value):
|
2013-02-22 11:09:17 +01:00
|
|
|
self._validate(value)
|
2013-04-18 20:26:40 +02:00
|
|
|
#assume not checking mandatory property
|
|
|
|
self.context.cfgimpl_get_values()._setvalue(self.opt, self)
|
2013-02-22 11:09:17 +01:00
|
|
|
super(Multi, self).__setitem__(key, value)
|
2013-02-08 11:50:22 +01:00
|
|
|
|
2013-02-22 11:09:17 +01:00
|
|
|
def append(self, value, force=False):
|
2013-02-08 11:50:22 +01:00
|
|
|
"""the list value can be updated (appened)
|
|
|
|
only if the option is a master
|
|
|
|
"""
|
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:
|
2013-06-12 10:22:50 +02:00
|
|
|
values = self.context.cfgimpl_get_values()
|
|
|
|
if value is None and self.opt.impl_has_callback():
|
|
|
|
value = values._getcallback_value(self.opt)
|
|
|
|
#Force None il return a list
|
|
|
|
if isinstance(value, list):
|
|
|
|
value = None
|
2013-02-22 11:09:17 +01:00
|
|
|
self._validate(value)
|
2013-05-17 18:11:14 +02:00
|
|
|
#set value without valid properties
|
|
|
|
self.context.cfgimpl_get_values()._setvalue(self.opt, self, validate_properties=not force)
|
2013-02-22 11:09:17 +01:00
|
|
|
super(Multi, self).append(value)
|
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():
|
|
|
|
if not values.is_default_owner(slave):
|
|
|
|
if slave.impl_has_callback():
|
|
|
|
index = self.__len__() - 1
|
|
|
|
dvalue = values._getcallback_value(slave, index=index)
|
|
|
|
else:
|
|
|
|
dvalue = slave.impl_getdefault_multi()
|
|
|
|
#get multi without valid properties
|
|
|
|
values.getitem(slave, validate_properties=False).append(
|
|
|
|
dvalue, force=True)
|
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))
|
|
|
|
self.context.cfgimpl_get_values()._setvalue(self.opt, self)
|
|
|
|
super(Multi, self).sort(cmp=cmp, key=key, reverse=reverse)
|
|
|
|
|
|
|
|
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))
|
|
|
|
self.context.cfgimpl_get_values()._setvalue(self.opt, self)
|
|
|
|
super(Multi, self).reverse()
|
|
|
|
|
|
|
|
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))
|
|
|
|
self.context.cfgimpl_get_values()._setvalue(self.opt, self)
|
|
|
|
super(Multi, self).insert(index, obj)
|
|
|
|
|
|
|
|
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))
|
|
|
|
self.context.cfgimpl_get_values()._setvalue(self.opt, self)
|
|
|
|
super(Multi, self).extend(iterable)
|
|
|
|
|
2013-02-22 11:09:17 +01:00
|
|
|
def _validate(self, value):
|
2013-04-03 12:20:26 +02:00
|
|
|
if value is not None and not self.opt._validate(value):
|
2013-04-14 12:01:32 +02:00
|
|
|
raise ValueError(_("invalid value {0} "
|
2013-04-16 09:45:42 +02:00
|
|
|
"for option {1}").format(str(value),
|
|
|
|
self.opt._name))
|
2013-02-22 11:09:17 +01:00
|
|
|
|
|
|
|
def pop(self, key, force=False):
|
2013-02-08 11:50:22 +01:00
|
|
|
"""the list value can be updated (poped)
|
|
|
|
only if the option is a master
|
|
|
|
|
|
|
|
:param key: index of the element to pop
|
|
|
|
:return: the requested element
|
|
|
|
"""
|
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():
|
2013-05-17 18:11:14 +02:00
|
|
|
values = self.context.cfgimpl_get_values()
|
|
|
|
if not values.is_default_owner(slave):
|
|
|
|
#get multi without valid properties
|
|
|
|
values.getitem(slave, validate_properties=False).pop(key, force=True)
|
|
|
|
#set value without valid properties
|
|
|
|
self.context.cfgimpl_get_values()._setvalue(self.opt, self, validate_properties=not force)
|
2013-02-08 11:50:22 +01:00
|
|
|
return super(Multi, self).pop(key)
|