You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
17 KiB

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Quanta Plus">
><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR><TH COLSPAN="3" ALIGN="center">Python Bindings for KDE (PyKDE-3.16.0)</TH></TR>
<TR><TD WIDTH="10%" ALIGN="left" VALIGN="bottom"><A HREF="examples.html" ACCESSKEY="P">Prev</A></TD>
<TD WIDTH="80%" ALIGN="center" VALIGN="bottom"></TD>
<TD WIDTH="10%" ALIGN="right" VALIGN="bottom"><A HREF="limits.html" ACCESSKEY="N">Next</A></TD>
<h1>DCOP and Extensions</h1>
DCOP is KDE's acronym for it's "Desktop Communications-Oriented Protocol" - basically a
lightweight and simple mechanism for inter-process communications (IPC). DCOP allows two
running applications to exchange messages or other information or exercise control over
each other.
While the DCOP implementation is convenient for C++ programmers, it presents some difficulties
for Python programmers. The DCOP extensions that have been added to PyKDE should make most
DCOP applications (either DCOP-client or DCOP-enabled applications) simple to write and
reliable to run
<h2>What Extensions?</h2>
There are three basic extensions added to PyKDE that are not part of KDE itself:
<dt>Packing/Unpacking TQByteArrays</dt>
DCOP passes data between applications using TQByteArrays. TQByteArrays can be difficult to
pack or unpack using PyTQt or PyKDE, so PyKDE has additional methods (dcop_add and dcop_next)
to make these operations simpler in Python
<dt>Client Extensions</dt>
PyKDE's DCOP client extensions make it easy and natural to call DCOP methods in other
DCOP-enabled applications - the application or DCOP object being referenced look like
Python classes, and the method being called looks to the programmer like a Python method.
<dt>DCOP Enabling (Export) Extensions</dt>
Another set of extensions makes it trivial to expose an application's methods via DCOP to
other applications. All that is retquired is to subclass a pre-written Python class and
provide a list of the methods to expose, along with a method signature listing the name
of the method, it's return type, and the the types of its arguments.
The methods for packing/unpacking TQByteArrays are available to the programmer, but are
primarily used transparently by the other PyKDE DCOP extensions. The client and export extensions
are two Python modules that are included and installed as part of PyKDE.
<h2>Calling DCOP Methods</h2>
Accessing a DCOP method in another application retquires 3 pieces of information: the name of
the application to be accessed, the name of the DCOP object which holds the method to be
called, and the name of the method itself.
<h3>Collection the Information</h3>
The easiest way to collect the retquired information is to use the kdcop application that
comes with PyKDE. kdcop is graphical application that looks like the image shown.
<IMG src="images/kdcop1.png" align="middle" border="0">
<h3>Application/Object/Method Information</h3>
Look at the entry for kicker, which has been expanded in the image. Underneath kicker (the
application name - kicker is the panel on the standard KDE screen) is a list of DCOP objects,
for example, Panel. Under each object is a list of methods the application/object exposes, for
example, "int panelPosition ()". This indicates the method panelPosition takes no arguments
and returns an integer.
<h3>Writing the Code</h3>
There are two ways to use the DCOP extensions to call the panelPosition method. The first is
from the application level, the second is from the object level. We can use the "application
level" in this case, because the object name "Panel" can be valid Python identifier (not all
object names have this property).
import dcopext
# ! other imports not shown !
app = KApplication ()
dcop = app.dcopClient ()
d = dcopext.DCOPApp ("kicker", dcop)
ok, panelPos = d.Panel.panelPosition ()
That's all there's to it in this case. We import dcopext, which contains the client extension
classes; from the KApplication instance, we "borrow" the DCOPClient instance (dcop); we create a
DCOPApp instance, passing it the name of the app ("kicker") and the DCOPClient instance; we
call kicker's Panel object's panelPosition method (d.Panel.panelPosition); lastly, the integer
value is returned to our application (panelPos) as the second item in a tuple - the first element
of the tuple (ok) is a boolean value indicating whether the call succeeded (True) or failed (False).
Many of the DCOP object names can't be used as Python identifiers (for example,"0x8239ae0" or
KIO::Scheduler in kicker, or EditInterface#1, which kwrite exports). In that case, it's
necessary to write the code at the object level, constructing a DCOPObj instead of a
DCOPApp (DCOPApp actually constructs a DCOPObj behind the scenese in the example above).
import dcopext
# ! other imports not shown !
o = dcopext.DCOPObj ("kicker", dcop, "Panel")
ok, panelPos = o.panelPosition ()
In this example, 'o' is a DCOPObj. In constructing 'o', we add a string representation of
the name of the object ("Panel") to the application name and DCOPClient object. We then
use the DCOPObj 'o' to call the the method (panelPosition) that the object supports.
<h3>More on Application Names</h3>
In the example above, kicker was the name of the application and the id we used to reference
the application as well. kicker is an example of a unique application - only one instance of
kicker can be running at any time.
Many applications (konqueror, for example) can have several instances running at the same
time. kdcop would display multiple instances like this:
<IMG src="images/kdcop2.png" border="0">
kdcop shows 3 instances of konqueror running in the example above. To perform a DCOP call in
this case, we'd need to know which instance of konqueror we want to send the call to. The
suffix on each instance of konqueror is the PID of the instance running. We simply pass the
full id (app name + pid - eg konqueror-14409) when constructing DCOPApp or DCOPObj.
If you instantiate the application you want to communicate with from your own application (that
will be making the DCOP calls), methods like KApplication.startServiceByDesktopName will
let you start the app and also return both the PID of the started app and the complete
identifier string needed to initiate DCOP communications. The identifier's name portion may or
may not be the same as the name of the application (see the example program,
whose ID is "petshop-####" (#### is the PID of the application instance).
<h3>Data Types</h3>
The DCOP extensions will support any of the following C++ data types:
<TR><TD>long</TD><TD>unsigned char</TD><TD>unsigned short</TD></TR>
<TR><TD>unsigned int</TD><TD>unsigned long</TD><TD>uchar</TD></TR>
Data conversion between C++ and Python types is done transparently. The integer types
map to Python int or Python long, the decimal types to Python double. A Python string
can be used for any argument that retquires a TQString or TQCString (return types will
always be the TQt object type). The TQValueList types take or return a Python list of the
indicated object. The TQMap types take or return a Python dict with the first type as
the key and the second type as data. All other types use the same object type in
Python and TQt (for instance, TQPoint or TQStringList).
It's possible to add support for more types in the future. To be added, a type retquires
a pair of overloaded TQDataStream operators ("&lt;&lt;" and "&gt;&gt;"). Types must also
exist in the libs that PyTQt and PyKDE support - types specific to applications (like
konqueror) cannot be supported at this time.
<h3>Other Extension Features</h3>
The dcopext module consists of 3 classes (DCOPApp, DCOPObj and DCOPMeth) corresponding to
applications, objects and methods respectively. These classes have additional variables and methods:
<li> DCOPApp.objects - returns a list of the applications DCOP objects. example: d.objects</li>
<li> DCOPApp.object(objname) - returns a DCOPObj for the DCOPObject. example: d.object ("Panel")</li>
<li> DCOPObj.methods - returns a list of the methods and object has. example: o.methods</li>
<li> DCOPObj.method (methname) - returns an DCOPMeth instance corresponding to the method, which
can be called. example: o.method("panelPosition")</li>
<li> DCOPMeth.valid - returns whether the method is valid or not (True/False). example:
<li>DCOPMeth.rtype - a method's return type. example d.Panel.panelPosition.rtype</li>
<li>DCOPMeth.argtypes - a list of the method's argument types. example d.Panel.panelPosition.argtypes</li>
<li>DCOPMeth.argnames - a list of the method's argument names. example d.Panel.panelPosition.argnames</li>
If a method isn't valid, it's rtype, argtypes and argnames values will all be None.
<h2>DCOP Enabling a Python Application</h2>
Enabling a Python application to handle DCOP calls is even simpler than making calls as a
DCOP client. Suppose a Python application has two methods we want to appear as int getValue()
and void setValue(int). The corresponding Python methods are get_value() set_value(i).
We want to export these methods under the object "Value". Here's the code:
from dcopexport import DCOPExObj
# ! other imports not shown !
class ValueObject (DCOPExObj):
def __init__ (self, id="Value"):
DCOPExObj.__init__ (self, id)
self.value = 0
self.addMethod ("int getValue()", self.get_value)
self.addMethod ("void setValue(int)", self.set_value)
def get_value(self):
return self.value
def set_value (self, i):
self.value = i
Note that the module for the DCOPExObj class is "dcopexport". The Python methods may be
part of the DCOPExObj subclass, part of another class, or global Python functions. They
must be callable from the DCOPExObj subclass being created. The dcopexport extension takes
care of everything else, including the "functions()" method which applications (yours or
kdcop, for example) can call to find out which methods are available and their return
and argument types. You can have multiple instances of DCOPExObj in a program. All of
the data types listed above are supported transparently - you don't have to pack or
unpack TQByteArrays.
<h2>Packing and Unpacking TQByteArrays</h2>
NOTE: It isn't necessary to use the dcop_add and dcop_next functions or worry about
TQByteArrays at all when using dcopext or dcopexport as shown above. Those modules
handle the packing and unpacking details automatically behind the scenes.
The dcop_add and dcop_next functions are available in the PyKDE tdecore module (they
may be relocated to a different module in the future). They use a TQDataStream to operate
on a TQByteArray. The TQByteArray can be thought of as a stack (a FIFO stack though) -
dcop_add pushes objects onto the stack, dcop_next pops objects off the stack. The first
object popped off will be the first object pushed on, etc.
The dcop_add function is actually a group of overloaded functions, some of which take
different argument counts. Here are some examples:
from tdecore import dcop_add, dcop_next
from qt import TQByteArray, TQDataStream, IO_ReadOnly, IO_WriteOnly, TQString,\
TQCString, TQValueList&lt;TQCString&gt;
from dcopext import numericTypes, stringTypes
b = TQByteArray ()
s = TQDataStream (b, IO_WriteOnly)
i = 6
d = 3.14
t = TQString ("Hello, World")
x = TQCString ("One")
y = TQCString ("Two")
z = TQCString ("Three")
l = [x, y, z]
dcop_add (s, i, "long")
dcop_add (s, d, "double")
dcop_add (s, t)
dcop_add (s, x)
dcop_add (s, l, "TQValueList&lt;TQCString&gt;")
Notice that for numeric types (integer or decimal) an additional string is needed to
specify the C++ type of the object - that's because Python has only 3 basic numeric
types, while C++ has at least 10 basic numeric types plus variations via typedefs.
Also, the TQValueList (and TQMap - not shown) type needs a qualifier - a Python list
type doesn't know (or care) what the type of its elements is.
Other types (TQString, TQCString) are uniquely typed, so no modifier is needed.
While it may change in the future, dcop_add right now retains the variable argument lists.
You can handle this in your own code easily if you import "numericTypes" and
"stringTypes" from dcopext as shown above. The following code will sort things out:
# atype is the type of the argument being processed (as a string)
# value is the object being packed into the TQByteArray
if atype in numericTypes:
dcop_add (s, value, atype)
elif atype in stringTypes and isinstance (value, str):
dcop_add (s, eval ("%s('%s')" % (atype, value)))
elif atype.startswith ("TQMap") or atype.startswith ("TQValueList"):
dcop_add (params, value, atype)
dcop_add (s, value)
At least in DCOP related applications, all of the necessary type information is always
easily available. The first if clause above processes numeric types; the second if
clause allows you to use Python strings in place of TQt's TQString or TQCString types; the
third if clause handles TQValueList and TQMap based types; the else clause handles
everything else.
Unpacking a TQByteArray is simpler - dcop_next always takes a TQDataStream instance and
a type name string. The code below assumes the same set of imports as above:
# b is a TQByteArray to be unpacked
s = TQDataStream (b, IO_ReadOnly)
i1 = dcop_next (s, "long")
d1 = dcop_next (s, "double")
t1 = dcop_next (s, "TQString")
x1 = dcop_next (s, "TQCString")
l1 = dcop_next (s, "TQValueList&lt;TQCString&gt;")
Of course the type specified in dcop_next to unpack the object must match the type of
the object originally packed, and must happen in the same order (you can't use this to cast or convert types). i1, d1, etc
should contain the same values as i, d, etc above.
The types that dcop_add/dcop_next can handle are the same types listed in the dcopext
section above.
The code for dcopext and dcopexport is based on and pcop.cpp written by Torben Weis
and Julian Rockey. It's available in the dcoppython/ section of the kde-bindings source code,
and can be used to implement DCOP communication without using PyTQt or PyKDE.
<TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" >
<TD WIDTH="33%" ALIGN="left" VALIGN="top"><A HREF="examples.html" ACCESSKEY="P">Prev</A></TD>
<TD WIDTH="34%" ALIGN="center" VALIGN="top"><A HREF="index.html" ACCESSKEY="H">Home</A></TD>
<TD WIDTH="33%" ALIGN="right" VALIGN="top"><A HREF="limits.html" ACCESSKEY="N">Next</A></TD>
<TD WIDTH="33%" ALIGN="left" VALIGN="top">Templates and Example Programs</TD>
<TD WIDTH="34%" ALIGN="center" VALIGN="top">&nbsp;</TD>
<TD WIDTH="33%" ALIGN="right" VALIGN="top">General Limitations</TD>