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', '')
86 background = os.environ.get('BACKGROUND', 'light').lower()
88 # From Randall Hopper:
89 # https://mail.python.org/pipermail/python-list/2001-March/112696.html
92 for _term in ['linux', 'rxvt', 'screen', 'term', 'vt100']:
98 if background == 'dark':
99 ps1_color = '3' # yellow
100 stdout_color = '7' # bold white
102 ps1_color = '4' # blue
103 stdout_color = '0' # bold black
105 sys.ps1 = '\001\033[3%sm\002>>>\001\033[0m\002 ' % ps1_color
106 sys.ps2 = '\001\033[1;32m\002...\001\033[0m\002 ' # bold green
108 # From Denis Otkidach
111 def __init__(self, fp, begin,
112 end='\033[0m'): # reset all attributes
118 self.__fp.write(self.__begin+s+self.__end)
120 def writelines(self, lines):
121 map(self.write, lines)
123 def __getattr__(self, attr):
124 return getattr(self.__fp, attr)
126 sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
127 sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
129 def myinput(prompt=None):
130 save_stdout = sys.stdout
131 sys.stdout = sys.__stdout__
133 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)
178 def pprint(self, value):
180 stream=ColoredFile(self.stdout,
181 '\033[1;3%sm' % stdout_color))
187 from subprocess import Popen, PIPE
189 class Pager(BasePager):
191 self.stdout = os.popen(pager, 'w')
193 class Pager(BasePager):
195 self.pipe = Popen(pager, shell=True, stdin=PIPE,
196 universal_newlines=True)
197 self.stdout = self.pipe.stdin
200 BasePager.close(self)
203 def displayhook(value):
204 if value is not None:
210 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
211 print_exc(file=pager)
214 sys.displayhook = displayhook
216 def excepthook(etype, evalue, etraceback):
217 lines = format_exception(etype, evalue, etraceback)
219 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
224 sys.excepthook = excepthook
228 # except ImportError:
231 # # cgitb.enable() overrides sys.excepthook
232 # cgitb.enable(format='text')
234 # From Thomas Heller:
235 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
241 # sys.excepthook = info
245 # From: Paul Magwene:
246 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
247 # With a lot of my fixes:
250 def __getitem__(self, key):
251 s = os.listdir(os.curdir)
254 def __getslice__(self, i, j):
255 s = os.listdir(os.curdir)
259 return str(os.listdir(os.curdir))
261 def __call__(self, path=None):
263 path = os.path.expanduser(os.path.expandvars(path))
266 return os.listdir(path)
273 def __call__(self, path=None):
274 path = os.path.expanduser(os.path.expandvars(path or '~'))
277 builtins.ls = DirLister()
278 builtins.cd = DirChanger()
280 # print working directory
291 # exit REPL with 'exit', 'quit' or simple 'x'
297 def __call__(self, msg=None):
302 # print conten of a file
306 return "Usage: cat('filename')"
308 def __call__(self, filename):
309 fp = open(filename, 'rU')
314 builtins.cat = _Cat()
320 os.system(os.environ["SHELL"])
323 def __call__(self, cmdline):
332 return "Usage: pager('filename')"
334 def __call__(self, filename):
335 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
337 builtins.pager = _Pager()
343 return "Usage: edit('filename')"
345 def __call__(self, filename):
346 editor = os.environ.get("VISUAL") \
347 or os.environ.get("EDITOR") or 'vi'
348 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
350 builtins.edit = builtins.editor = _Editor()