Pexpect might be useful for you: http://sourceforge.net/projects/pexpect/
Description from the webpage: "Pexpect is a Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. Pexpect is pure Python."
Answer from Filip Korling on Stack Overflow
» pip install cli-wrapper
Good example of CLI wrapper?
select - How to wrap a CLI program in Python (keeping the interactivity)? - Stack Overflow
subprocess - Help with wrapping a command line tool in Python - Stack Overflow
Using Python, what's an effective way to wrap a command line utility? - Software Engineering Stack Exchange
I'd like to write a python app that wraps an existing command line interface.
Does anyone know of good example apps to look at?
The CLI is fairly minimal but I am not quite sure how to approach this as I've mainly used Python for data analysis.
Thanks.
Edit (full example, from comment below):
Yes, so the CLI is for interacting with specific database/files. Basically I just use it to grab data, then I use/edit/analyze the data, and sometimes want to write back to the DB.
Examples:
mycli pull mydatabase (grab the database as csv for local use) mycli import new_data.csv (import new csv data into DB)
Right now I just do this with call commands in my Python scripts:
subprocess.call(['mycli', 'pull', 'mydatabase'])
I'd like to just make a simple Python Module w/ an API to do this instead.
from mycli import DbManager
db = DbManager()
db.pull('mydatabase')I can write this code now in a not DRY way. Basically just making a function for each thing the CLI can do.
I'd like to see an example of a larger application, so I can just understand how to best organize something like this in case it gets larger.
Pexpect might be useful for you: http://sourceforge.net/projects/pexpect/
Description from the webpage: "Pexpect is a Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. Pexpect is pure Python."
http://code.google.com/p/py-asterisk/
Introduction
The Python Asterisk package (codenamed py-Asterisk) is an attempt to produce high quality, well documented Python bindings for the Asterisk Manager API.
The eventual goal of the package is to allow rich specification of the Asterisk configuration in Python rather than in the quirky, unstructured, undocumented mess that we call the Asterisk configuration files.
Working Functionality
Python package implementing a manager client and event dispatcher. User-oriented command line interface to manager API.
You need to drop the stdout=subprocess.PIPE. Doing that disconnects the stdout of your process from Python's stdout and makes it retrievable using the Popen.communicate() function, like so:
import subprocess
spooky_path = 'ls'
cmd = [spooky_path, '-l']
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = process.communicate()[0]
print "Output:", output
process.wait()
print('Done')
To make it print directly you can use it without the stdout argument:
process = subprocess.Popen(cmd)
Or you can use the call function:
process = subprocess.call(cmd)
Try making your command into a single string:
cmd = 'spooky_path -a start -b end'
process = subprocess.Popen(cmd, shell=True)
» pip install pycliwrapper
Maybe you want something from Subprocess (MOTW).
I use code like this to make calls out to the shell:
from subprocess import Popen, PIPE
## shell out, prompt
def shell(args, input_=''):
''' uses subprocess pipes to call out to the shell.
args: args to the command
input: stdin
returns stdout, stderr
'''
p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate(input=input_)
return stdout, stderr
Does PExpect fits your needs?