Salome HOME
Rename Engines::Component to Engines::EngineComponent
[modules/kernel.git] / src / Container / SALOME_PyNode.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 #  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
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.
9 #
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.
14 #
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
18 #
19 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 #  File   : SALOME_PyNode.py
23 #  Author : Christian CAREMOLI, EDF
24 #  Module : SALOME
25 #  $Header$
26 #
27 import sys,traceback,string
28 import linecache
29 import cPickle
30 import Engines__POA
31 import SALOME__POA
32 import SALOME
33
34 class Generic(SALOME__POA.GenericObj):
35   """A Python implementation of the GenericObj CORBA IDL"""
36   def __init__(self,poa):
37     self.poa=poa
38     self.cnt=1
39
40   def Register(self):
41     self.cnt+=1
42
43   def Destroy(self):
44     self.cnt-=1
45     if self.cnt <= 0:
46       oid=self.poa.servant_to_id(self)
47       self.poa.deactivate_object(oid)
48
49 class PyNode_i (Engines__POA.PyNode,Generic):
50   """The implementation of the PyNode CORBA IDL"""
51   def __init__(self, nodeName,code,poa,my_container):
52     """Initialize the node : compilation in the local context"""
53     Generic.__init__(self,poa)
54     self.nodeName=nodeName
55     self.code=code
56     self.my_container=my_container._container
57     linecache.cache[nodeName]=0,None,string.split(code,'\n'),nodeName
58     ccode=compile(code,nodeName,'exec')
59     self.context={}
60     self.context["my_container"] = self.my_container
61     exec ccode in self.context
62
63   def execute(self,funcName,argsin):
64     """Execute the function funcName found in local context with pickled args (argsin)"""
65     try:
66       argsin,kws=cPickle.loads(argsin)
67       func=self.context[funcName]
68       argsout=func(*argsin,**kws)
69       argsout=cPickle.dumps(argsout,-1)
70       return argsout
71     except:
72       exc_typ,exc_val,exc_fr=sys.exc_info()
73       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
74       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyNode: %s, function: %s" % (self.nodeName,funcName),0))
75
76 class PyScriptNode_i (Engines__POA.PyScriptNode,Generic):
77   """The implementation of the PyScriptNode CORBA IDL that executes a script"""
78   def __init__(self, nodeName,code,poa,my_container):
79     """Initialize the node : compilation in the local context"""
80     Generic.__init__(self,poa)
81     self.nodeName=nodeName
82     self.code=code
83     self.my_container=my_container._container
84     linecache.cache[nodeName]=0,None,string.split(code,'\n'),nodeName
85     self.ccode=compile(code,nodeName,'exec')
86     self.context={}
87     self.context["my_container"] = self.my_container
88
89   def execute(self,outargsname,argsin):
90     """Execute the script stored in attribute ccode with pickled args (argsin)"""
91     try:
92       argsname,kws=cPickle.loads(argsin)
93       self.context.update(kws)
94       exec self.ccode in self.context
95       argsout=[]
96       for arg in outargsname:
97         if not self.context.has_key(arg):
98           raise KeyError("There is no variable %s in context" % arg)
99         argsout.append(self.context[arg])
100       argsout=cPickle.dumps(tuple(argsout),-1)
101       return argsout
102     except:
103       exc_typ,exc_val,exc_fr=sys.exc_info()
104       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
105       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s, outargsname: %s" % (self.nodeName,outargsname),0))