29
30
from autopilot.introspection.constants import (
30
31
AP_INTROSPECTION_IFACE,
32
CURRENT_WIRE_PROTOCOL_VERSION,
31
33
DBUS_INTROSPECTION_IFACE,
32
34
QT_AUTOPILOT_IFACE,
38
class WireProtocolVersionMismatch(RuntimeError):
39
"""Wire protocols mismatch."""
36
42
class DBusAddress(object):
38
44
"Store information about an Autopilot dbus backend, from keyword arguments."
45
_checked_backends = []
47
AddrTuple = namedtuple('AddressTuple', ['bus', 'connection', 'object_path'])
41
50
def SessionBus(connection, object_path):
70
79
# We cannot evaluate kwargs for accuracy now, since this class will be
71
80
# created at module import time, at which point the bus backend probably
72
81
# does not exist yet.
74
self._connection = connection
75
self._object_path = object_path
82
self._addr_tuple = DBusAddress.AddrTuple(bus, connection, object_path)
78
85
def introspection_iface(self):
79
if not isinstance(self._connection, basestring):
86
if not isinstance(self._addr_tuple.connection, basestring):
80
87
raise TypeError("Service name must be a string.")
81
if not isinstance(self._object_path, basestring):
88
if not isinstance(self._addr_tuple.object_path, basestring):
82
89
raise TypeError("Object name must be a string")
84
_debug_proxy_obj = self._bus.get_object(self._connection, self._object_path)
85
return dbus.Interface(_debug_proxy_obj, AP_INTROSPECTION_IFACE)
91
proxy_obj = self._addr_tuple.bus.get_object(
92
self._addr_tuple.connection,
93
self._addr_tuple.object_path
95
iface = dbus.Interface(proxy_obj, AP_INTROSPECTION_IFACE)
96
if self._addr_tuple not in DBusAddress._checked_backends:
98
self._check_version(iface)
99
except WireProtocolVersionMismatch:
102
DBusAddress._checked_backends.append(self._addr_tuple)
105
def _check_version(self, iface):
106
"""Check the wire protocol version on 'iface', and raise an error if the
107
version does not match what we were expecting.
111
version = iface.GetVersion()
112
except dbus.DBusException:
114
if version != CURRENT_WIRE_PROTOCOL_VERSION:
115
raise WireProtocolVersionMismatch(
116
"Wire protocol mismatch at %r: is %s, expecting %s" % (
119
CURRENT_WIRE_PROTOCOL_VERSION)
88
123
def dbus_introspection_iface(self):
89
dbus_object = self._bus.get_object(self._connection, self._object_path)
124
dbus_object = self._addr_tuple.bus.get_object(
125
self._addr_tuple.connection,
126
self._addr_tuple.object_path
90
128
return dbus.Interface(dbus_object, DBUS_INTROSPECTION_IFACE)
93
131
def qt_introspection_iface(self):
94
_debug_proxy_obj = self._bus.get_object(self._connection, self._object_path)
95
return dbus.Interface(_debug_proxy_obj, QT_AUTOPILOT_IFACE)
132
proxy_obj = self._addr_tuple.bus.get_object(
133
self._addr_tuple.connection,
134
self._addr_tuple.object_path
136
return dbus.Interface(proxy_obj, QT_AUTOPILOT_IFACE)
97
138
def __hash__(self):
98
return hash((self._bus, self._connection, self._object_path))
139
return hash(self._addr_tuple)
100
141
def __eq__(self, other):
101
return self._bus == other._bus and \
102
self._connection == other._connection and \
103
self._object_path == other._object_path
142
return self._addr_tuple.bus == other._addr_tuple.bus and \
143
self._addr_tuple.connection == other._addr_tuple.connection and \
144
self._addr_tuple.object_path == other._addr_tuple.object_path
105
146
def __ne__(self, other):
106
return self._object_path != other._object_path or \
107
self._connection != other._connection or \
108
self._bus != other._bus
147
return self._addr_tuple.object_path != other._addr_tuple.object_path or \
148
self._addr_tuple.connection != other._addr_tuple.connection or \
149
self._addr_tuple.bus != other._addr_tuple.bus
110
151
def __str__(self):
111
152
return repr(self)
113
154
def __repr__(self):
114
if self._bus._bus_type == dbus.Bus.TYPE_SESSION:
155
if self._addr_tuple.bus._bus_type == dbus.Bus.TYPE_SESSION:
116
elif self._bus._bus_type == dbus.Bus.TYPE_SYSTEM:
157
elif self._addr_tuple.bus._bus_type == dbus.Bus.TYPE_SYSTEM:
120
return "<%s bus %s %s>" % (name, self._connection, self._object_path)
161
return "<%s bus %s %s>" % (name, self._addr_tuple.connection, self._addr_tuple.object_path)