I'm trying to write a python application and I need to hide some of the complexity in order to improve the user experience.
To make a long story short, I would like to have something that spawn the default shell selected by the user, reads every user input, and possibly executes some lines of code and changes something in the input before executing the command.
For example, a simple implementation would be:
import os
while 1:
command = raw_input("$ ")
words = command.split(' ')
for i in range(len(words)):
if words[i] == 'APPLE':
//EXECUTE SOME PYTHON SCRIPT
words[i] = 'BANANA'
newcommand = ' '.join(words)
os.system(newcommand)
The downside of this implementation is that the shell spawned is not a consistent process, and it doesn't have all the interactive features offered by bash/zsh.
I looked into the subprocess module, and noticed that I can produce an interactive shell with:
import subprocess
sp = subprocess.Popen(["/bin/zsh", "-i"])
sp.communicate()
but I couldn't figure out how to parse the user input and execute something real time. I would much appreciate some help with this.
Thanks in advance for the help.
EDIT: In case I did not make it clear, I'm not interested in parsing the output from the spawned shell, but I'm interested in parsing the user typed input, before that the input is executed.