~kissiel/checkbox/fix-1420352

« back to all changes in this revision

Viewing changes to checkbox-support/checkbox_support/dbus/__init__.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
# This file is part of Checkbox.
 
2
#
 
3
# Copyright 2012 Canonical Ltd.
 
4
# Written by:
 
5
#   Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
 
6
#
 
7
# Checkbox is free software: you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License version 3,
 
9
# as published by the Free Software Foundation.
 
10
 
 
11
#
 
12
# Checkbox is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
"""
 
21
checkbox.dbus
 
22
=============
 
23
 
 
24
Utility modules for working with various things accessible over dbus
 
25
"""
 
26
 
 
27
import logging
 
28
 
 
29
from dbus import SystemBus
 
30
from dbus.mainloop.glib import DBusGMainLoop
 
31
from dbus import (Array, Boolean, Byte, Dictionary, Double, Int16, Int32,
 
32
                  Int64, ObjectPath, String, Struct, UInt16, UInt32, UInt64)
 
33
from gi.repository import GObject
 
34
 
 
35
 
 
36
def connect_to_system_bus():
 
37
    """
 
38
    Connect to the system bus properly.
 
39
 
 
40
    Returns a tuple (system_bus, loop) where loop is a GObject.MainLoop
 
41
    instance. The loop is there so that you can listen to signals.
 
42
    """
 
43
    # We'll need an event loop to observe signals. We will need the instance
 
44
    # later below so let's keep it. Note that we're not passing it directly
 
45
    # below as DBus needs specific API. The DBusGMainLoop class that we
 
46
    # instantiate and pass is going to work with this instance transparently.
 
47
    #
 
48
    # NOTE: DBus tutorial suggests that we should create the loop _before_
 
49
    # connecting to the bus.
 
50
    logging.debug("Setting up glib-based event loop")
 
51
    loop = GObject.MainLoop()
 
52
    # Let's get the system bus object. We need that to access UDisks2 object
 
53
    logging.debug("Connecting to DBus system bus")
 
54
    system_bus = SystemBus(mainloop=DBusGMainLoop())
 
55
    return system_bus, loop
 
56
 
 
57
 
 
58
def drop_dbus_type(value):
 
59
    """
 
60
    Convert types from the DBus bindings to their python counterparts.
 
61
 
 
62
    This function is mostly lossless, except for arrays of bytes (DBus
 
63
    signature "y") that are transparently converted to strings, assuming
 
64
    an UTF-8 encoded string.
 
65
 
 
66
    The point of this function is to simplify printing of nested DBus data that
 
67
    gets displayed in a rather illegible way.
 
68
    """
 
69
    if isinstance(value, Array) and value.signature == "y":
 
70
        # Some other things are reported as array of bytes that are just
 
71
        # strings but due to Unix heritage the encoding is not known.
 
72
        # In practice it is better to treat them as UTF-8 strings
 
73
        return bytes(value).decode("UTF-8", "replace").strip("\0")
 
74
    elif isinstance(value, (Struct, Array)):
 
75
        return [drop_dbus_type(item) for item in value]
 
76
    elif isinstance(value, (Dictionary)):
 
77
        return {drop_dbus_type(dict_key): drop_dbus_type(dict_value)
 
78
                for dict_key, dict_value in value.items()}
 
79
    elif isinstance(value, (String, ObjectPath)):
 
80
        return str(value)
 
81
    elif isinstance(value, (Byte, UInt16, UInt32, UInt64,
 
82
                            Int16, Int32, Int64)):
 
83
        return int(value)
 
84
    elif isinstance(value, Boolean):
 
85
        return bool(value)
 
86
    elif isinstance(value, Double):
 
87
        return float(value)
 
88
    else:
 
89
        return value