-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonrc.py
More file actions
210 lines (167 loc) · 6.04 KB
/
pythonrc.py
File metadata and controls
210 lines (167 loc) · 6.04 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# -*- coding: utf-8 -*-
"""
This file is executed when the Python interactive shell is started if
$PYTHONSTARTUP is in your environment and points to this file. It's just
regular Python commands, so do what you will. Your ~/.inputrc file can greatly
complement this file.
"""
# Imports we need
import sys
import os
import readline, rlcompleter
import atexit
import pprint
from tempfile import mkstemp
from code import InteractiveConsole
# Imports we want
import datetime
import pdb
# Enable Tab Completion
#######################
readline.parse_and_bind("tab: complete")
# Enable a History
##################
HISTFILE = "{}/.pyhistory".format(os.environ["HOME"])
# Read the existing history if there is one
if os.path.exists(HISTFILE):
readline.read_history_file(HISTFILE)
# Set maximum number of items that will be written to the history file
readline.set_history_length(2000)
def save_history():
readline.write_history_file(HISTFILE)
# Save history to history file
atexit.register(save_history)
# Enable Pretty Printing for stdout
###################################
def my_displayhook(value):
if value is not None:
if sys.version_info >= (3, 0):
import builtins
else:
try:
import __builtin__
__builtin__._ = value
except ImportError:
__builtins__._ = value
pprint.pprint(value)
sys.displayhook = my_displayhook
# 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.)
Remember that if you are using Solarized Colors, some may not display.
"""
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 = ''
# This seems to work in OSX & Ubuntu
_base = '\033[{0}m'
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.format(v)) for k, v in self.COLOR_TEMPLATES]))
else:
self.update(dict([(k, self.NoColor) for k, v in self.COLOR_TEMPLATES]))
_c = TermColors()
# Enable Color Prompts
######################
# Works in OSX & Ubuntu
sys.ps1 = '\001{0}\002>>> \001{1}\002'.format(_c['Green'], _c['Normal'])
sys.ps2 = '\001{0}\002... \001{1}\002'.format(_c['Red'], _c['Normal'])
# Welcome Message
#################
WELCOME = """\n{0}You've got tab completion, history, pretty printing, and color.
History will be saved in {1} when you exit.
{2}Type \e to get an external editor.
{3}""".format(_c['Cyan'], HISTFILE, _c['Brown'], _c['Normal'])
atexit.register(lambda: sys.stdout.write("""{0}Hope you've enjoyed your Python programming!\n\n""".format(_c['Normal'])))
# Django Helpers
################
def SECRET_KEY():
"Generates a new SECRET_KEY that can be used in a project settings file."
from random import choice
return ''.join(
[choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')
for i in range(50)])
# # If we're working with a Django project, set up the environment
# if 'DJANGO_SETTINGS_MODULE' in os.environ:
# from django.db.models.loading import get_models
# from django.test.client import Client
# from django.test.utils import setup_test_environment, teardown_test_environment
# from django.conf import settings as S
#
# class DjangoModels(object):
# """Loop through all the models in INSTALLED_APPS and import them."""
# def __init__(self):
# for m in get_models():
# setattr(self, m.__name__, m)
#
# A = DjangoModels()
# C = Client()
#
# WELCOME += """%(Green)s
# Django environment detected.
# * Your INSTALLED_APPS models are available as `A`.
# * Your project settings are available as `S`.
# * The Django test client is available as `C`.
# %(Normal)s""" % _c
#
# setup_test_environment()
# S.DEBUG_PROPAGATE_EXCEPTIONS = True
#
# WELCOME += """%(LightPurple)s
# Warning: the Django test environment has been set up; to restore the
# normal environment call `teardown_test_environment()`.
# Warning: DEBUG_PROPAGATE_EXCEPTIONS has been set to True.
# %(Normal)s""" % _c
# Start an external editor with \e
##################################
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/438813/
EDITOR = os.environ.get('EDITOR', 'vi')
EDIT_CMD = '\e'
class EditableBufferInteractiveConsole(InteractiveConsole):
def __init__(self, *args, **kwargs):
self.last_buffer = [] # This holds the last executed statement
InteractiveConsole.__init__(self, *args, **kwargs)
def runsource(self, source, *args):
self.last_buffer = [source.encode('utf-8')]
return InteractiveConsole.runsource(self, source, *args)
def raw_input(self, *args):
line = InteractiveConsole.raw_input(self, *args)
if line == EDIT_CMD:
fd, tmpfl = mkstemp('.py')
os.write(fd, b'\n'.join(self.last_buffer))
os.close(fd)
os.system('{0} {1}'.format(EDITOR, tmpfl))
line = open(tmpfl).read()
os.unlink(tmpfl)
tmpfl = ''
lines = line.split('\n')
for i in range(len(lines) - 1): self.push(lines[i])
line = lines[-1]
return line
c = EditableBufferInteractiveConsole(locals=locals())
c.interact(banner=WELCOME)
# .pythonrc over
################
# Exit the Python shell on exiting the InteractiveConsole
sys.exit()