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