Salome HOME
Merge branch 'master' into V9_dev
[modules/kernel.git] / src / Container / SALOME_PyNode.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2016  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, or (at your option) any later version.
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
28 import linecache
29 import pickle
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 UnRegister(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   def Destroy(self):
50     print("WARNING SALOME::GenericObj::Destroy() function is obsolete! Use UnRegister() instead.")
51     self.UnRegister()
52
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
59     self.code=code
60     self.my_container=my_container._container
61     linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
62     ccode=compile(code,nodeName,'exec')
63     self.context={}
64     self.context["my_container"] = self.my_container
65     exec(ccode, self.context)
66
67   def defineNewCustomVar(self,varName,valueOfVar):
68     self.context[varName] = pickle.loads(valueOfVar)
69     pass
70
71   def executeAnotherPieceOfCode(self,code):
72     """Called for initialization of container lodging self."""
73     try:
74       ccode=compile(code,self.nodeName,'exec')
75       exec(ccode, self.context)
76     except:
77       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
78
79   def execute(self,funcName,argsin):
80     """Execute the function funcName found in local context with pickled args (argsin)"""
81     try:
82       argsin,kws=pickle.loads(argsin)
83       func=self.context[funcName]
84       argsout=func(*argsin,**kws)
85       argsout=pickle.dumps(argsout,-1)
86       return argsout
87     except:
88       exc_typ,exc_val,exc_fr=sys.exc_info()
89       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
90       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyNode: %s, function: %s" % (self.nodeName,funcName),0))
91
92 class PyScriptNode_i (Engines__POA.PyScriptNode,Generic):
93   """The implementation of the PyScriptNode CORBA IDL that executes a script"""
94   def __init__(self, nodeName,code,poa,my_container):
95     """Initialize the node : compilation in the local context"""
96     Generic.__init__(self,poa)
97     self.nodeName=nodeName
98     self.code=code
99     self.my_container=my_container._container
100     linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
101     self.ccode=compile(code,nodeName,'exec')
102     self.context={}
103     self.context["my_container"] = self.my_container
104
105   def defineNewCustomVar(self,varName,valueOfVar):
106     self.context[varName] = pickle.loads(valueOfVar)
107     pass
108
109   def executeAnotherPieceOfCode(self,code):
110     """Called for initialization of container lodging self."""
111     try:
112       ccode=compile(code,self.nodeName,'exec')
113       exec(ccode, self.context)
114     except:
115       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
116
117   def assignNewCompiledCode(self,codeStr):
118     try:
119       self.code=codeStr
120       self.ccode=compile(codeStr,self.nodeName,'exec')
121     except:
122       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode.assignNewCompiledCode (%s) : code to be executed \"%s\"" %(self.nodeName,codeStr),0))
123
124   def execute(self,outargsname,argsin):
125     """Execute the script stored in attribute ccode with pickled args (argsin)"""
126     try:
127       argsname,kws=pickle.loads(argsin)
128       self.context.update(kws)
129       exec(self.ccode, self.context)
130       argsout=[]
131       for arg in outargsname:
132         if arg not in self.context:
133           raise KeyError("There is no variable %s in context" % arg)
134         argsout.append(self.context[arg])
135       argsout=pickle.dumps(tuple(argsout),-1)
136       return argsout
137     except:
138       exc_typ,exc_val,exc_fr=sys.exc_info()
139       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
140       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s, outargsname: %s" % (self.nodeName,outargsname),0))