1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
"""
Copyright 2004 Jim Bublitz
Terms and Conditions
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Except as contained in this notice, the name of the copyright holder shall
not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization from the
copyright holder.
"""
"""
This is a re-implementation in Python of pcop.cpp written by Torben Weis
and Julian Rockey, modified for Python and PyKDE.
To "DCOP-enable" an application, subclass DCOPExObj (be sure to call the
base class' __init__ method) and use 'addMethod' to identify the methods
which will be exposed via DCOP along with names of the Python methods that
implement the exposed methods.
A DCOP client application when doing DCOPCLient.call (...) will end up
running the 'process' method which demarshalls the arguments, calls the
specified Python method with the arg values passed in, and marshalls the
return value to be returned to the caller.
DCOPExMeth is basically a data structure to hold the parsed method info
(name, arg list, return type, signature)
This module requires the dcopext module, but only for the numericTypes and
stringTypes lists
"""
from dcop import DCOPObject, DCOPClient
from tdecore import dcop_add, dcop_next
from PyTQt.qt import TQString, TQCString, TQDataStream, IO_ReadOnly, IO_WriteOnly
numericTypes = ["char", "bool", "short", "int", "long", "uchar", "ushort", "uint", "ulong",
"unsigned char", "unsigned short", "unsigned int", "unsigned long",
"TQ_INT32", "pid_t", "float", "double"]
stringTypes = ["TQString", "TQCString"]
class DCOPExObj (DCOPObject):
def __init__(self, objid = None):
if isinstance (objid, bytes):
DCOPObject.__init__ (self, objid)
else:
DCOPObject.__init__ (self)
self.methods = {}
def process (self, meth, data, replyType, replyData):
# normalize the method signature received
_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);
# demarshall the arg list for the actual method call and call the method
s = TQDataStream (data, IO_ReadOnly)
arglist = []
count = len (self.method.argtypes)
if count == 0:
result = self.method.pymethod ()
else:
for i in range (len (self.method.argtypes)):
arglist.append (dcop_next (s, TQCString(self.method.argtypes[i])))
result = self.method.pymethod (*arglist)
# marshall the result as 'replyData'
if self.method.rtype != "void":
s = TQDataStream (replyData, IO_WriteOnly)
if self.method.rtype in numericTypes:
dcop_add (s, result, self.method.rtype)
elif self.method.rtype in stringTypes and isinstance (result, str):
dcop_add (s, eval ("%s('''%s''')" % (self.method.rtype, result)))
elif self.method.rtype.startswith ("TQMap") or self.method.rtype.startswith ("TQValueList"):
dcop_add (params, args [i], self.argtypes [i])
else:
if not result:
dcop_add (s, "")
else:
dcop_add (s, result)
# use append because we want to return the replyType reference,
# not a new TQCString
replyType.append (self.method.rtype)
# success
return True
def addMethod (self, signature, pymethod):
"""
add a method to the dict - makes it available to DCOP
signature - a string representing the C++ form of the method declaration
with arg names removed (eg
pymethod - the Python method corresponding to the method in signature
example:
def someMethod (a, b):
return str (a + b)
signature = "TQString someMethod (int, int)"
pymethod = someMethod
self.addMethod (signature, pymethod)
note that in this case you could add a second entry:
self.addMethod ("TQString someMethod (float, float)", someMethod)
pymethod can also be a class method, for example - self.someMethod or
someClass.someMethod. In the second case, someClass has to be an instance
of a class (perhaps SomeClass), not the class itself.
self.methods is a dict holding all of the methods exposed, indexed by
method signature. In the example above, the signature would be:
someMethod(TQString,TQString)
or everything but the return type, which is stored in the dict entry.
The dict entry is a DCOPExMeth instance.
"""
method = DCOPExMeth (signature, pymethod)
if method.sig:
self.methods [method.sig] = method
return method.sig != None
def matchMethod (self, meth):
# find the method in the dict if it's there
self.method = None
_meth = meth.data()
if _meth in self.methods:
self.method = self.methods [_meth]
return self.method != None
def functions (self):
# build the list of methods exposed for 'remoteFunctions' calls
# 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])).encode())
return funcs;
class DCOPExMeth:
"""
Encapsulates all of the method data - signature, arg list, return type
and corresponding Python method to be called
"""
def __init__ (self, method, pymethod):
self.pymethod = pymethod
if not self.parseMethod (method):
self.fcnname = self.sig = self.rtype = self.argtypes = None
def parseMethod (self, method):
# strip whitespace
method = DCOPClient.normalizeFunctionSignature(method.encode()).data().replace (">", "> ")
# the return type (rtype) and signature (sig)
self.rtype, tail = method.split (" ", 1)
self.sig = tail
if not tail:
return False
self.rtype = self.rtype.strip ()
i = tail.find ("(")
if i < 1:
return False
# the name of the method
self.fcnname = tail [:i].strip () + "("
# the list of arg types
self.argtypes = []
args = tail [i + 1 : -2].split (",")
if args and args != [""]:
for arg in args:
self.argtypes.append (arg.strip ())
return True
|