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 from pprint import pprint
119 def displayhook(value):
120 if value is not None:
121 __builtin__._ = value
124 sys.displayhook = displayhook
126 except (ImportError, locale.Error):
127 pass # no locale support or unsupported locale
131 # From: Paul Magwene:
132 # https://mail.python.org/pipermail/python-list/2001-March/086191.html
133 # With a lot of my fixes:
136 def __getitem__(self, key):
137 s = os.listdir(os.curdir)
140 def __getslice__(self, i, j):
141 s = os.listdir(os.curdir)
145 return str(os.listdir(os.curdir))
147 def __call__(self, path=None):
149 path = os.path.expanduser(os.path.expandvars(path))
152 return os.listdir(path)
159 def __call__(self, path=None):
160 path = os.path.expanduser(os.path.expandvars(path or '~'))
163 __builtin__.ls = DirLister()
164 __builtin__.cd = DirChanger()
166 # From Thomas Heller:
167 # https://mail.python.org/pipermail/python-list/2001-April/099020.html
173 # sys.excepthook = info
175 # print working directory
184 __builtin__.pwd = Pwd()
186 # exit REPL with 'exit', 'quit' or simple 'x'
192 def __call__(self, msg=None):
195 __builtin__.x = _Exit()
197 # In Python 2.5+ exit and quit are objects
198 if isinstance(__builtin__.exit, str):
199 __builtin__.exit = __builtin__.quit = x # noqa: x is defined as _Exit
201 # print conten of a file
205 return "Usage: cat('filename')"
207 def __call__(self, filename):
208 fp = open(filename, 'rU')
213 __builtin__.cat = _Cat()
219 os.system(os.environ["SHELL"])
222 def __call__(self, cmdline):
225 __builtin__.sh = _Sh()
231 return "Usage: pager('filename')"
233 def __call__(self, filename):
234 pager = os.environ.get("PAGER") or 'more'
235 os.system("%s '%s'" % (pager, filename.replace("'", '"\'"')))
237 __builtin__.pager = _Pager()
243 return "Usage: edit('filename')"
245 def __call__(self, filename):
246 editor = os.environ.get("VISUAL") \
247 or os.environ.get("EDITOR") or 'vi'
248 os.system("%s '%s'" % (editor, filename.replace("'", '"\'"')))
250 __builtin__.edit = __builtin__.editor = _Editor()