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