PageRenderTime 105ms CodeModel.GetById 94ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/keyecho.py

https://bitbucket.org/prologic/circuits/
Python | 38 lines | 25 code | 12 blank | 1 comment | 2 complexity | ecf3bc3190dc93e1a115b6785ed688e9 MD5 | raw file
 1#!/usr/bin/env python
 2
 3from circuits.io import File
 4from circuits import Component
 5
 6import sys
 7import tty
 8import termios
 9from contextlib import contextmanager
10
11
12@contextmanager
13def restore_tty_settings(fd):
14    old = termios.tcgetattr(fd)
15    yield
16    termios.tcsetattr(fd, termios.TCSADRAIN, old)
17
18    
19class Echo(Component):
20
21    def read(self, data):
22        if data.lower() == "q":
23            raise SystemExit, 0
24        else:
25            sys.stdout.write(data)
26            sys.stdout.flush()
27
28
29def main():
30    stdin = File("/dev/stdin", "r", bufsize=1)
31    
32    with restore_tty_settings(stdin._fd):
33        tty.setraw(stdin._fd)
34        (Echo() + stdin).run()
35
36
37if __name__ == '__main__':
38    main()