Jack Jansen | 1b3904b | 1997-06-20 16:22:07 +0000 | [diff] [blame^] | 1 | """quietconsole - A module to keep console I/O quiet but dump it when needed""" |
| 2 | import types |
| 3 | import sys |
| 4 | |
| 5 | class _PseudoStdin: |
| 6 | def __init__(self, stdouterr): |
| 7 | self.keep_stdin = sys.stdin |
| 8 | sys.stdin = self |
| 9 | self.keep_stdouterr = stdouterr |
| 10 | |
| 11 | def __del__(self): |
| 12 | self.keep_stdin = self.keep_stdouterr = None |
| 13 | |
| 14 | def _revert(self): |
| 15 | """Return to old state, with true stdio""" |
| 16 | if self.keep_stdin == None: |
| 17 | return |
| 18 | sys.stdin = self.keep_stdin |
| 19 | self.keep_stdin = None |
| 20 | self.keep_stdouterr._revert(1) |
| 21 | self.keep_stdouterr = None |
| 22 | |
| 23 | def read(self, *args): |
| 24 | self._revert() |
| 25 | return apply(sys.stdin.read, args) |
| 26 | |
| 27 | def readlines(self, *args): |
| 28 | self._revert() |
| 29 | return apply(sys.stdin.readlines, args) |
| 30 | |
| 31 | def readline(self, *args): |
| 32 | self._revert() |
| 33 | return apply(sys.stdin.readline, args) |
| 34 | |
| 35 | def close(self): |
| 36 | self._revert() |
| 37 | sys.stdin.close() |
| 38 | |
| 39 | class _PseudoStdouterr: |
| 40 | def __init__(self): |
| 41 | self.keep_stdout = sys.stdout |
| 42 | self.keep_stderr = sys.stderr |
| 43 | sys.stdout = sys.stderr = self |
| 44 | self.data = [] |
| 45 | |
| 46 | def __del__(self): |
| 47 | self.keep_stdout = self.keep_stderr = None |
| 48 | |
| 49 | def _revert(self, dumpdata=0): |
| 50 | if self.keep_stdout == None: |
| 51 | return |
| 52 | sys.stdout = self.keep_stdout |
| 53 | sys.stderr = self.keep_stderr |
| 54 | sys.keep_stdout = self.keep_stderr = None |
| 55 | if dumpdata and self.data: |
| 56 | for d in self.data: |
| 57 | sys.stdout.write(d) |
| 58 | self.data = None |
| 59 | |
| 60 | def write(self, arg): |
| 61 | self.data.append(arg) |
| 62 | |
| 63 | def writelines(self, arg): |
| 64 | for d in arg: |
| 65 | self.data.append(arg) |
| 66 | |
| 67 | def close(self): |
| 68 | self.keep_stdout = self.keep_stderr = self.data = None |
| 69 | |
| 70 | beenhere = 0 |
| 71 | |
| 72 | def install(): |
| 73 | global beenhere |
| 74 | if beenhere: |
| 75 | return |
| 76 | beenhere = 1 |
| 77 | # There's no point in re-installing if the console has been active |
| 78 | obj = _PseudoStdouterr() |
| 79 | _PseudoStdin(obj) |
| 80 | # No need to keep the objects, they're saved in sys.std{in,out,err} |
| 81 | |
| 82 | def revert(): |
| 83 | if type(sys.stdin) == types.FileType: |
| 84 | return # Not installed |
| 85 | sys.stdin._revert() |
| 86 | |
| 87 | def _test(): |
| 88 | import time |
| 89 | install() |
| 90 | print "You will not see this yet" |
| 91 | time.sleep(1) |
| 92 | print "You will not see this yet" |
| 93 | time.sleep(1) |
| 94 | print "You will not see this yet" |
| 95 | time.sleep(1) |
| 96 | print "You will not see this yet" |
| 97 | time.sleep(1) |
| 98 | print "You will not see this yet" |
| 99 | time.sleep(1) |
| 100 | print "5 seconds have passed, now you may type something" |
| 101 | rv = sys.stdin.readline() |
| 102 | print "You typed", rv |
| 103 | |
| 104 | if __name__ == '__main__': |
| 105 | _test() |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |