2012-05-14 10:18:01 +02:00
|
|
|
# Copyright (C) 2012 Team tiramisu (see README 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
|
|
|
|
#
|
|
|
|
# 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
|
2012-05-24 15:07:06 +02:00
|
|
|
# ____________________________________________________________
|
2012-05-13 20:48:51 +02:00
|
|
|
"enables us to carry out a calculation and return an option's value"
|
2012-07-06 11:46:08 +02:00
|
|
|
from tiramisu.error import DisabledOptionError, SpecialOwnersError
|
2012-05-24 15:07:06 +02:00
|
|
|
# ____________________________________________________________
|
|
|
|
# automatic Option object
|
|
|
|
special_owners = ['auto', 'fill']
|
|
|
|
|
2012-07-03 17:19:55 +02:00
|
|
|
def special_owner_factory(name, owner, value,
|
|
|
|
callback, callback_params=None, config=None):
|
|
|
|
# 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-05-24 15:07:06 +02:00
|
|
|
|
2012-07-03 17:19:55 +02:00
|
|
|
def calc_factory(name, callback, callback_params, config):
|
|
|
|
# FIXME we have to know the exact status of the config
|
|
|
|
# not to disrupt it
|
|
|
|
# config.freeze()
|
|
|
|
if callback_params is None:
|
|
|
|
callback_params = {}
|
2012-07-04 14:46:14 +02:00
|
|
|
tcparams = {}
|
2012-07-06 09:57:35 +02:00
|
|
|
one_is_multi = False
|
|
|
|
len_multi = 0
|
2012-07-03 17:19:55 +02:00
|
|
|
for key, value in callback_params.items():
|
|
|
|
if type(value) == tuple:
|
|
|
|
path, check_disabled = value
|
|
|
|
try:
|
|
|
|
opt_value = getattr(config, path)
|
2012-07-06 09:57:35 +02:00
|
|
|
opt = config.unwrap_from_path(path)
|
2012-07-03 17:19:55 +02:00
|
|
|
except DisabledOptionError, e:
|
|
|
|
if chek_disabled:
|
|
|
|
continue
|
|
|
|
raise DisabledOptionError(e)
|
2012-07-06 09:57:35 +02:00
|
|
|
is_multi = opt.is_multi()
|
|
|
|
if is_multi:
|
2012-07-06 11:46:08 +02:00
|
|
|
if opt_value != None:
|
|
|
|
len_value = len(opt_value)
|
|
|
|
if len_multi != 0 and len_multi != len_value:
|
|
|
|
raise SpecialOwnersError('unable to carry out a calculation, '
|
|
|
|
'option values with multi types must have same length for: '
|
|
|
|
+ name)
|
|
|
|
len_multi = len_value
|
2012-07-06 09:57:35 +02:00
|
|
|
one_is_multi = True
|
|
|
|
tcparams[key] = (opt_value, is_multi)
|
2012-07-04 14:46:14 +02:00
|
|
|
else:
|
2012-07-06 09:57:35 +02:00
|
|
|
tcparams[key] = (value, False)
|
|
|
|
|
|
|
|
if one_is_multi:
|
|
|
|
ret = []
|
|
|
|
for incr in range(len_multi):
|
|
|
|
tcp = {}
|
|
|
|
for key, couple in tcparams.items():
|
|
|
|
value, ismulti = couple
|
2012-07-06 11:46:08 +02:00
|
|
|
if ismulti and value != None:
|
2012-07-06 09:57:35 +02:00
|
|
|
tcp[key] = value[incr]
|
|
|
|
else:
|
|
|
|
tcp[key] = value
|
2012-07-06 11:46:08 +02:00
|
|
|
ret.append(calculate(name, callback, tcp))
|
2012-07-06 09:57:35 +02:00
|
|
|
return ret
|
|
|
|
else:
|
2012-07-06 11:46:08 +02:00
|
|
|
tcp = {}
|
|
|
|
for key, couple in tcparams.items():
|
|
|
|
tcp[key] = couple[0]
|
|
|
|
return calculate(name, callback, tcp)
|
2012-07-06 09:57:35 +02:00
|
|
|
|
2012-07-06 11:46:08 +02:00
|
|
|
def calculate(name, callback, tcparams):
|
2012-05-24 15:07:06 +02:00
|
|
|
try:
|
2012-07-06 11:46:08 +02:00
|
|
|
# XXX not only creole...
|
|
|
|
from creole import eosfunc
|
2012-07-04 14:46:14 +02:00
|
|
|
return getattr(eosfunc, callback)(**tcparams)
|
2012-07-06 11:46:08 +02:00
|
|
|
except AttributeError, err:
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
raise SpecialOwnersError("callback: {0} return error {1} for "
|
|
|
|
"option: {2}".format(callback, str(err), name))
|
2012-07-06 09:57:35 +02:00
|
|
|
|