-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpythonrc.py
136 lines (103 loc) · 3.67 KB
/
pythonrc.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
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
# -*- coding: utf-8 -*-
"""
bashrc => export PYTHONSTARTUP="/home/giorgis/.pythonrc.py"
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 pdb
# Enable a History
##################
HISTFILE="%s/.pyhistory" % 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(300)
def savehist():
readline.write_history_file(HISTFILE)
atexit.register(savehist)
readline.parse_and_bind("tab: complete")
# try:
# import readline, rlcompleter
# readline.parse_and_bind("tab: complete")
# readline.parse_and_bind('bind ^I rl_complete')
# # class MyCompleter(rlcompleter.Completer):
# # def complete(self, text, state):
# # if text.lstrip() == '':
# # if state == 0:
# # return text + '\t'
# # else:
# # return None
# # else:
# # return rlcompleter.Completer.complete(self, text, state)
# # readline.set_completer(MyCompleter().complete)
# except ImportError:
# print("Readline is not installed either. No tab completion is enabled.")
# Enable Color Prompts
######################
sys.ps1 = "%s>>> %s" % ("\x1B[32;1m", "\x1B[0m")
sys.ps2 = "%s... %s" % ("\x1B[31;1m", "\x1B[0m")
# Enable Pretty Printing for stdout
###################################
def my_displayhook(value):
if value is not None:
try:
import __builtin__
__builtin__._ = value
except ImportError:
__builtins__._ = value
pprint.pprint(value)
sys.displayhook = my_displayhook
# Welcome message
#################
WELCOME = """\
\x1B[36;5m
You've got color, history, and pretty printing.
(If your ~/.inputrc doesn't suck, you've also
got completion and vi-mode keybindings.)
Type \e to get an external editor.
\x1B[0m"""
atexit.register(lambda: sys.stdout.write("""\x1B[35;4m
Sheesh, I thought he'd never leave. Who invited that man?
\x1B[0m"""))
# 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('%s %s' % (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)
# Exit the Python shell on exiting the InteractiveConsole
sys.exit()