~ubuntu-branches/ubuntu/saucy/autopilot/saucy-proposed

« back to all changes in this revision

Viewing changes to autopilot/introspection/backends.py

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Łukasz 'sil2100' Zemczak, Ubuntu daily release
  • Date: 2013-07-12 00:02:34 UTC
  • mfrom: (52.5.1) (58.1.9 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130712000234-gsrwbjke7oi0yfxt
Tags: 1.3.1+13.10.20130712-0ubuntu1
[ Łukasz 'sil2100' Zemczak ]
* I don't like it, but it's the easiest way - make autopilot-touch
  Arch: any and make the python-ubuntu-platform-api [armhf] dependent.

[ Ubuntu daily release ]
* Automatic snapshot from revision 265

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
"Backend interface for autopilot."
21
21
from __future__ import absolute_import
22
22
 
 
23
from collections import namedtuple
23
24
import dbus
24
25
from autopilot.dbus_handler import (
25
26
    get_session_bus,
28
29
    )
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,
33
35
    )
34
36
 
35
37
 
 
38
class WireProtocolVersionMismatch(RuntimeError):
 
39
    """Wire protocols mismatch."""
 
40
 
 
41
 
36
42
class DBusAddress(object):
37
43
 
38
44
    "Store information about an Autopilot dbus backend, from keyword arguments."
 
45
    _checked_backends = []
 
46
 
 
47
    AddrTuple = namedtuple('AddressTuple', ['bus', 'connection', 'object_path'])
39
48
 
40
49
    @staticmethod
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.
73
 
        self._bus = bus
74
 
        self._connection = connection
75
 
        self._object_path = object_path
 
82
        self._addr_tuple = DBusAddress.AddrTuple(bus, connection, object_path)
76
83
 
77
84
    @property
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")
83
90
 
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
 
94
            )
 
95
        iface = dbus.Interface(proxy_obj, AP_INTROSPECTION_IFACE)
 
96
        if self._addr_tuple not in DBusAddress._checked_backends:
 
97
            try:
 
98
                self._check_version(iface)
 
99
            except WireProtocolVersionMismatch:
 
100
                raise
 
101
            else:
 
102
                DBusAddress._checked_backends.append(self._addr_tuple)
 
103
        return iface
 
104
 
 
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.
 
108
 
 
109
        """
 
110
        try:
 
111
            version = iface.GetVersion()
 
112
        except dbus.DBusException:
 
113
            version = "1.2"
 
114
        if version != CURRENT_WIRE_PROTOCOL_VERSION:
 
115
            raise WireProtocolVersionMismatch(
 
116
                "Wire protocol mismatch at %r: is %s, expecting %s" % (
 
117
                    self,
 
118
                    version,
 
119
                    CURRENT_WIRE_PROTOCOL_VERSION)
 
120
                )
86
121
 
87
122
    @property
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
 
127
            )
90
128
        return dbus.Interface(dbus_object, DBUS_INTROSPECTION_IFACE)
91
129
 
92
130
    @property
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
 
135
            )
 
136
        return dbus.Interface(proxy_obj, QT_AUTOPILOT_IFACE)
96
137
 
97
138
    def __hash__(self):
98
 
        return hash((self._bus, self._connection, self._object_path))
 
139
        return hash(self._addr_tuple)
99
140
 
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
104
145
 
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
109
150
 
110
151
    def __str__(self):
111
152
        return repr(self)
112
153
 
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:
115
156
            name = "session"
116
 
        elif self._bus._bus_type == dbus.Bus.TYPE_SYSTEM:
 
157
        elif self._addr_tuple.bus._bus_type == dbus.Bus.TYPE_SYSTEM:
117
158
            name = "system"
118
159
        else:
119
160
            name = "custom"
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)
121
162