-
Notifications
You must be signed in to change notification settings - Fork 0
/
pythonrc.symlink
160 lines (124 loc) · 3.98 KB
/
pythonrc.symlink
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
This file is executed when the Python interactive shell is started.
$PYTHONSTARTUP must be in the environment and point to this file.
"""
#######################
# IMPORTS & CONSTANTS #
#######################
import imp
import os
import sys
import re
import threading
import time
VIRTUAL_ENV = os.environ.get("VIRTUAL_ENV", None)
HOME = VIRTUAL_ENV or os.environ["HOME"]
#################################
# SAVE & RESTORE HISTORY STATES #
#################################
try:
import readline
except ImportError:
pass
else:
##################
# TAB COMPLETION #
##################
import rlcompleter
readline.parse_and_bind("TAB: complete")
######################
# PERSISTENT HISTORY #
######################
# Use separate history files for each virtual environment.
HISTFILE = os.path.join(HOME, ".pyhistory")
# Read the existing history if there is one.
if os.path.exists(HISTFILE):
try:
readline.read_history_file(HISTFILE)
except:
# If there was a problem reading the history file then it may have
# become corrupted, so we just delete it.
os.remove(HISTFILE)
# Set maximum number of commands written to the history file.
readline.set_history_length(256)
def savehist():
try:
readline.write_history_file(HISTFILE)
except NameError:
pass
except Exception as err:
print("Unable to save history file due to the following error: %s" % err)
# Register the ``savehist`` function to run when the user exits the shell.
import atexit
atexit.register(savehist)
#################
# COLOR SUPPORT #
#################
class TermColors(dict):
"""Gives easy access to ANSI color codes. Attempts to fall back to no color
for certain TERM values. (Mostly stolen from IPython.)"""
COLOR_TEMPLATES = (
("Black", "0;30"),
("Red", "0;31"),
("Green", "0;32"),
("Brown", "0;33"),
("Blue", "0;34"),
("Purple", "0;35"),
("Cyan", "0;36"),
("LightGray", "0;37"),
("DarkGray", "1;30"),
("LightRed", "1;31"),
("LightGreen", "1;32"),
("Yellow", "1;33"),
("LightBlue", "1;34"),
("LightPurple", "1;35"),
("LightCyan", "1;36"),
("White", "1;37"),
("Normal", "0"),
)
NoColor = ""
_base = "\001\033[%sm\002"
def __init__(self):
if os.environ.get("TERM") in (
"xterm-color",
"xterm-256color",
"linux",
"screen",
"screen-256color",
"screen-bce",
):
self.update(dict([(k, self._base % v) for k, v in self.COLOR_TEMPLATES]))
else:
self.update(dict([(k, self.NoColor) for k, v in self.COLOR_TEMPLATES]))
_c = TermColors()
################################
# PRETTY PRINT OUTPUT & ERRORS #
################################
# Make the prompts colorful.
sys.ps1 = "%s>>>%s" % (_c['LightGreen'], _c['Normal'])
sys.ps2 = "%s...%s" % (_c['LightPurple'], _c['Normal'])
# Enable pretty printing for STDOUT
def my_displayhook(value):
if value is not None:
# TODO: What's the point of this anyway?
try:
import __builtin__
__builtin__._ = value
except ImportError:
__builtins__._ = value
import pprint
pprint.pprint(value)
del pprint
sys.displayhook = my_displayhook
# Make errors and tracebacks stand out a bit more.
def my_excepthook(type, value, tb):
sys.stderr.write(_c["Yellow"])
import traceback
output = traceback.print_exception(type, value, tb)
del traceback
sys.stderr.write(_c["Normal"])
# NOTE: There is a bug (?) in Python 3, where a trailing color marker that's
# written to STDERR or STDOUT by itself does not color the subsequent lines.
# We work around this by manually calling ``flush`` afterwards.
sys.stderr.flush()
sys.excepthook = my_excepthook