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 histfiles = ['~/.python_history']
45 # if 'VIRTUAL_ENV' in os.environ:
46 # histfiles.append('$VIRTUAL_ENV/.python_history')
47 for histfile in histfiles:
49 histfile = os.path.expandvars(histfile)
50 histfile = os.path.expanduser(histfile)
51 readline.read_history_file(histfile)
56 histsize = os.environ.get('HISTSIZE')
59 histsize = int(histsize)
63 readline.set_history_length(histsize)
64 histfile = histfiles[-1]
65 histfile = os.path.expandvars(histfile)
66 histfile = os.path.expanduser(histfile)
68 readline.write_history_file(histfile)
73 atexit.register(savehist)
75 except (ImportError, AttributeError):
76 # no readline or atexit, or readline doesn't have
77 # {read,write}_history_file - ignore the error
82 term = os.environ.get('TERM', '')
83 for _term in ['cygwin', 'linux', 'putty']:
88 background = os.environ.get('BACKGROUND', 'light').lower()
90 # From Randall Hopper:
91 # https://mail.python.org/pipermail/python-list/2001-March/112696.html
94 for _term in ['cygwin', 'linux', 'putty', 'rxvt',
95 'screen', 'term', 'vt100']:
101 if background == 'dark':
102 ps1_color = '3' # yellow
103 stdout_color = '7' # bold white
105 ps1_color = '4' # blue
106 stdout_color = '0' # bold black
108 sys.ps1 = '\001\033[3%sm\002>>>\001\033[0m\002 ' % ps1_color
109 sys.ps2 = '\001\033[1;32m\002...\001\033[0m\002 ' # bold green
111 # From Denis Otkidach
114 def __init__(self, fp, begin,
115 end='\033[0m'): # reset all attributes
121 self.__fp.write(self.__begin+s+self.__end)
123 def writelines(self, lines):
124 map(self.write, lines)
126 def __getattr__(self, attr):
127 return getattr(self.__fp, attr)
129 sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
130 sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
132 def myinput(prompt=None):
133 save_stdout = sys.stdout
134 sys.stdout = sys.__stdout__
136 result = builtin_input(prompt)
139 sys.stdout = save_stdout
144 except AttributeError: # PY3
145 builtin_input = builtins.input
146 builtins.input = myinput
148 builtin_input = builtins.raw_input
149 builtins.raw_input = myinput
154 pass # locale was not compiled
157 locale.setlocale(locale.LC_ALL, '')
158 except (ImportError, locale.Error):
159 pass # no locale support or unsupported locale
161 # set displayhook and excepthook
163 from pprint import pprint
164 from traceback import format_exception, print_exc
166 pager = os.environ.get("PAGER") or 'more'
168 # if your pager is 'less', options '-F' and '-R' must be passed to it,
169 # and option '-X' is very much recommended
171 less = os.environ.get("LESS") or ''
172 for opt in 'X', 'R', 'F':
175 os.environ["LESS"] = less
178 def write(self, value):
179 self.stdout.write(value)
182 def pprint(self, value):
184 stream=ColoredFile(self.stdout,
185 '\033[1;3%sm' % stdout_color))
191 from subprocess import Popen, PIPE
193 class Pager(BasePager):
195 self.stdout = os.popen(pager, 'w')
197 class Pager(BasePager):
199 self.pipe = Popen(pager, shell=True, stdin=PIPE,
200 universal_newlines=True)
201 self.stdout = self.pipe.stdin
204 BasePager.close(self)
207 def displayhook(value):
208 if value is not None:
215 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
216 print_exc(file=pager)
219 sys.displayhook = displayhook
221 def excepthook(etype, evalue, etraceback):
222 lines = format_exception(etype, evalue, etraceback)
225 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
230 sys.excepthook = excepthook
234 # except ImportError:
237 # # cgitb.enable() overrides sys.excepthook
238 # cgitb.enable(format='text')
240 # From Thomas Heller:
241 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
247 # sys.excepthook = info
251 # From: Paul Magwene:
252 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
253 # With a lot of my fixes:
256 def __getitem__(self, key):
257 s = os.listdir(os.curdir)
260 def __getslice__(self, i, j):
261 s = os.listdir(os.curdir)
265 return str(os.listdir(os.curdir))
267 def __call__(self, path=None):
269 path = os.path.expanduser(os.path.expandvars(path))
272 return os.listdir(path)
279 def __call__(self, path=None):
280 path = os.path.expanduser(os.path.expandvars(path or '~'))
283 builtins.ls = DirLister()
284 builtins.cd = DirChanger()
286 # print working directory
297 # exit REPL with 'exit', 'quit' or simple 'x'
303 def __call__(self, msg=None):
308 # print conten of a file
312 return "Usage: cat('filename')"
314 def __call__(self, filename):
315 fp = open(filename, 'rU')
320 builtins.cat = _Cat()
326 os.system(os.environ["SHELL"])
329 def __call__(self, cmdline):
338 return "Usage: pager('filename')"
340 def __call__(self, filename):
341 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
343 builtins.pager = _Pager()
349 return "Usage: edit('filename')"
351 def __call__(self, filename):
352 editor = os.environ.get("VISUAL") \
353 or os.environ.get("EDITOR") or 'vi'
354 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
356 builtins.edit = builtins.editor = _Editor()