-
Notifications
You must be signed in to change notification settings - Fork 0
/
.pythonstartup.py
executable file
·79 lines (58 loc) · 1.82 KB
/
.pythonstartup.py
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
startenv = set(globals())
import atexit
import os
import sys
COLORTERMS = [
'xterm', 'xterm-color', 'xterm-256color', 'vt100',
]
# Define aliases so JSON can be pasted straight in.
true = True
false = False
null = None
try:
import readline
except ImportError:
readline = None
else:
import rlcompleter
def set_ps1():
if os.environ.get('TERM') in COLORTERMS and sys.platform != 'darwin':
# Tell terminal which chars are non-visible,
# so it can keep accurate track of line lengths.
# This doesn't seem to work on OSX.
nonvis = '\001'
vis = '\002'
prefix = '\x1b['
bold_yellow = prefix + '1;33m'
reset = prefix + '0m'
sys.ps1 = nonvis + bold_yellow + vis + '>>> ' + nonvis + reset
sys.ps2 = nonvis + bold_yellow + vis + '... ' + nonvis + reset
def enable_tab_completion():
if sys.platform == "darwin":
readline.parse_and_bind('bind ^I rl_complete')
else:
readline.parse_and_bind('tab: complete')
print(".pythonstartup.py: Bound readline 'complete' to [tab]")
def read_history_file():
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
readline.read_history_file(histfile)
print(f".pythonstartup.py: Read history ({histfile})")
except IOError:
pass
return histfile
def write_history_atexit(histfile):
atexit.register(readline.write_history_file, histfile)
print(f".pythonstartup.py: Registered to write history file atexit")
def delete_new_globals(startenv):
for key in set(globals()) - startenv:
del globals()[key]
def main():
set_ps1()
if readline:
enable_tab_completion()
histfile = read_history_file()
write_history_atexit(histfile)
delete_new_globals(startenv)
if __name__ == '__main__':
main()