Merge lp:~thomir-deactivatedaccount/autopilot/trunk-use-six into lp:autopilot

Proposed by Thomi Richards
Status: Merged
Approved by: Christopher Lee
Approved revision: 352
Merged at revision: 352
Proposed branch: lp:~thomir-deactivatedaccount/autopilot/trunk-use-six
Merge into: lp:autopilot
Diff against target: 466 lines (+38/-74)
11 files modified
autopilot/input/_X11.py (+5/-9)
autopilot/input/_osk.py (+2/-6)
autopilot/input/_uinput.py (+4/-8)
autopilot/introspection/__init__.py (+2/-6)
autopilot/introspection/dbus.py (+5/-13)
autopilot/keybindings.py (+4/-8)
autopilot/tests/functional/test_ap_apps.py (+2/-1)
autopilot/tests/unit/test_exceptions.py (+4/-6)
autopilot/tests/unit/test_matchers.py (+5/-10)
autopilot/vis/main_window.py (+3/-5)
autopilot/vis/objectproperties.py (+2/-2)
To merge this branch: bzr merge lp:~thomir-deactivatedaccount/autopilot/trunk-use-six
Reviewer Review Type Date Requested Status
Christopher Lee (community) Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+190843@code.launchpad.net

Commit message

Use the 'six' python module to help us get python 2/3 compatibility, rather than rolling our own.

Description of the change

Use the 'six' python module to help us get python 2/3 compatibility, rather than rolling our own.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Christopher Lee (veebers) wrote :

