From cd968844cd42fd658a9e2ec5c634f3fb5001ea65 Mon Sep 17 00:00:00 2001 From: Benedek Racz Date: Fri, 6 Sep 2019 11:52:11 +0200 Subject: [PATCH] [CLN] clean up around the example scripts --- README.md | 7 +++--- examples/README.md | 19 ++++++++++++++++ examples/cmd_wrapper.py | 47 +++++++++++++++++++++++++++++++++++++++ examples/foo.py | 10 +++++++-- examples/hello_wexpect.py | 30 +++++++++++++++++++++++++ examples/python.py | 7 +++--- 6 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 examples/README.md create mode 100644 examples/cmd_wrapper.py create mode 100644 examples/hello_wexpect.py diff --git a/README.md b/README.md index 264d1e8..02b0459 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # **wexpect** [![Build status](https://ci.appveyor.com/api/projects/status/tbji72d5s0tagrt9?svg=true)](https://ci.appveyor.com/project/raczben/wexpect) +[![codecov](https://codecov.io/gh/raczben/wexpect/branch/master/graph/badge.svg)](https://codecov.io/gh/raczben/wexpect) *Wexpect* is a Windows variant of [pexpect](https://pexpect.readthedocs.io/en/stable/). @@ -9,9 +10,9 @@ them automatically. ## You need wexpect if... - - you want to control any windows application from python script. - - you want to write test-automation script for a windows application. - - you want to post-process some application's standard output. + - you want to control any windows console application from python script. + - you want to write test-automation script for a windows console application. + - you want to automate your job by controlling multiple application parallel, synchoronusly. ## **Install** diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..a5e5ee2 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,19 @@ +# **wexpect examples** + +There are several example usage of wexpect. Choose one as template of your application. + +## hello_wexpect + +[hello_wexpect](./hello_wexpect.py) is the simplest example. It starts a windows command interpreter +(aka. cmd) lists the current directory and exits. + +## python + +[python](./python.py) is a full custom example code. This example script runs [foo](./foo.py) python +program, and communicates with it. For better understanding please run natively foo.py first, which +is a very basic stdio handler script. + +## cmd_wrapper + +[cmd_wrapper](./cmd_wrapper.py) is a simple wrapper around the cmd windows command interpreter. It +waits for commands executes them in the spawned cmd, and prints the results. diff --git a/examples/cmd_wrapper.py b/examples/cmd_wrapper.py new file mode 100644 index 0000000..acbc20b --- /dev/null +++ b/examples/cmd_wrapper.py @@ -0,0 +1,47 @@ +# A simple example code for wexpect + +from __future__ import print_function + +import sys +import os +import re + +here = os.path.dirname(os.path.abspath(__file__)) +wexpectPath = os.path.dirname(here) + +import wexpect + +# Path of cmd executable: +cmd_exe = 'cmd' +# The prompt should be more sophisticated than just a '>'. +cmdPrompt = re.compile('[A-Z]\:.+>') + +# Start the child process +p = wexpect.spawn(cmd_exe) + +# Wait for prompt +p.expect(cmdPrompt, timeout = 5) + +# print the texts +print(p.before, end='') +print(p.match.group(0), end='') + + +while True: + + # Wait and run a command. + command = input() + p.sendline(command) + + try: + # Wait for prompt + p.expect(cmdPrompt) + + # print the texts + print(p.before, end='') + print(p.match.group(0), end='') + + except wexpect.EOF: + # The program has exited + print('The program has exied... BY!') + break diff --git a/examples/foo.py b/examples/foo.py index a3e8536..44862b0 100644 --- a/examples/foo.py +++ b/examples/foo.py @@ -1,13 +1,19 @@ -from __future__ import print_function +''' +This is is a very basic stdio handler script. This is used by python.py example. +''' + import time +# Read an integer from the user: print('Give a small integer: ', end='') num = input() +# Wait the given time for i in range(int(num)): print('waiter ' + str(i)) time.sleep(0.2) +# Ask the name of the user to say hello. print('Give your name: ', end='') name = input() -print('Hello ' + str(name), end='') \ No newline at end of file +print('Hello ' + str(name), end='') diff --git a/examples/hello_wexpect.py b/examples/hello_wexpect.py new file mode 100644 index 0000000..c6ce839 --- /dev/null +++ b/examples/hello_wexpect.py @@ -0,0 +1,30 @@ +''' +This is the simplest example. It starts a windows command interpreter (aka. cmd) lists the current +directory and exits. +''' + +import wexpect + +# Start cmd as child process +child = wexpect.spawn('cmd.exe') + +# Wait for prompt when cmd becomes ready. +child.expect('>') + +# Prints the cmd's start message +print(child.before) + +# run list directory command +child.sendline('ls') + +# Waiting for prompt +child.expect('>') + +# Prints content of the directory +print(child.before) + +# Exit from cmd +child.sendline('exit') + +# Waiting for cmd termination. +child.wait() diff --git a/examples/python.py b/examples/python.py index 9bc899c..efbdf9d 100644 --- a/examples/python.py +++ b/examples/python.py @@ -1,6 +1,8 @@ -# A simple example code for wexpect written in python-2.7 +''' +This example script runs foo python program, and communicates with it. For better understanding +please run natively foo.py first, which is a very basic stdio handler script. +''' -from __future__ import print_function import sys import wexpect import os @@ -35,4 +37,3 @@ p.wait() # print the texts print(p.read(), end='') -