Salome HOME
48a839e8c8c31e0627aee67aa6cb84695d6946a3
[modules/kernel.git] / src / Container / SALOME_PyNode.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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
21 #  File   : SALOME_PyNode.py
22 #  Author : Christian CAREMOLI, EDF
23 #  Module : SALOME
24 #  $Header$
25 #
26 import sys,traceback
27 import linecache
28 import pickle
29 import Engines__POA
30 import SALOME__POA
31 import SALOME
32
33 class Generic(SALOME__POA.GenericObj):
34   """A Python implementation of the GenericObj CORBA IDL"""
35   def __init__(self,poa):
36     self.poa=poa
37     self.cnt=1
38
39   def Register(self):
40     print("Register called : %d"%self.cnt)
41     self.cnt+=1
42
43   def UnRegister(self):
44     print("UnRegister called : %d"%self.cnt)
45     self.cnt-=1
46     if self.cnt <= 0:
47       oid=self.poa.servant_to_id(self)
48       self.poa.deactivate_object(oid)
49
50   def Destroy(self):
51     print("WARNING SALOME::GenericObj::Destroy() function is obsolete! Use UnRegister() instead.")
52     self.UnRegister()
53
54   def __del__(self):
55     print("Destuctor called")
56
57 class PyNode_i (Engines__POA.PyNode,Generic):
58   """The implementation of the PyNode CORBA IDL"""
59   def __init__(self, nodeName,code,poa,my_container):
60     """Initialize the node : compilation in the local context"""
61     Generic.__init__(self,poa)
62     self.nodeName=nodeName
63     self.code=code
64     self.my_container=my_container._container
65     linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
66     ccode=compile(code,nodeName,'exec')
67     self.context={}
68     self.context["my_container"] = self.my_container
69     exec(ccode, self.context)
70
71   def defineNewCustomVar(self,varName,valueOfVar):
72     self.context[varName] = pickle.loads(valueOfVar)
73     pass
74
75   def executeAnotherPieceOfCode(self,code):
76     """Called for initialization of container lodging self."""
77     try:
78       ccode=compile(code,self.nodeName,'exec')
79       exec(ccode, self.context)
80     except:
81       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
82
83   def execute(self,funcName,argsin):
84     """Execute the function funcName found in local context with pickled args (argsin)"""
85     try:
86       argsin,kws=pickle.loads(argsin)
87       func=self.context[funcName]
88       argsout=func(*argsin,**kws)
89       argsout=pickle.dumps(argsout,-1)
90       return argsout
91     except:
92       exc_typ,exc_val,exc_fr=sys.exc_info()
93       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
94       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyNode: %s, function: %s" % (self.nodeName,funcName),0))
95
96 class PyScriptNode_i (Engines__POA.PyScriptNode,Generic):
97   """The implementation of the PyScriptNode CORBA IDL that executes a script"""
98   def __init__(self, nodeName,code,poa,my_container):
99     """Initialize the node : compilation in the local context"""
100     Generic.__init__(self,poa)
101     self.nodeName=nodeName
102     self.code=code
103     self.my_container=my_container._container
104     linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
105     self.ccode=compile(code,nodeName,'exec')
106     self.context={}
107     self.context["my_container"] = self.my_container
108
109   def defineNewCustomVar(self,varName,valueOfVar):
110     self.context[varName] = pickle.loads(valueOfVar)
111     pass
112
113   def executeAnotherPieceOfCode(self,code):
114     """Called for initialization of container lodging self."""
115     try:
116       ccode=compile(code,self.nodeName,'exec')
117       exec(ccode, self.context)
118     except:
119       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
120
121   def assignNewCompiledCode(self,codeStr):
122     try:
123       self.code=codeStr
124       self.ccode=compile(codeStr,self.nodeName,'exec')
125     except:
126       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode.assignNewCompiledCode (%s) : code to be executed \"%s\"" %(self.nodeName,codeStr),0))
127
128   def execute(self,outargsname,argsin):
129     """Execute the script stored in attribute ccode with pickled args (argsin)"""
130     try:
131       argsname,kws=pickle.loads(argsin)
132       self.context.update(kws)
133       exec(self.ccode, self.context)
134       argsout=[]
135       for arg in outargsname:
136         if arg not in self.context:
137           raise KeyError("There is no variable %s in context" % arg)
138         argsout.append(self.context[arg])
139       argsout=pickle.dumps(tuple(argsout),-1)
140       return argsout
141     except:
142       exc_typ,exc_val,exc_fr=sys.exc_info()
143       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
144       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s, outargsname: %s" % (self.nodeName,outargsname),0))
145
146   def getValueOfVarInContext(self,varName):
147     try:
148       return pickle.dumps(self.context[varName],-1)
149     except:
150       exc_typ,exc_val,exc_fr=sys.exc_info()
151       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
152       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))
153     pass
154   
155   def assignVarInContext(self, varName, value):
156     try:
157       self.context[varName][0] = pickle.loads(value)
158     except:
159       exc_typ,exc_val,exc_fr=sys.exc_info()
160       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
161       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))
162     pass
163
164   def callMethodOnVarInContext(self, varName, methodName, args):
165     try:
166       return pickle.dumps( getattr(self.context[varName][0],methodName)(*pickle.loads(args)),-1 )
167     except:
168       exc_typ,exc_val,exc_fr=sys.exc_info()
169       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
170       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))
171     pass