License examples under MIT/X11.
[dbus-python-phuang.git] / examples / list-system-services.py
bloba8a18295abf276e0038ac8449e522f0b4e5f283f
1 #!/usr/bin/env python
3 """Usage: python list-system-services.py [--session|--system]
4 List services on the system bus (default) or the session bus."""
6 # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/>
7 # Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/>
9 # Permission is hereby granted, free of charge, to any person
10 # obtaining a copy of this software and associated documentation
11 # files (the "Software"), to deal in the Software without
12 # restriction, including without limitation the rights to use, copy,
13 # modify, merge, publish, distribute, sublicense, and/or sell copies
14 # of the Software, and to permit persons to whom the Software is
15 # furnished to do so, subject to the following conditions:
17 # The above copyright notice and this permission notice shall be
18 # included in all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 # DEALINGS IN THE SOFTWARE.
29 import sys
31 import dbus
33 def main(argv):
34 factory = dbus.SystemBus
36 if len(argv) > 2:
37 sys.exit(__doc__)
38 elif len(argv) == 2:
39 if argv[1] == '--session':
40 factory = dbus.SessionBus
41 elif argv[1] != '--system':
42 sys.exit(__doc__)
44 # Get a connection to the system or session bus as appropriate
45 # We're only using blocking calls, so don't actually need a main loop here
46 bus = factory()
48 # This could be done by calling bus.list_names(), but here's
49 # more or less what that means:
51 # Get a reference to the desktop bus' standard object, denoted
52 # by the path /org/freedesktop/DBus.
53 dbus_object = bus.get_object('org.freedesktop.DBus',
54 '/org/freedesktop/DBus')
56 # The object /org/freedesktop/DBus
57 # implements the 'org.freedesktop.DBus' interface
58 dbus_iface = dbus.Interface(dbus_object, 'org.freedesktop.DBus')
60 # One of the member functions in the org.freedesktop.DBus interface
61 # is ListNames(), which provides a list of all the other services
62 # registered on this bus. Call it, and print the list.
63 services = dbus_iface.ListNames()
64 services.sort()
65 for service in services:
66 print service
68 if __name__ == '__main__':
69 main(sys.argv)