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
14 pyreadlinew32_startup = os.path.join(sys.prefix,
15 'lib', 'site-packages', 'pyreadline', 'configuration', 'startup.py')
17 if os.path.exists(pyreadlinew32_startup):
18 execfile(pyreadlinew32_startup)
22 # https://mail.python.org/pipermail/python-list/2001-March/062888.html
25 import rlcompleter, readline
26 initfile = os.environ.get('INPUTRC') or os.path.expanduser('~/.inputrc')
27 readline.read_init_file(initfile)
29 histfile = os.path.expanduser('~/.python-history')
31 readline.read_history_file(histfile)
36 histfilesize = os.environ.get('HISTFILESIZE') or \
37 os.environ.get('HISTSIZE')
40 histfilesize = int(histfilesize)
44 readline.set_history_length(histfilesize)
45 readline.write_history_file(histfile)
48 atexit.register(savehist)
50 except (ImportError, AttributeError):
51 pass # no readline or atexit, or readline doesn't have
52 # {read,write}_history_file - ignore the error
55 term = os.environ.get('TERM', '')
59 background = os.environ.get('BACKGROUND', 'light').lower()
61 # From Randall Hopper:
62 # https://mail.python.org/pipermail/python-list/2001-March/112696.html
64 for _term in ['linux', 'rxvt', 'screen', 'term', 'vt100']:
68 if background == 'dark':
69 ps1_color = '3' # yellow
70 stdout_color = '7' # bold white
72 ps1_color = '4' # blue
73 stdout_color = '0' # bold black
75 sys.ps1 = '\001\033[3%sm\002>>>\001\033[0m\002 ' % ps1_color
76 sys.ps2 = '\001\033[1;32m\002...\001\033[0m\002 ' # bold green
82 def __init__(self, fp, begin, end='\033[0m'): # reset all attributes
88 self.__fp.write(self.__begin+s+self.__end)
90 def writelines(self, lines):
91 map(self.write, lines)
93 def __getattr__(self, attr):
94 return getattr(self.__fp, attr)
96 sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
97 sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
104 pass # locale was not compiled
107 locale.setlocale(locale.LC_ALL, '')
109 from pprint import pprint
111 def displayhook(value):
112 if value is not None:
113 __builtin__._ = value
116 sys.displayhook = displayhook
118 except (ImportError, locale.Error):
119 pass # no locale support or unsupported locale
122 # From: Paul Magwene:
123 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
124 # With a lot of my fixes:
127 def __getitem__(self, key):
128 s = os.listdir(os.curdir)
131 def __getslice__(self, i, j):
132 s = os.listdir(os.curdir)
136 return str(os.listdir(os.curdir))
138 def __call__(self, path=None):
140 path = os.path.expanduser(os.path.expandvars(path))
143 return os.listdir(path)
150 def __call__(self, path=None):
151 path = os.path.expanduser(os.path.expandvars(path or '~'))
154 __builtin__.ls = DirLister()
155 __builtin__.cd = DirChanger()
157 # From Thomas Heller:
158 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
166 #sys.excepthook = info
176 __builtin__.pwd = Pwd()
183 def __call__(self, msg=None):
186 __builtin__.x = _Exit()
188 if isinstance(__builtin__.exit, str): # In Python 2.5+ exit and quit are objects
189 __builtin__.exit = __builtin__.quit = x
194 return "Usage: cat('filename')"
196 def __call__(self, filename):
197 fp = open(filename, 'rU')
202 __builtin__.cat = _Cat()
207 os.system(os.environ["SHELL"])
210 def __call__(self, cmdline):
213 __builtin__.sh = _Sh()
218 return "Usage: pager('filename')"
220 def __call__(self, filename):
221 pager = os.environ.get("PAGER") or 'more'
222 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
224 __builtin__.pager = _Pager()
229 return "Usage: edit('filename')"
231 def __call__(self, filename):
232 editor = os.environ.get("VISUAL") or os.environ.get("EDITOR") or 'vi'
233 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
235 __builtin__.edit = __builtin__.editor = _Editor()