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
31 initfile = os.environ.get('INPUTRC') \
32 or os.path.expanduser('~/.inputrc')
33 readline.read_init_file(initfile)
35 histfile = os.path.expanduser('~/.python-history')
37 readline.read_history_file(histfile)
42 histfilesize = os.environ.get('HISTFILESIZE') \
43 or os.environ.get('HISTSIZE')
46 histfilesize = int(histfilesize)
50 readline.set_history_length(histfilesize)
51 readline.write_history_file(histfile)
54 atexit.register(savehist)
56 except (ImportError, AttributeError):
57 # no readline or atexit, or readline doesn't have
58 # {read,write}_history_file - ignore the error
63 term = os.environ.get('TERM', '')
67 background = os.environ.get('BACKGROUND', 'light').lower()
69 # From Randall Hopper:
70 # https://mail.python.org/pipermail/python-list/2001-March/112696.html
72 for _term in ['linux', 'rxvt', 'screen', 'term', 'vt100']:
76 if background == 'dark':
77 ps1_color = '3' # yellow
78 stdout_color = '7' # bold white
80 ps1_color = '4' # blue
81 stdout_color = '0' # bold black
83 sys.ps1 = '\001\033[3%sm\002>>>\001\033[0m\002 ' % ps1_color
84 sys.ps2 = '\001\033[1;32m\002...\001\033[0m\002 ' # bold green
89 def __init__(self, fp, begin,
90 end='\033[0m'): # reset all attributes
96 self.__fp.write(self.__begin+s+self.__end)
98 def writelines(self, lines):
99 map(self.write, lines)
101 def __getattr__(self, attr):
102 return getattr(self.__fp, attr)
104 sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
105 sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
112 pass # locale was not compiled
115 locale.setlocale(locale.LC_ALL, '')
117 except (ImportError, locale.Error):
118 pass # no locale support or unsupported locale
122 from pprint import pprint
124 from subprocess import Popen, PIPE
126 use_subprocess = False
128 use_subprocess = True
129 pager = os.environ.get("PAGER") or 'more'
131 def displayhook(value):
132 if value is not None:
133 __builtin__._ = value
135 _pipe = Popen(pager, shell=True, stdin=PIPE)
138 pipe = os.popen(pager, 'w')
139 pprint(value, stream=pipe)
144 sys.displayhook = displayhook
148 # From: Paul Magwene:
149 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
150 # With a lot of my fixes:
153 def __getitem__(self, key):
154 s = os.listdir(os.curdir)
157 def __getslice__(self, i, j):
158 s = os.listdir(os.curdir)
162 return str(os.listdir(os.curdir))
164 def __call__(self, path=None):
166 path = os.path.expanduser(os.path.expandvars(path))
169 return os.listdir(path)
176 def __call__(self, path=None):
177 path = os.path.expanduser(os.path.expandvars(path or '~'))
180 __builtin__.ls = DirLister()
181 __builtin__.cd = DirChanger()
183 # From Thomas Heller:
184 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
190 # sys.excepthook = info
192 # print working directory
201 __builtin__.pwd = Pwd()
203 # exit REPL with 'exit', 'quit' or simple 'x'
209 def __call__(self, msg=None):
212 __builtin__.x = _Exit()
214 # In Python 2.5+ exit and quit are objects
215 if isinstance(__builtin__.exit, str):
216 __builtin__.exit = __builtin__.quit = x # noqa: x is defined as _Exit
218 # print conten of a file
222 return "Usage: cat('filename')"
224 def __call__(self, filename):
225 fp = open(filename, 'rU')
230 __builtin__.cat = _Cat()
236 os.system(os.environ["SHELL"])
239 def __call__(self, cmdline):
242 __builtin__.sh = _Sh()
248 return "Usage: pager('filename')"
250 def __call__(self, filename):
251 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
253 __builtin__.pager = _Pager()
259 return "Usage: edit('filename')"
261 def __call__(self, filename):
262 editor = os.environ.get("VISUAL") \
263 or os.environ.get("EDITOR") or 'vi'
264 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
266 __builtin__.edit = __builtin__.editor = _Editor()