Imported Upstream version 1.0.0
[platform/upstream/dbus-python.git] / doc / tutorial.txt
1 ====================
2 dbus-python tutorial
3 ====================
4
5 :Author: Simon McVittie, `Collabora Ltd.`_
6 :Date: 2006-06-14
7
8 .. _`Collabora Ltd.`: http://www.collabora.co.uk/
9
10 This tutorial requires Python 2.4 or up, and ``dbus-python`` 0.80rc4 or up.
11
12 .. contents::
13
14 .. --------------------------------------------------------------------
15
16 .. _Bus object:
17 .. _Bus objects:
18
19 Connecting to the Bus
20 =====================
21
22 Applications that use D-Bus typically connect to a *bus daemon*, which
23 forwards messages between the applications. To use D-Bus, you need to create a
24 ``Bus`` object representing the connection to the bus daemon.
25
26 There are generally two bus daemons you may be interested in. Each user
27 login session should have a *session bus*, which is local to that
28 session. It's used to communicate between desktop applications. Connect
29 to the session bus by creating a ``SessionBus`` object::
30
31     import dbus
32
33     session_bus = dbus.SessionBus()
34
35 The *system bus* is global and usually started during boot; it's used to
36 communicate with system services like udev_, NetworkManager_, and the
37 `Hardware Abstraction Layer daemon (hald)`_. To connect to the system
38 bus, create a ``SystemBus`` object::
39
40     import dbus
41
42     system_bus = dbus.SystemBus()
43
44 Of course, you can connect to both in the same application.
45
46 For special purposes, you might use a non-default Bus, or a connection
47 which isn't a Bus at all, using some new API added in dbus-python 0.81.0.
48 This is not described here, and will at some stage be the subject of a separate
49 tutorial.
50
51 .. _udev:
52     http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev.html
53 .. _NetworkManager:
54     http://www.gnome.org/projects/NetworkManager/
55 .. _Hardware Abstraction Layer daemon (hald):
56     http://www.freedesktop.org/wiki/Software/hal
57
58 .. --------------------------------------------------------------------
59
60 Making method calls
61 ===================
62
63 D-Bus applications can export objects for other applications' use. To
64 start working with an object in another application, you need to know:
65
66 * The *bus name*. This identifies which application you want to
67   communicate with. You'll usually identify applications by a
68   *well-known name*, which is a dot-separated string starting with a
69   reversed domain name, such as ``org.freedesktop.NetworkManager``
70   or ``com.example.WordProcessor``.
71
72 * The *object path*. Applications can export many objects - for
73   instance, example.com's word processor might provide an object
74   representing the word processor application itself and an object for
75   each document window opened, or it might also provide an object for
76   each paragraph within a document.
77   
78   To identify which one you want to interact with, you use an object path,
79   a slash-separated string resembling a filename. For instance, example.com's
80   word processor might provide an object at ``/`` representing the word
81   processor itself, and objects at ``/documents/123`` and
82   ``/documents/345`` representing opened document windows.
83
84 As you'd expect, one of the main things you can do with remote objects
85 is to call their methods. As in Python, methods may have parameters,
86 and they may return one or more values.
87
88 .. _proxy object:
89
90 Proxy objects
91 -------------
92
93 To interact with a remote object, you use a *proxy object*. This is a
94 Python object which acts as a proxy or "stand-in" for the remote object -
95 when you call a method on a proxy object, this causes dbus-python to make
96 a method call on the remote object, passing back any return values from
97 the remote object's method as the return values of the proxy method call.
98
99 To obtain a proxy object, call the ``get_object`` method on the ``Bus``.
100 For example, NetworkManager_ has the well-known name
101 ``org.freedesktop.NetworkManager`` and exports an object whose object
102 path is ``/org/freedesktop/NetworkManager``, plus an object per network
103 interface at object paths like
104 ``/org/freedesktop/NetworkManager/Devices/eth0``. You can get a proxy
105 for the object representing eth0 like this::
106
107     import dbus
108     bus = dbus.SystemBus()
109     proxy = bus.get_object('org.freedesktop.NetworkManager',
110                            '/org/freedesktop/NetworkManager/Devices/eth0')
111     # proxy is a dbus.proxies.ProxyObject
112
113 Interfaces and methods
114 ----------------------
115
116 D-Bus uses *interfaces* to provide a namespacing mechanism for methods.
117 An interface is a group of related methods and signals (more on signals
118 later), identified by a name which is a series of dot-separated components
119 starting with a reversed domain name. For instance, each NetworkManager_
120 object representing a network interface implements the interface
121 ``org.freedesktop.NetworkManager.Devices``, which has methods like
122 ``getProperties``.
123
124 To call a method, call the method of the same name on the proxy object,
125 passing in the interface name via the ``dbus_interface`` keyword argument::
126
127     import dbus
128     bus = dbus.SystemBus()
129     eth0 = bus.get_object('org.freedesktop.NetworkManager',
130                           '/org/freedesktop/NetworkManager/Devices/eth0')
131     props = eth0.getProperties(dbus_interface='org.freedesktop.NetworkManager.Devices')
132     # props is a tuple of properties, the first of which is the object path
133
134 .. _dbus.Interface:
135
136 As a short cut, if you're going to be calling many methods with the same
137 interface, you can construct a ``dbus.Interface`` object and call
138 methods on that, without needing to specify the interface again::
139
140     import dbus
141     bus = dbus.SystemBus()
142     eth0 = bus.get_object('org.freedesktop.NetworkManager',
143                           '/org/freedesktop/NetworkManager/Devices/eth0')
144     eth0_dev_iface = dbus.Interface(eth0,
145         dbus_interface='org.freedesktop.NetworkManager.Devices')
146     props = eth0_dev_iface.getProperties()
147     # props is the same as before
148
149 See also
150 ~~~~~~~~
151
152 See the example in ``examples/example-client.py``. Before running it,
153 you'll need to run ``examples/example-service.py`` in the background or
154 in another shell.
155
156 Data types
157 ----------
158
159 Unlike Python, D-Bus is statically typed - each method has a certain
160 *signature* representing the types of its arguments, and will not accept
161 arguments of other types.
162
163 D-Bus has an introspection mechanism, which ``dbus-python`` tries to use
164 to discover the correct argument types. If this succeeds, Python types
165 are converted into the right D-Bus data types automatically, if possible;
166 ``TypeError`` is raised if the type is inappropriate.
167
168 If the introspection mechanism fails (or the argument's type is
169 variant - see below), you have to provide arguments of
170 the correct type. ``dbus-python`` provides Python types corresponding to
171 the D-Bus data types, and a few native Python types are also converted to
172 D-Bus data types automatically. If you use a type which isn't among these,
173 a ``TypeError`` will be raised telling you that ``dbus-python`` was
174 unable to guess the D-Bus signature.
175
176 Basic types
177 ~~~~~~~~~~~
178
179 The following basic data types are supported.
180
181 ==========================  =============================  =====
182 Python type                 converted to D-Bus type        notes
183 ==========================  =============================  =====
184 D-Bus `proxy object`_       ObjectPath (signature 'o')     `(+)`_
185 `dbus.Interface`_           ObjectPath (signature 'o')     `(+)`_
186 `dbus.service.Object`_      ObjectPath (signature 'o')     `(+)`_
187 ``dbus.Boolean``            Boolean (signature 'b')        a subclass of ``int``
188 ``dbus.Byte``               byte (signature 'y')           a subclass of ``int``
189 ``dbus.Int16``              16-bit signed integer ('n')    a subclass of ``int``
190 ``dbus.Int32``              32-bit signed integer ('i')    a subclass of ``int``
191 ``dbus.Int64``              64-bit signed integer ('x')    `(*)`_
192 ``dbus.UInt16``             16-bit unsigned integer ('q')  a subclass of ``int``
193 ``dbus.UInt32``             32-bit unsigned integer ('u')  `(*)_`
194 ``dbus.UInt64``             64-bit unsigned integer ('t')  `(*)_`
195 ``dbus.Double``             double-precision float ('d')   a subclass of ``float``
196 ``dbus.ObjectPath``         object path ('o')              a subclass of ``str``
197 ``dbus.Signature``          signature ('g')                a subclass of ``str``
198 ``dbus.String``             string ('s')                   a subclass of 
199                                                            ``unicode``
200 ``dbus.UTF8String``         string ('s')                   a subclass of ``str``
201 ``bool``                    Boolean ('b')
202 ``int`` or subclass         32-bit signed integer ('i')
203 ``long`` or subclass        64-bit signed integer ('x')
204 ``float`` or subclass       double-precision float ('d')
205 ``str`` or subclass         string ('s')                   must be valid UTF-8
206 ``unicode`` or subclass     string ('s')
207 ==========================  =============================  =====
208
209 .. _(*):
210
211 Types marked (*) may be a subclass of either ``int`` or ``long``, depending
212 on platform.
213
214 .. _(+):
215
216 (+): D-Bus proxy objects, exported D-Bus service objects and anything
217 else with the special attribute ``__dbus_object_path__``, which
218 must be a string, are converted to their object-path. This might be
219 useful if you're writing an object-oriented API using dbus-python.
220
221 Basic type conversions
222 ~~~~~~~~~~~~~~~~~~~~~~
223
224 If introspection succeeded, ``dbus-python`` will also accept:
225
226 * for Boolean parameters, any object (converted as if via ``int(bool(...))``)
227 * for byte parameters, a single-character string (converted as if via ``ord()``)
228 * for byte and integer parameters, any integer (must be in the correct range)
229 * for object-path and signature parameters, any ``str`` or ``unicode``
230   subclass (the value must follow the appropriate syntax)
231
232 Container types
233 ~~~~~~~~~~~~~~~
234
235 D-Bus supports four container types: array (a variable-length sequence of the
236 same type), struct (a fixed-length sequence whose members may have
237 different types), dictionary (a mapping from values of the same basic type to
238 values of the same type), and variant (a container which may hold any
239 D-Bus type, including another variant).
240
241 Arrays are represented by Python lists, or by ``dbus.Array``, a subclass
242 of ``list``. When sending an array, if an introspected signature is
243 available, that will be used; otherwise, if the ``signature`` keyword
244 parameter was passed to the ``Array`` constructor, that will be used to
245 determine the contents' signature; otherwise, ``dbus-python`` will guess
246 from the array's first item.
247
248 The signature of an array is 'ax' where 'x' represents the signature of
249 one item. For instance, you could also have 'as' (array of strings) or
250 'a(ii)' (array of structs each containing two 32-bit integers).
251
252 There's also a type ``dbus.ByteArray`` which is a subclass of ``str``,
253 used as a more efficient representation of a D-Bus array of bytes
254 (signature 'ay').
255
256 Structs are represented by Python tuples, or by ``dbus.Struct``, a
257 subclass of ``tuple``. When sending a struct, if an introspected signature is
258 available, that will be used; otherwise, if the ``signature`` keyword
259 parameter was passed to the ``Array`` constructor, that will be used to
260 determine the contents' signature; otherwise, ``dbus-python`` will guess
261 from the array's first item.
262
263 The signature of a struct consists of the signatures of the contents,
264 in parentheses - for instance '(is)' is the signature of a struct
265 containing a 32-bit integer and a string.
266
267 Dictionaries are represented by Python dictionaries, or by
268 ``dbus.Dictionary``, a subclass of ``dict``. When sending a dictionary,
269 if an introspected signature is available, that will be used; otherwise,
270 if the ``signature`` keyword parameter was passed to the ``Dictionary``
271 constructor, that will be used to determine the contents' key and value
272 signatures; otherwise, ``dbus-python`` will guess from an arbitrary item
273 of the ``dict``.
274
275 The signature of a dictionary is 'a{xy}' where 'x' represents the
276 signature of the keys (which may not be a container type) and 'y'
277 represents the signature of the values. For instance,
278 'a{s(ii)}' is a dictionary where the keys are strings and the values are
279 structs containing two 32-bit integers.
280
281 Variants are represented by setting the ``variant_level`` keyword
282 argument in the constructor of any D-Bus data type to a value greater
283 than 0 (``variant_level`` 1 means a variant containing some other data type,
284 ``variant_level`` 2 means a variant containing a variant containing some
285 other data type, and so on). If a non-variant is passed as an argument
286 but introspection indicates that a variant is expected, it'll
287 automatically be wrapped in a variant.
288
289 The signature of a variant is 'v'.
290
291 .. _byte_arrays and utf8_strings:
292
293 Return values, and the ``byte_arrays`` and ``utf8_strings`` options
294 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
295
296 If a D-Bus method returns no value, the Python proxy method will return
297 ``None``.
298
299 If a D-Bus method returns one value, the Python proxy method will return
300 that value as one of the ``dbus.`` types - by default, strings are
301 returned as ``dbus.String`` (a subclass of Unicode) and byte arrays are
302 returned as a ``dbus.Array`` of ``dbus.Byte``.
303
304 If a D-Bus method returns multiple values, the Python proxy method
305 will return a tuple containing those values.
306
307 If you want strings returned as ``dbus.UTF8String`` (a subclass of
308 ``str``) pass the keyword parameter ``utf8_strings=True`` to the proxy
309 method.
310
311 If you want byte arrays returned as ``dbus.ByteArray`` (also a
312 subclass of ``str`` - in practice, this is often what you want) pass
313 the keyword parameter ``byte_arrays=True`` to the proxy method.
314
315 .. --------------------------------------------------------------------
316
317 Making asynchronous method calls
318 ================================
319
320 Asynchronous (non-blocking) method calls allow multiple method calls to
321 be in progress simultaneously, and allow your application to do other
322 work while it's waiting for the results. To make asynchronous calls,
323 you first need an event loop or "main loop".
324
325 Setting up an event loop
326 ------------------------
327
328 Currently, the only main loop supported by ``dbus-python`` is GLib.
329
330 ``dbus-python`` has a global default main loop, which is the easiest way
331 to use this functionality. To arrange for the GLib main loop to be the
332 default, use::
333
334     from dbus.mainloop.glib import DBusGMainLoop
335
336     DBusGMainLoop(set_as_default=True)
337
338 You must do this before `connecting to the bus`_.
339
340 Actually starting the main loop is as usual for ``pygobject``::
341
342     import gobject
343
344     loop = gobject.MainLoop()
345     loop.run()
346
347 While ``loop.run()`` is executing, GLib will run your callbacks when
348 appropriate. To stop, call ``loop.quit()``.
349
350 You can also set a main loop on a per-connection basis, by passing a
351 main loop to the Bus constructor::
352
353     import dbus
354     from dbus.mainloop.glib import DBusGMainLoop
355
356     dbus_loop = DBusGMainLoop()
357
358     bus = dbus.SessionBus(mainloop=dbus_loop)
359
360 This isn't very useful until we support more than one main loop, though.
361
362 Backwards compatibility: ``dbus.glib``
363 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
364
365 In versions of ``dbus-python`` prior to 0.80, the way to set GLib as the
366 default main loop was::
367
368     import dbus.glib
369
370 Executing that import statement would automatically load the GLib main
371 loop and make this the default. This is now deprecated, since it's
372 highly non-obvious, but may be useful if you want to write or understand
373 backwards-compatible code.
374
375 The Qt main loop
376 ~~~~~~~~~~~~~~~~
377
378 PyQt v4.2 and later includes support for integrating dbus-python with
379 the Qt event loop. To connect D-Bus to this main loop, call
380 ``dbus.mainloop.qt.DBusQtMainLoop`` instead of
381 ``dbus.mainloop.glib.DBusGMainLoop``. Otherwise the Qt loop is used in
382 exactly the same way as the GLib loop.
383
384 Making asynchronous calls
385 -------------------------
386
387 To make a call asynchronous, pass two callables as keyword arguments
388 ``reply_handler`` and ``error_handler`` to the proxy method. The proxy
389 method will immediately return `None`. At some later time, when the event
390 loop is running, one of these will happen: either
391
392 * the ``reply_handler`` will be called with the method's return values
393   as arguments; or
394
395 * the ``error_handler`` will be called with one argument, an instance of
396   ``DBusException`` representing a remote exception.
397
398 See also
399 ~~~~~~~~
400
401 ``examples/example-async-client.py`` makes asynchronous method calls to
402 the service provided by ``examples/example-service.py`` which return
403 either a value or an exception. As for ``examples/example-client.py``,
404 you need to run ``examples/example-service.py`` in the background or
405 in another shell first.
406
407 .. --------------------------------------------------------------------
408
409 Receiving signals
410 =================
411
412 To receive signals, the Bus needs to be connected to an event loop - see
413 section `Setting up an event loop`_. Signals will only be received while
414 the event loop is running.
415
416 Signal matching
417 ---------------
418
419 To respond to signals, you can use the ``add_signal_receiver`` method on
420 `Bus objects`_. This arranges for a callback to be called when a
421 matching signal is received, and has the following arguments:
422
423 * a callable (the ``handler_function``) which will be called by the event loop
424   when the signal is received - its parameters will be the arguments of
425   the signal
426
427 * the signal name, ``signal_name``: here None (the default) matches all names
428
429 * the D-Bus interface, ``dbus_interface``: again None is the default,
430   and matches all interfaces
431
432 * a sender bus name (well-known or unique), ``bus_name``: None is again
433   the default, and matches all senders. Well-known names match signals
434   from whatever application is currently the primary owner of that
435   well-known name.
436
437 * a sender object path, ``path``: once again None is the default and
438   matches all object paths
439
440 ``add_signal_receiver`` also has keyword arguments ``utf8_strings`` and
441 ``byte_arrays`` which influence the types used when calling the
442 handler function, in the same way as the `byte_arrays and utf8_strings`_
443 options on proxy methods.
444
445 ``add_signal_receiver`` returns a ``SignalMatch`` object. Its only
446 useful public API at the moment is a ``remove`` method with no
447 arguments, which removes the signal match from the connection.
448
449 Getting more information from a signal
450 --------------------------------------
451
452 You can also arrange for more information to be passed to the handler
453 function. If you pass the keyword arguments ``sender_keyword``,
454 ``destination_keyword``, ``interface_keyword``, ``member_keyword`` or
455 ``path_keyword`` to the ``connect_to_signal`` method, the appropriate
456 part of the signal message will be passed to the handler function as a
457 keyword argument: for instance if you use ::
458
459     def handler(sender=None):
460         print "got signal from %r" % sender
461
462     iface.connect_to_signal("Hello", handler, sender_keyword='sender')
463
464 and a signal ``Hello`` with no arguments is received from
465 ``com.example.Foo``, the ``handler`` function will be called with
466 ``sender='com.example.Foo'``.
467
468 String argument matching
469 ------------------------
470
471 If there are keyword parameters for the form ``arg``\ *n* where n is a
472 small non-negative number, their values must be ``unicode`` objects
473 or UTF-8 strings. The handler will only be called if that argument
474 of the signal (numbered from zero) is a D-Bus string (in particular,
475 not an object-path or a signature) with that value.
476
477 .. *this comment is to stop the above breaking vim syntax highlighting*
478
479 Receiving signals from a proxy object
480 -------------------------------------
481
482 `Proxy objects`_ have a special method ``connect_to_signal`` which
483 arranges for a callback to be called when a signal is received
484 from the corresponding remote object. The parameters are:
485
486 * the name of the signal
487
488 * a callable (the handler function) which will be called by the event loop
489   when the signal is received - its parameters will be the arguments of
490   the signal
491
492 * the handler function, a callable: the same as for ``add_signal_receiver``
493
494 * the keyword argument ``dbus_interface`` qualifies the name with its
495   interface
496
497 `dbus.Interface` objects have a similar ``connect_to_signal`` method,
498 but in this case you don't need the ``dbus_interface`` keyword argument
499 since the interface to use is already known.
500
501 The same extra keyword arguments as for ``add_signal_receiver`` are also
502 available, and just like ``add_signal_receiver``, it returns a
503 SignalMatch.
504
505 You shouldn't use proxy objects just to listen to signals, since they
506 might activate the relevant service when created, but if you already have a
507 proxy object in order to call methods, it's often convenient to use it to add
508 signal matches too.
509
510 See also
511 --------
512
513 ``examples/signal-recipient.py`` receives signals - it demonstrates
514 general signal matching as well as ``connect_to_signal``. Before running it,
515 you'll need to run ``examples/signal-emitter.py`` in the background or
516 in another shell.
517
518 .. _BusName:
519
520 .. --------------------------------------------------------------------
521
522 Claiming a bus name
523 ===================
524
525 FIXME describe `BusName`_ - perhaps fix its API first?
526
527 The unique-instance idiom
528 -------------------------
529
530 FIXME provide exemplary code, put it in examples
531
532 .. _exported object:
533 .. _exported objects:
534
535 .. --------------------------------------------------------------------
536
537 Exporting objects
538 =================
539
540 Objects made available to other applications over D-Bus are said to be
541 *exported*. All subclasses of ``dbus.service.Object`` are automatically
542 exported.
543
544 To export objects, the Bus needs to be connected to an event loop - see
545 section `Setting up an event loop`_. Exported methods will only be called,
546 and queued signals will only be sent, while the event loop is running.
547
548 .. _dbus.service.Object:
549
550 Inheriting from ``dbus.service.Object``
551 ---------------------------------------
552
553 To export an object onto the Bus, just subclass
554 ``dbus.service.Object``. Object expects either a `BusName`_ or a `Bus
555 object`_, and an object-path, to be passed to its constructor: arrange
556 for this information to be available. For example::
557
558     class Example(dbus.service.Object):
559         def __init__(self, object_path):
560             dbus.service.Object.__init__(self, dbus.SessionBus(), path)
561
562 This object will automatically support introspection, but won't do
563 anything particularly interesting. To fix that, you'll need to export some
564 methods and signals too.
565
566 FIXME also mention dbus.gobject.ExportedGObject once I've written it
567
568 Exporting methods with ``dbus.service.method``
569 ----------------------------------------------
570
571 To export a method, use the decorator ``dbus.service.method``. For
572 example::
573
574     class Example(dbus.service.Object):
575         def __init__(self, object_path):
576             dbus.service.Object.__init__(self, dbus.SessionBus(), path)
577
578         @dbus.service.method(dbus_interface='com.example.Sample',
579                              in_signature='v', out_signature='s')
580         def StringifyVariant(self, variant):
581             return str(variant)
582
583 The ``in_signature`` and ``out_signature`` are D-Bus signature strings
584 as described in `Data Types`_.
585
586 As well as the keywords shown, you can pass ``utf8_strings`` and
587 ``byte_arrays`` keyword arguments, which influence the types which will
588 be passed to the decorated method when it's called via D-Bus, in the
589 same way that the `byte_arrays and utf8_strings`_ options affect the
590 return value of a proxy method.
591
592 You can find a simple example in ``examples/example-service.py``, which
593 we used earlier to demonstrate ``examples/example-client.py``.
594
595 Finding out the caller's bus name
596 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
597
598 The ``method`` decorator accepts a ``sender_keyword`` keyword argument.
599 If you set that to a string, the unique bus name of the sender will be
600 passed to the decorated method as a keyword argument of that name::
601
602     class Example(dbus.service.Object):
603         def __init__(self, object_path):
604             dbus.service.Object.__init__(self, dbus.SessionBus(), path)
605
606         @dbus.service.method(dbus_interface='com.example.Sample',
607                              in_signature='', out_signature='s',
608                              sender_keyword='sender')
609         def SayHello(self, sender=None):
610             return 'Hello, %s!' % sender
611             # -> something like 'Hello, :1.1!'
612
613 Asynchronous method implementations
614 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
615
616 FIXME and also add an example, perhaps examples/example-async-service.py
617
618 Emitting signals with ``dbus.service.signal``
619 ---------------------------------------------
620
621 To export a signal, use the decorator ``dbus.service.signal``; to emit
622 that signal, call the decorated method. The decorated method can also
623 contain code which will be run when called, as usual. For example::
624
625     class Example(dbus.service.Object):
626         def __init__(self, object_path):
627             dbus.service.Object.__init__(self, dbus.SessionBus(), path)
628
629         @dbus.service.signal(dbus_interface='com.example.Sample',
630                              signature='us')
631         def NumberOfBottlesChanged(self, number, contents):
632             print "%d bottles of %s on the wall" % (number, contents)
633
634     e = Example('/bottle-counter')
635     e.NumberOfBottlesChanged(100, 'beer')
636     # -> emits com.example.Sample.NumberOfBottlesChanged(100, 'beer')
637     #    and prints "100 bottles of beer on the wall"
638
639 The signal will be queued for sending when the decorated method returns -
640 you can prevent the signal from being sent by raising an exception
641 from the decorated method (for instance, if the parameters are
642 inappropriate). The signal will only actually be sent when the event loop
643 next runs.
644
645 Example
646 ~~~~~~~
647
648 ``examples/example-signal-emitter.py`` emits some signals on demand when
649 one of its methods is called. (In reality, you'd emit a signal when some
650 sort of internal state changed, which may or may not be triggered by a
651 D-Bus method call.)
652
653 .. --------------------------------------------------------------------
654
655 License for this document
656 =========================
657
658 Copyright 2006-2007 `Collabora Ltd.`_
659
660 Permission is hereby granted, free of charge, to any person
661 obtaining a copy of this software and associated documentation
662 files (the "Software"), to deal in the Software without
663 restriction, including without limitation the rights to use, copy,
664 modify, merge, publish, distribute, sublicense, and/or sell copies
665 of the Software, and to permit persons to whom the Software is
666 furnished to do so, subject to the following conditions:
667
668 The above copyright notice and this permission notice shall be
669 included in all copies or substantial portions of the Software.
670
671 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
672 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
673 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
674 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
675 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
676 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
677 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
678 DEALINGS IN THE SOFTWARE.
679
680 ..
681   vim:set ft=rst sw=4 sts=4 et tw=72: