[CLN] clean up around the example scripts

This commit is contained in:
Benedek Racz 2019-09-06 11:52:11 +02:00
parent 813a462475
commit cd968844cd
6 changed files with 112 additions and 8 deletions

View File

@ -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**

19
examples/README.md Normal file
View File

@ -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<i></i>.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.

47
examples/cmd_wrapper.py Normal file
View File

@ -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

View File

@ -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='')
print('Hello ' + str(name), end='')

30
examples/hello_wexpect.py Normal file
View File

@ -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()

View File

@ -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='')