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 histfile = os.path.expanduser('~/.python_history')
41 readline.read_history_file(histfile)
46 histsize = os.environ.get('HISTSIZE')
49 histsize = int(histsize)
53 readline.set_history_length(histsize)
54 readline.write_history_file(histfile)
57 atexit.register(savehist)
59 except (ImportError, AttributeError):
60 # no readline or atexit, or readline doesn't have
61 # {read,write}_history_file - ignore the error
66 term = os.environ.get('TERM', '')
70 background = os.environ.get('BACKGROUND', 'light').lower()
72 # From Randall Hopper:
73 # https://mail.python.org/pipermail/python-list/2001-March/112696.html
75 for _term in ['linux', 'rxvt', 'screen', 'term', 'vt100']:
79 if background == 'dark':
80 ps1_color = '3' # yellow
81 stdout_color = '7' # bold white
83 ps1_color = '4' # blue
84 stdout_color = '0' # bold black
86 sys.ps1 = '\001\033[3%sm\002>>>\001\033[0m\002 ' % ps1_color
87 sys.ps2 = '\001\033[1;32m\002...\001\033[0m\002 ' # bold green
92 def __init__(self, fp, begin,
93 end='\033[0m'): # reset all attributes
99 self.__fp.write(self.__begin+s+self.__end)
101 def writelines(self, lines):
102 map(self.write, lines)
104 def __getattr__(self, attr):
105 return getattr(self.__fp, attr)
107 sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
108 sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
110 def myinput(prompt=None):
111 save_stdout = sys.stdout
112 sys.stdout = sys.__stdout__
113 result = builtin_input(prompt)
114 sys.stdout = save_stdout
119 except AttributeError: # PY3
120 builtin_input = builtins.input
121 builtins.input = myinput
123 builtin_input = builtins.raw_input
124 builtins.raw_input = myinput
131 pass # locale was not compiled
134 locale.setlocale(locale.LC_ALL, '')
135 except (ImportError, locale.Error):
136 pass # no locale support or unsupported locale
138 # set displayhook and excepthook
140 from pprint import pprint
141 from traceback import format_exception, print_exc
143 pager = os.environ.get("PAGER") or 'more'
145 # if your pager is 'less', options '-F' and '-R' must be passed to it,
146 # and option '-X' is very much recommended
148 less = os.environ.get("LESS") or ''
149 for opt in 'X', 'R', 'F':
152 os.environ["LESS"] = less
155 def write(self, value):
156 self.stdout.write(value)
158 def pprint(self, value):
160 stream=ColoredFile(self.stdout,
161 '\033[1;3%sm' % stdout_color))
167 from subprocess import Popen, PIPE
169 class Pager(BasePager):
171 self.stdout = os.popen(pager, 'w')
173 class Pager(BasePager):
175 self.pipe = Popen(pager, shell=True, stdin=PIPE,
176 universal_newlines=True)
177 self.stdout = self.pipe.stdin
180 BasePager.close(self)
183 def displayhook(value):
184 if value is not None:
190 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
191 print_exc(file=pager)
194 sys.displayhook = displayhook
196 def excepthook(etype, evalue, etraceback):
197 lines = format_exception(etype, evalue, etraceback)
199 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
204 sys.excepthook = excepthook
211 # # cgitb.enable() overrides sys.excepthook
212 # cgitb.enable(format='text')
214 # From Thomas Heller:
215 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
221 # sys.excepthook = info
225 # From: Paul Magwene:
226 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
227 # With a lot of my fixes:
230 def __getitem__(self, key):
231 s = os.listdir(os.curdir)
234 def __getslice__(self, i, j):
235 s = os.listdir(os.curdir)
239 return str(os.listdir(os.curdir))
241 def __call__(self, path=None):
243 path = os.path.expanduser(os.path.expandvars(path))
246 return os.listdir(path)
253 def __call__(self, path=None):
254 path = os.path.expanduser(os.path.expandvars(path or '~'))
257 builtins.ls = DirLister()
258 builtins.cd = DirChanger()
260 # print working directory
271 # exit REPL with 'exit', 'quit' or simple 'x'
277 def __call__(self, msg=None):
282 # print conten of a file
286 return "Usage: cat('filename')"
288 def __call__(self, filename):
289 fp = open(filename, 'rU')
294 builtins.cat = _Cat()
300 os.system(os.environ["SHELL"])
303 def __call__(self, cmdline):
312 return "Usage: pager('filename')"
314 def __call__(self, filename):
315 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
317 builtins.pager = _Pager()
323 return "Usage: edit('filename')"
325 def __call__(self, filename):
326 editor = os.environ.get("VISUAL") \
327 or os.environ.get("EDITOR") or 'vi'
328 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
330 builtins.edit = builtins.editor = _Editor()