1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2015 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 cPickle.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 assert(isinstance(self._var_ptr,SALOME._objref_PickelizedPyObjRdWrServer))
93 st=cPickle.dumps(elt,cPickle.HIGHEST_PROTOCOL)
94 self._var_ptr.setSerializedContent(st)
98 self._var_ptr.ptr().UnRegister()
102 class List(WrappedType,SALOMEWrappedStdType.List):
103 def __init__(self,varPtr,isTemporaryVar=False):
104 WrappedType.__init__(self,varPtr,isTemporaryVar)
105 self._wrapped_type=list
108 def __getitem__(self,*args):
109 ret=Caller(self._var_ptr,"__getitem__")
112 def __setitem__(self,*args):
113 ret=Caller(self._var_ptr,"__setitem__")
116 def __delitem__(self,*args):
117 ret=Caller(self._var_ptr,"__delitem__")
120 def append(self,*args):
121 ret=Caller(self._var_ptr,"append")
124 def extend(self,*args):
125 ret=Caller(self._var_ptr,"extend")
128 def insert(self,*args):
129 ret=Caller(self._var_ptr,"insert")
133 ret=Caller(self._var_ptr,"pop")
136 def remove(self,*args):
137 ret=Caller(self._var_ptr,"remove")
140 def reverse(self,*args):
141 ret=Caller(self._var_ptr,"reverse")
144 def sort(self,*args):
145 ret=Caller(self._var_ptr,"sort")
148 def count(self,*args):
149 return self.local_copy().count(*args)
151 def index(self,*args):
152 return self.local_copy().index(*args)
155 return len(self.local_copy())
158 class Dict(WrappedType,SALOMEWrappedStdType.Dict):
159 def __init__(self,varPtr,isTemporaryVar=False):
160 WrappedType.__init__(self,varPtr,isTemporaryVar)
161 self._wrapped_type=dict
164 def __getitem__(self,*args):
165 ret=Caller(self._var_ptr,"__getitem__")
168 def __setitem__(self,*args):
169 ret=Caller(self._var_ptr,"__setitem__")
172 def __delitem__(self,*args):
173 ret=Caller(self._var_ptr,"__delitem__")
176 def clear(self,*args):
177 ret=Caller(self._var_ptr,"clear")
181 ret=Caller(self._var_ptr,"get")
184 def items(self,*args):
185 ret=Caller(self._var_ptr,"items")
189 ret=Caller(self._var_ptr,"pop")
192 def popitem(self,*args):
193 ret=Caller(self._var_ptr,"popitem")
196 def setdefault(self,*args):
197 ret=Caller(self._var_ptr,"setdefault")
200 def update(self,*args):
201 ret=Caller(self._var_ptr,"update")
204 def values(self,*args):
205 ret=Caller(self._var_ptr,"values")
210 def __contains__(self,*args):
211 return self.local_copy().__contains__(*args)
213 def has_key(self,*args):
214 return self.local_copy().has_key(*args)
216 def keys(self,*args):
217 return self.local_copy().keys(*args)
219 def copy(self,*args):
220 return self.local_copy().copy(*args)
223 return len(self.local_copy())
227 class Tuple(WrappedType,SALOMEWrappedStdType.Tuple):
228 def __init__(self,varPtr,isTemporaryVar=False):
229 WrappedType.__init__(self,varPtr,isTemporaryVar)
230 self._wrapped_type=tuple
233 def __getitem__(self,*args):
234 ret=Caller(self._var_ptr,"__getitem__")
239 def count(self,*args):
240 return self.local_copy().count(*args)
242 def index(self,*args):
243 return self.local_copy().index(*args)
246 return len(self.local_copy())
250 class Float(WrappedType,SALOMEWrappedStdType.Float):
251 def __init__(self,varPtr,isTemporaryVar=False):
252 WrappedType.__init__(self,varPtr,isTemporaryVar)
253 self._wrapped_type=float
256 def __iadd__(self,*args):
257 return self.local_copy().__add__(*args)
259 def __isub__(self,*args):
260 return self.local_copy().__sub__(*args)
262 def __imul__(self,*args):
263 return self.local_copy().__mul__(*args)
265 def __idiv__(self,*args):
266 return self.local_copy().__div__(*args)
268 def __add__(self,*args):
269 return self.local_copy().__add__(*args)
271 def __sub__(self,*args):
272 return self.local_copy().__sub__(*args)
274 def __mul__(self,*args):
275 return self.local_copy().__mul__(*args)
277 def __div__(self,*args):
278 return self.local_copy().__div__(*args)
280 def __pow__(self,*args):
281 return self.local_copy().__pow__(*args)
283 def as_integer_ratio(self,*args):
284 return self.local_copy().as_integer_ratio(*args)
286 def conjugate(self,*args):
287 return self.local_copy().conjugate(*args)
289 def fromhex(self,*args):
290 return self.local_copy().fromhex(*args)
293 return self.local_copy().hex(*args)
295 def imag(self,*args):
296 return self.local_copy().imag(*args)
298 def is_integer(self,*args):
299 return self.local_copy().is_integer(*args)
301 def real(self,*args):
302 return self.local_copy().real(*args)
305 class Int(WrappedType,SALOMEWrappedStdType.Int):
306 def __init__(self,varPtr,isTemporaryVar=False):
307 WrappedType.__init__(self,varPtr,isTemporaryVar)
308 self._wrapped_type=int
311 def __iadd__(self,*args):
312 return self.local_copy().__add__(*args)
314 def __isub__(self,*args):
315 return self.local_copy().__sub__(*args)
317 def __imul__(self,*args):
318 return self.local_copy().__mul__(*args)
320 def __imod__(self,*args):
321 return self.local_copy().__mod__(*args)
323 def __idiv__(self,*args):
324 return self.local_copy().__div__(*args)
326 def __add__(self,*args):
327 return self.local_copy().__add__(*args)
329 def __sub__(self,*args):
330 return self.local_copy().__sub__(*args)
332 def __mul__(self,*args):
333 return self.local_copy().__mul__(*args)
335 def __mod__(self,*args):
336 return self.local_copy().__mod__(*args)
338 def __div__(self,*args):
339 return self.local_copy().__div__(*args)
341 def __pow__(self,*args):
342 return self.local_copy().__pow__(*args)
344 def bit_length(self,*args):
345 return self.local_copy().bit_length(*args)
347 def conjugate(self,*args):
348 return self.local_copy().conjugate(*args)
350 def denominator(self,*args):
351 return self.local_copy().denominator(*args)
353 def imag(self,*args):
354 return self.local_copy().imag(*args)
356 def numerator(self,*args):
357 return self.local_copy().numerator(*args)
359 def real(self,*args):
360 return self.local_copy().real(*args)
363 class String(WrappedType,SALOMEWrappedStdType.String):
364 def __init__(self,varPtr,isTemporaryVar=False):
365 WrappedType.__init__(self,varPtr,isTemporaryVar)
366 self._wrapped_type=int
369 def __add__(self,*args):
370 return self.local_copy().__add__(*args)
372 def __iadd__(self,*args):
373 return self.local_copy().__add__(*args)
375 def __getitem__(self,*args):
376 return self.local_copy().__getitem__(*args)
378 def capitalize(self,*args):
379 return self.local_copy().capitalize(*args)
381 def center(self,*args):
382 return self.local_copy().center(*args)
384 def count(self,*args):
385 return self.local_copy().count(*args)
387 def decode(self,*args):
388 return self.local_copy().decode(*args)
390 def encode(self,*args):
391 return self.local_copy().encode(*args)
393 def endswith(self,*args):
394 return self.local_copy().endswith(*args)
396 def expandtabs(self,*args):
397 return self.local_copy().expandtabs(*args)
399 def find(self,*args):
400 return self.local_copy().find(*args)
402 def format(self,*args):
403 return self.local_copy().format(*args)
405 def index(self,*args):
406 return self.local_copy().index(*args)
408 def isalnum(self,*args):
409 return self.local_copy().isalnum(*args)
411 def isalpha(self,*args):
412 return self.local_copy().isalpha(*args)
414 def isdigit(self,*args):
415 return self.local_copy().isdigit(*args)
417 def islower(self,*args):
418 return self.local_copy().islower(*args)
420 def isspace(self,*args):
421 return self.local_copy().isspace(*args)
423 def istitle(self,*args):
424 return self.local_copy().istitle(*args)
426 def isupper(self,*args):
427 return self.local_copy().isupper(*args)
429 def join(self,*args):
430 return self.local_copy().join(*args)
432 def ljust(self,*args):
433 return self.local_copy().ljust(*args)
435 def lower(self,*args):
436 return self.local_copy().lower(*args)
438 def lstrip(self,*args):
439 return self.local_copy().lstrip(*args)
441 def partition(self,*args):
442 return self.local_copy().partition(*args)
444 def replace(self,*args):
445 return self.local_copy().replace(*args)
447 def rfind(self,*args):
448 return self.local_copy().rfind(*args)
450 def rindex(self,*args):
451 return self.local_copy().rindex(*args)
453 def rjust(self,*args):
454 return self.local_copy().rjust(*args)
456 def rpartition(self,*args):
457 return self.local_copy().rpartition(*args)
459 def rsplit(self,*args):
460 return self.local_copy().rsplit(*args)
462 def rstrip(self,*args):
463 return self.local_copy().rstrip(*args)
465 def split(self,*args):
466 return self.local_copy().split(*args)
468 def splitlines(self,*args):
469 return self.local_copy().splitlines(*args)
471 def startswith(self,*args):
472 return self.local_copy().startswith(*args)
474 def strip(self,*args):
475 return self.local_copy().strip(*args)
477 def swapcase(self,*args):
478 return self.local_copy().swapcase(*args)
480 def title(self,*args):
481 return self.local_copy().title(*args)
483 def translate(self,*args):
484 return self.local_copy().translate(*args)
486 def upper(self,*args):
487 return self.local_copy().upper(*args)
489 def zfill(self,*args):
490 return self.local_copy().zfill(*args)
493 return len(self.local_copy())
497 def __init__(self,varPtr,meth):
502 def __call__(self,*args):
503 ret=self._var_ptr.invokePythonMethodOn(self._meth,cPickle.dumps(args,cPickle.HIGHEST_PROTOCOL))
504 return GetHandlerFromRef(ret,True)
507 PyHandlerTypeMap={int:Int,float:Float,str:String,list:List,tuple:Tuple,dict:Dict}
509 def GetHandlerFromRef(objCorba,isTempVar=False):
510 """ Returns a client that allows to handle a remote corba ref of a global var easily.
512 assert(isinstance(objCorba,SALOME._objref_PickelizedPyObjServer))
513 v=cPickle.loads(objCorba.fetchSerializedContent())
515 objCorba.UnRegister()
517 return PyHandlerTypeMap[v.__class__](objCorba,isTempVar)
520 def CreateRdOnlyGlobalVar(value,varName,scopeName):
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)
527 def CreateRdExtGlobalVar(value,varName,scopeName):
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)
534 def GetHandlerFromName(varName,scopeName):
537 dsm=salome.naming_service.Resolve("/DataServerManager")
538 d2s=dsm.retriveDataScope(scopeName)
539 return GetHandlerFromRef(d2s.retrieveVar(varName),False)