]> git.phdru.name Git - dotfiles.git/blob - lib/python/init.py
Refactor loop
[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', 'rxvt', 'screen', 'term', 'vt100']:
63         if _term not in term:
64             continue
65
66         if background == 'dark':
67             ps1_color = '3' # yellow
68             stdout_color = '7' # bold white
69         else:
70             ps1_color = '4' # blue
71             stdout_color = '0' # bold black
72
73         sys.ps1 = '\001\033[3%sm\002>>>\001\033[0m\002 ' % ps1_color
74         sys.ps2 = '\001\033[1;32m\002...\001\033[0m\002 ' # bold green
75
76
77         # From Denis Otkidach
78
79         class ColoredFile:
80             def __init__(self, fp, begin, end='\033[0m'): # reset all attributes
81                 self.__fp = fp
82                 self.__begin = begin
83                 self.__end = end
84
85             def write(self, s):
86                 self.__fp.write(self.__begin+s+self.__end)
87
88             def writelines(self, lines):
89                 map(self.write, lines)
90
91             def __getattr__(self, attr):
92                 return getattr(self.__fp, attr)
93
94         sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
95         sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
96
97         break
98
99     try:
100         import locale
101     except ImportError:
102         pass # locale was not compiled
103     else:
104         try:
105             locale.setlocale(locale.LC_ALL, '')
106
107             from pprint import pprint
108
109             def displayhook(value):
110                 if value is not None:
111                     __builtin__._ = value
112                     pprint(value)
113
114             sys.displayhook = displayhook
115
116         except (ImportError, locale.Error):
117             pass # no locale support or unsupported locale
118
119
120     # From: Paul Magwene with a lot of my fixes
121
122     class DirLister:
123         def __getitem__(self, key):
124             s = os.listdir(os.curdir)
125             return s[key]
126
127         def __getslice__(self, i, j):
128             s = os.listdir(os.curdir)
129             return s[i:j]
130
131         def __repr__(self):
132             return str(os.listdir(os.curdir))
133
134         def __call__(self, path=None):
135             if path:
136                 path = os.path.expanduser(os.path.expandvars(path))
137             else:
138                 path = os.curdir
139             return os.listdir(path)
140
141     class DirChanger:
142         def __repr__(self):
143             self()
144             return os.getcwd()
145
146         def __call__(self, path=None):
147             path = os.path.expanduser(os.path.expandvars(path or '~'))
148             os.chdir(path)
149
150     __builtin__.ls = DirLister()
151     __builtin__.cd = DirChanger()
152
153
154     # From Thomas Heller
155     #
156     #import pdb
157     #
158     #def info(*args):
159     #   pdb.pm()
160     #sys.excepthook = info
161
162
163     class Pwd:
164         def __repr__(self):
165             return os.getcwd()
166
167         def __call__(self):
168             return repr(self)
169
170     __builtin__.pwd = Pwd()
171
172
173     class _Exit:
174         def __repr__(self):
175             sys.exit()
176
177         def __call__(self, msg=None):
178             sys.exit(msg)
179
180     __builtin__.x = _Exit()
181
182     if isinstance(__builtin__.exit, str): # In Python 2.5+ exit and quit are objects
183         __builtin__.exit = __builtin__.quit = x
184
185
186 init()
187 del init