/usr/share/doc/python-dbus-doc/tutorial.html is in python-dbus-doc 1.2.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 | <?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.14: http://docutils.sourceforge.net/" />
<title>dbus-python tutorial</title>
<meta name="author" content="Simon McVittie, Collabora Ltd." />
<meta name="date" content="2006-06-14" />
<style type="text/css">
/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 7952 2016-07-26 18:15:59Z milde $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/
/* used to remove borders from tables and images */
.borderless, table.borderless td, table.borderless th {
border: 0 }
table.borderless td, table.borderless th {
/* Override padding for "table.docutils td" with "! important".
The right padding separates the table cells. */
padding: 0 0.5em 0 0 ! important }
.first {
/* Override more specific margin styles with "! important". */
margin-top: 0 ! important }
.last, .with-subtitle {
margin-bottom: 0 ! important }
.hidden {
display: none }
.subscript {
vertical-align: sub;
font-size: smaller }
.superscript {
vertical-align: super;
font-size: smaller }
a.toc-backref {
text-decoration: none ;
color: black }
blockquote.epigraph {
margin: 2em 5em ; }
dl.docutils dd {
margin-bottom: 0.5em }
object[type="image/svg+xml"], object[type="application/x-shockwave-flash"] {
overflow: hidden;
}
/* Uncomment (and remove this text!) to get bold-faced definition list terms
dl.docutils dt {
font-weight: bold }
*/
div.abstract {
margin: 2em 5em }
div.abstract p.topic-title {
font-weight: bold ;
text-align: center }
div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
margin: 2em ;
border: medium outset ;
padding: 1em }
div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
font-weight: bold ;
font-family: sans-serif }
div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title, .code .error {
color: red ;
font-weight: bold ;
font-family: sans-serif }
/* Uncomment (and remove this text!) to get reduced vertical space in
compound paragraphs.
div.compound .compound-first, div.compound .compound-middle {
margin-bottom: 0.5em }
div.compound .compound-last, div.compound .compound-middle {
margin-top: 0.5em }
*/
div.dedication {
margin: 2em 5em ;
text-align: center ;
font-style: italic }
div.dedication p.topic-title {
font-weight: bold ;
font-style: normal }
div.figure {
margin-left: 2em ;
margin-right: 2em }
div.footer, div.header {
clear: both;
font-size: smaller }
div.line-block {
display: block ;
margin-top: 1em ;
margin-bottom: 1em }
div.line-block div.line-block {
margin-top: 0 ;
margin-bottom: 0 ;
margin-left: 1.5em }
div.sidebar {
margin: 0 0 0.5em 1em ;
border: medium outset ;
padding: 1em ;
background-color: #ffffee ;
width: 40% ;
float: right ;
clear: right }
div.sidebar p.rubric {
font-family: sans-serif ;
font-size: medium }
div.system-messages {
margin: 5em }
div.system-messages h1 {
color: red }
div.system-message {
border: medium outset ;
padding: 1em }
div.system-message p.system-message-title {
color: red ;
font-weight: bold }
div.topic {
margin: 2em }
h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
margin-top: 0.4em }
h1.title {
text-align: center }
h2.subtitle {
text-align: center }
hr.docutils {
width: 75% }
img.align-left, .figure.align-left, object.align-left, table.align-left {
clear: left ;
float: left ;
margin-right: 1em }
img.align-right, .figure.align-right, object.align-right, table.align-right {
clear: right ;
float: right ;
margin-left: 1em }
img.align-center, .figure.align-center, object.align-center {
display: block;
margin-left: auto;
margin-right: auto;
}
table.align-center {
margin-left: auto;
margin-right: auto;
}
.align-left {
text-align: left }
.align-center {
clear: both ;
text-align: center }
.align-right {
text-align: right }
/* reset inner alignment in figures */
div.align-right {
text-align: inherit }
/* div.align-center * { */
/* text-align: left } */
.align-top {
vertical-align: top }
.align-middle {
vertical-align: middle }
.align-bottom {
vertical-align: bottom }
ol.simple, ul.simple {
margin-bottom: 1em }
ol.arabic {
list-style: decimal }
ol.loweralpha {
list-style: lower-alpha }
ol.upperalpha {
list-style: upper-alpha }
ol.lowerroman {
list-style: lower-roman }
ol.upperroman {
list-style: upper-roman }
p.attribution {
text-align: right ;
margin-left: 50% }
p.caption {
font-style: italic }
p.credits {
font-style: italic ;
font-size: smaller }
p.label {
white-space: nowrap }
p.rubric {
font-weight: bold ;
font-size: larger ;
color: maroon ;
text-align: center }
p.sidebar-title {
font-family: sans-serif ;
font-weight: bold ;
font-size: larger }
p.sidebar-subtitle {
font-family: sans-serif ;
font-weight: bold }
p.topic-title {
font-weight: bold }
pre.address {
margin-bottom: 0 ;
margin-top: 0 ;
font: inherit }
pre.literal-block, pre.doctest-block, pre.math, pre.code {
margin-left: 2em ;
margin-right: 2em }
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
pre.code .literal.string, code .literal.string { color: #0C5404 }
pre.code .name.builtin, code .name.builtin { color: #352B84 }
pre.code .deleted, code .deleted { background-color: #DEB0A1}
pre.code .inserted, code .inserted { background-color: #A3D289}
span.classifier {
font-family: sans-serif ;
font-style: oblique }
span.classifier-delimiter {
font-family: sans-serif ;
font-weight: bold }
span.interpreted {
font-family: sans-serif }
span.option {
white-space: nowrap }
span.pre {
white-space: pre }
span.problematic {
color: red }
span.section-subtitle {
/* font-size relative to parent (h1..h6 element) */
font-size: 80% }
table.citation {
border-left: solid 1px gray;
margin-left: 1px }
table.docinfo {
margin: 2em 4em }
table.docutils {
margin-top: 0.5em ;
margin-bottom: 0.5em }
table.footnote {
border-left: solid 1px black;
margin-left: 1px }
table.docutils td, table.docutils th,
table.docinfo td, table.docinfo th {
padding-left: 0.5em ;
padding-right: 0.5em ;
vertical-align: top }
table.docutils th.field-name, table.docinfo th.docinfo-name {
font-weight: bold ;
text-align: left ;
white-space: nowrap ;
padding-left: 0 }
/* "booktabs" style (no vertical lines) */
table.docutils.booktabs {
border: 0px;
border-top: 2px solid;
border-bottom: 2px solid;
border-collapse: collapse;
}
table.docutils.booktabs * {
border: 0px;
}
table.docutils.booktabs th {
border-bottom: thin solid;
text-align: left;
}
h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
font-size: 100% }
ul.auto-toc {
list-style-type: none }
</style>
</head>
<body>
<div class="document" id="dbus-python-tutorial">
<h1 class="title">dbus-python tutorial</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>Simon McVittie, <a class="last reference external" href="http://www.collabora.co.uk/">Collabora Ltd.</a></td></tr>
<tr><th class="docinfo-name">Date:</th>
<td>2006-06-14</td></tr>
</tbody>
</table>
<p>This tutorial requires Python 2.4 or up, and <tt class="docutils literal"><span class="pre">dbus-python</span></tt> 0.80rc4 or up.</p>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#connecting-to-the-bus" id="id5">Connecting to the Bus</a></li>
<li><a class="reference internal" href="#making-method-calls" id="id6">Making method calls</a><ul>
<li><a class="reference internal" href="#proxy-objects" id="id7">Proxy objects</a></li>
<li><a class="reference internal" href="#interfaces-and-methods" id="id8">Interfaces and methods</a><ul>
<li><a class="reference internal" href="#see-also" id="id9">See also</a></li>
</ul>
</li>
<li><a class="reference internal" href="#data-types" id="id10">Data types</a><ul>
<li><a class="reference internal" href="#basic-types" id="id11">Basic types</a></li>
<li><a class="reference internal" href="#basic-type-conversions" id="id12">Basic type conversions</a></li>
<li><a class="reference internal" href="#container-types" id="id13">Container types</a></li>
<li><a class="reference internal" href="#return-values-and-the-byte-arrays-and-utf8-strings-options" id="id14">Return values, and the <tt class="docutils literal">byte_arrays</tt> and <tt class="docutils literal">utf8_strings</tt> options</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#making-asynchronous-method-calls" id="id15">Making asynchronous method calls</a><ul>
<li><a class="reference internal" href="#setting-up-an-event-loop" id="id16">Setting up an event loop</a><ul>
<li><a class="reference internal" href="#backwards-compatibility-dbus-glib" id="id17">Backwards compatibility: <tt class="docutils literal">dbus.glib</tt></a></li>
<li><a class="reference internal" href="#the-qt-main-loop" id="id18">The Qt main loop</a></li>
</ul>
</li>
<li><a class="reference internal" href="#making-asynchronous-calls" id="id19">Making asynchronous calls</a><ul>
<li><a class="reference internal" href="#id3" id="id20">See also</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#receiving-signals" id="id21">Receiving signals</a><ul>
<li><a class="reference internal" href="#signal-matching" id="id22">Signal matching</a></li>
<li><a class="reference internal" href="#getting-more-information-from-a-signal" id="id23">Getting more information from a signal</a></li>
<li><a class="reference internal" href="#string-argument-matching" id="id24">String argument matching</a></li>
<li><a class="reference internal" href="#receiving-signals-from-a-proxy-object" id="id25">Receiving signals from a proxy object</a></li>
<li><a class="reference internal" href="#id4" id="id26">See also</a></li>
</ul>
</li>
<li><a class="reference internal" href="#claiming-a-bus-name" id="id27">Claiming a bus name</a><ul>
<li><a class="reference internal" href="#the-unique-instance-idiom" id="id28">The unique-instance idiom</a></li>
</ul>
</li>
<li><a class="reference internal" href="#exporting-objects" id="id29">Exporting objects</a><ul>
<li><a class="reference internal" href="#inheriting-from-dbus-service-object" id="id30">Inheriting from <tt class="docutils literal">dbus.service.Object</tt></a></li>
<li><a class="reference internal" href="#exporting-methods-with-dbus-service-method" id="id31">Exporting methods with <tt class="docutils literal">dbus.service.method</tt></a><ul>
<li><a class="reference internal" href="#finding-out-the-caller-s-bus-name" id="id32">Finding out the caller's bus name</a></li>
<li><a class="reference internal" href="#asynchronous-method-implementations" id="id33">Asynchronous method implementations</a></li>
</ul>
</li>
<li><a class="reference internal" href="#emitting-signals-with-dbus-service-signal" id="id34">Emitting signals with <tt class="docutils literal">dbus.service.signal</tt></a><ul>
<li><a class="reference internal" href="#example" id="id35">Example</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#license-for-this-document" id="id36">License for this document</a></li>
</ul>
</div>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<div class="section" id="connecting-to-the-bus">
<span id="bus-objects"></span><span id="bus-object"></span><h1><a class="toc-backref" href="#id5">Connecting to the Bus</a></h1>
<p>Applications that use D-Bus typically connect to a <em>bus daemon</em>, which
forwards messages between the applications. To use D-Bus, you need to create a
<tt class="docutils literal">Bus</tt> object representing the connection to the bus daemon.</p>
<p>There are generally two bus daemons you may be interested in. Each user
login session should have a <em>session bus</em>, which is local to that
session. It's used to communicate between desktop applications. Connect
to the session bus by creating a <tt class="docutils literal">SessionBus</tt> object:</p>
<pre class="literal-block">
import dbus
session_bus = dbus.SessionBus()
</pre>
<p>The <em>system bus</em> is global and usually started during boot; it's used to
communicate with system services like <a class="reference external" href="http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev.html">udev</a>, <a class="reference external" href="http://www.gnome.org/projects/NetworkManager/">NetworkManager</a>, and the
<a class="reference external" href="http://www.freedesktop.org/wiki/Software/hal">Hardware Abstraction Layer daemon (hald)</a>. To connect to the system
bus, create a <tt class="docutils literal">SystemBus</tt> object:</p>
<pre class="literal-block">
import dbus
system_bus = dbus.SystemBus()
</pre>
<p>Of course, you can connect to both in the same application.</p>
<p>For special purposes, you might use a non-default Bus, or a connection
which isn't a Bus at all, using some new API added in dbus-python 0.81.0.
This is not described here, and will at some stage be the subject of a separate
tutorial.</p>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
</div>
<div class="section" id="making-method-calls">
<h1><a class="toc-backref" href="#id6">Making method calls</a></h1>
<p>D-Bus applications can export objects for other applications' use. To
start working with an object in another application, you need to know:</p>
<ul>
<li><p class="first">The <em>bus name</em>. This identifies which application you want to
communicate with. You'll usually identify applications by a
<em>well-known name</em>, which is a dot-separated string starting with a
reversed domain name, such as <tt class="docutils literal">org.freedesktop.NetworkManager</tt>
or <tt class="docutils literal">com.example.WordProcessor</tt>.</p>
</li>
<li><p class="first">The <em>object path</em>. Applications can export many objects - for
instance, example.com's word processor might provide an object
representing the word processor application itself and an object for
each document window opened, or it might also provide an object for
each paragraph within a document.</p>
<p>To identify which one you want to interact with, you use an object path,
a slash-separated string resembling a filename. For instance, example.com's
word processor might provide an object at <tt class="docutils literal">/</tt> representing the word
processor itself, and objects at <tt class="docutils literal">/documents/123</tt> and
<tt class="docutils literal">/documents/345</tt> representing opened document windows.</p>
</li>
</ul>
<p>As you'd expect, one of the main things you can do with remote objects
is to call their methods. As in Python, methods may have parameters,
and they may return one or more values.</p>
<div class="section" id="proxy-objects">
<span id="proxy-object"></span><h2><a class="toc-backref" href="#id7">Proxy objects</a></h2>
<p>To interact with a remote object, you use a <em>proxy object</em>. This is a
Python object which acts as a proxy or "stand-in" for the remote object -
when you call a method on a proxy object, this causes dbus-python to make
a method call on the remote object, passing back any return values from
the remote object's method as the return values of the proxy method call.</p>
<p>To obtain a proxy object, call the <tt class="docutils literal">get_object</tt> method on the <tt class="docutils literal">Bus</tt>.
For example, <a class="reference external" href="http://www.gnome.org/projects/NetworkManager/">NetworkManager</a> has the well-known name
<tt class="docutils literal">org.freedesktop.NetworkManager</tt> and exports an object whose object
path is <tt class="docutils literal">/org/freedesktop/NetworkManager</tt>, plus an object per network
interface at object paths like
<tt class="docutils literal">/org/freedesktop/NetworkManager/Devices/eth0</tt>. You can get a proxy
for the object representing eth0 like this:</p>
<pre class="literal-block">
import dbus
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.NetworkManager',
'/org/freedesktop/NetworkManager/Devices/eth0')
# proxy is a dbus.proxies.ProxyObject
</pre>
</div>
<div class="section" id="interfaces-and-methods">
<h2><a class="toc-backref" href="#id8">Interfaces and methods</a></h2>
<p>D-Bus uses <em>interfaces</em> to provide a namespacing mechanism for methods.
An interface is a group of related methods and signals (more on signals
later), identified by a name which is a series of dot-separated components
starting with a reversed domain name. For instance, each <a class="reference external" href="http://www.gnome.org/projects/NetworkManager/">NetworkManager</a>
object representing a network interface implements the interface
<tt class="docutils literal">org.freedesktop.NetworkManager.Devices</tt>, which has methods like
<tt class="docutils literal">getProperties</tt>.</p>
<p>To call a method, call the method of the same name on the proxy object,
passing in the interface name via the <tt class="docutils literal">dbus_interface</tt> keyword argument:</p>
<pre class="literal-block">
import dbus
bus = dbus.SystemBus()
eth0 = bus.get_object('org.freedesktop.NetworkManager',
'/org/freedesktop/NetworkManager/Devices/eth0')
props = eth0.getProperties(dbus_interface='org.freedesktop.NetworkManager.Devices')
# props is a tuple of properties, the first of which is the object path
</pre>
<p id="dbus-interface">As a short cut, if you're going to be calling many methods with the same
interface, you can construct a <tt class="docutils literal">dbus.Interface</tt> object and call
methods on that, without needing to specify the interface again:</p>
<pre class="literal-block">
import dbus
bus = dbus.SystemBus()
eth0 = bus.get_object('org.freedesktop.NetworkManager',
'/org/freedesktop/NetworkManager/Devices/eth0')
eth0_dev_iface = dbus.Interface(eth0,
dbus_interface='org.freedesktop.NetworkManager.Devices')
props = eth0_dev_iface.getProperties()
# props is the same as before
</pre>
<div class="section" id="see-also">
<h3><a class="toc-backref" href="#id9">See also</a></h3>
<p>See the example in <tt class="docutils literal"><span class="pre">examples/example-client.py</span></tt>. Before running it,
you'll need to run <tt class="docutils literal"><span class="pre">examples/example-service.py</span></tt> in the background or
in another shell.</p>
</div>
</div>
<div class="section" id="data-types">
<h2><a class="toc-backref" href="#id10">Data types</a></h2>
<p>Unlike Python, D-Bus is statically typed - each method has a certain
<em>signature</em> representing the types of its arguments, and will not accept
arguments of other types.</p>
<p>D-Bus has an introspection mechanism, which <tt class="docutils literal"><span class="pre">dbus-python</span></tt> tries to use
to discover the correct argument types. If this succeeds, Python types
are converted into the right D-Bus data types automatically, if possible;
<tt class="docutils literal">TypeError</tt> is raised if the type is inappropriate.</p>
<p>If the introspection mechanism fails (or the argument's type is
variant - see below), you have to provide arguments of
the correct type. <tt class="docutils literal"><span class="pre">dbus-python</span></tt> provides Python types corresponding to
the D-Bus data types, and a few native Python types are also converted to
D-Bus data types automatically. If you use a type which isn't among these,
a <tt class="docutils literal">TypeError</tt> will be raised telling you that <tt class="docutils literal"><span class="pre">dbus-python</span></tt> was
unable to guess the D-Bus signature.</p>
<div class="section" id="basic-types">
<h3><a class="toc-backref" href="#id11">Basic types</a></h3>
<p>The following basic data types are supported.</p>
<table border="1" class="docutils">
<colgroup>
<col width="33%" />
<col width="37%" />
<col width="29%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Python type</th>
<th class="head">converted to D-Bus type</th>
<th class="head">notes</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>D-Bus <a class="reference internal" href="#proxy-object">proxy object</a></td>
<td>ObjectPath (signature 'o')</td>
<td><a class="reference internal" href="#id2">(+)</a></td>
</tr>
<tr><td><a class="reference internal" href="#dbus-interface">dbus.Interface</a></td>
<td>ObjectPath (signature 'o')</td>
<td><a class="reference internal" href="#id2">(+)</a></td>
</tr>
<tr><td><a class="reference internal" href="#dbus-service-object">dbus.service.Object</a></td>
<td>ObjectPath (signature 'o')</td>
<td><a class="reference internal" href="#id2">(+)</a></td>
</tr>
<tr><td><tt class="docutils literal">dbus.Boolean</tt></td>
<td>Boolean (signature 'b')</td>
<td>a subclass of <tt class="docutils literal">int</tt></td>
</tr>
<tr><td><tt class="docutils literal">dbus.Byte</tt></td>
<td>byte (signature 'y')</td>
<td>a subclass of <tt class="docutils literal">int</tt></td>
</tr>
<tr><td><tt class="docutils literal">dbus.Int16</tt></td>
<td>16-bit signed integer ('n')</td>
<td>a subclass of <tt class="docutils literal">int</tt></td>
</tr>
<tr><td><tt class="docutils literal">dbus.Int32</tt></td>
<td>32-bit signed integer ('i')</td>
<td>a subclass of <tt class="docutils literal">int</tt></td>
</tr>
<tr><td><tt class="docutils literal">dbus.Int64</tt></td>
<td>64-bit signed integer ('x')</td>
<td><a class="reference internal" href="#id1">(*)</a></td>
</tr>
<tr><td><tt class="docutils literal">dbus.UInt16</tt></td>
<td>16-bit unsigned integer ('q')</td>
<td>a subclass of <tt class="docutils literal">int</tt></td>
</tr>
<tr><td><tt class="docutils literal">dbus.UInt32</tt></td>
<td>32-bit unsigned integer ('u')</td>
<td><cite>(*)_</cite></td>
</tr>
<tr><td><tt class="docutils literal">dbus.UInt64</tt></td>
<td>64-bit unsigned integer ('t')</td>
<td><cite>(*)_</cite></td>
</tr>
<tr><td><tt class="docutils literal">dbus.Double</tt></td>
<td>double-precision float ('d')</td>
<td>a subclass of <tt class="docutils literal">float</tt></td>
</tr>
<tr><td><tt class="docutils literal">dbus.ObjectPath</tt></td>
<td>object path ('o')</td>
<td>a subclass of <tt class="docutils literal">str</tt></td>
</tr>
<tr><td><tt class="docutils literal">dbus.Signature</tt></td>
<td>signature ('g')</td>
<td>a subclass of <tt class="docutils literal">str</tt></td>
</tr>
<tr><td><tt class="docutils literal">dbus.String</tt></td>
<td>string ('s')</td>
<td>a subclass of
<tt class="docutils literal">unicode</tt></td>
</tr>
<tr><td><tt class="docutils literal">dbus.UTF8String</tt></td>
<td>string ('s')</td>
<td>a subclass of <tt class="docutils literal">str</tt></td>
</tr>
<tr><td><tt class="docutils literal">bool</tt></td>
<td>Boolean ('b')</td>
<td> </td>
</tr>
<tr><td><tt class="docutils literal">int</tt> or subclass</td>
<td>32-bit signed integer ('i')</td>
<td> </td>
</tr>
<tr><td><tt class="docutils literal">long</tt> or subclass</td>
<td>64-bit signed integer ('x')</td>
<td> </td>
</tr>
<tr><td><tt class="docutils literal">float</tt> or subclass</td>
<td>double-precision float ('d')</td>
<td> </td>
</tr>
<tr><td><tt class="docutils literal">str</tt> or subclass</td>
<td>string ('s')</td>
<td>must be valid UTF-8</td>
</tr>
<tr><td><tt class="docutils literal">unicode</tt> or subclass</td>
<td>string ('s')</td>
<td> </td>
</tr>
</tbody>
</table>
<p id="id1">Types marked (*) may be a subclass of either <tt class="docutils literal">int</tt> or <tt class="docutils literal">long</tt>, depending
on platform.</p>
<p id="id2">(+): D-Bus proxy objects, exported D-Bus service objects and anything
else with the special attribute <tt class="docutils literal">__dbus_object_path__</tt>, which
must be a string, are converted to their object-path. This might be
useful if you're writing an object-oriented API using dbus-python.</p>
</div>
<div class="section" id="basic-type-conversions">
<h3><a class="toc-backref" href="#id12">Basic type conversions</a></h3>
<p>If introspection succeeded, <tt class="docutils literal"><span class="pre">dbus-python</span></tt> will also accept:</p>
<ul class="simple">
<li>for Boolean parameters, any object (converted as if via <tt class="docutils literal"><span class="pre">int(bool(...))</span></tt>)</li>
<li>for byte parameters, a single-character string (converted as if via <tt class="docutils literal">ord()</tt>)</li>
<li>for byte and integer parameters, any integer (must be in the correct range)</li>
<li>for object-path and signature parameters, any <tt class="docutils literal">str</tt> or <tt class="docutils literal">unicode</tt>
subclass (the value must follow the appropriate syntax)</li>
</ul>
</div>
<div class="section" id="container-types">
<h3><a class="toc-backref" href="#id13">Container types</a></h3>
<p>D-Bus supports four container types: array (a variable-length sequence of the
same type), struct (a fixed-length sequence whose members may have
different types), dictionary (a mapping from values of the same basic type to
values of the same type), and variant (a container which may hold any
D-Bus type, including another variant).</p>
<p>Arrays are represented by Python lists, or by <tt class="docutils literal">dbus.Array</tt>, a subclass
of <tt class="docutils literal">list</tt>. When sending an array, if an introspected signature is
available, that will be used; otherwise, if the <tt class="docutils literal">signature</tt> keyword
parameter was passed to the <tt class="docutils literal">Array</tt> constructor, that will be used to
determine the contents' signature; otherwise, <tt class="docutils literal"><span class="pre">dbus-python</span></tt> will guess
from the array's first item.</p>
<p>The signature of an array is 'ax' where 'x' represents the signature of
one item. For instance, you could also have 'as' (array of strings) or
'a(ii)' (array of structs each containing two 32-bit integers).</p>
<p>There's also a type <tt class="docutils literal">dbus.ByteArray</tt> which is a subclass of <tt class="docutils literal">str</tt>,
used as a more efficient representation of a D-Bus array of bytes
(signature 'ay').</p>
<p>Structs are represented by Python tuples, or by <tt class="docutils literal">dbus.Struct</tt>, a
subclass of <tt class="docutils literal">tuple</tt>. When sending a struct, if an introspected signature is
available, that will be used; otherwise, if the <tt class="docutils literal">signature</tt> keyword
parameter was passed to the <tt class="docutils literal">Array</tt> constructor, that will be used to
determine the contents' signature; otherwise, <tt class="docutils literal"><span class="pre">dbus-python</span></tt> will guess
from the array's first item.</p>
<p>The signature of a struct consists of the signatures of the contents,
in parentheses - for instance '(is)' is the signature of a struct
containing a 32-bit integer and a string.</p>
<p>Dictionaries are represented by Python dictionaries, or by
<tt class="docutils literal">dbus.Dictionary</tt>, a subclass of <tt class="docutils literal">dict</tt>. When sending a dictionary,
if an introspected signature is available, that will be used; otherwise,
if the <tt class="docutils literal">signature</tt> keyword parameter was passed to the <tt class="docutils literal">Dictionary</tt>
constructor, that will be used to determine the contents' key and value
signatures; otherwise, <tt class="docutils literal"><span class="pre">dbus-python</span></tt> will guess from an arbitrary item
of the <tt class="docutils literal">dict</tt>.</p>
<p>The signature of a dictionary is 'a{xy}' where 'x' represents the
signature of the keys (which may not be a container type) and 'y'
represents the signature of the values. For instance,
'a{s(ii)}' is a dictionary where the keys are strings and the values are
structs containing two 32-bit integers.</p>
<p>Variants are represented by setting the <tt class="docutils literal">variant_level</tt> keyword
argument in the constructor of any D-Bus data type to a value greater
than 0 (<tt class="docutils literal">variant_level</tt> 1 means a variant containing some other data type,
<tt class="docutils literal">variant_level</tt> 2 means a variant containing a variant containing some
other data type, and so on). If a non-variant is passed as an argument
but introspection indicates that a variant is expected, it'll
automatically be wrapped in a variant.</p>
<p>The signature of a variant is 'v'.</p>
</div>
<div class="section" id="return-values-and-the-byte-arrays-and-utf8-strings-options">
<span id="byte-arrays-and-utf8-strings"></span><h3><a class="toc-backref" href="#id14">Return values, and the <tt class="docutils literal">byte_arrays</tt> and <tt class="docutils literal">utf8_strings</tt> options</a></h3>
<p>If a D-Bus method returns no value, the Python proxy method will return
<tt class="docutils literal">None</tt>.</p>
<p>If a D-Bus method returns one value, the Python proxy method will return
that value as one of the <tt class="docutils literal">dbus.</tt> types - by default, strings are
returned as <tt class="docutils literal">dbus.String</tt> (a subclass of Unicode) and byte arrays are
returned as a <tt class="docutils literal">dbus.Array</tt> of <tt class="docutils literal">dbus.Byte</tt>.</p>
<p>If a D-Bus method returns multiple values, the Python proxy method
will return a tuple containing those values.</p>
<p>If you want strings returned as <tt class="docutils literal">dbus.UTF8String</tt> (a subclass of
<tt class="docutils literal">str</tt>) pass the keyword parameter <tt class="docutils literal">utf8_strings=True</tt> to the proxy
method.</p>
<p>If you want byte arrays returned as <tt class="docutils literal">dbus.ByteArray</tt> (also a
subclass of <tt class="docutils literal">str</tt> - in practice, this is often what you want) pass
the keyword parameter <tt class="docutils literal">byte_arrays=True</tt> to the proxy method.</p>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
</div>
</div>
</div>
<div class="section" id="making-asynchronous-method-calls">
<h1><a class="toc-backref" href="#id15">Making asynchronous method calls</a></h1>
<p>Asynchronous (non-blocking) method calls allow multiple method calls to
be in progress simultaneously, and allow your application to do other
work while it's waiting for the results. To make asynchronous calls,
you first need an event loop or "main loop".</p>
<div class="section" id="setting-up-an-event-loop">
<h2><a class="toc-backref" href="#id16">Setting up an event loop</a></h2>
<p>Currently, the only main loop supported by <tt class="docutils literal"><span class="pre">dbus-python</span></tt> is GLib.</p>
<p><tt class="docutils literal"><span class="pre">dbus-python</span></tt> has a global default main loop, which is the easiest way
to use this functionality. To arrange for the GLib main loop to be the
default, use:</p>
<pre class="literal-block">
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
</pre>
<p>You must do this before <a class="reference internal" href="#connecting-to-the-bus">connecting to the bus</a>.</p>
<p>Actually starting the main loop is as usual for <tt class="docutils literal">pygi</tt>:</p>
<pre class="literal-block">
from gi.repository import GLib
loop = GLib.MainLoop()
loop.run()
</pre>
<p>While <tt class="docutils literal">loop.run()</tt> is executing, GLib will run your callbacks when
appropriate. To stop, call <tt class="docutils literal">loop.quit()</tt>.</p>
<p>You can also set a main loop on a per-connection basis, by passing a
main loop to the Bus constructor:</p>
<pre class="literal-block">
import dbus
from dbus.mainloop.glib import DBusGMainLoop
dbus_loop = DBusGMainLoop()
bus = dbus.SessionBus(mainloop=dbus_loop)
</pre>
<p>This isn't very useful until we support more than one main loop, though.</p>
<div class="section" id="backwards-compatibility-dbus-glib">
<h3><a class="toc-backref" href="#id17">Backwards compatibility: <tt class="docutils literal">dbus.glib</tt></a></h3>
<p>In versions of <tt class="docutils literal"><span class="pre">dbus-python</span></tt> prior to 0.80, the way to set GLib as the
default main loop was:</p>
<pre class="literal-block">
import dbus.glib
</pre>
<p>Executing that import statement would automatically load the GLib main
loop and make this the default. This is now deprecated, since it's
highly non-obvious, but may be useful if you want to write or understand
backwards-compatible code.</p>
</div>
<div class="section" id="the-qt-main-loop">
<h3><a class="toc-backref" href="#id18">The Qt main loop</a></h3>
<p>PyQt v4.2 and later includes support for integrating dbus-python with
the Qt event loop. To connect D-Bus to this main loop, call
<tt class="docutils literal">dbus.mainloop.qt.DBusQtMainLoop</tt> instead of
<tt class="docutils literal">dbus.mainloop.glib.DBusGMainLoop</tt>. Otherwise the Qt loop is used in
exactly the same way as the GLib loop.</p>
</div>
</div>
<div class="section" id="making-asynchronous-calls">
<h2><a class="toc-backref" href="#id19">Making asynchronous calls</a></h2>
<p>To make a call asynchronous, pass two callables as keyword arguments
<tt class="docutils literal">reply_handler</tt> and <tt class="docutils literal">error_handler</tt> to the proxy method. The proxy
method will immediately return <cite>None</cite>. At some later time, when the event
loop is running, one of these will happen: either</p>
<ul class="simple">
<li>the <tt class="docutils literal">reply_handler</tt> will be called with the method's return values
as arguments; or</li>
<li>the <tt class="docutils literal">error_handler</tt> will be called with one argument, an instance of
<tt class="docutils literal">DBusException</tt> representing a remote exception.</li>
</ul>
<div class="section" id="id3">
<h3><a class="toc-backref" href="#id20">See also</a></h3>
<p><tt class="docutils literal"><span class="pre">examples/example-async-client.py</span></tt> makes asynchronous method calls to
the service provided by <tt class="docutils literal"><span class="pre">examples/example-service.py</span></tt> which return
either a value or an exception. As for <tt class="docutils literal"><span class="pre">examples/example-client.py</span></tt>,
you need to run <tt class="docutils literal"><span class="pre">examples/example-service.py</span></tt> in the background or
in another shell first.</p>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
</div>
</div>
</div>
<div class="section" id="receiving-signals">
<h1><a class="toc-backref" href="#id21">Receiving signals</a></h1>
<p>To receive signals, the Bus needs to be connected to an event loop - see
section <a class="reference internal" href="#setting-up-an-event-loop">Setting up an event loop</a>. Signals will only be received while
the event loop is running.</p>
<div class="section" id="signal-matching">
<h2><a class="toc-backref" href="#id22">Signal matching</a></h2>
<p>To respond to signals, you can use the <tt class="docutils literal">add_signal_receiver</tt> method on
<a class="reference internal" href="#bus-objects">Bus objects</a>. This arranges for a callback to be called when a
matching signal is received, and has the following arguments:</p>
<ul class="simple">
<li>a callable (the <tt class="docutils literal">handler_function</tt>) which will be called by the event loop
when the signal is received - its parameters will be the arguments of
the signal</li>
<li>the signal name, <tt class="docutils literal">signal_name</tt>: here None (the default) matches all names</li>
<li>the D-Bus interface, <tt class="docutils literal">dbus_interface</tt>: again None is the default,
and matches all interfaces</li>
<li>a sender bus name (well-known or unique), <tt class="docutils literal">bus_name</tt>: None is again
the default, and matches all senders. Well-known names match signals
from whatever application is currently the primary owner of that
well-known name.</li>
<li>a sender object path, <tt class="docutils literal">path</tt>: once again None is the default and
matches all object paths</li>
</ul>
<p><tt class="docutils literal">add_signal_receiver</tt> also has keyword arguments <tt class="docutils literal">utf8_strings</tt> and
<tt class="docutils literal">byte_arrays</tt> which influence the types used when calling the
handler function, in the same way as the <a class="reference internal" href="#byte-arrays-and-utf8-strings">byte_arrays and utf8_strings</a>
options on proxy methods.</p>
<p><tt class="docutils literal">add_signal_receiver</tt> returns a <tt class="docutils literal">SignalMatch</tt> object. Its only
useful public API at the moment is a <tt class="docutils literal">remove</tt> method with no
arguments, which removes the signal match from the connection.</p>
</div>
<div class="section" id="getting-more-information-from-a-signal">
<h2><a class="toc-backref" href="#id23">Getting more information from a signal</a></h2>
<p>You can also arrange for more information to be passed to the handler
function. If you pass the keyword arguments <tt class="docutils literal">sender_keyword</tt>,
<tt class="docutils literal">destination_keyword</tt>, <tt class="docutils literal">interface_keyword</tt>, <tt class="docutils literal">member_keyword</tt> or
<tt class="docutils literal">path_keyword</tt> to the <tt class="docutils literal">connect_to_signal</tt> method, the appropriate
part of the signal message will be passed to the handler function as a
keyword argument: for instance if you use</p>
<pre class="literal-block">
def handler(sender=None):
print "got signal from %r" % sender
iface.connect_to_signal("Hello", handler, sender_keyword='sender')
</pre>
<p>and a signal <tt class="docutils literal">Hello</tt> with no arguments is received from
<tt class="docutils literal">com.example.Foo</tt>, the <tt class="docutils literal">handler</tt> function will be called with
<tt class="docutils literal"><span class="pre">sender='com.example.Foo'</span></tt>.</p>
</div>
<div class="section" id="string-argument-matching">
<h2><a class="toc-backref" href="#id24">String argument matching</a></h2>
<p>If there are keyword parameters for the form <tt class="docutils literal">arg</tt><em>n</em> where n is a
small non-negative number, their values must be <tt class="docutils literal">unicode</tt> objects
or UTF-8 strings. The handler will only be called if that argument
of the signal (numbered from zero) is a D-Bus string (in particular,
not an object-path or a signature) with that value.</p>
<!-- *this comment is to stop the above breaking vim syntax highlighting* -->
</div>
<div class="section" id="receiving-signals-from-a-proxy-object">
<h2><a class="toc-backref" href="#id25">Receiving signals from a proxy object</a></h2>
<p><a class="reference internal" href="#proxy-objects">Proxy objects</a> have a special method <tt class="docutils literal">connect_to_signal</tt> which
arranges for a callback to be called when a signal is received
from the corresponding remote object. The parameters are:</p>
<ul class="simple">
<li>the name of the signal</li>
<li>a callable (the handler function) which will be called by the event loop
when the signal is received - its parameters will be the arguments of
the signal</li>
<li>the handler function, a callable: the same as for <tt class="docutils literal">add_signal_receiver</tt></li>
<li>the keyword argument <tt class="docutils literal">dbus_interface</tt> qualifies the name with its
interface</li>
</ul>
<p><cite>dbus.Interface</cite> objects have a similar <tt class="docutils literal">connect_to_signal</tt> method,
but in this case you don't need the <tt class="docutils literal">dbus_interface</tt> keyword argument
since the interface to use is already known.</p>
<p>The same extra keyword arguments as for <tt class="docutils literal">add_signal_receiver</tt> are also
available, and just like <tt class="docutils literal">add_signal_receiver</tt>, it returns a
SignalMatch.</p>
<p>You shouldn't use proxy objects just to listen to signals, since they
might activate the relevant service when created, but if you already have a
proxy object in order to call methods, it's often convenient to use it to add
signal matches too.</p>
</div>
<div class="section" id="id4">
<h2><a class="toc-backref" href="#id26">See also</a></h2>
<p><tt class="docutils literal"><span class="pre">examples/signal-recipient.py</span></tt> receives signals - it demonstrates
general signal matching as well as <tt class="docutils literal">connect_to_signal</tt>. Before running it,
you'll need to run <tt class="docutils literal"><span class="pre">examples/signal-emitter.py</span></tt> in the background or
in another shell.</p>
<span class="target" id="busname"></span><!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
</div>
</div>
<div class="section" id="claiming-a-bus-name">
<h1><a class="toc-backref" href="#id27">Claiming a bus name</a></h1>
<p>FIXME describe <a class="reference internal" href="#busname">BusName</a> - perhaps fix its API first?</p>
<div class="section" id="the-unique-instance-idiom">
<h2><a class="toc-backref" href="#id28">The unique-instance idiom</a></h2>
<p>FIXME provide exemplary code, put it in examples</p>
<span class="target" id="exported-objects"><span id="exported-object"></span></span><!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
</div>
</div>
<div class="section" id="exporting-objects">
<h1><a class="toc-backref" href="#id29">Exporting objects</a></h1>
<p>Objects made available to other applications over D-Bus are said to be
<em>exported</em>. All subclasses of <tt class="docutils literal">dbus.service.Object</tt> are automatically
exported.</p>
<p>To export objects, the Bus needs to be connected to an event loop - see
section <a class="reference internal" href="#setting-up-an-event-loop">Setting up an event loop</a>. Exported methods will only be called,
and queued signals will only be sent, while the event loop is running.</p>
<div class="section" id="inheriting-from-dbus-service-object">
<span id="dbus-service-object"></span><h2><a class="toc-backref" href="#id30">Inheriting from <tt class="docutils literal">dbus.service.Object</tt></a></h2>
<p>To export an object onto the Bus, just subclass
<tt class="docutils literal">dbus.service.Object</tt>. Object expects either a <a class="reference internal" href="#busname">BusName</a> or a <a class="reference internal" href="#bus-object">Bus
object</a>, and an object-path, to be passed to its constructor: arrange
for this information to be available. For example:</p>
<pre class="literal-block">
class Example(dbus.service.Object):
def __init__(self, object_path):
dbus.service.Object.__init__(self, dbus.SessionBus(), path)
</pre>
<p>This object will automatically support introspection, but won't do
anything particularly interesting. To fix that, you'll need to export some
methods and signals too.</p>
<p>FIXME also mention dbus.gobject.ExportedGObject once I've written it</p>
</div>
<div class="section" id="exporting-methods-with-dbus-service-method">
<h2><a class="toc-backref" href="#id31">Exporting methods with <tt class="docutils literal">dbus.service.method</tt></a></h2>
<p>To export a method, use the decorator <tt class="docutils literal">dbus.service.method</tt>. For
example:</p>
<pre class="literal-block">
class Example(dbus.service.Object):
def __init__(self, object_path):
dbus.service.Object.__init__(self, dbus.SessionBus(), path)
@dbus.service.method(dbus_interface='com.example.Sample',
in_signature='v', out_signature='s')
def StringifyVariant(self, variant):
return str(variant)
</pre>
<p>The <tt class="docutils literal">in_signature</tt> and <tt class="docutils literal">out_signature</tt> are D-Bus signature strings
as described in <a class="reference internal" href="#data-types">Data Types</a>.</p>
<p>As well as the keywords shown, you can pass <tt class="docutils literal">utf8_strings</tt> and
<tt class="docutils literal">byte_arrays</tt> keyword arguments, which influence the types which will
be passed to the decorated method when it's called via D-Bus, in the
same way that the <a class="reference internal" href="#byte-arrays-and-utf8-strings">byte_arrays and utf8_strings</a> options affect the
return value of a proxy method.</p>
<p>You can find a simple example in <tt class="docutils literal"><span class="pre">examples/example-service.py</span></tt>, which
we used earlier to demonstrate <tt class="docutils literal"><span class="pre">examples/example-client.py</span></tt>.</p>
<div class="section" id="finding-out-the-caller-s-bus-name">
<h3><a class="toc-backref" href="#id32">Finding out the caller's bus name</a></h3>
<p>The <tt class="docutils literal">method</tt> decorator accepts a <tt class="docutils literal">sender_keyword</tt> keyword argument.
If you set that to a string, the unique bus name of the sender will be
passed to the decorated method as a keyword argument of that name:</p>
<pre class="literal-block">
class Example(dbus.service.Object):
def __init__(self, object_path):
dbus.service.Object.__init__(self, dbus.SessionBus(), path)
@dbus.service.method(dbus_interface='com.example.Sample',
in_signature='', out_signature='s',
sender_keyword='sender')
def SayHello(self, sender=None):
return 'Hello, %s!' % sender
# -> something like 'Hello, :1.1!'
</pre>
</div>
<div class="section" id="asynchronous-method-implementations">
<h3><a class="toc-backref" href="#id33">Asynchronous method implementations</a></h3>
<p>FIXME and also add an example, perhaps examples/example-async-service.py</p>
</div>
</div>
<div class="section" id="emitting-signals-with-dbus-service-signal">
<h2><a class="toc-backref" href="#id34">Emitting signals with <tt class="docutils literal">dbus.service.signal</tt></a></h2>
<p>To export a signal, use the decorator <tt class="docutils literal">dbus.service.signal</tt>; to emit
that signal, call the decorated method. The decorated method can also
contain code which will be run when called, as usual. For example:</p>
<pre class="literal-block">
class Example(dbus.service.Object):
def __init__(self, object_path):
dbus.service.Object.__init__(self, dbus.SessionBus(), path)
@dbus.service.signal(dbus_interface='com.example.Sample',
signature='us')
def NumberOfBottlesChanged(self, number, contents):
print "%d bottles of %s on the wall" % (number, contents)
e = Example('/bottle-counter')
e.NumberOfBottlesChanged(100, 'beer')
# -> emits com.example.Sample.NumberOfBottlesChanged(100, 'beer')
# and prints "100 bottles of beer on the wall"
</pre>
<p>The signal will be queued for sending when the decorated method returns -
you can prevent the signal from being sent by raising an exception
from the decorated method (for instance, if the parameters are
inappropriate). The signal will only actually be sent when the event loop
next runs.</p>
<div class="section" id="example">
<h3><a class="toc-backref" href="#id35">Example</a></h3>
<p><tt class="docutils literal"><span class="pre">examples/example-signal-emitter.py</span></tt> emits some signals on demand when
one of its methods is called. (In reality, you'd emit a signal when some
sort of internal state changed, which may or may not be triggered by a
D-Bus method call.)</p>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
</div>
</div>
</div>
<div class="section" id="license-for-this-document">
<h1><a class="toc-backref" href="#id36">License for this document</a></h1>
<p>Copyright 2006-2007 <a class="reference external" href="http://www.collabora.co.uk/">Collabora Ltd.</a></p>
<p>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:</p>
<p>The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.</p>
<p>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 AUTHORS OR COPYRIGHT
HOLDERS 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.</p>
<!-- vim:set ft=rst sw=4 sts=4 et tw=72: -->
</div>
</div>
</body>
</html>
|