-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpythonrc
116 lines (97 loc) · 3.45 KB
/
pythonrc
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
try:
get_ipython
except NameError:
# not running under ipython
import atexit
import errno
import os
import pprint
import readline
import rlcompleter
import sys
from code import InteractiveConsole
from tempfile import mkstemp
#
# setup history and completion
#
history_path = os.path.expanduser('~/.python_history')
try:
open(history_path).close()
except IOError as e:
if e.errno == errno.ENOENT:
open(history_path, 'w').close()
else:
raise
readline.read_history_file(history_path)
readline.set_history_length(1000)
readline.parse_and_bind('tab: complete')
atexit.register(readline.write_history_file, history_path)
WELCOME = '''
pythonrc loaded'''.lstrip()
#te = '\001\033 \002'
#sys.ps1 = u'\001\033[30;42m\002 python \001\033[32;44m\002\ue0b0\001\033[30;44m\002 >>> \001\033[34;49m\002\ue0b0 \001\033[0m\002'.encode('utf8')
#sys.ps2 = u'\001\033[30;42m\002 python \001\033[32;44m\002\ue0b0\001\033[30;44m\002 ... \001\033[34;49m\002\ue0b0 \001\033[0m\002'.encode('utf8')
#
# pretty print stdout
#
def displayhook(value):
import pprint
if value is not None:
try:
import __builtin__
__builtin__._ = value
except ImportError:
__builtins__._ = value
pprint.pprint(value)
sys.displayhook = displayhook
#
# django
#
def SECRET_KEY():
from random import choice
return ''.join([
choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')
for i in range(50)
])
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 _settings
class DjangoModels(object):
def __init__(self):
for m in get_models():
setattr(self, m.__name__, m)
_models = DjangoModels()
_client = Client()
WELCOME += '''
_client ie: Client() == _client...
_models ie: _models.FeatureScorecard...
_settings ie: _settings.DEBUG'''
EDITOR = os.environ.get('EDITOR', 'vim')
EDIT_CMD = '\e'
class EditableBufferInteractiveConsole(InteractiveConsole):
def __init__(self, *args, **kwargs):
self.last_buffer = []
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, fn = mkstemp('.py')
os.write(fd, b'\n'.join(self.last_buffer))
os.close(fd)
os.system('%s %s' % (EDITOR, fn))
line = open(fn).read()
os.unlink(fn)
readline.add_history(line)
lines = line.splitlines()
for i in range(len(lines) - 1):
self.push(lines[i])
line = lines[-1]
return line
c = EditableBufferInteractiveConsole(locals=locals())
c.interact(banner=WELCOME)
sys.exit()