1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2020 CEA/DEN, EDF R&D, 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")
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
64 self.my_container=my_container._container
65 linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
66 ccode=compile(code,nodeName,'exec')
68 self.context["my_container"] = self.my_container
69 exec(ccode, self.context)
71 def getContainer(self):
72 return self.my_container
80 def defineNewCustomVar(self,varName,valueOfVar):
81 self.context[varName] = pickle.loads(valueOfVar)
84 def executeAnotherPieceOfCode(self,code):
85 """Called for initialization of container lodging self."""
87 ccode=compile(code,self.nodeName,'exec')
88 exec(ccode, self.context)
90 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
92 def execute(self,funcName,argsin):
93 """Execute the function funcName found in local context with pickled args (argsin)"""
95 argsin,kws=pickle.loads(argsin)
96 func=self.context[funcName]
97 argsout=func(*argsin,**kws)
98 argsout=pickle.dumps(argsout,-1)
101 exc_typ,exc_val,exc_fr=sys.exc_info()
102 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
103 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyNode: %s, function: %s" % (self.nodeName,funcName),0))
105 class PyScriptNode_i (Engines__POA.PyScriptNode,Generic):
106 """The implementation of the PyScriptNode CORBA IDL that executes a script"""
107 def __init__(self, nodeName,code,poa,my_container):
108 """Initialize the node : compilation in the local context"""
109 Generic.__init__(self,poa)
110 self.nodeName=nodeName
112 self.my_container=my_container._container
113 linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
114 self.ccode=compile(code,nodeName,'exec')
116 self.context["my_container"] = self.my_container
118 def getContainer(self):
119 return self.my_container
127 def defineNewCustomVar(self,varName,valueOfVar):
128 self.context[varName] = pickle.loads(valueOfVar)
131 def executeAnotherPieceOfCode(self,code):
132 """Called for initialization of container lodging self."""
134 ccode=compile(code,self.nodeName,'exec')
135 exec(ccode, self.context)
137 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
139 def assignNewCompiledCode(self,codeStr):
142 self.ccode=compile(codeStr,self.nodeName,'exec')
144 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode.assignNewCompiledCode (%s) : code to be executed \"%s\"" %(self.nodeName,codeStr),0))
146 def execute(self,outargsname,argsin):
147 """Execute the script stored in attribute ccode with pickled args (argsin)"""
149 argsname,kws=pickle.loads(argsin)
150 self.context.update(kws)
151 exec(self.ccode, self.context)
153 for arg in outargsname:
154 if arg not in self.context:
155 raise KeyError("There is no variable %s in context" % arg)
156 argsout.append(self.context[arg])
157 argsout=pickle.dumps(tuple(argsout),-1)
160 exc_typ,exc_val,exc_fr=sys.exc_info()
161 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
162 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s, outargsname: %s" % (self.nodeName,outargsname),0))
164 def executeFirst(self,argsin):
165 """ Same than first part of self.execute to reduce memory peak."""
168 _,kws=pickle.loads(argsin)
169 self.context.update(kws)
171 exc_typ,exc_val,exc_fr=sys.exc_info()
172 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
173 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode:First %s" % (self.nodeName),0))
175 def executeSecond(self,outargsname):
176 """ Same than second part of self.execute to reduce memory peak."""
178 exec(self.ccode, self.context)
180 for arg in outargsname:
181 if arg not in self.context:
182 raise KeyError("There is no variable %s in context" % arg)
183 argsout.append(self.context[arg])
184 argsout=pickle.dumps(tuple(argsout),-1)
187 exc_typ,exc_val,exc_fr=sys.exc_info()
188 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
189 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode:Second %s, outargsname: %s" % (self.nodeName,outargsname),0))
191 def getValueOfVarInContext(self,varName):
193 return pickle.dumps(self.context[varName],-1)
195 exc_typ,exc_val,exc_fr=sys.exc_info()
196 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
197 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))
200 def assignVarInContext(self, varName, value):
202 self.context[varName][0] = pickle.loads(value)
204 exc_typ,exc_val,exc_fr=sys.exc_info()
205 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
206 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))
209 def callMethodOnVarInContext(self, varName, methodName, args):
211 return pickle.dumps( getattr(self.context[varName][0],methodName)(*pickle.loads(args)),-1 )
213 exc_typ,exc_val,exc_fr=sys.exc_info()
214 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
215 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s" %self.nodeName,0))