forked from cambridgehackers/python-adb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_stub.py
More file actions
43 lines (31 loc) · 1.15 KB
/
common_stub.py
File metadata and controls
43 lines (31 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""Stubs for tests using common's usb handling."""
import binascii
import string
PRINTABLE_DATA = set(string.printable) - set(string.whitespace)
def _Dotify(data):
return ''.join(char if char in PRINTABLE_DATA else '.' for char in data)
class StubUsb(object):
"""UsbHandle stub."""
def __init__(self):
self.written_data = []
self.read_data = []
self.timeout_ms = 0
def BulkWrite(self, data, unused_timeout_ms=None):
expected_data = self.written_data.pop(0)
if expected_data != data:
raise ValueError('Expected %s, got %s (%s)' % (
_Dotify(expected_data), binascii.hexlify(data), _Dotify(data)))
def BulkRead(self, length,
timeout_ms=None): # pylint: disable=unused-argument
data = self.read_data.pop(0)
if length < len(data):
raise ValueError(
'Overflow packet length. Read %d bytes, got %d bytes: %s',
length, len(data))
return data
def ExpectWrite(self, data):
self.written_data.append(data)
def ExpectRead(self, data):
self.read_data.append(data)
def Timeout(self, timeout_ms):
return timeout_ms if timeout_ms is not None else self.timeout_ms