2013-04-03 12:20:26 +02:00
|
|
|
# Copyright (C) 2012-2013 Team tiramisu (see AUTHORS for all contributors)
|
2012-07-13 09:37:35 +02: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
|
|
|
|
#
|
2012-09-18 09:48:41 +02:00
|
|
|
# The original `Config` design model is unproudly borrowed from
|
2012-07-13 09:37:35 +02:00
|
|
|
# the rough gus of pypy: pypy: http://codespeak.net/svn/pypy/dist/pypy/config/
|
|
|
|
# the whole pypy projet is under MIT licence
|
|
|
|
# ____________________________________________________________
|
|
|
|
"enables us to carry out a calculation and return an option's value"
|
2013-04-14 12:01:32 +02:00
|
|
|
from tiramisu.error import PropertiesOptionError, ConfigError
|
2013-04-22 16:48:56 +02:00
|
|
|
from tiramisu.i18n import _
|
2012-07-13 09:37:35 +02:00
|
|
|
# ____________________________________________________________
|
|
|
|
# automatic Option object
|
2012-09-18 09:48:41 +02:00
|
|
|
#def special_owner_factory(name, owner, value,
|
2012-08-14 10:55:08 +02:00
|
|
|
# callback, callback_params=None, config=None):
|
2012-09-18 09:48:41 +02:00
|
|
|
# # in case of an 'auto' and a 'fill' without a value,
|
2012-08-14 10:55:08 +02:00
|
|
|
# # we have to carry out a calculation
|
|
|
|
# return calc_factory(name, callback, callback_params, config)
|
2013-04-03 12:20:26 +02:00
|
|
|
|
|
|
|
|
2013-06-12 10:22:50 +02:00
|
|
|
def carry_out_calculation(name, config, callback, callback_params, index=None):
|
2013-05-23 14:55:52 +02:00
|
|
|
"a function that carries out a calculation for an option's value"
|
2013-04-03 12:20:26 +02:00
|
|
|
#callback, callback_params = option.getcallback()
|
|
|
|
#if callback_params is None:
|
|
|
|
# callback_params = {}
|
2012-07-13 09:37:35 +02:00
|
|
|
tcparams = {}
|
|
|
|
one_is_multi = False
|
|
|
|
len_multi = 0
|
2012-10-17 11:14:17 +02:00
|
|
|
|
|
|
|
for key, values in callback_params.items():
|
|
|
|
for value in values:
|
|
|
|
if type(value) == tuple:
|
2013-04-22 20:42:54 +02:00
|
|
|
path, check_disabled = value
|
2013-04-22 16:48:13 +02:00
|
|
|
if config is None:
|
2013-04-22 20:42:54 +02:00
|
|
|
if check_disabled:
|
|
|
|
continue
|
2013-04-22 16:48:13 +02:00
|
|
|
raise ConfigError(_('no config specified but needed'))
|
2012-10-17 11:14:17 +02:00
|
|
|
try:
|
2013-04-03 12:20:26 +02:00
|
|
|
opt_value = config._getattr(path, force_permissive=True)
|
2013-06-26 16:26:42 +02:00
|
|
|
opt = config.unwrap_from_path(path, force_permissive=True)
|
2012-10-17 11:14:17 +02:00
|
|
|
except PropertiesOptionError, err:
|
|
|
|
if check_disabled:
|
|
|
|
continue
|
2013-06-12 17:07:29 +02:00
|
|
|
raise ConfigError(_('unable to carry out a calculation, '
|
|
|
|
'option {0} has properties: {1} for: '
|
2013-06-13 22:06:49 +02:00
|
|
|
'{2}').format(path, err.proptype,
|
2013-06-12 17:07:29 +02:00
|
|
|
name))
|
2013-05-10 15:10:06 +02:00
|
|
|
is_multi = opt.impl_is_multi()
|
2012-10-17 11:14:17 +02:00
|
|
|
if is_multi:
|
2013-04-03 12:20:26 +02:00
|
|
|
if opt_value is not None:
|
2012-10-17 11:14:17 +02:00
|
|
|
len_value = len(opt_value)
|
|
|
|
if len_multi != 0 and len_multi != len_value:
|
2013-06-12 17:07:29 +02:00
|
|
|
raise ConfigError(_('unable to carry out a '
|
2013-04-14 12:01:32 +02:00
|
|
|
'calculation, option values with'
|
|
|
|
' multi types must have same '
|
2013-06-12 17:07:29 +02:00
|
|
|
'length for: {0}').format(name))
|
2012-10-17 11:14:17 +02:00
|
|
|
len_multi = len_value
|
|
|
|
one_is_multi = True
|
|
|
|
tcparams.setdefault(key, []).append((opt_value, is_multi))
|
|
|
|
else:
|
|
|
|
tcparams.setdefault(key, []).append((value, False))
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
|
|
if one_is_multi:
|
|
|
|
ret = []
|
2013-06-12 10:22:50 +02:00
|
|
|
if index:
|
|
|
|
range_ = [index]
|
|
|
|
else:
|
|
|
|
range_ = range(len_multi)
|
|
|
|
for incr in range_:
|
2012-07-13 09:37:35 +02:00
|
|
|
tcp = {}
|
2012-10-17 11:14:17 +02:00
|
|
|
params = []
|
|
|
|
for key, couples in tcparams.items():
|
|
|
|
for couple in couples:
|
|
|
|
value, ismulti = couple
|
2013-04-03 12:20:26 +02:00
|
|
|
if ismulti and value is not None:
|
2012-10-17 11:14:17 +02:00
|
|
|
if key == '':
|
|
|
|
params.append(value[incr])
|
|
|
|
else:
|
|
|
|
if len(value) > incr:
|
|
|
|
tcp[key] = value[incr]
|
|
|
|
else:
|
|
|
|
tcp[key] = ''
|
2012-09-18 09:48:41 +02:00
|
|
|
else:
|
2012-10-17 11:14:17 +02:00
|
|
|
if key == '':
|
|
|
|
params.append(value)
|
|
|
|
else:
|
|
|
|
tcp[key] = value
|
2012-12-04 12:06:26 +01:00
|
|
|
calc = calculate(name, callback, params, tcp)
|
2013-06-12 10:22:50 +02:00
|
|
|
if index:
|
|
|
|
ret = calc
|
2012-12-04 12:06:26 +01:00
|
|
|
else:
|
2013-06-12 10:22:50 +02:00
|
|
|
if isinstance(calc, list):
|
|
|
|
ret.extend(calc)
|
|
|
|
else:
|
|
|
|
ret.append(calc)
|
2012-12-04 12:06:26 +01:00
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
return ret
|
|
|
|
else:
|
|
|
|
tcp = {}
|
2012-09-18 09:48:41 +02:00
|
|
|
params = []
|
2012-10-17 11:14:17 +02:00
|
|
|
for key, couples in tcparams.items():
|
|
|
|
for couple in couples:
|
|
|
|
if key == '':
|
|
|
|
value = couple[0]
|
|
|
|
params.append(value)
|
|
|
|
else:
|
|
|
|
tcp[key] = couple[0]
|
2012-09-18 09:48:41 +02:00
|
|
|
return calculate(name, callback, params, tcp)
|
|
|
|
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2012-09-18 09:48:41 +02:00
|
|
|
def calculate(name, callback, params, tcparams):
|
2013-05-23 14:55:52 +02:00
|
|
|
"""wrapper that launches the 'callback'
|
2013-05-10 16:02:27 +02:00
|
|
|
|
|
|
|
:param callback: callback name
|
|
|
|
:param params: in the callback's arity, the unnamed parameters
|
2013-05-21 18:42:56 +02:00
|
|
|
:param tcparams: in the callback's arity, the named parameters
|
2013-05-23 14:55:52 +02:00
|
|
|
|
2013-05-10 16:02:27 +02:00
|
|
|
"""
|
2013-04-22 16:48:13 +02:00
|
|
|
return callback(*params, **tcparams)
|