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__
135 result = builtin_input(prompt)
136 sys.stdout = save_stdout
141 except AttributeError: # PY3
142 builtin_input = builtins.input
143 builtins.input = myinput
145 builtin_input = builtins.raw_input
146 builtins.raw_input = myinput
151 pass # locale was not compiled
154 locale.setlocale(locale.LC_ALL, '')
155 except (ImportError, locale.Error):
156 pass # no locale support or unsupported locale
158 # set displayhook and excepthook
160 from pprint import pprint
161 from traceback import format_exception, print_exc
163 pager = os.environ.get("PAGER") or 'more'
165 # if your pager is 'less', options '-F' and '-R' must be passed to it,
166 # and option '-X' is very much recommended
168 less = os.environ.get("LESS") or ''
169 for opt in 'X', 'R', 'F':
172 os.environ["LESS"] = less
175 def write(self, value):
176 self.stdout.write(value)
179 def pprint(self, value):
181 stream=ColoredFile(self.stdout,
182 '\033[1;3%sm' % stdout_color))
188 from subprocess import Popen, PIPE
190 class Pager(BasePager):
192 self.stdout = os.popen(pager, 'w')
194 class Pager(BasePager):
196 self.pipe = Popen(pager, shell=True, stdin=PIPE,
197 universal_newlines=True)
198 self.stdout = self.pipe.stdin
201 BasePager.close(self)
204 def displayhook(value):
205 if value is not None:
212 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
213 print_exc(file=pager)
216 sys.displayhook = displayhook
218 def excepthook(etype, evalue, etraceback):
219 lines = format_exception(etype, evalue, etraceback)
222 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
227 sys.excepthook = excepthook
231 # except ImportError:
234 # # cgitb.enable() overrides sys.excepthook
235 # cgitb.enable(format='text')
237 # From Thomas Heller:
238 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
244 # sys.excepthook = info
248 # From: Paul Magwene:
249 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
250 # With a lot of my fixes:
253 def __getitem__(self, key):
254 s = os.listdir(os.curdir)
257 def __getslice__(self, i, j):
258 s = os.listdir(os.curdir)
262 return str(os.listdir(os.curdir))
264 def __call__(self, path=None):
266 path = os.path.expanduser(os.path.expandvars(path))
269 return os.listdir(path)
276 def __call__(self, path=None):
277 path = os.path.expanduser(os.path.expandvars(path or '~'))
280 builtins.ls = DirLister()
281 builtins.cd = DirChanger()
283 # print working directory
294 # exit REPL with 'exit', 'quit' or simple 'x'
300 def __call__(self, msg=None):
305 # print conten of a file
309 return "Usage: cat('filename')"
311 def __call__(self, filename):
312 fp = open(filename, 'rU')
317 builtins.cat = _Cat()
323 os.system(os.environ["SHELL"])
326 def __call__(self, cmdline):
335 return "Usage: pager('filename')"
337 def __call__(self, filename):
338 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
340 builtins.pager = _Pager()
346 return "Usage: edit('filename')"
348 def __call__(self, filename):
349 editor = os.environ.get("VISUAL") \
350 or os.environ.get("EDITOR") or 'vi'
351 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
353 builtins.edit = builtins.editor = _Editor()