Salome HOME
971af63625f244b3cc36ce71d77f8092d4295e8e
[tools/eficas.git] / Noyau / N_FONCTION.py
1 #@ MODIF N_FONCTION Noyau  DATE 28/06/2011   AUTEUR COURTOIS M.COURTOIS 
2 # -*- coding: iso-8859-1 -*-
3 # RESPONSABLE COURTOIS M.COURTOIS
4 #            CONFIGURATION MANAGEMENT OF EDF VERSION
5 # ======================================================================
6 # COPYRIGHT (C) 1991 - 2011  EDF R&D                  WWW.CODE-ASTER.ORG
7 # THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
8 # IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
9 # THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
10 # (AT YOUR OPTION) ANY LATER VERSION.
11 #
12 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
13 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
14 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
15 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.
16 #
17 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
18 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
19 #    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
20 #
21 #
22 # ======================================================================
23
24 # Attention : cet import permet d'avoir, en Python, le comportement
25 # de la division réelle pour les entiers, et non la division entière
26 # 1/2=0.5 (et non 0). Comportement par défaut dans Python 3.0.
27 from __future__ import division
28
29 from N_ASSD import ASSD
30 from N_info import message, SUPERV
31
32 class FONCTION(ASSD):
33     pass
34
35 class formule(ASSD):
36     def __init__(self, *args, **kwargs):
37         ASSD.__init__(self, *args, **kwargs)
38         self.nompar = None
39         self.expression = None
40
41     def __call__(self, *val):
42         context = {}
43         # cas de INCLUDE (ou POURSUITE dans Eficas)
44         context.update(getattr(self.parent, 'contexte_fichier_init', {}))
45         # récupération des constantes locales en cas de MACRO
46         context.update(getattr(self.parent, 'macro_const_context', {}))
47         for param, value in zip(self.nompar, val):
48             context[param] = value
49         try:
50             res = eval(self.expression, self.jdc.const_context, context)
51         except Exception, exc:
52             message.error(SUPERV, "ERREUR LORS DE L'ÉVALUATION DE LA FORMULE '%s' " \
53                           ":\n>> %s",self.nom, str(exc))
54             raise
55         return res
56
57     def setFormule(self, nom_para, texte):
58         """Cette methode sert a initialiser les attributs
59         nompar, expression et code qui sont utilisés
60         dans l'évaluation de la formule."""
61         self.nompar = nom_para
62         self.expression = texte
63         try :
64             self.code = compile(texte, texte, 'eval')
65         except SyntaxError, exc:
66             message.error(SUPERV, "ERREUR LORS DE LA CREATION DE LA FORMULE '%s' " \
67                           ":\n>> %s", self.nom, str(exc))
68             raise
69
70     def __setstate__(self,state):
71         """Cette methode sert a restaurer l'attribut code lors d'un unpickle."""
72         self.__dict__.update(state)                   # update attributes
73         self.setFormule(self.nompar, self.expression) # restore code attribute
74
75     def __getstate__(self):
76         """Pour les formules, il faut enlever l'attribut code qui n'est
77         pas picklable."""
78         d = ASSD.__getstate__(self)
79         del d['code']
80         return d
81
82     def Parametres(self):
83         """Equivalent de fonction.Parametres pour pouvoir utiliser des formules
84         à la place de fonctions dans certaines macro-commandes.
85         """
86         from SD.sd_fonction import sd_formule
87         from Utilitai.Utmess import UTMESS
88         if self.accessible():
89             TypeProl={ 'E':'EXCLU', 'L':'LINEAIRE', 'C':'CONSTANT', 'I':'INTERPRE' }
90             sd = sd_formule(self.get_name())
91             prol = sd.PROL.get()
92             nova = sd.NOVA.get()
93             if prol is None or nova is None:
94                 UTMESS('F', 'SDVERI_2', valk=[objev])
95             dico={
96                 'INTERPOL'    : ['LIN','LIN'],
97                 'NOM_PARA'    : [s.strip() for s in nova],
98                 'NOM_RESU'    : prol[3][0:16].strip(),
99                 'PROL_DROITE' : TypeProl['E'],
100                 'PROL_GAUCHE' : TypeProl['E'],
101             }
102         else:
103             raise Accas.AsException("Erreur dans fonction.Parametres en PAR_LOT='OUI'")
104         return dico
105
106
107 class formule_c(formule):
108     pass
109
110