]> SALOME platform Git repositories - modules/kernel.git/blob - src/Container/SALOME_PyNode.py
Salome HOME
77dead70281517ac4945c0b87b37f630c3e01a9a
[modules/kernel.git] / src / Container / SALOME_PyNode.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2023  CEA, EDF, 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     pass
57
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
64     self.code=code
65     self.my_container=my_container._container
66     linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
67     ccode=compile(code,nodeName,'exec')
68     self.context={}
69     self.context["my_container"] = self.my_container
70     exec(ccode, self.context)
71
72   def getContainer(self):
73     return self.my_container
74
75   def getCode(self):
76     return self.code
77
78   def getName(self):
79     return self.nodeName
80
81   def defineNewCustomVar(self,varName,valueOfVar):
82     self.context[varName] = pickle.loads(valueOfVar)
83     pass
84
85   def executeAnotherPieceOfCode(self,code):
86     """Called for initialization of container lodging self."""
87     try:
88       ccode=compile(code,self.nodeName,'exec')
89       exec(ccode, self.context)
90     except Exception:
91       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
92
93   def execute(self,funcName,argsin):
94     """Execute the function funcName found in local context with pickled args (argsin)"""
95     try:
96       argsin,kws=pickle.loads(argsin)
97       func=self.context[funcName]
98       argsout=func(*argsin,**kws)
99       argsout=pickle.dumps(argsout,-1)
100       return argsout
101     except Exception:
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))
105
106 class SenderByte_i(SALOME__POA.SenderByte,Generic):
107   def __init__(self,poa,bytesToSend):
108     Generic.__init__(self,poa)
109     self.bytesToSend = bytesToSend
110
111   def getSize(self):
112     return len(self.bytesToSend)
113
114   def sendPart(self,n1,n2):
115     return self.bytesToSend[n1:n2]
116     
117 class SeqByteReceiver:
118   CHUNK_SIZE = 2000000000
119   def __init__(self,sender):
120     self._obj = sender
121   def __del__(self):
122     self._obj.UnRegister()
123     pass
124   def data(self):
125     size = self._obj.getSize()
126     if size <= SeqByteReceiver.CHUNK_SIZE:
127       return self.fetchOneShot( size )
128     else:
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
141
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
148     self.code=code
149     self.my_container=my_container._container
150     linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
151     self.ccode=compile(code,nodeName,'exec')
152     self.context={}
153     self.context["my_container"] = self.my_container
154
155   def getContainer(self):
156     return self.my_container
157
158   def getCode(self):
159     return self.code
160
161   def getName(self):
162     return self.nodeName
163
164   def defineNewCustomVar(self,varName,valueOfVar):
165     self.context[varName] = pickle.loads(valueOfVar)
166     pass
167
168   def executeAnotherPieceOfCode(self,code):
169     """Called for initialization of container lodging self."""
170     try:
171       ccode=compile(code,self.nodeName,'exec')
172       exec(ccode, self.context)
173     except Exception:
174       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
175
176   def assignNewCompiledCode(self,codeStr):
177     try:
178       self.code=codeStr
179       self.ccode=compile(codeStr,self.nodeName,'exec')
180     except Exception:
181       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode.assignNewCompiledCode (%s) : code to be executed \"%s\"" %(self.nodeName,codeStr),0))
182
183   def execute(self,outargsname,argsin):
184     """Execute the script stored in attribute ccode with pickled args (argsin)"""
185     try:
186       argsname,kws=pickle.loads(argsin)
187       self.context.update(kws)
188       exec(self.ccode, self.context)
189       argsout=[]
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)
195       return argsout
196     except Exception:
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))
200
201   def executeFirst(self,argsin):
202     """ Same than first part of self.execute to reduce memory peak."""
203     import time
204     try:
205       data = None
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)
211     except Exception:
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))
215
216   def executeSecond(self,outargsname):
217     """ Same than second part of self.execute to reduce memory peak."""
218     try:
219       exec(self.ccode, self.context)
220       argsout=[]
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 )
230     except Exception:
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))
234
235   def getValueOfVarInContext(self,varName):
236     try:
237       return pickle.dumps(self.context[varName],-1)
238     except Exception:
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))
242     pass
243   
244   def assignVarInContext(self, varName, value):
245     try:
246       self.context[varName][0] = pickle.loads(value)
247     except Exception:
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))
251     pass
252
253   def callMethodOnVarInContext(self, varName, methodName, args):
254     try:
255       return pickle.dumps( getattr(self.context[varName][0],methodName)(*pickle.loads(args)),-1 )
256     except Exception:
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))
260     pass