1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 # Author : Anthony Geay
24 import SALOMEWrappedStdType
26 class InvokatorStyle(object):
27 def __init__(self,varPtr):
33 class InvokatorPossibleStyle(InvokatorStyle):
34 def __init__(self,varPtr):
35 InvokatorStyle.__init__(self,varPtr)
36 assert(self.__class__.IsOK(varPtr))
38 def invokePythonMethodOn(self,meth,argsInStrFrmt):
39 return self._var_ptr.invokePythonMethodOn(meth,argsInStrFrmt)
43 return isinstance(varPtr,SALOME._objref_PickelizedPyObjRdExtServer) or isinstance(varPtr,SALOME._objref_PickelizedPyObjRdWrServer)
46 class InvokatorImpossibleStyle(InvokatorStyle):
47 def __init__(self,varPtr):
48 InvokatorStyle.__init__(self,varPtr)
49 assert(self.__class__.IsOK(varPtr))
51 def invokePythonMethodOn(self,meth,argsInStrFrmt):
52 raise Exception("Cannot invoke because it is readonly var !")
56 return isinstance(varPtr,SALOME._objref_PickelizedPyObjRdOnlyServer)
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)))
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
77 return self._var_ptr.ptr()
80 return pickle.loads(self._var_ptr.ptr().fetchSerializedContent())
83 return self.local_copy().__str__()
86 return self.local_copy().__repr__()
89 return (self._wrapped_type,(self.local_copy(),))
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)
99 self._var_ptr.ptr().UnRegister()
103 class List(WrappedType,SALOMEWrappedStdType.List):
104 def __init__(self,varPtr,isTemporaryVar=False):
105 WrappedType.__init__(self,varPtr,isTemporaryVar)
106 self._wrapped_type=list
109 def __getitem__(self,*args):
110 ret=Caller(self._var_ptr,"__getitem__")
113 def __setitem__(self,*args):
114 ret=Caller(self._var_ptr,"__setitem__")
117 def __delitem__(self,*args):
118 ret=Caller(self._var_ptr,"__delitem__")
121 def append(self,*args):
122 ret=Caller(self._var_ptr,"append")
125 def extend(self,*args):
126 ret=Caller(self._var_ptr,"extend")
129 def insert(self,*args):
130 ret=Caller(self._var_ptr,"insert")
134 ret=Caller(self._var_ptr,"pop")
137 def remove(self,*args):
138 ret=Caller(self._var_ptr,"remove")
141 def reverse(self,*args):
142 ret=Caller(self._var_ptr,"reverse")
145 def sort(self,*args):
146 ret=Caller(self._var_ptr,"sort")
149 def count(self,*args):
150 return self.local_copy().count(*args)
152 def index(self,*args):
153 return self.local_copy().index(*args)
156 return len(self.local_copy())
159 class Dict(WrappedType,SALOMEWrappedStdType.Dict):
160 def __init__(self,varPtr,isTemporaryVar=False):
161 WrappedType.__init__(self,varPtr,isTemporaryVar)
162 self._wrapped_type=dict
165 def __getitem__(self,*args):
166 ret=Caller(self._var_ptr,"__getitem__")
169 def __setitem__(self,*args):
170 ret=Caller(self._var_ptr,"__setitem__")
173 def __delitem__(self,*args):
174 ret=Caller(self._var_ptr,"__delitem__")
177 def clear(self,*args):
178 ret=Caller(self._var_ptr,"clear")
182 ret=Caller(self._var_ptr,"get")
185 def items(self,*args):
186 ret=Caller(self._var_ptr,"items")
190 ret=Caller(self._var_ptr,"pop")
193 def popitem(self,*args):
194 ret=Caller(self._var_ptr,"popitem")
197 def setdefault(self,*args):
198 ret=Caller(self._var_ptr,"setdefault")
201 def update(self,*args):
202 ret=Caller(self._var_ptr,"update")
205 def values(self,*args):
206 ret=Caller(self._var_ptr,"values")
211 def __contains__(self,*args):
212 return self.local_copy().__contains__(*args)
214 def has_key(self,*args):
215 return self.local_copy().has_key(*args)
217 def keys(self,*args):
218 return self.local_copy().keys(*args)
220 def copy(self,*args):
221 return self.local_copy().copy(*args)
224 return len(self.local_copy())
228 class Tuple(WrappedType,SALOMEWrappedStdType.Tuple):
229 def __init__(self,varPtr,isTemporaryVar=False):
230 WrappedType.__init__(self,varPtr,isTemporaryVar)
231 self._wrapped_type=tuple
234 def __getitem__(self,*args):
235 ret=Caller(self._var_ptr,"__getitem__")
240 def count(self,*args):
241 return self.local_copy().count(*args)
243 def index(self,*args):
244 return self.local_copy().index(*args)
247 return len(self.local_copy())
251 class Float(WrappedType,SALOMEWrappedStdType.Float):
252 def __init__(self,varPtr,isTemporaryVar=False):
253 WrappedType.__init__(self,varPtr,isTemporaryVar)
254 self._wrapped_type=float
257 def __iadd__(self,*args):
258 return self.local_copy().__add__(*args)
260 def __isub__(self,*args):
261 return self.local_copy().__sub__(*args)
263 def __imul__(self,*args):
264 return self.local_copy().__mul__(*args)
266 def __itruediv__(self,*args):
267 return self.local_copy().__truediv__(*args)
269 def __ifloordiv__(self,*args):
270 return self.local_copy().__floordiv__(*args)
272 def __add__(self,*args):
273 return self.local_copy().__add__(*args)
275 def __sub__(self,*args):
276 return self.local_copy().__sub__(*args)
278 def __mul__(self,*args):
279 return self.local_copy().__mul__(*args)
281 def __floordiv__(self,*args):
282 return self.local_copy().__floordiv__(*args)
284 def __truediv__(self,*args):
285 return self.local_copy().__truediv__(*args)
287 def __pow__(self,*args):
288 return self.local_copy().__pow__(*args)
290 def as_integer_ratio(self,*args):
291 return self.local_copy().as_integer_ratio(*args)
293 def conjugate(self,*args):
294 return self.local_copy().conjugate(*args)
296 def fromhex(self,*args):
297 return self.local_copy().fromhex(*args)
300 return self.local_copy().hex(*args)
302 def imag(self,*args):
303 return self.local_copy().imag(*args)
305 def is_integer(self,*args):
306 return self.local_copy().is_integer(*args)
308 def real(self,*args):
309 return self.local_copy().real(*args)
312 class Int(WrappedType,SALOMEWrappedStdType.Int):
313 def __init__(self,varPtr,isTemporaryVar=False):
314 WrappedType.__init__(self,varPtr,isTemporaryVar)
315 self._wrapped_type=int
318 def __iadd__(self,*args):
319 return self.local_copy().__add__(*args)
321 def __isub__(self,*args):
322 return self.local_copy().__sub__(*args)
324 def __imul__(self,*args):
325 return self.local_copy().__mul__(*args)
327 def __imod__(self,*args):
328 return self.local_copy().__mod__(*args)
330 def __itruediv__(self,*args):
331 return self.local_copy().__truediv__(*args)
333 def __ifloordiv__(self,*args):
334 return self.local_copy().__floordiv__(*args)
336 def __add__(self,*args):
337 return self.local_copy().__add__(*args)
339 def __sub__(self,*args):
340 return self.local_copy().__sub__(*args)
342 def __mul__(self,*args):
343 return self.local_copy().__mul__(*args)
345 def __mod__(self,*args):
346 return self.local_copy().__mod__(*args)
348 def __truediv__(self,*args):
349 return self.local_copy().__truediv__(*args)
351 def __floordiv__(self,*args):
352 return self.local_copy().__floordiv__(*args)
354 def __pow__(self,*args):
355 return self.local_copy().__pow__(*args)
357 def bit_length(self,*args):
358 return self.local_copy().bit_length(*args)
360 def conjugate(self,*args):
361 return self.local_copy().conjugate(*args)
363 def denominator(self,*args):
364 return self.local_copy().denominator(*args)
366 def imag(self,*args):
367 return self.local_copy().imag(*args)
369 def numerator(self,*args):
370 return self.local_copy().numerator(*args)
372 def real(self,*args):
373 return self.local_copy().real(*args)
376 class String(WrappedType,SALOMEWrappedStdType.String):
377 def __init__(self,varPtr,isTemporaryVar=False):
378 WrappedType.__init__(self,varPtr,isTemporaryVar)
379 self._wrapped_type=int
382 def __add__(self,*args):
383 return self.local_copy().__add__(*args)
385 def __iadd__(self,*args):
386 return self.local_copy().__add__(*args)
388 def __getitem__(self,*args):
389 return self.local_copy().__getitem__(*args)
391 def capitalize(self,*args):
392 return self.local_copy().capitalize(*args)
394 def center(self,*args):
395 return self.local_copy().center(*args)
397 def count(self,*args):
398 return self.local_copy().count(*args)
400 def decode(self,*args):
401 return self.local_copy().decode(*args)
403 def encode(self,*args):
404 return self.local_copy().encode(*args)
406 def endswith(self,*args):
407 return self.local_copy().endswith(*args)
409 def expandtabs(self,*args):
410 return self.local_copy().expandtabs(*args)
412 def find(self,*args):
413 return self.local_copy().find(*args)
415 def format(self,*args):
416 return self.local_copy().format(*args)
418 def index(self,*args):
419 return self.local_copy().index(*args)
421 def isalnum(self,*args):
422 return self.local_copy().isalnum(*args)
424 def isalpha(self,*args):
425 return self.local_copy().isalpha(*args)
427 def isdigit(self,*args):
428 return self.local_copy().isdigit(*args)
430 def islower(self,*args):
431 return self.local_copy().islower(*args)
433 def isspace(self,*args):
434 return self.local_copy().isspace(*args)
436 def istitle(self,*args):
437 return self.local_copy().istitle(*args)
439 def isupper(self,*args):
440 return self.local_copy().isupper(*args)
442 def join(self,*args):
443 return self.local_copy().join(*args)
445 def ljust(self,*args):
446 return self.local_copy().ljust(*args)
448 def lower(self,*args):
449 return self.local_copy().lower(*args)
451 def lstrip(self,*args):
452 return self.local_copy().lstrip(*args)
454 def partition(self,*args):
455 return self.local_copy().partition(*args)
457 def replace(self,*args):
458 return self.local_copy().replace(*args)
460 def rfind(self,*args):
461 return self.local_copy().rfind(*args)
463 def rindex(self,*args):
464 return self.local_copy().rindex(*args)
466 def rjust(self,*args):
467 return self.local_copy().rjust(*args)
469 def rpartition(self,*args):
470 return self.local_copy().rpartition(*args)
472 def rsplit(self,*args):
473 return self.local_copy().rsplit(*args)
475 def rstrip(self,*args):
476 return self.local_copy().rstrip(*args)
478 def split(self,*args):
479 return self.local_copy().split(*args)
481 def splitlines(self,*args):
482 return self.local_copy().splitlines(*args)
484 def startswith(self,*args):
485 return self.local_copy().startswith(*args)
487 def strip(self,*args):
488 return self.local_copy().strip(*args)
490 def swapcase(self,*args):
491 return self.local_copy().swapcase(*args)
493 def title(self,*args):
494 return self.local_copy().title(*args)
496 def translate(self,*args):
497 return self.local_copy().translate(*args)
499 def upper(self,*args):
500 return self.local_copy().upper(*args)
502 def zfill(self,*args):
503 return self.local_copy().zfill(*args)
506 return len(self.local_copy())
510 def __init__(self,varPtr,meth):
515 def __call__(self,*args):
516 ret=self._var_ptr.invokePythonMethodOn(self._meth,pickle.dumps(args,pickle.HIGHEST_PROTOCOL))
517 return GetHandlerFromRef(ret,True)
520 PyHandlerTypeMap={int:Int,float:Float,str:String,list:List,tuple:Tuple,dict:Dict}
522 def GetHandlerFromRef(objCorba,isTempVar=False):
523 """ Returns a client that allows to handle a remote corba ref of a global var easily.
525 assert(isinstance(objCorba,SALOME._objref_PickelizedPyObjServer))
526 v=pickle.loads(objCorba.fetchSerializedContent())
528 objCorba.UnRegister()
530 return PyHandlerTypeMap[v.__class__](objCorba,isTempVar)
533 def CreateRdOnlyGlobalVar(value,varName,scopeName):
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)
540 def CreateRdExtGlobalVar(value,varName,scopeName):
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)
547 def GetHandlerFromName(varName,scopeName):
550 dsm=salome.naming_service.Resolve("/DataServerManager")
551 d2s=dsm.retriveDataScope(scopeName)
552 return GetHandlerFromRef(d2s.retrieveVar(varName),False)