1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2023 CEA, EDF, OPEN CASCADE
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # File : SALOME_PyNode.py
22 # Author : Christian CAREMOLI, EDF
33 class Generic(SALOME__POA.GenericObj):
34 """A Python implementation of the GenericObj CORBA IDL"""
35 def __init__(self,poa):
40 #print("Register called : %d"%self.cnt)
44 #print("UnRegister called : %d"%self.cnt)
47 oid=self.poa.servant_to_id(self)
48 self.poa.deactivate_object(oid)
51 print("WARNING SALOME::GenericObj::Destroy() function is obsolete! Use UnRegister() instead.")
55 #print("Destuctor called")
58 class PyNode_i (Engines__POA.PyNode,Generic):
59 """The implementation of the PyNode CORBA IDL"""
60 def __init__(self, nodeName,code,poa,my_container):
61 """Initialize the node : compilation in the local context"""
62 Generic.__init__(self,poa)
63 self.nodeName=nodeName
65 self.my_container=my_container._container
66 linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
67 ccode=compile(code,nodeName,'exec')
69 self.context["my_container"] = self.my_container
70 exec(ccode, self.context)
72 def getContainer(self):
73 return self.my_container
81 def defineNewCustomVar(self,varName,valueOfVar):
82 self.context[varName] = pickle.loads(valueOfVar)
85 def executeAnotherPieceOfCode(self,code):
86 """Called for initialization of container lodging self."""
88 ccode=compile(code,self.nodeName,'exec')
89 exec(ccode, self.context)
91 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
93 def execute(self,funcName,argsin):
94 """Execute the function funcName found in local context with pickled args (argsin)"""
96 argsin,kws=pickle.loads(argsin)
97 func=self.context[funcName]
98 argsout=func(*argsin,**kws)
99 argsout=pickle.dumps(argsout,-1)
102 exc_typ,exc_val,exc_fr=sys.exc_info()
103 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
104 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyNode: %s, function: %s" % (self.nodeName,funcName),0))
106 class SenderByte_i(SALOME__POA.SenderByte,Generic):
107 def __init__(self,poa,bytesToSend):
108 Generic.__init__(self,poa)
109 self.bytesToSend = bytesToSend
112 return len(self.bytesToSend)
114 def sendPart(self,n1,n2):
115 return self.bytesToSend[n1:n2]
117 class SeqByteReceiver:
118 CHUNK_SIZE = 2000000000
119 def __init__(self,sender):
122 self._obj.UnRegister()
125 size = self._obj.getSize()
126 if size <= SeqByteReceiver.CHUNK_SIZE:
127 return self.fetchOneShot( size )
129 return self.fetchByChunks( size )
130 def fetchOneShot(self,size):
131 return self._obj.sendPart(0,size)
132 def fetchByChunks(self,size):
133 data_for_split_case = bytes(0)
134 EFF_CHUNK_SIZE = SeqByteReceiver.CHUNK_SIZE // 8
135 iStart = 0 ; iEnd = EFF_CHUNK_SIZE
136 while iStart!=iEnd and iEnd <= size:
137 part = self._obj.sendPart(iStart,iEnd)
138 data_for_split_case = bytes(0).join( [data_for_split_case,part] )
139 iStart = iEnd; iEnd = min(iStart + EFF_CHUNK_SIZE,size)
140 return data_for_split_case
142 class PyScriptNode_i (Engines__POA.PyScriptNode,Generic):
143 """The implementation of the PyScriptNode CORBA IDL that executes a script"""
144 def __init__(self, nodeName,code,poa,my_container):
145 """Initialize the node : compilation in the local context"""
146 Generic.__init__(self,poa)
147 self.nodeName=nodeName
149 self.my_container=my_container._container
150 linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
151 self.ccode=compile(code,nodeName,'exec')
153 self.context["my_container"] = self.my_container
155 def getContainer(self):
156 return self.my_container
164 def defineNewCustomVar(self,varName,valueOfVar):
165 self.context[varName] = pickle.loads(valueOfVar)
168 def executeAnotherPieceOfCode(self,code):
169 """Called for initialization of container lodging self."""
171 ccode=compile(code,self.nodeName,'exec')
172 exec(ccode, self.context)
174 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
176 def assignNewCompiledCode(self,codeStr):
179 self.ccode=compile(codeStr,self.nodeName,'exec')
181 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode.assignNewCompiledCode (%s) : code to be executed \"%s\"" %(self.nodeName,codeStr),0))
183 def execute(self,outargsname,argsin):
184 """Execute the script stored in attribute ccode with pickled args (argsin)"""
186 argsname,kws=pickle.loads(argsin)
187 self.context.update(kws)
188 exec(self.ccode, self.context)
190 for arg in outargsname:
191 if arg not in self.context:
192 raise KeyError("There is no variable %s in context" % arg)
193 argsout.append(self.context[arg])
194 argsout=pickle.dumps(tuple(argsout),-1)
197 exc_typ,exc_val,exc_fr=sys.exc_info()
198 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
199 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s, outargsname: %s" % (self.nodeName,outargsname),0))
201 def executeFirst(self,argsin):
202 """ Same than first part of self.execute to reduce memory peak."""
206 if True: # to force call of SeqByteReceiver's destructor
207 argsInPy = SeqByteReceiver( argsin )
208 data = argsInPy.data()
209 _,kws=pickle.loads(data)
210 self.context.update(kws)
212 exc_typ,exc_val,exc_fr=sys.exc_info()
213 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
214 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode:First %s" % (self.nodeName),0))
216 def executeSecond(self,outargsname):
217 """ Same than second part of self.execute to reduce memory peak."""
219 exec(self.ccode, self.context)
221 for arg in outargsname:
222 if arg not in self.context:
223 raise KeyError("There is no variable %s in context" % arg)
224 argsout.append(self.context[arg])
225 argsout=pickle.dumps(tuple(argsout),-1)
226 ret = SenderByte_i( self.poa,argsout )
227 id_o = self.poa.activate_object(ret)
228 retObj = self.poa.id_to_reference(id_o)
229 return retObj._narrow( SALOME.SenderByte )
231 exc_typ,exc_val,exc_fr=sys.exc_info()
232 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
233 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode:Second %s, outargsname: %s" % (self.nodeName,outargsname),0))
235 def getValueOfVarInContext(self,varName):
237 return pickle.dumps(self.context[varName],-1)
239 exc_typ,exc_val,exc_fr=sys.exc_info()
240 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
241 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))
244 def assignVarInContext(self, varName, value):
246 self.context[varName][0] = pickle.loads(value)
248 exc_typ,exc_val,exc_fr=sys.exc_info()
249 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
250 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))
253 def callMethodOnVarInContext(self, varName, methodName, args):
255 return pickle.dumps( getattr(self.context[varName][0],methodName)(*pickle.loads(args)),-1 )
257 exc_typ,exc_val,exc_fr=sys.exc_info()
258 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
259 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))