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
25 class InvokatorStyle(object):
26 def __init__(self,varPtr):
32 class InvokatorPossibleStyle(InvokatorStyle):
33 def __init__(self,varPtr):
34 InvokatorStyle.__init__(self,varPtr)
35 assert(self.__class__.IsOK(varPtr))
37 def invokePythonMethodOn(self,meth,argsInStrFrmt):
38 return self._var_ptr.invokePythonMethodOn(meth,argsInStrFrmt)
42 return isinstance(varPtr,SALOME._objref_PickelizedPyObjRdExtServer) or isinstance(varPtr,SALOME._objref_PickelizedPyObjRdWrServer)
45 class InvokatorImpossibleStyle(InvokatorStyle):
46 def __init__(self,varPtr):
47 InvokatorStyle.__init__(self,varPtr)
48 assert(self.__class__.IsOK(varPtr))
50 def invokePythonMethodOn(self,meth,argsInStrFrmt):
51 raise Exception("Cannot invoke because it is readonly var !")
55 return isinstance(varPtr,SALOME._objref_PickelizedPyObjRdOnlyServer)
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)))
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
76 return self._var_ptr.ptr()
79 return cPickle.loads(self._var_ptr.ptr().fetchSerializedContent())
82 return self.local_copy().__str__()
85 return self.local_copy().__repr__()
88 return (self._wrapped_type,(self.local_copy(),))
91 assert(isinstance(self._var_ptr,SALOME._objref_PickelizedPyObjRdWrServer))
92 st=cPickle.dumps(elt,cPickle.HIGHEST_PROTOCOL)
93 self._var_ptr.setSerializedContent(st)
97 self._var_ptr.ptr().UnRegister()
101 class List(WrappedType):
102 def __init__(self,varPtr,isTemporaryVar=False):
103 WrappedType.__init__(self,varPtr,isTemporaryVar)
104 self._wrapped_type=list
107 def __getitem__(self,*args):
108 ret=Caller(self._var_ptr,"__getitem__")
111 def __setitem__(self,*args):
112 ret=Caller(self._var_ptr,"__setitem__")
115 def __delitem__(self,*args):
116 ret=Caller(self._var_ptr,"__delitem__")
119 def append(self,*args):
120 ret=Caller(self._var_ptr,"append")
123 def extend(self,*args):
124 ret=Caller(self._var_ptr,"extend")
127 def insert(self,*args):
128 ret=Caller(self._var_ptr,"insert")
132 ret=Caller(self._var_ptr,"pop")
135 def remove(self,*args):
136 ret=Caller(self._var_ptr,"remove")
139 def reverse(self,*args):
140 ret=Caller(self._var_ptr,"reverse")
143 def sort(self,*args):
144 ret=Caller(self._var_ptr,"sort")
147 def count(self,*args):
148 return self.local_copy().count(*args)
150 def index(self,*args):
151 return self.local_copy().index(*args)
154 return len(self.local_copy())
157 class Dict(WrappedType):
158 def __init__(self,varPtr,isTemporaryVar=False):
159 WrappedType.__init__(self,varPtr,isTemporaryVar)
160 self._wrapped_type=dict
163 def __getitem__(self,*args):
164 ret=Caller(self._var_ptr,"__getitem__")
167 def __setitem__(self,*args):
168 ret=Caller(self._var_ptr,"__setitem__")
171 def __delitem__(self,*args):
172 ret=Caller(self._var_ptr,"__delitem__")
175 def clear(self,*args):
176 ret=Caller(self._var_ptr,"clear")
180 ret=Caller(self._var_ptr,"get")
183 def items(self,*args):
184 ret=Caller(self._var_ptr,"items")
188 ret=Caller(self._var_ptr,"pop")
191 def popitem(self,*args):
192 ret=Caller(self._var_ptr,"popitem")
195 def setdefault(self,*args):
196 ret=Caller(self._var_ptr,"setdefault")
199 def update(self,*args):
200 ret=Caller(self._var_ptr,"update")
203 def values(self,*args):
204 ret=Caller(self._var_ptr,"values")
209 def __contains__(self,*args):
210 return self.local_copy().__contains__(*args)
212 def has_key(self,*args):
213 return self.local_copy().has_key(*args)
215 def keys(self,*args):
216 return self.local_copy().keys(*args)
218 def copy(self,*args):
219 return self.local_copy().copy(*args)
222 return len(self.local_copy())
226 class Tuple(WrappedType):
227 def __init__(self,varPtr,isTemporaryVar=False):
228 WrappedType.__init__(self,varPtr,isTemporaryVar)
229 self._wrapped_type=tuple
232 def __getitem__(self,*args):
233 ret=Caller(self._var_ptr,"__getitem__")
238 def count(self,*args):
239 return self.local_copy().count(*args)
241 def index(self,*args):
242 return self.local_copy().index(*args)
245 return len(self.local_copy())
249 class Float(WrappedType):
250 def __init__(self,varPtr,isTemporaryVar=False):
251 WrappedType.__init__(self,varPtr,isTemporaryVar)
252 self._wrapped_type=float
255 def __iadd__(self,*args):
256 return self.local_copy().__add__(*args)
258 def __isub__(self,*args):
259 return self.local_copy().__sub__(*args)
261 def __imul__(self,*args):
262 return self.local_copy().__mul__(*args)
264 def __idiv__(self,*args):
265 return self.local_copy().__div__(*args)
267 def __add__(self,*args):
268 return self.local_copy().__add__(*args)
270 def __sub__(self,*args):
271 return self.local_copy().__sub__(*args)
273 def __mul__(self,*args):
274 return self.local_copy().__mul__(*args)
276 def __div__(self,*args):
277 return self.local_copy().__div__(*args)
279 def __pow__(self,*args):
280 return self.local_copy().__pow__(*args)
282 def as_integer_ratio(self,*args):
283 return self.local_copy().as_integer_ratio(*args)
285 def conjugate(self,*args):
286 return self.local_copy().conjugate(*args)
288 def fromhex(self,*args):
289 return self.local_copy().fromhex(*args)
292 return self.local_copy().hex(*args)
294 def imag(self,*args):
295 return self.local_copy().imag(*args)
297 def is_integer(self,*args):
298 return self.local_copy().is_integer(*args)
300 def real(self,*args):
301 return self.local_copy().real(*args)
304 class Int(WrappedType):
305 def __init__(self,varPtr,isTemporaryVar=False):
306 WrappedType.__init__(self,varPtr,isTemporaryVar)
307 self._wrapped_type=int
310 def __iadd__(self,*args):
311 return self.local_copy().__add__(*args)
313 def __isub__(self,*args):
314 return self.local_copy().__sub__(*args)
316 def __imul__(self,*args):
317 return self.local_copy().__mul__(*args)
319 def __imod__(self,*args):
320 return self.local_copy().__mod__(*args)
322 def __idiv__(self,*args):
323 return self.local_copy().__div__(*args)
325 def __add__(self,*args):
326 return self.local_copy().__add__(*args)
328 def __sub__(self,*args):
329 return self.local_copy().__sub__(*args)
331 def __mul__(self,*args):
332 return self.local_copy().__mul__(*args)
334 def __mod__(self,*args):
335 return self.local_copy().__mod__(*args)
337 def __div__(self,*args):
338 return self.local_copy().__div__(*args)
340 def __pow__(self,*args):
341 return self.local_copy().__pow__(*args)
343 def bit_length(self,*args):
344 return self.local_copy().bit_length(*args)
346 def conjugate(self,*args):
347 return self.local_copy().conjugate(*args)
349 def denominator(self,*args):
350 return self.local_copy().denominator(*args)
352 def imag(self,*args):
353 return self.local_copy().imag(*args)
355 def numerator(self,*args):
356 return self.local_copy().numerator(*args)
358 def real(self,*args):
359 return self.local_copy().real(*args)
362 class String(WrappedType):
363 def __init__(self,varPtr,isTemporaryVar=False):
364 WrappedType.__init__(self,varPtr,isTemporaryVar)
365 self._wrapped_type=int
368 def __add__(self,*args):
369 return self.local_copy().__add__(*args)
371 def __iadd__(self,*args):
372 return self.local_copy().__add__(*args)
374 def __getitem__(self,*args):
375 return self.local_copy().__getitem__(*args)
377 def capitalize(self,*args):
378 return self.local_copy().capitalize(*args)
380 def center(self,*args):
381 return self.local_copy().center(*args)
383 def count(self,*args):
384 return self.local_copy().count(*args)
386 def decode(self,*args):
387 return self.local_copy().decode(*args)
389 def encode(self,*args):
390 return self.local_copy().encode(*args)
392 def endswith(self,*args):
393 return self.local_copy().endswith(*args)
395 def expandtabs(self,*args):
396 return self.local_copy().expandtabs(*args)
398 def find(self,*args):
399 return self.local_copy().find(*args)
401 def format(self,*args):
402 return self.local_copy().format(*args)
404 def index(self,*args):
405 return self.local_copy().index(*args)
407 def isalnum(self,*args):
408 return self.local_copy().isalnum(*args)
410 def isalpha(self,*args):
411 return self.local_copy().isalpha(*args)
413 def isdigit(self,*args):
414 return self.local_copy().isdigit(*args)
416 def islower(self,*args):
417 return self.local_copy().islower(*args)
419 def isspace(self,*args):
420 return self.local_copy().isspace(*args)
422 def istitle(self,*args):
423 return self.local_copy().istitle(*args)
425 def isupper(self,*args):
426 return self.local_copy().isupper(*args)
428 def join(self,*args):
429 return self.local_copy().join(*args)
431 def ljust(self,*args):
432 return self.local_copy().ljust(*args)
434 def lower(self,*args):
435 return self.local_copy().lower(*args)
437 def lstrip(self,*args):
438 return self.local_copy().lstrip(*args)
440 def partition(self,*args):
441 return self.local_copy().partition(*args)
443 def replace(self,*args):
444 return self.local_copy().replace(*args)
446 def rfind(self,*args):
447 return self.local_copy().rfind(*args)
449 def rindex(self,*args):
450 return self.local_copy().rindex(*args)
452 def rjust(self,*args):
453 return self.local_copy().rjust(*args)
455 def rpartition(self,*args):
456 return self.local_copy().rpartition(*args)
458 def rsplit(self,*args):
459 return self.local_copy().rsplit(*args)
461 def rstrip(self,*args):
462 return self.local_copy().rstrip(*args)
464 def split(self,*args):
465 return self.local_copy().split(*args)
467 def splitlines(self,*args):
468 return self.local_copy().splitlines(*args)
470 def startswith(self,*args):
471 return self.local_copy().startswith(*args)
473 def strip(self,*args):
474 return self.local_copy().strip(*args)
476 def swapcase(self,*args):
477 return self.local_copy().swapcase(*args)
479 def title(self,*args):
480 return self.local_copy().title(*args)
482 def translate(self,*args):
483 return self.local_copy().translate(*args)
485 def upper(self,*args):
486 return self.local_copy().upper(*args)
488 def zfill(self,*args):
489 return self.local_copy().zfill(*args)
492 return len(self.local_copy())
496 def __init__(self,varPtr,meth):
501 def __call__(self,*args):
502 ret=self._var_ptr.invokePythonMethodOn(self._meth,cPickle.dumps(args,cPickle.HIGHEST_PROTOCOL))
503 return GetHandlerFromRef(ret,True)
506 PyHandlerTypeMap={int:Int,float:Float,str:String,list:List,tuple:Tuple,dict:Dict}
508 def GetHandlerFromRef(objCorba,isTempVar=False):
509 """ Returns a client that allows to handle a remote corba ref of a global var easily.
511 assert(isinstance(objCorba,SALOME._objref_PickelizedPyObjServer))
512 v=cPickle.loads(objCorba.fetchSerializedContent())
514 objCorba.UnRegister()
516 return PyHandlerTypeMap[v.__class__](objCorba,isTempVar)
519 def CreateRdOnlyGlobalVar(value,varName,scopeName):
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)
526 def CreateRdExtGlobalVar(value,varName,scopeName):
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)
533 def GetHandlerFromName(varName,scopeName):
536 dsm=salome.naming_service.Resolve("/DataServerManager")
537 d2s=dsm.retriveDataScope(scopeName)
538 return GetHandlerFromRef(d2s.retrieveVar(varName),False)