~cgregan/checkbox/audio_dolby

« back to all changes in this revision

Viewing changes to checkbox-support/checkbox_support/dbus/udisks2.py

  • Committer: Sylvain Pineau
  • Date: 2014-01-07 13:39:38 UTC
  • mto: This revision was merged to the branch mainline in revision 2588.
  • Revision ID: sylvain.pineau@canonical.com-20140107133938-46v5ehofwa9whl1e
checkbox-support: Copy required modules from checkbox-old/checkbox

and their corresponding tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.
 
2
# Written by:
 
3
#   Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License version 3,
 
7
# as published by the Free Software Foundation.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
"""
 
18
checkbox.dbus.udisks2
 
19
=====================
 
20
 
 
21
Module for working with UDisks2 from python.
 
22
 
 
23
There are two main classes that are interesting here.
 
24
 
 
25
The first class is UDisksObserver, which is easy to setup and has pythonic API
 
26
to all of the stuff that happens in UDisks2. It offers simple signal handlers
 
27
for any changes that occur in UDisks2 that were advertised by DBus.
 
28
 
 
29
The second class is UDisksModel, that builds on the observer class to offer
 
30
persistent collection of objects managed by UDisks2.
 
31
 
 
32
To work with this model you will likely want to look at:
 
33
    http://udisks.freedesktop.org/docs/latest/ref-dbus.html
 
34
"""
 
35
 
 
36
import logging
 
37
 
 
38
from dbus import Interface, PROPERTIES_IFACE
 
39
from dbus.exceptions import DBusException
 
40
 
 
41
from checkbox.dbus import drop_dbus_type
 
42
 
 
43
__all__ = ['UDisks2Observer', 'UDisks2Model', 'Signal', 'is_udisks2_supported',
 
44
           'lookup_udev_device']
 
45
 
 
46
 
 
47
def is_udisks2_supported(system_bus):
 
48
    """
 
49
    Check if udisks2 is available on the system bus.
 
50
 
 
51
    ..note::
 
52
        Calling this _may_ trigger activation of the UDisks2 daemon but it
 
53
        should only happen on systems where it is already expected to run all
 
54
        the time.
 
55
    """
 
56
    observer = UDisks2Observer()
 
57
    try:
 
58
        logging.debug("Trying to connect to UDisks2...")
 
59
        observer.connect_to_bus(system_bus)
 
60
    except DBusException as exc:
 
61
        if exc.get_dbus_name() == "org.freedesktop.DBus.Error.ServiceUnknown":
 
62
            logging.debug("No UDisks2 on the system bus")
 
63
            return False
 
64
        else:
 
65
            raise
 
66
    else:
 
67
        logging.debug("Got UDisks2 connection")
 
68
        return True
 
69
 
 
70
 
 
71
def map_udisks1_connection_bus(udisks1_connection_bus):
 
72
    """
 
73
    Map the value of udisks1 ConnectionBus property to the corresponding values
 
74
    in udisks2. This a lossy function as some values are no longer supported.
 
75
 
 
76
    Incorrect values raise LookupError
 
77
    """
 
78
    return {
 
79
        'ata_serial_esata': '',  # gone from udisks2
 
80
        'firewire': 'ieee1394',  # renamed
 
81
        'scsi': '',              # gone from udisks2
 
82
        'sdio': 'sdio',          # as-is
 
83
        'usb': 'usb',            # as-is
 
84
    }[udisks1_connection_bus]
 
85
 
 
86
 
 
87
def lookup_udev_device(udisks2_object, udev_devices):
 
88
    """
 
89
    Find the udev_device that corresponds to the udisks2 object
 
90
 
 
91
    Devices are matched by unix filesystem path of the special file (device).
 
92
    The udisks2_object must implement the block device interface (so that the
 
93
    block device path can be determined) or a ValueError is raised.
 
94
 
 
95
    The udisks2_object must be the dictionary that maps from interface names to
 
96
    dictionaries of properties. For compatible data see
 
97
    UDisks2Model.managed_objects The udev_devices must be a list of udev
 
98
    device, as returned from GUdev.
 
99
 
 
100
    If there is no match, LookupError is raised with the unix block device
 
101
    path.
 
102
    """
 
