Salome HOME
bug 53033
[tools/eficas.git] / Ihm / I_FONCTION.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013   EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 import string
21 from I_ASSD import ASSD
22 from Extensions.i18n import tr
23 from Extensions.eficas_exception import EficasException
24
25 class FONCTION(ASSD):
26   def __init__(self,etape=None,sd=None,reg='oui'):
27     if reg=='oui':
28       self.jdc.register_fonction(self)
29
30   def get_formule(self):
31     """
32     Retourne une formule decrivant self sous la forme d'un tuple :
33     (nom,type_retourne,arguments,corps)
34     """
35     if hasattr(self.etape,'get_formule'):
36       # on est dans le cas d'une formule Aster
37       return self.etape.get_formule()
38     else:
39       # on est dans le cas d'une fonction
40       return (self.nom,'REEL','(REEL:x)','''bidon''')
41
42 # On ajoute la classe formule pour etre coherent avec la
43 # modification de C Durand sur la gestion des formules dans le superviseur
44 # On conserve l'ancienne classe fonction (ceinture et bretelles)
45 class fonction(FONCTION) : pass
46
47 from Extensions import param2
48 class formule(FONCTION) : 
49    def __call__(self,*val):
50       if len(val) != len(self.nompar):
51          raise TypeError(" %s() takes exactly %d argument (%d given)" % (self.nom,len(self.nompar),len(val)))
52       return param2.Unop2(self.nom,self.real_call,val)
53
54    def real_call(self,*val):
55       if hasattr(self.parent,'contexte_fichier_init'):
56                         context=self.parent.contexte_fichier_init
57       else            : context={}
58       i=0
59       for param in self.nompar :
60          context[param]=val[i]
61          i=i+1
62       try :
63        res=eval(self.expression,self.jdc.const_context, context)
64       except :
65 ####### A TRAVAILLER DEPUIS ICI !!
66        print tr(75 * '!')
67        print '! ' + tr(string.ljust("Erreur evaluation formule %s", 72), self.nom) + '!'
68        print tr(75 * '!')
69        raise EficasException
70       return res
71
72