Salome HOME
updated copyright message
[modules/kernel.git] / src / SALOMESDS / SALOMEGlobalVarHelper.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2023  CEA, EDF, 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 # Author : Anthony Geay
21
22 # dict,list,tuple,int,float,str
23 import SALOME
24 import pickle
25
26 class List:
27     def __init__(self,varPtr,isTemporaryVar=False):
28         assert(isinstance(varPtr,SALOME._objref_StringDataServer))
29         self._var_ptr=varPtr
30         if not isTemporaryVar:
31             self._var_ptr.Register()
32         self._is_temp=isTemporaryVar
33         pass
34
35     def __del__(self):
36         self._var_ptr.UnRegister()
37         pass
38
39     def assign(self,elt):
40         st=pickle.dumps(elt,pickle.HIGHEST_PROTOCOL)
41         self._var_ptr.setSerializedContent(st)
42         pass
43
44     def __getitem__(self,*args):
45         ret=Caller(self._var_ptr,"__getitem__")
46         return ret(*args)
47
48     def __setitem__(self,*args):
49         ret=Caller(self._var_ptr,"__setitem__")
50         return ret(*args)
51
52     def append(self,*args):
53         ret=Caller(self._var_ptr,"append")
54         return ret(*args)
55
56     def __str__(self):
57         return self.local_copy().__str__()
58
59     def __repr__(self):
60         return self.local_copy().__repr__()
61
62     def local_copy(self):
63         return pickle.loads(self._var_ptr.fetchSerializedContent())
64
65     def __reduce__(self):
66         return (list,(self.local_copy(),))
67
68     pass
69
70 class Tuple:
71     def __init__(self,varPtr,isTemporaryVar=False):
72         assert(isinstance(varPtr,SALOME._objref_StringDataServer))
73         self._var_ptr=varPtr
74         if not isTemporaryVar:
75             self._var_ptr.Register()
76         self._is_temp=isTemporaryVar
77         pass
78
79     def __del__(self):
80         self._var_ptr.UnRegister()
81         pass
82
83     def assign(self,elt):
84         st=pickle.dumps(elt,pickle.HIGHEST_PROTOCOL)
85         self._var_ptr.setSerializedContent(st)
86         pass
87
88     def __getitem__(self,*args):
89         ret=Caller(self._var_ptr,"__getitem__")
90         return ret(*args)
91
92     def __setitem__(self,*args):
93         ret=Caller(self._var_ptr,"__setitem__")
94         return ret(*args)
95
96     def __str__(self):
97         return self.local_copy().__str__()
98
99     def __repr__(self):
100         return self.local_copy().__repr__()
101
102     def local_copy(self):
103         return pickle.loads(self._var_ptr.fetchSerializedContent())
104
105     def __reduce__(self):
106         return (tuple,(self.local_copy(),))
107
108     pass
109
110
111 class Int:
112     def __init__(self,varPtr,isTemporaryVar=False):
113         assert(isinstance(varPtr,SALOME._objref_StringDataServer))
114         self._var_ptr=varPtr
115         if not isTemporaryVar:
116             self._var_ptr.Register()
117         self._is_temp=isTemporaryVar
118         pass
119
120     def __del__(self):
121         self._var_ptr.UnRegister()
122         pass
123
124     def __iadd__(self,*args):
125         ret=Caller(self._var_ptr,"__add__")
126         return ret(*args)
127
128     def __isub__(self,*args):
129         ret=Caller(self._var_ptr,"__sub__")
130         return ret(*args)
131
132     def assign(self,elt):
133         st=pickle.dumps(elt,pickle.HIGHEST_PROTOCOL)
134         self._var_ptr.setSerializedContent(st)
135         pass
136
137     def __str__(self):
138         return self.local_copy().__str__()
139
140     def __repr__(self):
141         return self.local_copy().__repr__()
142
143     def local_copy(self):
144         return pickle.loads(self._var_ptr.fetchSerializedContent())
145
146     def __reduce__(self):
147         return (int,(self.local_copy(),))
148
149     pass
150
151 class Dict:
152     def __init__(self,varPtr,isTemporaryVar=False):
153         assert(isinstance(varPtr,SALOME._objref_StringDataServer))
154         self._var_ptr=varPtr
155         if not isTemporaryVar:
156             self._var_ptr.Register()
157         self._is_temp=isTemporaryVar
158         pass
159
160     def __del__(self):
161         self._var_ptr.UnRegister()
162         pass
163
164     def assign(self,elt):
165         st=pickle.dumps(elt,pickle.HIGHEST_PROTOCOL)
166         self._var_ptr.setSerializedContent(st)
167         pass
168
169     def __getitem__(self,*args):
170         ret=Caller(self._var_ptr,"__getitem__")
171         return ret(*args)
172
173     def __setitem__(self,*args):
174         ret=Caller(self._var_ptr,"__setitem__")
175         return ret(*args)
176
177     def __len__(self):
178         return len(self.local_copy())
179
180     def __str__(self):
181         return self.local_copy().__str__()
182
183     def __repr__(self):
184         return self.local_copy().__repr__()
185
186     def local_copy(self):
187         return pickle.loads(self._var_ptr.fetchSerializedContent())
188
189     def __reduce__(self):
190         return (dict,(self.local_copy(),))
191
192     pass
193
194 class Caller:
195     def __init__(self,varPtr,meth):
196         assert(isinstance(varPtr,SALOME._objref_StringDataServer))
197         self._var_ptr=varPtr
198         self._meth=meth
199         pass
200
201     def __call__(self,*args):
202         ret=self._var_ptr.invokePythonMethodOn(self._meth,pickle.dumps(args,pickle.HIGHEST_PROTOCOL))
203         return GetHandlerFromRef(ret,True)
204     pass
205
206 PyHandlerTypeMap={int:Int,list:List,tuple:Tuple,dict:Dict}
207
208 def GetHandlerFromRef(objCorba,isTempVar=False):
209     v=pickle.loads(objCorba.fetchSerializedContent())
210     if v is None:
211         return None
212     return PyHandlerTypeMap[v.__class__](objCorba,isTempVar)