]> git.phdru.name Git - dotfiles.git/blob - lib/python/init.py
Initial import
[dotfiles.git] / lib / python / init.py
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):
6 #
7 # PYTHONSTARTUP=$HOME/init.py # or where you really put it
8 # export PYTHONSTARTUP
9
10 def init():
11     import sys, os
12     import __builtin__
13
14     pyreadlinew32_startup = os.path.join(sys.prefix,
15         'lib', 'site-packages', 'pyreadline', 'configuration', 'startup.py')
16
17     if os.path.exists(pyreadlinew32_startup):
18         execfile(pyreadlinew32_startup)
19
20     else:
21         # From Bruce Edge
22
23         try:
24             import rlcompleter, readline
25             initfile = os.environ.get('INPUTRC') or os.path.expanduser('~/.inputrc')
26             readline.read_init_file(initfile)
27
28             histfile = os.path.expanduser('~/.python-history')
29             try:
30                 readline.read_history_file(histfile)
31             except IOError:
32                 pass # No such file
33
34             def savehist():
35                 histfilesize = os.environ.get('HISTFILESIZE') or \
36                                os.environ.get('HISTSIZE')
37                 if histfilesize:
38                     try:
39                         histfilesize = int(histfilesize)
40                     except ValueError:
41                         pass
42                     else:
43                         readline.set_history_length(histfilesize)
44                 readline.write_history_file(histfile)
45
46             import atexit
47             atexit.register(savehist)
48
49         except (ImportError, AttributeError):
50             pass # no readline or atexit, or readline doesn't have
51                  # {read,write}_history_file - ignore the error
52
53
54     term = os.environ.get('TERM', '')
55     if 'linux' in term:
56         background = 'dark'
57     else:
58         background = os.environ.get('BACKGROUND', 'light').lower()
59
60     # From Randall Hopper
61
62     for _term in ['linux', 'term', 'rxvt', 'vt100', 'screen']:
63         if _term in term:
64             if background == 'dark':
65                 ps1_color = '3' # yellow
66                 stdout_color = '7' # bold white
67             else:
68                 ps1_color = '4' # blue
69                 stdout_color = '0' # bold black
70
71             sys.ps1 = '\001\033[3%sm\002>>>\001\033[0m\002 ' % ps1_color
72             sys.ps2 = '\001\033[1;32m\002...\001\033[0m\002 ' # bold green
73
74
75             # From Denis Otkidach
76
77             class ColoredFile:
78                 def __init__(self, fp, begin, end='\033[0m'): # reset all attributes
79                     self.__fp = fp
80                     self.__begin = begin
81                     self.__end = end
82
83                 def write(self, s):
84                     self.__fp.write(self.__begin+s+self.__end)
85
86                 def writelines(self, lines):
87                     map(self.write, lines)
88
89                 def __getattr__(self, attr):
90                     return getattr(self.__fp, attr)
91
92             sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
93             sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
94
95             break
96
97     try:
98         import locale
99     except ImportError:
100         pass # locale was not compiled
101     else:
102         try:
103             locale.setlocale(locale.LC_ALL, '')
104
105             from pprint import pprint
106
107             def displayhook(value):
108                 if value is not None:
109                     __builtin__._ = value
110                     pprint(value)
111
112             sys.displayhook = displayhook
113
114         except (ImportError, locale.Error):
115             pass # no locale support or unsupported locale
116
117
118     # From: Paul Magwene with a lot of my fixes
119
120     class DirLister:
121         def __getitem__(self, key):
122             s = os.listdir(os.curdir)
123             return s[key]
124
125         def __getslice__(self, i, j):
126             s = os.listdir(os.curdir)
127             return s[i:j]
128
129         def __repr__(self):
130             return str(os.listdir(os.curdir))
131
132         def __call__(self, path=None):
133             if path:
134                 path = os.path.expanduser(os.path.expandvars(path))
135             else:
136                 path = os.curdir
137             return os.listdir(path)
138
139     class DirChanger:
140         def __repr__(self):
141             self()
142             return os.getcwd()
143
144         def __call__(self, path=None):
145             path = os.path.expanduser(os.path.expandvars(path or '~'))
146             os.chdir(path)
147
148     __builtin__.ls = DirLister()
149     __builtin__.cd = DirChanger()
150
151
152     # From Thomas Heller
153     #
154     #import pdb
155     #
156     #def info(*args):
157     #   pdb.pm()
158     #sys.excepthook = info
159
160
161     class Pwd:
162         def __repr__(self):
163             return os.getcwd()
164
165         def __call__(self):
166             return repr(self)
167
168     __builtin__.pwd = Pwd()
169
170
171     class _Exit:
172         def __repr__(self):
173             sys.exit()
174
175         def __call__(self, msg=None):
176             sys.exit(msg)
177
178     __builtin__.x = _Exit()
179
180     if isinstance(__builtin__.exit, str): # In Python 2.5+ exit and quit are objects
181         __builtin__.exit = __builtin__.quit = x
182
183
184 init()
185 del init