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.stdout.write(value)
140 def pprint(self, value):
142 stream=ColoredFile(self.stdout,
143 '\033[1;3%sm' % stdout_color))
149 from subprocess import Popen, PIPE
151 class Pager(BasePager):
153 self.pipe = Popen(pager, shell=True, stdin=PIPE)
154 self.stdout = self.pipe.stdin
157 BasePager.close(self)
160 class Pager(BasePager):
162 self.stdout = os.popen(pager, 'w')
164 def displayhook(value):
165 if value is not None:
171 sys.displayhook = displayhook
173 from traceback import format_exception
175 def excepthook(etype, evalue, etraceback):
176 lines = format_exception(etype, evalue, etraceback)
178 pager.stdout = ColoredFile(pager.stdout, '\033[31m') # red
183 sys.excepthook = excepthook
190 # # cgitb.enable() overrides sys.excepthook
191 # cgitb.enable(format='text')
193 # From Thomas Heller:
194 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
200 # sys.excepthook = info
204 # From: Paul Magwene:
205 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
206 # With a lot of my fixes:
209 def __getitem__(self, key):
210 s = os.listdir(os.curdir)
213 def __getslice__(self, i, j):
214 s = os.listdir(os.curdir)
218 return str(os.listdir(os.curdir))
220 def __call__(self, path=None):
222 path = os.path.expanduser(os.path.expandvars(path))
225 return os.listdir(path)
232 def __call__(self, path=None):
233 path = os.path.expanduser(os.path.expandvars(path or '~'))
236 builtins.ls = DirLister()
237 builtins.cd = DirChanger()
239 # print working directory
250 # exit REPL with 'exit', 'quit' or simple 'x'
256 def __call__(self, msg=None):
261 # In Python 2.5+ exit and quit are objects
262 if isinstance(builtins.exit, str):
263 builtins.exit = builtins.quit = x # noqa: x is defined as _Exit
265 # print conten of a file
269 return "Usage: cat('filename')"
271 def __call__(self, filename):
272 fp = open(filename, 'rU')
277 builtins.cat = _Cat()
283 os.system(os.environ["SHELL"])
286 def __call__(self, cmdline):
295 return "Usage: pager('filename')"
297 def __call__(self, filename):
298 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
300 builtins.pager = _Pager()
306 return "Usage: edit('filename')"
308 def __call__(self, filename):
309 editor = os.environ.get("VISUAL") \
310 or os.environ.get("EDITOR") or 'vi'
311 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
313 builtins.edit = builtins.editor = _Editor()