-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinteractive.py
More file actions
61 lines (53 loc) · 1.61 KB
/
interactive.py
File metadata and controls
61 lines (53 loc) · 1.61 KB
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
import sys
import os
import code
from dataclasses import dataclass
# local imports
from constants import *
from myLogging import *
from exceptionHandler import handleCurrentException
import i18n
def prepareInteractive(reset=True):
print()
if reset:
if os.name == 'nt':
# clear the terminal
os.system('cls')
else:
# On linux & mac use ANSI Sequence for this
print('\033[2J\033[H')
HISTORY_SIZE = 1000
def getHistoryFilePath():
envVar = 'HOME'
if os.name == 'nt':
envVar = 'USERPROFILE'
d = os.getenv(envVar, None)
if d:
return os.path.join(d, ".wypp_history")
else:
return None
class TypecheckedInteractiveConsole(code.InteractiveConsole):
def showtraceback(self) -> None:
handleCurrentException(exit=False, removeFirstTb=True, file=sys.stdout)
def enterInteractive(userDefs: dict, checkTypes: bool, loadingFailed: bool):
for k, v in userDefs.items():
globals()[k] = v
print()
if checkTypes:
consoleClass = TypecheckedInteractiveConsole
else:
consoleClass = code.InteractiveConsole
historyFile = getHistoryFilePath()
try:
import readline
readline.parse_and_bind('tab: complete')
if historyFile and os.path.exists(historyFile):
readline.read_history_file(historyFile)
except:
pass
try:
consoleClass(locals=userDefs).interact(banner="", exitmsg='')
finally:
if readline and historyFile:
readline.set_history_length(HISTORY_SIZE)
readline.write_history_file(historyFile)