103
    try:
 
104
        block_props = udisks2_object[UDISKS2_BLOCK_INTERFACE]
 
105
    except KeyError:
 
106
        raise ValueError("udisks2_object must be a block device")
 
107
    else:
 
108
        block_dev = block_props['Device']
 
109
    for udev_device in udev_devices:
 
110
        if udev_device.get_device_file() == block_dev:
 
111
            return udev_device
 
112
    raise LookupError(block_dev)
 
113
 
 
114
 
 
115
# The well-known name for the ObjectManager interface, sadly it is not a part
 
116
# of the python binding along with the rest of well-known names.
 
117
OBJECT_MANAGER_INTERFACE = "org.freedesktop.DBus.ObjectManager"
 
118
 
 
119
# The well-known name of the filesystem interface implemented by certain
 
120
# objects exposed by UDisks2
 
121
UDISKS2_FILESYSTEM_INTERFACE = "org.freedesktop.UDisks2.Filesystem"
 
122
 
 
123
# The well-known name of the block (device) interface implemented by certain
 
124
# objects exposed by UDisks2
 
125
UDISKS2_BLOCK_INTERFACE = "org.freedesktop.UDisks2.Block"
 
126
 
 
127
# The well-known name of the drive interface implemented by certain objects
 
128
# exposed by UDisks2
 
129
UDISKS2_DRIVE_INTERFACE = "org.freedesktop.UDisks2.Drive"
 
130
 
 
131
 
 
132
class Signal:
 
133
    """
 
134
    Basic signal that supports arbitrary listeners.
 
135
 
 
136
    While this class can be used directly it is best used with the helper
 
137
    decorator Signal.define on a member function. The function body is ignored,
 
138
    apart from the documentation.
 
139
 
 
140
    The function name then becomes a unique (per encapsulating class instance)
 
141
    object (an instance of this Signal class) that is created on demand.
 
142
 
 
143
    In practice you just have a documentation and use
 
144
    object.signal_name.connect() and object.signal_name(*args, **kwargs) to
 
145
    fire it.
 
146
    """
 
147
 
 
148
    def __init__(self, signal_name):
 
149
        """
 
150
        Construct a signal with the given name
 
151
        """
 
152
        self._listeners = []
 
153
        self._signal_name = signal_name
 
154
 
 
155
    def connect(self, listener):
 
156
        """
 
157
        Connect a new listener to this signal
 
158
 
 
159
        That listener will be called whenever fire() is invoked on the signal
 
160
        """
 
161
        self._listeners.append(listener)
 
162
 
 
163
    def disconnect(self, listener):
 
164
        """
 
165
        Disconnect an existing listener from this signal
 
166
        """
 
167
        self._listeners.remove(listener)
 
168
 
 
169
    def fire(self, args, kwargs):
 
170
        """
 
171
        Fire this signal with the specified arguments and keyword arguments.
 
172
 
 
173
        Typically this is used by using __call__() on this object which is more
 
174
        natural as it does all the argument packing/unpacking transparently.
 
175
        """
 
176
        for listener in self._listeners:
 
177
            listener(*args, **kwargs)
 
178
 
 
179
    def __call__(self, *args, **kwargs):
 
180
        """
 
181
        Call fire() with all arguments forwarded transparently
 
182
        """
 
183
        self.fire(args, kwargs)
 
184
 
 
185
    @classmethod
 
186
    def define(cls, dummy_func):
 
187
        """
 
188
        Helper decorator to define a signal descriptor in a class
 
189
 
 
190
        The decorated function is never called but is used to get
 
191
        documentation.
 
192
        """
 
193
        return _SignalDescriptor(dummy_func)
 
194
 
 
195
 
 
196
class _SignalDescriptor:
 
197
    """
 
198
    Descriptor for convenient signal access.
 
199
 
 
200
    Typically this class is used indirectly, when accessed from Signal.define
 
201
    method decorator. It is used to do all the magic required when accessing
 
202
    signal name on a class or instance.
 
203
    """
 
