in automation/tinc/main/ext/pexpect.py [0:0]
def expect_list(self, pattern_list, timeout = -1, searchwindowsize = -1):
"""This takes a list of compiled regular expressions and returns
the index into the pattern_list that matched the child output.
The list may also contain EOF or TIMEOUT (which are not
compiled regular expressions). This method is similar to
the expect() method except that expect_list() does not
recompile the pattern list on every call.
This may help if you are trying to optimize for speed, otherwise
just use the expect() method. This is called by expect().
If timeout==-1 then the self.timeout value is used.
If searchwindowsize==-1 then the self.searchwindowsize value is used.
"""
self.patterns = pattern_list
if timeout == -1:
timeout = self.timeout
if timeout is not None:
end_time = time.time() + timeout
if searchwindowsize == -1:
searchwindowsize = self.searchwindowsize
try:
incoming = self.buffer
while True: # Keep reading until exception or return.
# Sequence through the list of patterns looking for a match.
first_match = -1
for cre in pattern_list:
if cre is EOF or cre is TIMEOUT:
continue # The patterns for PexpectExceptions are handled differently.
if searchwindowsize is None: # search everything
match = cre.search(incoming)
else:
startpos = max(0, len(incoming) - searchwindowsize)
match = cre.search(incoming, startpos)
if match is None:
continue
if first_match > match.start() or first_match == -1:
first_match = match.start()
self.match = match
self.match_index = pattern_list.index(cre)
if first_match > -1:
self.buffer = incoming[self.match.end() : ]
self.before = incoming[ : self.match.start()]
self.after = incoming[self.match.start() : self.match.end()]
return self.match_index
# No match at this point
if timeout < 0 and timeout is not None:
raise TIMEOUT ('Timeout exceeded in expect_list().')
# Still have time left, so read more data
c = self.read_nonblocking (self.maxread, timeout)
time.sleep (0.0001)
incoming = incoming + c
if timeout is not None:
timeout = end_time - time.time()
except EOF, e:
self.buffer = ''
self.before = incoming
self.after = EOF
if EOF in pattern_list:
self.match = EOF
self.match_index = pattern_list.index(EOF)
return self.match_index
else:
self.match = None
self.match_index = None
raise EOF (str(e) + '\n' + str(self))
except TIMEOUT, e:
self.before = incoming
self.after = TIMEOUT
if TIMEOUT in pattern_list:
self.match = TIMEOUT
self.match_index = pattern_list.index(TIMEOUT)
return self.match_index
else:
self.match = None
self.match_index = None
raise TIMEOUT (str(e) + '\n' + str(self))
except Exception:
self.before = incoming
self.after = None
self.match = None
self.match_index = None
raise