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 histsize = os.environ.get('HISTSIZE')
49 histsize = int(histsize)
53 readline.set_history_length(histsize)
54 readline.write_history_file(histfile)
57 atexit.register(savehist)
59 except (ImportError, AttributeError):
60 # no readline or atexit, or readline doesn't have
61 # {read,write}_history_file - ignore the error
66 term = os.environ.get('TERM', '')
70 background = os.environ.get('BACKGROUND', 'light').lower()
72 # From Randall Hopper:
73 # https://mail.python.org/pipermail/python-list/2001-March/112696.html
75 for _term in ['linux', 'rxvt', 'screen', 'term', 'vt100']:
79 if background == 'dark':
80 ps1_color = '3' # yellow
81 stdout_color = '7' # bold white
83 ps1_color = '4' # blue
84 stdout_color = '0' # bold black
86 sys.ps1 = '\001\033[3%sm\002>>>\001\033[0m\002 ' % ps1_color
87 sys.ps2 = '\001\033[1;32m\002...\001\033[0m\002 ' # bold green
92 def __init__(self, fp, begin,
93 end='\033[0m'): # reset all attributes
99 self.__fp.write(self.__begin+s+self.__end)
101 def writelines(self, lines):
102 map(self.write, lines)
104 def __getattr__(self, attr):
105 return getattr(self.__fp, attr)
107 sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
108 sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
115 pass # locale was not compiled
118 locale.setlocale(locale.LC_ALL, '')
119 except (ImportError, locale.Error):
120 pass # no locale support or unsupported locale
122 # set displayhook and excepthook
124 from pprint import pprint
125 pager = os.environ.get("PAGER") or 'more'
127 # if your pager is 'less', options '-F' and '-R' must be passed to it,
128 # and option '-X' is very much recommended
130 less = os.environ.get("LESS") or ''
131 for opt in 'X', 'R', 'F':
134 os.environ["LESS"] = less
137 def write(self, value):
138 self.stdin.write(value)
140 def pprint(self, value):
141 pprint(value, stream=self.stdin)
147 from subprocess import Popen, PIPE
149 class Pager(BasePager):
151 self.pipe = Popen(pager, shell=True, stdin=PIPE)
152 self.stdin = self.pipe.stdin
155 BasePager.close(self)
158 class Pager(BasePager):
160 self.stdin = os.popen(pager, 'w')
162 def displayhook(value):
163 if value is not None:
169 sys.displayhook = displayhook
171 from traceback import format_exception
173 def excepthook(etype, evalue, etraceback):
174 lines = format_exception(etype, evalue, etraceback)
178 '\033[31m' + line.rstrip('\n') + '\033[0m\n') # red, reset
181 sys.excepthook = excepthook
188 # # cgitb.enable() overrides sys.excepthook
189 # cgitb.enable(format='text')
191 # From Thomas Heller:
192 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
198 # sys.excepthook = info
202 # From: Paul Magwene:
203 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
204 # With a lot of my fixes:
207 def __getitem__(self, key):
208 s = os.listdir(os.curdir)
211 def __getslice__(self, i, j):
212 s = os.listdir(os.curdir)
216 return str(os.listdir(os.curdir))
218 def __call__(self, path=None):
220 path = os.path.expanduser(os.path.expandvars(path))
223 return os.listdir(path)
230 def __call__(self, path=None):
231 path = os.path.expanduser(os.path.expandvars(path or '~'))
234 builtins.ls = DirLister()
235 builtins.cd = DirChanger()
237 # print working directory
248 # exit REPL with 'exit', 'quit' or simple 'x'
254 def __call__(self, msg=None):
259 # In Python 2.5+ exit and quit are objects
260 if isinstance(builtins.exit, str):
261 builtins.exit = builtins.quit = x # noqa: x is defined as _Exit
263 # print conten of a file
267 return "Usage: cat('filename')"
269 def __call__(self, filename):
270 fp = open(filename, 'rU')
275 builtins.cat = _Cat()
281 os.system(os.environ["SHELL"])
284 def __call__(self, cmdline):
293 return "Usage: pager('filename')"
295 def __call__(self, filename):
296 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
298 builtins.pager = _Pager()
304 return "Usage: edit('filename')"
306 def __call__(self, filename):
307 editor = os.environ.get("VISUAL") \
308 or os.environ.get("EDITOR") or 'vi'
309 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
311 builtins.edit = builtins.editor = _Editor()