OILS / demo / coproc.py View on Github | oilshell.org

36 lines, 24 significant
1#!/usr/bin/env python2
2"""
3coproc.py
4"""
5
6import sys
7
8
9def main(argv):
10 print >>sys.stderr, argv
11 try:
12 command = argv[1]
13 except IndexError:
14 command = 'upper'
15
16 if command == 'upper':
17 func = lambda x: x.upper()
18 else:
19 func = lambda x: x.lower()
20
21 while True:
22 line = sys.stdin.readline()
23 if not line:
24 print >>sys.stderr, 'DONE %s' % command
25 break
26 sys.stdout.write(func(line))
27 # If we don't do this, it hangs forever
28 sys.stdout.flush()
29
30
31if __name__ == '__main__':
32 try:
33 main(sys.argv)
34 except RuntimeError as e:
35 print >>sys.stderr, 'FATAL: %s' % e
36 sys.exit(1)