summaryrefslogtreecommitdiff
path: root/python/dbus.py
blob: 3a997da1b35432d591653bdf8c6a316feb037aef (plain)
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

"""Module for high-level communication over the FreeDesktop.org Bus (DBus)

DBus allows you to share and access remote objects between processes
running on the desktop, and also to access system services (such as
the print spool).

To use DBus, first get a Bus object, which provides a connection to one
of a few standard dbus-daemon instances that might be running. From the
Bus you can get a RemoteService. A service is provided by an application or
process connected to the Bus, and represents a set of objects. Once you
have a RemoteService you can get a RemoteObject that implements a specific interface
(an interface is just a standard group of member functions). Then you can call
those member functions directly.

You can think of a complete method call as looking something like:

Bus:SESSION -> Service:org.gnome.Evolution -> Object:/org/gnome/Evolution/Inbox -> Interface: org.gnome.Evolution.MailFolder -> Method: Forward('message1', 'seth@gnome.org')

This communicates over the SESSION Bus to the org.gnome.Evolution process to call the
Forward method of the /org/gnome/Evolution/Inbox object (which provides the
org.gnome.Evolution.MailFolder interface) with two string arguments.

For example, the dbus-daemon itself provides a service and some objects:

# Get a connection to the desktop-wide SESSION bus
bus = dbus.Bus(dbus.Bus.TYPE_SESSION)

# Get the service provided by the dbus-daemon named org.freedesktop.DBus
dbus_service = bus.get_service('org.freedesktop.DBus')

# Get a reference to the desktop bus' standard object, denoted
# by the path /org/freedesktop/DBus. The object /org/freedesktop/DBus
# implements the 'org.freedesktop.DBus' interface
dbus_object = dbus_service.get_object('/org/freedesktop/DBus',
                                       'org.freedesktop.DBus')

# One of the member functions in the org.freedesktop.DBus interface
# is ListServices(), which provides a list of all the other services
# registered on this bus. Call it, and print the list.
print(dbus_object.ListServices())
"""

import dbus_bindings

class Bus:
    """A connection to a DBus daemon.

    One of three possible standard buses, the SESSION, SYSTEM,
    or ACTIVATION bus
    """
    TYPE_SESSION    = dbus_bindings.BUS_SESSION
    TYPE_SYSTEM     = dbus_bindings.BUS_SYSTEM
    TYPE_ACTIVATION = dbus_bindings.BUS_ACTIVATION

    def __init__(self, bus_type=TYPE_SESSION, glib_mainloop=True):
        self._connection = dbus_bindings.bus_get(bus_type)

        self._connection.add_filter(self._signal_func)
        self._match_rule_to_receivers = { }
        if (glib_mainloop):
            self._connection.setup_with_g_main()

    def get_service(self, service_name="org.freedesktop.Broadcast"):
        """Get one of the RemoteServices connected to this Bus. service_name
        is just a string of the form 'com.widgetcorp.MyService'
        """
        return RemoteService(self._connection, service_name)

    def add_signal_receiver(self, receiver, interface=None, service=None, path=None):
        match_rule = self._get_match_rule(interface, service, path)
        
        if (not self._match_rule_to_receivers.has_key(match_rule)):
            self._match_rule_to_receivers[match_rule] = [ ]
        self._match_rule_to_receivers[match_rule].append(receiver)
        
        dbus_bindings.bus_add_match(self._connection, match_rule)

    def remove_signal_receiver(self, receiver, interface=None, service=None, path=None):
        match_rule = self._get_match_rule(interface, service, path)

        if self._match_rule_to_receivers.has_key(match_rule):
            if self._match_rule_to_receivers[match_rule].__contains__(receiver):
                self._match_rule_to_receivers[match_rule].remove(receiver)
                dbus_bindings.bus_remove_match(self._connection, match_rule)

    def get_connection(self):
        """Get the dbus_bindings.Connection object associated with this Bus"""
        return self._connection

    def _get_match_rule(self, interface, service, path):
##        if (interface):
##            match_rule = match_rule + ",interface='%s'" % (interface)
##        if (service):
##            match_rule = match_rule + ",service='%s'" % (service)
##        if (path):
##            match_rule = match_rule + ",path='%s'" % (path)
        # FIXME: use the service here too!!!
        return "type='signal',interface='%s',path='%s'" % (interface, path)
    
    def _signal_func(self, connection, message):
        if (message.get_type() != dbus_bindings.MESSAGE_TYPE_SIGNAL):
            return
        
        interface = message.get_interface()
        service   = message.get_sender()
        path      = message.get_path()
        member    = message.get_member()

        match_rule = self._get_match_rule(interface, service, path)

        if (self._match_rule_to_receivers.has_key(match_rule)):
            receivers = self._match_rule_to_receivers[match_rule]
            args = [interface, member, service, path, message]
            for receiver in receivers:
                receiver(*args)
        

