Back to index

salome-kernel  6.5.0
SALOME_PyNode.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 #  -*- coding: iso-8859-1 -*-
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # This library is free software; you can redistribute it and/or
00006 # modify it under the terms of the GNU Lesser General Public
00007 # License as published by the Free Software Foundation; either
00008 # version 2.1 of the License.
00009 #
00010 # This library is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 # Lesser General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU Lesser General Public
00016 # License along with this library; if not, write to the Free Software
00017 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 #
00019 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00020 #
00021 
00022 #  File   : SALOME_PyNode.py
00023 #  Author : Christian CAREMOLI, EDF
00024 #  Module : SALOME
00025 #  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Container/Attic/SALOME_PyNode.py,v 1.1.2.2.14.6.12.1 2012-04-12 14:05:03 vsr Exp $
00026 #
00027 import sys,traceback,string
00028 import linecache
00029 import cPickle
00030 import Engines__POA
00031 import SALOME__POA
00032 import SALOME
00033 
00034 class Generic(SALOME__POA.GenericObj):
00035   """A Python implementation of the GenericObj CORBA IDL"""
00036   def __init__(self,poa):
00037     self.poa=poa
00038     self.cnt=1
00039 
00040   def Register(self):
00041     self.cnt+=1
00042 
00043   def UnRegister(self):
00044     self.cnt-=1
00045     if self.cnt <= 0:
00046       oid=self.poa.servant_to_id(self)
00047       self.poa.deactivate_object(oid)
00048 
00049   def Destroy(self):
00050     print "WARNING SALOME::GenericObj::Destroy() function is obsolete! Use UnRegister() instead."
00051     self.UnRegister()
00052 
00053 class PyNode_i (Engines__POA.PyNode,Generic):
00054   """The implementation of the PyNode CORBA IDL"""
00055   def __init__(self, nodeName,code,poa,my_container):
00056     """Initialize the node : compilation in the local context"""
00057     Generic.__init__(self,poa)
00058     self.nodeName=nodeName
00059     self.code=code
00060     self.my_container=my_container._container
00061     linecache.cache[nodeName]=0,None,string.split(code,'\n'),nodeName
00062     ccode=compile(code,nodeName,'exec')
00063     self.context={}
00064     self.context["my_container"] = self.my_container
00065     exec ccode in self.context
00066 
00067   def execute(self,funcName,argsin):
00068     """Execute the function funcName found in local context with pickled args (argsin)"""
00069     try:
00070       argsin,kws=cPickle.loads(argsin)
00071       func=self.context[funcName]
00072       argsout=func(*argsin,**kws)
00073       argsout=cPickle.dumps(argsout,-1)
00074       return argsout
00075     except:
00076       exc_typ,exc_val,exc_fr=sys.exc_info()
00077       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
00078       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyNode: %s, function: %s" % (self.nodeName,funcName),0))
00079 
00080 class PyScriptNode_i (Engines__POA.PyScriptNode,Generic):
00081   """The implementation of the PyScriptNode CORBA IDL that executes a script"""
00082   def __init__(self, nodeName,code,poa,my_container):
00083     """Initialize the node : compilation in the local context"""
00084     Generic.__init__(self,poa)
00085     self.nodeName=nodeName
00086     self.code=code
00087     self.my_container=my_container._container
00088     linecache.cache[nodeName]=0,None,string.split(code,'\n'),nodeName
00089     self.ccode=compile(code,nodeName,'exec')
00090     self.context={}
00091     self.context["my_container"] = self.my_container
00092 
00093   def execute(self,outargsname,argsin):
00094     """Execute the script stored in attribute ccode with pickled args (argsin)"""
00095     try:
00096       argsname,kws=cPickle.loads(argsin)
00097       self.context.update(kws)
00098       exec self.ccode in self.context
00099       argsout=[]
00100       for arg in outargsname:
00101         if not self.context.has_key(arg):
00102           raise KeyError("There is no variable %s in context" % arg)
00103         argsout.append(self.context[arg])
00104       argsout=cPickle.dumps(tuple(argsout),-1)
00105       return argsout
00106     except:
00107       exc_typ,exc_val,exc_fr=sys.exc_info()
00108       l=traceback.format_exception(exc_typ,exc_val,exc_fr)
00109       raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"PyScriptNode: %s, outargsname: %s" % (self.nodeName,outargsname),0))