Read until one from a list of a regular expressions matches. The first argument is a list of regular expressions, either compiled (re.Pattern instances) or uncompiled (strings). The optional second argument is a timeout, in seconds; default is no timeout.
(self, list, timeout=None)
| 586 | sys.stdout.flush() |
| 587 | |
| 588 | def expect(self, list, timeout=None): |
| 589 | """Read until one from a list of a regular expressions matches. |
| 590 | |
| 591 | The first argument is a list of regular expressions, either |
| 592 | compiled (re.Pattern instances) or uncompiled (strings). |
| 593 | The optional second argument is a timeout, in seconds; default |
| 594 | is no timeout. |
| 595 | |
| 596 | Return a tuple of three items: the index in the list of the |
| 597 | first regular expression that matches; the re.Match object |
| 598 | returned; and the text read up till and including the match. |
| 599 | |
| 600 | If EOF is read and no text was read, raise EOFError. |
| 601 | Otherwise, when nothing matches, return (-1, None, text) where |
| 602 | text is the text received so far (may be the empty string if a |
| 603 | timeout happened). |
| 604 | |
| 605 | If a regular expression ends with a greedy match (e.g. '.*') |
| 606 | or if more than one expression can match the same input, the |
| 607 | results are undeterministic, and may depend on the I/O timing. |
| 608 | |
| 609 | """ |
| 610 | re = None |
| 611 | list = list[:] |
| 612 | indices = range(len(list)) |
| 613 | for i in indices: |
| 614 | if not hasattr(list[i], "search"): |
| 615 | if not re: import re |
| 616 | list[i] = re.compile(list[i]) |
| 617 | if timeout is not None: |
| 618 | deadline = _time() + timeout |
| 619 | with _TelnetSelector() as selector: |
| 620 | selector.register(self, selectors.EVENT_READ) |
| 621 | while not self.eof: |
| 622 | self.process_rawq() |
| 623 | for i in indices: |
| 624 | m = list[i].search(self.cookedq) |
| 625 | if m: |
| 626 | e = m.end() |
| 627 | text = self.cookedq[:e] |
| 628 | self.cookedq = self.cookedq[e:] |
| 629 | return (i, m, text) |
| 630 | if timeout is not None: |
| 631 | ready = selector.select(timeout) |
| 632 | timeout = deadline - _time() |
| 633 | if not ready: |
| 634 | if timeout < 0: |
| 635 | break |
| 636 | else: |
| 637 | continue |
| 638 | self.fill_rawq() |
| 639 | text = self.read_very_lazy() |
| 640 | if not text and self.eof: |
| 641 | raise EOFError |
| 642 | return (-1, None, text) |
| 643 | |
| 644 | def __enter__(self): |
| 645 | return self |
nothing calls this directly
no test coverage detected