204
 
 
205
    def __init__(self, dummy_func):
 
206
        self.signal_name = dummy_func.__name__
 
207
        self.__doc__ = dummy_func.__doc__
 
208
 
 
209
    def __repr__(self):
 
210
        return "<SignalDecorator for signal: %r>" % self.signal_name
 
211
 
 
212
    def __get__(self, instance, owner):
 
213
        if instance is None:
 
214
            return self
 
215
        # Ensure that the instance has __signals__ property
 
216
        if not hasattr(instance, "__signals__"):
 
217
            instance.__signals__ = {}
 
218
        if self.signal_name not in instance.__signals__:
 
219
            instance.__signals__[self.signal_name] = Signal(self.signal_name)
 
220
        return instance.__signals__[self.signal_name]
 
221
 
 
222
    def __set__(self, instance, value):
 
223
        raise AttributeError("You cannot overwrite signals")
 
224
 
 
225
    def __delete__(self, instance):
 
226
        raise AttributeError("You cannot delete signals")
 
227
 
 
228
 
 
229
class UDisks2Observer:
 
230
    """
 
231
    Class for observing ongoing changes in UDisks2
 
232
    """
 
233
 
 
234
    def __init__(self):
 
235
        """
 
236
        Create a UDisks2 model.
 
237
 
 
238
        The model must be connected to a bus before it is first used, see
 
239
        connect()
 
240
        """
 
241
        # Proxy to the UDisks2 object
 
242
        self._udisks2_obj = None
 
243
        # Proxy to the ObjectManager interface exposed by UDisks2 object
 
244
        self._udisks2_obj_manager = None
 
245
 
 
246
    @Signal.define
 
247
    def on_initial_objects(self, managed_objects):
 
248
        """
 
249
        Signal fired when the initial list of objects becomes available
 
250
        """
 
251
 
 
252
    @Signal.define
 
253
    def on_interfaces_added(self, object_path, interfaces_and_properties):
 
254
        """
 
255
        Signal fired when one or more interfaces gets added to a specific
 
256
        object.
 
257
        """
 
258
 
 
259
    @Signal.define
 
260
    def on_interfaces_removed(self, object_path, interfaces):
 
261
        """
 
262
        Signal fired when one or more interface gets removed from a specific
 
263
        object
 
264
        """
 
265
 
 
266
    @Signal.define
 
267
    def on_properties_changed(self, interface_name, changed_properties,
 
268
                              invalidated_properties, sender=None):
 
269
        """
 
270
        Signal fired when one or more property changes value or becomes
 
271
        invalidated.
 
272
        """
 
273
 
 
274
    def connect_to_bus(self, bus):
 
275
        """
 
276
        Establish initial connection to UDisks2 on the specified DBus bus.
 
277
 
 
278
        This will also load the initial set of objects from UDisks2 and thus
 
279
        fire the on_initial_objects() signal from the model. Please call this
 
280
        method only after connecting that signal if you want to observe that
 
281
        event.
 
282
        """
 
283
        # Once everything is ready connect to udisks2
 
284
        self._connect_to_udisks2(bus)
 
285
        # And read all the initial objects and setup
 
286
        # change event handlers
 
287
        self._get_initial_objects()
 
288
 
 
289
    def _connect_to_udisks2(self, bus):
 
290
        """
 
291
        Setup the initial connection to UDisks2
 
292
 
 
293
        This step can fail if UDisks2 is not available and cannot be
 
294
        service-activated.
 
295
        """
 
296
        # Access the /org/freedesktop/UDisks2 object sitting on the
 
297
        # org.freedesktop.UDisks2 bus name. This will trigger the necessary
 
298
        # activation if udisksd is not running for any reason
 
299
        logging.debug("Accessing main UDisks2 object")
 
300
        self._udisks2_obj = bus.get_object(
 
301
            "org.freedesktop.UDisks2", "/org/freedesktop/UDisks2")
 
