2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 # File : SALOME_PyNode.py
23 # Author : Christian CAREMOLI, EDF
27 import sys,traceback,string
34 class Generic(SALOME__POA.GenericObj):
35 """A Python implementation of the GenericObj CORBA IDL"""
36 def __init__(self,poa):
46 oid=self.poa.servant_to_id(self)
47 self.poa.deactivate_object(oid)
50 print "WARNING SALOME::GenericObj::Destroy() function is obsolete! Use UnRegister() instead."
53 class PyNode_i (Engines__POA.PyNode,Generic):
54 """The implementation of the PyNode CORBA IDL"""
55 def __init__(self, nodeName,code,poa,my_container):
56 """Initialize the node : compilation in the local context"""
57 Generic.__init__(self,poa)
58 self.nodeName=nodeName
60 self.my_container=my_container._container
61 linecache.cache[nodeName]=0,None,string.split(code,'\n'),nodeName
62 ccode=compile(code,nodeName,'exec')
64 self.context["my_container"] = self.my_container
65 exec ccode in self.context
67 def executeAnotherPieceOfCode(self,code):
68 """Called for initialization of container lodging self."""
70 ccode=compile(code,self.nodeName,'exec')
71 exec ccode in self.context
73 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
75 def execute(self,funcName,argsin):
76 """Execute the function funcName found in local context with pickled args (argsin)"""
78 argsin,kws=cPickle.loads(argsin)
79 func=self.context[funcName]
80 argsout=func(*argsin,**kws)
81 argsout=cPickle.dumps(argsout,-1)
84 exc_typ,exc_val,exc_fr=sys.exc_info()
85 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
86 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyNode: %s, function: %s" % (self.nodeName,funcName),0))
88 class PyScriptNode_i (Engines__POA.PyScriptNode,Generic):
89 """The implementation of the PyScriptNode CORBA IDL that executes a script"""
90 def __init__(self, nodeName,code,poa,my_container):
91 """Initialize the node : compilation in the local context"""
92 Generic.__init__(self,poa)
93 self.nodeName=nodeName
95 self.my_container=my_container._container
96 linecache.cache[nodeName]=0,None,string.split(code,'\n'),nodeName
97 self.ccode=compile(code,nodeName,'exec')
99 self.context["my_container"] = self.my_container
101 def executeAnotherPieceOfCode(self,code):
102 """Called for initialization of container lodging self."""
104 ccode=compile(code,self.nodeName,'exec')
105 exec ccode in self.context
107 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
109 def execute(self,outargsname,argsin):
110 """Execute the script stored in attribute ccode with pickled args (argsin)"""
112 argsname,kws=cPickle.loads(argsin)
113 self.context.update(kws)
114 exec self.ccode in self.context
116 for arg in outargsname:
117 if not self.context.has_key(arg):
118 raise KeyError("There is no variable %s in context" % arg)
119 argsout.append(self.context[arg])
120 argsout=cPickle.dumps(tuple(argsout),-1)
123 exc_typ,exc_val,exc_fr=sys.exc_info()
124 l=traceback.format_exception(exc_typ,exc_val,exc_fr)
125 raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s, outargsname: %s" % (self.nodeName,outargsname),0))