tiramisu/tiramisu/autolib.py

118 lines
4.8 KiB
Python
Raw Normal View History

2012-07-25 09:04:22 +02:00
# Copyright (C) 2012 Team tiramisu (see AUTHORS for all contributors)
#
# 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
# 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"
2012-10-15 15:06:41 +02:00
from tiramisu.error import PropertiesOptionError, ConflictConfigError
# ____________________________________________________________
# automatic Option object
2012-09-18 09:48:41 +02:00
#def special_owner_factory(name, owner, value,
# 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,
# # we have to carry out a calculation
# return calc_factory(name, callback, callback_params, config)
2012-10-15 15:06:41 +02:00
def carry_out_calculation(name, option, config):
2012-09-18 09:48:41 +02:00
# FIXME we have to know the exact status of the config
# not to disrupt it
# config.freeze()
2012-10-15 15:06:41 +02:00
callback=option.getcallback()
callback_params=option.getcallback_params()
if callback_params is None:
callback_params = {}
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:
path, check_disabled = value
try:
opt_value = config._getattr(path, permissive=True)
opt = config.unwrap_from_path(path)
except PropertiesOptionError, err:
if check_disabled:
continue
raise PropertiesOptionError(err, err.proptype)
is_multi = opt.is_multi()
if is_multi:
if opt_value != None:
len_value = len(opt_value)
if len_multi != 0 and len_multi != len_value:
raise ConflictConfigError('unable to carry out a calculation, '
'option values with multi types must have same length for: '
+ name)
len_multi = len_value
one_is_multi = True
tcparams.setdefault(key, []).append((opt_value, is_multi))
else:
tcparams.setdefault(key, []).append((value, False))
if one_is_multi:
ret = []
for incr in range(len_multi):
tcp = {}
2012-10-17 11:14:17 +02:00
params = []
for key, couples in tcparams.items():
for couple in couples:
value, ismulti = couple
if ismulti and value != None:
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
calc = calculate(name, callback, params, tcp)
if isinstance(calc, list):
ret.extend(calc)
else:
ret.append(calc)
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)
def calculate(name, callback, params, tcparams):
try:
# XXX not only creole...
from creole import eosfunc
2012-09-18 09:48:41 +02:00
return getattr(eosfunc, callback)(*params, **tcparams)
except AttributeError, err:
import traceback
traceback.print_exc()
raise ConflictConfigError("callback: {0} return error {1} for "
"option: {2}".format(callback, str(err), name))