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