302
        # Now extract the standard ObjectManager interface so that we can
 
303
        # observe and iterate the collection of objects that UDisks2 provides.
 
304
        logging.debug("Accessing ObjectManager interface on UDisks2 object")
 
305
        self._udisks2_obj_manager = Interface(
 
306
            self._udisks2_obj, OBJECT_MANAGER_INTERFACE)
 
307
        # Connect to the PropertiesChanged signal. Here unlike before we want
 
308
        # to listen to all signals, regardless of who was sending them in the
 
309
        # first place.
 
310
        logging.debug("Setting up DBus signal handler for PropertiesChanged")
 
311
        bus.add_signal_receiver(
 
312
            self._on_properties_changed,
 
313
            signal_name="PropertiesChanged",
 
314
            dbus_interface=PROPERTIES_IFACE,
 
315
            # Use the sender_keyword keyword argument to indicate that we wish
 
316
            # to know the sender of each signal. For consistency with other
 
317
            # signals we choose to use the 'object_path' keyword argument.
 
318
            sender_keyword='sender')
 
319
 
 
320
    def _get_initial_objects(self):
 
321
        """
 
322
        Get the initial collection of objects.
 
323
 
 
324
        Needs to be called before the first signals from DBus are observed.
 
325
        Requires a working connection to UDisks2.
 
326
        """
 
327
        # Having this interface we can now peek at the existing objects.
 
328
        # We can use the standard method GetManagedObjects() to do that
 
329
        logging.debug("Accessing GetManagedObjects() on UDisks2 object")
 
330
        managed_objects = self._udisks2_obj_manager.GetManagedObjects()
 
331
        managed_objects = drop_dbus_type(managed_objects)
 
332
        # Fire the public signal for getting initial objects
 
333
        self.on_initial_objects(managed_objects)
 
334
        # Connect our internal handles to the DBus signal handlers
 
335
        logging.debug("Setting up DBus signal handler for InterfacesAdded")
 
336
        self._udisks2_obj_manager.connect_to_signal(
 
337
            "InterfacesAdded", self._on_interfaces_added)
 
338
        logging.debug("Setting up DBus signal handler for InterfacesRemoved")
 
339
        self._udisks2_obj_manager.connect_to_signal(
 
340
            "InterfacesRemoved", self._on_interfaces_removed)
 
341
 
 
342
    def _on_interfaces_added(self, object_path, interfaces_and_properties):
 
343
        """
 
344
        Internal callback that is called by DBus
 
345
 
 
346
        This function is responsible for firing the public signal
 
347
        """
 
348
        # Convert from dbus types
 
349
        object_path = drop_dbus_type(object_path)
 
350
        interfaces_and_properties = drop_dbus_type(interfaces_and_properties)
 
351
        # Log what's going on
 
352
        logging.debug("The object %r has gained the following interfaces and "
 
353
                      "properties: %r", object_path, interfaces_and_properties)
 
354
        # Call the signal handler
 
355
        self.on_interfaces_added(object_path, interfaces_and_properties)
 
356
 
 
357
    def _on_interfaces_removed(self, object_path, interfaces):
 
358
        """
 
359
        Internal callback that is called by DBus
 
360
 
 
361
        This function is responsible for firing the public signal
 
362
        """
 
363
        # Convert from dbus types
 
364
        object_path = drop_dbus_type(object_path)
 
365
        interfaces = drop_dbus_type(interfaces)
 
366
        # Log what's going on
 
367
        logging.debug("The object %r has lost the following interfaces: %r",
 
368
                      object_path, interfaces)
 
369
        # Call the signal handler
 
370
        self.on_interfaces_removed(object_path, interfaces)
 
371
 
 
372
    def _on_properties_changed(self, interface_name, changed_properties,
 
373
                               invalidated_properties, sender=None):
 
374
        """
 
375
        Internal callback that is called by DBus
 
376
 
 
377
        This function is responsible for firing the public signal
 
378
        """
 
379
        # Convert from dbus types
 
380
        interface_name = drop_dbus_type(interface_name)
 
