MCPcopy Create free account
hub / github.com/pexpect/pexpect / compile_pattern_list

Method compile_pattern_list

pexpect/spawnbase.py:205–252  ·  view source on GitHub ↗

This compiles a pattern-string or a list of pattern-strings. Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or a list of those. Patterns may also be None which results in an empty list (you might do this if waiting for an EOF or TIMEOUT condition without ex

(self, patterns)

Source from the content-addressed store, hash-verified

203 )
204
205 def compile_pattern_list(self, patterns):
206 '''This compiles a pattern-string or a list of pattern-strings.
207 Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or a list of
208 those. Patterns may also be None which results in an empty list (you
209 might do this if waiting for an EOF or TIMEOUT condition without
210 expecting any pattern).
211
212 This is used by expect() when calling expect_list(). Thus expect() is
213 nothing more than::
214
215 cpl = self.compile_pattern_list(pl)
216 return self.expect_list(cpl, timeout)
217
218 If you are using expect() within a loop it may be more
219 efficient to compile the patterns first and then call expect_list().
220 This avoid calls in a loop to compile_pattern_list()::
221
222 cpl = self.compile_pattern_list(my_pattern)
223 while some_condition:
224 ...
225 i = self.expect_list(cpl, timeout)
226 ...
227 '''
228
229 if patterns is None:
230 return []
231 if not isinstance(patterns, list):
232 patterns = [patterns]
233
234 # Allow dot to match \n
235 compile_flags = re.DOTALL
236 if self.ignorecase:
237 compile_flags = compile_flags | re.IGNORECASE
238 compiled_pattern_list = []
239 for idx, p in enumerate(patterns):
240 if isinstance(p, self.allowed_string_types):
241 p = self._coerce_expect_string(p)
242 compiled_pattern_list.append(re.compile(p, compile_flags))
243 elif p is EOF:
244 compiled_pattern_list.append(EOF)
245 elif p is TIMEOUT:
246 compiled_pattern_list.append(TIMEOUT)
247 elif isinstance(p, type(re.compile(''))):
248 p = self._coerce_expect_re(p)
249 compiled_pattern_list.append(p)
250 else:
251 self._pattern_type_err(p)
252 return compiled_pattern_list
253
254 def expect(self, pattern, timeout=-1, searchwindowsize=-1, async_=False, **kw):
255 '''This seeks through the stream until a pattern is matched. The

Callers 1

expectMethod · 0.95

Calls 3

_coerce_expect_stringMethod · 0.95
_coerce_expect_reMethod · 0.95
_pattern_type_errMethod · 0.95

Tested by

no test coverage detected