Python: How do I capture "cooked mode" / cbreak output of subprocess? -
what i'm trying do: i'm attempting write wrapper in python modifies output of openbsd command-line program before displayed user. command-line program interactive, , it's execution indefinite, wrapper has able modify output in "realtime" produced program. user running wrapper has able interact command-line program.
the problem i'm having: script works far, except when command-line program goes cooked-mode (cbreak). user able interact, program output shown, , string replace works. but, when child/subprocess in cooked mode, no output @ all. type not shown, until end of script execution, on exit, dumps @ once. non-cooked mode output shows after type in cooked-mode, , cooked-mode info still cached until end.
what have far: have far wrapper:
#!/usr/local/bin/python import subprocess process = subprocess.popen(['./cooked.pl'], stdout=subprocess.pipe) while true: output = process.stdout.readline() if output == '' , process.poll() not none: break if output: str.replace(output,"[3]","---"); print output.strip() rc = process.poll() print "done.\n"
the actual program run older (1990) one; 1 referenced in script above drop-in substitution testing, written in perl. here source perl test script (cooked.pl):
#!/usr/bin/perl use strict; use term::readkey; $key; readmode 3; # cooked mode { while (not defined($key=readkey(-1))) { # no keypress } print "[3] got key $key\n"; } while($key ne "q"); readmode 0; # reset tty mode before exiting
my question summarized: need way wrap command-line program such can modify output before shown user, while allowing command-line program run indefinitely , allowing interaction. attempting python subprocess, running issues when command-line program in cooked-mode / cbreak; no output shown @ during cooked-mode on command-line program side.
thanks in advance help. open doing in language possibly. cannot make changes command-line program used in final script.
Comments
Post a Comment