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 histfile = os.path.expanduser('~/.python_history')
41 readline.read_history_file(histfile)
46 histfilesize = os.environ.get('HISTFILESIZE') \
47 or os.environ.get('HISTSIZE')
50 histfilesize = int(histfilesize)
54 readline.set_history_length(histfilesize)
55 readline.write_history_file(histfile)
58 atexit.register(savehist)
60 except (ImportError, AttributeError):
61 # no readline or atexit, or readline doesn't have
62 # {read,write}_history_file - ignore the error
67 term = os.environ.get('TERM', '')
71 background = os.environ.get('BACKGROUND', 'light').lower()
73 # From Randall Hopper:
74 # https://mail.python.org/pipermail/python-list/2001-March/112696.html
76 for _term in ['linux', 'rxvt', 'screen', 'term', 'vt100']:
80 if background == 'dark':
81 ps1_color = '3' # yellow
82 stdout_color = '7' # bold white
84 ps1_color = '4' # blue
85 stdout_color = '0' # bold black
87 sys.ps1 = '\001\033[3%sm\002>>>\001\033[0m\002 ' % ps1_color
88 sys.ps2 = '\001\033[1;32m\002...\001\033[0m\002 ' # bold green
93 def __init__(self, fp, begin,
94 end='\033[0m'): # reset all attributes
100 self.__fp.write(self.__begin+s+self.__end)
102 def writelines(self, lines):
103 map(self.write, lines)
105 def __getattr__(self, attr):
106 return getattr(self.__fp, attr)
108 sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
109 sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
116 pass # locale was not compiled
119 locale.setlocale(locale.LC_ALL, '')
120 except (ImportError, locale.Error):
121 pass # no locale support or unsupported locale
123 # set displayhook and excepthook
125 from pprint import pprint
126 pager = os.environ.get("PAGER") or 'more'
128 # if your pager is 'less', options '-F' and '-R' must be passed to it,
129 # and option '-X' is very much recommended
131 less = os.environ.get("LESS") or ''
132 for opt in 'X', 'R', 'F':
135 os.environ["LESS"] = less
138 def write(self, value):
139 self.stdin.write(value)
141 def pprint(self, value):
142 pprint(value, stream=self.stdin)
148 from subprocess import Popen, PIPE
150 class Pager(BasePager):
152 self.pipe = Popen(pager, shell=True, stdin=PIPE)
153 self.stdin = self.pipe.stdin
156 BasePager.close(self)
159 class Pager(BasePager):
161 self.stdin = os.popen(pager, 'w')
163 def displayhook(value):
164 if value is not None:
170 sys.displayhook = displayhook
172 from traceback import format_exception
174 def excepthook(etype, evalue, etraceback):
175 lines = format_exception(etype, evalue, etraceback)
179 '\033[31m' + line.rstrip('\n') + '\033[0m\n') # red, reset
182 sys.excepthook = excepthook
189 # # cgitb.enable() overrides sys.excepthook
190 # cgitb.enable(format='text')
192 # From Thomas Heller:
193 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
199 # sys.excepthook = info
203 # From: Paul Magwene:
204 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
205 # With a lot of my fixes:
208 def __getitem__(self, key):
209 s = os.listdir(os.curdir)
212 def __getslice__(self, i, j):
213 s = os.listdir(os.curdir)
217 return str(os.listdir(os.curdir))
219 def __call__(self, path=None):
221 path = os.path.expanduser(os.path.expandvars(path))
224 return os.listdir(path)
231 def __call__(self, path=None):
232 path = os.path.expanduser(os.path.expandvars(path or '~'))
235 builtins.ls = DirLister()
236 builtins.cd = DirChanger()
238 # print working directory
249 # exit REPL with 'exit', 'quit' or simple 'x'
255 def __call__(self, msg=None):
260 # In Python 2.5+ exit and quit are objects
261 if isinstance(builtins.exit, str):
262 builtins.exit = builtins.quit = x # noqa: x is defined as _Exit
264 # print conten of a file
268 return "Usage: cat('filename')"
270 def __call__(self, filename):
271 fp = open(filename, 'rU')
276 builtins.cat = _Cat()
282 os.system(os.environ["SHELL"])
285 def __call__(self, cmdline):
294 return "Usage: pager('filename')"
296 def __call__(self, filename):
297 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
299 builtins.pager = _Pager()
305 return "Usage: edit('filename')"
307 def __call__(self, filename):
308 editor = os.environ.get("VISUAL") \
309 or os.environ.get("EDITOR") or 'vi'
310 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
312 builtins.edit = builtins.editor = _Editor()