381
        changed_properties = drop_dbus_type(changed_properties)
 
382
        invalidated_properties = drop_dbus_type(invalidated_properties)
 
383
        sender = drop_dbus_type(sender)
 
384
        # Log what's going on
 
385
        logging.debug("Some object with the interface %r has changed "
 
386
                      "properties: %r and invalidated properties %r "
 
387
                      "(sender: %s)",
 
388
                      interface_name, changed_properties,
 
389
                      invalidated_properties, sender)
 
390
        # Call the signal handler
 
391
        self.on_properties_changed(interface_name, changed_properties,
 
392
                                   invalidated_properties, sender)
 
393
 
 
394
 
 
395
class UDisks2Model:
 
396
    """
 
397
    Model for working with UDisks2
 
398
 
 
399
    This class maintains a persistent model of what UDisks2 knows about, based
 
400
    on the UDisks2Observer class and the signals it offers.
 
401
    """
 
402
 
 
403
    def __init__(self, observer):
 
404
        """
 
405
        Create a UDisks2 model.
 
406
 
 
407
        The model will track changes using the specified observer (which is
 
408
        expected to be a UDisks2Observer instance)
 
409
 
 
410
        You should only connect the observer to the bus after creating the
 
411
        model otherwise the initial objects will not be detected.
 
412
        """
 
413
        # Local state, everything that UDisks2 tells us
 
414
        self._managed_objects = {}
 
415
        self._observer = observer
 
416
        # Connect all the signals to the observer
 
417
        self._observer.on_initial_objects.connect(self._on_initial_objects)
 
418
        self._observer.on_interfaces_added.connect(self._on_interfaces_added)
 
419
        self._observer.on_interfaces_removed.connect(
 
420
            self._on_interfaces_removed)
 
421
        self._observer.on_properties_changed.connect(
 
422
            self._on_properties_changed)
 
423
 
 
424
    @Signal.define
 
425
    def on_change(self):
 
426
        """
 
427
        Signal sent whenever the collection of managed object changes
 
428
 
 
429
        Note that this signal is fired _after_ the change has occurred
 
430
        """
 
431
 
 
432
    @property
 
433
    def managed_objects(self):
 
434
        """
 
435
        A collection of objects that is managed by this model. All changes as
 
436
        well as the initial state, are reflected here.
 
437
        """
 
438
        return self._managed_objects
 
439
 
 
440
    def _on_initial_objects(self, managed_objects):
 
441
        """
 
442
        Internal callback called when we get the initial collection of objects
 
443
        """
 
444
        self._managed_objects = drop_dbus_type(managed_objects)
 
445
 
 
446
    def _on_interfaces_added(self, object_path, interfaces_and_properties):
 
447
        """
 
448
        Internal callback called when an interface is added to certain object
 
449
        """
 
450
        # Update internal state
 
451
        if object_path not in self._managed_objects:
 
452
            self._managed_objects[object_path] = {}
 
453
        obj = self._managed_objects[object_path]
 
454
        obj.update(interfaces_and_properties)
 
455
        # Fire the change signal
 
456
        self.on_change()
 
457
 
 
458
    def _on_interfaces_removed(self, object_path, interfaces):
 
459
        """
 
460
        Internal callback called when an interface is removed from a certain
 
461
        object
 
462
        """
 
463
        # Update internal state
 
464
        if object_path in self._managed_objects:
 
465
            obj = self._managed_objects[object_path]
 
466
            for interface in interfaces:
 
467
                if interface in obj:
 
468
                    del obj[interface]
 
469
        # Fire the change signal
 
470
        self.on_change()
 
471
 
 
472
    def _on_properties_changed(self, interface_name, changed_properties,
 
473
                               invalidated_properties, sender=None):
 
474
        # XXX: This is a workaround the fact that we cannot
 
475
        # properly track changes to all properties :-(
 
476
        self._managed_objects = drop_dbus_type(
 
477
            self._observer._udisks2_obj_manager.GetManagedObjects())
 
478
        # Fire the change signal()
 
479
        self.on_change()