1 # This is startup file for interactive python.
2 # It is not automatically loaded by python interpreter.
3 # To instruct the interpreter to load it insert the following commands
4 # into your .profile (use whatever syntax and initialization file
5 # is appropriate for your shell):
7 # PYTHONSTARTUP=$HOME/init.py # or where you really put it
13 import __builtin__ as builtins
21 pyreadlinew32_startup = os.path.join(
22 sys.prefix, 'lib', 'site-packages',
23 'pyreadline', 'configuration', 'startup.py')
25 if os.path.exists(pyreadlinew32_startup):
26 execfile(pyreadlinew32_startup)
30 # https://mail.python.org/pipermail/python-list/2001-March/062888.html
33 import rlcompleter # noqa: need for completion
35 initfile = os.environ.get('INPUTRC') \
36 or os.path.expanduser('~/.inputrc')
37 readline.read_init_file(initfile)
39 #if 'libedit' in readline.__doc__:
40 # readline.parse_and_bind("bind ^I rl_complete")
42 # readline.parse_and_bind("tab: complete")
44 histfile = os.path.expanduser('~/.python_history')
46 readline.read_history_file(histfile)
51 histsize = os.environ.get('HISTSIZE')
54 histsize = int(histsize)
58 readline.set_history_length(histsize)
59 readline.write_history_file(histfile)
62 atexit.register(savehist)
64 except (ImportError, AttributeError):
65 # no readline or atexit, or readline doesn't have
66 # {read,write}_history_file - ignore the error
71 term = os.environ.get('TERM', '')
75 background = os.environ.get('BACKGROUND', 'light').lower()
77 # From Randall Hopper:
78 # https://mail.python.org/pipermail/python-list/2001-March/112696.html
80 for _term in ['linux', 'rxvt', 'screen', 'term', 'vt100']:
84 if background == 'dark':
85 ps1_color = '3' # yellow
86 stdout_color = '7' # bold white
88 ps1_color = '4' # blue
89 stdout_color = '0' # bold black
91 sys.ps1 = '\001\033[3%sm\002>>>\001\033[0m\002 ' % ps1_color
92 sys.ps2 = '\001\033[1;32m\002...\001\033[0m\002 ' # bold green
97 def __init__(self, fp, begin,
98 end='\033[0m'): # reset all attributes
104 self.__fp.write(self.__begin+s+self.__end)
106 def writelines(self, lines):
107 map(self.write, lines)
109 def __getattr__(self, attr):
110 return getattr(self.__fp, attr)
112 sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
113 sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
115 def myinput(prompt=None):
116 save_stdout = sys.stdout
117 sys.stdout = sys.__stdout__
118 result = builtin_input(prompt)
119 sys.stdout = save_stdout
124 except AttributeError: # PY3
125 builtin_input = builtins.input
126 builtins.input = myinput
128 builtin_input = builtins.raw_input
129 builtins.raw_input = myinput
136 pass # locale was not compiled
139 locale.setlocale(locale.LC_ALL, '')
140 except (ImportError, locale.Error):
141 pass # no locale support or unsupported locale
143 # set displayhook and excepthook
145 from pprint import pprint
146 from traceback import format_exception, print_exc
148 pager = os.environ.get("PAGER") or 'more'
150 # if your pager is 'less', options '-F' and '-R' must be passed to it,
151 # and option '-X' is very much recommended
153 less = os.environ.get("LESS") or ''
154 for opt in 'X', 'R', 'F':
157 os.environ["LESS"] = less
160 def write(self, value):
161 self.stdout.write(value)
163 def pprint(self, value):
165 stream=ColoredFile(self.stdout,
166 '\033[1;3%sm' % stdout_color))
172 from subprocess import Popen, PIPE
174 class Pager(BasePager):
176 self.stdout = os.popen(pager, 'w')
178 class Pager(BasePager):
180 self.pipe = Popen(pager, shell=True, stdin=PIPE,
181 universal_newlines=True)
182 self.stdout = self.pipe.stdin
185 BasePager.close(self)
188 def displayhook(value):
189 if value is not None:
195 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
196 print_exc(file=pager)
199 sys.displayhook = displayhook
201 def excepthook(etype, evalue, etraceback):
202 lines = format_exception(etype, evalue, etraceback)
204 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
209 sys.excepthook = excepthook
216 # # cgitb.enable() overrides sys.excepthook
217 # cgitb.enable(format='text')
219 # From Thomas Heller:
220 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
226 # sys.excepthook = info
230 # From: Paul Magwene:
231 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
232 # With a lot of my fixes:
235 def __getitem__(self, key):
236 s = os.listdir(os.curdir)
239 def __getslice__(self, i, j):
240 s = os.listdir(os.curdir)
244 return str(os.listdir(os.curdir))
246 def __call__(self, path=None):
248 path = os.path.expanduser(os.path.expandvars(path))
251 return os.listdir(path)
258 def __call__(self, path=None):
259 path = os.path.expanduser(os.path.expandvars(path or '~'))
262 builtins.ls = DirLister()
263 builtins.cd = DirChanger()
265 # print working directory
276 # exit REPL with 'exit', 'quit' or simple 'x'
282 def __call__(self, msg=None):
287 # print conten of a file
291 return "Usage: cat('filename')"
293 def __call__(self, filename):
294 fp = open(filename, 'rU')
299 builtins.cat = _Cat()
305 os.system(os.environ["SHELL"])
308 def __call__(self, cmdline):
317 return "Usage: pager('filename')"
319 def __call__(self, filename):
320 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
322 builtins.pager = _Pager()
328 return "Usage: edit('filename')"
330 def __call__(self, filename):
331 editor = os.environ.get("VISUAL") \
332 or os.environ.get("EDITOR") or 'vi'
333 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
335 builtins.edit = builtins.editor = _Editor()