]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
Little cleanup for global vars.
authorAnthony Geay <anthony.geay@edf.fr>
Tue, 30 Jun 2015 15:19:33 +0000 (17:19 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Tue, 30 Jun 2015 15:19:33 +0000 (17:19 +0200)
src/SALOMESDS/SalomeSDSClt.py
src/SALOMESDS/TestSalomeSDS2.py

index 9e0969de2dd49c4f2e5e213c7c4219445c1722d8..48eac9d8c669832ff0269f2c106f6adb901cbe86 100644 (file)
 import SALOME
 import cPickle
 
+class InvokatorStyle(object):
+    def __init__(self,varPtr):
+        self._var_ptr=varPtr
+    def ptr(self):
+        return self._var_ptr
+    pass
+
+class InvokatorPossibleStyle(InvokatorStyle):
+    def __init__(self,varPtr):
+        InvokatorStyle.__init__(self,varPtr)
+        assert(self.__class__.IsOK(varPtr))
+
+    def invokePythonMethodOn(self,meth,argsInStrFrmt):
+        return self._var_ptr.invokePythonMethodOn(meth,argsInStrFrmt)
+        
+    @classmethod
+    def IsOK(cls,varPtr):
+        return isinstance(varPtr,SALOME._objref_PickelizedPyObjRdExtServer)
+    pass
+
+class InvokatorImpossibleStyle(InvokatorStyle):
+    def __init__(self,varPtr):
+        InvokatorStyle.__init__(self,varPtr)
+        assert(self.__class__.IsOK(varPtr))
+        
+    def invokePythonMethodOn(self,meth,argsInStrFrmt):
+        raise Exception("Cannot invoke because it is readonly var !")
+
+    @classmethod
+    def IsOK(cls,varPtr):
+        return isinstance(varPtr,SALOME._objref_PickelizedPyObjRdOnlyServer)
+    pass
+
+def InvokatorStyleFactory(varPtr):
+    if InvokatorImpossibleStyle.IsOK(varPtr):
+        return InvokatorImpossibleStyle(varPtr)
+    if InvokatorPossibleStyle.IsOK(varPtr):
+        return InvokatorPossibleStyle(varPtr)
+    raise Exception("InvokatorStyleFactory : unmanaged type of var (%s)!"%(type(varPtr)))
+    pass
+
 class WrappedType(object):
     def __init__(self,varPtr,isTemporaryVar=False):
         assert(isinstance(varPtr,SALOME._objref_PickelizedPyObjServer))
-        self._var_ptr=varPtr
+        self._var_ptr=InvokatorStyleFactory(varPtr)
         if not isTemporaryVar:
-            self._var_ptr.Register()
+            self._var_ptr.ptr().Register()
         self._is_temp=isTemporaryVar
         pass
 
+    def ptr(self):
+        return self._var_ptr.ptr()
+
     def local_copy(self):
-        return cPickle.loads(self._var_ptr.fetchSerializedContent())
+        return cPickle.loads(self._var_ptr.ptr().fetchSerializedContent())
 
     def __str__(self):
         return self.local_copy().__str__()
@@ -50,7 +94,7 @@ class WrappedType(object):
         pass
 
     def __del__(self):
-        self._var_ptr.UnRegister()
+        self._var_ptr.ptr().UnRegister()
         pass
     pass
 
@@ -450,7 +494,6 @@ class String(WrappedType):
 
 class Caller:
     def __init__(self,varPtr,meth):
-        assert(isinstance(varPtr,SALOME._objref_PickelizedPyObjServer))
         self._var_ptr=varPtr
         self._meth=meth
         pass
@@ -475,18 +518,21 @@ def GetHandlerFromRef(objCorba,isTempVar=False):
     
 def CreateRdOnlyGlobalVar(value,varName,scopeName):
     import salome
+    salome.salome_init()
     dsm=salome.naming_service.Resolve("/DataServerManager")
     d2s,isCreated=dsm.giveADataScopeCalled(scopeName)
     return GetHandlerFromRef(d2s.createRdOnlyVar(varName,cPickle.dumps(value,cPickle.HIGHEST_PROTOCOL)),False)
     
 def CreateRdExtGlobalVar(value,varName,scopeName):
     import salome
+    salome.salome_init()
     dsm=salome.naming_service.Resolve("/DataServerManager")
     d2s,isCreated=dsm.giveADataScopeCalled(scopeName)
     return GetHandlerFromRef(d2s.createRdExtVar(varName,cPickle.dumps(value,cPickle.HIGHEST_PROTOCOL)),False)
 
-def GetHandlerFromName(scopeName,varName):
+def GetHandlerFromName(varName,scopeName):
     import salome
+    salome.salome_init()
     dsm=salome.naming_service.Resolve("/DataServerManager")
     d2s=dsm.retriveDataScope(scopeName)
     return GetHandlerFromRef(d2s.retrieveVar(varName),False)
index 67466774a80ea45b9548e24a3bd68cc5a5891dda..eb1c1ce59d017f08befc77520486dbf03d5dfec7 100644 (file)
@@ -43,7 +43,7 @@ class SalomeSDS2Test(unittest.TestCase):
     self.assertEqual(a.local_copy(),[5,["rt",8],5,["rt",8],["rt",8]])
     a[4].append(7)
     self.assertEqual(a.local_copy(),[5,["rt",8],5,["rt",8],["rt",8,7]])
-    a._var_ptr.getMyDataScopeServer().deleteVar("a")
+    a.ptr().getMyDataScopeServer().deleteVar("a")
     pass
   
   def testDict1(self):
@@ -64,14 +64,14 @@ class SalomeSDS2Test(unittest.TestCase):
     self.assertEqual(a.local_copy(),{"ab":4,"cd":[5,77],"ef":["a","bb","ccc"],"gh":{"ab":4,"cd":[5,77],"ef":["a","bb","ccc"]}})
     a["gh"]["cd"].append(99) ; a["cd"].append(88)
     self.assertEqual(a.local_copy(),{"ab":4,"cd":[5,77,88],"ef":["a","bb","ccc"],"gh":{"ab":4,"cd":[5,77,99],"ef":["a","bb","ccc"]}})
-    a._var_ptr.getMyDataScopeServer().deleteVar("a")
+    a.ptr().getMyDataScopeServer().deleteVar("a")
     pass
 
   def testReadOnly1(self):
     a=SalomeSDSClt.CreateRdOnlyGlobalVar({"ab":4,"cd":[5,77]},"a","Scope0")
     self.assertEqual(a.local_copy(),{"ab":4,"cd":[5,77]})
-    self.assertRaises(AttributeError,a.__getitem__,"ab")
-    a._var_ptr.getMyDataScopeServer().deleteVar("a")
+    self.assertRaises(Exception,a.__getitem__,"ab")
+    a.ptr().getMyDataScopeServer().deleteVar("a")
 
   def setUp(self):
     salome.salome_init()