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
18 pyreadlinew32_startup = os.path.join(
19 sys.prefix, 'lib', 'site-packages',
20 'pyreadline', 'configuration', 'startup.py')
22 if os.path.exists(pyreadlinew32_startup):
23 execfile(pyreadlinew32_startup)
27 # https://mail.python.org/pipermail/python-list/2001-March/062888.html
30 import rlcompleter # noqa: need for completion
32 initfile = os.environ.get('INPUTRC') \
33 or os.path.expanduser('~/.inputrc')
34 readline.read_init_file(initfile)
36 histfile = os.path.expanduser('~/.python-history')
38 readline.read_history_file(histfile)
43 histfilesize = os.environ.get('HISTFILESIZE') \
44 or os.environ.get('HISTSIZE')
47 histfilesize = int(histfilesize)
51 readline.set_history_length(histfilesize)
52 readline.write_history_file(histfile)
55 atexit.register(savehist)
57 except (ImportError, AttributeError):
58 # no readline or atexit, or readline doesn't have
59 # {read,write}_history_file - ignore the error
64 term = os.environ.get('TERM', '')
68 background = os.environ.get('BACKGROUND', 'light').lower()
70 # From Randall Hopper:
71 # https://mail.python.org/pipermail/python-list/2001-March/112696.html
73 for _term in ['linux', 'rxvt', 'screen', 'term', 'vt100']:
77 if background == 'dark':
78 ps1_color = '3' # yellow
79 stdout_color = '7' # bold white
81 ps1_color = '4' # blue
82 stdout_color = '0' # bold black
84 sys.ps1 = '\001\033[3%sm\002>>>\001\033[0m\002 ' % ps1_color
85 sys.ps2 = '\001\033[1;32m\002...\001\033[0m\002 ' # bold green
90 def __init__(self, fp, begin,
91 end='\033[0m'): # reset all attributes
97 self.__fp.write(self.__begin+s+self.__end)
99 def writelines(self, lines):
100 map(self.write, lines)
102 def __getattr__(self, attr):
103 return getattr(self.__fp, attr)
105 sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
106 sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
113 pass # locale was not compiled
116 locale.setlocale(locale.LC_ALL, '')
117 except (ImportError, locale.Error):
118 pass # no locale support or unsupported locale
120 # set displayhook and excepthook
122 from pprint import pprint
123 pager = os.environ.get("PAGER") or 'more'
125 # if your pager is 'less', options '-F' and '-R' must be passed to it,
126 # and option '-X' is very much recommended
128 less = os.environ.get("LESS") or ''
129 for opt in 'X', 'R', 'F':
132 os.environ["LESS"] = less
135 def write(self, value):
136 self.stdin.write(value)
138 def pprint(self, value):
139 pprint(value, stream=self.stdin)
145 from subprocess import Popen, PIPE
147 class Pager(BasePager):
149 self.pipe = Popen(pager, shell=True, stdin=PIPE)
150 self.stdin = self.pipe.stdin
153 BasePager.close(self)
156 class Pager(BasePager):
158 self.stdin = os.popen(pager, 'w')
160 def displayhook(value):
161 if value is not None:
162 __builtin__._ = value
167 sys.displayhook = displayhook
169 from traceback import format_exception
171 def excepthook(etype, evalue, etraceback):
172 lines = format_exception(etype, evalue, etraceback)
176 '\033[31m' + line.rstrip('\n') + '\033[0m\n') # red, reset
179 sys.excepthook = excepthook
181 # From Thomas Heller:
182 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
188 # sys.excepthook = info
192 # From: Paul Magwene:
193 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
194 # With a lot of my fixes:
197 def __getitem__(self, key):
198 s = os.listdir(os.curdir)
201 def __getslice__(self, i, j):
202 s = os.listdir(os.curdir)
206 return str(os.listdir(os.curdir))
208 def __call__(self, path=None):
210 path = os.path.expanduser(os.path.expandvars(path))
213 return os.listdir(path)
220 def __call__(self, path=None):
221 path = os.path.expanduser(os.path.expandvars(path or '~'))
224 __builtin__.ls = DirLister()
225 __builtin__.cd = DirChanger()
227 # print working directory
236 __builtin__.pwd = Pwd()
238 # exit REPL with 'exit', 'quit' or simple 'x'
244 def __call__(self, msg=None):
247 __builtin__.x = _Exit()
249 # In Python 2.5+ exit and quit are objects
250 if isinstance(__builtin__.exit, str):
251 __builtin__.exit = __builtin__.quit = x # noqa: x is defined as _Exit
253 # print conten of a file
257 return "Usage: cat('filename')"
259 def __call__(self, filename):
260 fp = open(filename, 'rU')
265 __builtin__.cat = _Cat()
271 os.system(os.environ["SHELL"])
274 def __call__(self, cmdline):
277 __builtin__.sh = _Sh()
283 return "Usage: pager('filename')"
285 def __call__(self, filename):
286 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
288 __builtin__.pager = _Pager()
294 return "Usage: edit('filename')"
296 def __call__(self, filename):
297 editor = os.environ.get("VISUAL") \
298 or os.environ.get("EDITOR") or 'vi'
299 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
301 __builtin__.edit = __builtin__.editor = _Editor()