diff options
author | Thomas Kluyver <takowl@gmail.com> | 2014-10-13 12:21:37 -0700 |
---|---|---|
committer | Thomas Kluyver <takowl@gmail.com> | 2014-10-13 12:21:37 -0700 |
commit | 184f9c4c6fb9e7033755449812b6532843e356a9 (patch) | |
tree | 8cf4f33c4e3ed2553aa4994affc76e763570b958 | |
parent | 170d52a251b30f528fc35fecb8af9d14c775e96a (diff) | |
parent | 1eb3e95da705b2ae5a44ef113edfa61ad681bd94 (diff) | |
download | pexpect-184f9c4c6fb9e7033755449812b6532843e356a9.tar.gz |
Merge pull request #124 from h2suzuki/run-with-tuple-list-of-events
Modify run() to allow a tuple list of events.
-rw-r--r-- | pexpect/__init__.py | 34 | ||||
-rwxr-xr-x | tests/test_run.py | 8 |
2 files changed, 32 insertions, 10 deletions
diff --git a/pexpect/__init__.py b/pexpect/__init__.py index f070067..8e2b21e 100644 --- a/pexpect/__init__.py +++ b/pexpect/__init__.py @@ -198,18 +198,29 @@ def run(command, timeout=-1, withexitstatus=False, events=None, run("mencoder dvd://1 -o video.avi -oac copy -ovc copy", events={TIMEOUT:print_ticks}, timeout=5) - The 'events' argument should be a dictionary of patterns and responses. - Whenever one of the patterns is seen in the command out run() will send the - associated response string. Note that you should put newlines in your - string if Enter is necessary. The responses may also contain callback - functions. Any callback is function that takes a dictionary as an argument. + The 'events' argument should be either a dictionary or a tuple list that + contains patterns and responses. Whenever one of the patterns is seen + in the command output, run() will send the associated response string. + So, run() in the above example can be also written as: + + run("mencoder dvd://1 -o video.avi -oac copy -ovc copy", + events=[(TIMEOUT,print_ticks)], timeout=5) + + Use a tuple list for events if the command output requires a delicate + control over what pattern should be matched, since the tuple list is passed + to pexpect() as its pattern list, with the order of patterns preserved. + + Note that you should put newlines in your string if Enter is necessary. + + Like the example above, the responses may also contain callback functions. + Any callback is a function that takes a dictionary as an argument. The dictionary contains all the locals from the run() function, so you can access the child spawn object or any other variable defined in run() (event_count, child, and extra_args are the most useful). A callback may - return True to stop the current run process otherwise run() continues until - the next event. A callback may also return a string which will be sent to - the child. 'extra_args' is not used by directly run(). It provides a way to - pass data to a callback function through run() through the locals + return True to stop the current run process. Otherwise run() continues + until the next event. A callback may also return a string which will be + sent to the child. 'extra_args' is not used by directly run(). It provides + a way to pass data to a callback function through run() through the locals dictionary passed to a callback. ''' return _run(command, timeout=timeout, withexitstatus=withexitstatus, @@ -235,7 +246,10 @@ def _run(command, timeout, withexitstatus, events, extra_args, logfile, cwd, else: child = _spawn(command, timeout=timeout, maxread=2000, logfile=logfile, cwd=cwd, env=env, **kwargs) - if events is not None: + if isinstance(events, list): + patterns= [x for x,y in events] + responses = [y for x,y in events] + elif isinstance(events, dict): patterns = list(events.keys()) responses = list(events.values()) else: diff --git a/tests/test_run.py b/tests/test_run.py index 814b70a..077f857 100755 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -64,6 +64,14 @@ class RunFuncTestCase(PexpectTestCase.PexpectTestCase): withexitstatus=1) assert exitstatus != 0 + def test_run_tuple_list (self): + events = [('abc\r\n.*[$#]','echo "def"\n'), + ('def\r\n.*[$#]','exit\n'), + ('[$#] ','echo "abc"\n')] + (data, exitstatus) = pexpect.run('bash', withexitstatus=1, + events=events, timeout=10) + assert exitstatus == 0 + class RunUnicodeFuncTestCase(RunFuncTestCase): runfunc = staticmethod(pexpect.runu) cr = b'\r'.decode('ascii') |