class RemoteObject:
    """A remote Object.

    A RemoteObject is provided by a RemoteService on a particular Bus. RemoteObjects
    have member functions, and can be called like normal Python objects.
    """
    def __init__(self, connection, service_name, object_path, interface):
        self._connection   = connection
        self._service_name = service_name
        self._object_path  = object_path
        self._interface    = interface

    def __getattr__(self, member):
        if member == '__call__':
            return object.__call__
        else:
            return RemoteMethod(self._connection, self._service_name,
                                self._object_path, self._interface, member)


class RemoteMethod:
    """A remote Method.

    Typically a member of a RemoteObject. Calls to the
    method produce messages that travel over the Bus and are routed
    to a specific Service.
    """
    def __init__(self, connection, service_name, object_path, interface, method_name):
        self._connection   = connection
        self._service_name = service_name
        self._object_path  = object_path
        self._interface    = interface
        self._method_name  = method_name

    def __call__(self, *args):
        message = dbus_bindings.MethodCall(self._object_path, self._interface, self._method_name)
        message.set_destination(self._service_name)
        
        # Add the arguments to the function
        iter = message.get_iter()
        for arg in args:
            iter.append(arg)

        reply_message = self._connection.send_with_reply_and_block(message, 5000)

        args_tuple = reply_message.get_args_list()
        if len(args_tuple) == 0:
            return
        elif len(args_tuple) == 1:
            return args_tuple[0]
        else:
            return args_tuple

class Service:
    """A base class for exporting your own Services across the Bus

    Just inherit from Service, providing the name of your service
    (e.g. org.designfu.SampleService).
    """
    def __init__(self, service_name, bus=None):
        self._service_name = service_name
                             
        if bus == None:
            # Get the default bus
            self._bus = Bus()
        else:
            self._bus = bus

        dbus_bindings.bus_acquire_service(self._bus.get_connection(), service_name)

    def get_bus(self):
        """Get the Bus this Service is on"""
        return self._bus

    def get_service_name(self):
        """Get the name of this service"""
        return self._service_name

class Object:
    """A base class for exporting your own Objects across the Bus.

    Just inherit from Object and provide a list of methods to share
    across the Bus. These will appear as member functions of your
    ServiceObject.
    """
    def __init__(self, object_path, methods_to_share, service):
        self._object_path = object_path
        self._service = service
        self._bus = service.get_bus()
        self._connection = self._bus.get_connection()

        self._method_name_to_method = self._build_method_dictionary(methods_to_share)
        
        self._connection.register_object_path(object_path, self._unregister_cb, self._message_cb)

    def broadcast_signal(self, interface, signal_name):
        message = dbus_bindings.Signal(self._object_path, interface, signal_name)
        #FIXME: need to set_sender, but it always disconnects when we do this
        #message.set_sender(self._service.get_service_name())
        self._connection.send(message)

    def _unregister_cb(self, connection):
        print ("Unregister")
        
    def _message_cb(self, connection, message):
        target_method_name = message.get_member()
        target_method = self._method_name_to_method[target_method_name]
        args = message.get_args_list()
        
        try:
            retval = target_method(*args)
        except Exception, e:
            if e.__module__ == '__main__':
                # FIXME: is it right to use .__name__ here?
                error_name = e.__class__.__name__
            else:
                error_name = e.__module__ + '.' + str(e.__class__.__name__)
            error_contents = str(e)
            reply = dbus_bindings.Error(message, error_name, error_contents)
        else:
            reply = dbus_bindings.MethodReturn(message)
            if retval != None:
                iter = reply.get_iter()
                iter.append(retval)
                
        self._connection.send(reply)

    def _build_method_dictionary(self, methods):
        method_dict = {}
        for method in methods:
            if method_dict.has_key(method.__name__):
                print ('WARNING: registering DBus Object methods, already have a method named %s' % (method.__name__))
            method_dict[method.__name__] = method
        return method_dict

class RemoteService:
    """A remote service providing objects.

    A service is typically a process or application that provides
    remote objects, but can also be the broadcast service that
    receives signals from all applications on the Bus.
    """
    
    def __init__(self, connection, service_name):
        self._connection     = connection
        self._service_name   = service_name

    def get_object(self, object_path, interface):
        """Get an object provided by this Service that implements a
        particular interface. object_path is a string of the form
        '/com/widgetcorp/MyService/MyObject1'. interface looks a lot
        like a service_name (they're often the same) and is of the form,
        'com.widgetcorp.MyInterface', and mostly just defines the
        set of member functions that will be present in the object.
        """
        return RemoteObject(self._connection, self._service_name, object_path, interface)