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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
import time
class Expecter(object):
def __init__(self, spawn, searcher, searchwindowsize=-1):
self.spawn = spawn
self.searcher = searcher
if searchwindowsize == -1:
searchwindowsize = spawn.searchwindowsize
self.searchwindowsize = searchwindowsize
def new_data(self, data):
spawn = self.spawn
searcher = self.searcher
incoming = spawn.buffer + data
freshlen = len(data)
index = searcher.search(incoming, freshlen, self.searchwindowsize)
if index >= 0:
spawn.buffer = incoming[searcher.end:]
spawn.before = incoming[: searcher.start]
spawn.after = incoming[searcher.start: searcher.end]
spawn.match = searcher.match
spawn.match_index = index
# Found a match
return index
spawn.buffer = incoming
def eof(self, err=None):
spawn = self.spawn
from . import EOF
spawn.before = spawn.buffer
spawn.buffer = spawn.string_type()
spawn.after = EOF
index = self.searcher.eof_index
if index >= 0:
spawn.match = EOF
spawn.match_index = index
return index
else:
spawn.match = None
spawn.match_index = None
msg = str(spawn)
if err is not None:
msg = str(err) + '\n' + msg
raise EOF(msg)
def timeout(self, err=None):
spawn = self.spawn
from . import TIMEOUT
spawn.before = spawn.buffer
spawn.after = TIMEOUT
index = self.searcher.timeout_index
if index >= 0:
spawn.match = TIMEOUT
spawn.match_index = index
return index
else:
spawn.match = None
spawn.match_index = None
msg = str(spawn)
if err is not None:
msg = str(err) + '\n' + msg
raise TIMEOUT(msg)
def errored(self):
spawn = self.spawn
spawn.before = spawn.buffer
spawn.after = None
spawn.match = None
spawn.match_index = None
def expect_loop(self, timeout=-1):
"""Blocking expect"""
spawn = self.spawn
from . import EOF, TIMEOUT
if timeout == -1:
timeout = self.spawn.timeout
if timeout is not None:
end_time = time.time() + timeout
try:
incoming = spawn.buffer
spawn.buffer = spawn.string_type() # Treat buffer as new data
while True:
idx = self.new_data(incoming)
# Keep reading until exception or return.
if idx is not None:
return idx
# No match at this point
if (timeout is not None) and (timeout < 0):
return self.timeout()
# Still have time left, so read more data
incoming = spawn.read_nonblocking(spawn.maxread, timeout)
time.sleep(0.0001)
if timeout is not None:
timeout = end_time - time.time()
except EOF as e:
return self.eof(e)
except TIMEOUT as e:
return self.timeout(e)
except:
self.errored()
raise
|