Salome HOME
debug intempestif
[tools/eficas.git] / Ihm / I_FONCTION.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2021   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 from __future__ import absolute_import
21 from __future__ import print_function
22 from .I_ASSD import ASSD
23 from Extensions.i18n import tr
24 from Extensions.eficas_exception import EficasException
25
26 class FONCTION(ASSD):
27     def __init__(self,etape=None,sd=None,reg='oui'):
28         if reg=='oui':
29             self.jdc.registerFonction(self)
30
31     def getFormule(self):
32         """
33         Retourne une formule decrivant self sous la forme d'un tuple :
34         (nom,type_retourne,arguments,corps)
35         """
36         if hasattr(self.etape,'getFormule'):
37             # on est dans le cas d'une formule Aster
38             return self.etape.getFormule()
39         else:
40             # on est dans le cas d'une fonction
41             return (self.nom,'REEL','(REEL:x)','''bidon''')
42
43 # On ajoute la classe formule pour etre coherent avec la
44 # modification de C Durand sur la gestion des formules dans le superviseur
45 # On conserve l'ancienne classe fonction (ceinture et bretelles)
46 class fonction(FONCTION) : pass
47
48 from Extensions import param2
49 class formule(FONCTION) :
50     def __call__(self,*val):
51         if len(val) != len(self.nompar):
52             raise TypeError(" %s() takes exactly %d argument (%d given)" % (self.nom,len(self.nompar),len(val)))
53         return param2.Unop2(self.nom,self.realCall,val)
54
55     def realCall(self,*val):
56         if hasattr(self.parent,'contexte_fichier_init'):
57             context=self.parent.contexte_fichier_init
58         else            : context={}
59         i=0
60         for param in self.nompar :
61             context[param]=val[i]
62             i=i+1
63         try :
64             res=eval(self.expression,self.jdc.const_context, context)
65         except :
66             print (75 * '!')
67             print ('! ' + "Erreur evaluation formule %s" % self.nom + 20*'!')
68             print (75 * '!')
69             raise EficasException
70         return res