Salome HOME
Copyrights update 2015.
[modules/kernel.git] / src / SALOMESDS / SalomeSDSClt.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2015  CEA/DEN, EDF R&D, 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 import SALOME
23 import cPickle
24
25 class WrappedType(object):
26     def __init__(self,varPtr,isTemporaryVar=False):
27         assert(isinstance(varPtr,SALOME._objref_PickelizedPyObjServer))
28         self._var_ptr=varPtr
29         if not isTemporaryVar:
30             self._var_ptr.Register()
31         self._is_temp=isTemporaryVar
32         pass
33
34     def local_copy(self):
35         return cPickle.loads(self._var_ptr.fetchSerializedContent())
36
37     def __str__(self):
38         return self.local_copy().__str__()
39
40     def __repr__(self):
41         return self.local_copy().__repr__()
42
43     def __reduce__(self):
44         return (self._wrapped_type,(self.local_copy(),))
45
46     def assign(self,elt):
47         assert(isinstance(self._var_ptr,SALOME._objref_PickelizedPyObjRdWrServer))
48         st=cPickle.dumps(elt,cPickle.HIGHEST_PROTOCOL)
49         self._var_ptr.setSerializedContent(st)
50         pass
51
52     def __del__(self):
53         self._var_ptr.UnRegister()
54         pass
55     pass
56
57 class List(WrappedType):
58     def __init__(self,varPtr,isTemporaryVar=False):
59         WrappedType.__init__(self,varPtr,isTemporaryVar)
60         self._wrapped_type=list
61         pass
62
63     def __getitem__(self,*args):
64         ret=Caller(self._var_ptr,"__getitem__")
65         return ret(*args)
66
67     def __setitem__(self,*args):
68         ret=Caller(self._var_ptr,"__setitem__")
69         return ret(*args)
70     
71     def __delitem__(self,*args):
72         ret=Caller(self._var_ptr,"__delitem__")
73         return ret(*args)
74
75     def append(self,*args):
76         ret=Caller(self._var_ptr,"append")
77         return ret(*args)
78
79     def extend(self,*args):
80         ret=Caller(self._var_ptr,"extend")
81         return ret(*args)
82
83     def insert(self,*args):
84         ret=Caller(self._var_ptr,"insert")
85         return ret(*args)
86
87     def pop(self,*args):
88         ret=Caller(self._var_ptr,"pop")
89         return ret(*args)
90
91     def remove(self,*args):
92         ret=Caller(self._var_ptr,"remove")
93         return ret(*args)
94
95     def reverse(self,*args):
96         ret=Caller(self._var_ptr,"reverse")
97         return ret(*args)
98
99     def sort(self,*args):
100         ret=Caller(self._var_ptr,"sort")
101         return ret(*args)
102     
103     def count(self,*args):
104         return self.local_copy().count(*args)
105
106     def index(self,*args):
107         return self.local_copy().index(*args)
108
109     def __len__(self):
110         return len(self.local_copy())
111     pass
112
113 class Dict(WrappedType):
114     def __init__(self,varPtr,isTemporaryVar=False):
115         WrappedType.__init__(self,varPtr,isTemporaryVar)
116         self._wrapped_type=dict
117         pass
118
119     def __getitem__(self,*args):
120         ret=Caller(self._var_ptr,"__getitem__")
121         return ret(*args)
122
123     def __setitem__(self,*args):
124         ret=Caller(self._var_ptr,"__setitem__")
125         return ret(*args)
126
127     def __delitem__(self,*args):
128         ret=Caller(self._var_ptr,"__delitem__")
129         return ret(*args)
130     
131     def clear(self,*args):
132         ret=Caller(self._var_ptr,"clear")
133         return ret(*args)
134
135     def get(self,*args):
136         ret=Caller(self._var_ptr,"get")
137         return ret(*args)
138
139     def items(self,*args):
140         ret=Caller(self._var_ptr,"items")
141         return ret(*args)
142
143     def pop(self,*args):
144         ret=Caller(self._var_ptr,"pop")
145         return ret(*args)
146
147     def popitem(self,*args):
148         ret=Caller(self._var_ptr,"popitem")
149         return ret(*args)
150
151     def setdefault(self,*args):
152         ret=Caller(self._var_ptr,"setdefault")
153         return ret(*args)
154
155     def update(self,*args):
156         ret=Caller(self._var_ptr,"update")
157         return ret(*args)
158
159     def values(self,*args):
160         ret=Caller(self._var_ptr,"values")
161         return ret(*args)
162  
163     # work on local copy
164
165     def __contains__(self,*args):
166         return self.local_copy().__contains__(*args)
167
168     def has_key(self,*args):
169         return self.local_copy().has_key(*args)
170
171     def keys(self,*args):
172         return self.local_copy().keys(*args)
173
174     def copy(self,*args):
175         return self.local_copy().copy(*args)
176
177     def __len__(self):
178         return len(self.local_copy())
179
180     pass
181
182 class Tuple(WrappedType):
183     def __init__(self,varPtr,isTemporaryVar=False):
184         WrappedType.__init__(self,varPtr,isTemporaryVar)
185         self._wrapped_type=tuple
186         pass
187
188     def __getitem__(self,*args):
189         ret=Caller(self._var_ptr,"__getitem__")
190         return ret(*args)
191     
192     # work on local copy
193
194     def count(self,*args):
195         return self.local_copy().count(*args)
196
197     def index(self,*args):
198         return self.local_copy().index(*args)
199     
200     def __len__(self):
201         return len(self.local_copy())
202
203     pass
204
205 class Float(WrappedType):
206     def __init__(self,varPtr,isTemporaryVar=False):
207         WrappedType.__init__(self,varPtr,isTemporaryVar)
208         self._wrapped_type=float
209         pass
210
211     def __iadd__(self,*args):
212         return self.local_copy().__add__(*args)
213
214     def __isub__(self,*args):
215         return self.local_copy().__sub__(*args)
216
217     def __imul__(self,*args):
218         return self.local_copy().__mul__(*args)
219
220     def __idiv__(self,*args):
221         return self.local_copy().__div__(*args)
222
223     def __add__(self,*args):
224         return self.local_copy().__add__(*args)
225
226     def __sub__(self,*args):
227         return self.local_copy().__sub__(*args)
228
229     def __mul__(self,*args):
230         return self.local_copy().__mul__(*args)
231
232     def __div__(self,*args):
233         return self.local_copy().__div__(*args)
234     
235     def __pow__(self,*args):
236         return self.local_copy().__pow__(*args)
237
238     def as_integer_ratio(self,*args):
239         return self.local_copy().as_integer_ratio(*args)
240
241     def conjugate(self,*args):
242         return self.local_copy().conjugate(*args)
243
244     def fromhex(self,*args):
245         return self.local_copy().fromhex(*args)
246
247     def hex(self,*args):
248         return self.local_copy().hex(*args)
249
250     def imag(self,*args):
251         return self.local_copy().imag(*args)
252
253     def is_integer(self,*args):
254         return self.local_copy().is_integer(*args)
255
256     def real(self,*args):
257         return self.local_copy().real(*args)
258     pass
259
260 class Int(WrappedType):
261     def __init__(self,varPtr,isTemporaryVar=False):
262         WrappedType.__init__(self,varPtr,isTemporaryVar)
263         self._wrapped_type=int
264         pass
265
266     def __iadd__(self,*args):
267         return self.local_copy().__add__(*args)
268
269     def __isub__(self,*args):
270         return self.local_copy().__sub__(*args)
271
272     def __imul__(self,*args):
273         return self.local_copy().__mul__(*args)
274
275     def __imod__(self,*args):
276         return self.local_copy().__mod__(*args)
277     
278     def __idiv__(self,*args):
279         return self.local_copy().__div__(*args)
280
281     def __add__(self,*args):
282         return self.local_copy().__add__(*args)
283
284     def __sub__(self,*args):
285         return self.local_copy().__sub__(*args)
286
287     def __mul__(self,*args):
288         return self.local_copy().__mul__(*args)
289
290     def __mod__(self,*args):
291         return self.local_copy().__mod__(*args)
292
293     def __div__(self,*args):
294         return self.local_copy().__div__(*args)
295     
296     def __pow__(self,*args):
297         return self.local_copy().__pow__(*args)
298
299     def bit_length(self,*args):
300         return self.local_copy().bit_length(*args)
301
302     def conjugate(self,*args):
303         return self.local_copy().conjugate(*args)
304
305     def denominator(self,*args):
306         return self.local_copy().denominator(*args)
307
308     def imag(self,*args):
309         return self.local_copy().imag(*args)
310     
311     def numerator(self,*args):
312         return self.local_copy().numerator(*args)
313
314     def real(self,*args):
315         return self.local_copy().real(*args)
316     pass
317
318 class String(WrappedType):
319     def __init__(self,varPtr,isTemporaryVar=False):
320         WrappedType.__init__(self,varPtr,isTemporaryVar)
321         self._wrapped_type=int
322         pass
323
324     def __add__(self,*args):
325         return self.local_copy().__add__(*args)
326
327     def __iadd__(self,*args):
328         return self.local_copy().__add__(*args)
329
330     def __getitem__(self,*args):
331         return self.local_copy().__getitem__(*args)
332
333     def capitalize(self,*args):
334         return self.local_copy().capitalize(*args)
335
336     def center(self,*args):
337         return self.local_copy().center(*args)
338
339     def count(self,*args):
340         return self.local_copy().count(*args)
341
342     def decode(self,*args):
343         return self.local_copy().decode(*args)
344
345     def encode(self,*args):
346         return self.local_copy().encode(*args)
347
348     def endswith(self,*args):
349         return self.local_copy().endswith(*args)
350
351     def expandtabs(self,*args):
352         return self.local_copy().expandtabs(*args)
353
354     def find(self,*args):
355         return self.local_copy().find(*args)
356
357     def format(self,*args):
358         return self.local_copy().format(*args)
359
360     def index(self,*args):
361         return self.local_copy().index(*args)
362
363     def isalnum(self,*args):
364         return self.local_copy().isalnum(*args)
365
366     def isalpha(self,*args):
367         return self.local_copy().isalpha(*args)
368
369     def isdigit(self,*args):
370         return self.local_copy().isdigit(*args)
371
372     def islower(self,*args):
373         return self.local_copy().islower(*args)
374
375     def isspace(self,*args):
376         return self.local_copy().isspace(*args)
377
378     def istitle(self,*args):
379         return self.local_copy().istitle(*args)
380
381     def isupper(self,*args):
382         return self.local_copy().isupper(*args)
383
384     def join(self,*args):
385         return self.local_copy().join(*args)
386
387     def ljust(self,*args):
388         return self.local_copy().ljust(*args)
389
390     def lower(self,*args):
391         return self.local_copy().lower(*args)
392
393     def lstrip(self,*args):
394         return self.local_copy().lstrip(*args)
395
396     def partition(self,*args):
397         return self.local_copy().partition(*args)
398
399     def replace(self,*args):
400         return self.local_copy().replace(*args)
401
402     def rfind(self,*args):
403         return self.local_copy().rfind(*args)
404
405     def rindex(self,*args):
406         return self.local_copy().rindex(*args)
407
408     def rjust(self,*args):
409         return self.local_copy().rjust(*args)
410
411     def rpartition(self,*args):
412         return self.local_copy().rpartition(*args)
413
414     def rsplit(self,*args):
415         return self.local_copy().rsplit(*args)
416
417     def rstrip(self,*args):
418         return self.local_copy().rstrip(*args)
419
420     def split(self,*args):
421         return self.local_copy().split(*args)
422
423     def splitlines(self,*args):
424         return self.local_copy().splitlines(*args)
425
426     def startswith(self,*args):
427         return self.local_copy().startswith(*args)
428
429     def strip(self,*args):
430         return self.local_copy().strip(*args)
431
432     def swapcase(self,*args):
433         return self.local_copy().swapcase(*args)
434
435     def title(self,*args):
436         return self.local_copy().title(*args)
437
438     def translate(self,*args):
439         return self.local_copy().translate(*args)
440
441     def upper(self,*args):
442         return self.local_copy().upper(*args)
443
444     def zfill(self,*args):
445         return self.local_copy().zfill(*args)
446
447     def __len__(self):
448         return len(self.local_copy())
449     pass
450
451 class Caller:
452     def __init__(self,varPtr,meth):
453         assert(isinstance(varPtr,SALOME._objref_PickelizedPyObjServer))
454         self._var_ptr=varPtr
455         self._meth=meth
456         pass
457
458     def __call__(self,*args):
459         ret=self._var_ptr.invokePythonMethodOn(self._meth,cPickle.dumps(args,cPickle.HIGHEST_PROTOCOL))
460         return GetHandlerFromRef(ret,True)
461     pass
462
463 PyHandlerTypeMap={int:Int,float:Float,str:String,list:List,tuple:Tuple,dict:Dict}
464
465 def GetHandlerFromRef(objCorba,isTempVar=False):
466     """ Returns a client that allows to handle a remote corba ref of a global var easily.
467     """
468     assert(isinstance(objCorba,SALOME._objref_PickelizedPyObjServer))
469     v=cPickle.loads(objCorba.fetchSerializedContent())
470     if v is None:
471         objCorba.UnRegister()
472         return None
473     return PyHandlerTypeMap[v.__class__](objCorba,isTempVar)
474     
475     
476 def CreateRdOnlyGlobalVar(value,varName,scopeName):
477     import salome
478     dsm=salome.naming_service.Resolve("/DataServerManager")
479     d2s,isCreated=dsm.giveADataScopeCalled(scopeName)
480     return GetHandlerFromRef(d2s.createRdOnlyVar(varName,cPickle.dumps(value,cPickle.HIGHEST_PROTOCOL)),False)
481     
482 def CreateRdExtGlobalVar(value,varName,scopeName):
483     import salome
484     dsm=salome.naming_service.Resolve("/DataServerManager")
485     d2s,isCreated=dsm.giveADataScopeCalled(scopeName)
486     return GetHandlerFromRef(d2s.createRdExtVar(varName,cPickle.dumps(value,cPickle.HIGHEST_PROTOCOL)),False)
487
488 def GetHandlerFromName(scopeName,varName):
489     import salome
490     dsm=salome.naming_service.Resolve("/DataServerManager")
491     d2s=dsm.retriveDataScope(scopeName)
492     return GetHandlerFromRef(d2s.retrieveVar(varName),False)