1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2019 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 defineNewCustomVar(self,varName,valueOfVar):
72 self.context[varName] = pickle.loads(valueOfVar)
75 def executeAnotherPieceOfCode(self,code):
76 """Called for initialization of container lodging self."""
78 ccode=compile(code,self.nodeName,'exec')
79 exec(ccode, self.context)
81 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
83 def execute(self,funcName,argsin):
84 """Execute the function funcName found in local context with pickled args (argsin)"""
86 argsin,kws=pickle.loads(argsin)
87 func=self.context[funcName]
88 argsout=func(*argsin,**kws)
89 argsout=pickle.dumps(argsout,-1)
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))
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
103 self.my_container=my_container._container
104 linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
105 self.ccode=compile(code,nodeName,'exec')
107 self.context["my_container"] = self.my_container
109 def defineNewCustomVar(self,varName,valueOfVar):
110 self.context[varName] = pickle.loads(valueOfVar)
113 def executeAnotherPieceOfCode(self,code):
114 """Called for initialization of container lodging self."""
116 ccode=compile(code,self.nodeName,'exec')
117 exec(ccode, self.context)
119 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
121 def assignNewCompiledCode(self,codeStr):
124 self.ccode=compile(codeStr,self.nodeName,'exec')
126 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode.assignNewCompiledCode (%s) : code to be executed \"%s\"" %(self.nodeName,codeStr),0))
128 def execute(self,outargsname,argsin):
129 """Execute the script stored in attribute ccode with pickled args (argsin)"""
131 argsname,kws=pickle.loads(argsin)
132 self.context.update(kws)
133 exec(self.ccode, self.context)
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)
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))
146 def getValueOfVarInContext(self,varName):
148 return pickle.dumps(self.context[varName],-1)
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))
155 def assignVarInContext(self, varName, value):
157 self.context[varName][0] = pickle.loads(value)
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))
164 def callMethodOnVarInContext(self, varName, methodName, args):
166 return pickle.dumps( getattr(self.context[varName][0],methodName)(*pickle.loads(args)),-1 )
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))