Salome HOME
Updated copyright comment
[modules/kernel.git] / src / KERNEL_PY / salome_pynode.py
1 # Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 #  File   : salome_pynode.py
21 #  Author : Christian CAREMOLI, EDF
22 #  Module : SALOME
23 #  $Header$
24 #
25 """
26  When imported this module adds to CORBA proxy (from PyNode type) automatic pickle and unpickle
27  of arguments and results when calling execute method. It also converts the SALOME exception into a standard python
28  exception
29 """
30 import omniORB
31 import pickle
32 import SALOME
33 import Engines
34
35 class SmartPyNode(Engines._objref_PyNode):
36   def __init__(self):
37     Engines._objref_PyNode.__init__(self)
38
39   def execute(self,functionName,*args,**kws):
40     try:
41       args=pickle.dumps((args,kws),-1)
42       results=Engines._objref_PyNode.execute(self,functionName,args)
43       x=pickle.loads(results)
44       return x
45     except SALOME.SALOME_Exception as e:
46       raise ValueError(e.details.text)
47
48   def __getattr__(self,name):
49     """ a trick to be able to call directly a remote method by its name : no need to use execute"""
50     if name[0]== '_':
51       raise AttributeError(name)
52     def afunc(*args,**kws):
53       return self.execute(name,*args,**kws)
54     return afunc
55
56 class SmartPyScriptNode(Engines._objref_PyScriptNode):
57   def __init__(self):
58     Engines._objref_PyScriptNode.__init__(self)
59
60   def execute(self,outargsname,*args,**kws):
61     #the tuple args are ignored
62     try:
63       args=pickle.dumps(((),kws),-1)
64       results=Engines._objref_PyScriptNode.execute(self,outargsname,args)
65       x=pickle.loads(results)
66       return x
67     except SALOME.SALOME_Exception as e:
68       raise ValueError(e.details.text)
69
70 #Register the new proxy for PyNode
71 omniORB.registerObjref(Engines._objref_PyNode._NP_RepositoryId, SmartPyNode)
72 #Register the new proxy for PyScriptNode
73 omniORB.registerObjref(Engines._objref_PyScriptNode._NP_RepositoryId, SmartPyScriptNode)
74