summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Kluyver <takowl@gmail.com>2014-06-06 18:00:20 -0700
committerThomas Kluyver <takowl@gmail.com>2014-09-21 12:06:50 -0700
commit418ae537e9f43f4cf90d897cdd9a80bcb4a5f9f5 (patch)
tree7cf16129cd26b4f6b24b05bea017380bae6360ab
parentf3a6b696baca0b1f4599a8a562b1884a94e61393 (diff)
downloadpexpect-418ae537e9f43f4cf90d897cdd9a80bcb4a5f9f5.tar.gz
Initial stab at asyncio integration
-rw-r--r--pexpect/async.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/pexpect/async.py b/pexpect/async.py
new file mode 100644
index 0000000..87c9a1c
--- /dev/null
+++ b/pexpect/async.py
@@ -0,0 +1,72 @@
+import asyncio
+
+from pexpect import EOF, searcher_re
+
+@asyncio.coroutine
+def expect_async(spawn, pattern):
+ searcher = searcher_re(spawn.compile_pattern_list(pattern))
+
+ def protocol_factory():
+ return PatternWaiter(spawn, searcher)
+ transport, pw = yield from asyncio.get_event_loop()\
+ .connect_read_pipe(protocol_factory, spawn)
+
+ return (yield from pw.fut)
+
+class PatternWaiter(asyncio.Protocol):
+ def __init__(self, spawn, searcher):
+ self.spawn = spawn
+ self.searcher = searcher
+ self.fut = asyncio.Future()
+
+ def found(self, result):
+ if not self.fut.done():
+ self.fut.set_result(result)
+
+ def error(self, exc):
+ if not self.fut.done():
+ self.fut.set_exception(exc)
+
+ def data_received(self, data):
+ spawn = self.spawn
+ s = spawn._coerce_read_string(data)
+ spawn._log(s, 'read')
+
+ searcher = self.searcher
+ try:
+ incoming = spawn.buffer + s
+ freshlen = len(s)
+ index = searcher.search(incoming, freshlen)
+ 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
+ self.found(index)
+ except Exception as e:
+ spawn.before = incoming
+ spawn.after = None
+ spawn.match = None
+ spawn.match_index = None
+ self.error(e)
+
+ spawn.buffer = incoming
+
+ def eof_received(self):
+ spawn = self.spawn
+ 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
+ self.found(index)
+ else:
+ spawn.match = None
+ spawn.match_index = None
+ self.error(EOF(str(spawn)))
+
+ \ No newline at end of file