added win_unicode_console module
[twitter-api-cdsw-solutions] / win_unicode_console / console.py
1
2 import code
3 import sys
4 import __main__
5
6
7 def print_banner():
8         print("Python {} on {}".format(sys.version, sys.platform))
9         print('Type "help", "copyright", "credits" or "license" for more information.')
10
11 class InteractiveConsole(code.InteractiveConsole):
12         # code.InteractiveConsole without banner
13         # exits on EOF
14         # also more robust treating of sys.ps1, sys.ps2
15         # prints prompt into stderr rather than stdout
16         # flushes sys.stderr and sys.stdout
17         
18         def __init__(self, locals=None, filename="<stdin>"):
19                 self.done = False
20                 super().__init__(locals, filename)
21         
22         def raw_input(self, prompt=""):
23                 sys.stderr.write(prompt)
24                 return input()
25         
26         def runcode(self, code):
27                 super().runcode(code)
28                 sys.stderr.flush()
29                 sys.stdout.flush()
30         
31         def interact(self):
32                 #sys.ps1 = "~>> "
33                 #sys.ps2 = "~.. "
34                 
35                 try:
36                         sys.ps1
37                 except AttributeError:
38                         sys.ps1 = ">>> "
39                 
40                 try:
41                         sys.ps2
42                 except AttributeError:
43                         sys.ps2 = "... "
44                 
45                 more = 0
46                 while not self.done:
47                         try:
48                                 if more:
49                                         try:
50                                                 prompt = sys.ps2
51                                         except AttributeError:
52                                                 prompt = ""
53                                 else:
54                                         try:
55                                                 prompt = sys.ps1
56                                         except AttributeError:
57                                                 prompt = ""
58                                 
59                                 try:
60                                         line = self.raw_input(prompt)
61                                 except EOFError:
62                                         self.on_EOF()
63                                 else:
64                                         more = self.push(line)
65                                 
66                         except KeyboardInterrupt:
67                                 self.write("\nKeyboardInterrupt\n")
68                                 self.resetbuffer()
69                                 more = 0
70         
71         def on_EOF(self):
72                 self.write("\n")
73                 # sys.exit()
74                 raise SystemExit from None
75
76
77 running_console = None
78
79 def enable():
80         global running_console
81         
82         if running_console is not None:
83                 raise RuntimeError("interactive console already running")
84         else:
85                 running_console = InteractiveConsole(__main__.__dict__) 
86                 running_console.interact() 
87
88 def disable():
89         global running_console
90         
91         if running_console is None:
92                 raise RuntimeError("interactive console is not running")
93         else:
94                 running_console.done = True
95                 running_console = None
96

Benjamin Mako Hill || Want to submit a patch?