]> SALOME platform Git repositories - modules/kernel.git/blob - src/Container/SALOME_PyNode.py
Salome HOME
CCAR: add the PyNode object that can be created in a container
[modules/kernel.git] / src / Container / SALOME_PyNode.py
1 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
6 #  This library is free software; you can redistribute it and/or
7 #  modify it under the terms of the GNU Lesser General Public
8 #  License as published by the Free Software Foundation; either
9 #  version 2.1 of the License.
10 #
11 #  This library is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 #  Lesser General Public License for more details.
15 #
16 #  You should have received a copy of the GNU Lesser General Public
17 #  License along with this library; if not, write to the Free Software
18 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22 #  File   : SALOME_PyNode.py
23 #  Author : Christian CAREMOLI, EDF
24 #  Module : SALOME
25 #  $Header$
26 #
27
28 import sys,traceback,string
29 import linecache
30 import cPickle
31 import Engines__POA
32 import SALOME__POA
33 import SALOME
34
35 class Generic(SALOME__POA.GenericObj):
36   """A Python implementation of the GenericObj CORBA IDL"""
37   def __init__(self,poa):
38     self.poa=poa
39     self.cnt=1
40
41   def Register(self):
42     self.cnt+=1
43
44   def Destroy(self):
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 class PyNode_i (Engines__POA.PyNode,Generic):
51   """The implementation of the PyNode CORBA IDL"""
52   def __init__(self, nodeName,code,poa):
53     """Initialize the node : compilation in the local context"""
54     Generic.__init__(self,poa)
55     self.nodeName=nodeName
56     self.code=code
57     linecache.cache[nodeName]=0,None,string.split(code,'\n'),nodeName
58     ccode=compile(code,nodeName,'exec')
59     self.context={}
60     exec ccode in self.context
61
62   def execute(self,funcName,argsin): 
63     """Execute the function funcName found in local context with pickled args (argsin)"""
64     try:
65       argsin,kws=cPickle.loads(argsin)
66       func=self.context[funcName]
67       argsout=func(*argsin,**kws)
68       argsout=cPickle.dumps(argsout,-1)
69       return argsout
70     except:
71       exc_typ,exc_val,exc_fr=sys.exc_info()
72       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
73       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyNode: %s, function: %s" % (self.nodeName,funcName),0))
74