diff options
author | Michele Calgaro <michele.calgaro@yahoo.it> | 2023-01-03 15:29:41 +0900 |
---|---|---|
committer | Michele Calgaro <michele.calgaro@yahoo.it> | 2023-01-07 22:14:56 +0900 |
commit | 4ce11cc30df1805459a615960fdec204a975548a (patch) | |
tree | 084f1e1fa404bdf0d082b1a58acf392b5d177326 /extensions | |
parent | 065f41b4908b11278d4334e8c85150ec0fd4a9f6 (diff) | |
download | pytde-4ce11cc30df1805459a615960fdec204a975548a.tar.gz pytde-4ce11cc30df1805459a615960fdec204a975548a.zip |
Drop python2 support.
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
Diffstat (limited to 'extensions')
-rw-r--r-- | extensions/dcopexport.py | 23 | ||||
-rw-r--r-- | extensions/dcopext.py | 1139 |
2 files changed, 580 insertions, 582 deletions
diff --git a/extensions/dcopexport.py b/extensions/dcopexport.py index 6cb8b6b..7af07f1 100644 --- a/extensions/dcopexport.py +++ b/extensions/dcopexport.py @@ -59,8 +59,8 @@ numericTypes = ["char", "bool", "short", "int", "long", "uchar", "ushort", "uint stringTypes = ["TQString", "TQCString"] class DCOPExObj (DCOPObject): - def __init__ (self, objid = None): - if isinstance (objid, str): + def __init__(self, objid = None): + if isinstance (objid, bytes): DCOPObject.__init__ (self, objid) else: DCOPObject.__init__ (self) @@ -69,12 +69,12 @@ class DCOPExObj (DCOPObject): def process (self, meth, data, replyType, replyData): # normalize the method signature received - meth = str (DCOPClient.normalizeFunctionSignature (meth)).replace (">", "> ") + _meth = DCOPClient.normalizeFunctionSignature(meth).replace (">", "> ") # see if this method is available from us via DCOP # if we don't have it, maybe DCOPObject already provides it (eg - qt object) - if not self.matchMethod (meth): - return DCOPObject.process(self, meth, data, replyType, replyData); + if not self.matchMethod (_meth): + return DCOPObject.process(self, _meth, data, replyType, replyData); # demarshall the arg list for the actual method call and call the method s = TQDataStream (data, IO_ReadOnly) @@ -84,7 +84,7 @@ class DCOPExObj (DCOPObject): result = self.method.pymethod () else: for i in range (len (self.method.argtypes)): - arglist.append (dcop_next (s, TQCString (self.method.argtypes [i]))) + arglist.append (dcop_next (s, TQCString(self.method.argtypes[i]))) result = self.method.pymethod (*arglist) @@ -149,8 +149,9 @@ class DCOPExObj (DCOPObject): def matchMethod (self, meth): # find the method in the dict if it's there self.method = None - if meth in self.methods: - self.method = self.methods [meth] + _meth = meth.data() + if _meth in self.methods: + self.method = self.methods [_meth] return self.method != None def functions (self): @@ -158,7 +159,7 @@ class DCOPExObj (DCOPObject): # from the entries in the self.methods dict funcs = DCOPObject.functions (self) for func in self.methods.keys (): - funcs.append (" ".join ([self.methods [func].rtype, func])) + funcs.append((" ".join([self.methods[func].rtype, func])).encode()) return funcs; class DCOPExMeth: @@ -173,7 +174,7 @@ class DCOPExMeth: def parseMethod (self, method): # strip whitespace - method = str (DCOPClient.normalizeFunctionSignature (method)).replace (">", "> ") + method = DCOPClient.normalizeFunctionSignature(method.encode()).data().replace (">", "> ") # the return type (rtype) and signature (sig) self.rtype, tail = method.split (" ", 1) @@ -191,7 +192,7 @@ class DCOPExMeth: # the list of arg types self.argtypes = [] - args = tail [i + 1 : -1].split (",") + args = tail [i + 1 : -2].split (",") if args and args != [""]: for arg in args: self.argtypes.append (arg.strip ()) diff --git a/extensions/dcopext.py b/extensions/dcopext.py index f8ab5bf..e9edd30 100644 --- a/extensions/dcopext.py +++ b/extensions/dcopext.py @@ -52,12 +52,12 @@ typedefIntTypes = set(["uchar", "ushort", "uint", "ulong", "TQ_INT8", "Q_INT16", "TQ_INT32", "TQ_LONG", "TQ_UINT8", "Q_UINT16", "TQ_UINT32", "Q_ULONG", "sitze_t", "ssize_t", "int8_t", "int16_t", "int32_t", - "uint8_t", "uint16_t", "uint32_t", "pid_t", "uid_t", - "off_t"]) + "uint8_t", "uint16_t", "uint32_t", "pid_t", "uid_t", + "off_t"]) # XXX string and std::string too? stringTypes = set(["TQString", "TQCString"]) pythonStringTypes = set([TQString, TQCString, str]) -stringTypesDict = {"TQString":TQString,"TQCString":TQCString,"str":str,"unicode":unicode} +stringTypesDict = {"TQString":TQString,"TQCString":TQCString,"str":str} VOID = 0 BOOLEAN = 1 # XXX bool is not supported by dcop_add, but maybe some time... @@ -73,54 +73,54 @@ Torben Weis and Julian Rockey) The three classes below (DCOPApp, DCOPObj and DCOPMeth) allow transparent Python calls to DCOP methods. For example: - d = DCOPApp ("kicker", dcop) + d = DCOPApp ("kicker", dcop) (where "kicker" is the complete name of an application and 'dcop' is the dcopClient instance owned by the TDEApplication creating the DCOPApp instance) creates a DCOPApp instance. All of the classes in this file "borrow" a DCOPClient instance from the calling application. - d.objects + d.objects will return a list of the DCOP objects the application supplies. - o = d.object ("Panel") + o = d.object ("Panel") will return a DCOPObj corresponding to applications "Panel" DCOP object. Similarly: - o.methods + o.methods will return a list of the methods the object supplies and - m = o.method ("panelSize") + m = o.method ("panelSize") will return a DCOPMeth corresponding to Panel's panelSize() method. The m instance also holds the methods return type, list of argument types (argtypes) and argument names (argnames). - m.valid + m.valid is a boolean which indicates if the method encapsulated by m is a valid method for the application/object specified. However it isn't necessary to explicitly create the DCOPObj and DCOPMeth. - d.Panel.panelSize.valid + d.Panel.panelSize.valid for example, will also indicate if the method is valid without creating the intermediate 'o' and 'm' instances explicitly. - d = DCOPApp ("kicker", dcop) - ok, res = d.Panel.panelSize () + d = DCOPApp ("kicker", dcop) + ok, res = d.Panel.panelSize () is all the code necessary to perform the indicated DCOP call and return the value the call returns. In this case, panelSize takes no arguments and returns an int. 'ok' returns the status of the DCOP call (success = True, failure = False). - ok = d.Panel.addURLButton (TQString ("http://www.kde.org")) + ok = d.Panel.addURLButton (TQString ("http://www.kde.org")) would call addURLButton with the required argument, and return nothing but the DCOP call status(since its return type is 'void'). @@ -129,32 +129,32 @@ Note that to instantiate a DCOPObj directly, you need to have a valid DCOPApp to pass to DCOPObj's __init__ method. Similarly, DCOPMeth requires a valid DCOPOBject. For example: - d = DCOPApp ("kicker", dcop) - o = DCOPObj (d, "Panel") - m = DCOPMeth (o, "panelSize") + d = DCOPApp ("kicker", dcop) + o = DCOPObj (d, "Panel") + m = DCOPMeth (o, "panelSize") or - m = DCOPMeth (DCOPObj (DCOPApp ("kicker", dcop), "Panel"), "panelSize") + m = DCOPMeth (DCOPObj (DCOPApp ("kicker", dcop), "Panel"), "panelSize") """ # support stuff: -def _xiter(*seqences): - iters = [iter(seq) for seq in seqences] +def _xiter(*sequences): + iters = [iter(seq) for seq in sequences] - try: - while True: - yield [it.next() for it in iters] - - except StopIteration: - pass + try: + while True: + yield [next(it) for it in iters] + + except StopIteration: + pass def isStringType(s): - for stringType in pythonStringTypes: - if isinstance(s,stringType): - return True - return False + for stringType in pythonStringTypes: + if isinstance(s,stringType): + return True + return False # method syntax: # -------------- @@ -171,553 +171,550 @@ def isStringType(s): # int ::= "0x" [0-9a-fA-F]+ | [0-9]+ class MethodParser(object): - ident_r = re.compile("[_a-zA-Z][_a-zA-Z0-9]*") - num_r = re.compile("0x[0-0a-fA-F]+|[0-9]+") - - def __init__(self,method): - self.method = str(method) - self.rtype = None - self.name = None - self.args = [] - - self.parseMethod() - - def __repr__(self): - return "%s(%s)" % (self.__class__.__name__, repr(self.method)) - - def getDecl(self): - return ''.join([self.name, '(', ','.join(argtp for (argtp, kind), argname in self.args), ')']) - - def parseMethod(self): - i = self.parseRtype(self.method,0) - i, self.name = self.parseIdentifier(self.method,i) - i = self.parseArgs(self.method,i) - - if i != len(self.method): - raise SyntaxError, "invalid function definition: %s" % self.method - - @staticmethod - def skipws(s,i): - while s[i:i+1].isspace(): - i += 1 - return i - - def parseArg(self,s,i): - i, tp = self.parseType(s,i) - name = self.parseIdentifier(s,i) - - if name: - i, name = name - else: - name = None - - return i, (tp, name) - - def parseIdentifier(self,s,i): - i = MethodParser.skipws(s,i) - m = MethodParser.ident_r.match(s,i) - - if m: - return m.end(), s[i:m.end()] - else: - return False - - def parseInteger(self,s,i): - i = MethodParser.skipws(s,i) - m = MethodParser.num_r.match(s,i) - - if m: - return m.end(), s[i:m.end()] - else: - return False - - def parseArgs(self,s,i): - i = MethodParser.skipws(s,i) - - if s[i:i+1] == '(': - i += 1 - i = MethodParser.skipws(s,i) - - while i < len(s) and s[i:i+1] != ')': - i, arg = self.parseArg(s,i) - i = MethodParser.skipws(s,i) - - self.args.append(arg) - - if s[i:i+1] == ',': - i += 1 - - else: - break - - if s[i:i+1] == ')': - i += 1 - else: - raise SyntaxError, "missing ')'." - else: - raise SyntaxError, "missing '('." - - return i - - def parseType(self,s,i): - num = self.parseNumberType(s,i) - - if num: - return num - - i, ns = self.parseNamespace(s,i) - i, tp = self.parseTypespec(s,i) - - tp = ns + tp - - if tp in stringTypes: - return i, (tp, STRING) - - else: - return i, (tp, CLASS) - - def parseTypespec(self,s,i): - i, tp = self.parseIdentifier(s,i) - i, tplst = self.parseTypelist(s,i) - - return i, tp + tplst - - def parseTypelist(self,s,i): - L = [] - newi = MethodParser.skipws(s,i) - - if s[newi:newi+1] == '<': - i = newi + 1 - i = MethodParser.skipws(s,i) - - L.append('<') - - while i < len(s) and s[i:i+1] != '>': - # template-parameter can be integers!! - - num = self.parseInteger(s,i) - - if num: - i, tp = num - - else: - i, (tp, kind) = self.parseType(s,i) - - i = MethodParser.skipws(s,i) - - L.append(tp) - - if s[i:i+1] == ',': - i += 1 - L.append(',') - - else: - break - - - if s[i:i+1] == '>': - i += 1 - L.append('>') - - else: - raise SyntaxError, "missing '>'." - - return i, ''.join(L) - - def parseNumberType(self,s,i): - i, tp = self.parseIdentifier(s,i) - L = [] - - if tp == 'bool': - return i, (tp, BOOLEAN) - - elif tp in typedefIntTypes: - return i, (tp, INTEGER) - - elif tp in ('signed','unsigned'): - L.append(tp) - next = self.parseIdentifier(s,i) - - if next and next[1] in POD: - i, tp = next - - else: - # type can be fully quallyfied here! - return i, (tp, INTEGER) - - if tp in POD: - L.append(tp) - - else: - # else no number-type at all! - - return False - - # long - # long int - # long long - # long long int - # long double - # short - # short int - - if tp == 'short': - # short - - next = self.parseIdentifier(s,i) - - if next and next[1] == 'int': - # short int - - i, tp = next - L.append(tp) - - elif tp == 'long': - # long - - next = self.parseIdentifier(s,i) - - if next: - if next[1] in ('int', 'double'): - # long int - # long double - - i, tp = next - L.append(tp) - - elif next[1] == 'long': - # long long - # XXX: this is 64bit! how should I handle this? - - i, tp = next - L.append(tp) - - next = self.parseIdentifier(s,i) - - if next and next[1] == 'int': - # long long int - - i, tp = next - L.append(tp) - - if tp in ('float', 'double'): - return i, (' '.join(L), FLOAT) - - else: - return i, (' '.join(L), INTEGER) - - # - # :: - # foo:: - # ::foo:: - # foo::bar:: - # ::foo::bar:: - # ... - def parseNamespace(self,s,i): - L = [] - i = MethodParser.skipws(s,i) - - if s[i:i+2] == "::": - i += 2 - L.append("::") - - while i < len(s): - ns = self.parseIdentifier(s,i) - - if not ns: - break - - newi, ns = ns - newi = MethodParser.skipws(s,newi) - - if s[newi:newi+2] != "::": - break - - i = newi + 2 - - L.append( ns ) - L.append( "::" ) - - return i, ''.join(L) - - - def parseRtype(self,s,i): - tp = self.parseIdentifier(s,i) - - if tp and tp[1] == 'void': - i, tp = tp - self.rtype = (tp,VOID) - - else: - i, self.rtype = self.parseType(s,i) - - return i + ident_r = re.compile("[_a-zA-Z][_a-zA-Z0-9]*") + num_r = re.compile("0x[0-0a-fA-F]+|[0-9]+") + + def __init__(self,method): + self.method = method + self.rtype = None + self.name = None + self.args = [] + + self.parseMethod() + + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, repr(self.method)) + + def getDecl(self): + return ''.join([self.name, '(', ','.join(argtp for (argtp, kind), argname in self.args), ')']) + + def parseMethod(self): + i = self.parseRtype(self.method,0) + i, self.name = self.parseIdentifier(self.method,i) + i = self.parseArgs(self.method,i) + + if i != (len(self.method) - 1): + raise SyntaxError("invalid function definition: %s" % self.method) + + @staticmethod + def skipws(s,i): + while s[i:i+1].isspace(): + i += 1 + return i + + def parseArg(self,s,i): + i, tp = self.parseType(s,i) + name = self.parseIdentifier(s,i) + + if name: + i, name = name + else: + name = None + + return i, (tp, name) + + def parseIdentifier(self,s,i): + i = MethodParser.skipws(s,i) + m = MethodParser.ident_r.match(s,i) + + if m: + return m.end(), s[i:m.end()] + else: + return False + + def parseInteger(self,s,i): + i = MethodParser.skipws(s,i) + m = MethodParser.num_r.match(s,i) + + if m: + return m.end(), s[i:m.end()] + else: + return False + + def parseArgs(self,s,i): + i = MethodParser.skipws(s,i) + + if s[i:i+1] == '(': + i += 1 + i = MethodParser.skipws(s,i) + + while i < len(s) and s[i:i+1] != ')': + i, arg = self.parseArg(s,i) + i = MethodParser.skipws(s,i) + + self.args.append(arg) + + if s[i:i+1] == ',': + i += 1 + + else: + break + + if s[i:i+1] == ')': + i += 1 + else: + raise SyntaxError("missing ')'.") + else: + raise SyntaxError("missing '('.") + + return i + + def parseType(self,s,i): + num = self.parseNumberType(s,i) + + if num: + return num + + i, ns = self.parseNamespace(s,i) + i, tp = self.parseTypespec(s,i) + + tp = ns + tp + + if tp in stringTypes: + return i, (tp, STRING) + + else: + return i, (tp, CLASS) + + def parseTypespec(self,s,i): + i, tp = self.parseIdentifier(s,i) + i, tplst = self.parseTypelist(s,i) + + return i, tp + tplst + + def parseTypelist(self,s,i): + L = [] + newi = MethodParser.skipws(s,i) + + if s[newi:newi+1] == '<': + i = newi + 1 + i = MethodParser.skipws(s,i) + + L.append('<') + + while i < len(s) and s[i:i+1] != '>': + # template-parameter can be integers!! + + num = self.parseInteger(s,i) + + if num: + i, tp = num + + else: + i, (tp, kind) = self.parseType(s,i) + + i = MethodParser.skipws(s,i) + + L.append(tp) + + if s[i:i+1] == ',': + i += 1 + L.append(',') + + else: + break + + + if s[i:i+1] == '>': + i += 1 + L.append('>') + + else: + raise SyntaxError("missing '>'.") + + return i, ''.join(L) + + def parseNumberType(self,s,i): + i, tp = self.parseIdentifier(s,i) + L = [] + + if tp == 'bool': + return i, (tp, BOOLEAN) + + elif tp in typedefIntTypes: + return i, (tp, INTEGER) + + elif tp in ('signed','unsigned'): + L.append(tp) + next = self.parseIdentifier(s,i) + + if next and next[1] in POD: + i, tp = next + + else: + # type can be fully quallyfied here! + return i, (tp, INTEGER) + + if tp in POD: + L.append(tp) + + else: + # else no number-type at all! + + return False + + # long + # long int + # long long + # long long int + # long double + # short + # short int + + if tp == 'short': + # short + + next = self.parseIdentifier(s,i) + + if next and next[1] == 'int': + # short int + + i, tp = next + L.append(tp) + + elif tp == 'long': + # long + + next = self.parseIdentifier(s,i) + + if next: + if next[1] in ('int', 'double'): + # long int + # long double + + i, tp = next + L.append(tp) + + elif next[1] == 'long': + # long long + # XXX: this is 64bit! how should I handle this? + + i, tp = next + L.append(tp) + + next = self.parseIdentifier(s,i) + + if next and next[1] == 'int': + # long long int + + i, tp = next + L.append(tp) + + if tp in ('float', 'double'): + return i, (' '.join(L), FLOAT) + + else: + return i, (' '.join(L), INTEGER) + + # + # :: + # foo:: + # ::foo:: + # foo::bar:: + # ::foo::bar:: + # ... + def parseNamespace(self,s,i): + L = [] + i = MethodParser.skipws(s,i) + + if s[i:i+2] == "::": + i += 2 + L.append("::") + + while i < len(s): + ns = self.parseIdentifier(s,i) + + if not ns: + break + + newi, ns = ns + newi = MethodParser.skipws(s,newi) + + if s[newi:newi+2] != "::": + break + + i = newi + 2 + + L.append( ns ) + L.append( "::" ) + + return i, ''.join(L) + + + def parseRtype(self,s,i): + tp = self.parseIdentifier(s,i) + + if tp and tp[1] == 'void': + i, tp = tp + self.rtype = (tp,VOID) + + else: + i, self.rtype = self.parseType(s,i) + + return i def DCOPAppsIter(client): - for app in client.registeredApplications(): - yield str(app) + for app in client.registeredApplications(): + yield str(app) class DCOPApp(object): - """ - An object corresponding to an application with a DCOP interface - - Can return a list of the DCOP objects the application exposes, - or create and return an instance of a specific DCOP object. - """ - def __init__ (self, name, client): - self.appname = name - self.appclient = client - - def __getattr__ (self, item ): - if item == "objects": - objs, ok = self.appclient.remoteObjects(self.appname) - - if ok: - return objs - else: - return None - - return DCOPObj(self, item) - - def __iter__(self): - objs, ok = self.appclient.remoteObjects(self.appname) - - if ok: - for obj in objs: - yield str(obj) - - # sometimes a object-name is not a valid python identifier. - # in that case you can use dcopapp['non-valid::object/name'] - def __getitem__(self,name): - return DCOPObj(self, name) - - def object (self, object): - return DCOPObj (self, object) - - def __repr__(self): - return '%s(%s,%s)' % (self.__class__.__name__,repr(self.appname),repr(self.appclient)) - - def __str__(self): - return repr(self) + """ + An object corresponding to an application with a DCOP interface + + Can return a list of the DCOP objects the application exposes, + or create and return an instance of a specific DCOP object. + """ + def __init__ (self, name, client): + self.appname = name.encode() + self.appclient = client + + def __getattr__ (self, item ): + if item == "objects": + objs, ok = self.appclient.remoteObjects(self.appname) + + if ok: + return objs + else: + return None + + return DCOPObj(self, item) + + def __iter__(self): + objs, ok = self.appclient.remoteObjects(self.appname) + + if ok: + for obj in objs: + yield str(obj) + + # sometimes a object-name is not a valid python identifier. + # in that case you can use dcopapp['non-valid::object/name'] + def __getitem__(self,name): + return DCOPObj(self, name) + + def object (self, object): + return DCOPObj (self, object) + + def __repr__(self): + return '%s(%s,%s)' % (self.__class__.__name__,repr(self.appname),repr(self.appclient)) + + def __str__(self): + return repr(self) class DCOPObj(object): - """ - An object corresponding to a specific DCOP object owned by a - specific application with a DCOP interface - - Can return a list of the DCOP methods the object exposes, - or create and return an instance of a specific DCOP method. - """ - - def __init__ (self, *args): - if isStringType(args[0]): - self.appname = args [0] - self.objclient = args [1] - self.objname = args [2] - else: - self.appname = args [0].appname - self.objname = args [1] - self.objclient = args [0].appclient - - self.objmethods = self.getMethods() - - def __repr__( self ): - return "%s(%s,%s)" % (self.__class__.__name__,repr(self.appname), repr(self.objname)) - - def __str__( self ): - return repr(self) - - def __getattr__( self, item ): - if item == "methods": - return self.objmethods - - return DCOPMeth(self, item) - - def __getitem__(self,name): - return DCOPMeth(self, name) - - def getMethods(self): - flist, ok = self.objclient.remoteFunctions(self.appname, self.objname) - - if ok: - return flist - else: - return None - - def __iter__(self): - flist, ok = self.objclient.remoteFunctions(self.appname, self.objname) - - if ok: - for meth in flist: - yield str(meth) - - def getMethodNames(self): - return [MethodParser(meth).name for meth in self.objmethods] - - def getParsedMethods(self): - return [MethodParser(meth) for meth in self.objmethods] - - def method(self, method): - return DCOPMeth(self, method) + """ + An object corresponding to a specific DCOP object owned by a + specific application with a DCOP interface + + Can return a list of the DCOP methods the object exposes, + or create and return an instance of a specific DCOP method. + """ + + def __init__ (self, *args): + if isStringType(args[0]): + self.appname = args [0].encode() + self.objclient = args [1] + self.objname = args [2] + else: + self.appname = args [0].appname + self.objname = args [1] + self.objclient = args [0].appclient + + self.objmethods = self.getMethods() + + def __repr__( self ): + return "%s(%s,%s)" % (self.__class__.__name__,repr(self.appname), repr(self.objname)) + + def __str__( self ): + return repr(self) + + def __getattr__( self, item ): + if item == "methods": + return self.objmethods + + return DCOPMeth(self, item) + + def __getitem__(self,name): + return DCOPMeth(self, name) + + def getMethods(self): + flist, ok = self.objclient.remoteFunctions(self.appname, self.objname.encode()) + + if ok: + return flist + else: + return None + + def __iter__(self): + flist, ok = self.objclient.remoteFunctions(self.appname, self.objname) + + if ok: + for meth in flist: + yield str(meth) + + def getMethodNames(self): + return [MethodParser(meth).name for meth in self.objmethods] + + def getParsedMethods(self): + return [MethodParser(meth) for meth in self.objmethods] + + def method(self, method): + return DCOPMeth(self, method) class DCOPMeth(object): - """ - An object corresponding to a specific DCOP method owned by a - specific DCOP object. - """ - def __init__(self, dcopObj, name): - self.argtypes = [] - self.argnames = [] - self.fcnname = [] - self.rtype = [] - self.appname = dcopObj.appname - self.objname = dcopObj.objname - self.methname = name - self.client = dcopObj.objclient - try: - self.methods = [str(meth) for meth in dcopObj.objmethods] - except TypeError: - self.methods = [] - self.valid = self.findMethod() -# -# if not self.valid: -# self.fcnname = self.rtype = self.argtypes = self.argnames = None - - def __repr__( self ): - return "%s(%s,%s,%s)" % (self.__class__.__name__,repr(self.appname),repr(self.objname),repr(self.methname)) - - def __str__(self): - return repr(self) - - def __call__(self, *args): - return self.dcop_call(args) - - def __iter__(self): - return iter(self.fcnname) - - def dcop_call(self, args): - # method valid? - if not self.valid: - return False, None - - found = self.getMatchingMethod(args) - - if found is None: - return False, None - - meth, argtypes = found - - ok, replyType, replyData = self.client.call(self.appname, self.objname, meth, self.__marshall(args,argtypes)) - - if ok: - return ok, self.__unmarshall(replyData, replyType) - else: - return ok, None - - def getMatchingMethod(self,args): - count = len(args) - - for funct, argtypes in _xiter(self.fcnname, self.argtypes): - if len(argtypes) == count: - match = True - - for (wanttp, wantkind), have in _xiter(argtypes,args): - if wantkind == BOOLEAN: - if not isinstance(have, bool): - match = False - break - - elif wantkind == INTEGER: - if not isinstance(have, int): - match = False - break - - elif wantkind == FLOAT: - if not isinstance(have, float): - match = False - break - - elif wantkind == STRING: - if not isStringType(have): - match = False - break - - elif wanttp != have.__class__.__name__: - match = False - break - - if match: - return funct, argtypes - return None - - def findMethod(self): - has = False - - for meth in self.methods: - fun = MethodParser(meth) - - if fun.name == self.methname: - self.argtypes.append([argtp for argtp, argname in fun.args]) - self.argnames.append([argname for argtp, argname in fun.args]) - self.rtype.append(fun.rtype) - self.fcnname.append(fun.getDecl()) - - has = True - - return has - - def __marshall(self, args, argtypes): - data = TQByteArray() - if argtypes == []: - return data - - params = TQDataStream (data, IO_WriteOnly) - - for arg, (argtype, argkind) in _xiter(args, argtypes): - if argkind == BOOLEAN: - # XXX for now, let bools be handelt like int - dcop_add(params, int(arg), 'int') - - elif argkind in (INTEGER, FLOAT): - dcop_add(params, arg, argtype) - - elif argkind == STRING: - # convert it to the right string type: - if argtype != arg.__class__.__name__: - arg = stringTypesDict[argtype](arg) - - dcop_add(params, arg) - - elif argtype.startswith("TQMap") or argtype.startswith("TQValueList"): - dcop_add(params, arg, argtype) - - # XXX: - # Is 'isinstance(arg, eval(argtype))' really good? - # What if 'argtype' is located in some modul? Like 'qt.TQString'. - # Then this will fail (but it should not!). - # And the worst thing: the eval() will raise a NameError! - # - # On the other hand 'arg.__class__.__name__ == argtype' has the - # disadvantage that it can't be a derived class! - # - # Would no check at all be better?? - # - # But I doubt a derived class would be ok anyway. I have to check - # this in the DCOP-docu, but I think a derived class would not be - # correctly unmarshalled, because a derived class could be marshalled - # in a total different way to it's super-class. - elif arg.__class__.__name__ == argtype: - dcop_add(params, arg) - - else: - raise TypeError, "expected type %s, got type %s." % (argtype, arg.__class__.__name__) - - return data - - def __unmarshall(self, data, type_): - s = TQDataStream(data, IO_ReadOnly) - - if str(type_) in stringTypes: - return unicode(dcop_next(s, type_)) - else: - return dcop_next(s, type_) + """ + An object corresponding to a specific DCOP method owned by a + specific DCOP object. + """ + def __init__(self, dcopObj, name): + self.argtypes = [] + self.argnames = [] + self.fcnname = [] + self.rtype = [] + self.appname = dcopObj.appname + self.objname = dcopObj.objname + self.methname = name + self.client = dcopObj.objclient + try: + self.methods = [meth.data() for meth in dcopObj.objmethods] + except TypeError: + self.methods = [] + self.valid = self.findMethod() + + def __repr__( self ): + return "%s(%s,%s,%s)" % (self.__class__.__name__,repr(self.appname),repr(self.objname),repr(self.methname)) + + def __str__(self): + return repr(self) + + def __call__(self, *args): + return self.dcop_call(args) + + def __iter__(self): + return iter(self.fcnname) + + def dcop_call(self, args): + # method valid? + if not self.valid: + return False, None + + found = self.getMatchingMethod(args) + + if found is None: + return False, None + + meth, argtypes = found + + ok, replyType, replyData = self.client.call(self.appname, self.objname.encode(), meth.encode(), self.__marshall(args,argtypes)) + + if ok: + return ok, self.__unmarshall(replyData, replyType) + else: + return ok, None + + def getMatchingMethod(self,args): + count = len(args) + + for funct, argtypes in _xiter(self.fcnname, self.argtypes): + if len(argtypes) == count: + match = True + + for (wanttp, wantkind), have in _xiter(argtypes,args): + if wantkind == BOOLEAN: + if not isinstance(have, bool): + match = False + break + + elif wantkind == INTEGER: + if not isinstance(have, int): + match = False + break + + elif wantkind == FLOAT: + if not isinstance(have, float): + match = False + break + + elif wantkind == STRING: + if not isStringType(have): + match = False + break + + elif wanttp != have.__class__.__name__: + match = False + break + + if match: + return funct, argtypes + return None + + def findMethod(self): + has = False + + for meth in self.methods: + fun = MethodParser(meth) + + if fun.name == self.methname: + self.argtypes.append([argtp for argtp, argname in fun.args]) + self.argnames.append([argname for argtp, argname in fun.args]) + self.rtype.append(fun.rtype) + self.fcnname.append(fun.getDecl()) + + has = True + + return has + + def __marshall(self, args, argtypes): + data = TQByteArray() + if argtypes == []: + return data + + params = TQDataStream (data, IO_WriteOnly) + + for arg, (argtype, argkind) in _xiter(args, argtypes): + if argkind == BOOLEAN: + # XXX for now, let bools be handelt like int + dcop_add(params, int(arg), b'int') + + elif argkind in (INTEGER, FLOAT): + dcop_add(params, arg, argtype.encode()) + + elif argkind == STRING: + # convert it to the right string type: + if argtype != arg.__class__.__name__: + arg = stringTypesDict[argtype](arg) + + dcop_add(params, arg) + + elif argtype.startswith("TQMap") or argtype.startswith("TQValueList"): + dcop_add(params, arg, argtype) + + # XXX: + # Is 'isinstance(arg, eval(argtype))' really good? + # What if 'argtype' is located in some modul? Like 'qt.TQString'. + # Then this will fail (but it should not!). + # And the worst thing: the eval() will raise a NameError! + # + # On the other hand 'arg.__class__.__name__ == argtype' has the + # disadvantage that it can't be a derived class! + # + # Would no check at all be better?? + # + # But I doubt a derived class would be ok anyway. I have to check + # this in the DCOP-docu, but I think a derived class would not be + # correctly unmarshalled, because a derived class could be marshalled + # in a total different way to it's super-class. + elif arg.__class__.__name__ == argtype: + dcop_add(params, arg) + + else: + raise TypeError("expected type %s, got type %s." % (argtype, arg.__class__.__name__)) + + return data + + def __unmarshall(self, data, type_): + s = TQDataStream(data, IO_ReadOnly) + + if str(type_.data()) in stringTypes: + return dcop_next(s, type_).decode() + else: + return dcop_next(s, type_) |