string getScopeName();
StringVec listVars();
BasicDataServer retrieveVar(in string varName);
- StringDataServer createGlobalStringVar(in string varName);
+ StringDataServer createGlobalStringVar(in string typeName, in string varName);
StringDataServer createGlobalTmpVar(in ByteVec newValue);
void shutdownIfNotHostedByDSM();
};
#
# Author : Anthony Geay
+# dict,list,tuple,int,float,str
import SALOME
import cPickle
-class SALOMEGlobalVarHelper:
+class List(object):
def __init__(self,varPtr,isTemporaryVar=False):
assert(isinstance(varPtr,SALOME._objref_StringDataServer))
self._var_ptr=varPtr
def __del__(self):
self._var_ptr.UnRegister()
pass
-
+
+ def assign(self,elt):
+ st=cPickle.dumps(elt,cPickle.HIGHEST_PROTOCOL)
+ self._var_ptr.setSerializedContent(st)
+ pass
+
+ def __getitem__(self,*args):
+ ret=Caller(self._var_ptr,"__getitem__")
+ return ret(*args)
+
+ def __setitem__(self,*args):
+ ret=Caller(self._var_ptr,"__setitem__")
+ return ret(*args)
+
+ def append(self,*args):
+ ret=Caller(self._var_ptr,"append")
+ return ret(*args)
+
+ def __str__(self):
+ return self.local_copy().__str__()
+
+ def __repr__(self):
+ return self.local_copy().__repr__()
+
+ def local_copy(self):
+ return cPickle.loads(self._var_ptr.fetchSerializedContent())
+
+ def __reduce__(self):
+ return (list,(self.local_copy(),))
+
+ pass
+
+class Tuple(object):
+ def __init__(self,varPtr,isTemporaryVar=False):
+ assert(isinstance(varPtr,SALOME._objref_StringDataServer))
+ self._var_ptr=varPtr
+ if not isTemporaryVar:
+ self._var_ptr.Register()
+ self._is_temp=isTemporaryVar
+ pass
+
+ def __del__(self):
+ self._var_ptr.UnRegister()
+ pass
+
def assign(self,elt):
st=cPickle.dumps(elt,cPickle.HIGHEST_PROTOCOL)
self._var_ptr.setSerializedContent(st)
pass
+
+ def __getitem__(self,*args):
+ ret=Caller(self._var_ptr,"__getitem__")
+ return ret(*args)
+
+ def __setitem__(self,*args):
+ ret=Caller(self._var_ptr,"__setitem__")
+ return ret(*args)
+
+ def __str__(self):
+ return self.local_copy().__str__()
+
+ def __repr__(self):
+ return self.local_copy().__repr__()
+
+ def local_copy(self):
+ return cPickle.loads(self._var_ptr.fetchSerializedContent())
+
+ def __reduce__(self):
+ return (tuple,(self.local_copy(),))
+
+ pass
- def __getattr__(self,meth):
- #print "__getattr__,%s"%(meth)
- return SALOMEGlobalVarMethodHelper(self._var_ptr,meth)
+
+class Int(object):
+ def __init__(self,varPtr,isTemporaryVar=False):
+ assert(isinstance(varPtr,SALOME._objref_StringDataServer))
+ self._var_ptr=varPtr
+ if not isTemporaryVar:
+ self._var_ptr.Register()
+ self._is_temp=isTemporaryVar
+ pass
+
+ def __del__(self):
+ self._var_ptr.UnRegister()
+ pass
+
+ def __iadd__(self,*args):
+ ret=Caller(self._var_ptr,"__add__")
+ return ret(*args)
+
+ def __isub__(self,*args):
+ ret=Caller(self._var_ptr,"__sub__")
+ return ret(*args)
+
+ def assign(self,elt):
+ st=cPickle.dumps(elt,cPickle.HIGHEST_PROTOCOL)
+ self._var_ptr.setSerializedContent(st)
+ pass
def __str__(self):
return self.local_copy().__str__()
return self.local_copy().__repr__()
def local_copy(self):
- return cPickle.loads(self._var_ptr.fetchSerializedContent())
+ return cPickle.loads(self._var_ptr.fetchSerializedContent())
+
+ def __reduce__(self):
+ return (int,(self.local_copy(),))
+
pass
-class SALOMEGlobalVarMethodHelper:
+class Dict(object):
+ def __init__(self,varPtr,isTemporaryVar=False):
+ assert(isinstance(varPtr,SALOME._objref_StringDataServer))
+ self._var_ptr=varPtr
+ if not isTemporaryVar:
+ self._var_ptr.Register()
+ self._is_temp=isTemporaryVar
+ pass
+
+ def __del__(self):
+ self._var_ptr.UnRegister()
+ pass
+
+ def assign(self,elt):
+ st=cPickle.dumps(elt,cPickle.HIGHEST_PROTOCOL)
+ self._var_ptr.setSerializedContent(st)
+ pass
+
+ def __getitem__(self,*args):
+ ret=Caller(self._var_ptr,"__getitem__")
+ return ret(*args)
+
+ def __setitem__(self,*args):
+ ret=Caller(self._var_ptr,"__setitem__")
+ return ret(*args)
+
+ def __len__(self):
+ return len(self.local_copy())
+
+ def __str__(self):
+ return self.local_copy().__str__()
+
+ def __repr__(self):
+ return self.local_copy().__repr__()
+
+ def local_copy(self):
+ return cPickle.loads(self._var_ptr.fetchSerializedContent())
+
+ def __reduce__(self):
+ return (dict,(self.local_copy(),))
+
+ pass
+
+class Caller:
def __init__(self,varPtr,meth):
assert(isinstance(varPtr,SALOME._objref_StringDataServer))
self._var_ptr=varPtr
- self._var_ptr.Register()
self._meth=meth
pass
def __call__(self,*args):
- #print "__call__ %s : %s"%(self._meth,str(args))
ret=self._var_ptr.invokePythonMethodOn(self._meth,cPickle.dumps(args,cPickle.HIGHEST_PROTOCOL))
- ret=SALOMEGlobalVarHelper(ret,True)
- if self._meth not in ["__len__","__coerce__"]:
- return ret
- else:
- return ret.local_copy()
-
- def __del__(self):
- self._var_ptr.UnRegister()
- pass
+ return GetHandlerFromRef(ret,True)
pass
+
+PyHandlerTypeMap={int:Int,list:List,tuple:Tuple,dict:Dict}
+
+def GetHandlerFromRef(objCorba,isTempVar=False):
+ v=cPickle.loads(objCorba.fetchSerializedContent())
+ if v is None:
+ return None
+ return PyHandlerTypeMap[v.__class__](objCorba,isTempVar)
/*!
* Called remotely -> to protect against throw
*/
-SALOME::StringDataServer_ptr DataScopeServer::createGlobalStringVar(const char *varName)
+SALOME::StringDataServer_ptr DataScopeServer::createGlobalStringVar(const char *typeName, const char *varName)
{
- std::string varNameCpp(varName);
+ std::string varNameCpp(varName),typeNameCpp(typeName);
std::vector<std::string> allNames(getAllVarNames());
std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varNameCpp));
if(it!=allNames.end())
std::ostringstream oss; oss << "DataScopeServer::createGlobalStringVar : name \"" << varNameCpp << "\" already exists !";
throw Exception(oss.str());
}
- AutoRefCountPtr<StringDataServer> tmp(new StringDataServer(this,varNameCpp));
+ AutoRefCountPtr<StringDataServer> tmp(new StringDataServer(this,typeNameCpp,varNameCpp));
CORBA::Object_var ret(activateWithDedicatedPOA(tmp));
std::pair< SALOME::BasicDataServer_var, AutoRefCountPtr<BasicDataServer> > p(SALOME::BasicDataServer::_narrow(ret),DynamicCastSafe<StringDataServer,BasicDataServer>(tmp));
_vars.push_back(p);
{
static const char TMP_VAR_NAME[]="TmP";
std::string vn(BuildTmpVarNameFrom(TMP_VAR_NAME));
- StringDataServer *retCpp(new StringDataServer(this,vn));
+ StringDataServer *retCpp(new StringDataServer(this,vn,newValue));
retCpp->setPOA(_poa);
- retCpp->setSerializedContent(newValue);
//
PortableServer::ObjectId_var id(_poa->activate_object(retCpp));
CORBA::Object_var ret(_poa->id_to_reference(id));
char *getScopeName();
SALOME::StringVec *listVars();
SALOME::BasicDataServer_ptr retrieveVar(const char *varName);
- SALOME::StringDataServer_ptr createGlobalStringVar(const char *varName);
+ SALOME::StringDataServer_ptr createGlobalStringVar(const char *typeName, const char *varName);
SALOME::StringDataServer_ptr createGlobalTmpVar(const SALOME::ByteVec& newValue);
void shutdownIfNotHostedByDSM();
~DataScopeServer();
using namespace SALOMESDS;
-StringDataServer::StringDataServer(DataScopeServer *father, const std::string& varName):BasicDataServer(father,varName),_self(Py_None)
+StringDataServer::StringDataServer(DataScopeServer *father, const std::string& typeName, const std::string& varName):BasicDataServer(father,varName),_self(0)
{
- Py_XINCREF(Py_None);
+ _self=CreateDftObjFromType(father->getGlobals(),typeName);
+}
+
+StringDataServer::StringDataServer(DataScopeServer *father, const std::string& varName, const SALOME::ByteVec& value):BasicDataServer(father,varName),_self(0)
+{
+ setSerializedContentInternal(value);
}
//! obj is consumed
*/
void StringDataServer::setSerializedContent(const SALOME::ByteVec& newValue)
{
- checkReadOnlyStatusRegardingConstness("StringDataServer::setSerializedContent : read only var !");
- std::string data;
- FromByteSeqToCpp(newValue,data);
- setNewPyObj(getPyObjFromPickled(data));
+ setSerializedContentInternal(newValue);
}
/*!
Py_XDECREF(_self);
_self=obj;
}
+
+void StringDataServer::setSerializedContentInternal(const SALOME::ByteVec& newValue)
+{
+ checkReadOnlyStatusRegardingConstness("StringDataServer::setSerializedContent : read only var !");
+ std::string data;
+ FromByteSeqToCpp(newValue,data);
+ setNewPyObj(getPyObjFromPickled(data));
+}
+
+PyObject *StringDataServer::CreateDftObjFromType(PyObject *globals, const std::string& typeName)
+{
+ PyObject *builtins(PyDict_GetItemString(globals,"__builtins__"));
+ if(!builtins)
+ throw Exception("StringDataServer constructor : no __builtins__ in globals !");
+ PyObject *builtins2(PyModule_GetDict(builtins));
+ if(!builtins2)
+ throw Exception("StringDataServer constructor : internal error fail to invoke __dict__ on __builtins__ !");
+ PyObject *tmp(PyDict_GetItemString(builtins2,typeName.c_str()));
+ if(!tmp)
+ {
+ std::ostringstream oss; oss << "StringDataServer::CreateDftObjFromType : Invalid type name \"" << typeName << "\" !";
+ throw Exception(oss.str());
+ }
+ PyObject *args(PyTuple_New(0));
+ PyObject *ret(PyObject_CallObject(tmp,args));
+ Py_XDECREF(args);
+ return ret;
+}
class StringDataServer : public BasicDataServer, public virtual POA_SALOME::StringDataServer
{
public:
- StringDataServer(DataScopeServer *father, const std::string& varName);
+ StringDataServer(DataScopeServer *father, const std::string& typeName, const std::string& varName);
+ StringDataServer(DataScopeServer *father, const std::string& varName, const SALOME::ByteVec& value);
StringDataServer(DataScopeServer *father, const std::string& varName, PyObject *obj);
~StringDataServer();
void setSerializedContent(const SALOME::ByteVec& newValue);
PyObject *getPyObjFromPickled(const std::string& pickledData);
std::string pickelize(PyObject *obj);
void setNewPyObj(PyObject *obj);
+ void setSerializedContentInternal(const SALOME::ByteVec& newValue);
+ static PyObject *CreateDftObjFromType(PyObject *globals, const std::string& typeName);
private:
static const char FAKE_VAR_NAME_FOR_WORK[];
PyObject *_self;
--- /dev/null
+# -*- coding: iso-8859-1 -*-
+# Copyright (C) 2007-2014 CEA/DEN, EDF R&D
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
+# Author : Anthony Geay
+
+# dict,list,tuple,int,float,str
+import SALOME
+import cPickle
+
+class List(object):
+ def __init__(self,varPtr,isTemporaryVar=False):
+ assert(isinstance(varPtr,SALOME._objref_StringDataServer))
+ self._var_ptr=varPtr
+ if not isTemporaryVar:
+ self._var_ptr.Register()
+ self._is_temp=isTemporaryVar
+ pass
+
+ def __del__(self):
+ self._var_ptr.UnRegister()
+ pass
+
+ def assign(self,elt):
+ st=cPickle.dumps(elt,cPickle.HIGHEST_PROTOCOL)
+ self._var_ptr.setSerializedContent(st)
+ pass
+
+ def __getitem__(self,*args):
+ ret=Caller(self._var_ptr,"__getitem__")
+ return ret(*args)
+
+ def __setitem__(self,*args):
+ ret=Caller(self._var_ptr,"__setitem__")
+ return ret(*args)
+
+ def append(self,*args):
+ ret=Caller(self._var_ptr,"append")
+ return ret(*args)
+
+ def __str__(self):
+ return self.local_copy().__str__()
+
+ def __repr__(self):
+ return self.local_copy().__repr__()
+
+ def local_copy(self):
+ return cPickle.loads(self._var_ptr.fetchSerializedContent())
+
+ def __reduce__(self):
+ return (list,(self.local_copy(),))
+
+ pass
+
+class Tuple(object):
+ def __init__(self,varPtr,isTemporaryVar=False):
+ assert(isinstance(varPtr,SALOME._objref_StringDataServer))
+ self._var_ptr=varPtr
+ if not isTemporaryVar:
+ self._var_ptr.Register()
+ self._is_temp=isTemporaryVar
+ pass
+
+ def __del__(self):
+ self._var_ptr.UnRegister()
+ pass
+
+ def assign(self,elt):
+ st=cPickle.dumps(elt,cPickle.HIGHEST_PROTOCOL)
+ self._var_ptr.setSerializedContent(st)
+ pass
+
+ def __getitem__(self,*args):
+ ret=Caller(self._var_ptr,"__getitem__")
+ return ret(*args)
+
+ def __setitem__(self,*args):
+ ret=Caller(self._var_ptr,"__setitem__")
+ return ret(*args)
+
+ def __str__(self):
+ return self.local_copy().__str__()
+
+ def __repr__(self):
+ return self.local_copy().__repr__()
+
+ def local_copy(self):
+ return cPickle.loads(self._var_ptr.fetchSerializedContent())
+
+ def __reduce__(self):
+ return (tuple,(self.local_copy(),))
+
+ pass
+
+
+class Int(object):
+ def __init__(self,varPtr,isTemporaryVar=False):
+ assert(isinstance(varPtr,SALOME._objref_StringDataServer))
+ self._var_ptr=varPtr
+ if not isTemporaryVar:
+ self._var_ptr.Register()
+ self._is_temp=isTemporaryVar
+ pass
+
+ def __del__(self):
+ self._var_ptr.UnRegister()
+ pass
+
+ def __iadd__(self,*args):
+ ret=Caller(self._var_ptr,"__add__")
+ return ret(*args)
+
+ def __isub__(self,*args):
+ ret=Caller(self._var_ptr,"__sub__")
+ return ret(*args)
+
+ def assign(self,elt):
+ st=cPickle.dumps(elt,cPickle.HIGHEST_PROTOCOL)
+ self._var_ptr.setSerializedContent(st)
+ pass
+
+ def __str__(self):
+ return self.local_copy().__str__()
+
+ def __repr__(self):
+ return self.local_copy().__repr__()
+
+ def local_copy(self):
+ return cPickle.loads(self._var_ptr.fetchSerializedContent())
+
+ def __reduce__(self):
+ return (int,(self.local_copy(),))
+
+ pass
+
+class Dict(object):
+ def __init__(self,varPtr,isTemporaryVar=False):
+ assert(isinstance(varPtr,SALOME._objref_StringDataServer))
+ self._var_ptr=varPtr
+ if not isTemporaryVar:
+ self._var_ptr.Register()
+ self._is_temp=isTemporaryVar
+ pass
+
+ def __del__(self):
+ self._var_ptr.UnRegister()
+ pass
+
+ def assign(self,elt):
+ st=cPickle.dumps(elt,cPickle.HIGHEST_PROTOCOL)
+ self._var_ptr.setSerializedContent(st)
+ pass
+
+ def __getitem__(self,*args):
+ ret=Caller(self._var_ptr,"__getitem__")
+ return ret(*args)
+
+ def __setitem__(self,*args):
+ ret=Caller(self._var_ptr,"__setitem__")
+ return ret(*args)
+
+ def __len__(self):
+ return len(self.local_copy())
+
+ def __str__(self):
+ return self.local_copy().__str__()
+
+ def __repr__(self):
+ return self.local_copy().__repr__()
+
+ def local_copy(self):
+ return cPickle.loads(self._var_ptr.fetchSerializedContent())
+
+ def __reduce__(self):
+ return (dict,(self.local_copy(),))
+
+ pass
+
+class Caller:
+ def __init__(self,varPtr,meth):
+ assert(isinstance(varPtr,SALOME._objref_StringDataServer))
+ self._var_ptr=varPtr
+ self._meth=meth
+ pass
+
+ def __call__(self,*args):
+ ret=self._var_ptr.invokePythonMethodOn(self._meth,cPickle.dumps(args,cPickle.HIGHEST_PROTOCOL))
+ return GetHandlerFromRef(ret,True)
+ pass
+
+PyHandlerTypeMap={int:Int,list:List,tuple:Tuple,dict:Dict}
+
+def GetHandlerFromRef(objCorba,isTempVar=False):
+ """ Returns a client that allows to handle a remote corba ref of a global var easily.
+ """
+ v=cPickle.loads(objCorba.fetchSerializedContent())
+ if v is None:
+ return None
+ return PyHandlerTypeMap[v.__class__](objCorba,isTempVar)
+
+def GetHandlerFromName(scopeName,varName):
+ import salome
+ dsm=salome.naming_service.Resolve("/DataServerManager")
+ d2s=dsm.giveADataScopeCalled(scopeName)
+ return GetHandlerFromRef(d2s.retrieveVar("a"),False)
--- /dev/null
+# -*- coding: iso-8859-1 -*-
+# Copyright (C) 2007-2014 CEA/DEN, EDF R&D
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
+# Author : Anthony Geay
+
+import salome
+import SALOME
+import cPickle
+import gc
+import SalomeSDSClt
+
+st="jjj"
+dsm=salome.naming_service.Resolve("/DataServerManager")
+assert(dsm.isAliveAndKicking("Default"))
+assert(isinstance(dsm,SALOME._objref_DataServerManager))
+assert(isinstance(dsm.getDefaultScope(),SALOME._objref_DataScopeServer))
+d2s=dsm.createDataScope("tonyy")
+assert(isinstance(d2s,SALOME._objref_DataScopeServer))
+a=d2s.createGlobalStringVar("int","c")
+assert(a.getVarName()=="c")
+#
+a.setSerializedContent(cPickle.dumps(st,cPickle.HIGHEST_PROTOCOL))
+assert(cPickle.loads(a.fetchSerializedContent())==st)
+assert(cPickle.loads(a.fetchSerializedContent())==st)
+assert(cPickle.loads(d2s.retrieveVar("c").fetchSerializedContent())==st)
+assert(isinstance(d2s.retrieveVar("c"),SALOME._objref_StringDataServer))
+assert(dsm.listScopes()==['Default','tonyy'])
+dsm.createDataScope("S2")
+assert(dsm.retriveDataScope("S2").getScopeName()=="S2")
+assert(dsm.listScopes()==['Default','tonyy','S2'])
+dsm.removeDataScope("Default")
+assert(dsm.listScopes()==['tonyy','S2'])
+dsm.removeDataScope("tonyy")
+assert(dsm.listScopes()==['S2'])
+a=dsm.retriveDataScope("S2").createGlobalStringVar("int","a")
+#
+sname="S7"
+dsm=salome.naming_service.Resolve("/DataServerManager")
+st=cPickle.dumps([],cPickle.HIGHEST_PROTOCOL)
+a=dsm.giveADataScopeCalled(sname).createGlobalStringVar("list","a")
+dsm.giveADataScopeCalled(sname)
+a.setSerializedContent(cPickle.dumps((0,),cPickle.HIGHEST_PROTOCOL))
+assert(cPickle.loads(a.fetchSerializedContent())==(0,))
+a.setSerializedContent(st)
+assert(cPickle.loads(a.fetchSerializedContent())==[])
+tmp=a.invokePythonMethodOn("append",cPickle.dumps((0,),cPickle.HIGHEST_PROTOCOL))
+assert(cPickle.loads(a.fetchSerializedContent())==[0])
+for i in xrange(0,1000):
+ tmp=a.invokePythonMethodOn("append",cPickle.dumps((i,),cPickle.HIGHEST_PROTOCOL))
+ pass
+dsm.removeDataScope(sname)
+#
+sname="S4"
+d2s=dsm.giveADataScopeCalled(sname)
+d2s.createGlobalStringVar("list","a")
+a=SalomeSDSClt.GetHandlerFromRef(dsm.retriveDataScope(sname).retrieveVar("a"))
+a.append(1)
+for i in xrange(1000):
+ a.append(i)
+ pass
+assert(sum(a.local_copy())==499501)
+assert(dsm.isAliveAndKicking(sname))
+#
+dsm=salome.naming_service.Resolve("/DataServerManager")
+sname="S11"
+d2s=dsm.giveADataScopeCalled(sname)
+d2s.createGlobalStringVar("dict","a")
+a=SalomeSDSClt.GetHandlerFromName(sname,"a")
+assert(isinstance(a,SalomeSDSClt.Dict))
+a.assign({})
+a["ab"]=45 ; gc.collect(0)
+a["cd"]=[4,5] ; gc.collect(0)
+a["cd"].append(77) ; gc.collect(0)
+a["cd"].append([4,7,8]) ; gc.collect(0)
+a["cd"][3].append(3) ; gc.collect(0)
+a["ab"]+=2
+assert(a.local_copy()=={'ab':47,'cd':[4,5,77,[4,7,8,3]]})
+a["ab"]-=1
+assert(a.local_copy()=={'ab':46,'cd':[4,5,77,[4,7,8,3]]})
+assert(len(a)==2)
+c=a["cd"] ; gc.collect(0)
+assert(isinstance(c,SalomeSDSClt.List))
+a["cd"]=26 ; gc.collect(0)
+assert(a.local_copy()=={'ab':46,'cd':26})
+a["cd"]=c
+assert(a.local_copy()=={'ab':46,'cd':[4,5,77,[4,7,8,3]]})