aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/deprecated/python/win-unicode-console/win_unicode_console/console.py
blob: f5da52ca885d0c0428636bc30aa009bd3a3ee511 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from __future__ import print_function # PY2

import __main__
import code
import sys

from .info import PY2


def print_banner(file=sys.stderr):
	print("Python {} on {}".format(sys.version, sys.platform), file=file)
	print('Type "help", "copyright", "credits" or "license" for more information.', file=file)

# PY3 # class InteractiveConsole(code.InteractiveConsole):
class InteractiveConsole(code.InteractiveConsole, object):
	# code.InteractiveConsole without banner
	# exits on EOF
	# also more robust treating of sys.ps1, sys.ps2
	# prints prompt into stderr rather than stdout
	# flushes sys.stderr and sys.stdout
	
	def __init__(self, locals=None, filename="<stdin>"):
		self.done = False
		# PY3 # super().__init__(locals, filename)
		super(InteractiveConsole, self).__init__(locals, filename)
	
	def raw_input(self, prompt=""):
		sys.stderr.write(prompt)
		if PY2:
			return raw_input()
		else:
			return input()
	
	def runcode(self, code):
		# PY3 # super().runcode(code)
		super(InteractiveConsole, self).runcode(code)
		sys.stderr.flush()
		sys.stdout.flush()
	
	def interact(self):
		#sys.ps1 = "~>> "
		#sys.ps2 = "~.. "
		
		try:
			sys.ps1
		except AttributeError:
			sys.ps1 = ">>> "
		
		try:
			sys.ps2
		except AttributeError:
			sys.ps2 = "... "
		
		more = 0
		while not self.done:
			try:
				if more:
					try:
						prompt = sys.ps2
					except AttributeError:
						prompt = ""
				else:
					try:
						prompt = sys.ps1
					except AttributeError:
						prompt = ""
				
				try:
					line = self.raw_input(prompt)
				except EOFError:
					self.on_EOF()
				else:
					more = self.push(line)
				
			except KeyboardInterrupt:
				self.write("\nKeyboardInterrupt\n")
				self.resetbuffer()
				more = 0
	
	def on_EOF(self):
		self.write("\n")
		# PY3 # raise SystemExit from None
		raise SystemExit


running_console = None

def enable():
	global running_console
	
	if running_console is not None:
		raise RuntimeError("interactive console already running")
	else:
		running_console = InteractiveConsole(__main__.__dict__) 
		running_console.interact() 

def disable():
	global running_console
	
	if running_console is None:
		raise RuntimeError("interactive console is not running")
	else:
		running_console.done = True
		running_console = None