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)
67 readline.write_history_file(histfile)
70 atexit.register(savehist)
72 except (ImportError, AttributeError):
73 # no readline or atexit, or readline doesn't have
74 # {read,write}_history_file - ignore the error
79 term = os.environ.get('TERM', '')
83 background = os.environ.get('BACKGROUND', 'light').lower()
85 # From Randall Hopper:
86 # https://mail.python.org/pipermail/python-list/2001-March/112696.html
89 for _term in ['linux', 'rxvt', 'screen', 'term', 'vt100']:
95 if background == 'dark':
96 ps1_color = '3' # yellow
97 stdout_color = '7' # bold white
99 ps1_color = '4' # blue
100 stdout_color = '0' # bold black
102 sys.ps1 = '\001\033[3%sm\002>>>\001\033[0m\002 ' % ps1_color
103 sys.ps2 = '\001\033[1;32m\002...\001\033[0m\002 ' # bold green
105 # From Denis Otkidach
108 def __init__(self, fp, begin,
109 end='\033[0m'): # reset all attributes
115 self.__fp.write(self.__begin+s+self.__end)
117 def writelines(self, lines):
118 map(self.write, lines)
120 def __getattr__(self, attr):
121 return getattr(self.__fp, attr)
123 sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
124 sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
126 def myinput(prompt=None):
127 save_stdout = sys.stdout
128 sys.stdout = sys.__stdout__
130 result = builtin_input(prompt)
133 sys.stdout = save_stdout
138 except AttributeError: # PY3
139 builtin_input = builtins.input
140 builtins.input = myinput
142 builtin_input = builtins.raw_input
143 builtins.raw_input = myinput
148 pass # locale was not compiled
151 locale.setlocale(locale.LC_ALL, '')
152 except (ImportError, locale.Error):
153 pass # no locale support or unsupported locale
155 # set displayhook and excepthook
157 from pprint import pprint
158 from traceback import format_exception, print_exc
160 pager = os.environ.get("PAGER") or 'more'
162 # if your pager is 'less', options '-F' and '-R' must be passed to it,
163 # and option '-X' is very much recommended
165 less = os.environ.get("LESS") or ''
166 for opt in 'X', 'R', 'F':
169 os.environ["LESS"] = less
172 def write(self, value):
173 self.stdout.write(value)
175 def pprint(self, value):
177 stream=ColoredFile(self.stdout,
178 '\033[1;3%sm' % stdout_color))
184 from subprocess import Popen, PIPE
186 class Pager(BasePager):
188 self.stdout = os.popen(pager, 'w')
190 class Pager(BasePager):
192 self.pipe = Popen(pager, shell=True, stdin=PIPE,
193 universal_newlines=True)
194 self.stdout = self.pipe.stdin
197 BasePager.close(self)
200 def displayhook(value):
201 if value is not None:
207 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
208 print_exc(file=pager)
211 sys.displayhook = displayhook
213 def excepthook(etype, evalue, etraceback):
214 lines = format_exception(etype, evalue, etraceback)
216 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
221 sys.excepthook = excepthook
225 # except ImportError:
228 # # cgitb.enable() overrides sys.excepthook
229 # cgitb.enable(format='text')
231 # From Thomas Heller:
232 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
238 # sys.excepthook = info
242 # From: Paul Magwene:
243 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
244 # With a lot of my fixes:
247 def __getitem__(self, key):
248 s = os.listdir(os.curdir)
251 def __getslice__(self, i, j):
252 s = os.listdir(os.curdir)
256 return str(os.listdir(os.curdir))
258 def __call__(self, path=None):
260 path = os.path.expanduser(os.path.expandvars(path))
263 return os.listdir(path)
270 def __call__(self, path=None):
271 path = os.path.expanduser(os.path.expandvars(path or '~'))
274 builtins.ls = DirLister()
275 builtins.cd = DirChanger()
277 # print working directory
288 # exit REPL with 'exit', 'quit' or simple 'x'
294 def __call__(self, msg=None):
299 # print conten of a file
303 return "Usage: cat('filename')"
305 def __call__(self, filename):
306 fp = open(filename, 'rU')
311 builtins.cat = _Cat()
317 os.system(os.environ["SHELL"])
320 def __call__(self, cmdline):
329 return "Usage: pager('filename')"
331 def __call__(self, filename):
332 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
334 builtins.pager = _Pager()
340 return "Usage: edit('filename')"
342 def __call__(self, filename):
343 editor = os.environ.get("VISUAL") \
344 or os.environ.get("EDITOR") or 'vi'
345 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
347 builtins.edit = builtins.editor = _Editor()