1
# This file is part of Checkbox.
3
# Copyright 2012 Canonical Ltd.
5
# Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
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.
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.
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/>.
24
Utility modules for working with various things accessible over dbus
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
36
def connect_to_system_bus():
38
Connect to the system bus properly.
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.
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.
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
58
def drop_dbus_type(value):
60
Convert types from the DBus bindings to their python counterparts.
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.
66
The point of this function is to simplify printing of nested DBus data that
67
gets displayed in a rather illegible way.
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)):
81
elif isinstance(value, (Byte, UInt16, UInt32, UInt64,
82
Int16, Int32, Int64)):
84
elif isinstance(value, Boolean):
86
elif isinstance(value, Double):