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