Looks good to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'autopilot/input/_X11.py'
2--- autopilot/input/_X11.py 2013-10-10 00:35:30 +0000
3+++ autopilot/input/_X11.py 2013-10-13 21:52:16 +0000
4@@ -27,7 +27,7 @@
5 from __future__ import absolute_import
6
7 import logging
8-import sys
9+import six
10 from time import sleep
11
12 from autopilot.display import is_point_on_any_screen, move_mouse_to_screen
13@@ -46,10 +46,6 @@
14 _DISPLAY = None
15 logger = logging.getLogger(__name__)
16
17-# py2 compatible alias for py3
18-if sys.version >= '3':
19- basestring = str
20-
21
22 def get_display():
23 """Return the Xlib display object.
24@@ -141,7 +137,7 @@
25 presses the 'Alt' and 'F2' keys.
26
27 """
28- if not isinstance(keys, basestring):
29+ if not isinstance(keys, six.string_types):
30 raise TypeError("'keys' argument must be a string.")
31 logger.debug("Pressing keys %r with delay %f", keys, delay)
32 for key in self.__translate_keys(keys):
33@@ -160,7 +156,7 @@
34 releases the 'Alt' and 'F2' keys.
35
36 """
37- if not isinstance(keys, basestring):
38+ if not isinstance(keys, six.string_types):
39 raise TypeError("'keys' argument must be a string.")
40 logger.debug("Releasing keys %r with delay %f", keys, delay)
41 # release keys in the reverse order they were pressed in.
42@@ -196,7 +192,7 @@
43 and a 't').
44
45 """
46- if not isinstance(string, basestring):
47+ if not isinstance(string, six.string_types):
48 raise TypeError("'keys' argument must be a string.")
49 logger.debug("Typing text %r", string)
50 for key in string:
51@@ -223,7 +219,7 @@
52 _PRESSED_KEYS = []
53
54 def __perform_on_key(self, key, event):
55- if not isinstance(key, basestring):
56+ if not isinstance(key, six.string_types):
57 raise TypeError("Key parameter must be a string")
58
59 keycode = 0
60
61=== modified file 'autopilot/input/_osk.py'
62--- autopilot/input/_osk.py 2013-09-05 09:38:04 +0000
63+++ autopilot/input/_osk.py 2013-10-13 21:52:16 +0000
64@@ -18,7 +18,7 @@
65 #
66
67 import logging
68-import sys
69+import six
70 from time import sleep
71 from contextlib import contextmanager
72
73@@ -32,10 +32,6 @@
74
75 logger = logging.getLogger(__name__)
76
77-# py2 compatible alias for py3
78-if sys.version >= '3':
79- basestring = str
80-
81
82 class Keyboard(KeyboardBase):
83
84@@ -106,7 +102,7 @@
85 not supported by the OSK Backend (or the current OSK langauge layout.)
86
87 """
88- if not isinstance(string, basestring):
89+ if not isinstance(string, six.string_types):
90 raise TypeError("'string' argument must be a string.")
91 logger.debug("Typing text: %s", string)
92 self._keyboard.type(string, delay)
93
94=== modified file 'autopilot/input/_uinput.py'
95--- autopilot/input/_uinput.py 2013-10-10 00:35:30 +0000
96+++ autopilot/input/_uinput.py 2013-10-13 21:52:16 +0000
97@@ -29,7 +29,7 @@
98 from time import sleep
99 from evdev import UInput, ecodes as e
100 import os.path
101-import sys
102+import six
103
104 logger = logging.getLogger(__name__)
105
106@@ -38,10 +38,6 @@
107
108 _PRESSED_KEYS = []
109
110-# py2 compatible alias for py3
111-if sys.version >= '3':
112- basestring = str
113-
114
115 def _get_devnode_path():
116 """Provide a fallback uinput node for devices which don't support udev"""
117@@ -76,7 +72,7 @@
118 presses the 'Alt' and 'F2' keys.
119
120 """
121- if not isinstance(keys, basestring):
122+ if not isinstance(keys, six.string_types):
123 raise TypeError("'keys' argument must be a string.")
124
125 for key in self._sanitise_keys(keys):
126@@ -99,7 +95,7 @@
127 Keys are released in the reverse order in which they are specified.
128
129 """
130- if not isinstance(keys, basestring):
131+ if not isinstance(keys, six.string_types):
132 raise TypeError("'keys' argument must be a string.")
133
134 for key in reversed(self._sanitise_keys(keys)):
135@@ -134,7 +130,7 @@
136 (such as 'Alt' will be interpreted as an 'A', and 'l', and a 't').
137
138 """
139- if not isinstance(string, basestring):
140+ if not isinstance(string, six.string_types):
141 raise TypeError("'keys' argument must be a string.")
142 logger.debug("Typing text %r", string)
143 for key in string:
144
145=== modified file 'autopilot/introspection/__init__.py'
146--- autopilot/introspection/__init__.py 2013-10-07 03:14:09 +0000
147+++ autopilot/introspection/__init__.py 2013-10-13 21:52:16 +0000
148@@ -34,7 +34,7 @@
149 from functools import partial
150 import os
151 import psutil
152-import sys
153+import six
154
155 from autopilot.introspection.backends import DBusAddress
156 from autopilot.introspection.constants import (
157@@ -63,10 +63,6 @@
158 # Keep track of known connections during search
159 connection_list = []
160
161-# py2 compatible alias for py3
162-if sys.version >= '3':
163- basestring = str
164-
165
166 class ProcessSearchError(RuntimeError):
167 pass
168@@ -118,7 +114,7 @@
169 prepare the environment before launching the application itself.
170 """
171
172- if not isinstance(application, basestring):
173+ if not isinstance(application, six.string_types):
174 raise TypeError("'application' parameter must be a string.")
175 cwd = kwargs.pop('launch_dir', None)
176 capture_output = kwargs.pop('capture_output', True)
177
178=== modified file 'autopilot/introspection/dbus.py'
179--- autopilot/introspection/dbus.py 2013-10-07 03:55:03 +0000
180+++ autopilot/introspection/dbus.py 2013-10-13 21:52:16 +0000
181@@ -30,7 +30,7 @@
182 from contextlib import contextmanager
183 import logging
184 import re
185-import sys
186+import six
187 from time import sleep
188 from uuid import uuid4
189
190@@ -43,14 +43,6 @@
191 logger = logging.getLogger(__name__)
192
193
194-# py2 compatible alias for py3
195-if sys.version >= '3':
196- _PY3 = True
197- basestring = str
198-else:
199- _PY3 = False
200-
201-
202 class StateNotFoundError(RuntimeError):
203
204 """Raised when a piece of state information is not found.
205@@ -96,7 +88,7 @@
206 )
207
208 def __str__(self):
209- if sys.version_info[0] >= 3:
210+ if six.PY3:
211 return self._message
212 else:
213 return self._message.encode('utf8')
214@@ -225,7 +217,7 @@
215 result = []
216 for instance in instances:
217 # Skip items that are not instances of the desired type:
218- if isinstance(desired_type, basestring):
219+ if isinstance(desired_type, six.string_types):
220 if instance.__class__.__name__ != desired_type:
221 continue
222 elif not isinstance(instance, desired_type):
223@@ -529,7 +521,7 @@
224 :raises: **TypeError** on invalid *piece* parameter.
225
226 """
227- if not isinstance(piece, basestring):
228+ if not isinstance(piece, six.string_types):
229 raise TypeError(
230 "XPath query must be a string, not %r", type(piece))
231
232@@ -631,7 +623,7 @@
233
234 """
235 if isinstance(value, str):
236- if _PY3:
237+ if six.PY3:
238 escaped_value = value.encode("unicode_escape")\
239 .decode('ASCII')\
240 .replace("'", "\\'")
241
242=== modified file 'autopilot/keybindings.py'
243--- autopilot/keybindings.py 2013-09-16 19:58:11 +0000
244+++ autopilot/keybindings.py 2013-10-13 21:52:16 +0000
245@@ -37,7 +37,7 @@
246
247 import logging
248 import re
249-import sys
250+import six
251
252 from autopilot.input import Keyboard
253 from autopilot.utilities import Silence
254@@ -45,10 +45,6 @@
255 logger = logging.getLogger(__name__)
256
257
258-# py2 compatible alias for py3
259-if sys.version >= '3':
260- basestring = str
261-
262 #
263 # Fill this dictionary with keybindings we want to store.
264 #
265@@ -145,12 +141,12 @@
266 dictionaries.
267
268 """
269- if not isinstance(binding_name, basestring):
270+ if not isinstance(binding_name, six.string_types):
271 raise TypeError("binding_name must be a string.")
272 if binding_name not in _keys:
273 raise ValueError("Unknown binding name '%s'." % (binding_name))
274 v = _keys[binding_name]
275- if isinstance(v, basestring):
276+ if isinstance(v, six.string_types):
277 return v
278 else:
279 return _get_compiz_keybinding(v)
280@@ -232,7 +228,7 @@
281 :param string keystroke_string: A compizconfig-style keystroke string.
282
283 """
284- if not isinstance(keystroke_string, basestring):
285+ if not isinstance(keystroke_string, six.string_types):
286 raise TypeError("keystroke string must be a string.")
287
288 translations = {
289
290=== modified file 'autopilot/tests/functional/test_ap_apps.py'
291--- autopilot/tests/functional/test_ap_apps.py 2013-09-18 13:50:18 +0000
292+++ autopilot/tests/functional/test_ap_apps.py 2013-10-13 21:52:16 +0000
293@@ -24,6 +24,7 @@
294 import subprocess
295 import logging
296 import sys
297+import six
298 from mock import patch
299 from tempfile import mktemp
300 from testtools.matchers import raises, LessThan
301@@ -39,7 +40,7 @@
302
303
304 # backwards compatible alias for Python 3
305-if sys.version > '3':
306+if six.PY3:
307 xrange = range
308
309 logger = logging.getLogger(__name__)
310
311=== modified file 'autopilot/tests/unit/test_exceptions.py'
312--- autopilot/tests/unit/test_exceptions.py 2013-09-18 17:17:08 +0000
313+++ autopilot/tests/unit/test_exceptions.py 2013-10-13 21:52:16 +0000
314@@ -17,14 +17,12 @@
315 # along with this program. If not, see <http://www.gnu.org/licenses/>.
316 #
317
318-import sys
319+import six
320 from testtools import TestCase
321 from testtools.matchers import raises, Equals
322
323 from autopilot.introspection.dbus import StateNotFoundError
324
325-_Py2 = sys.version_info[0] == 2
326-
327
328 class StateNotFoundTests(TestCase):
329
330@@ -45,7 +43,7 @@
331 str(err),
332 Equals("State not found for class 'MyClass'.")
333 )
334- if _Py2:
335+ if not six.PY3:
336 self.assertThat(
337 unicode(err),
338 Equals(u"State not found for class 'MyClass'.")
339@@ -58,7 +56,7 @@
340 str(err),
341 Equals("State not found with filters {'foo': 'bar'}.")
342 )
343- if _Py2:
344+ if not six.PY3:
345 self.assertThat(
346 unicode(err),
347 Equals(u"State not found with filters {'foo': 'bar'}.")
348@@ -72,7 +70,7 @@
349 Equals("State not found for class 'MyClass'"
350 " and filters {'foo': 'bar'}.")
351 )
352- if _Py2:
353+ if not six.PY3:
354 self.assertThat(
355 unicode(err),
356 Equals(u"State not found for class 'MyClass'"
357
358=== modified file 'autopilot/tests/unit/test_matchers.py'
359--- autopilot/tests/unit/test_matchers.py 2013-10-07 03:14:09 +0000
360+++ autopilot/tests/unit/test_matchers.py 2013-10-13 21:52:16 +0000
361@@ -20,14 +20,10 @@
362
363 from __future__ import absolute_import
364
365-import sys
366-
367-from autopilot.introspection.dbus import DBusIntrospectionObject
368-from autopilot.matchers import Eventually
369-
370 from contextlib import contextmanager
371 import dbus
372 from testscenarios import TestWithScenarios
373+import six
374 from testtools import TestCase
375 from testtools.matchers import (
376 Contains,
377@@ -39,9 +35,8 @@
378 )
379 from time import time
380
381-
382-if sys.version >= '3':
383- unicode = str
384+from autopilot.introspection.dbus import DBusIntrospectionObject
385+from autopilot.matchers import Eventually
386
387
388 @contextmanager
389@@ -76,10 +71,10 @@
390 if attribute_type == 'callable':
391 return lambda: result
392 elif attribute_type == 'wait_for':
393- if isinstance(result, unicode):
394+ if isinstance(result, six.text_type):
395 obj = FakeObject(dict(id=[0, 123], attr=[0, dbus.String(result)]))
396 return obj.attr
397- elif isinstance(result, bytes):
398+ elif isinstance(result, six.binary_type):
399 obj = FakeObject(
400 dict(id=[0, 123], attr=[0, dbus.UTF8String(result)])
401 )
402
403=== modified file 'autopilot/vis/main_window.py'
404--- autopilot/vis/main_window.py 2013-09-11 18:45:00 +0000
405+++ autopilot/vis/main_window.py 2013-10-13 21:52:16 +0000
406@@ -20,9 +20,9 @@
407
408 from __future__ import absolute_import
409
410-import sys
411 import dbus
412 from PyQt4 import QtGui, QtCore
413+import six
414
415 from autopilot.introspection import (
416 _get_dbus_address_object,
417@@ -34,8 +34,6 @@
418 from autopilot.vis.objectproperties import TreeNodeDetailWidget
419 from autopilot.vis.resources import get_qt_icon
420
421-_PY3 = sys.version >= '3'
422-
423
424 class MainWindow(QtGui.QMainWindow):
425 def __init__(self, dbus_bus):
426@@ -47,7 +45,7 @@
427
428 def readSettings(self):
429 settings = QtCore.QSettings()
430- if _PY3:
431+ if six.PY3:
432 self.restoreGeometry(settings.value("geometry").data())
433 self.restoreState(settings.value("windowState").data())
434 else:
435@@ -116,7 +114,7 @@
436
437 def conn_list_activated(self, index):
438 dbus_details = self.connection_list.itemData(index)
439- if not _PY3:
440+ if not six.PY3:
441 dbus_details = dbus_details.toPyObject()
442 if dbus_details:
443 self.tree_model = VisTreeModel(dbus_details)
444
445=== modified file 'autopilot/vis/objectproperties.py'
446--- autopilot/vis/objectproperties.py 2013-09-11 18:45:00 +0000
447+++ autopilot/vis/objectproperties.py 2013-10-13 21:52:16 +0000
448@@ -20,8 +20,8 @@
449
450 """Code for introspection tree object properties."""
451
452-import sys
453
454+import six
455 from PyQt4 import QtGui, QtCore
456
457
458@@ -97,7 +97,7 @@
459 def __init__(self, *args, **kwargs):
460 super(PropertyView, self).__init__(*args, **kwargs)
461
462- if sys.version >= '3':
463+ if six.PY3:
464 header_titles = ["Name", "Value"]
465 else:
466 header_titles = QtCore.QStringList(["Name", "Value"])

Subscribers

People subscribed via source and target branches