]> SALOME platform Git repositories - modules/kernel.git/blob - src/Container/SALOME_PyNode.py
Salome HOME
Merge branch 'V9_2_2_BR'
[modules/kernel.git] / src / Container / SALOME_PyNode.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 #  File   : SALOME_PyNode.py
22 #  Author : Christian CAREMOLI, EDF
23 #  Module : SALOME
24 #  $Header$
25 #
26 import sys,traceback
27 import linecache
28 import pickle
29 import Engines__POA
30 import SALOME__POA
31 import SALOME
32
33 class Generic(SALOME__POA.GenericObj):
34   """A Python implementation of the GenericObj CORBA IDL"""
35   def __init__(self,poa):
36     self.poa=poa
37     self.cnt=1
38
39   def Register(self):
40     self.cnt+=1
41
42   def UnRegister(self):
43     self.cnt-=1
44     if self.cnt <= 0:
45       oid=self.poa.servant_to_id(self)
46       self.poa.deactivate_object(oid)
47
48   def Destroy(self):
49     print("WARNING SALOME::GenericObj::Destroy() function is obsolete! Use UnRegister() instead.")
50     self.UnRegister()
51
52 class PyNode_i (Engines__POA.PyNode,Generic):
53   """The implementation of the PyNode CORBA IDL"""
54   def __init__(self, nodeName,code,poa,my_container):
55     """Initialize the node : compilation in the local context"""
56     Generic.__init__(self,poa)
57     self.nodeName=nodeName
58     self.code=code
59     self.my_container=my_container._container
60     linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
61     ccode=compile(code,nodeName,'exec')
62     self.context={}
63     self.context["my_container"] = self.my_container
64     exec(ccode, self.context)
65
66   def defineNewCustomVar(self,varName,valueOfVar):
67     self.context[varName] = pickle.loads(valueOfVar)
68     pass
69
70   def executeAnotherPieceOfCode(self,code):
71     """Called for initialization of container lodging self."""
72     try:
73       ccode=compile(code,self.nodeName,'exec')
74       exec(ccode, self.context)
75     except:
76       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
77
78   def execute(self,funcName,argsin):
79     """Execute the function funcName found in local context with pickled args (argsin)"""
80     try:
81       argsin,kws=pickle.loads(argsin)
82       func=self.context[funcName]
83       argsout=func(*argsin,**kws)
84       argsout=pickle.dumps(argsout,-1)
85       return argsout
86     except:
87       exc_typ,exc_val,exc_fr=sys.exc_info()
88       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
89       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyNode: %s, function: %s" % (self.nodeName,funcName),0))
90
91 class PyScriptNode_i (Engines__POA.PyScriptNode,Generic):
92   """The implementation of the PyScriptNode CORBA IDL that executes a script"""
93   def __init__(self, nodeName,code,poa,my_container):
94     """Initialize the node : compilation in the local context"""
95     Generic.__init__(self,poa)
96     self.nodeName=nodeName
97     self.code=code
98     self.my_container=my_container._container
99     linecache.cache[nodeName]=0,None,code.split('\n'),nodeName
100     self.ccode=compile(code,nodeName,'exec')
101     self.context={}
102     self.context["my_container"] = self.my_container
103
104   def defineNewCustomVar(self,varName,valueOfVar):
105     self.context[varName] = pickle.loads(valueOfVar)
106     pass
107
108   def executeAnotherPieceOfCode(self,code):
109     """Called for initialization of container lodging self."""
110     try:
111       ccode=compile(code,self.nodeName,'exec')
112       exec(ccode, self.context)
113     except:
114       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode (%s) : code to be executed \"%s\"" %(self.nodeName,code),0))
115
116   def assignNewCompiledCode(self,codeStr):
117     try:
118       self.code=codeStr
119       self.ccode=compile(codeStr,self.nodeName,'exec')
120     except:
121       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"","PyScriptNode.assignNewCompiledCode (%s) : code to be executed \"%s\"" %(self.nodeName,codeStr),0))
122
123   def execute(self,outargsname,argsin):
124     """Execute the script stored in attribute ccode with pickled args (argsin)"""
125     try:
126       argsname,kws=pickle.loads(argsin)
127       self.context.update(kws)
128       exec(self.ccode, self.context)
129       argsout=[]
130       for arg in outargsname:
131         if arg not in self.context:
132           raise KeyError("There is no variable %s in context" % arg)
133         argsout.append(self.context[arg])
134       argsout=pickle.dumps(tuple(argsout),-1)
135       return argsout
136     except:
137       exc_typ,exc_val,exc_fr=sys.exc_info()
138       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
139       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s, outargsname: %s" % (self.nodeName,outargsname),0))