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):
45 oid=self.poa.servant_to_id(self)
46 self.poa.deactivate_object(oid)
49 print("WARNING SALOME::GenericObj::Destroy() function is obsolete! Use UnRegister() instead.")
52 class PyNode_i (Engines__POA.PyNode,Generic):
53 """The implementation of the PyNode CORBA IDL"""
54 def __init__(self, nodeName,code,poa,my_container):
55 """Initialize the node : compilation in the local context"""
56 Generic.__init__(self,poa)
57 self.nodeName=nodeName
59 self.my_container=my_container._container
60 linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
61 ccode=compile(code,nodeName,'exec')
63 self.context["my_container"] = self.my_container
64 exec(ccode, self.context)
66 def defineNewCustomVar(self,varName,valueOfVar):
67 self.context[varName] = pickle.loads(valueOfVar)
70 def executeAnotherPieceOfCode(self,code):
71 """Called for initialization of container lodging self."""
73 ccode=compile(code,self.nodeName,'exec')
74 exec(ccode, self.context)
76 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
78 def execute(self,funcName,argsin):
79 """Execute the function funcName found in local context with pickled args (argsin)"""
81 argsin,kws=pickle.loads(argsin)
82 func=self.context[funcName]
83 argsout=func(*argsin,**kws)
84 argsout=pickle.dumps(argsout,-1)
87 exc_typ,exc_val,exc_fr=sys.exc_info()
88 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
89 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyNode: %s, function: %s" % (self.nodeName,funcName),0))
91 class PyScriptNode_i (Engines__POA.PyScriptNode,Generic):
92 """The implementation of the PyScriptNode CORBA IDL that executes a script"""
93 def __init__(self, nodeName,code,poa,my_container):
94 """Initialize the node : compilation in the local context"""
95 Generic.__init__(self,poa)
96 self.nodeName=nodeName
98 self.my_container=my_container._container
99 linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
100 self.ccode=compile(code,nodeName,'exec')
102 self.context["my_container"] = self.my_container
104 def defineNewCustomVar(self,varName,valueOfVar):
105 self.context[varName] = pickle.loads(valueOfVar)
108 def executeAnotherPieceOfCode(self,code):
109 """Called for initialization of container lodging self."""
111 ccode=compile(code,self.nodeName,'exec')
112 exec(ccode, self.context)
114 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
116 def assignNewCompiledCode(self,codeStr):
119 self.ccode=compile(codeStr,self.nodeName,'exec')
121 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode.assignNewCompiledCode (%s) : code to be executed \"%s\"" %(self.nodeName,codeStr),0))
123 def execute(self,outargsname,argsin):
124 """Execute the script stored in attribute ccode with pickled args (argsin)"""
126 argsname,kws=pickle.loads(argsin)
127 self.context.update(kws)
128 exec(self.ccode, self.context)
130 for arg in outargsname:
131 if arg not in self.context:
132 raise KeyError("There is no variable %s in context" % arg)
133 argsout.append(self.context[arg])
134 argsout=pickle.dumps(tuple(argsout),-1)
137 exc_typ,exc_val,exc_fr=sys.exc_info()
138 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
139 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s, outargsname: %s" % (self.nodeName,outargsname),0))