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
81 for _term in ['linux', 'rxvt', 'screen', 'term', 'vt100']:
87 if background == 'dark':
88 ps1_color = '3' # yellow
89 stdout_color = '7' # bold white
91 ps1_color = '4' # blue
92 stdout_color = '0' # bold black
94 sys.ps1 = '\001\033[3%sm\002>>>\001\033[0m\002 ' % ps1_color
95 sys.ps2 = '\001\033[1;32m\002...\001\033[0m\002 ' # bold green
100 def __init__(self, fp, begin,
101 end='\033[0m'): # reset all attributes
107 self.__fp.write(self.__begin+s+self.__end)
109 def writelines(self, lines):
110 map(self.write, lines)
112 def __getattr__(self, attr):
113 return getattr(self.__fp, attr)
115 sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
116 sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
118 def myinput(prompt=None):
119 save_stdout = sys.stdout
120 sys.stdout = sys.__stdout__
122 result = builtin_input(prompt)
125 sys.stdout = save_stdout
130 except AttributeError: # PY3
131 builtin_input = builtins.input
132 builtins.input = myinput
134 builtin_input = builtins.raw_input
135 builtins.raw_input = myinput
140 pass # locale was not compiled
143 locale.setlocale(locale.LC_ALL, '')
144 except (ImportError, locale.Error):
145 pass # no locale support or unsupported locale
147 # set displayhook and excepthook
149 from pprint import pprint
150 from traceback import format_exception, print_exc
152 pager = os.environ.get("PAGER") or 'more'
154 # if your pager is 'less', options '-F' and '-R' must be passed to it,
155 # and option '-X' is very much recommended
157 less = os.environ.get("LESS") or ''
158 for opt in 'X', 'R', 'F':
161 os.environ["LESS"] = less
164 def write(self, value):
165 self.stdout.write(value)
167 def pprint(self, value):
169 stream=ColoredFile(self.stdout,
170 '\033[1;3%sm' % stdout_color))
176 from subprocess import Popen, PIPE
178 class Pager(BasePager):
180 self.stdout = os.popen(pager, 'w')
182 class Pager(BasePager):
184 self.pipe = Popen(pager, shell=True, stdin=PIPE,
185 universal_newlines=True)
186 self.stdout = self.pipe.stdin
189 BasePager.close(self)
192 def displayhook(value):
193 if value is not None:
199 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
200 print_exc(file=pager)
203 sys.displayhook = displayhook
205 def excepthook(etype, evalue, etraceback):
206 lines = format_exception(etype, evalue, etraceback)
208 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
213 sys.excepthook = excepthook
220 # # cgitb.enable() overrides sys.excepthook
221 # cgitb.enable(format='text')
223 # From Thomas Heller:
224 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
230 # sys.excepthook = info
234 # From: Paul Magwene:
235 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
236 # With a lot of my fixes:
239 def __getitem__(self, key):
240 s = os.listdir(os.curdir)
243 def __getslice__(self, i, j):
244 s = os.listdir(os.curdir)
248 return str(os.listdir(os.curdir))
250 def __call__(self, path=None):
252 path = os.path.expanduser(os.path.expandvars(path))
255 return os.listdir(path)
262 def __call__(self, path=None):
263 path = os.path.expanduser(os.path.expandvars(path or '~'))
266 builtins.ls = DirLister()
267 builtins.cd = DirChanger()
269 # print working directory
280 # exit REPL with 'exit', 'quit' or simple 'x'
286 def __call__(self, msg=None):
291 # print conten of a file
295 return "Usage: cat('filename')"
297 def __call__(self, filename):
298 fp = open(filename, 'rU')
303 builtins.cat = _Cat()
309 os.system(os.environ["SHELL"])
312 def __call__(self, cmdline):
321 return "Usage: pager('filename')"
323 def __call__(self, filename):
324 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
326 builtins.pager = _Pager()
332 return "Usage: edit('filename')"
334 def __call__(self, filename):
335 editor = os.environ.get("VISUAL") \
336 or os.environ.get("EDITOR") or 'vi'
337 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
339 builtins.edit = builtins.editor = _Editor()