diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile.am | 8 | ||||
-rw-r--r-- | examples/Makefile.in | 393 | ||||
-rw-r--r-- | examples/example-async-client.py | 132 | ||||
-rw-r--r-- | examples/example-client.py | 78 | ||||
-rw-r--r-- | examples/example-service.py | 83 | ||||
-rw-r--r-- | examples/example-signal-emitter.py | 69 | ||||
-rw-r--r-- | examples/example-signal-recipient.py | 99 | ||||
-rw-r--r-- | examples/list-system-services.py | 69 |
8 files changed, 931 insertions, 0 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am new file mode 100644 index 0000000..316b4bb --- /dev/null +++ b/examples/Makefile.am @@ -0,0 +1,8 @@ +EXTRA_DIST = \ + example-async-client.py \ + example-client.py \ + example-service.py \ + example-signal-emitter.py \ + example-signal-recipient.py \ + list-system-services.py +# miss out the gconf examples for now - they don't work diff --git a/examples/Makefile.in b/examples/Makefile.in new file mode 100644 index 0000000..2623d52 --- /dev/null +++ b/examples/Makefile.in @@ -0,0 +1,393 @@ +# Makefile.in generated by automake 1.11.1 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, +# 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, +# Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ +VPATH = @srcdir@ +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = @build@ +host_triplet = @host@ +subdir = examples +DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/am-check-pymod.m4 \ + $(top_srcdir)/m4/am-check-python-headers.m4 \ + $(top_srcdir)/m4/dbus-py-add-rst2htmlflag.m4 \ + $(top_srcdir)/m4/jh-add-cflag.m4 $(top_srcdir)/m4/libtool.m4 \ + $(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \ + $(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \ + $(top_srcdir)/m4/tp-compiler-flag.m4 \ + $(top_srcdir)/m4/tp-compiler-warnings.m4 \ + $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +AM_V_GEN = $(am__v_GEN_$(V)) +am__v_GEN_ = $(am__v_GEN_$(AM_DEFAULT_VERBOSITY)) +am__v_GEN_0 = @echo " GEN " $@; +AM_V_at = $(am__v_at_$(V)) +am__v_at_ = $(am__v_at_$(AM_DEFAULT_VERBOSITY)) +am__v_at_0 = @ +SOURCES = +DIST_SOURCES = +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +AMTAR = @AMTAR@ +AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ +AR = @AR@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +CC = @CC@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DBUS_CFLAGS = @DBUS_CFLAGS@ +DBUS_GLIB_CFLAGS = @DBUS_GLIB_CFLAGS@ +DBUS_GLIB_LIBS = @DBUS_GLIB_LIBS@ +DBUS_LIBS = @DBUS_LIBS@ +DBUS_PYTHON_MAJOR_VERSION = @DBUS_PYTHON_MAJOR_VERSION@ +DBUS_PYTHON_MICRO_VERSION = @DBUS_PYTHON_MICRO_VERSION@ +DBUS_PYTHON_MINOR_VERSION = @DBUS_PYTHON_MINOR_VERSION@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +EPYDOC = @EPYDOC@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +GREP = @GREP@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +LD = @LD@ +LDFLAGS = @LDFLAGS@ +LIBOBJS = @LIBOBJS@ +LIBS = @LIBS@ +LIBTOOL = @LIBTOOL@ +LIPO = @LIPO@ +LN_S = @LN_S@ +LTLIBOBJS = @LTLIBOBJS@ +MAKEINFO = @MAKEINFO@ +MKDIR_P = @MKDIR_P@ +NM = @NM@ +NMEDIT = @NMEDIT@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +OTOOL = @OTOOL@ +OTOOL64 = @OTOOL64@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_URL = @PACKAGE_URL@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +PKG_CONFIG = @PKG_CONFIG@ +PLATFORM = @PLATFORM@ +PYTHON = @PYTHON@ +PYTHON_EXEC_PREFIX = @PYTHON_EXEC_PREFIX@ +PYTHON_INCLUDES = @PYTHON_INCLUDES@ +PYTHON_PLATFORM = @PYTHON_PLATFORM@ +PYTHON_PREFIX = @PYTHON_PREFIX@ +PYTHON_VERSION = @PYTHON_VERSION@ +RANLIB = @RANLIB@ +RST2HTML = @RST2HTML@ +RST2HTMLFLAGS = @RST2HTMLFLAGS@ +SED = @SED@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +STRIP = @STRIP@ +VERSION = @VERSION@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_CC = @ac_ct_CC@ +ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build = @build@ +build_alias = @build_alias@ +build_cpu = @build_cpu@ +build_os = @build_os@ +build_vendor = @build_vendor@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host = @host@ +host_alias = @host_alias@ +host_cpu = @host_cpu@ +host_os = @host_os@ +host_vendor = @host_vendor@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localedir = @localedir@ +localstatedir = @localstatedir@ +lt_ECHO = @lt_ECHO@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +pkgpyexecdir = @pkgpyexecdir@ +pkgpythondir = @pkgpythondir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +pyexecdir = @pyexecdir@ +pythondir = @pythondir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +target_alias = @target_alias@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +EXTRA_DIST = \ + example-async-client.py \ + example-client.py \ + example-service.py \ + example-signal-emitter.py \ + example-signal-recipient.py \ + list-system-services.py + +all: all-am + +.SUFFIXES: +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu examples/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --gnu examples/Makefile +.PRECIOUS: Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs +tags: TAGS +TAGS: + +ctags: CTAGS +CTAGS: + + +distdir: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile +installdirs: +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + `test -z '$(STRIP)' || \ + echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-generic clean-libtool mostlyclean-am + +distclean: distclean-am + -rm -f Makefile +distclean-am: clean-am distclean-generic + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-generic mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: + +.MAKE: install-am install-strip + +.PHONY: all all-am check check-am clean clean-generic clean-libtool \ + distclean distclean-generic distclean-libtool distdir dvi \ + dvi-am html html-am info info-am install install-am \ + install-data install-data-am install-dvi install-dvi-am \ + install-exec install-exec-am install-html install-html-am \ + install-info install-info-am install-man install-pdf \ + install-pdf-am install-ps install-ps-am install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-generic \ + mostlyclean-libtool pdf pdf-am ps ps-am uninstall uninstall-am + +# miss out the gconf examples for now - they don't work + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/examples/example-async-client.py b/examples/example-async-client.py new file mode 100644 index 0000000..7a8214a --- /dev/null +++ b/examples/example-async-client.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python + +usage = """Usage: +python example-service.py & +python example-async-client.py +python example-client.py --exit-service +""" + +# Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> +# Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/> +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation +# files (the "Software"), to deal in the Software without +# restriction, including without limitation the rights to use, copy, +# modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE 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. + +import sys +import traceback + +import gobject + +import dbus +import dbus.mainloop.glib + +# Callbacks for asynchronous calls + +def handle_hello_reply(r): + global hello_replied + hello_replied = True + + print str(r) + + if hello_replied and raise_replied: + loop.quit() + +def handle_hello_error(e): + global failed + global hello_replied + hello_replied = True + failed = True + + print "HelloWorld raised an exception! That's not meant to happen..." + print "\t", str(e) + + if hello_replied and raise_replied: + loop.quit() + +def handle_hello_error(e): + global failed + global hello_replied + hello_replied = True + failed = True + + print "HelloWorld raised an exception! That's not meant to happen..." + print "\t", str(e) + + if hello_replied and raise_replied: + loop.quit() + +def handle_raise_reply(): + global failed + global raise_replied + raise_replied = True + failed = True + + print "RaiseException returned normally! That's not meant to happen..." + + if hello_replied and raise_replied: + loop.quit() + +def handle_raise_error(e): + global raise_replied + raise_replied = True + + print "RaiseException raised an exception as expected:" + print "\t", str(e) + + if hello_replied and raise_replied: + loop.quit() + +def make_calls(): + # To make an async call, use the reply_handler and error_handler kwargs + remote_object.HelloWorld("Hello from example-async-client.py!", + dbus_interface='com.example.SampleInterface', + reply_handler=handle_hello_reply, + error_handler=handle_hello_error) + + # Interface objects also support async calls + iface = dbus.Interface(remote_object, 'com.example.SampleInterface') + + iface.RaiseException(reply_handler=handle_raise_reply, + error_handler=handle_raise_error) + + return False + +if __name__ == '__main__': + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + + bus = dbus.SessionBus() + try: + remote_object = bus.get_object("com.example.SampleService","/SomeObject") + except dbus.DBusException: + traceback.print_exc() + print usage + sys.exit(1) + + # Make the method call after a short delay + gobject.timeout_add(1000, make_calls) + + failed = False + hello_replied = False + raise_replied = False + + loop = gobject.MainLoop() + loop.run() + if failed: + raise SystemExit("Example async client failed!") diff --git a/examples/example-client.py b/examples/example-client.py new file mode 100644 index 0000000..796f262 --- /dev/null +++ b/examples/example-client.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python + +usage = """Usage: +python example-service.py & +python example-client.py +python example-client.py --exit-service +""" + +# Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> +# Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/> +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation +# files (the "Software"), to deal in the Software without +# restriction, including without limitation the rights to use, copy, +# modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE 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. + +import sys +from traceback import print_exc + +import dbus + +def main(): + bus = dbus.SessionBus() + + try: + remote_object = bus.get_object("com.example.SampleService", + "/SomeObject") + + # you can either specify the dbus_interface in each call... + hello_reply_list = remote_object.HelloWorld("Hello from example-client.py!", + dbus_interface = "com.example.SampleInterface") + except dbus.DBusException: + print_exc() + print usage + sys.exit(1) + + print (hello_reply_list) + + # ... or create an Interface wrapper for the remote object + iface = dbus.Interface(remote_object, "com.example.SampleInterface") + + hello_reply_tuple = iface.GetTuple() + + print hello_reply_tuple + + hello_reply_dict = iface.GetDict() + + print hello_reply_dict + + # D-Bus exceptions are mapped to Python exceptions + try: + iface.RaiseException() + except dbus.DBusException, e: + print str(e) + + # introspection is automatically supported + print remote_object.Introspect(dbus_interface="org.freedesktop.DBus.Introspectable") + + if sys.argv[1:] == ['--exit-service']: + iface.Exit() + +if __name__ == '__main__': + main() diff --git a/examples/example-service.py b/examples/example-service.py new file mode 100644 index 0000000..c42b526 --- /dev/null +++ b/examples/example-service.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python + +usage = """Usage: +python example-service.py & +python example-client.py +python example-async-client.py +python example-client.py --exit-service +""" + +# Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> +# Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/> +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation +# files (the "Software"), to deal in the Software without +# restriction, including without limitation the rights to use, copy, +# modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE 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. + +import gobject + +import dbus +import dbus.service +import dbus.mainloop.glib + +class DemoException(dbus.DBusException): + _dbus_error_name = 'com.example.DemoException' + +class SomeObject(dbus.service.Object): + + @dbus.service.method("com.example.SampleInterface", + in_signature='s', out_signature='as') + def HelloWorld(self, hello_message): + print (str(hello_message)) + return ["Hello", " from example-service.py", "with unique name", + session_bus.get_unique_name()] + + @dbus.service.method("com.example.SampleInterface", + in_signature='', out_signature='') + def RaiseException(self): + raise DemoException('The RaiseException method does what you might ' + 'expect') + + @dbus.service.method("com.example.SampleInterface", + in_signature='', out_signature='(ss)') + def GetTuple(self): + return ("Hello Tuple", " from example-service.py") + + @dbus.service.method("com.example.SampleInterface", + in_signature='', out_signature='a{ss}') + def GetDict(self): + return {"first": "Hello Dict", "second": " from example-service.py"} + + @dbus.service.method("com.example.SampleInterface", + in_signature='', out_signature='') + def Exit(self): + mainloop.quit() + + +if __name__ == '__main__': + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + + session_bus = dbus.SessionBus() + name = dbus.service.BusName("com.example.SampleService", session_bus) + object = SomeObject(session_bus, '/SomeObject') + + mainloop = gobject.MainLoop() + print "Running example service." + print usage + mainloop.run() diff --git a/examples/example-signal-emitter.py b/examples/example-signal-emitter.py new file mode 100644 index 0000000..bb832aa --- /dev/null +++ b/examples/example-signal-emitter.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python + +usage = """Usage: +python example-signal-emitter.py & +python example-signal-recipient.py +python example-signal-recipient.py --exit-service +""" + +# Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> +# Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/> +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation +# files (the "Software"), to deal in the Software without +# restriction, including without limitation the rights to use, copy, +# modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE 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. + +import gobject + +import dbus +import dbus.service +import dbus.mainloop.glib + +class TestObject(dbus.service.Object): + def __init__(self, conn, object_path='/com/example/TestService/object'): + dbus.service.Object.__init__(self, conn, object_path) + + @dbus.service.signal('com.example.TestService') + def HelloSignal(self, message): + # The signal is emitted when this method exits + # You can have code here if you wish + pass + + @dbus.service.method('com.example.TestService') + def emitHelloSignal(self): + #you emit signals by calling the signal's skeleton method + self.HelloSignal('Hello') + return 'Signal emitted' + + @dbus.service.method("com.example.TestService", + in_signature='', out_signature='') + def Exit(self): + loop.quit() + +if __name__ == '__main__': + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + + session_bus = dbus.SessionBus() + name = dbus.service.BusName('com.example.TestService', session_bus) + object = TestObject(session_bus) + + loop = gobject.MainLoop() + print "Running example signal emitter service." + print usage + loop.run() diff --git a/examples/example-signal-recipient.py b/examples/example-signal-recipient.py new file mode 100644 index 0000000..4810f5e --- /dev/null +++ b/examples/example-signal-recipient.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python + +usage = """Usage: +python example-signal-emitter.py & +python example-signal-recipient.py +python example-signal-recipient.py --exit-service +""" + +# Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> +# Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/> +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation +# files (the "Software"), to deal in the Software without +# restriction, including without limitation the rights to use, copy, +# modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE 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. + +import sys +import traceback + +import gobject + +import dbus +import dbus.mainloop.glib + +def handle_reply(msg): + print msg + +def handle_error(e): + print str(e) + +def emit_signal(): + #call the emitHelloSignal method + object.emitHelloSignal(dbus_interface="com.example.TestService") + #reply_handler = handle_reply, error_handler = handle_error) + # exit after waiting a short time for the signal + gobject.timeout_add(2000, loop.quit) + + if sys.argv[1:] == ['--exit-service']: + object.Exit(dbus_interface='com.example.TestService') + + return False + +def hello_signal_handler(hello_string): + print ("Received signal (by connecting using remote object) and it says: " + + hello_string) + +def catchall_signal_handler(*args, **kwargs): + print ("Caught signal (in catchall handler) " + + kwargs['dbus_interface'] + "." + kwargs['member']) + for arg in args: + print " " + str(arg) + +def catchall_hello_signals_handler(hello_string): + print "Received a hello signal and it says " + hello_string + +def catchall_testservice_interface_handler(hello_string, dbus_message): + print "com.example.TestService interface says " + hello_string + " when it sent signal " + dbus_message.get_member() + + +if __name__ == '__main__': + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + + bus = dbus.SessionBus() + try: + object = bus.get_object("com.example.TestService","/com/example/TestService/object") + + object.connect_to_signal("HelloSignal", hello_signal_handler, dbus_interface="com.example.TestService", arg0="Hello") + except dbus.DBusException: + traceback.print_exc() + print usage + sys.exit(1) + + #lets make a catchall + bus.add_signal_receiver(catchall_signal_handler, interface_keyword='dbus_interface', member_keyword='member') + + bus.add_signal_receiver(catchall_hello_signals_handler, dbus_interface = "com.example.TestService", signal_name = "HelloSignal") + + bus.add_signal_receiver(catchall_testservice_interface_handler, dbus_interface = "com.example.TestService", message_keyword='dbus_message') + + # Tell the remote object to emit the signal after a short delay + gobject.timeout_add(2000, emit_signal) + + loop = gobject.MainLoop() + loop.run() diff --git a/examples/list-system-services.py b/examples/list-system-services.py new file mode 100644 index 0000000..a8a1829 --- /dev/null +++ b/examples/list-system-services.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python + +"""Usage: python list-system-services.py [--session|--system] +List services on the system bus (default) or the session bus.""" + +# Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> +# Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/> +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation +# files (the "Software"), to deal in the Software without +# restriction, including without limitation the rights to use, copy, +# modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE 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. + +import sys + +import dbus + +def main(argv): + factory = dbus.SystemBus + + if len(argv) > 2: + sys.exit(__doc__) + elif len(argv) == 2: + if argv[1] == '--session': + factory = dbus.SessionBus + elif argv[1] != '--system': + sys.exit(__doc__) + + # Get a connection to the system or session bus as appropriate + # We're only using blocking calls, so don't actually need a main loop here + bus = factory() + + # This could be done by calling bus.list_names(), but here's + # more or less what that means: + + # Get a reference to the desktop bus' standard object, denoted + # by the path /org/freedesktop/DBus. + dbus_object = bus.get_object('org.freedesktop.DBus', + '/org/freedesktop/DBus') + + # The object /org/freedesktop/DBus + # implements the 'org.freedesktop.DBus' interface + dbus_iface = dbus.Interface(dbus_object, 'org.freedesktop.DBus') + + # One of the member functions in the org.freedesktop.DBus interface + # is ListNames(), which provides a list of all the other services + # registered on this bus. Call it, and print the list. + services = dbus_iface.ListNames() + services.sort() + for service in services: + print service + +if __name__ == '__main__': + main(sys.argv) |