Show More
@@ -1,46 +1,46 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ IPython extension: add %clear magic """ |
|
3 | 3 | |
|
4 | 4 | import IPython.ipapi |
|
5 | 5 | import gc |
|
6 | 6 | ip = IPython.ipapi.get() |
|
7 | 7 | |
|
8 | 8 | |
|
9 | 9 | def clear_f(self,arg): |
|
10 | 10 | """ Clear various data (e.g. stored history data) |
|
11 | 11 | |
|
12 | 12 | %clear out - clear output history |
|
13 | 13 | %clear in - clear input history |
|
14 | 14 | """ |
|
15 | 15 | |
|
16 | 16 | api = self.getapi() |
|
17 | 17 | for target in arg.split(): |
|
18 | 18 | if target == 'out': |
|
19 |
print "Flushing output cache (%d entries)" % len(api.user_ns |
|
|
19 | print "Flushing output cache (%d entries)" % len(api.user_ns['_oh']) | |
|
20 | 20 | self.outputcache.flush() |
|
21 | 21 | elif target == 'in': |
|
22 | 22 | print "Flushing input history" |
|
23 | 23 | from IPython import iplib |
|
24 | 24 | del self.input_hist[:] |
|
25 | 25 | del self.input_hist_raw[:] |
|
26 | 26 | for n in range(1,self.outputcache.prompt_count + 1): |
|
27 | 27 | key = '_i'+`n` |
|
28 | 28 | try: |
|
29 | 29 | del self.user_ns[key] |
|
30 | 30 | except: pass |
|
31 | 31 | elif target == 'array': |
|
32 | 32 | try: |
|
33 | 33 | pylab=ip.IP.pylab |
|
34 | 34 | for x in self.user_ns.keys(): |
|
35 | 35 | if isinstance(self.user_ns[x],pylab.arraytype): |
|
36 | 36 | del self.user_ns[x] |
|
37 | 37 | except AttributeError: |
|
38 | 38 | print "Clear array only available in -pylab mode" |
|
39 | 39 | gc.collect() |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | ip.expose_magic("clear",clear_f) |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | |
|
46 | 46 |
@@ -1,66 +1,66 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ IPython extension: new prefilters for output grabbing |
|
3 | 3 | |
|
4 | 4 | Provides |
|
5 | 5 | |
|
6 | 6 | var = %magic blah blah |
|
7 | 7 | |
|
8 | 8 | var = !ls |
|
9 | 9 | |
|
10 | 10 | $Id: genutils.py 1077 2006-01-24 18:15:27Z vivainio $ |
|
11 | 11 | |
|
12 | 12 | """ |
|
13 | 13 | |
|
14 | 14 | import IPython.ipapi |
|
15 | 15 | from IPython.genutils import * |
|
16 | 16 | |
|
17 | 17 | ip = IPython.ipapi.get() |
|
18 | 18 | |
|
19 | 19 | import re |
|
20 | 20 | |
|
21 | 21 | def hnd_magic(line,mo): |
|
22 | 22 | """ Handle a = %mymagic blah blah """ |
|
23 | 23 | #cmd = genutils.make_quoted_expr(mo.group('syscmd')) |
|
24 | 24 | #mag = 'ipmagic |
|
25 | 25 | #return "%s = %s" |
|
26 | 26 | var = mo.group('varname') |
|
27 | 27 | cmd = mo.group('cmd') |
|
28 | 28 | expr = make_quoted_expr(cmd) |
|
29 | 29 | return itpl('$var = _ip.magic($expr)') |
|
30 | 30 | |
|
31 | 31 | def hnd_syscmd(line,mo): |
|
32 | 32 | """ Handle a = !ls """ |
|
33 | 33 | #cmd = genutils.make_quoted_expr(mo.group('syscmd')) |
|
34 | 34 | #mag = 'ipmagic |
|
35 | 35 | #return "%s = %s" |
|
36 | 36 | var = mo.group('varname') |
|
37 | 37 | cmd = mo.group('cmd') |
|
38 | 38 | expr = make_quoted_expr(itpl("sc -lv =$cmd")) |
|
39 | 39 | return itpl('$var = _ip.magic($expr)') |
|
40 | 40 | |
|
41 | 41 | def install_re_handler(pat, hnd): |
|
42 |
ip.meta |
|
|
42 | ip.meta.re_prefilters.append((re.compile(pat), hnd)) | |
|
43 | 43 | |
|
44 | 44 | def init_handlers(): |
|
45 | 45 | |
|
46 |
ip.meta |
|
|
46 | ip.meta.re_prefilters = [] | |
|
47 | 47 | |
|
48 | 48 | install_re_handler('(?P<varname>[\w\.]+)\s*=\s*%(?P<cmd>.*)', |
|
49 | 49 | hnd_magic |
|
50 | 50 | ) |
|
51 | 51 | |
|
52 | 52 | install_re_handler('(?P<varname>[\w\.]+)\s*=\s*!(?P<cmd>.*)', |
|
53 | 53 | hnd_syscmd |
|
54 | 54 | ) |
|
55 | 55 | |
|
56 | 56 | init_handlers() |
|
57 | 57 | |
|
58 | 58 | def regex_prefilter_f(self,line): |
|
59 |
for pat, handler in ip.meta |
|
|
59 | for pat, handler in ip.meta.re_prefilters: | |
|
60 | 60 | mo = pat.match(line) |
|
61 | 61 | if mo: |
|
62 | 62 | return handler(line,mo) |
|
63 | 63 | |
|
64 | 64 | raise IPython.ipapi.TryNext |
|
65 | 65 | |
|
66 | 66 | ip.set_hook('input_prefilter', regex_prefilter_f) |
@@ -1,1841 +1,1841 b'' | |||
|
1 | 1 | # -*- coding: iso-8859-1 -*- |
|
2 | 2 | |
|
3 | 3 | """ |
|
4 | 4 | ``ipipe`` provides classes to be used in an interactive Python session. Doing a |
|
5 | 5 | ``from ipipe import *`` is the preferred way to do this. The name of all |
|
6 | 6 | objects imported this way starts with ``i`` to minimize collisions. |
|
7 | 7 | |
|
8 | 8 | ``ipipe`` supports "pipeline expressions", which is something resembling Unix |
|
9 | 9 | pipes. An example is: |
|
10 | 10 | |
|
11 | 11 | >>> ienv | isort("key.lower()") |
|
12 | 12 | |
|
13 | 13 | This gives a listing of all environment variables sorted by name. |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | There are three types of objects in a pipeline expression: |
|
17 | 17 | |
|
18 | 18 | * ``Table``s: These objects produce items. Examples are ``ls`` (listing the |
|
19 | 19 | current directory, ``ienv`` (listing environment variables), ``ipwd`` (listing |
|
20 | 20 | user account) and ``igrp`` (listing user groups). A ``Table`` must be the |
|
21 | 21 | first object in a pipe expression. |
|
22 | 22 | |
|
23 | 23 | * ``Pipe``s: These objects sit in the middle of a pipe expression. They |
|
24 | 24 | transform the input in some way (e.g. filtering or sorting it). Examples are: |
|
25 | 25 | ``ifilter`` (which filters the input pipe), ``isort`` (which sorts the input |
|
26 | 26 | pipe) and ``ieval`` (which evaluates a function or expression for each object |
|
27 | 27 | in the input pipe). |
|
28 | 28 | |
|
29 | 29 | * ``Display``s: These objects can be put as the last object in a pipeline |
|
30 | 30 | expression. There are responsible for displaying the result of the pipeline |
|
31 | 31 | expression. If a pipeline expression doesn't end in a display object a default |
|
32 | 32 | display objects will be used. One example is ``browse`` which is a ``curses`` |
|
33 | 33 | based browser. |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | Adding support for pipeline expressions to your own objects can be done through |
|
37 | 37 | three extensions points (all of them optional): |
|
38 | 38 | |
|
39 | 39 | * An object that will be displayed as a row by a ``Display`` object should |
|
40 | 40 | implement the method ``__xattrs__(self, mode)``. This method must return a |
|
41 | 41 | sequence of attribute names. This sequence may also contain integers, which |
|
42 | 42 | will be treated as sequence indizes. Also supported is ``None``, which uses |
|
43 | 43 | the object itself and callables which will be called with the object as the |
|
44 | 44 | an argument. If ``__xattrs__()`` isn't implemented ``(None,)`` will be used as |
|
45 | 45 | the attribute sequence (i.e. the object itself (it's ``repr()`` format) will |
|
46 | 46 | be being displayed. The global function ``xattrs()`` implements this |
|
47 | 47 | functionality. |
|
48 | 48 | |
|
49 | 49 | * When an object ``foo`` is displayed in the header, footer or table cell of the |
|
50 | 50 | browser ``foo.__xrepr__(mode)`` is called. Mode can be ``"header"`` or |
|
51 | 51 | ``"footer"`` for the header or footer line and ``"cell"`` for a table cell. |
|
52 | 52 | ``__xrepr__()```must return an iterable (e.g. by being a generator) which |
|
53 | 53 | produces the following items: The first item should be a tuple containing |
|
54 | 54 | the alignment (-1 left aligned, 0 centered and 1 right aligned) and whether |
|
55 | 55 | the complete output must be displayed or if the browser is allowed to stop |
|
56 | 56 | output after enough text has been produced (e.g. a syntax highlighted text |
|
57 | 57 | line would use ``True``, but for a large data structure (i.e. a nested list, |
|
58 | 58 | tuple or dictionary) ``False`` would be used). The other output ``__xrepr__()`` |
|
59 | 59 | may produce is tuples of ``Style```objects and text (which contain the text |
|
60 | 60 | representation of the object; see the ``astyle`` module). If ``__xrepr__()`` |
|
61 | 61 | recursively outputs a data structure the function ``xrepr(object, mode)`` can |
|
62 | 62 | be used and ``"default"`` must be passed as the mode in these calls. This in |
|
63 | 63 | turn calls the ``__xrepr__()`` method on ``object`` (or uses ``repr(object)`` |
|
64 | 64 | as the string representation if ``__xrepr__()`` doesn't exist). |
|
65 | 65 | |
|
66 | 66 | * Objects that can be iterated by ``Pipe``s must implement the method |
|
67 | 67 | ``__xiter__(self, mode)``. ``mode`` can take the following values: |
|
68 | 68 | |
|
69 | 69 | - ``"default"``: This is the default value and ist always used by pipeline |
|
70 | 70 | expressions. Other values are only used in the browser. |
|
71 | 71 | - ``None``: This value is passed by the browser. The object must return an |
|
72 | 72 | iterable of ``XMode`` objects describing all modes supported by the object. |
|
73 | 73 | (This should never include ``"default"`` or ``None``). |
|
74 | 74 | - Any other value that the object supports. |
|
75 | 75 | |
|
76 | 76 | The global function ``xiter()`` can be called to get such an iterator. If |
|
77 | 77 | the method ``_xiter__`` isn't implemented, ``xiter()`` falls back to |
|
78 | 78 | ``__iter__``. In addition to that, dictionaries and modules receive special |
|
79 | 79 | treatment (returning an iterator over ``(key, value)`` pairs). This makes it |
|
80 | 80 | possible to use dictionaries and modules in pipeline expressions, for example: |
|
81 | 81 | |
|
82 | 82 | >>> import sys |
|
83 | 83 | >>> sys | ifilter("isinstance(value, int)") | idump |
|
84 | 84 | key |value |
|
85 | 85 | api_version| 1012 |
|
86 | 86 | dllhandle | 503316480 |
|
87 | 87 | hexversion | 33817328 |
|
88 | 88 | maxint |2147483647 |
|
89 | 89 | maxunicode | 65535 |
|
90 | 90 | >>> sys.modules | ifilter("_.value is not None") | isort("_.key.lower()") |
|
91 | 91 | ... |
|
92 | 92 | |
|
93 | 93 | Note: The expression strings passed to ``ifilter()`` and ``isort()`` can |
|
94 | 94 | refer to the object to be filtered or sorted via the variable ``_`` and to any |
|
95 | 95 | of the attributes of the object, i.e.: |
|
96 | 96 | |
|
97 | 97 | >>> sys.modules | ifilter("_.value is not None") | isort("_.key.lower()") |
|
98 | 98 | |
|
99 | 99 | does the same as |
|
100 | 100 | |
|
101 | 101 | >>> sys.modules | ifilter("value is not None") | isort("key.lower()") |
|
102 | 102 | |
|
103 | 103 | In addition to expression strings, it's possible to pass callables (taking |
|
104 | 104 | the object as an argument) to ``ifilter()``, ``isort()`` and ``ieval()``: |
|
105 | 105 | |
|
106 | 106 | >>> sys | ifilter(lambda _:isinstance(_.value, int)) \ |
|
107 | 107 | ... | ieval(lambda _: (_.key, hex(_.value))) | idump |
|
108 | 108 | 0 |1 |
|
109 | 109 | api_version|0x3f4 |
|
110 | 110 | dllhandle |0x1e000000 |
|
111 | 111 | hexversion |0x20402f0 |
|
112 | 112 | maxint |0x7fffffff |
|
113 | 113 | maxunicode |0xffff |
|
114 | 114 | """ |
|
115 | 115 | |
|
116 | 116 | import sys, os, os.path, stat, glob, new, csv, datetime, types |
|
117 | 117 | import itertools, mimetypes |
|
118 | 118 | |
|
119 | 119 | try: # Python 2.3 compatibility |
|
120 | 120 | import collections |
|
121 | 121 | except ImportError: |
|
122 | 122 | deque = list |
|
123 | 123 | else: |
|
124 | 124 | deque = collections.deque |
|
125 | 125 | |
|
126 | 126 | try: # Python 2.3 compatibility |
|
127 | 127 | set |
|
128 | 128 | except NameError: |
|
129 | 129 | import sets |
|
130 | 130 | set = sets.Set |
|
131 | 131 | |
|
132 | 132 | try: # Python 2.3 compatibility |
|
133 | 133 | sorted |
|
134 | 134 | except NameError: |
|
135 | 135 | def sorted(iterator, key=None, reverse=False): |
|
136 | 136 | items = list(iterator) |
|
137 | 137 | if key is not None: |
|
138 | 138 | items.sort(lambda i1, i2: cmp(key(i1), key(i2))) |
|
139 | 139 | else: |
|
140 | 140 | items.sort() |
|
141 | 141 | if reverse: |
|
142 | 142 | items.reverse() |
|
143 | 143 | return items |
|
144 | 144 | |
|
145 | 145 | try: |
|
146 | 146 | import pwd |
|
147 | 147 | except ImportError: |
|
148 | 148 | pwd = None |
|
149 | 149 | |
|
150 | 150 | try: |
|
151 | 151 | import grp |
|
152 | 152 | except ImportError: |
|
153 | 153 | grp = None |
|
154 | 154 | |
|
155 | 155 | import path |
|
156 | 156 | try: |
|
157 | 157 | from IPython import genutils, ipapi |
|
158 | 158 | except ImportError: |
|
159 | 159 | genutils = None |
|
160 | 160 | ipapi = None |
|
161 | 161 | |
|
162 | 162 | import astyle |
|
163 | 163 | |
|
164 | 164 | |
|
165 | 165 | __all__ = [ |
|
166 | 166 | "ifile", "ils", "iglob", "iwalk", "ipwdentry", "ipwd", "igrpentry", "igrp", |
|
167 | 167 | "icsv", "ix", "ichain", "isort", "ifilter", "ieval", "ienum", "ienv", |
|
168 | 168 | "idump", "iless" |
|
169 | 169 | ] |
|
170 | 170 | |
|
171 | 171 | |
|
172 | 172 | os.stat_float_times(True) # enable microseconds |
|
173 | 173 | |
|
174 | 174 | |
|
175 | 175 | class AttrNamespace(object): |
|
176 | 176 | """ |
|
177 | 177 | Helper class that is used for providing a namespace for evaluating |
|
178 | 178 | expressions containing attribute names of an object. |
|
179 | 179 | """ |
|
180 | 180 | def __init__(self, wrapped): |
|
181 | 181 | self.wrapped = wrapped |
|
182 | 182 | |
|
183 | 183 | def __getitem__(self, name): |
|
184 | 184 | if name == "_": |
|
185 | 185 | return self.wrapped |
|
186 | 186 | try: |
|
187 | 187 | return getattr(self.wrapped, name) |
|
188 | 188 | except AttributeError: |
|
189 | 189 | raise KeyError(name) |
|
190 | 190 | |
|
191 | 191 | # Python 2.3 compatibility |
|
192 | 192 | # use eval workaround to find out which names are used in the |
|
193 | 193 | # eval string and put them into the locals. This works for most |
|
194 | 194 | # normal uses case, bizarre ones like accessing the locals() |
|
195 | 195 | # will fail |
|
196 | 196 | try: |
|
197 | 197 | eval("_", None, AttrNamespace(None)) |
|
198 | 198 | except TypeError: |
|
199 | 199 | real_eval = eval |
|
200 | 200 | def eval(codestring, _globals, _locals): |
|
201 | 201 | """ |
|
202 | 202 | eval(source[, globals[, locals]]) -> value |
|
203 | 203 | |
|
204 | 204 | Evaluate the source in the context of globals and locals. |
|
205 | 205 | The source may be a string representing a Python expression |
|
206 | 206 | or a code object as returned by compile(). |
|
207 | 207 | The globals must be a dictionary and locals can be any mappping. |
|
208 | 208 | |
|
209 | 209 | This function is a workaround for the shortcomings of |
|
210 | 210 | Python 2.3's eval. |
|
211 | 211 | """ |
|
212 | 212 | |
|
213 | 213 | code = compile(codestring, "_eval", "eval") |
|
214 | 214 | newlocals = {} |
|
215 | 215 | for name in code.co_names: |
|
216 | 216 | try: |
|
217 | 217 | newlocals[name] = _locals[name] |
|
218 | 218 | except KeyError: |
|
219 | 219 | pass |
|
220 | 220 | return real_eval(code, _globals, newlocals) |
|
221 | 221 | |
|
222 | 222 | |
|
223 | 223 | noitem = object() |
|
224 | 224 | |
|
225 | 225 | def item(iterator, index, default=noitem): |
|
226 | 226 | """ |
|
227 | 227 | Return the ``index``th item from the iterator ``iterator``. |
|
228 | 228 | ``index`` must be an integer (negative integers are relative to the |
|
229 | 229 | end (i.e. the last item produced by the iterator)). |
|
230 | 230 | |
|
231 | 231 | If ``default`` is given, this will be the default value when |
|
232 | 232 | the iterator doesn't contain an item at this position. Otherwise an |
|
233 | 233 | ``IndexError`` will be raised. |
|
234 | 234 | |
|
235 | 235 | Note that using this function will partially or totally exhaust the |
|
236 | 236 | iterator. |
|
237 | 237 | """ |
|
238 | 238 | i = index |
|
239 | 239 | if i>=0: |
|
240 | 240 | for item in iterator: |
|
241 | 241 | if not i: |
|
242 | 242 | return item |
|
243 | 243 | i -= 1 |
|
244 | 244 | else: |
|
245 | 245 | i = -index |
|
246 | 246 | cache = deque() |
|
247 | 247 | for item in iterator: |
|
248 | 248 | cache.append(item) |
|
249 | 249 | if len(cache)>i: |
|
250 | 250 | cache.popleft() |
|
251 | 251 | if len(cache)==i: |
|
252 | 252 | return cache.popleft() |
|
253 | 253 | if default is noitem: |
|
254 | 254 | raise IndexError(index) |
|
255 | 255 | else: |
|
256 | 256 | return default |
|
257 | 257 | |
|
258 | 258 | |
|
259 | 259 | def getglobals(g): |
|
260 | 260 | if g is None: |
|
261 | 261 | if ipapi is not None: |
|
262 |
return ipapi.get().user_ns |
|
|
262 | return ipapi.get().user_ns | |
|
263 | 263 | else: |
|
264 | 264 | return globals() |
|
265 | 265 | return g |
|
266 | 266 | |
|
267 | 267 | |
|
268 | 268 | class Table(object): |
|
269 | 269 | """ |
|
270 | 270 | A ``Table`` is an object that produces items (just like a normal Python |
|
271 | 271 | iterator/generator does) and can be used as the first object in a pipeline |
|
272 | 272 | expression. The displayhook will open the default browser for such an object |
|
273 | 273 | (instead of simply printing the ``repr()`` result). |
|
274 | 274 | """ |
|
275 | 275 | |
|
276 | 276 | # We want to support ``foo`` and ``foo()`` in pipeline expression: |
|
277 | 277 | # So we implement the required operators (``|`` and ``+``) in the metaclass, |
|
278 | 278 | # instantiate the class and forward the operator to the instance |
|
279 | 279 | class __metaclass__(type): |
|
280 | 280 | def __iter__(self): |
|
281 | 281 | return iter(self()) |
|
282 | 282 | |
|
283 | 283 | def __or__(self, other): |
|
284 | 284 | return self() | other |
|
285 | 285 | |
|
286 | 286 | def __add__(self, other): |
|
287 | 287 | return self() + other |
|
288 | 288 | |
|
289 | 289 | def __radd__(self, other): |
|
290 | 290 | return other + self() |
|
291 | 291 | |
|
292 | 292 | def __getitem__(self, index): |
|
293 | 293 | return self()[index] |
|
294 | 294 | |
|
295 | 295 | def __getitem__(self, index): |
|
296 | 296 | return item(self, index) |
|
297 | 297 | |
|
298 | 298 | def __contains__(self, item): |
|
299 | 299 | for haveitem in self: |
|
300 | 300 | if item == haveitem: |
|
301 | 301 | return True |
|
302 | 302 | return False |
|
303 | 303 | |
|
304 | 304 | def __or__(self, other): |
|
305 | 305 | # autoinstantiate right hand side |
|
306 | 306 | if isinstance(other, type) and issubclass(other, (Table, Display)): |
|
307 | 307 | other = other() |
|
308 | 308 | # treat simple strings and functions as ``ieval`` instances |
|
309 | 309 | elif not isinstance(other, Display) and not isinstance(other, Table): |
|
310 | 310 | other = ieval(other) |
|
311 | 311 | # forward operations to the right hand side |
|
312 | 312 | return other.__ror__(self) |
|
313 | 313 | |
|
314 | 314 | def __add__(self, other): |
|
315 | 315 | # autoinstantiate right hand side |
|
316 | 316 | if isinstance(other, type) and issubclass(other, Table): |
|
317 | 317 | other = other() |
|
318 | 318 | return ichain(self, other) |
|
319 | 319 | |
|
320 | 320 | def __radd__(self, other): |
|
321 | 321 | # autoinstantiate left hand side |
|
322 | 322 | if isinstance(other, type) and issubclass(other, Table): |
|
323 | 323 | other = other() |
|
324 | 324 | return ichain(other, self) |
|
325 | 325 | |
|
326 | 326 | def __iter__(self): |
|
327 | 327 | return xiter(self, "default") |
|
328 | 328 | |
|
329 | 329 | |
|
330 | 330 | class Pipe(Table): |
|
331 | 331 | """ |
|
332 | 332 | A ``Pipe`` is an object that can be used in a pipeline expression. It |
|
333 | 333 | processes the objects it gets from its input ``Table``/``Pipe``. Note that |
|
334 | 334 | a ``Pipe`` object can't be used as the first object in a pipeline |
|
335 | 335 | expression, as it doesn't produces items itself. |
|
336 | 336 | """ |
|
337 | 337 | class __metaclass__(Table.__metaclass__): |
|
338 | 338 | def __ror__(self, input): |
|
339 | 339 | return input | self() |
|
340 | 340 | |
|
341 | 341 | def __ror__(self, input): |
|
342 | 342 | # autoinstantiate left hand side |
|
343 | 343 | if isinstance(input, type) and issubclass(input, Table): |
|
344 | 344 | input = input() |
|
345 | 345 | self.input = input |
|
346 | 346 | return self |
|
347 | 347 | |
|
348 | 348 | |
|
349 | 349 | def _getattr(obj, name, default=noitem): |
|
350 | 350 | """ |
|
351 | 351 | Internal helper for getting an attribute of an item. If ``name`` is ``None`` |
|
352 | 352 | return the object itself. If ``name`` is an integer, use ``__getitem__`` |
|
353 | 353 | instead. If the attribute or item does not exist, return ``default``. |
|
354 | 354 | """ |
|
355 | 355 | if name is None: |
|
356 | 356 | return obj |
|
357 | 357 | elif isinstance(name, basestring): |
|
358 | 358 | if name.endswith("()"): |
|
359 | 359 | return getattr(obj, name[:-2], default)() |
|
360 | 360 | else: |
|
361 | 361 | return getattr(obj, name, default) |
|
362 | 362 | elif callable(name): |
|
363 | 363 | try: |
|
364 | 364 | return name(obj) |
|
365 | 365 | except AttributeError: |
|
366 | 366 | return default |
|
367 | 367 | else: |
|
368 | 368 | try: |
|
369 | 369 | return obj[name] |
|
370 | 370 | except IndexError: |
|
371 | 371 | return default |
|
372 | 372 | |
|
373 | 373 | |
|
374 | 374 | def _attrname(name): |
|
375 | 375 | """ |
|
376 | 376 | Internal helper that gives a proper name for the attribute ``name`` |
|
377 | 377 | (which might be ``None`` or an ``int``). |
|
378 | 378 | """ |
|
379 | 379 | if name is None: |
|
380 | 380 | return "_" |
|
381 | 381 | elif isinstance(name, basestring): |
|
382 | 382 | return name |
|
383 | 383 | elif callable(name): |
|
384 | 384 | return getattr(name, "__xname__", name.__name__) |
|
385 | 385 | else: |
|
386 | 386 | return str(name) |
|
387 | 387 | |
|
388 | 388 | |
|
389 | 389 | def xrepr(item, mode): |
|
390 | 390 | try: |
|
391 | 391 | func = item.__xrepr__ |
|
392 | 392 | except AttributeError: |
|
393 | 393 | pass |
|
394 | 394 | else: |
|
395 | 395 | try: |
|
396 | 396 | for x in func(mode): |
|
397 | 397 | yield x |
|
398 | 398 | except (KeyboardInterrupt, SystemExit): |
|
399 | 399 | raise |
|
400 | 400 | except Exception: |
|
401 | 401 | yield (astyle.style_default, repr(item)) |
|
402 | 402 | return |
|
403 | 403 | if item is None: |
|
404 | 404 | yield (astyle.style_type_none, repr(item)) |
|
405 | 405 | elif isinstance(item, bool): |
|
406 | 406 | yield (astyle.style_type_bool, repr(item)) |
|
407 | 407 | elif isinstance(item, str): |
|
408 | 408 | if mode == "cell": |
|
409 | 409 | yield (astyle.style_default, repr(item.expandtabs(tab))[1:-1]) |
|
410 | 410 | else: |
|
411 | 411 | yield (astyle.style_default, repr(item)) |
|
412 | 412 | elif isinstance(item, unicode): |
|
413 | 413 | if mode == "cell": |
|
414 | 414 | yield (astyle.style_default, repr(item.expandtabs(tab))[2:-1]) |
|
415 | 415 | else: |
|
416 | 416 | yield (astyle.style_default, repr(item)) |
|
417 | 417 | elif isinstance(item, (int, long, float)): |
|
418 | 418 | yield (1, True) |
|
419 | 419 | yield (astyle.style_type_number, repr(item)) |
|
420 | 420 | elif isinstance(item, complex): |
|
421 | 421 | yield (astyle.style_type_number, repr(item)) |
|
422 | 422 | elif isinstance(item, datetime.datetime): |
|
423 | 423 | if mode == "cell": |
|
424 | 424 | # Don't use strftime() here, as this requires year >= 1900 |
|
425 | 425 | yield (astyle.style_type_datetime, |
|
426 | 426 | "%04d-%02d-%02d %02d:%02d:%02d.%06d" % \ |
|
427 | 427 | (item.year, item.month, item.day, |
|
428 | 428 | item.hour, item.minute, item.second, |
|
429 | 429 | item.microsecond), |
|
430 | 430 | ) |
|
431 | 431 | else: |
|
432 | 432 | yield (astyle.style_type_datetime, repr(item)) |
|
433 | 433 | elif isinstance(item, datetime.date): |
|
434 | 434 | if mode == "cell": |
|
435 | 435 | yield (astyle.style_type_datetime, |
|
436 | 436 | "%04d-%02d-%02d" % (item.year, item.month, item.day)) |
|
437 | 437 | else: |
|
438 | 438 | yield (astyle.style_type_datetime, repr(item)) |
|
439 | 439 | elif isinstance(item, datetime.time): |
|
440 | 440 | if mode == "cell": |
|
441 | 441 | yield (astyle.style_type_datetime, |
|
442 | 442 | "%02d:%02d:%02d.%06d" % \ |
|
443 | 443 | (item.hour, item.minute, item.second, item.microsecond)) |
|
444 | 444 | else: |
|
445 | 445 | yield (astyle.style_type_datetime, repr(item)) |
|
446 | 446 | elif isinstance(item, datetime.timedelta): |
|
447 | 447 | yield (astyle.style_type_datetime, repr(item)) |
|
448 | 448 | elif isinstance(item, Exception): |
|
449 | 449 | if item.__class__.__module__ == "exceptions": |
|
450 | 450 | classname = item.__class__.__name__ |
|
451 | 451 | else: |
|
452 | 452 | classname = "%s.%s" % \ |
|
453 | 453 | (item.__class__.__module__, item.__class__.__name__) |
|
454 | 454 | if mode == "header" or mode == "footer": |
|
455 | 455 | yield (astyle.style_error, "%s: %s" % (classname, item)) |
|
456 | 456 | else: |
|
457 | 457 | yield (astyle.style_error, classname) |
|
458 | 458 | elif isinstance(item, (list, tuple)): |
|
459 | 459 | if mode == "header" or mode == "footer": |
|
460 | 460 | if item.__class__.__module__ == "__builtin__": |
|
461 | 461 | classname = item.__class__.__name__ |
|
462 | 462 | else: |
|
463 | 463 | classname = "%s.%s" % \ |
|
464 | 464 | (item.__class__.__module__,item.__class__.__name__) |
|
465 | 465 | yield (astyle.style_default, |
|
466 | 466 | "<%s object with %d items at 0x%x>" % \ |
|
467 | 467 | (classname, len(item), id(item))) |
|
468 | 468 | else: |
|
469 | 469 | yield (-1, False) |
|
470 | 470 | if isinstance(item, list): |
|
471 | 471 | yield (astyle.style_default, "[") |
|
472 | 472 | end = "]" |
|
473 | 473 | else: |
|
474 | 474 | yield (astyle.style_default, "(") |
|
475 | 475 | end = ")" |
|
476 | 476 | for (i, subitem) in enumerate(item): |
|
477 | 477 | if i: |
|
478 | 478 | yield (astyle.style_default, ", ") |
|
479 | 479 | for part in xrepr(subitem, "default"): |
|
480 | 480 | yield part |
|
481 | 481 | yield (astyle.style_default, end) |
|
482 | 482 | elif isinstance(item, (dict, types.DictProxyType)): |
|
483 | 483 | if mode == "header" or mode == "footer": |
|
484 | 484 | if item.__class__.__module__ == "__builtin__": |
|
485 | 485 | classname = item.__class__.__name__ |
|
486 | 486 | else: |
|
487 | 487 | classname = "%s.%s" % \ |
|
488 | 488 | (item.__class__.__module__,item.__class__.__name__) |
|
489 | 489 | yield (astyle.style_default, |
|
490 | 490 | "<%s object with %d items at 0x%x>" % \ |
|
491 | 491 | (classname, len(item), id(item))) |
|
492 | 492 | else: |
|
493 | 493 | yield (-1, False) |
|
494 | 494 | if isinstance(item, dict): |
|
495 | 495 | yield (astyle.style_default, "{") |
|
496 | 496 | end = "}" |
|
497 | 497 | else: |
|
498 | 498 | yield (astyle.style_default, "dictproxy((") |
|
499 | 499 | end = "})" |
|
500 | 500 | for (i, (key, value)) in enumerate(item.iteritems()): |
|
501 | 501 | if i: |
|
502 | 502 | yield (astyle.style_default, ", ") |
|
503 | 503 | for part in xrepr(key, "default"): |
|
504 | 504 | yield part |
|
505 | 505 | yield (astyle.style_default, ": ") |
|
506 | 506 | for part in xrepr(value, "default"): |
|
507 | 507 | yield part |
|
508 | 508 | yield (astyle.style_default, end) |
|
509 | 509 | else: |
|
510 | 510 | yield (astyle.style_default, repr(item)) |
|
511 | 511 | |
|
512 | 512 | |
|
513 | 513 | def xattrs(item, mode): |
|
514 | 514 | try: |
|
515 | 515 | func = item.__xattrs__ |
|
516 | 516 | except AttributeError: |
|
517 | 517 | if mode == "detail": |
|
518 | 518 | return dir(item) |
|
519 | 519 | else: |
|
520 | 520 | return (None,) |
|
521 | 521 | else: |
|
522 | 522 | try: |
|
523 | 523 | return func(mode) |
|
524 | 524 | except (KeyboardInterrupt, SystemExit): |
|
525 | 525 | raise |
|
526 | 526 | except Exception: |
|
527 | 527 | return (None,) |
|
528 | 528 | |
|
529 | 529 | |
|
530 | 530 | def xiter(item, mode): |
|
531 | 531 | if mode == "detail": |
|
532 | 532 | def items(): |
|
533 | 533 | for name in xattrs(item, mode): |
|
534 | 534 | yield XAttr(item, name) |
|
535 | 535 | return items() |
|
536 | 536 | try: |
|
537 | 537 | func = item.__xiter__ |
|
538 | 538 | except AttributeError: |
|
539 | 539 | if isinstance(item, (dict, types.DictProxyType)): |
|
540 | 540 | def items(item): |
|
541 | 541 | fields = ("key", "value") |
|
542 | 542 | for (key, value) in item.iteritems(): |
|
543 | 543 | yield Fields(fields, key=key, value=value) |
|
544 | 544 | return items(item) |
|
545 | 545 | elif isinstance(item, new.module): |
|
546 | 546 | def items(item): |
|
547 | 547 | fields = ("key", "value") |
|
548 | 548 | for key in sorted(item.__dict__): |
|
549 | 549 | yield Fields(fields, key=key, value=getattr(item, key)) |
|
550 | 550 | return items(item) |
|
551 | 551 | elif isinstance(item, basestring): |
|
552 | 552 | if not len(item): |
|
553 | 553 | raise ValueError("can't enter empty string") |
|
554 | 554 | lines = item.splitlines() |
|
555 | 555 | if len(lines) <= 1: |
|
556 | 556 | raise ValueError("can't enter one line string") |
|
557 | 557 | return iter(lines) |
|
558 | 558 | return iter(item) |
|
559 | 559 | else: |
|
560 | 560 | return iter(func(mode)) # iter() just to be safe |
|
561 | 561 | |
|
562 | 562 | |
|
563 | 563 | class ichain(Pipe): |
|
564 | 564 | """ |
|
565 | 565 | Chains multiple ``Table``s into one. |
|
566 | 566 | """ |
|
567 | 567 | |
|
568 | 568 | def __init__(self, *iters): |
|
569 | 569 | self.iters = iters |
|
570 | 570 | |
|
571 | 571 | def __xiter__(self, mode): |
|
572 | 572 | return itertools.chain(*self.iters) |
|
573 | 573 | |
|
574 | 574 | def __xrepr__(self, mode): |
|
575 | 575 | if mode == "header" or mode == "footer": |
|
576 | 576 | for (i, item) in enumerate(self.iters): |
|
577 | 577 | if i: |
|
578 | 578 | yield (astyle.style_default, "+") |
|
579 | 579 | if isinstance(item, Pipe): |
|
580 | 580 | yield (astyle.style_default, "(") |
|
581 | 581 | for part in xrepr(item, mode): |
|
582 | 582 | yield part |
|
583 | 583 | if isinstance(item, Pipe): |
|
584 | 584 | yield (astyle.style_default, ")") |
|
585 | 585 | else: |
|
586 | 586 | yield (astyle.style_default, repr(self)) |
|
587 | 587 | |
|
588 | 588 | def __repr__(self): |
|
589 | 589 | args = ", ".join([repr(it) for it in self.iters]) |
|
590 | 590 | return "%s.%s(%s)" % \ |
|
591 | 591 | (self.__class__.__module__, self.__class__.__name__, args) |
|
592 | 592 | |
|
593 | 593 | |
|
594 | 594 | class ifile(path.path): |
|
595 | 595 | """ |
|
596 | 596 | file (or directory) object. |
|
597 | 597 | """ |
|
598 | 598 | |
|
599 | 599 | def __add_(self, other): |
|
600 | 600 | return ifile(path._base(self) + other) |
|
601 | 601 | |
|
602 | 602 | def __radd_(self, other): |
|
603 | 603 | return ifile(other + path._base(self)) |
|
604 | 604 | |
|
605 | 605 | def __div_(self, other): |
|
606 | 606 | return ifile(path.__div__(self, other)) |
|
607 | 607 | |
|
608 | 608 | def getcwd(): |
|
609 | 609 | return ifile(path.path.getcwd()) |
|
610 | 610 | getcwd.__doc__ = path.path.getcwd.__doc__ |
|
611 | 611 | getcwd = staticmethod(getcwd) |
|
612 | 612 | |
|
613 | 613 | def abspath(self): |
|
614 | 614 | return ifile(path.path.abspath(self)) |
|
615 | 615 | abspath.__doc__ = path.path.abspath.__doc__ |
|
616 | 616 | |
|
617 | 617 | def normcase(self): |
|
618 | 618 | return ifile(path.path.normcase(self)) |
|
619 | 619 | normcase.__doc__ = path.path.normcase.__doc__ |
|
620 | 620 | |
|
621 | 621 | def normpath(self): |
|
622 | 622 | return ifile(path.path.normpath(self)) |
|
623 | 623 | normpath.__doc__ = path.path.normpath.__doc__ |
|
624 | 624 | |
|
625 | 625 | def realpath(self): |
|
626 | 626 | return ifile(path.path.realpath(self)) |
|
627 | 627 | realpath.__doc__ = path.path.realpath.__doc__ |
|
628 | 628 | |
|
629 | 629 | def expanduser(self): |
|
630 | 630 | return ifile(path.path.expanduser(self)) |
|
631 | 631 | expanduser.__doc__ = path.path.expanduser.__doc__ |
|
632 | 632 | |
|
633 | 633 | def expandvars(self): |
|
634 | 634 | return ifile(path.path.expandvars(self)) |
|
635 | 635 | expandvars.__doc__ = path.path.expandvars.__doc__ |
|
636 | 636 | |
|
637 | 637 | def dirname(self): |
|
638 | 638 | return ifile(path.path.dirname(self)) |
|
639 | 639 | dirname.__doc__ = path.path.dirname.__doc__ |
|
640 | 640 | |
|
641 | 641 | parent = property(dirname, None, None, path.path.parent.__doc__) |
|
642 | 642 | |
|
643 | 643 | def splitpath(self): |
|
644 | 644 | (parent, child) = path.path.splitpath(self) |
|
645 | 645 | return (ifile(parent), child) |
|
646 | 646 | splitpath.__doc__ = path.path.splitpath.__doc__ |
|
647 | 647 | |
|
648 | 648 | def splitdrive(self): |
|
649 | 649 | (drive, rel) = path.path.splitdrive(self) |
|
650 | 650 | return (ifile(drive), rel) |
|
651 | 651 | splitdrive.__doc__ = path.path.splitdrive.__doc__ |
|
652 | 652 | |
|
653 | 653 | def splitext(self): |
|
654 | 654 | (filename, ext) = path.path.splitext(self) |
|
655 | 655 | return (ifile(filename), ext) |
|
656 | 656 | splitext.__doc__ = path.path.splitext.__doc__ |
|
657 | 657 | |
|
658 | 658 | if hasattr(path.path, "splitunc"): |
|
659 | 659 | def splitunc(self): |
|
660 | 660 | (unc, rest) = path.path.splitunc(self) |
|
661 | 661 | return (ifile(unc), rest) |
|
662 | 662 | splitunc.__doc__ = path.path.splitunc.__doc__ |
|
663 | 663 | |
|
664 | 664 | def _get_uncshare(self): |
|
665 | 665 | unc, r = os.path.splitunc(self) |
|
666 | 666 | return ifile(unc) |
|
667 | 667 | |
|
668 | 668 | uncshare = property( |
|
669 | 669 | _get_uncshare, None, None, |
|
670 | 670 | """ The UNC mount point for this path. |
|
671 | 671 | This is empty for paths on local drives. """) |
|
672 | 672 | |
|
673 | 673 | def joinpath(self, *args): |
|
674 | 674 | return ifile(path.path.joinpath(self, *args)) |
|
675 | 675 | joinpath.__doc__ = path.path.joinpath.__doc__ |
|
676 | 676 | |
|
677 | 677 | def splitall(self): |
|
678 | 678 | return map(ifile, path.path.splitall(self)) |
|
679 | 679 | splitall.__doc__ = path.path.splitall.__doc__ |
|
680 | 680 | |
|
681 | 681 | def relpath(self): |
|
682 | 682 | return ifile(path.path.relpath(self)) |
|
683 | 683 | relpath.__doc__ = path.path.relpath.__doc__ |
|
684 | 684 | |
|
685 | 685 | def relpathto(self, dest): |
|
686 | 686 | return ifile(path.path.relpathto(self, dest)) |
|
687 | 687 | relpathto.__doc__ = path.path.relpathto.__doc__ |
|
688 | 688 | |
|
689 | 689 | def listdir(self, pattern=None): |
|
690 | 690 | return [ifile(child) for child in path.path.listdir(self, pattern)] |
|
691 | 691 | listdir.__doc__ = path.path.listdir.__doc__ |
|
692 | 692 | |
|
693 | 693 | def dirs(self, pattern=None): |
|
694 | 694 | return [ifile(child) for child in path.path.dirs(self, pattern)] |
|
695 | 695 | dirs.__doc__ = path.path.dirs.__doc__ |
|
696 | 696 | |
|
697 | 697 | def files(self, pattern=None): |
|
698 | 698 | return [ifile(child) for child in path.path.files(self, pattern)] |
|
699 | 699 | files.__doc__ = path.path.files.__doc__ |
|
700 | 700 | |
|
701 | 701 | def walk(self, pattern=None): |
|
702 | 702 | for child in path.path.walk(self, pattern): |
|
703 | 703 | yield ifile(child) |
|
704 | 704 | walk.__doc__ = path.path.walk.__doc__ |
|
705 | 705 | |
|
706 | 706 | def walkdirs(self, pattern=None): |
|
707 | 707 | for child in path.path.walkdirs(self, pattern): |
|
708 | 708 | yield ifile(child) |
|
709 | 709 | walkdirs.__doc__ = path.path.walkdirs.__doc__ |
|
710 | 710 | |
|
711 | 711 | def walkfiles(self, pattern=None): |
|
712 | 712 | for child in path.path.walkfiles(self, pattern): |
|
713 | 713 | yield ifile(child) |
|
714 | 714 | walkfiles.__doc__ = path.path.walkfiles.__doc__ |
|
715 | 715 | |
|
716 | 716 | def glob(self, pattern): |
|
717 | 717 | return map(ifile, path.path.glob(self, pattern)) |
|
718 | 718 | glob.__doc__ = path.path.glob.__doc__ |
|
719 | 719 | |
|
720 | 720 | if hasattr(os, 'readlink'): |
|
721 | 721 | def readlink(self): |
|
722 | 722 | return ifile(path.path.readlink(self)) |
|
723 | 723 | readlink.__doc__ = path.path.readlink.__doc__ |
|
724 | 724 | |
|
725 | 725 | def readlinkabs(self): |
|
726 | 726 | return ifile(path.path.readlinkabs(self)) |
|
727 | 727 | readlinkabs.__doc__ = path.path.readlinkabs.__doc__ |
|
728 | 728 | |
|
729 | 729 | def getmode(self): |
|
730 | 730 | return self.stat().st_mode |
|
731 | 731 | mode = property(getmode, None, None, "Access mode") |
|
732 | 732 | |
|
733 | 733 | def gettype(self): |
|
734 | 734 | data = [ |
|
735 | 735 | (stat.S_ISREG, "file"), |
|
736 | 736 | (stat.S_ISDIR, "dir"), |
|
737 | 737 | (stat.S_ISCHR, "chardev"), |
|
738 | 738 | (stat.S_ISBLK, "blockdev"), |
|
739 | 739 | (stat.S_ISFIFO, "fifo"), |
|
740 | 740 | (stat.S_ISLNK, "symlink"), |
|
741 | 741 | (stat.S_ISSOCK,"socket"), |
|
742 | 742 | ] |
|
743 | 743 | lstat = self.lstat() |
|
744 | 744 | if lstat is not None: |
|
745 | 745 | types = set([text for (func, text) in data if func(lstat.st_mode)]) |
|
746 | 746 | else: |
|
747 | 747 | types = set() |
|
748 | 748 | m = self.mode |
|
749 | 749 | types.update([text for (func, text) in data if func(m)]) |
|
750 | 750 | return ", ".join(types) |
|
751 | 751 | type = property(gettype, None, None, "file type (file, directory, link, etc.)") |
|
752 | 752 | |
|
753 | 753 | def getmodestr(self): |
|
754 | 754 | m = self.mode |
|
755 | 755 | data = [ |
|
756 | 756 | (stat.S_IRUSR, "-r"), |
|
757 | 757 | (stat.S_IWUSR, "-w"), |
|
758 | 758 | (stat.S_IXUSR, "-x"), |
|
759 | 759 | (stat.S_IRGRP, "-r"), |
|
760 | 760 | (stat.S_IWGRP, "-w"), |
|
761 | 761 | (stat.S_IXGRP, "-x"), |
|
762 | 762 | (stat.S_IROTH, "-r"), |
|
763 | 763 | (stat.S_IWOTH, "-w"), |
|
764 | 764 | (stat.S_IXOTH, "-x"), |
|
765 | 765 | ] |
|
766 | 766 | return "".join([text[bool(m&bit)] for (bit, text) in data]) |
|
767 | 767 | |
|
768 | 768 | modestr = property(getmodestr, None, None, "Access mode as string") |
|
769 | 769 | |
|
770 | 770 | def getblocks(self): |
|
771 | 771 | return self.stat().st_blocks |
|
772 | 772 | blocks = property(getblocks, None, None, "File size in blocks") |
|
773 | 773 | |
|
774 | 774 | def getblksize(self): |
|
775 | 775 | return self.stat().st_blksize |
|
776 | 776 | blksize = property(getblksize, None, None, "Filesystem block size") |
|
777 | 777 | |
|
778 | 778 | def getdev(self): |
|
779 | 779 | return self.stat().st_dev |
|
780 | 780 | dev = property(getdev) |
|
781 | 781 | |
|
782 | 782 | def getnlink(self): |
|
783 | 783 | return self.stat().st_nlink |
|
784 | 784 | nlink = property(getnlink, None, None, "Number of links") |
|
785 | 785 | |
|
786 | 786 | def getuid(self): |
|
787 | 787 | return self.stat().st_uid |
|
788 | 788 | uid = property(getuid, None, None, "User id of file owner") |
|
789 | 789 | |
|
790 | 790 | def getgid(self): |
|
791 | 791 | return self.stat().st_gid |
|
792 | 792 | gid = property(getgid, None, None, "Group id of file owner") |
|
793 | 793 | |
|
794 | 794 | def getowner(self): |
|
795 | 795 | stat = self.stat() |
|
796 | 796 | try: |
|
797 | 797 | return pwd.getpwuid(stat.st_uid).pw_name |
|
798 | 798 | except KeyError: |
|
799 | 799 | return stat.st_uid |
|
800 | 800 | owner = property(getowner, None, None, "Owner name (or id)") |
|
801 | 801 | |
|
802 | 802 | def getgroup(self): |
|
803 | 803 | stat = self.stat() |
|
804 | 804 | try: |
|
805 | 805 | return grp.getgrgid(stat.st_gid).gr_name |
|
806 | 806 | except KeyError: |
|
807 | 807 | return stat.st_gid |
|
808 | 808 | group = property(getgroup, None, None, "Group name (or id)") |
|
809 | 809 | |
|
810 | 810 | def getadate(self): |
|
811 | 811 | return datetime.datetime.utcfromtimestamp(self.atime) |
|
812 | 812 | adate = property(getadate, None, None, "Access date") |
|
813 | 813 | |
|
814 | 814 | def getcdate(self): |
|
815 | 815 | return datetime.datetime.utcfromtimestamp(self.ctime) |
|
816 | 816 | cdate = property(getcdate, None, None, "Creation date") |
|
817 | 817 | |
|
818 | 818 | def getmdate(self): |
|
819 | 819 | return datetime.datetime.utcfromtimestamp(self.mtime) |
|
820 | 820 | mdate = property(getmdate, None, None, "Modification date") |
|
821 | 821 | |
|
822 | 822 | def getmimetype(self): |
|
823 | 823 | return mimetypes.guess_type(self.basename())[0] |
|
824 | 824 | mimetype = property(getmimetype, None, None, "MIME type") |
|
825 | 825 | |
|
826 | 826 | def getencoding(self): |
|
827 | 827 | return mimetypes.guess_type(self.basename())[1] |
|
828 | 828 | encoding = property(getencoding, None, None, "Compression") |
|
829 | 829 | |
|
830 | 830 | def __repr__(self): |
|
831 | 831 | return "ifile(%s)" % path._base.__repr__(self) |
|
832 | 832 | |
|
833 | 833 | defaultattrs = (None, "type", "size", "modestr", "owner", "group", "mdate") |
|
834 | 834 | |
|
835 | 835 | def __xattrs__(self, mode): |
|
836 | 836 | if mode == "detail": |
|
837 | 837 | return ( |
|
838 | 838 | "name", "basename()", "abspath()", "realpath()", |
|
839 | 839 | "type", "mode", "modestr", "stat()", "lstat()", |
|
840 | 840 | "uid", "gid", "owner", "group", "dev", "nlink", |
|
841 | 841 | "ctime", "mtime", "atime", "cdate", "mdate", "adate", |
|
842 | 842 | "size", "blocks", "blksize", "isdir()", "islink()", |
|
843 | 843 | "mimetype", "encoding" |
|
844 | 844 | ) |
|
845 | 845 | return self.defaultattrs |
|
846 | 846 | |
|
847 | 847 | def __xrepr__(self, mode): |
|
848 | 848 | try: |
|
849 | 849 | if self.isdir(): |
|
850 | 850 | name = "idir" |
|
851 | 851 | style = astyle.style_dir |
|
852 | 852 | else: |
|
853 | 853 | name = "ifile" |
|
854 | 854 | style = astyle.style_file |
|
855 | 855 | except IOError: |
|
856 | 856 | name = "ifile" |
|
857 | 857 | style = astyle.style_default |
|
858 | 858 | if mode == "cell" or mode in "header" or mode == "footer": |
|
859 | 859 | abspath = repr(path._base(self.normpath())) |
|
860 | 860 | if abspath.startswith("u"): |
|
861 | 861 | abspath = abspath[2:-1] |
|
862 | 862 | else: |
|
863 | 863 | abspath = abspath[1:-1] |
|
864 | 864 | if mode == "cell": |
|
865 | 865 | yield (style, abspath) |
|
866 | 866 | else: |
|
867 | 867 | yield (style, "%s(%s)" % (name, abspath)) |
|
868 | 868 | else: |
|
869 | 869 | yield (style, repr(self)) |
|
870 | 870 | |
|
871 | 871 | def __xiter__(self, mode): |
|
872 | 872 | if self.isdir(): |
|
873 | 873 | yield iparentdir(self / os.pardir) |
|
874 | 874 | for child in sorted(self.listdir()): |
|
875 | 875 | yield child |
|
876 | 876 | else: |
|
877 | 877 | f = self.open("rb") |
|
878 | 878 | for line in f: |
|
879 | 879 | yield line |
|
880 | 880 | f.close() |
|
881 | 881 | |
|
882 | 882 | |
|
883 | 883 | class iparentdir(ifile): |
|
884 | 884 | def __xrepr__(self, mode): |
|
885 | 885 | if mode == "cell": |
|
886 | 886 | yield (astyle.style_dir, os.pardir) |
|
887 | 887 | else: |
|
888 | 888 | for part in ifile.__xrepr__(self, mode): |
|
889 | 889 | yield part |
|
890 | 890 | |
|
891 | 891 | |
|
892 | 892 | class ils(Table): |
|
893 | 893 | """ |
|
894 | 894 | List the current (or a specific) directory. |
|
895 | 895 | |
|
896 | 896 | Examples: |
|
897 | 897 | |
|
898 | 898 | >>> ils |
|
899 | 899 | >>> ils("/usr/local/lib/python2.4") |
|
900 | 900 | >>> ils("~") |
|
901 | 901 | """ |
|
902 | 902 | def __init__(self, base=os.curdir): |
|
903 | 903 | self.base = os.path.expanduser(base) |
|
904 | 904 | |
|
905 | 905 | def __xiter__(self, mode): |
|
906 | 906 | return xiter(ifile(self.base), mode) |
|
907 | 907 | |
|
908 | 908 | def __xrepr__(self, mode): |
|
909 | 909 | return ifile(self.base).__xrepr__(mode) |
|
910 | 910 | |
|
911 | 911 | def __repr__(self): |
|
912 | 912 | return "%s.%s(%r)" % \ |
|
913 | 913 | (self.__class__.__module__, self.__class__.__name__, self.base) |
|
914 | 914 | |
|
915 | 915 | |
|
916 | 916 | class iglob(Table): |
|
917 | 917 | """ |
|
918 | 918 | List all files and directories matching a specified pattern. |
|
919 | 919 | (See ``glob.glob()`` for more info.). |
|
920 | 920 | |
|
921 | 921 | Examples: |
|
922 | 922 | |
|
923 | 923 | >>> iglob("*.py") |
|
924 | 924 | """ |
|
925 | 925 | def __init__(self, glob): |
|
926 | 926 | self.glob = glob |
|
927 | 927 | |
|
928 | 928 | def __xiter__(self, mode): |
|
929 | 929 | for name in glob.glob(self.glob): |
|
930 | 930 | yield ifile(name) |
|
931 | 931 | |
|
932 | 932 | def __xrepr__(self, mode): |
|
933 | 933 | if mode == "header" or mode == "footer" or mode == "cell": |
|
934 | 934 | yield (astyle.style_default, |
|
935 | 935 | "%s(%r)" % (self.__class__.__name__, self.glob)) |
|
936 | 936 | else: |
|
937 | 937 | yield (astyle.style_default, repr(self)) |
|
938 | 938 | |
|
939 | 939 | def __repr__(self): |
|
940 | 940 | return "%s.%s(%r)" % \ |
|
941 | 941 | (self.__class__.__module__, self.__class__.__name__, self.glob) |
|
942 | 942 | |
|
943 | 943 | |
|
944 | 944 | class iwalk(Table): |
|
945 | 945 | """ |
|
946 | 946 | List all files and directories in a directory and it's subdirectory. |
|
947 | 947 | |
|
948 | 948 | >>> iwalk |
|
949 | 949 | >>> iwalk("/usr/local/lib/python2.4") |
|
950 | 950 | >>> iwalk("~") |
|
951 | 951 | """ |
|
952 | 952 | def __init__(self, base=os.curdir, dirs=True, files=True): |
|
953 | 953 | self.base = os.path.expanduser(base) |
|
954 | 954 | self.dirs = dirs |
|
955 | 955 | self.files = files |
|
956 | 956 | |
|
957 | 957 | def __xiter__(self, mode): |
|
958 | 958 | for (dirpath, dirnames, filenames) in os.walk(self.base): |
|
959 | 959 | if self.dirs: |
|
960 | 960 | for name in sorted(dirnames): |
|
961 | 961 | yield ifile(os.path.join(dirpath, name)) |
|
962 | 962 | if self.files: |
|
963 | 963 | for name in sorted(filenames): |
|
964 | 964 | yield ifile(os.path.join(dirpath, name)) |
|
965 | 965 | |
|
966 | 966 | def __xrepr__(self, mode): |
|
967 | 967 | if mode == "header" or mode == "footer" or mode == "cell": |
|
968 | 968 | yield (astyle.style_default, |
|
969 | 969 | "%s(%r)" % (self.__class__.__name__, self.base)) |
|
970 | 970 | else: |
|
971 | 971 | yield (astyle.style_default, repr(self)) |
|
972 | 972 | |
|
973 | 973 | def __repr__(self): |
|
974 | 974 | return "%s.%s(%r)" % \ |
|
975 | 975 | (self.__class__.__module__, self.__class__.__name__, self.base) |
|
976 | 976 | |
|
977 | 977 | |
|
978 | 978 | class ipwdentry(object): |
|
979 | 979 | """ |
|
980 | 980 | ``ipwdentry`` objects encapsulate entries in the Unix user account and |
|
981 | 981 | password database. |
|
982 | 982 | """ |
|
983 | 983 | def __init__(self, id): |
|
984 | 984 | self._id = id |
|
985 | 985 | self._entry = None |
|
986 | 986 | |
|
987 | 987 | def _getentry(self): |
|
988 | 988 | if self._entry is None: |
|
989 | 989 | if isinstance(self._id, basestring): |
|
990 | 990 | self._entry = pwd.getpwnam(self._id) |
|
991 | 991 | else: |
|
992 | 992 | self._entry = pwd.getpwuid(self._id) |
|
993 | 993 | return self._entry |
|
994 | 994 | |
|
995 | 995 | def getname(self): |
|
996 | 996 | if isinstance(self._id, basestring): |
|
997 | 997 | return self._id |
|
998 | 998 | else: |
|
999 | 999 | return self._getentry().pw_name |
|
1000 | 1000 | name = property(getname, None, None, "User name") |
|
1001 | 1001 | |
|
1002 | 1002 | def getpasswd(self): |
|
1003 | 1003 | return self._getentry().pw_passwd |
|
1004 | 1004 | passwd = property(getpasswd, None, None, "Password") |
|
1005 | 1005 | |
|
1006 | 1006 | def getuid(self): |
|
1007 | 1007 | if isinstance(self._id, basestring): |
|
1008 | 1008 | return self._getentry().pw_uid |
|
1009 | 1009 | else: |
|
1010 | 1010 | return self._id |
|
1011 | 1011 | uid = property(getuid, None, None, "User id") |
|
1012 | 1012 | |
|
1013 | 1013 | def getgid(self): |
|
1014 | 1014 | return self._getentry().pw_gid |
|
1015 | 1015 | gid = property(getgid, None, None, "Primary group id") |
|
1016 | 1016 | |
|
1017 | 1017 | def getgroup(self): |
|
1018 | 1018 | return igrpentry(self.gid) |
|
1019 | 1019 | group = property(getgroup, None, None, "Group") |
|
1020 | 1020 | |
|
1021 | 1021 | def getgecos(self): |
|
1022 | 1022 | return self._getentry().pw_gecos |
|
1023 | 1023 | gecos = property(getgecos, None, None, "Information (e.g. full user name)") |
|
1024 | 1024 | |
|
1025 | 1025 | def getdir(self): |
|
1026 | 1026 | return self._getentry().pw_dir |
|
1027 | 1027 | dir = property(getdir, None, None, "$HOME directory") |
|
1028 | 1028 | |
|
1029 | 1029 | def getshell(self): |
|
1030 | 1030 | return self._getentry().pw_shell |
|
1031 | 1031 | shell = property(getshell, None, None, "Login shell") |
|
1032 | 1032 | |
|
1033 | 1033 | def __xattrs__(self, mode): |
|
1034 | 1034 | return ("name", "passwd", "uid", "gid", "gecos", "dir", "shell") |
|
1035 | 1035 | |
|
1036 | 1036 | def __repr__(self): |
|
1037 | 1037 | return "%s.%s(%r)" % \ |
|
1038 | 1038 | (self.__class__.__module__, self.__class__.__name__, self._id) |
|
1039 | 1039 | |
|
1040 | 1040 | |
|
1041 | 1041 | class ipwd(Table): |
|
1042 | 1042 | """ |
|
1043 | 1043 | List all entries in the Unix user account and password database. |
|
1044 | 1044 | |
|
1045 | 1045 | Example: |
|
1046 | 1046 | |
|
1047 | 1047 | >>> ipwd | isort("uid") |
|
1048 | 1048 | """ |
|
1049 | 1049 | def __iter__(self): |
|
1050 | 1050 | for entry in pwd.getpwall(): |
|
1051 | 1051 | yield ipwdentry(entry.pw_name) |
|
1052 | 1052 | |
|
1053 | 1053 | def __xrepr__(self, mode): |
|
1054 | 1054 | if mode == "header" or mode == "footer" or mode == "cell": |
|
1055 | 1055 | yield (astyle.style_default, "%s()" % self.__class__.__name__) |
|
1056 | 1056 | else: |
|
1057 | 1057 | yield (astyle.style_default, repr(self)) |
|
1058 | 1058 | |
|
1059 | 1059 | |
|
1060 | 1060 | class igrpentry(object): |
|
1061 | 1061 | """ |
|
1062 | 1062 | ``igrpentry`` objects encapsulate entries in the Unix group database. |
|
1063 | 1063 | """ |
|
1064 | 1064 | def __init__(self, id): |
|
1065 | 1065 | self._id = id |
|
1066 | 1066 | self._entry = None |
|
1067 | 1067 | |
|
1068 | 1068 | def _getentry(self): |
|
1069 | 1069 | if self._entry is None: |
|
1070 | 1070 | if isinstance(self._id, basestring): |
|
1071 | 1071 | self._entry = grp.getgrnam(self._id) |
|
1072 | 1072 | else: |
|
1073 | 1073 | self._entry = grp.getgrgid(self._id) |
|
1074 | 1074 | return self._entry |
|
1075 | 1075 | |
|
1076 | 1076 | def getname(self): |
|
1077 | 1077 | if isinstance(self._id, basestring): |
|
1078 | 1078 | return self._id |
|
1079 | 1079 | else: |
|
1080 | 1080 | return self._getentry().gr_name |
|
1081 | 1081 | name = property(getname, None, None, "Group name") |
|
1082 | 1082 | |
|
1083 | 1083 | def getpasswd(self): |
|
1084 | 1084 | return self._getentry().gr_passwd |
|
1085 | 1085 | passwd = property(getpasswd, None, None, "Password") |
|
1086 | 1086 | |
|
1087 | 1087 | def getgid(self): |
|
1088 | 1088 | if isinstance(self._id, basestring): |
|
1089 | 1089 | return self._getentry().gr_gid |
|
1090 | 1090 | else: |
|
1091 | 1091 | return self._id |
|
1092 | 1092 | gid = property(getgid, None, None, "Group id") |
|
1093 | 1093 | |
|
1094 | 1094 | def getmem(self): |
|
1095 | 1095 | return self._getentry().gr_mem |
|
1096 | 1096 | mem = property(getmem, None, None, "Members") |
|
1097 | 1097 | |
|
1098 | 1098 | def __xattrs__(self, mode): |
|
1099 | 1099 | return ("name", "passwd", "gid", "mem") |
|
1100 | 1100 | |
|
1101 | 1101 | def __xrepr__(self, mode): |
|
1102 | 1102 | if mode == "header" or mode == "footer" or mode == "cell": |
|
1103 | 1103 | yield (astyle.style_default, "group ") |
|
1104 | 1104 | try: |
|
1105 | 1105 | yield (astyle.style_default, self.name) |
|
1106 | 1106 | except KeyError: |
|
1107 | 1107 | if isinstance(self._id, basestring): |
|
1108 | 1108 | yield (astyle.style_default, self.name_id) |
|
1109 | 1109 | else: |
|
1110 | 1110 | yield (astyle.style_type_number, str(self._id)) |
|
1111 | 1111 | else: |
|
1112 | 1112 | yield (astyle.style_default, repr(self)) |
|
1113 | 1113 | |
|
1114 | 1114 | def __xiter__(self, mode): |
|
1115 | 1115 | for member in self.mem: |
|
1116 | 1116 | yield ipwdentry(member) |
|
1117 | 1117 | |
|
1118 | 1118 | def __repr__(self): |
|
1119 | 1119 | return "%s.%s(%r)" % \ |
|
1120 | 1120 | (self.__class__.__module__, self.__class__.__name__, self._id) |
|
1121 | 1121 | |
|
1122 | 1122 | |
|
1123 | 1123 | class igrp(Table): |
|
1124 | 1124 | """ |
|
1125 | 1125 | This ``Table`` lists all entries in the Unix group database. |
|
1126 | 1126 | """ |
|
1127 | 1127 | def __xiter__(self, mode): |
|
1128 | 1128 | for entry in grp.getgrall(): |
|
1129 | 1129 | yield igrpentry(entry.gr_name) |
|
1130 | 1130 | |
|
1131 | 1131 | def __xrepr__(self, mode): |
|
1132 | 1132 | if mode == "header" or mode == "footer": |
|
1133 | 1133 | yield (astyle.style_default, "%s()" % self.__class__.__name__) |
|
1134 | 1134 | else: |
|
1135 | 1135 | yield (astyle.style_default, repr(self)) |
|
1136 | 1136 | |
|
1137 | 1137 | |
|
1138 | 1138 | class Fields(object): |
|
1139 | 1139 | def __init__(self, fieldnames, **fields): |
|
1140 | 1140 | self.__fieldnames = fieldnames |
|
1141 | 1141 | for (key, value) in fields.iteritems(): |
|
1142 | 1142 | setattr(self, key, value) |
|
1143 | 1143 | |
|
1144 | 1144 | def __xattrs__(self, mode): |
|
1145 | 1145 | return self.__fieldnames |
|
1146 | 1146 | |
|
1147 | 1147 | def __xrepr__(self, mode): |
|
1148 | 1148 | yield (-1, False) |
|
1149 | 1149 | if mode == "header" or mode == "cell": |
|
1150 | 1150 | yield (astyle.style_default, self.__class__.__name__) |
|
1151 | 1151 | yield (astyle.style_default, "(") |
|
1152 | 1152 | for (i, f) in enumerate(self.__fieldnames): |
|
1153 | 1153 | if i: |
|
1154 | 1154 | yield (astyle.style_default, ", ") |
|
1155 | 1155 | yield (astyle.style_default, f) |
|
1156 | 1156 | yield (astyle.style_default, "=") |
|
1157 | 1157 | for part in xrepr(getattr(self, f), "default"): |
|
1158 | 1158 | yield part |
|
1159 | 1159 | yield (astyle.style_default, ")") |
|
1160 | 1160 | elif mode == "footer": |
|
1161 | 1161 | yield (astyle.style_default, self.__class__.__name__) |
|
1162 | 1162 | yield (astyle.style_default, "(") |
|
1163 | 1163 | for (i, f) in enumerate(self.__fieldnames): |
|
1164 | 1164 | if i: |
|
1165 | 1165 | yield (astyle.style_default, ", ") |
|
1166 | 1166 | yield (astyle.style_default, f) |
|
1167 | 1167 | yield (astyle.style_default, ")") |
|
1168 | 1168 | else: |
|
1169 | 1169 | yield (astyle.style_default, repr(self)) |
|
1170 | 1170 | |
|
1171 | 1171 | |
|
1172 | 1172 | class FieldTable(Table, list): |
|
1173 | 1173 | def __init__(self, *fields): |
|
1174 | 1174 | Table.__init__(self) |
|
1175 | 1175 | list.__init__(self) |
|
1176 | 1176 | self.fields = fields |
|
1177 | 1177 | |
|
1178 | 1178 | def add(self, **fields): |
|
1179 | 1179 | self.append(Fields(self.fields, **fields)) |
|
1180 | 1180 | |
|
1181 | 1181 | def __xiter__(self, mode): |
|
1182 | 1182 | return list.__iter__(self) |
|
1183 | 1183 | |
|
1184 | 1184 | def __xrepr__(self, mode): |
|
1185 | 1185 | yield (-1, False) |
|
1186 | 1186 | if mode == "header" or mode == "footer": |
|
1187 | 1187 | yield (astyle.style_default, self.__class__.__name__) |
|
1188 | 1188 | yield (astyle.style_default, "(") |
|
1189 | 1189 | for (i, f) in enumerate(self.__fieldnames): |
|
1190 | 1190 | if i: |
|
1191 | 1191 | yield (astyle.style_default, ", ") |
|
1192 | 1192 | yield (astyle.style_default, f) |
|
1193 | 1193 | yield (astyle.style_default, ")") |
|
1194 | 1194 | else: |
|
1195 | 1195 | yield (astyle.style_default, repr(self)) |
|
1196 | 1196 | |
|
1197 | 1197 | def __repr__(self): |
|
1198 | 1198 | return "<%s.%s object with fields=%r at 0x%x>" % \ |
|
1199 | 1199 | (self.__class__.__module__, self.__class__.__name__, |
|
1200 | 1200 | ", ".join(map(repr, self.fields)), id(self)) |
|
1201 | 1201 | |
|
1202 | 1202 | |
|
1203 | 1203 | class List(list): |
|
1204 | 1204 | def __xattrs__(self, mode): |
|
1205 | 1205 | return xrange(len(self)) |
|
1206 | 1206 | |
|
1207 | 1207 | def __xrepr__(self, mode): |
|
1208 | 1208 | yield (-1, False) |
|
1209 | 1209 | if mode == "header" or mode == "cell" or mode == "footer" or mode == "default": |
|
1210 | 1210 | yield (astyle.style_default, self.__class__.__name__) |
|
1211 | 1211 | yield (astyle.style_default, "(") |
|
1212 | 1212 | for (i, item) in enumerate(self): |
|
1213 | 1213 | if i: |
|
1214 | 1214 | yield (astyle.style_default, ", ") |
|
1215 | 1215 | for part in xrepr(item, "default"): |
|
1216 | 1216 | yield part |
|
1217 | 1217 | yield (astyle.style_default, ")") |
|
1218 | 1218 | else: |
|
1219 | 1219 | yield (astyle.style_default, repr(self)) |
|
1220 | 1220 | |
|
1221 | 1221 | |
|
1222 | 1222 | class ienv(Table): |
|
1223 | 1223 | """ |
|
1224 | 1224 | List environment variables. |
|
1225 | 1225 | |
|
1226 | 1226 | Example: |
|
1227 | 1227 | |
|
1228 | 1228 | >>> ienv |
|
1229 | 1229 | """ |
|
1230 | 1230 | |
|
1231 | 1231 | def __xiter__(self, mode): |
|
1232 | 1232 | fields = ("key", "value") |
|
1233 | 1233 | for (key, value) in os.environ.iteritems(): |
|
1234 | 1234 | yield Fields(fields, key=key, value=value) |
|
1235 | 1235 | |
|
1236 | 1236 | def __xrepr__(self, mode): |
|
1237 | 1237 | if mode == "header" or mode == "cell": |
|
1238 | 1238 | yield (astyle.style_default, "%s()" % self.__class__.__name__) |
|
1239 | 1239 | else: |
|
1240 | 1240 | yield (astyle.style_default, repr(self)) |
|
1241 | 1241 | |
|
1242 | 1242 | |
|
1243 | 1243 | class icsv(Pipe): |
|
1244 | 1244 | """ |
|
1245 | 1245 | This ``Pipe`` lists turn the input (with must be a pipe outputting lines |
|
1246 | 1246 | or an ``ifile``) into lines of CVS columns. |
|
1247 | 1247 | """ |
|
1248 | 1248 | def __init__(self, **csvargs): |
|
1249 | 1249 | """ |
|
1250 | 1250 | Create an ``icsv`` object. ``cvsargs`` will be passed through as |
|
1251 | 1251 | keyword arguments to ``cvs.reader()``. |
|
1252 | 1252 | """ |
|
1253 | 1253 | self.csvargs = csvargs |
|
1254 | 1254 | |
|
1255 | 1255 | def __xiter__(self, mode): |
|
1256 | 1256 | input = self.input |
|
1257 | 1257 | if isinstance(input, ifile): |
|
1258 | 1258 | input = input.open("rb") |
|
1259 | 1259 | reader = csv.reader(input, **self.csvargs) |
|
1260 | 1260 | for line in reader: |
|
1261 | 1261 | yield List(line) |
|
1262 | 1262 | |
|
1263 | 1263 | def __xrepr__(self, mode): |
|
1264 | 1264 | yield (-1, False) |
|
1265 | 1265 | if mode == "header" or mode == "footer": |
|
1266 | 1266 | input = getattr(self, "input", None) |
|
1267 | 1267 | if input is not None: |
|
1268 | 1268 | for part in xrepr(input, mode): |
|
1269 | 1269 | yield part |
|
1270 | 1270 | yield (astyle.style_default, " | ") |
|
1271 | 1271 | yield (astyle.style_default, "%s(" % self.__class__.__name__) |
|
1272 | 1272 | for (i, (name, value)) in enumerate(self.csvargs.iteritems()): |
|
1273 | 1273 | if i: |
|
1274 | 1274 | yield (astyle.style_default, ", ") |
|
1275 | 1275 | yield (astyle.style_default, name) |
|
1276 | 1276 | yield (astyle.style_default, "=") |
|
1277 | 1277 | for part in xrepr(value, "default"): |
|
1278 | 1278 | yield part |
|
1279 | 1279 | yield (astyle.style_default, ")") |
|
1280 | 1280 | else: |
|
1281 | 1281 | yield (astyle.style_default, repr(self)) |
|
1282 | 1282 | |
|
1283 | 1283 | def __repr__(self): |
|
1284 | 1284 | args = ", ".join(["%s=%r" % item for item in self.csvargs.iteritems()]) |
|
1285 | 1285 | return "<%s.%s %s at 0x%x>" % \ |
|
1286 | 1286 | (self.__class__.__module__, self.__class__.__name__, args, id(self)) |
|
1287 | 1287 | |
|
1288 | 1288 | |
|
1289 | 1289 | class ix(Table): |
|
1290 | 1290 | """ |
|
1291 | 1291 | Execute a system command and list its output as lines |
|
1292 | 1292 | (similar to ``os.popen()``). |
|
1293 | 1293 | |
|
1294 | 1294 | Examples: |
|
1295 | 1295 | |
|
1296 | 1296 | >>> ix("ps x") |
|
1297 | 1297 | >>> ix("find .") | ifile |
|
1298 | 1298 | """ |
|
1299 | 1299 | def __init__(self, cmd): |
|
1300 | 1300 | self.cmd = cmd |
|
1301 | 1301 | self._pipe = None |
|
1302 | 1302 | |
|
1303 | 1303 | def __xiter__(self, mode): |
|
1304 | 1304 | self._pipe = os.popen(self.cmd) |
|
1305 | 1305 | for l in self._pipe: |
|
1306 | 1306 | yield l.rstrip("\r\n") |
|
1307 | 1307 | self._pipe.close() |
|
1308 | 1308 | self._pipe = None |
|
1309 | 1309 | |
|
1310 | 1310 | def __del__(self): |
|
1311 | 1311 | if self._pipe is not None and not self._pipe.closed: |
|
1312 | 1312 | self._pipe.close() |
|
1313 | 1313 | self._pipe = None |
|
1314 | 1314 | |
|
1315 | 1315 | def __xrepr__(self, mode): |
|
1316 | 1316 | if mode == "header" or mode == "footer": |
|
1317 | 1317 | yield (astyle.style_default, |
|
1318 | 1318 | "%s(%r)" % (self.__class__.__name__, self.cmd)) |
|
1319 | 1319 | else: |
|
1320 | 1320 | yield (astyle.style_default, repr(self)) |
|
1321 | 1321 | |
|
1322 | 1322 | def __repr__(self): |
|
1323 | 1323 | return "%s.%s(%r)" % \ |
|
1324 | 1324 | (self.__class__.__module__, self.__class__.__name__, self.cmd) |
|
1325 | 1325 | |
|
1326 | 1326 | |
|
1327 | 1327 | class ifilter(Pipe): |
|
1328 | 1328 | """ |
|
1329 | 1329 | Filter an input pipe. Only objects where an expression evaluates to true |
|
1330 | 1330 | (and doesn't raise an exception) are listed. |
|
1331 | 1331 | |
|
1332 | 1332 | Examples: |
|
1333 | 1333 | |
|
1334 | 1334 | >>> ils | ifilter("_.isfile() and size>1000") |
|
1335 | 1335 | >>> igrp | ifilter("len(mem)") |
|
1336 | 1336 | >>> sys.modules | ifilter(lambda _:_.value is not None) |
|
1337 | 1337 | """ |
|
1338 | 1338 | |
|
1339 | 1339 | def __init__(self, expr, globals=None, errors="raiseifallfail"): |
|
1340 | 1340 | """ |
|
1341 | 1341 | Create an ``ifilter`` object. ``expr`` can be a callable or a string |
|
1342 | 1342 | containing an expression. ``globals`` will be used as the global |
|
1343 | 1343 | namespace for calling string expressions (defaulting to IPython's |
|
1344 | 1344 | user namespace). ``errors`` specifies how exception during evaluation |
|
1345 | 1345 | of ``expr`` are handled: |
|
1346 | 1346 | |
|
1347 | 1347 | * ``drop``: drop all items that have errors; |
|
1348 | 1348 | |
|
1349 | 1349 | * ``keep``: keep all items that have errors; |
|
1350 | 1350 | |
|
1351 | 1351 | * ``keeperror``: keep the exception of all items that have errors; |
|
1352 | 1352 | |
|
1353 | 1353 | * ``raise``: raise the exception; |
|
1354 | 1354 | |
|
1355 | 1355 | * ``raiseifallfail``: raise the first exception if all items have errors; |
|
1356 | 1356 | otherwise drop those with errors (this is the default). |
|
1357 | 1357 | """ |
|
1358 | 1358 | self.expr = expr |
|
1359 | 1359 | self.globals = globals |
|
1360 | 1360 | self.errors = errors |
|
1361 | 1361 | |
|
1362 | 1362 | def __xiter__(self, mode): |
|
1363 | 1363 | if callable(self.expr): |
|
1364 | 1364 | def test(item): |
|
1365 | 1365 | return self.expr(item) |
|
1366 | 1366 | else: |
|
1367 | 1367 | g = getglobals(self.globals) |
|
1368 | 1368 | def test(item): |
|
1369 | 1369 | return eval(self.expr, g, AttrNamespace(item)) |
|
1370 | 1370 | |
|
1371 | 1371 | ok = 0 |
|
1372 | 1372 | exc_info = None |
|
1373 | 1373 | for item in xiter(self.input, mode): |
|
1374 | 1374 | try: |
|
1375 | 1375 | if test(item): |
|
1376 | 1376 | yield item |
|
1377 | 1377 | ok += 1 |
|
1378 | 1378 | except (KeyboardInterrupt, SystemExit): |
|
1379 | 1379 | raise |
|
1380 | 1380 | except Exception, exc: |
|
1381 | 1381 | if self.errors == "drop": |
|
1382 | 1382 | pass # Ignore errors |
|
1383 | 1383 | elif self.errors == "keep": |
|
1384 | 1384 | yield item |
|
1385 | 1385 | elif self.errors == "keeperror": |
|
1386 | 1386 | yield exc |
|
1387 | 1387 | elif self.errors == "raise": |
|
1388 | 1388 | raise |
|
1389 | 1389 | elif self.errors == "raiseifallfail": |
|
1390 | 1390 | if exc_info is None: |
|
1391 | 1391 | exc_info = sys.exc_info() |
|
1392 | 1392 | if not ok and exc_info is not None: |
|
1393 | 1393 | raise exc_info[0], exc_info[1], exc_info[2] |
|
1394 | 1394 | |
|
1395 | 1395 | def __xrepr__(self, mode): |
|
1396 | 1396 | if mode == "header" or mode == "footer": |
|
1397 | 1397 | input = getattr(self, "input", None) |
|
1398 | 1398 | if input is not None: |
|
1399 | 1399 | for part in xrepr(input, mode): |
|
1400 | 1400 | yield part |
|
1401 | 1401 | yield (astyle.style_default, " | ") |
|
1402 | 1402 | yield (astyle.style_default, "%s(" % self.__class__.__name__) |
|
1403 | 1403 | for part in xrepr(self.expr, "default"): |
|
1404 | 1404 | yield part |
|
1405 | 1405 | yield (astyle.style_default, ")") |
|
1406 | 1406 | else: |
|
1407 | 1407 | yield (astyle.style_default, repr(self)) |
|
1408 | 1408 | |
|
1409 | 1409 | def __repr__(self): |
|
1410 | 1410 | return "<%s.%s expr=%r at 0x%x>" % \ |
|
1411 | 1411 | (self.__class__.__module__, self.__class__.__name__, |
|
1412 | 1412 | self.expr, id(self)) |
|
1413 | 1413 | |
|
1414 | 1414 | |
|
1415 | 1415 | class ieval(Pipe): |
|
1416 | 1416 | """ |
|
1417 | 1417 | Evaluate an expression for each object in the input pipe. |
|
1418 | 1418 | |
|
1419 | 1419 | Examples: |
|
1420 | 1420 | |
|
1421 | 1421 | >>> ils | ieval("_.abspath()") |
|
1422 | 1422 | >>> sys.path | ieval(ifile) |
|
1423 | 1423 | """ |
|
1424 | 1424 | |
|
1425 | 1425 | def __init__(self, expr, globals=None, errors="raiseifallfail"): |
|
1426 | 1426 | """ |
|
1427 | 1427 | Create an ``ieval`` object. ``expr`` can be a callable or a string |
|
1428 | 1428 | containing an expression. For the meaning of ``globals`` and |
|
1429 | 1429 | ``errors`` see ``ifilter``. |
|
1430 | 1430 | """ |
|
1431 | 1431 | self.expr = expr |
|
1432 | 1432 | self.globals = globals |
|
1433 | 1433 | self.errors = errors |
|
1434 | 1434 | |
|
1435 | 1435 | def __xiter__(self, mode): |
|
1436 | 1436 | if callable(self.expr): |
|
1437 | 1437 | def do(item): |
|
1438 | 1438 | return self.expr(item) |
|
1439 | 1439 | else: |
|
1440 | 1440 | g = getglobals(self.globals) |
|
1441 | 1441 | def do(item): |
|
1442 | 1442 | return eval(self.expr, g, AttrNamespace(item)) |
|
1443 | 1443 | |
|
1444 | 1444 | ok = 0 |
|
1445 | 1445 | exc_info = None |
|
1446 | 1446 | for item in xiter(self.input, mode): |
|
1447 | 1447 | try: |
|
1448 | 1448 | yield do(item) |
|
1449 | 1449 | except (KeyboardInterrupt, SystemExit): |
|
1450 | 1450 | raise |
|
1451 | 1451 | except Exception, exc: |
|
1452 | 1452 | if self.errors == "drop": |
|
1453 | 1453 | pass # Ignore errors |
|
1454 | 1454 | elif self.errors == "keep": |
|
1455 | 1455 | yield item |
|
1456 | 1456 | elif self.errors == "keeperror": |
|
1457 | 1457 | yield exc |
|
1458 | 1458 | elif self.errors == "raise": |
|
1459 | 1459 | raise |
|
1460 | 1460 | elif self.errors == "raiseifallfail": |
|
1461 | 1461 | if exc_info is None: |
|
1462 | 1462 | exc_info = sys.exc_info() |
|
1463 | 1463 | if not ok and exc_info is not None: |
|
1464 | 1464 | raise exc_info[0], exc_info[1], exc_info[2] |
|
1465 | 1465 | |
|
1466 | 1466 | def __xrepr__(self, mode): |
|
1467 | 1467 | if mode == "header" or mode == "footer": |
|
1468 | 1468 | input = getattr(self, "input", None) |
|
1469 | 1469 | if input is not None: |
|
1470 | 1470 | for part in xrepr(input, mode): |
|
1471 | 1471 | yield part |
|
1472 | 1472 | yield (astyle.style_default, " | ") |
|
1473 | 1473 | yield (astyle.style_default, "%s(" % self.__class__.__name__) |
|
1474 | 1474 | for part in xrepr(self.expr, "default"): |
|
1475 | 1475 | yield part |
|
1476 | 1476 | yield (astyle.style_default, ")") |
|
1477 | 1477 | else: |
|
1478 | 1478 | yield (astyle.style_default, repr(self)) |
|
1479 | 1479 | |
|
1480 | 1480 | def __repr__(self): |
|
1481 | 1481 | return "<%s.%s expr=%r at 0x%x>" % \ |
|
1482 | 1482 | (self.__class__.__module__, self.__class__.__name__, |
|
1483 | 1483 | self.expr, id(self)) |
|
1484 | 1484 | |
|
1485 | 1485 | |
|
1486 | 1486 | class ienum(Pipe): |
|
1487 | 1487 | """ |
|
1488 | 1488 | Enumerate the input pipe (i.e. wrap each input object in an object |
|
1489 | 1489 | with ``index`` and ``object`` attributes). |
|
1490 | 1490 | |
|
1491 | 1491 | Examples: |
|
1492 | 1492 | |
|
1493 | 1493 | >>> xrange(20) | ieval("_,_*_") | ienum | ifilter("index % 2 == 0") | ieval("object") |
|
1494 | 1494 | """ |
|
1495 | 1495 | def __xiter__(self, mode): |
|
1496 | 1496 | fields = ("index", "object") |
|
1497 | 1497 | for (index, object) in enumerate(xiter(self.input, mode)): |
|
1498 | 1498 | yield Fields(fields, index=index, object=object) |
|
1499 | 1499 | |
|
1500 | 1500 | |
|
1501 | 1501 | class isort(Pipe): |
|
1502 | 1502 | """ |
|
1503 | 1503 | Sorts the input pipe. |
|
1504 | 1504 | |
|
1505 | 1505 | Examples: |
|
1506 | 1506 | |
|
1507 | 1507 | >>> ils | isort("size") |
|
1508 | 1508 | >>> ils | isort("_.isdir(), _.lower()", reverse=True) |
|
1509 | 1509 | """ |
|
1510 | 1510 | |
|
1511 | 1511 | def __init__(self, key, globals=None, reverse=False): |
|
1512 | 1512 | """ |
|
1513 | 1513 | Create an ``isort`` object. ``key`` can be a callable or a string |
|
1514 | 1514 | containing an expression. If ``reverse`` is true the sort order will |
|
1515 | 1515 | be reversed. For the meaning of ``globals`` see ``ifilter``. |
|
1516 | 1516 | """ |
|
1517 | 1517 | self.key = key |
|
1518 | 1518 | self.globals = globals |
|
1519 | 1519 | self.reverse = reverse |
|
1520 | 1520 | |
|
1521 | 1521 | def __xiter__(self, mode): |
|
1522 | 1522 | if callable(self.key): |
|
1523 | 1523 | items = sorted( |
|
1524 | 1524 | xiter(self.input, mode), |
|
1525 | 1525 | key=self.key, |
|
1526 | 1526 | reverse=self.reverse |
|
1527 | 1527 | ) |
|
1528 | 1528 | else: |
|
1529 | 1529 | g = getglobals(self.globals) |
|
1530 | 1530 | def key(item): |
|
1531 | 1531 | return eval(self.key, g, AttrNamespace(item)) |
|
1532 | 1532 | items = sorted( |
|
1533 | 1533 | xiter(self.input, mode), |
|
1534 | 1534 | key=key, |
|
1535 | 1535 | reverse=self.reverse |
|
1536 | 1536 | ) |
|
1537 | 1537 | for item in items: |
|
1538 | 1538 | yield item |
|
1539 | 1539 | |
|
1540 | 1540 | def __xrepr__(self, mode): |
|
1541 | 1541 | if mode == "header" or mode == "footer": |
|
1542 | 1542 | input = getattr(self, "input", None) |
|
1543 | 1543 | if input is not None: |
|
1544 | 1544 | for part in xrepr(input, mode): |
|
1545 | 1545 | yield part |
|
1546 | 1546 | yield (astyle.style_default, " | ") |
|
1547 | 1547 | yield (astyle.style_default, "%s(" % self.__class__.__name__) |
|
1548 | 1548 | for part in xrepr(self.key, "default"): |
|
1549 | 1549 | yield part |
|
1550 | 1550 | if self.reverse: |
|
1551 | 1551 | yield (astyle.style_default, ", ") |
|
1552 | 1552 | for part in xrepr(True, "default"): |
|
1553 | 1553 | yield part |
|
1554 | 1554 | yield (astyle.style_default, ")") |
|
1555 | 1555 | else: |
|
1556 | 1556 | yield (astyle.style_default, repr(self)) |
|
1557 | 1557 | |
|
1558 | 1558 | def __repr__(self): |
|
1559 | 1559 | return "<%s.%s key=%r reverse=%r at 0x%x>" % \ |
|
1560 | 1560 | (self.__class__.__module__, self.__class__.__name__, |
|
1561 | 1561 | self.key, self.reverse, id(self)) |
|
1562 | 1562 | |
|
1563 | 1563 | |
|
1564 | 1564 | tab = 3 # for expandtabs() |
|
1565 | 1565 | |
|
1566 | 1566 | def _format(field): |
|
1567 | 1567 | if isinstance(field, str): |
|
1568 | 1568 | text = repr(field.expandtabs(tab))[1:-1] |
|
1569 | 1569 | elif isinstance(field, unicode): |
|
1570 | 1570 | text = repr(field.expandtabs(tab))[2:-1] |
|
1571 | 1571 | elif isinstance(field, datetime.datetime): |
|
1572 | 1572 | # Don't use strftime() here, as this requires year >= 1900 |
|
1573 | 1573 | text = "%04d-%02d-%02d %02d:%02d:%02d.%06d" % \ |
|
1574 | 1574 | (field.year, field.month, field.day, |
|
1575 | 1575 | field.hour, field.minute, field.second, field.microsecond) |
|
1576 | 1576 | elif isinstance(field, datetime.date): |
|
1577 | 1577 | text = "%04d-%02d-%02d" % (field.year, field.month, field.day) |
|
1578 | 1578 | else: |
|
1579 | 1579 | text = repr(field) |
|
1580 | 1580 | return text |
|
1581 | 1581 | |
|
1582 | 1582 | |
|
1583 | 1583 | class Display(object): |
|
1584 | 1584 | class __metaclass__(type): |
|
1585 | 1585 | def __ror__(self, input): |
|
1586 | 1586 | return input | self() |
|
1587 | 1587 | |
|
1588 | 1588 | def __ror__(self, input): |
|
1589 | 1589 | self.input = input |
|
1590 | 1590 | return self |
|
1591 | 1591 | |
|
1592 | 1592 | def display(self): |
|
1593 | 1593 | pass |
|
1594 | 1594 | |
|
1595 | 1595 | |
|
1596 | 1596 | class iless(Display): |
|
1597 | 1597 | cmd = "less --quit-if-one-screen --LONG-PROMPT --LINE-NUMBERS --chop-long-lines --shift=8 --RAW-CONTROL-CHARS" |
|
1598 | 1598 | |
|
1599 | 1599 | def display(self): |
|
1600 | 1600 | try: |
|
1601 | 1601 | pager = os.popen(self.cmd, "w") |
|
1602 | 1602 | try: |
|
1603 | 1603 | for item in xiter(self.input, "default"): |
|
1604 | 1604 | attrs = xattrs(item, "default") |
|
1605 | 1605 | attrs = ["%s=%s" % (a, _format(_getattr(item, a))) for a in attrs] |
|
1606 | 1606 | pager.write(" ".join(attrs)) |
|
1607 | 1607 | pager.write("\n") |
|
1608 | 1608 | finally: |
|
1609 | 1609 | pager.close() |
|
1610 | 1610 | except Exception, exc: |
|
1611 | 1611 | print "%s: %s" % (exc.__class__.__name__, str(exc)) |
|
1612 | 1612 | |
|
1613 | 1613 | |
|
1614 | 1614 | def xformat(value, mode, maxlength): |
|
1615 | 1615 | align = None |
|
1616 | 1616 | full = True |
|
1617 | 1617 | width = 0 |
|
1618 | 1618 | text = astyle.Text() |
|
1619 | 1619 | for (style, part) in xrepr(value, mode): |
|
1620 | 1620 | # only consider the first result |
|
1621 | 1621 | if align is None: |
|
1622 | 1622 | if isinstance(style, int): |
|
1623 | 1623 | # (style, text) really is (alignment, stop) |
|
1624 | 1624 | align = style |
|
1625 | 1625 | full = part |
|
1626 | 1626 | continue |
|
1627 | 1627 | else: |
|
1628 | 1628 | align = -1 |
|
1629 | 1629 | full = True |
|
1630 | 1630 | if not isinstance(style, int): |
|
1631 | 1631 | text.append((style, part)) |
|
1632 | 1632 | width += len(part) |
|
1633 | 1633 | if width >= maxlength and not full: |
|
1634 | 1634 | text.append((astyle.style_ellisis, "...")) |
|
1635 | 1635 | width += 3 |
|
1636 | 1636 | break |
|
1637 | 1637 | if align is None: # default to left alignment |
|
1638 | 1638 | align = -1 |
|
1639 | 1639 | return (align, width, text) |
|
1640 | 1640 | |
|
1641 | 1641 | |
|
1642 | 1642 | class idump(Display): |
|
1643 | 1643 | # The approximate maximum length of a column entry |
|
1644 | 1644 | maxattrlength = 200 |
|
1645 | 1645 | |
|
1646 | 1646 | # Style for column names |
|
1647 | 1647 | style_header = astyle.Style.fromstr("white:black:bold") |
|
1648 | 1648 | |
|
1649 | 1649 | def __init__(self, *attrs): |
|
1650 | 1650 | self.attrs = attrs |
|
1651 | 1651 | self.headerpadchar = " " |
|
1652 | 1652 | self.headersepchar = "|" |
|
1653 | 1653 | self.datapadchar = " " |
|
1654 | 1654 | self.datasepchar = "|" |
|
1655 | 1655 | |
|
1656 | 1656 | def display(self): |
|
1657 | 1657 | stream = genutils.Term.cout |
|
1658 | 1658 | allattrs = [] |
|
1659 | 1659 | allattrset = set() |
|
1660 | 1660 | colwidths = {} |
|
1661 | 1661 | rows = [] |
|
1662 | 1662 | for item in xiter(self.input, "default"): |
|
1663 | 1663 | row = {} |
|
1664 | 1664 | attrs = self.attrs |
|
1665 | 1665 | if not attrs: |
|
1666 | 1666 | attrs = xattrs(item, "default") |
|
1667 | 1667 | for attrname in attrs: |
|
1668 | 1668 | if attrname not in allattrset: |
|
1669 | 1669 | allattrs.append(attrname) |
|
1670 | 1670 | allattrset.add(attrname) |
|
1671 | 1671 | colwidths[attrname] = len(_attrname(attrname)) |
|
1672 | 1672 | try: |
|
1673 | 1673 | value = _getattr(item, attrname, None) |
|
1674 | 1674 | except (KeyboardInterrupt, SystemExit): |
|
1675 | 1675 | raise |
|
1676 | 1676 | except Exception, exc: |
|
1677 | 1677 | value = exc |
|
1678 | 1678 | (align, width, text) = xformat(value, "cell", self.maxattrlength) |
|
1679 | 1679 | colwidths[attrname] = max(colwidths[attrname], width) |
|
1680 | 1680 | # remember alignment, length and colored parts |
|
1681 | 1681 | row[attrname] = (align, width, text) |
|
1682 | 1682 | rows.append(row) |
|
1683 | 1683 | |
|
1684 | 1684 | stream.write("\n") |
|
1685 | 1685 | for (i, attrname) in enumerate(allattrs): |
|
1686 | 1686 | self.style_header(_attrname(attrname)).write(stream) |
|
1687 | 1687 | spc = colwidths[attrname] - len(_attrname(attrname)) |
|
1688 | 1688 | if i < len(colwidths)-1: |
|
1689 | 1689 | stream.write(self.headerpadchar*spc) |
|
1690 | 1690 | stream.write(self.headersepchar) |
|
1691 | 1691 | stream.write("\n") |
|
1692 | 1692 | |
|
1693 | 1693 | for row in rows: |
|
1694 | 1694 | for (i, attrname) in enumerate(allattrs): |
|
1695 | 1695 | (align, width, text) = row[attrname] |
|
1696 | 1696 | spc = colwidths[attrname] - width |
|
1697 | 1697 | if align == -1: |
|
1698 | 1698 | text.write(stream) |
|
1699 | 1699 | if i < len(colwidths)-1: |
|
1700 | 1700 | stream.write(self.datapadchar*spc) |
|
1701 | 1701 | elif align == 0: |
|
1702 | 1702 | spc = colwidths[attrname] - width |
|
1703 | 1703 | spc1 = spc//2 |
|
1704 | 1704 | spc2 = spc-spc1 |
|
1705 | 1705 | stream.write(self.datapadchar*spc1) |
|
1706 | 1706 | text.write(stream) |
|
1707 | 1707 | if i < len(colwidths)-1: |
|
1708 | 1708 | stream.write(self.datapadchar*spc2) |
|
1709 | 1709 | else: |
|
1710 | 1710 | stream.write(self.datapadchar*spc) |
|
1711 | 1711 | text.write(stream) |
|
1712 | 1712 | if i < len(colwidths)-1: |
|
1713 | 1713 | stream.write(self.datasepchar) |
|
1714 | 1714 | stream.write("\n") |
|
1715 | 1715 | |
|
1716 | 1716 | |
|
1717 | 1717 | class XMode(object): |
|
1718 | 1718 | """ |
|
1719 | 1719 | An ``XMode`` object describes one enter mode available for an object |
|
1720 | 1720 | """ |
|
1721 | 1721 | def __init__(self, object, mode, title=None, description=None): |
|
1722 | 1722 | """ |
|
1723 | 1723 | Create a new ``XMode`` object for the object ``object``. This object |
|
1724 | 1724 | must support the enter mode ``mode`` (i.e. ``object.__xiter__(mode)`` |
|
1725 | 1725 | must return an iterable). ``title`` and ``description`` will be |
|
1726 | 1726 | displayed in the browser when selecting among the available modes. |
|
1727 | 1727 | """ |
|
1728 | 1728 | self.object = object |
|
1729 | 1729 | self.mode = mode |
|
1730 | 1730 | self.title = title |
|
1731 | 1731 | self.description = description |
|
1732 | 1732 | |
|
1733 | 1733 | def __repr__(self): |
|
1734 | 1734 | return "<%s.%s object mode=%r at 0x%x>" % \ |
|
1735 | 1735 | (self.__class__.__module__, self.__class__.__name__, |
|
1736 | 1736 | self.mode, id(self)) |
|
1737 | 1737 | |
|
1738 | 1738 | def __xrepr__(self, mode): |
|
1739 | 1739 | if mode == "header" or mode == "footer": |
|
1740 | 1740 | yield (astyle.style_default, self.title) |
|
1741 | 1741 | else: |
|
1742 | 1742 | yield (astyle.style_default, repr(self)) |
|
1743 | 1743 | |
|
1744 | 1744 | def __xattrs__(self, mode): |
|
1745 | 1745 | if mode == "detail": |
|
1746 | 1746 | return ("object", "mode", "title", "description") |
|
1747 | 1747 | return ("title", "description") |
|
1748 | 1748 | |
|
1749 | 1749 | def __xiter__(self, mode): |
|
1750 | 1750 | return xiter(self.object, self.mode) |
|
1751 | 1751 | |
|
1752 | 1752 | |
|
1753 | 1753 | class XAttr(object): |
|
1754 | 1754 | def __init__(self, object, name): |
|
1755 | 1755 | self.name = _attrname(name) |
|
1756 | 1756 | |
|
1757 | 1757 | try: |
|
1758 | 1758 | self.value = _getattr(object, name) |
|
1759 | 1759 | except (KeyboardInterrupt, SystemExit): |
|
1760 | 1760 | raise |
|
1761 | 1761 | except Exception, exc: |
|
1762 | 1762 | if exc.__class__.__module__ == "exceptions": |
|
1763 | 1763 | self.value = exc.__class__.__name__ |
|
1764 | 1764 | else: |
|
1765 | 1765 | self.value = "%s.%s" % \ |
|
1766 | 1766 | (exc.__class__.__module__, exc.__class__.__name__) |
|
1767 | 1767 | self.type = self.value |
|
1768 | 1768 | else: |
|
1769 | 1769 | t = type(self.value) |
|
1770 | 1770 | if t.__module__ == "__builtin__": |
|
1771 | 1771 | self.type = t.__name__ |
|
1772 | 1772 | else: |
|
1773 | 1773 | self.type = "%s.%s" % (t.__module__, t.__name__) |
|
1774 | 1774 | |
|
1775 | 1775 | doc = None |
|
1776 | 1776 | if isinstance(name, basestring): |
|
1777 | 1777 | if name.endswith("()"): |
|
1778 | 1778 | doc = getattr(getattr(object, name[:-2]), "__doc__", None) |
|
1779 | 1779 | else: |
|
1780 | 1780 | try: |
|
1781 | 1781 | meta = getattr(type(object), name) |
|
1782 | 1782 | except AttributeError: |
|
1783 | 1783 | pass |
|
1784 | 1784 | else: |
|
1785 | 1785 | if isinstance(meta, property): |
|
1786 | 1786 | doc = getattr(meta, "__doc__", None) |
|
1787 | 1787 | elif callable(name): |
|
1788 | 1788 | doc = getattr(name, "__doc__", None) |
|
1789 | 1789 | if isinstance(doc, basestring): |
|
1790 | 1790 | doc = doc.strip() |
|
1791 | 1791 | self.doc = doc |
|
1792 | 1792 | |
|
1793 | 1793 | def __xattrs__(self, mode): |
|
1794 | 1794 | return ("name", "type", "doc", "value") |
|
1795 | 1795 | |
|
1796 | 1796 | |
|
1797 | 1797 | try: |
|
1798 | 1798 | from ibrowse import ibrowse |
|
1799 | 1799 | except ImportError: |
|
1800 | 1800 | # No curses (probably Windows) => use ``idump`` as the default display. |
|
1801 | 1801 | defaultdisplay = idump |
|
1802 | 1802 | else: |
|
1803 | 1803 | defaultdisplay = ibrowse |
|
1804 | 1804 | __all__.append("ibrowse") |
|
1805 | 1805 | |
|
1806 | 1806 | |
|
1807 | 1807 | # If we're running under IPython, install an IPython displayhook that |
|
1808 | 1808 | # returns the object from Display.display(), else install a displayhook |
|
1809 | 1809 | # directly as sys.displayhook |
|
1810 | 1810 | api = None |
|
1811 | 1811 | if ipapi is not None: |
|
1812 | 1812 | try: |
|
1813 | 1813 | api = ipapi.get() |
|
1814 | 1814 | except AttributeError: |
|
1815 | 1815 | pass |
|
1816 | 1816 | |
|
1817 | 1817 | if api is not None: |
|
1818 | 1818 | def displayhook(self, obj): |
|
1819 | 1819 | if isinstance(obj, type) and issubclass(obj, Table): |
|
1820 | 1820 | obj = obj() |
|
1821 | 1821 | if isinstance(obj, Table): |
|
1822 | 1822 | obj = obj | defaultdisplay |
|
1823 | 1823 | if isinstance(obj, Display): |
|
1824 | 1824 | return obj.display() |
|
1825 | 1825 | else: |
|
1826 | 1826 | raise ipapi.TryNext |
|
1827 | 1827 | api.set_hook("result_display", displayhook) |
|
1828 | 1828 | else: |
|
1829 | 1829 | def installdisplayhook(): |
|
1830 | 1830 | _originalhook = sys.displayhook |
|
1831 | 1831 | def displayhook(obj): |
|
1832 | 1832 | if isinstance(obj, type) and issubclass(obj, Table): |
|
1833 | 1833 | obj = obj() |
|
1834 | 1834 | if isinstance(obj, Table): |
|
1835 | 1835 | obj = obj | defaultdisplay |
|
1836 | 1836 | if isinstance(obj, Display): |
|
1837 | 1837 | return obj.display() |
|
1838 | 1838 | else: |
|
1839 | 1839 | _originalhook(obj) |
|
1840 | 1840 | sys.displayhook = displayhook |
|
1841 | 1841 | installdisplayhook() |
|
1 | NO CONTENT: file renamed from IPython/Extensions/ipy_sane_defaults.py to IPython/Extensions/ipy_defaults.py |
@@ -1,24 +1,24 b'' | |||
|
1 | 1 | """ System wide configuration file for IPython. |
|
2 | 2 | |
|
3 | 3 | This will be imported by ipython for all users. |
|
4 | 4 | |
|
5 | 5 | After this ipy_user_conf.py is imported, user specific configuration |
|
6 | 6 | should reside there. |
|
7 | 7 | |
|
8 | 8 | """ |
|
9 | 9 | |
|
10 | 10 | import IPython.ipapi |
|
11 | 11 | ip = IPython.ipapi.get() |
|
12 | 12 | |
|
13 | 13 | # add system wide configuration information, import extensions etc. here. |
|
14 | 14 | # nothing here is essential |
|
15 | 15 | |
|
16 | 16 | import sys |
|
17 | 17 | |
|
18 | 18 | import ext_rehashdir # %rehashdir magic |
|
19 | 19 | import ext_rescapture # var = !ls and var = %magic |
|
20 | 20 | import pspersistence # %store magic |
|
21 | 21 | import clearcmd # %clear |
|
22 | 22 | # Basic readline config |
|
23 | 23 | |
|
24 |
o = ip.options |
|
|
24 | o = ip.options |
@@ -1,176 +1,176 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | %store magic for lightweight persistence. |
|
4 | 4 | |
|
5 | 5 | Stores variables, aliases etc. in PickleShare database. |
|
6 | 6 | |
|
7 | 7 | $Id: iplib.py 1107 2006-01-30 19:02:20Z vivainio $ |
|
8 | 8 | """ |
|
9 | 9 | |
|
10 | 10 | import IPython.ipapi |
|
11 | 11 | ip = IPython.ipapi.get() |
|
12 | 12 | |
|
13 | 13 | import pickleshare |
|
14 | 14 | |
|
15 | 15 | import inspect,pickle,os,sys,textwrap |
|
16 | 16 | from IPython.FakeModule import FakeModule |
|
17 | 17 | |
|
18 | 18 | def restore_aliases(self): |
|
19 | 19 | ip = self.getapi() |
|
20 |
staliases = ip. |
|
|
20 | staliases = ip.db.get('stored_aliases', {}) | |
|
21 | 21 | for k,v in staliases.items(): |
|
22 | 22 | #print "restore alias",k,v # dbg |
|
23 | 23 | self.alias_table[k] = v |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | def refresh_variables(ip): |
|
27 |
db = ip. |
|
|
27 | db = ip.db | |
|
28 | 28 | for key in db.keys('autorestore/*'): |
|
29 | 29 | # strip autorestore |
|
30 | 30 | justkey = os.path.basename(key) |
|
31 | 31 | try: |
|
32 | 32 | obj = db[key] |
|
33 | 33 | except KeyError: |
|
34 | 34 | print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey |
|
35 | 35 | print "The error was:",sys.exc_info()[0] |
|
36 | 36 | else: |
|
37 | 37 | #print "restored",justkey,"=",obj #dbg |
|
38 |
ip.user_ns |
|
|
38 | ip.user_ns[justkey] = obj | |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | def restore_data(self): |
|
43 | 43 | ip = self.getapi() |
|
44 | 44 | refresh_variables(ip) |
|
45 | 45 | restore_aliases(self) |
|
46 | 46 | raise IPython.ipapi.TryNext |
|
47 | 47 | |
|
48 | 48 | ip.set_hook('late_startup_hook', restore_data) |
|
49 | 49 | |
|
50 | 50 | def magic_store(self, parameter_s=''): |
|
51 | 51 | """Lightweight persistence for python variables. |
|
52 | 52 | |
|
53 | 53 | Example: |
|
54 | 54 | |
|
55 | 55 | ville@badger[~]|1> A = ['hello',10,'world']\\ |
|
56 | 56 | ville@badger[~]|2> %store A\\ |
|
57 | 57 | ville@badger[~]|3> Exit |
|
58 | 58 | |
|
59 | 59 | (IPython session is closed and started again...) |
|
60 | 60 | |
|
61 | 61 | ville@badger:~$ ipython -p pysh\\ |
|
62 | 62 | ville@badger[~]|1> print A |
|
63 | 63 | |
|
64 | 64 | ['hello', 10, 'world'] |
|
65 | 65 | |
|
66 | 66 | Usage: |
|
67 | 67 | |
|
68 | 68 | %store - Show list of all variables and their current values\\ |
|
69 | 69 | %store <var> - Store the *current* value of the variable to disk\\ |
|
70 | 70 | %store -d <var> - Remove the variable and its value from storage\\ |
|
71 | 71 | %store -z - Remove all variables from storage\\ |
|
72 | 72 | %store -r - Refresh all variables from store (delete current vals)\\ |
|
73 | 73 | %store foo >a.txt - Store value of foo to new file a.txt\\ |
|
74 | 74 | %store foo >>a.txt - Append value of foo to file a.txt\\ |
|
75 | 75 | |
|
76 | 76 | It should be noted that if you change the value of a variable, you |
|
77 | 77 | need to %store it again if you want to persist the new value. |
|
78 | 78 | |
|
79 | 79 | Note also that the variables will need to be pickleable; most basic |
|
80 | 80 | python types can be safely %stored. |
|
81 | 81 | """ |
|
82 | 82 | |
|
83 | 83 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') |
|
84 | 84 | args = argsl.split(None,1) |
|
85 | 85 | ip = self.getapi() |
|
86 |
db = ip. |
|
|
86 | db = ip.db | |
|
87 | 87 | # delete |
|
88 | 88 | if opts.has_key('d'): |
|
89 | 89 | try: |
|
90 | 90 | todel = args[0] |
|
91 | 91 | except IndexError: |
|
92 | 92 | error('You must provide the variable to forget') |
|
93 | 93 | else: |
|
94 | 94 | try: |
|
95 | 95 | del db['autorestore/' + todel] |
|
96 | 96 | except: |
|
97 | 97 | error("Can't delete variable '%s'" % todel) |
|
98 | 98 | # reset |
|
99 | 99 | elif opts.has_key('z'): |
|
100 | 100 | for k in db.keys('autorestore/*'): |
|
101 | 101 | del db[k] |
|
102 | 102 | |
|
103 | 103 | elif opts.has_key('r'): |
|
104 | 104 | refresh_variables(ip) |
|
105 | 105 | |
|
106 | 106 | |
|
107 | 107 | # run without arguments -> list variables & values |
|
108 | 108 | elif not args: |
|
109 | 109 | vars = self.db.keys('autorestore/*') |
|
110 | 110 | vars.sort() |
|
111 | 111 | if vars: |
|
112 | 112 | size = max(map(len,vars)) |
|
113 | 113 | else: |
|
114 | 114 | size = 0 |
|
115 | 115 | |
|
116 | 116 | print 'Stored variables and their in-db values:' |
|
117 | 117 | fmt = '%-'+str(size)+'s -> %s' |
|
118 | 118 | get = db.get |
|
119 | 119 | for var in vars: |
|
120 | 120 | justkey = os.path.basename(var) |
|
121 | 121 | # print 30 first characters from every var |
|
122 | 122 | print fmt % (justkey,repr(get(var,'<unavailable>'))[:50]) |
|
123 | 123 | |
|
124 | 124 | # default action - store the variable |
|
125 | 125 | else: |
|
126 | 126 | # %store foo >file.txt or >>file.txt |
|
127 | 127 | if len(args) > 1 and args[1].startswith('>'): |
|
128 | 128 | fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) |
|
129 | 129 | if args[1].startswith('>>'): |
|
130 | 130 | fil = open(fnam,'a') |
|
131 | 131 | else: |
|
132 | 132 | fil = open(fnam,'w') |
|
133 | 133 | obj = ip.ev(args[0]) |
|
134 | 134 | print "Writing '%s' (%s) to file '%s'." % (args[0], |
|
135 | 135 | obj.__class__.__name__, fnam) |
|
136 | 136 | |
|
137 | 137 | |
|
138 | 138 | if not isinstance (obj,basestring): |
|
139 | 139 | from pprint import pprint |
|
140 | 140 | pprint(obj,fil) |
|
141 | 141 | else: |
|
142 | 142 | fil.write(obj) |
|
143 | 143 | if not obj.endswith('\n'): |
|
144 | 144 | fil.write('\n') |
|
145 | 145 | |
|
146 | 146 | fil.close() |
|
147 | 147 | return |
|
148 | 148 | |
|
149 | 149 | # %store foo |
|
150 | 150 | try: |
|
151 |
obj = ip.user_ns |
|
|
151 | obj = ip.user_ns[args[0]] | |
|
152 | 152 | except KeyError: |
|
153 | 153 | # it might be an alias |
|
154 | 154 | if args[0] in self.alias_table: |
|
155 | 155 | staliases = db.get('stored_aliases',{}) |
|
156 | 156 | staliases[ args[0] ] = self.alias_table[ args[0] ] |
|
157 | 157 | db['stored_aliases'] = staliases |
|
158 | 158 | print "Alias stored:", args[0], self.alias_table[ args[0] ] |
|
159 | 159 | return |
|
160 | 160 | else: |
|
161 | 161 | print "Error: unknown variable '%s'" % args[0] |
|
162 | 162 | |
|
163 | 163 | else: |
|
164 | 164 | if isinstance(inspect.getmodule(obj), FakeModule): |
|
165 | 165 | print textwrap.dedent("""\ |
|
166 | 166 | Warning:%s is %s |
|
167 | 167 | Proper storage of interactively declared classes (or instances |
|
168 | 168 | of those classes) is not possible! Only instances |
|
169 | 169 | of classes in real modules on file system can be %%store'd. |
|
170 | 170 | """ % (args[0], obj) ) |
|
171 | 171 | return |
|
172 | 172 | #pickled = pickle.dumps(obj) |
|
173 | 173 | self.db[ 'autorestore/' + args[0] ] = obj |
|
174 | 174 | print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__) |
|
175 | 175 | |
|
176 | 176 | ip.expose_magic('store',magic_store) |
@@ -1,2952 +1,2953 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Magic functions for InteractiveShell. |
|
3 | 3 | |
|
4 |
$Id: Magic.py 1 |
|
|
4 | $Id: Magic.py 1314 2006-05-19 18:24:14Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
8 | 8 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 11 | # the file COPYING, distributed as part of this software. |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | |
|
14 | 14 | #**************************************************************************** |
|
15 | 15 | # Modules and globals |
|
16 | 16 | |
|
17 | 17 | from IPython import Release |
|
18 | 18 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
19 | 19 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
20 | 20 | __license__ = Release.license |
|
21 | 21 | |
|
22 | 22 | # Python standard modules |
|
23 | 23 | import __builtin__ |
|
24 | 24 | import bdb |
|
25 | 25 | import inspect |
|
26 | 26 | import os |
|
27 | 27 | import pdb |
|
28 | 28 | import pydoc |
|
29 | import shlex | |
|
29 | 30 | import sys |
|
30 | 31 | import re |
|
31 | 32 | import tempfile |
|
32 | 33 | import time |
|
33 | 34 | import cPickle as pickle |
|
34 | 35 | import textwrap |
|
35 | 36 | from cStringIO import StringIO |
|
36 | 37 | from getopt import getopt,GetoptError |
|
37 | 38 | from pprint import pprint, pformat |
|
38 | 39 | |
|
39 | 40 | # profile isn't bundled by default in Debian for license reasons |
|
40 | 41 | try: |
|
41 | 42 | import profile,pstats |
|
42 | 43 | except ImportError: |
|
43 | 44 | profile = pstats = None |
|
44 | 45 | |
|
45 | 46 | # Homebrewed |
|
46 | 47 | import IPython |
|
47 | 48 | from IPython import Debugger, OInspect, wildcard |
|
48 | 49 | from IPython.FakeModule import FakeModule |
|
49 | 50 | from IPython.Itpl import Itpl, itpl, printpl,itplns |
|
50 | 51 | from IPython.PyColorize import Parser |
|
51 | 52 | from IPython.ipstruct import Struct |
|
52 | 53 | from IPython.macro import Macro |
|
53 | 54 | from IPython.genutils import * |
|
54 | 55 | from IPython import platutils |
|
55 | 56 | |
|
56 | 57 | #*************************************************************************** |
|
57 | 58 | # Utility functions |
|
58 | 59 | def on_off(tag): |
|
59 | 60 | """Return an ON/OFF string for a 1/0 input. Simple utility function.""" |
|
60 | 61 | return ['OFF','ON'][tag] |
|
61 | 62 | |
|
62 | 63 | class Bunch: pass |
|
63 | 64 | |
|
64 | 65 | #*************************************************************************** |
|
65 | 66 | # Main class implementing Magic functionality |
|
66 | 67 | class Magic: |
|
67 | 68 | """Magic functions for InteractiveShell. |
|
68 | 69 | |
|
69 | 70 | Shell functions which can be reached as %function_name. All magic |
|
70 | 71 | functions should accept a string, which they can parse for their own |
|
71 | 72 | needs. This can make some functions easier to type, eg `%cd ../` |
|
72 | 73 | vs. `%cd("../")` |
|
73 | 74 | |
|
74 | 75 | ALL definitions MUST begin with the prefix magic_. The user won't need it |
|
75 | 76 | at the command line, but it is is needed in the definition. """ |
|
76 | 77 | |
|
77 | 78 | # class globals |
|
78 | 79 | auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.', |
|
79 | 80 | 'Automagic is ON, % prefix NOT needed for magic functions.'] |
|
80 | 81 | |
|
81 | 82 | #...................................................................... |
|
82 | 83 | # some utility functions |
|
83 | 84 | |
|
84 | 85 | def __init__(self,shell): |
|
85 | 86 | |
|
86 | 87 | self.options_table = {} |
|
87 | 88 | if profile is None: |
|
88 | 89 | self.magic_prun = self.profile_missing_notice |
|
89 | 90 | self.shell = shell |
|
90 | 91 | |
|
91 | 92 | # namespace for holding state we may need |
|
92 | 93 | self._magic_state = Bunch() |
|
93 | 94 | |
|
94 | 95 | def profile_missing_notice(self, *args, **kwargs): |
|
95 | 96 | error("""\ |
|
96 | 97 | The profile module could not be found. If you are a Debian user, |
|
97 | 98 | it has been removed from the standard Debian package because of its non-free |
|
98 | 99 | license. To use profiling, please install"python2.3-profiler" from non-free.""") |
|
99 | 100 | |
|
100 | 101 | def default_option(self,fn,optstr): |
|
101 | 102 | """Make an entry in the options_table for fn, with value optstr""" |
|
102 | 103 | |
|
103 | 104 | if fn not in self.lsmagic(): |
|
104 | 105 | error("%s is not a magic function" % fn) |
|
105 | 106 | self.options_table[fn] = optstr |
|
106 | 107 | |
|
107 | 108 | def lsmagic(self): |
|
108 | 109 | """Return a list of currently available magic functions. |
|
109 | 110 | |
|
110 | 111 | Gives a list of the bare names after mangling (['ls','cd', ...], not |
|
111 | 112 | ['magic_ls','magic_cd',...]""" |
|
112 | 113 | |
|
113 | 114 | # FIXME. This needs a cleanup, in the way the magics list is built. |
|
114 | 115 | |
|
115 | 116 | # magics in class definition |
|
116 | 117 | class_magic = lambda fn: fn.startswith('magic_') and \ |
|
117 | 118 | callable(Magic.__dict__[fn]) |
|
118 | 119 | # in instance namespace (run-time user additions) |
|
119 | 120 | inst_magic = lambda fn: fn.startswith('magic_') and \ |
|
120 | 121 | callable(self.__dict__[fn]) |
|
121 | 122 | # and bound magics by user (so they can access self): |
|
122 | 123 | inst_bound_magic = lambda fn: fn.startswith('magic_') and \ |
|
123 | 124 | callable(self.__class__.__dict__[fn]) |
|
124 | 125 | magics = filter(class_magic,Magic.__dict__.keys()) + \ |
|
125 | 126 | filter(inst_magic,self.__dict__.keys()) + \ |
|
126 | 127 | filter(inst_bound_magic,self.__class__.__dict__.keys()) |
|
127 | 128 | out = [] |
|
128 | 129 | for fn in magics: |
|
129 | 130 | out.append(fn.replace('magic_','',1)) |
|
130 | 131 | out.sort() |
|
131 | 132 | return out |
|
132 | 133 | |
|
133 | 134 | def extract_input_slices(self,slices,raw=False): |
|
134 | 135 | """Return as a string a set of input history slices. |
|
135 | 136 | |
|
136 | 137 | Inputs: |
|
137 | 138 | |
|
138 | 139 | - slices: the set of slices is given as a list of strings (like |
|
139 | 140 | ['1','4:8','9'], since this function is for use by magic functions |
|
140 | 141 | which get their arguments as strings. |
|
141 | 142 | |
|
142 | 143 | Optional inputs: |
|
143 | 144 | |
|
144 | 145 | - raw(False): by default, the processed input is used. If this is |
|
145 | 146 | true, the raw input history is used instead. |
|
146 | 147 | |
|
147 | 148 | Note that slices can be called with two notations: |
|
148 | 149 | |
|
149 | 150 | N:M -> standard python form, means including items N...(M-1). |
|
150 | 151 | |
|
151 | 152 | N-M -> include items N..M (closed endpoint).""" |
|
152 | 153 | |
|
153 | 154 | if raw: |
|
154 | 155 | hist = self.shell.input_hist_raw |
|
155 | 156 | else: |
|
156 | 157 | hist = self.shell.input_hist |
|
157 | 158 | |
|
158 | 159 | cmds = [] |
|
159 | 160 | for chunk in slices: |
|
160 | 161 | if ':' in chunk: |
|
161 | 162 | ini,fin = map(int,chunk.split(':')) |
|
162 | 163 | elif '-' in chunk: |
|
163 | 164 | ini,fin = map(int,chunk.split('-')) |
|
164 | 165 | fin += 1 |
|
165 | 166 | else: |
|
166 | 167 | ini = int(chunk) |
|
167 | 168 | fin = ini+1 |
|
168 | 169 | cmds.append(hist[ini:fin]) |
|
169 | 170 | return cmds |
|
170 | 171 | |
|
171 | 172 | def _ofind(self,oname): |
|
172 | 173 | """Find an object in the available namespaces. |
|
173 | 174 | |
|
174 | 175 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
175 | 176 | |
|
176 | 177 | Has special code to detect magic functions. |
|
177 | 178 | """ |
|
178 | 179 | |
|
179 | 180 | oname = oname.strip() |
|
180 | 181 | |
|
181 | 182 | # Namespaces to search in: |
|
182 | 183 | user_ns = self.shell.user_ns |
|
183 | 184 | internal_ns = self.shell.internal_ns |
|
184 | 185 | builtin_ns = __builtin__.__dict__ |
|
185 | 186 | alias_ns = self.shell.alias_table |
|
186 | 187 | |
|
187 | 188 | # Put them in a list. The order is important so that we find things in |
|
188 | 189 | # the same order that Python finds them. |
|
189 | 190 | namespaces = [ ('Interactive',user_ns), |
|
190 | 191 | ('IPython internal',internal_ns), |
|
191 | 192 | ('Python builtin',builtin_ns), |
|
192 | 193 | ('Alias',alias_ns), |
|
193 | 194 | ] |
|
194 | 195 | |
|
195 | 196 | # initialize results to 'null' |
|
196 | 197 | found = 0; obj = None; ospace = None; ds = None; |
|
197 | 198 | ismagic = 0; isalias = 0 |
|
198 | 199 | |
|
199 | 200 | # Look for the given name by splitting it in parts. If the head is |
|
200 | 201 | # found, then we look for all the remaining parts as members, and only |
|
201 | 202 | # declare success if we can find them all. |
|
202 | 203 | oname_parts = oname.split('.') |
|
203 | 204 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
204 | 205 | for nsname,ns in namespaces: |
|
205 | 206 | try: |
|
206 | 207 | obj = ns[oname_head] |
|
207 | 208 | except KeyError: |
|
208 | 209 | continue |
|
209 | 210 | else: |
|
210 | 211 | for part in oname_rest: |
|
211 | 212 | try: |
|
212 | 213 | obj = getattr(obj,part) |
|
213 | 214 | except: |
|
214 | 215 | # Blanket except b/c some badly implemented objects |
|
215 | 216 | # allow __getattr__ to raise exceptions other than |
|
216 | 217 | # AttributeError, which then crashes IPython. |
|
217 | 218 | break |
|
218 | 219 | else: |
|
219 | 220 | # If we finish the for loop (no break), we got all members |
|
220 | 221 | found = 1 |
|
221 | 222 | ospace = nsname |
|
222 | 223 | if ns == alias_ns: |
|
223 | 224 | isalias = 1 |
|
224 | 225 | break # namespace loop |
|
225 | 226 | |
|
226 | 227 | # Try to see if it's magic |
|
227 | 228 | if not found: |
|
228 | 229 | if oname.startswith(self.shell.ESC_MAGIC): |
|
229 | 230 | oname = oname[1:] |
|
230 | 231 | obj = getattr(self,'magic_'+oname,None) |
|
231 | 232 | if obj is not None: |
|
232 | 233 | found = 1 |
|
233 | 234 | ospace = 'IPython internal' |
|
234 | 235 | ismagic = 1 |
|
235 | 236 | |
|
236 | 237 | # Last try: special-case some literals like '', [], {}, etc: |
|
237 | 238 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
238 | 239 | obj = eval(oname_head) |
|
239 | 240 | found = 1 |
|
240 | 241 | ospace = 'Interactive' |
|
241 | 242 | |
|
242 | 243 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
243 | 244 | 'ismagic':ismagic, 'isalias':isalias} |
|
244 | 245 | |
|
245 | 246 | def arg_err(self,func): |
|
246 | 247 | """Print docstring if incorrect arguments were passed""" |
|
247 | 248 | print 'Error in arguments:' |
|
248 | 249 | print OInspect.getdoc(func) |
|
249 | 250 | |
|
250 | 251 | def format_latex(self,strng): |
|
251 | 252 | """Format a string for latex inclusion.""" |
|
252 | 253 | |
|
253 | 254 | # Characters that need to be escaped for latex: |
|
254 | 255 | escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE) |
|
255 | 256 | # Magic command names as headers: |
|
256 | 257 | cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC, |
|
257 | 258 | re.MULTILINE) |
|
258 | 259 | # Magic commands |
|
259 | 260 | cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC, |
|
260 | 261 | re.MULTILINE) |
|
261 | 262 | # Paragraph continue |
|
262 | 263 | par_re = re.compile(r'\\$',re.MULTILINE) |
|
263 | 264 | |
|
264 | 265 | # The "\n" symbol |
|
265 | 266 | newline_re = re.compile(r'\\n') |
|
266 | 267 | |
|
267 | 268 | # Now build the string for output: |
|
268 | 269 | #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng) |
|
269 | 270 | strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:', |
|
270 | 271 | strng) |
|
271 | 272 | strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng) |
|
272 | 273 | strng = par_re.sub(r'\\\\',strng) |
|
273 | 274 | strng = escape_re.sub(r'\\\1',strng) |
|
274 | 275 | strng = newline_re.sub(r'\\textbackslash{}n',strng) |
|
275 | 276 | return strng |
|
276 | 277 | |
|
277 | 278 | def format_screen(self,strng): |
|
278 | 279 | """Format a string for screen printing. |
|
279 | 280 | |
|
280 | 281 | This removes some latex-type format codes.""" |
|
281 | 282 | # Paragraph continue |
|
282 | 283 | par_re = re.compile(r'\\$',re.MULTILINE) |
|
283 | 284 | strng = par_re.sub('',strng) |
|
284 | 285 | return strng |
|
285 | 286 | |
|
286 | 287 | def parse_options(self,arg_str,opt_str,*long_opts,**kw): |
|
287 | 288 | """Parse options passed to an argument string. |
|
288 | 289 | |
|
289 | 290 | The interface is similar to that of getopt(), but it returns back a |
|
290 | 291 | Struct with the options as keys and the stripped argument string still |
|
291 | 292 | as a string. |
|
292 | 293 | |
|
293 | 294 | arg_str is quoted as a true sys.argv vector by using shlex.split. |
|
294 | 295 | This allows us to easily expand variables, glob files, quote |
|
295 | 296 | arguments, etc. |
|
296 | 297 | |
|
297 | 298 | Options: |
|
298 | 299 | -mode: default 'string'. If given as 'list', the argument string is |
|
299 | 300 | returned as a list (split on whitespace) instead of a string. |
|
300 | 301 | |
|
301 | 302 | -list_all: put all option values in lists. Normally only options |
|
302 | 303 | appearing more than once are put in a list.""" |
|
303 | 304 | |
|
304 | 305 | # inject default options at the beginning of the input line |
|
305 | 306 | caller = sys._getframe(1).f_code.co_name.replace('magic_','') |
|
306 | 307 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) |
|
307 | 308 | |
|
308 | 309 | mode = kw.get('mode','string') |
|
309 | 310 | if mode not in ['string','list']: |
|
310 | 311 | raise ValueError,'incorrect mode given: %s' % mode |
|
311 | 312 | # Get options |
|
312 | 313 | list_all = kw.get('list_all',0) |
|
313 | 314 | |
|
314 | 315 | # Check if we have more than one argument to warrant extra processing: |
|
315 | 316 | odict = {} # Dictionary with options |
|
316 | 317 | args = arg_str.split() |
|
317 | 318 | if len(args) >= 1: |
|
318 | 319 | # If the list of inputs only has 0 or 1 thing in it, there's no |
|
319 | 320 | # need to look for options |
|
320 |
argv = shlex |
|
|
321 | argv = shlex.split(arg_str) | |
|
321 | 322 | # Do regular option processing |
|
322 | 323 | try: |
|
323 | 324 | opts,args = getopt(argv,opt_str,*long_opts) |
|
324 | 325 | except GetoptError,e: |
|
325 | 326 | raise GetoptError('%s ( allowed: "%s" %s)' % (e.msg,opt_str, |
|
326 | 327 | " ".join(long_opts))) |
|
327 | 328 | for o,a in opts: |
|
328 | 329 | if o.startswith('--'): |
|
329 | 330 | o = o[2:] |
|
330 | 331 | else: |
|
331 | 332 | o = o[1:] |
|
332 | 333 | try: |
|
333 | 334 | odict[o].append(a) |
|
334 | 335 | except AttributeError: |
|
335 | 336 | odict[o] = [odict[o],a] |
|
336 | 337 | except KeyError: |
|
337 | 338 | if list_all: |
|
338 | 339 | odict[o] = [a] |
|
339 | 340 | else: |
|
340 | 341 | odict[o] = a |
|
341 | 342 | |
|
342 | 343 | # Prepare opts,args for return |
|
343 | 344 | opts = Struct(odict) |
|
344 | 345 | if mode == 'string': |
|
345 | 346 | args = ' '.join(args) |
|
346 | 347 | |
|
347 | 348 | return opts,args |
|
348 | 349 | |
|
349 | 350 | #...................................................................... |
|
350 | 351 | # And now the actual magic functions |
|
351 | 352 | |
|
352 | 353 | # Functions for IPython shell work (vars,funcs, config, etc) |
|
353 | 354 | def magic_lsmagic(self, parameter_s = ''): |
|
354 | 355 | """List currently available magic functions.""" |
|
355 | 356 | mesc = self.shell.ESC_MAGIC |
|
356 | 357 | print 'Available magic functions:\n'+mesc+\ |
|
357 | 358 | (' '+mesc).join(self.lsmagic()) |
|
358 | 359 | print '\n' + Magic.auto_status[self.shell.rc.automagic] |
|
359 | 360 | return None |
|
360 | 361 | |
|
361 | 362 | def magic_magic(self, parameter_s = ''): |
|
362 | 363 | """Print information about the magic function system.""" |
|
363 | 364 | |
|
364 | 365 | mode = '' |
|
365 | 366 | try: |
|
366 | 367 | if parameter_s.split()[0] == '-latex': |
|
367 | 368 | mode = 'latex' |
|
368 | 369 | if parameter_s.split()[0] == '-brief': |
|
369 | 370 | mode = 'brief' |
|
370 | 371 | except: |
|
371 | 372 | pass |
|
372 | 373 | |
|
373 | 374 | magic_docs = [] |
|
374 | 375 | for fname in self.lsmagic(): |
|
375 | 376 | mname = 'magic_' + fname |
|
376 | 377 | for space in (Magic,self,self.__class__): |
|
377 | 378 | try: |
|
378 | 379 | fn = space.__dict__[mname] |
|
379 | 380 | except KeyError: |
|
380 | 381 | pass |
|
381 | 382 | else: |
|
382 | 383 | break |
|
383 | 384 | if mode == 'brief': |
|
384 | 385 | # only first line |
|
385 | 386 | fndoc = fn.__doc__.split('\n',1)[0] |
|
386 | 387 | else: |
|
387 | 388 | fndoc = fn.__doc__ |
|
388 | 389 | |
|
389 | 390 | magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC, |
|
390 | 391 | fname,fndoc)) |
|
391 | 392 | magic_docs = ''.join(magic_docs) |
|
392 | 393 | |
|
393 | 394 | if mode == 'latex': |
|
394 | 395 | print self.format_latex(magic_docs) |
|
395 | 396 | return |
|
396 | 397 | else: |
|
397 | 398 | magic_docs = self.format_screen(magic_docs) |
|
398 | 399 | if mode == 'brief': |
|
399 | 400 | return magic_docs |
|
400 | 401 | |
|
401 | 402 | outmsg = """ |
|
402 | 403 | IPython's 'magic' functions |
|
403 | 404 | =========================== |
|
404 | 405 | |
|
405 | 406 | The magic function system provides a series of functions which allow you to |
|
406 | 407 | control the behavior of IPython itself, plus a lot of system-type |
|
407 | 408 | features. All these functions are prefixed with a % character, but parameters |
|
408 | 409 | are given without parentheses or quotes. |
|
409 | 410 | |
|
410 | 411 | NOTE: If you have 'automagic' enabled (via the command line option or with the |
|
411 | 412 | %automagic function), you don't need to type in the % explicitly. By default, |
|
412 | 413 | IPython ships with automagic on, so you should only rarely need the % escape. |
|
413 | 414 | |
|
414 | 415 | Example: typing '%cd mydir' (without the quotes) changes you working directory |
|
415 | 416 | to 'mydir', if it exists. |
|
416 | 417 | |
|
417 | 418 | You can define your own magic functions to extend the system. See the supplied |
|
418 | 419 | ipythonrc and example-magic.py files for details (in your ipython |
|
419 | 420 | configuration directory, typically $HOME/.ipython/). |
|
420 | 421 | |
|
421 | 422 | You can also define your own aliased names for magic functions. In your |
|
422 | 423 | ipythonrc file, placing a line like: |
|
423 | 424 | |
|
424 | 425 | execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile |
|
425 | 426 | |
|
426 | 427 | will define %pf as a new name for %profile. |
|
427 | 428 | |
|
428 | 429 | You can also call magics in code using the ipmagic() function, which IPython |
|
429 | 430 | automatically adds to the builtin namespace. Type 'ipmagic?' for details. |
|
430 | 431 | |
|
431 | 432 | For a list of the available magic functions, use %lsmagic. For a description |
|
432 | 433 | of any of them, type %magic_name?, e.g. '%cd?'. |
|
433 | 434 | |
|
434 | 435 | Currently the magic system has the following functions:\n""" |
|
435 | 436 | |
|
436 | 437 | mesc = self.shell.ESC_MAGIC |
|
437 | 438 | outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):" |
|
438 | 439 | "\n\n%s%s\n\n%s" % (outmsg, |
|
439 | 440 | magic_docs,mesc,mesc, |
|
440 | 441 | (' '+mesc).join(self.lsmagic()), |
|
441 | 442 | Magic.auto_status[self.shell.rc.automagic] ) ) |
|
442 | 443 | |
|
443 | 444 | page(outmsg,screen_lines=self.shell.rc.screen_length) |
|
444 | 445 | |
|
445 | 446 | def magic_automagic(self, parameter_s = ''): |
|
446 | 447 | """Make magic functions callable without having to type the initial %. |
|
447 | 448 | |
|
448 | 449 | Toggles on/off (when off, you must call it as %automagic, of |
|
449 | 450 | course). Note that magic functions have lowest priority, so if there's |
|
450 | 451 | a variable whose name collides with that of a magic fn, automagic |
|
451 | 452 | won't work for that function (you get the variable instead). However, |
|
452 | 453 | if you delete the variable (del var), the previously shadowed magic |
|
453 | 454 | function becomes visible to automagic again.""" |
|
454 | 455 | |
|
455 | 456 | rc = self.shell.rc |
|
456 | 457 | rc.automagic = not rc.automagic |
|
457 | 458 | print '\n' + Magic.auto_status[rc.automagic] |
|
458 | 459 | |
|
459 | 460 | def magic_autocall(self, parameter_s = ''): |
|
460 | 461 | """Make functions callable without having to type parentheses. |
|
461 | 462 | |
|
462 | 463 | Usage: |
|
463 | 464 | |
|
464 | 465 | %autocall [mode] |
|
465 | 466 | |
|
466 | 467 | The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the |
|
467 | 468 | value is toggled on and off (remembering the previous state).""" |
|
468 | 469 | |
|
469 | 470 | rc = self.shell.rc |
|
470 | 471 | |
|
471 | 472 | if parameter_s: |
|
472 | 473 | arg = int(parameter_s) |
|
473 | 474 | else: |
|
474 | 475 | arg = 'toggle' |
|
475 | 476 | |
|
476 | 477 | if not arg in (0,1,2,'toggle'): |
|
477 | 478 | error('Valid modes: (0->Off, 1->Smart, 2->Full') |
|
478 | 479 | return |
|
479 | 480 | |
|
480 | 481 | if arg in (0,1,2): |
|
481 | 482 | rc.autocall = arg |
|
482 | 483 | else: # toggle |
|
483 | 484 | if rc.autocall: |
|
484 | 485 | self._magic_state.autocall_save = rc.autocall |
|
485 | 486 | rc.autocall = 0 |
|
486 | 487 | else: |
|
487 | 488 | try: |
|
488 | 489 | rc.autocall = self._magic_state.autocall_save |
|
489 | 490 | except AttributeError: |
|
490 | 491 | rc.autocall = self._magic_state.autocall_save = 1 |
|
491 | 492 | |
|
492 | 493 | print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall] |
|
493 | 494 | |
|
494 | 495 | def magic_autoindent(self, parameter_s = ''): |
|
495 | 496 | """Toggle autoindent on/off (if available).""" |
|
496 | 497 | |
|
497 | 498 | self.shell.set_autoindent() |
|
498 | 499 | print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent] |
|
499 | 500 | |
|
500 | 501 | def magic_system_verbose(self, parameter_s = ''): |
|
501 | 502 | """Toggle verbose printing of system calls on/off.""" |
|
502 | 503 | |
|
503 | 504 | self.shell.rc_set_toggle('system_verbose') |
|
504 | 505 | print "System verbose printing is:",\ |
|
505 | 506 | ['OFF','ON'][self.shell.rc.system_verbose] |
|
506 | 507 | |
|
507 | 508 | def magic_history(self, parameter_s = ''): |
|
508 | 509 | """Print input history (_i<n> variables), with most recent last. |
|
509 | 510 | |
|
510 | 511 | %history -> print at most 40 inputs (some may be multi-line)\\ |
|
511 | 512 | %history n -> print at most n inputs\\ |
|
512 | 513 | %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\ |
|
513 | 514 | |
|
514 | 515 | Each input's number <n> is shown, and is accessible as the |
|
515 | 516 | automatically generated variable _i<n>. Multi-line statements are |
|
516 | 517 | printed starting at a new line for easy copy/paste. |
|
517 | 518 | |
|
518 | 519 | |
|
519 | 520 | Options: |
|
520 | 521 | |
|
521 | 522 | -n: do NOT print line numbers. This is useful if you want to get a |
|
522 | 523 | printout of many lines which can be directly pasted into a text |
|
523 | 524 | editor. |
|
524 | 525 | |
|
525 | 526 | This feature is only available if numbered prompts are in use. |
|
526 | 527 | |
|
527 | 528 | -r: print the 'raw' history. IPython filters your input and |
|
528 | 529 | converts it all into valid Python source before executing it (things |
|
529 | 530 | like magics or aliases are turned into function calls, for |
|
530 | 531 | example). With this option, you'll see the unfiltered history |
|
531 | 532 | instead of the filtered version: '%cd /' will be seen as '%cd /' |
|
532 | 533 | instead of '_ip.magic("%cd /")'. |
|
533 | 534 | """ |
|
534 | 535 | |
|
535 | 536 | shell = self.shell |
|
536 | 537 | if not shell.outputcache.do_full_cache: |
|
537 | 538 | print 'This feature is only available if numbered prompts are in use.' |
|
538 | 539 | return |
|
539 | 540 | opts,args = self.parse_options(parameter_s,'nr',mode='list') |
|
540 | 541 | |
|
541 | 542 | if opts.has_key('r'): |
|
542 | 543 | input_hist = shell.input_hist_raw |
|
543 | 544 | else: |
|
544 | 545 | input_hist = shell.input_hist |
|
545 | 546 | |
|
546 | 547 | default_length = 40 |
|
547 | 548 | if len(args) == 0: |
|
548 | 549 | final = len(input_hist) |
|
549 | 550 | init = max(1,final-default_length) |
|
550 | 551 | elif len(args) == 1: |
|
551 | 552 | final = len(input_hist) |
|
552 | 553 | init = max(1,final-int(args[0])) |
|
553 | 554 | elif len(args) == 2: |
|
554 | 555 | init,final = map(int,args) |
|
555 | 556 | else: |
|
556 | 557 | warn('%hist takes 0, 1 or 2 arguments separated by spaces.') |
|
557 | 558 | print self.magic_hist.__doc__ |
|
558 | 559 | return |
|
559 | 560 | width = len(str(final)) |
|
560 | 561 | line_sep = ['','\n'] |
|
561 | 562 | print_nums = not opts.has_key('n') |
|
562 | 563 | for in_num in range(init,final): |
|
563 | 564 | inline = input_hist[in_num] |
|
564 | 565 | multiline = int(inline.count('\n') > 1) |
|
565 | 566 | if print_nums: |
|
566 | 567 | print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]), |
|
567 | 568 | print inline, |
|
568 | 569 | |
|
569 | 570 | def magic_hist(self, parameter_s=''): |
|
570 | 571 | """Alternate name for %history.""" |
|
571 | 572 | return self.magic_history(parameter_s) |
|
572 | 573 | |
|
573 | 574 | def magic_p(self, parameter_s=''): |
|
574 | 575 | """Just a short alias for Python's 'print'.""" |
|
575 | 576 | exec 'print ' + parameter_s in self.shell.user_ns |
|
576 | 577 | |
|
577 | 578 | def magic_r(self, parameter_s=''): |
|
578 | 579 | """Repeat previous input. |
|
579 | 580 | |
|
580 | 581 | If given an argument, repeats the previous command which starts with |
|
581 | 582 | the same string, otherwise it just repeats the previous input. |
|
582 | 583 | |
|
583 | 584 | Shell escaped commands (with ! as first character) are not recognized |
|
584 | 585 | by this system, only pure python code and magic commands. |
|
585 | 586 | """ |
|
586 | 587 | |
|
587 | 588 | start = parameter_s.strip() |
|
588 | 589 | esc_magic = self.shell.ESC_MAGIC |
|
589 | 590 | # Identify magic commands even if automagic is on (which means |
|
590 | 591 | # the in-memory version is different from that typed by the user). |
|
591 | 592 | if self.shell.rc.automagic: |
|
592 | 593 | start_magic = esc_magic+start |
|
593 | 594 | else: |
|
594 | 595 | start_magic = start |
|
595 | 596 | # Look through the input history in reverse |
|
596 | 597 | for n in range(len(self.shell.input_hist)-2,0,-1): |
|
597 | 598 | input = self.shell.input_hist[n] |
|
598 | 599 | # skip plain 'r' lines so we don't recurse to infinity |
|
599 | 600 | if input != '_ip.magic("r")\n' and \ |
|
600 | 601 | (input.startswith(start) or input.startswith(start_magic)): |
|
601 | 602 | #print 'match',`input` # dbg |
|
602 | 603 | print 'Executing:',input, |
|
603 | 604 | self.shell.runlines(input) |
|
604 | 605 | return |
|
605 | 606 | print 'No previous input matching `%s` found.' % start |
|
606 | 607 | |
|
607 | 608 | def magic_page(self, parameter_s=''): |
|
608 | 609 | """Pretty print the object and display it through a pager. |
|
609 | 610 | |
|
610 | 611 | If no parameter is given, use _ (last output).""" |
|
611 | 612 | # After a function contributed by Olivier Aubert, slightly modified. |
|
612 | 613 | |
|
613 | 614 | oname = parameter_s and parameter_s or '_' |
|
614 | 615 | info = self._ofind(oname) |
|
615 | 616 | if info['found']: |
|
616 | 617 | page(pformat(info['obj'])) |
|
617 | 618 | else: |
|
618 | 619 | print 'Object `%s` not found' % oname |
|
619 | 620 | |
|
620 | 621 | def magic_profile(self, parameter_s=''): |
|
621 | 622 | """Print your currently active IPyhton profile.""" |
|
622 | 623 | if self.shell.rc.profile: |
|
623 | 624 | printpl('Current IPython profile: $self.shell.rc.profile.') |
|
624 | 625 | else: |
|
625 | 626 | print 'No profile active.' |
|
626 | 627 | |
|
627 | 628 | def _inspect(self,meth,oname,**kw): |
|
628 | 629 | """Generic interface to the inspector system. |
|
629 | 630 | |
|
630 | 631 | This function is meant to be called by pdef, pdoc & friends.""" |
|
631 | 632 | |
|
632 | 633 | oname = oname.strip() |
|
633 | 634 | info = Struct(self._ofind(oname)) |
|
634 | 635 | if info.found: |
|
635 | 636 | pmethod = getattr(self.shell.inspector,meth) |
|
636 | 637 | formatter = info.ismagic and self.format_screen or None |
|
637 | 638 | if meth == 'pdoc': |
|
638 | 639 | pmethod(info.obj,oname,formatter) |
|
639 | 640 | elif meth == 'pinfo': |
|
640 | 641 | pmethod(info.obj,oname,formatter,info,**kw) |
|
641 | 642 | else: |
|
642 | 643 | pmethod(info.obj,oname) |
|
643 | 644 | else: |
|
644 | 645 | print 'Object `%s` not found.' % oname |
|
645 | 646 | return 'not found' # so callers can take other action |
|
646 | 647 | |
|
647 | 648 | def magic_pdef(self, parameter_s=''): |
|
648 | 649 | """Print the definition header for any callable object. |
|
649 | 650 | |
|
650 | 651 | If the object is a class, print the constructor information.""" |
|
651 | 652 | self._inspect('pdef',parameter_s) |
|
652 | 653 | |
|
653 | 654 | def magic_pdoc(self, parameter_s=''): |
|
654 | 655 | """Print the docstring for an object. |
|
655 | 656 | |
|
656 | 657 | If the given object is a class, it will print both the class and the |
|
657 | 658 | constructor docstrings.""" |
|
658 | 659 | self._inspect('pdoc',parameter_s) |
|
659 | 660 | |
|
660 | 661 | def magic_psource(self, parameter_s=''): |
|
661 | 662 | """Print (or run through pager) the source code for an object.""" |
|
662 | 663 | self._inspect('psource',parameter_s) |
|
663 | 664 | |
|
664 | 665 | def magic_pfile(self, parameter_s=''): |
|
665 | 666 | """Print (or run through pager) the file where an object is defined. |
|
666 | 667 | |
|
667 | 668 | The file opens at the line where the object definition begins. IPython |
|
668 | 669 | will honor the environment variable PAGER if set, and otherwise will |
|
669 | 670 | do its best to print the file in a convenient form. |
|
670 | 671 | |
|
671 | 672 | If the given argument is not an object currently defined, IPython will |
|
672 | 673 | try to interpret it as a filename (automatically adding a .py extension |
|
673 | 674 | if needed). You can thus use %pfile as a syntax highlighting code |
|
674 | 675 | viewer.""" |
|
675 | 676 | |
|
676 | 677 | # first interpret argument as an object name |
|
677 | 678 | out = self._inspect('pfile',parameter_s) |
|
678 | 679 | # if not, try the input as a filename |
|
679 | 680 | if out == 'not found': |
|
680 | 681 | try: |
|
681 | 682 | filename = get_py_filename(parameter_s) |
|
682 | 683 | except IOError,msg: |
|
683 | 684 | print msg |
|
684 | 685 | return |
|
685 | 686 | page(self.shell.inspector.format(file(filename).read())) |
|
686 | 687 | |
|
687 | 688 | def magic_pinfo(self, parameter_s=''): |
|
688 | 689 | """Provide detailed information about an object. |
|
689 | 690 | |
|
690 | 691 | '%pinfo object' is just a synonym for object? or ?object.""" |
|
691 | 692 | |
|
692 | 693 | #print 'pinfo par: <%s>' % parameter_s # dbg |
|
693 | 694 | |
|
694 | 695 | # detail_level: 0 -> obj? , 1 -> obj?? |
|
695 | 696 | detail_level = 0 |
|
696 | 697 | # We need to detect if we got called as 'pinfo pinfo foo', which can |
|
697 | 698 | # happen if the user types 'pinfo foo?' at the cmd line. |
|
698 | 699 | pinfo,qmark1,oname,qmark2 = \ |
|
699 | 700 | re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups() |
|
700 | 701 | if pinfo or qmark1 or qmark2: |
|
701 | 702 | detail_level = 1 |
|
702 | 703 | if "*" in oname: |
|
703 | 704 | self.magic_psearch(oname) |
|
704 | 705 | else: |
|
705 | 706 | self._inspect('pinfo',oname,detail_level=detail_level) |
|
706 | 707 | |
|
707 | 708 | def magic_psearch(self, parameter_s=''): |
|
708 | 709 | """Search for object in namespaces by wildcard. |
|
709 | 710 | |
|
710 | 711 | %psearch [options] PATTERN [OBJECT TYPE] |
|
711 | 712 | |
|
712 | 713 | Note: ? can be used as a synonym for %psearch, at the beginning or at |
|
713 | 714 | the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the |
|
714 | 715 | rest of the command line must be unchanged (options come first), so |
|
715 | 716 | for example the following forms are equivalent |
|
716 | 717 | |
|
717 | 718 | %psearch -i a* function |
|
718 | 719 | -i a* function? |
|
719 | 720 | ?-i a* function |
|
720 | 721 | |
|
721 | 722 | Arguments: |
|
722 | 723 | |
|
723 | 724 | PATTERN |
|
724 | 725 | |
|
725 | 726 | where PATTERN is a string containing * as a wildcard similar to its |
|
726 | 727 | use in a shell. The pattern is matched in all namespaces on the |
|
727 | 728 | search path. By default objects starting with a single _ are not |
|
728 | 729 | matched, many IPython generated objects have a single |
|
729 | 730 | underscore. The default is case insensitive matching. Matching is |
|
730 | 731 | also done on the attributes of objects and not only on the objects |
|
731 | 732 | in a module. |
|
732 | 733 | |
|
733 | 734 | [OBJECT TYPE] |
|
734 | 735 | |
|
735 | 736 | Is the name of a python type from the types module. The name is |
|
736 | 737 | given in lowercase without the ending type, ex. StringType is |
|
737 | 738 | written string. By adding a type here only objects matching the |
|
738 | 739 | given type are matched. Using all here makes the pattern match all |
|
739 | 740 | types (this is the default). |
|
740 | 741 | |
|
741 | 742 | Options: |
|
742 | 743 | |
|
743 | 744 | -a: makes the pattern match even objects whose names start with a |
|
744 | 745 | single underscore. These names are normally ommitted from the |
|
745 | 746 | search. |
|
746 | 747 | |
|
747 | 748 | -i/-c: make the pattern case insensitive/sensitive. If neither of |
|
748 | 749 | these options is given, the default is read from your ipythonrc |
|
749 | 750 | file. The option name which sets this value is |
|
750 | 751 | 'wildcards_case_sensitive'. If this option is not specified in your |
|
751 | 752 | ipythonrc file, IPython's internal default is to do a case sensitive |
|
752 | 753 | search. |
|
753 | 754 | |
|
754 | 755 | -e/-s NAMESPACE: exclude/search a given namespace. The pattern you |
|
755 | 756 | specifiy can be searched in any of the following namespaces: |
|
756 | 757 | 'builtin', 'user', 'user_global','internal', 'alias', where |
|
757 | 758 | 'builtin' and 'user' are the search defaults. Note that you should |
|
758 | 759 | not use quotes when specifying namespaces. |
|
759 | 760 | |
|
760 | 761 | 'Builtin' contains the python module builtin, 'user' contains all |
|
761 | 762 | user data, 'alias' only contain the shell aliases and no python |
|
762 | 763 | objects, 'internal' contains objects used by IPython. The |
|
763 | 764 | 'user_global' namespace is only used by embedded IPython instances, |
|
764 | 765 | and it contains module-level globals. You can add namespaces to the |
|
765 | 766 | search with -s or exclude them with -e (these options can be given |
|
766 | 767 | more than once). |
|
767 | 768 | |
|
768 | 769 | Examples: |
|
769 | 770 | |
|
770 | 771 | %psearch a* -> objects beginning with an a |
|
771 | 772 | %psearch -e builtin a* -> objects NOT in the builtin space starting in a |
|
772 | 773 | %psearch a* function -> all functions beginning with an a |
|
773 | 774 | %psearch re.e* -> objects beginning with an e in module re |
|
774 | 775 | %psearch r*.e* -> objects that start with e in modules starting in r |
|
775 | 776 | %psearch r*.* string -> all strings in modules beginning with r |
|
776 | 777 | |
|
777 | 778 | Case sensitve search: |
|
778 | 779 | |
|
779 | 780 | %psearch -c a* list all object beginning with lower case a |
|
780 | 781 | |
|
781 | 782 | Show objects beginning with a single _: |
|
782 | 783 | |
|
783 | 784 | %psearch -a _* list objects beginning with a single underscore""" |
|
784 | 785 | |
|
785 | 786 | # default namespaces to be searched |
|
786 | 787 | def_search = ['user','builtin'] |
|
787 | 788 | |
|
788 | 789 | # Process options/args |
|
789 | 790 | opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True) |
|
790 | 791 | opt = opts.get |
|
791 | 792 | shell = self.shell |
|
792 | 793 | psearch = shell.inspector.psearch |
|
793 | 794 | |
|
794 | 795 | # select case options |
|
795 | 796 | if opts.has_key('i'): |
|
796 | 797 | ignore_case = True |
|
797 | 798 | elif opts.has_key('c'): |
|
798 | 799 | ignore_case = False |
|
799 | 800 | else: |
|
800 | 801 | ignore_case = not shell.rc.wildcards_case_sensitive |
|
801 | 802 | |
|
802 | 803 | # Build list of namespaces to search from user options |
|
803 | 804 | def_search.extend(opt('s',[])) |
|
804 | 805 | ns_exclude = ns_exclude=opt('e',[]) |
|
805 | 806 | ns_search = [nm for nm in def_search if nm not in ns_exclude] |
|
806 | 807 | |
|
807 | 808 | # Call the actual search |
|
808 | 809 | try: |
|
809 | 810 | psearch(args,shell.ns_table,ns_search, |
|
810 | 811 | show_all=opt('a'),ignore_case=ignore_case) |
|
811 | 812 | except: |
|
812 | 813 | shell.showtraceback() |
|
813 | 814 | |
|
814 | 815 | def magic_who_ls(self, parameter_s=''): |
|
815 | 816 | """Return a sorted list of all interactive variables. |
|
816 | 817 | |
|
817 | 818 | If arguments are given, only variables of types matching these |
|
818 | 819 | arguments are returned.""" |
|
819 | 820 | |
|
820 | 821 | user_ns = self.shell.user_ns |
|
821 | 822 | internal_ns = self.shell.internal_ns |
|
822 | 823 | user_config_ns = self.shell.user_config_ns |
|
823 | 824 | out = [] |
|
824 | 825 | typelist = parameter_s.split() |
|
825 | 826 | |
|
826 | 827 | for i in user_ns: |
|
827 | 828 | if not (i.startswith('_') or i.startswith('_i')) \ |
|
828 | 829 | and not (i in internal_ns or i in user_config_ns): |
|
829 | 830 | if typelist: |
|
830 | 831 | if type(user_ns[i]).__name__ in typelist: |
|
831 | 832 | out.append(i) |
|
832 | 833 | else: |
|
833 | 834 | out.append(i) |
|
834 | 835 | out.sort() |
|
835 | 836 | return out |
|
836 | 837 | |
|
837 | 838 | def magic_who(self, parameter_s=''): |
|
838 | 839 | """Print all interactive variables, with some minimal formatting. |
|
839 | 840 | |
|
840 | 841 | If any arguments are given, only variables whose type matches one of |
|
841 | 842 | these are printed. For example: |
|
842 | 843 | |
|
843 | 844 | %who function str |
|
844 | 845 | |
|
845 | 846 | will only list functions and strings, excluding all other types of |
|
846 | 847 | variables. To find the proper type names, simply use type(var) at a |
|
847 | 848 | command line to see how python prints type names. For example: |
|
848 | 849 | |
|
849 | 850 | In [1]: type('hello')\\ |
|
850 | 851 | Out[1]: <type 'str'> |
|
851 | 852 | |
|
852 | 853 | indicates that the type name for strings is 'str'. |
|
853 | 854 | |
|
854 | 855 | %who always excludes executed names loaded through your configuration |
|
855 | 856 | file and things which are internal to IPython. |
|
856 | 857 | |
|
857 | 858 | This is deliberate, as typically you may load many modules and the |
|
858 | 859 | purpose of %who is to show you only what you've manually defined.""" |
|
859 | 860 | |
|
860 | 861 | varlist = self.magic_who_ls(parameter_s) |
|
861 | 862 | if not varlist: |
|
862 | 863 | print 'Interactive namespace is empty.' |
|
863 | 864 | return |
|
864 | 865 | |
|
865 | 866 | # if we have variables, move on... |
|
866 | 867 | |
|
867 | 868 | # stupid flushing problem: when prompts have no separators, stdout is |
|
868 | 869 | # getting lost. I'm starting to think this is a python bug. I'm having |
|
869 | 870 | # to force a flush with a print because even a sys.stdout.flush |
|
870 | 871 | # doesn't seem to do anything! |
|
871 | 872 | |
|
872 | 873 | count = 0 |
|
873 | 874 | for i in varlist: |
|
874 | 875 | print i+'\t', |
|
875 | 876 | count += 1 |
|
876 | 877 | if count > 8: |
|
877 | 878 | count = 0 |
|
878 | 879 | |
|
879 | 880 | sys.stdout.flush() # FIXME. Why the hell isn't this flushing??? |
|
880 | 881 | |
|
881 | 882 | print # well, this does force a flush at the expense of an extra \n |
|
882 | 883 | |
|
883 | 884 | def magic_whos(self, parameter_s=''): |
|
884 | 885 | """Like %who, but gives some extra information about each variable. |
|
885 | 886 | |
|
886 | 887 | The same type filtering of %who can be applied here. |
|
887 | 888 | |
|
888 | 889 | For all variables, the type is printed. Additionally it prints: |
|
889 | 890 | |
|
890 | 891 | - For {},[],(): their length. |
|
891 | 892 | |
|
892 | 893 | - For Numeric arrays, a summary with shape, number of elements, |
|
893 | 894 | typecode and size in memory. |
|
894 | 895 | |
|
895 | 896 | - Everything else: a string representation, snipping their middle if |
|
896 | 897 | too long.""" |
|
897 | 898 | |
|
898 | 899 | varnames = self.magic_who_ls(parameter_s) |
|
899 | 900 | if not varnames: |
|
900 | 901 | print 'Interactive namespace is empty.' |
|
901 | 902 | return |
|
902 | 903 | |
|
903 | 904 | # if we have variables, move on... |
|
904 | 905 | |
|
905 | 906 | # for these types, show len() instead of data: |
|
906 | 907 | seq_types = [types.DictType,types.ListType,types.TupleType] |
|
907 | 908 | |
|
908 | 909 | # for Numeric arrays, display summary info |
|
909 | 910 | try: |
|
910 | 911 | import Numeric |
|
911 | 912 | except ImportError: |
|
912 | 913 | array_type = None |
|
913 | 914 | else: |
|
914 | 915 | array_type = Numeric.ArrayType.__name__ |
|
915 | 916 | |
|
916 | 917 | # Find all variable names and types so we can figure out column sizes |
|
917 | 918 | get_vars = lambda i: self.shell.user_ns[i] |
|
918 | 919 | type_name = lambda v: type(v).__name__ |
|
919 | 920 | varlist = map(get_vars,varnames) |
|
920 | 921 | |
|
921 | 922 | typelist = [] |
|
922 | 923 | for vv in varlist: |
|
923 | 924 | tt = type_name(vv) |
|
924 | 925 | if tt=='instance': |
|
925 | 926 | typelist.append(str(vv.__class__)) |
|
926 | 927 | else: |
|
927 | 928 | typelist.append(tt) |
|
928 | 929 | |
|
929 | 930 | # column labels and # of spaces as separator |
|
930 | 931 | varlabel = 'Variable' |
|
931 | 932 | typelabel = 'Type' |
|
932 | 933 | datalabel = 'Data/Info' |
|
933 | 934 | colsep = 3 |
|
934 | 935 | # variable format strings |
|
935 | 936 | vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)" |
|
936 | 937 | vfmt_short = '$vstr[:25]<...>$vstr[-25:]' |
|
937 | 938 | aformat = "%s: %s elems, type `%s`, %s bytes" |
|
938 | 939 | # find the size of the columns to format the output nicely |
|
939 | 940 | varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep |
|
940 | 941 | typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep |
|
941 | 942 | # table header |
|
942 | 943 | print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \ |
|
943 | 944 | ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1) |
|
944 | 945 | # and the table itself |
|
945 | 946 | kb = 1024 |
|
946 | 947 | Mb = 1048576 # kb**2 |
|
947 | 948 | for vname,var,vtype in zip(varnames,varlist,typelist): |
|
948 | 949 | print itpl(vformat), |
|
949 | 950 | if vtype in seq_types: |
|
950 | 951 | print len(var) |
|
951 | 952 | elif vtype==array_type: |
|
952 | 953 | vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1] |
|
953 | 954 | vsize = Numeric.size(var) |
|
954 | 955 | vbytes = vsize*var.itemsize() |
|
955 | 956 | if vbytes < 100000: |
|
956 | 957 | print aformat % (vshape,vsize,var.typecode(),vbytes) |
|
957 | 958 | else: |
|
958 | 959 | print aformat % (vshape,vsize,var.typecode(),vbytes), |
|
959 | 960 | if vbytes < Mb: |
|
960 | 961 | print '(%s kb)' % (vbytes/kb,) |
|
961 | 962 | else: |
|
962 | 963 | print '(%s Mb)' % (vbytes/Mb,) |
|
963 | 964 | else: |
|
964 | 965 | vstr = str(var).replace('\n','\\n') |
|
965 | 966 | if len(vstr) < 50: |
|
966 | 967 | print vstr |
|
967 | 968 | else: |
|
968 | 969 | printpl(vfmt_short) |
|
969 | 970 | |
|
970 | 971 | def magic_reset(self, parameter_s=''): |
|
971 | 972 | """Resets the namespace by removing all names defined by the user. |
|
972 | 973 | |
|
973 | 974 | Input/Output history are left around in case you need them.""" |
|
974 | 975 | |
|
975 | 976 | ans = raw_input( |
|
976 | 977 | "Once deleted, variables cannot be recovered. Proceed (y/n)? ") |
|
977 | 978 | if not ans.lower() == 'y': |
|
978 | 979 | print 'Nothing done.' |
|
979 | 980 | return |
|
980 | 981 | user_ns = self.shell.user_ns |
|
981 | 982 | for i in self.magic_who_ls(): |
|
982 | 983 | del(user_ns[i]) |
|
983 | 984 | |
|
984 | 985 | def magic_config(self,parameter_s=''): |
|
985 | 986 | """Show IPython's internal configuration.""" |
|
986 | 987 | |
|
987 | 988 | page('Current configuration structure:\n'+ |
|
988 | 989 | pformat(self.shell.rc.dict())) |
|
989 | 990 | |
|
990 | 991 | def magic_logstart(self,parameter_s=''): |
|
991 | 992 | """Start logging anywhere in a session. |
|
992 | 993 | |
|
993 | 994 | %logstart [-o|-t] [log_name [log_mode]] |
|
994 | 995 | |
|
995 | 996 | If no name is given, it defaults to a file named 'ipython_log.py' in your |
|
996 | 997 | current directory, in 'rotate' mode (see below). |
|
997 | 998 | |
|
998 | 999 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
999 | 1000 | history up to that point and then continues logging. |
|
1000 | 1001 | |
|
1001 | 1002 | %logstart takes a second optional parameter: logging mode. This can be one |
|
1002 | 1003 | of (note that the modes are given unquoted):\\ |
|
1003 | 1004 | append: well, that says it.\\ |
|
1004 | 1005 | backup: rename (if exists) to name~ and start name.\\ |
|
1005 | 1006 | global: single logfile in your home dir, appended to.\\ |
|
1006 | 1007 | over : overwrite existing log.\\ |
|
1007 | 1008 | rotate: create rotating logs name.1~, name.2~, etc. |
|
1008 | 1009 | |
|
1009 | 1010 | Options: |
|
1010 | 1011 | |
|
1011 | 1012 | -o: log also IPython's output. In this mode, all commands which |
|
1012 | 1013 | generate an Out[NN] prompt are recorded to the logfile, right after |
|
1013 | 1014 | their corresponding input line. The output lines are always |
|
1014 | 1015 | prepended with a '#[Out]# ' marker, so that the log remains valid |
|
1015 | 1016 | Python code. |
|
1016 | 1017 | |
|
1017 | 1018 | Since this marker is always the same, filtering only the output from |
|
1018 | 1019 | a log is very easy, using for example a simple awk call: |
|
1019 | 1020 | |
|
1020 | 1021 | awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py |
|
1021 | 1022 | |
|
1022 | 1023 | -t: put timestamps before each input line logged (these are put in |
|
1023 | 1024 | comments).""" |
|
1024 | 1025 | |
|
1025 | 1026 | opts,par = self.parse_options(parameter_s,'ot') |
|
1026 | 1027 | log_output = 'o' in opts |
|
1027 | 1028 | timestamp = 't' in opts |
|
1028 | 1029 | |
|
1029 | 1030 | rc = self.shell.rc |
|
1030 | 1031 | logger = self.shell.logger |
|
1031 | 1032 | |
|
1032 | 1033 | # if no args are given, the defaults set in the logger constructor by |
|
1033 | 1034 | # ipytohn remain valid |
|
1034 | 1035 | if par: |
|
1035 | 1036 | try: |
|
1036 | 1037 | logfname,logmode = par.split() |
|
1037 | 1038 | except: |
|
1038 | 1039 | logfname = par |
|
1039 | 1040 | logmode = 'backup' |
|
1040 | 1041 | else: |
|
1041 | 1042 | logfname = logger.logfname |
|
1042 | 1043 | logmode = logger.logmode |
|
1043 | 1044 | # put logfname into rc struct as if it had been called on the command |
|
1044 | 1045 | # line, so it ends up saved in the log header Save it in case we need |
|
1045 | 1046 | # to restore it... |
|
1046 | 1047 | old_logfile = rc.opts.get('logfile','') |
|
1047 | 1048 | if logfname: |
|
1048 | 1049 | logfname = os.path.expanduser(logfname) |
|
1049 | 1050 | rc.opts.logfile = logfname |
|
1050 | 1051 | loghead = self.shell.loghead_tpl % (rc.opts,rc.args) |
|
1051 | 1052 | try: |
|
1052 | 1053 | started = logger.logstart(logfname,loghead,logmode, |
|
1053 | 1054 | log_output,timestamp) |
|
1054 | 1055 | except: |
|
1055 | 1056 | rc.opts.logfile = old_logfile |
|
1056 | 1057 | warn("Couldn't start log: %s" % sys.exc_info()[1]) |
|
1057 | 1058 | else: |
|
1058 | 1059 | # log input history up to this point, optionally interleaving |
|
1059 | 1060 | # output if requested |
|
1060 | 1061 | |
|
1061 | 1062 | if timestamp: |
|
1062 | 1063 | # disable timestamping for the previous history, since we've |
|
1063 | 1064 | # lost those already (no time machine here). |
|
1064 | 1065 | logger.timestamp = False |
|
1065 | 1066 | if log_output: |
|
1066 | 1067 | log_write = logger.log_write |
|
1067 | 1068 | input_hist = self.shell.input_hist |
|
1068 | 1069 | output_hist = self.shell.output_hist |
|
1069 | 1070 | for n in range(1,len(input_hist)-1): |
|
1070 | 1071 | log_write(input_hist[n].rstrip()) |
|
1071 | 1072 | if n in output_hist: |
|
1072 | 1073 | log_write(repr(output_hist[n]),'output') |
|
1073 | 1074 | else: |
|
1074 | 1075 | logger.log_write(self.shell.input_hist[1:]) |
|
1075 | 1076 | if timestamp: |
|
1076 | 1077 | # re-enable timestamping |
|
1077 | 1078 | logger.timestamp = True |
|
1078 | 1079 | |
|
1079 | 1080 | print ('Activating auto-logging. ' |
|
1080 | 1081 | 'Current session state plus future input saved.') |
|
1081 | 1082 | logger.logstate() |
|
1082 | 1083 | |
|
1083 | 1084 | def magic_logoff(self,parameter_s=''): |
|
1084 | 1085 | """Temporarily stop logging. |
|
1085 | 1086 | |
|
1086 | 1087 | You must have previously started logging.""" |
|
1087 | 1088 | self.shell.logger.switch_log(0) |
|
1088 | 1089 | |
|
1089 | 1090 | def magic_logon(self,parameter_s=''): |
|
1090 | 1091 | """Restart logging. |
|
1091 | 1092 | |
|
1092 | 1093 | This function is for restarting logging which you've temporarily |
|
1093 | 1094 | stopped with %logoff. For starting logging for the first time, you |
|
1094 | 1095 | must use the %logstart function, which allows you to specify an |
|
1095 | 1096 | optional log filename.""" |
|
1096 | 1097 | |
|
1097 | 1098 | self.shell.logger.switch_log(1) |
|
1098 | 1099 | |
|
1099 | 1100 | def magic_logstate(self,parameter_s=''): |
|
1100 | 1101 | """Print the status of the logging system.""" |
|
1101 | 1102 | |
|
1102 | 1103 | self.shell.logger.logstate() |
|
1103 | 1104 | |
|
1104 | 1105 | def magic_pdb(self, parameter_s=''): |
|
1105 | 1106 | """Control the calling of the pdb interactive debugger. |
|
1106 | 1107 | |
|
1107 | 1108 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without |
|
1108 | 1109 | argument it works as a toggle. |
|
1109 | 1110 | |
|
1110 | 1111 | When an exception is triggered, IPython can optionally call the |
|
1111 | 1112 | interactive pdb debugger after the traceback printout. %pdb toggles |
|
1112 | 1113 | this feature on and off.""" |
|
1113 | 1114 | |
|
1114 | 1115 | par = parameter_s.strip().lower() |
|
1115 | 1116 | |
|
1116 | 1117 | if par: |
|
1117 | 1118 | try: |
|
1118 | 1119 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] |
|
1119 | 1120 | except KeyError: |
|
1120 | 1121 | print ('Incorrect argument. Use on/1, off/0, ' |
|
1121 | 1122 | 'or nothing for a toggle.') |
|
1122 | 1123 | return |
|
1123 | 1124 | else: |
|
1124 | 1125 | # toggle |
|
1125 | 1126 | new_pdb = not self.shell.InteractiveTB.call_pdb |
|
1126 | 1127 | |
|
1127 | 1128 | # set on the shell |
|
1128 | 1129 | self.shell.call_pdb = new_pdb |
|
1129 | 1130 | print 'Automatic pdb calling has been turned',on_off(new_pdb) |
|
1130 | 1131 | |
|
1131 | 1132 | def magic_prun(self, parameter_s ='',user_mode=1, |
|
1132 | 1133 | opts=None,arg_lst=None,prog_ns=None): |
|
1133 | 1134 | |
|
1134 | 1135 | """Run a statement through the python code profiler. |
|
1135 | 1136 | |
|
1136 | 1137 | Usage:\\ |
|
1137 | 1138 | %prun [options] statement |
|
1138 | 1139 | |
|
1139 | 1140 | The given statement (which doesn't require quote marks) is run via the |
|
1140 | 1141 | python profiler in a manner similar to the profile.run() function. |
|
1141 | 1142 | Namespaces are internally managed to work correctly; profile.run |
|
1142 | 1143 | cannot be used in IPython because it makes certain assumptions about |
|
1143 | 1144 | namespaces which do not hold under IPython. |
|
1144 | 1145 | |
|
1145 | 1146 | Options: |
|
1146 | 1147 | |
|
1147 | 1148 | -l <limit>: you can place restrictions on what or how much of the |
|
1148 | 1149 | profile gets printed. The limit value can be: |
|
1149 | 1150 | |
|
1150 | 1151 | * A string: only information for function names containing this string |
|
1151 | 1152 | is printed. |
|
1152 | 1153 | |
|
1153 | 1154 | * An integer: only these many lines are printed. |
|
1154 | 1155 | |
|
1155 | 1156 | * A float (between 0 and 1): this fraction of the report is printed |
|
1156 | 1157 | (for example, use a limit of 0.4 to see the topmost 40% only). |
|
1157 | 1158 | |
|
1158 | 1159 | You can combine several limits with repeated use of the option. For |
|
1159 | 1160 | example, '-l __init__ -l 5' will print only the topmost 5 lines of |
|
1160 | 1161 | information about class constructors. |
|
1161 | 1162 | |
|
1162 | 1163 | -r: return the pstats.Stats object generated by the profiling. This |
|
1163 | 1164 | object has all the information about the profile in it, and you can |
|
1164 | 1165 | later use it for further analysis or in other functions. |
|
1165 | 1166 | |
|
1166 | 1167 | Since magic functions have a particular form of calling which prevents |
|
1167 | 1168 | you from writing something like:\\ |
|
1168 | 1169 | In [1]: p = %prun -r print 4 # invalid!\\ |
|
1169 | 1170 | you must instead use IPython's automatic variables to assign this:\\ |
|
1170 | 1171 | In [1]: %prun -r print 4 \\ |
|
1171 | 1172 | Out[1]: <pstats.Stats instance at 0x8222cec>\\ |
|
1172 | 1173 | In [2]: stats = _ |
|
1173 | 1174 | |
|
1174 | 1175 | If you really need to assign this value via an explicit function call, |
|
1175 | 1176 | you can always tap directly into the true name of the magic function |
|
1176 | 1177 | by using the _ip.magic function:\\ |
|
1177 | 1178 | In [3]: stats = _ip.magic('prun','-r print 4') |
|
1178 | 1179 | |
|
1179 | 1180 | You can type _ip.magic? for more details. |
|
1180 | 1181 | |
|
1181 | 1182 | -s <key>: sort profile by given key. You can provide more than one key |
|
1182 | 1183 | by using the option several times: '-s key1 -s key2 -s key3...'. The |
|
1183 | 1184 | default sorting key is 'time'. |
|
1184 | 1185 | |
|
1185 | 1186 | The following is copied verbatim from the profile documentation |
|
1186 | 1187 | referenced below: |
|
1187 | 1188 | |
|
1188 | 1189 | When more than one key is provided, additional keys are used as |
|
1189 | 1190 | secondary criteria when the there is equality in all keys selected |
|
1190 | 1191 | before them. |
|
1191 | 1192 | |
|
1192 | 1193 | Abbreviations can be used for any key names, as long as the |
|
1193 | 1194 | abbreviation is unambiguous. The following are the keys currently |
|
1194 | 1195 | defined: |
|
1195 | 1196 | |
|
1196 | 1197 | Valid Arg Meaning\\ |
|
1197 | 1198 | "calls" call count\\ |
|
1198 | 1199 | "cumulative" cumulative time\\ |
|
1199 | 1200 | "file" file name\\ |
|
1200 | 1201 | "module" file name\\ |
|
1201 | 1202 | "pcalls" primitive call count\\ |
|
1202 | 1203 | "line" line number\\ |
|
1203 | 1204 | "name" function name\\ |
|
1204 | 1205 | "nfl" name/file/line\\ |
|
1205 | 1206 | "stdname" standard name\\ |
|
1206 | 1207 | "time" internal time |
|
1207 | 1208 | |
|
1208 | 1209 | Note that all sorts on statistics are in descending order (placing |
|
1209 | 1210 | most time consuming items first), where as name, file, and line number |
|
1210 | 1211 | searches are in ascending order (i.e., alphabetical). The subtle |
|
1211 | 1212 | distinction between "nfl" and "stdname" is that the standard name is a |
|
1212 | 1213 | sort of the name as printed, which means that the embedded line |
|
1213 | 1214 | numbers get compared in an odd way. For example, lines 3, 20, and 40 |
|
1214 | 1215 | would (if the file names were the same) appear in the string order |
|
1215 | 1216 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the |
|
1216 | 1217 | line numbers. In fact, sort_stats("nfl") is the same as |
|
1217 | 1218 | sort_stats("name", "file", "line"). |
|
1218 | 1219 | |
|
1219 | 1220 | -T <filename>: save profile results as shown on screen to a text |
|
1220 | 1221 | file. The profile is still shown on screen. |
|
1221 | 1222 | |
|
1222 | 1223 | -D <filename>: save (via dump_stats) profile statistics to given |
|
1223 | 1224 | filename. This data is in a format understod by the pstats module, and |
|
1224 | 1225 | is generated by a call to the dump_stats() method of profile |
|
1225 | 1226 | objects. The profile is still shown on screen. |
|
1226 | 1227 | |
|
1227 | 1228 | If you want to run complete programs under the profiler's control, use |
|
1228 | 1229 | '%run -p [prof_opts] filename.py [args to program]' where prof_opts |
|
1229 | 1230 | contains profiler specific options as described here. |
|
1230 | 1231 | |
|
1231 | 1232 | You can read the complete documentation for the profile module with:\\ |
|
1232 | 1233 | In [1]: import profile; profile.help() """ |
|
1233 | 1234 | |
|
1234 | 1235 | opts_def = Struct(D=[''],l=[],s=['time'],T=['']) |
|
1235 | 1236 | # protect user quote marks |
|
1236 | 1237 | parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'") |
|
1237 | 1238 | |
|
1238 | 1239 | if user_mode: # regular user call |
|
1239 | 1240 | opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:', |
|
1240 | 1241 | list_all=1) |
|
1241 | 1242 | namespace = self.shell.user_ns |
|
1242 | 1243 | else: # called to run a program by %run -p |
|
1243 | 1244 | try: |
|
1244 | 1245 | filename = get_py_filename(arg_lst[0]) |
|
1245 | 1246 | except IOError,msg: |
|
1246 | 1247 | error(msg) |
|
1247 | 1248 | return |
|
1248 | 1249 | |
|
1249 | 1250 | arg_str = 'execfile(filename,prog_ns)' |
|
1250 | 1251 | namespace = locals() |
|
1251 | 1252 | |
|
1252 | 1253 | opts.merge(opts_def) |
|
1253 | 1254 | |
|
1254 | 1255 | prof = profile.Profile() |
|
1255 | 1256 | try: |
|
1256 | 1257 | prof = prof.runctx(arg_str,namespace,namespace) |
|
1257 | 1258 | sys_exit = '' |
|
1258 | 1259 | except SystemExit: |
|
1259 | 1260 | sys_exit = """*** SystemExit exception caught in code being profiled.""" |
|
1260 | 1261 | |
|
1261 | 1262 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) |
|
1262 | 1263 | |
|
1263 | 1264 | lims = opts.l |
|
1264 | 1265 | if lims: |
|
1265 | 1266 | lims = [] # rebuild lims with ints/floats/strings |
|
1266 | 1267 | for lim in opts.l: |
|
1267 | 1268 | try: |
|
1268 | 1269 | lims.append(int(lim)) |
|
1269 | 1270 | except ValueError: |
|
1270 | 1271 | try: |
|
1271 | 1272 | lims.append(float(lim)) |
|
1272 | 1273 | except ValueError: |
|
1273 | 1274 | lims.append(lim) |
|
1274 | 1275 | |
|
1275 | 1276 | # trap output |
|
1276 | 1277 | sys_stdout = sys.stdout |
|
1277 | 1278 | stdout_trap = StringIO() |
|
1278 | 1279 | try: |
|
1279 | 1280 | sys.stdout = stdout_trap |
|
1280 | 1281 | stats.print_stats(*lims) |
|
1281 | 1282 | finally: |
|
1282 | 1283 | sys.stdout = sys_stdout |
|
1283 | 1284 | output = stdout_trap.getvalue() |
|
1284 | 1285 | output = output.rstrip() |
|
1285 | 1286 | |
|
1286 | 1287 | page(output,screen_lines=self.shell.rc.screen_length) |
|
1287 | 1288 | print sys_exit, |
|
1288 | 1289 | |
|
1289 | 1290 | dump_file = opts.D[0] |
|
1290 | 1291 | text_file = opts.T[0] |
|
1291 | 1292 | if dump_file: |
|
1292 | 1293 | prof.dump_stats(dump_file) |
|
1293 | 1294 | print '\n*** Profile stats marshalled to file',\ |
|
1294 | 1295 | `dump_file`+'.',sys_exit |
|
1295 | 1296 | if text_file: |
|
1296 | 1297 | file(text_file,'w').write(output) |
|
1297 | 1298 | print '\n*** Profile printout saved to text file',\ |
|
1298 | 1299 | `text_file`+'.',sys_exit |
|
1299 | 1300 | |
|
1300 | 1301 | if opts.has_key('r'): |
|
1301 | 1302 | return stats |
|
1302 | 1303 | else: |
|
1303 | 1304 | return None |
|
1304 | 1305 | |
|
1305 | 1306 | def magic_run(self, parameter_s ='',runner=None): |
|
1306 | 1307 | """Run the named file inside IPython as a program. |
|
1307 | 1308 | |
|
1308 | 1309 | Usage:\\ |
|
1309 | 1310 | %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args] |
|
1310 | 1311 | |
|
1311 | 1312 | Parameters after the filename are passed as command-line arguments to |
|
1312 | 1313 | the program (put in sys.argv). Then, control returns to IPython's |
|
1313 | 1314 | prompt. |
|
1314 | 1315 | |
|
1315 | 1316 | This is similar to running at a system prompt:\\ |
|
1316 | 1317 | $ python file args\\ |
|
1317 | 1318 | but with the advantage of giving you IPython's tracebacks, and of |
|
1318 | 1319 | loading all variables into your interactive namespace for further use |
|
1319 | 1320 | (unless -p is used, see below). |
|
1320 | 1321 | |
|
1321 | 1322 | The file is executed in a namespace initially consisting only of |
|
1322 | 1323 | __name__=='__main__' and sys.argv constructed as indicated. It thus |
|
1323 | 1324 | sees its environment as if it were being run as a stand-alone |
|
1324 | 1325 | program. But after execution, the IPython interactive namespace gets |
|
1325 | 1326 | updated with all variables defined in the program (except for __name__ |
|
1326 | 1327 | and sys.argv). This allows for very convenient loading of code for |
|
1327 | 1328 | interactive work, while giving each program a 'clean sheet' to run in. |
|
1328 | 1329 | |
|
1329 | 1330 | Options: |
|
1330 | 1331 | |
|
1331 | 1332 | -n: __name__ is NOT set to '__main__', but to the running file's name |
|
1332 | 1333 | without extension (as python does under import). This allows running |
|
1333 | 1334 | scripts and reloading the definitions in them without calling code |
|
1334 | 1335 | protected by an ' if __name__ == "__main__" ' clause. |
|
1335 | 1336 | |
|
1336 | 1337 | -i: run the file in IPython's namespace instead of an empty one. This |
|
1337 | 1338 | is useful if you are experimenting with code written in a text editor |
|
1338 | 1339 | which depends on variables defined interactively. |
|
1339 | 1340 | |
|
1340 | 1341 | -e: ignore sys.exit() calls or SystemExit exceptions in the script |
|
1341 | 1342 | being run. This is particularly useful if IPython is being used to |
|
1342 | 1343 | run unittests, which always exit with a sys.exit() call. In such |
|
1343 | 1344 | cases you are interested in the output of the test results, not in |
|
1344 | 1345 | seeing a traceback of the unittest module. |
|
1345 | 1346 | |
|
1346 | 1347 | -t: print timing information at the end of the run. IPython will give |
|
1347 | 1348 | you an estimated CPU time consumption for your script, which under |
|
1348 | 1349 | Unix uses the resource module to avoid the wraparound problems of |
|
1349 | 1350 | time.clock(). Under Unix, an estimate of time spent on system tasks |
|
1350 | 1351 | is also given (for Windows platforms this is reported as 0.0). |
|
1351 | 1352 | |
|
1352 | 1353 | If -t is given, an additional -N<N> option can be given, where <N> |
|
1353 | 1354 | must be an integer indicating how many times you want the script to |
|
1354 | 1355 | run. The final timing report will include total and per run results. |
|
1355 | 1356 | |
|
1356 | 1357 | For example (testing the script uniq_stable.py): |
|
1357 | 1358 | |
|
1358 | 1359 | In [1]: run -t uniq_stable |
|
1359 | 1360 | |
|
1360 | 1361 | IPython CPU timings (estimated):\\ |
|
1361 | 1362 | User : 0.19597 s.\\ |
|
1362 | 1363 | System: 0.0 s.\\ |
|
1363 | 1364 | |
|
1364 | 1365 | In [2]: run -t -N5 uniq_stable |
|
1365 | 1366 | |
|
1366 | 1367 | IPython CPU timings (estimated):\\ |
|
1367 | 1368 | Total runs performed: 5\\ |
|
1368 | 1369 | Times : Total Per run\\ |
|
1369 | 1370 | User : 0.910862 s, 0.1821724 s.\\ |
|
1370 | 1371 | System: 0.0 s, 0.0 s. |
|
1371 | 1372 | |
|
1372 | 1373 | -d: run your program under the control of pdb, the Python debugger. |
|
1373 | 1374 | This allows you to execute your program step by step, watch variables, |
|
1374 | 1375 | etc. Internally, what IPython does is similar to calling: |
|
1375 | 1376 | |
|
1376 | 1377 | pdb.run('execfile("YOURFILENAME")') |
|
1377 | 1378 | |
|
1378 | 1379 | with a breakpoint set on line 1 of your file. You can change the line |
|
1379 | 1380 | number for this automatic breakpoint to be <N> by using the -bN option |
|
1380 | 1381 | (where N must be an integer). For example: |
|
1381 | 1382 | |
|
1382 | 1383 | %run -d -b40 myscript |
|
1383 | 1384 | |
|
1384 | 1385 | will set the first breakpoint at line 40 in myscript.py. Note that |
|
1385 | 1386 | the first breakpoint must be set on a line which actually does |
|
1386 | 1387 | something (not a comment or docstring) for it to stop execution. |
|
1387 | 1388 | |
|
1388 | 1389 | When the pdb debugger starts, you will see a (Pdb) prompt. You must |
|
1389 | 1390 | first enter 'c' (without qoutes) to start execution up to the first |
|
1390 | 1391 | breakpoint. |
|
1391 | 1392 | |
|
1392 | 1393 | Entering 'help' gives information about the use of the debugger. You |
|
1393 | 1394 | can easily see pdb's full documentation with "import pdb;pdb.help()" |
|
1394 | 1395 | at a prompt. |
|
1395 | 1396 | |
|
1396 | 1397 | -p: run program under the control of the Python profiler module (which |
|
1397 | 1398 | prints a detailed report of execution times, function calls, etc). |
|
1398 | 1399 | |
|
1399 | 1400 | You can pass other options after -p which affect the behavior of the |
|
1400 | 1401 | profiler itself. See the docs for %prun for details. |
|
1401 | 1402 | |
|
1402 | 1403 | In this mode, the program's variables do NOT propagate back to the |
|
1403 | 1404 | IPython interactive namespace (because they remain in the namespace |
|
1404 | 1405 | where the profiler executes them). |
|
1405 | 1406 | |
|
1406 | 1407 | Internally this triggers a call to %prun, see its documentation for |
|
1407 | 1408 | details on the options available specifically for profiling.""" |
|
1408 | 1409 | |
|
1409 | 1410 | # get arguments and set sys.argv for program to be run. |
|
1410 | 1411 | opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e', |
|
1411 | 1412 | mode='list',list_all=1) |
|
1412 | 1413 | |
|
1413 | 1414 | try: |
|
1414 | 1415 | filename = get_py_filename(arg_lst[0]) |
|
1415 | 1416 | except IndexError: |
|
1416 | 1417 | warn('you must provide at least a filename.') |
|
1417 | 1418 | print '\n%run:\n',OInspect.getdoc(self.magic_run) |
|
1418 | 1419 | return |
|
1419 | 1420 | except IOError,msg: |
|
1420 | 1421 | error(msg) |
|
1421 | 1422 | return |
|
1422 | 1423 | |
|
1423 | 1424 | # Control the response to exit() calls made by the script being run |
|
1424 | 1425 | exit_ignore = opts.has_key('e') |
|
1425 | 1426 | |
|
1426 | 1427 | # Make sure that the running script gets a proper sys.argv as if it |
|
1427 | 1428 | # were run from a system shell. |
|
1428 | 1429 | save_argv = sys.argv # save it for later restoring |
|
1429 | 1430 | sys.argv = [filename]+ arg_lst[1:] # put in the proper filename |
|
1430 | 1431 | |
|
1431 | 1432 | if opts.has_key('i'): |
|
1432 | 1433 | prog_ns = self.shell.user_ns |
|
1433 | 1434 | __name__save = self.shell.user_ns['__name__'] |
|
1434 | 1435 | prog_ns['__name__'] = '__main__' |
|
1435 | 1436 | else: |
|
1436 | 1437 | if opts.has_key('n'): |
|
1437 | 1438 | name = os.path.splitext(os.path.basename(filename))[0] |
|
1438 | 1439 | else: |
|
1439 | 1440 | name = '__main__' |
|
1440 | 1441 | prog_ns = {'__name__':name} |
|
1441 | 1442 | |
|
1442 | 1443 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must |
|
1443 | 1444 | # set the __file__ global in the script's namespace |
|
1444 | 1445 | prog_ns['__file__'] = filename |
|
1445 | 1446 | |
|
1446 | 1447 | # pickle fix. See iplib for an explanation. But we need to make sure |
|
1447 | 1448 | # that, if we overwrite __main__, we replace it at the end |
|
1448 | 1449 | if prog_ns['__name__'] == '__main__': |
|
1449 | 1450 | restore_main = sys.modules['__main__'] |
|
1450 | 1451 | else: |
|
1451 | 1452 | restore_main = False |
|
1452 | 1453 | |
|
1453 | 1454 | sys.modules[prog_ns['__name__']] = FakeModule(prog_ns) |
|
1454 | 1455 | |
|
1455 | 1456 | stats = None |
|
1456 | 1457 | try: |
|
1457 | 1458 | if opts.has_key('p'): |
|
1458 | 1459 | stats = self.magic_prun('',0,opts,arg_lst,prog_ns) |
|
1459 | 1460 | else: |
|
1460 | 1461 | if opts.has_key('d'): |
|
1461 | 1462 | deb = Debugger.Pdb(self.shell.rc.colors) |
|
1462 | 1463 | # reset Breakpoint state, which is moronically kept |
|
1463 | 1464 | # in a class |
|
1464 | 1465 | bdb.Breakpoint.next = 1 |
|
1465 | 1466 | bdb.Breakpoint.bplist = {} |
|
1466 | 1467 | bdb.Breakpoint.bpbynumber = [None] |
|
1467 | 1468 | # Set an initial breakpoint to stop execution |
|
1468 | 1469 | maxtries = 10 |
|
1469 | 1470 | bp = int(opts.get('b',[1])[0]) |
|
1470 | 1471 | checkline = deb.checkline(filename,bp) |
|
1471 | 1472 | if not checkline: |
|
1472 | 1473 | for bp in range(bp+1,bp+maxtries+1): |
|
1473 | 1474 | if deb.checkline(filename,bp): |
|
1474 | 1475 | break |
|
1475 | 1476 | else: |
|
1476 | 1477 | msg = ("\nI failed to find a valid line to set " |
|
1477 | 1478 | "a breakpoint\n" |
|
1478 | 1479 | "after trying up to line: %s.\n" |
|
1479 | 1480 | "Please set a valid breakpoint manually " |
|
1480 | 1481 | "with the -b option." % bp) |
|
1481 | 1482 | error(msg) |
|
1482 | 1483 | return |
|
1483 | 1484 | # if we find a good linenumber, set the breakpoint |
|
1484 | 1485 | deb.do_break('%s:%s' % (filename,bp)) |
|
1485 | 1486 | # Start file run |
|
1486 | 1487 | print "NOTE: Enter 'c' at the", |
|
1487 | 1488 | print "ipdb> prompt to start your script." |
|
1488 | 1489 | try: |
|
1489 | 1490 | deb.run('execfile("%s")' % filename,prog_ns) |
|
1490 | 1491 | except: |
|
1491 | 1492 | etype, value, tb = sys.exc_info() |
|
1492 | 1493 | # Skip three frames in the traceback: the %run one, |
|
1493 | 1494 | # one inside bdb.py, and the command-line typed by the |
|
1494 | 1495 | # user (run by exec in pdb itself). |
|
1495 | 1496 | self.shell.InteractiveTB(etype,value,tb,tb_offset=3) |
|
1496 | 1497 | else: |
|
1497 | 1498 | if runner is None: |
|
1498 | 1499 | runner = self.shell.safe_execfile |
|
1499 | 1500 | if opts.has_key('t'): |
|
1500 | 1501 | try: |
|
1501 | 1502 | nruns = int(opts['N'][0]) |
|
1502 | 1503 | if nruns < 1: |
|
1503 | 1504 | error('Number of runs must be >=1') |
|
1504 | 1505 | return |
|
1505 | 1506 | except (KeyError): |
|
1506 | 1507 | nruns = 1 |
|
1507 | 1508 | if nruns == 1: |
|
1508 | 1509 | t0 = clock2() |
|
1509 | 1510 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) |
|
1510 | 1511 | t1 = clock2() |
|
1511 | 1512 | t_usr = t1[0]-t0[0] |
|
1512 | 1513 | t_sys = t1[1]-t1[1] |
|
1513 | 1514 | print "\nIPython CPU timings (estimated):" |
|
1514 | 1515 | print " User : %10s s." % t_usr |
|
1515 | 1516 | print " System: %10s s." % t_sys |
|
1516 | 1517 | else: |
|
1517 | 1518 | runs = range(nruns) |
|
1518 | 1519 | t0 = clock2() |
|
1519 | 1520 | for nr in runs: |
|
1520 | 1521 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) |
|
1521 | 1522 | t1 = clock2() |
|
1522 | 1523 | t_usr = t1[0]-t0[0] |
|
1523 | 1524 | t_sys = t1[1]-t1[1] |
|
1524 | 1525 | print "\nIPython CPU timings (estimated):" |
|
1525 | 1526 | print "Total runs performed:",nruns |
|
1526 | 1527 | print " Times : %10s %10s" % ('Total','Per run') |
|
1527 | 1528 | print " User : %10s s, %10s s." % (t_usr,t_usr/nruns) |
|
1528 | 1529 | print " System: %10s s, %10s s." % (t_sys,t_sys/nruns) |
|
1529 | 1530 | |
|
1530 | 1531 | else: |
|
1531 | 1532 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) |
|
1532 | 1533 | if opts.has_key('i'): |
|
1533 | 1534 | self.shell.user_ns['__name__'] = __name__save |
|
1534 | 1535 | else: |
|
1535 | 1536 | # update IPython interactive namespace |
|
1536 | 1537 | del prog_ns['__name__'] |
|
1537 | 1538 | self.shell.user_ns.update(prog_ns) |
|
1538 | 1539 | finally: |
|
1539 | 1540 | sys.argv = save_argv |
|
1540 | 1541 | if restore_main: |
|
1541 | 1542 | sys.modules['__main__'] = restore_main |
|
1542 | 1543 | return stats |
|
1543 | 1544 | |
|
1544 | 1545 | def magic_runlog(self, parameter_s =''): |
|
1545 | 1546 | """Run files as logs. |
|
1546 | 1547 | |
|
1547 | 1548 | Usage:\\ |
|
1548 | 1549 | %runlog file1 file2 ... |
|
1549 | 1550 | |
|
1550 | 1551 | Run the named files (treating them as log files) in sequence inside |
|
1551 | 1552 | the interpreter, and return to the prompt. This is much slower than |
|
1552 | 1553 | %run because each line is executed in a try/except block, but it |
|
1553 | 1554 | allows running files with syntax errors in them. |
|
1554 | 1555 | |
|
1555 | 1556 | Normally IPython will guess when a file is one of its own logfiles, so |
|
1556 | 1557 | you can typically use %run even for logs. This shorthand allows you to |
|
1557 | 1558 | force any file to be treated as a log file.""" |
|
1558 | 1559 | |
|
1559 | 1560 | for f in parameter_s.split(): |
|
1560 | 1561 | self.shell.safe_execfile(f,self.shell.user_ns, |
|
1561 | 1562 | self.shell.user_ns,islog=1) |
|
1562 | 1563 | |
|
1563 | 1564 | def magic_timeit(self, parameter_s =''): |
|
1564 | 1565 | """Time execution of a Python statement or expression |
|
1565 | 1566 | |
|
1566 | 1567 | Usage:\\ |
|
1567 | 1568 | %timeit [-n<N> -r<R> [-t|-c]] statement |
|
1568 | 1569 | |
|
1569 | 1570 | Time execution of a Python statement or expression using the timeit |
|
1570 | 1571 | module. |
|
1571 | 1572 | |
|
1572 | 1573 | Options: |
|
1573 | 1574 | -n<N>: execute the given statement <N> times in a loop. If this value |
|
1574 | 1575 | is not given, a fitting value is chosen. |
|
1575 | 1576 | |
|
1576 | 1577 | -r<R>: repeat the loop iteration <R> times and take the best result. |
|
1577 | 1578 | Default: 3 |
|
1578 | 1579 | |
|
1579 | 1580 | -t: use time.time to measure the time, which is the default on Unix. |
|
1580 | 1581 | This function measures wall time. |
|
1581 | 1582 | |
|
1582 | 1583 | -c: use time.clock to measure the time, which is the default on |
|
1583 | 1584 | Windows and measures wall time. On Unix, resource.getrusage is used |
|
1584 | 1585 | instead and returns the CPU user time. |
|
1585 | 1586 | |
|
1586 | 1587 | -p<P>: use a precision of <P> digits to display the timing result. |
|
1587 | 1588 | Default: 3 |
|
1588 | 1589 | |
|
1589 | 1590 | |
|
1590 | 1591 | Examples:\\ |
|
1591 | 1592 | In [1]: %timeit pass |
|
1592 | 1593 | 10000000 loops, best of 3: 53.3 ns per loop |
|
1593 | 1594 | |
|
1594 | 1595 | In [2]: u = None |
|
1595 | 1596 | |
|
1596 | 1597 | In [3]: %timeit u is None |
|
1597 | 1598 | 10000000 loops, best of 3: 184 ns per loop |
|
1598 | 1599 | |
|
1599 | 1600 | In [4]: %timeit -r 4 u == None |
|
1600 | 1601 | 1000000 loops, best of 4: 242 ns per loop |
|
1601 | 1602 | |
|
1602 | 1603 | In [5]: import time |
|
1603 | 1604 | |
|
1604 | 1605 | In [6]: %timeit -n1 time.sleep(2) |
|
1605 | 1606 | 1 loops, best of 3: 2 s per loop |
|
1606 | 1607 | |
|
1607 | 1608 | |
|
1608 | 1609 | The times reported by %timeit will be slightly higher than those reported |
|
1609 | 1610 | by the timeit.py script when variables are accessed. This is due to the |
|
1610 | 1611 | fact that %timeit executes the statement in the namespace of the shell, |
|
1611 | 1612 | compared with timeit.py, which uses a single setup statement to import |
|
1612 | 1613 | function or create variables. Generally, the bias does not matter as long |
|
1613 | 1614 | as results from timeit.py are not mixed with those from %timeit.""" |
|
1614 | 1615 | import timeit |
|
1615 | 1616 | import math |
|
1616 | 1617 | |
|
1617 | 1618 | units = ["s", "ms", "\xc2\xb5s", "ns"] |
|
1618 | 1619 | scaling = [1, 1e3, 1e6, 1e9] |
|
1619 | 1620 | |
|
1620 | 1621 | opts, stmt = self.parse_options(parameter_s,'n:r:tcp:') |
|
1621 | 1622 | if stmt == "": |
|
1622 | 1623 | return |
|
1623 | 1624 | timefunc = timeit.default_timer |
|
1624 | 1625 | number = int(getattr(opts, "n", 0)) |
|
1625 | 1626 | repeat = int(getattr(opts, "r", timeit.default_repeat)) |
|
1626 | 1627 | precision = int(getattr(opts, "p", 3)) |
|
1627 | 1628 | if hasattr(opts, "t"): |
|
1628 | 1629 | timefunc = time.time |
|
1629 | 1630 | if hasattr(opts, "c"): |
|
1630 | 1631 | timefunc = clock |
|
1631 | 1632 | |
|
1632 | 1633 | timer = timeit.Timer(timer=timefunc) |
|
1633 | 1634 | # this code has tight coupling to the inner workings of timeit.Timer, |
|
1634 | 1635 | # but is there a better way to achieve that the code stmt has access |
|
1635 | 1636 | # to the shell namespace? |
|
1636 | 1637 | |
|
1637 | 1638 | src = timeit.template % {'stmt': timeit.reindent(stmt, 8), 'setup': "pass"} |
|
1638 | 1639 | code = compile(src, "<magic-timeit>", "exec") |
|
1639 | 1640 | ns = {} |
|
1640 | 1641 | exec code in self.shell.user_ns, ns |
|
1641 | 1642 | timer.inner = ns["inner"] |
|
1642 | 1643 | |
|
1643 | 1644 | if number == 0: |
|
1644 | 1645 | # determine number so that 0.2 <= total time < 2.0 |
|
1645 | 1646 | number = 1 |
|
1646 | 1647 | for i in range(1, 10): |
|
1647 | 1648 | number *= 10 |
|
1648 | 1649 | if timer.timeit(number) >= 0.2: |
|
1649 | 1650 | break |
|
1650 | 1651 | |
|
1651 | 1652 | best = min(timer.repeat(repeat, number)) / number |
|
1652 | 1653 | |
|
1653 | 1654 | if best > 0.0: |
|
1654 | 1655 | order = min(-int(math.floor(math.log10(best)) // 3), 3) |
|
1655 | 1656 | else: |
|
1656 | 1657 | order = 3 |
|
1657 |
print "%d loops, best of %d: %.*g %s per loop" % (number, repeat, |
|
|
1658 | print "%d loops, best of %d: %.*g %s per loop" % (number, repeat, | |
|
1659 | precision, | |
|
1658 | 1660 | best * scaling[order], |
|
1659 | 1661 | units[order]) |
|
1660 | 1662 | |
|
1661 | ||
|
1662 | ||
|
1663 | ||
|
1664 | 1663 | def magic_time(self,parameter_s = ''): |
|
1665 | 1664 | """Time execution of a Python statement or expression. |
|
1666 | 1665 | |
|
1667 | 1666 | The CPU and wall clock times are printed, and the value of the |
|
1668 | 1667 | expression (if any) is returned. Note that under Win32, system time |
|
1669 | 1668 | is always reported as 0, since it can not be measured. |
|
1670 | 1669 | |
|
1671 | 1670 | This function provides very basic timing functionality. In Python |
|
1672 |
2.3, the timeit module offers more control and sophistication, |
|
|
1673 | now IPython supports Python 2.2, so we can not rely on timeit being | |
|
1674 | present. | |
|
1671 | 2.3, the timeit module offers more control and sophistication, so this | |
|
1672 | could be rewritten to use it (patches welcome). | |
|
1675 | 1673 | |
|
1676 | 1674 | Some examples: |
|
1677 | 1675 | |
|
1678 | 1676 | In [1]: time 2**128 |
|
1679 | 1677 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1680 | 1678 | Wall time: 0.00 |
|
1681 | 1679 | Out[1]: 340282366920938463463374607431768211456L |
|
1682 | 1680 | |
|
1683 | 1681 | In [2]: n = 1000000 |
|
1684 | 1682 | |
|
1685 | 1683 | In [3]: time sum(range(n)) |
|
1686 | 1684 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s |
|
1687 | 1685 | Wall time: 1.37 |
|
1688 | 1686 | Out[3]: 499999500000L |
|
1689 | 1687 | |
|
1690 | 1688 | In [4]: time print 'hello world' |
|
1691 | 1689 | hello world |
|
1692 | 1690 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1693 | 1691 | Wall time: 0.00 |
|
1694 | 1692 | """ |
|
1695 | 1693 | |
|
1696 | 1694 | # fail immediately if the given expression can't be compiled |
|
1697 | 1695 | try: |
|
1698 | 1696 | mode = 'eval' |
|
1699 | 1697 | code = compile(parameter_s,'<timed eval>',mode) |
|
1700 | 1698 | except SyntaxError: |
|
1701 | 1699 | mode = 'exec' |
|
1702 | 1700 | code = compile(parameter_s,'<timed exec>',mode) |
|
1703 | 1701 | # skew measurement as little as possible |
|
1704 | 1702 | glob = self.shell.user_ns |
|
1705 | 1703 | clk = clock2 |
|
1706 | 1704 | wtime = time.time |
|
1707 | 1705 | # time execution |
|
1708 | 1706 | wall_st = wtime() |
|
1709 | 1707 | if mode=='eval': |
|
1710 | 1708 | st = clk() |
|
1711 | 1709 | out = eval(code,glob) |
|
1712 | 1710 | end = clk() |
|
1713 | 1711 | else: |
|
1714 | 1712 | st = clk() |
|
1715 | 1713 | exec code in glob |
|
1716 | 1714 | end = clk() |
|
1717 | 1715 | out = None |
|
1718 | 1716 | wall_end = wtime() |
|
1719 | 1717 | # Compute actual times and report |
|
1720 | 1718 | wall_time = wall_end-wall_st |
|
1721 | 1719 | cpu_user = end[0]-st[0] |
|
1722 | 1720 | cpu_sys = end[1]-st[1] |
|
1723 | 1721 | cpu_tot = cpu_user+cpu_sys |
|
1724 | 1722 | print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \ |
|
1725 | 1723 | (cpu_user,cpu_sys,cpu_tot) |
|
1726 | 1724 | print "Wall time: %.2f" % wall_time |
|
1727 | 1725 | return out |
|
1728 | 1726 | |
|
1729 | 1727 | def magic_macro(self,parameter_s = ''): |
|
1730 | 1728 | """Define a set of input lines as a macro for future re-execution. |
|
1731 | 1729 | |
|
1732 | 1730 | Usage:\\ |
|
1733 | 1731 | %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... |
|
1734 | 1732 | |
|
1735 | 1733 | Options: |
|
1736 | 1734 | |
|
1737 | 1735 | -r: use 'raw' input. By default, the 'processed' history is used, |
|
1738 | 1736 | so that magics are loaded in their transformed version to valid |
|
1739 | 1737 | Python. If this option is given, the raw input as typed as the |
|
1740 | 1738 | command line is used instead. |
|
1741 | 1739 | |
|
1742 | 1740 | This will define a global variable called `name` which is a string |
|
1743 | 1741 | made of joining the slices and lines you specify (n1,n2,... numbers |
|
1744 | 1742 | above) from your input history into a single string. This variable |
|
1745 | 1743 | acts like an automatic function which re-executes those lines as if |
|
1746 | 1744 | you had typed them. You just type 'name' at the prompt and the code |
|
1747 | 1745 | executes. |
|
1748 | 1746 | |
|
1749 | 1747 | The notation for indicating number ranges is: n1-n2 means 'use line |
|
1750 | 1748 | numbers n1,...n2' (the endpoint is included). That is, '5-7' means |
|
1751 | 1749 | using the lines numbered 5,6 and 7. |
|
1752 | 1750 | |
|
1753 | 1751 | Note: as a 'hidden' feature, you can also use traditional python slice |
|
1754 | 1752 | notation, where N:M means numbers N through M-1. |
|
1755 | 1753 | |
|
1756 | 1754 | For example, if your history contains (%hist prints it): |
|
1757 | 1755 | |
|
1758 | 1756 | 44: x=1\\ |
|
1759 | 1757 | 45: y=3\\ |
|
1760 | 1758 | 46: z=x+y\\ |
|
1761 | 1759 | 47: print x\\ |
|
1762 | 1760 | 48: a=5\\ |
|
1763 | 1761 | 49: print 'x',x,'y',y\\ |
|
1764 | 1762 | |
|
1765 | 1763 | you can create a macro with lines 44 through 47 (included) and line 49 |
|
1766 | 1764 | called my_macro with: |
|
1767 | 1765 | |
|
1768 | 1766 | In [51]: %macro my_macro 44-47 49 |
|
1769 | 1767 | |
|
1770 | 1768 | Now, typing `my_macro` (without quotes) will re-execute all this code |
|
1771 | 1769 | in one pass. |
|
1772 | 1770 | |
|
1773 | 1771 | You don't need to give the line-numbers in order, and any given line |
|
1774 | 1772 | number can appear multiple times. You can assemble macros with any |
|
1775 | 1773 | lines from your input history in any order. |
|
1776 | 1774 | |
|
1777 | 1775 | The macro is a simple object which holds its value in an attribute, |
|
1778 | 1776 | but IPython's display system checks for macros and executes them as |
|
1779 | 1777 | code instead of printing them when you type their name. |
|
1780 | 1778 | |
|
1781 | 1779 | You can view a macro's contents by explicitly printing it with: |
|
1782 | 1780 | |
|
1783 | 1781 | 'print macro_name'. |
|
1784 | 1782 | |
|
1785 | 1783 | For one-off cases which DON'T contain magic function calls in them you |
|
1786 | 1784 | can obtain similar results by explicitly executing slices from your |
|
1787 | 1785 | input history with: |
|
1788 | 1786 | |
|
1789 | 1787 | In [60]: exec In[44:48]+In[49]""" |
|
1790 | 1788 | |
|
1791 | 1789 | opts,args = self.parse_options(parameter_s,'r',mode='list') |
|
1792 | 1790 | name,ranges = args[0], args[1:] |
|
1793 | 1791 | #print 'rng',ranges # dbg |
|
1794 | 1792 | lines = self.extract_input_slices(ranges,opts.has_key('r')) |
|
1795 | 1793 | macro = Macro(lines) |
|
1796 | 1794 | self.shell.user_ns.update({name:macro}) |
|
1797 | 1795 | print 'Macro `%s` created. To execute, type its name (without quotes).' % name |
|
1798 | 1796 | print 'Macro contents:' |
|
1799 | 1797 | print macro, |
|
1800 | 1798 | |
|
1801 | 1799 | def magic_save(self,parameter_s = ''): |
|
1802 | 1800 | """Save a set of lines to a given filename. |
|
1803 | 1801 | |
|
1804 | 1802 | Usage:\\ |
|
1805 | 1803 | %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ... |
|
1806 | 1804 | |
|
1807 | 1805 | Options: |
|
1808 | 1806 | |
|
1809 | 1807 | -r: use 'raw' input. By default, the 'processed' history is used, |
|
1810 | 1808 | so that magics are loaded in their transformed version to valid |
|
1811 | 1809 | Python. If this option is given, the raw input as typed as the |
|
1812 | 1810 | command line is used instead. |
|
1813 | 1811 | |
|
1814 | 1812 | This function uses the same syntax as %macro for line extraction, but |
|
1815 | 1813 | instead of creating a macro it saves the resulting string to the |
|
1816 | 1814 | filename you specify. |
|
1817 | 1815 | |
|
1818 | 1816 | It adds a '.py' extension to the file if you don't do so yourself, and |
|
1819 | 1817 | it asks for confirmation before overwriting existing files.""" |
|
1820 | 1818 | |
|
1821 | 1819 | opts,args = self.parse_options(parameter_s,'r',mode='list') |
|
1822 | 1820 | fname,ranges = args[0], args[1:] |
|
1823 | 1821 | if not fname.endswith('.py'): |
|
1824 | 1822 | fname += '.py' |
|
1825 | 1823 | if os.path.isfile(fname): |
|
1826 | 1824 | ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname) |
|
1827 | 1825 | if ans.lower() not in ['y','yes']: |
|
1828 | 1826 | print 'Operation cancelled.' |
|
1829 | 1827 | return |
|
1830 | 1828 | cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r'))) |
|
1831 | 1829 | f = file(fname,'w') |
|
1832 | 1830 | f.write(cmds) |
|
1833 | 1831 | f.close() |
|
1834 | 1832 | print 'The following commands were written to file `%s`:' % fname |
|
1835 | 1833 | print cmds |
|
1836 | 1834 | |
|
1837 | 1835 | def _edit_macro(self,mname,macro): |
|
1838 | 1836 | """open an editor with the macro data in a file""" |
|
1839 | 1837 | filename = self.shell.mktempfile(macro.value) |
|
1840 | 1838 | self.shell.hooks.editor(filename) |
|
1841 | 1839 | |
|
1842 | 1840 | # and make a new macro object, to replace the old one |
|
1843 | 1841 | mfile = open(filename) |
|
1844 | 1842 | mvalue = mfile.read() |
|
1845 | 1843 | mfile.close() |
|
1846 | 1844 | self.shell.user_ns[mname] = Macro(mvalue) |
|
1847 | 1845 | |
|
1848 | 1846 | def magic_ed(self,parameter_s=''): |
|
1849 | 1847 | """Alias to %edit.""" |
|
1850 | 1848 | return self.magic_edit(parameter_s) |
|
1851 | 1849 | |
|
1852 | 1850 | def magic_edit(self,parameter_s='',last_call=['','']): |
|
1853 | 1851 | """Bring up an editor and execute the resulting code. |
|
1854 | 1852 | |
|
1855 | 1853 | Usage: |
|
1856 | 1854 | %edit [options] [args] |
|
1857 | 1855 | |
|
1858 | 1856 | %edit runs IPython's editor hook. The default version of this hook is |
|
1859 | 1857 | set to call the __IPYTHON__.rc.editor command. This is read from your |
|
1860 | 1858 | environment variable $EDITOR. If this isn't found, it will default to |
|
1861 | 1859 | vi under Linux/Unix and to notepad under Windows. See the end of this |
|
1862 | 1860 | docstring for how to change the editor hook. |
|
1863 | 1861 | |
|
1864 | 1862 | You can also set the value of this editor via the command line option |
|
1865 | 1863 | '-editor' or in your ipythonrc file. This is useful if you wish to use |
|
1866 | 1864 | specifically for IPython an editor different from your typical default |
|
1867 | 1865 | (and for Windows users who typically don't set environment variables). |
|
1868 | 1866 | |
|
1869 | 1867 | This command allows you to conveniently edit multi-line code right in |
|
1870 | 1868 | your IPython session. |
|
1871 | 1869 | |
|
1872 | 1870 | If called without arguments, %edit opens up an empty editor with a |
|
1873 | 1871 | temporary file and will execute the contents of this file when you |
|
1874 | 1872 | close it (don't forget to save it!). |
|
1875 | 1873 | |
|
1876 | 1874 | |
|
1877 | 1875 | Options: |
|
1878 | 1876 | |
|
1879 | 1877 | -p: this will call the editor with the same data as the previous time |
|
1880 | 1878 | it was used, regardless of how long ago (in your current session) it |
|
1881 | 1879 | was. |
|
1882 | 1880 | |
|
1883 | 1881 | -r: use 'raw' input. This option only applies to input taken from the |
|
1884 | 1882 | user's history. By default, the 'processed' history is used, so that |
|
1885 | 1883 | magics are loaded in their transformed version to valid Python. If |
|
1886 | 1884 | this option is given, the raw input as typed as the command line is |
|
1887 | 1885 | used instead. When you exit the editor, it will be executed by |
|
1888 | 1886 | IPython's own processor. |
|
1889 | 1887 | |
|
1890 | 1888 | -x: do not execute the edited code immediately upon exit. This is |
|
1891 | 1889 | mainly useful if you are editing programs which need to be called with |
|
1892 | 1890 | command line arguments, which you can then do using %run. |
|
1893 | 1891 | |
|
1894 | 1892 | |
|
1895 | 1893 | Arguments: |
|
1896 | 1894 | |
|
1897 | 1895 | If arguments are given, the following possibilites exist: |
|
1898 | 1896 | |
|
1899 | 1897 | - The arguments are numbers or pairs of colon-separated numbers (like |
|
1900 | 1898 | 1 4:8 9). These are interpreted as lines of previous input to be |
|
1901 | 1899 | loaded into the editor. The syntax is the same of the %macro command. |
|
1902 | 1900 | |
|
1903 | 1901 | - If the argument doesn't start with a number, it is evaluated as a |
|
1904 | 1902 | variable and its contents loaded into the editor. You can thus edit |
|
1905 | 1903 | any string which contains python code (including the result of |
|
1906 | 1904 | previous edits). |
|
1907 | 1905 | |
|
1908 | 1906 | - If the argument is the name of an object (other than a string), |
|
1909 | 1907 | IPython will try to locate the file where it was defined and open the |
|
1910 | 1908 | editor at the point where it is defined. You can use `%edit function` |
|
1911 | 1909 | to load an editor exactly at the point where 'function' is defined, |
|
1912 | 1910 | edit it and have the file be executed automatically. |
|
1913 | 1911 | |
|
1914 | 1912 | If the object is a macro (see %macro for details), this opens up your |
|
1915 | 1913 | specified editor with a temporary file containing the macro's data. |
|
1916 | 1914 | Upon exit, the macro is reloaded with the contents of the file. |
|
1917 | 1915 | |
|
1918 | 1916 | Note: opening at an exact line is only supported under Unix, and some |
|
1919 | 1917 | editors (like kedit and gedit up to Gnome 2.8) do not understand the |
|
1920 | 1918 | '+NUMBER' parameter necessary for this feature. Good editors like |
|
1921 | 1919 | (X)Emacs, vi, jed, pico and joe all do. |
|
1922 | 1920 | |
|
1923 | 1921 | - If the argument is not found as a variable, IPython will look for a |
|
1924 | 1922 | file with that name (adding .py if necessary) and load it into the |
|
1925 | 1923 | editor. It will execute its contents with execfile() when you exit, |
|
1926 | 1924 | loading any code in the file into your interactive namespace. |
|
1927 | 1925 | |
|
1928 | 1926 | After executing your code, %edit will return as output the code you |
|
1929 | 1927 | typed in the editor (except when it was an existing file). This way |
|
1930 | 1928 | you can reload the code in further invocations of %edit as a variable, |
|
1931 | 1929 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of |
|
1932 | 1930 | the output. |
|
1933 | 1931 | |
|
1934 | 1932 | Note that %edit is also available through the alias %ed. |
|
1935 | 1933 | |
|
1936 | 1934 | This is an example of creating a simple function inside the editor and |
|
1937 | 1935 | then modifying it. First, start up the editor: |
|
1938 | 1936 | |
|
1939 | 1937 | In [1]: ed\\ |
|
1940 | 1938 | Editing... done. Executing edited code...\\ |
|
1941 | 1939 | Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n' |
|
1942 | 1940 | |
|
1943 | 1941 | We can then call the function foo(): |
|
1944 | 1942 | |
|
1945 | 1943 | In [2]: foo()\\ |
|
1946 | 1944 | foo() was defined in an editing session |
|
1947 | 1945 | |
|
1948 | 1946 | Now we edit foo. IPython automatically loads the editor with the |
|
1949 | 1947 | (temporary) file where foo() was previously defined: |
|
1950 | 1948 | |
|
1951 | 1949 | In [3]: ed foo\\ |
|
1952 | 1950 | Editing... done. Executing edited code... |
|
1953 | 1951 | |
|
1954 | 1952 | And if we call foo() again we get the modified version: |
|
1955 | 1953 | |
|
1956 | 1954 | In [4]: foo()\\ |
|
1957 | 1955 | foo() has now been changed! |
|
1958 | 1956 | |
|
1959 | 1957 | Here is an example of how to edit a code snippet successive |
|
1960 | 1958 | times. First we call the editor: |
|
1961 | 1959 | |
|
1962 | 1960 | In [8]: ed\\ |
|
1963 | 1961 | Editing... done. Executing edited code...\\ |
|
1964 | 1962 | hello\\ |
|
1965 | 1963 | Out[8]: "print 'hello'\\n" |
|
1966 | 1964 | |
|
1967 | 1965 | Now we call it again with the previous output (stored in _): |
|
1968 | 1966 | |
|
1969 | 1967 | In [9]: ed _\\ |
|
1970 | 1968 | Editing... done. Executing edited code...\\ |
|
1971 | 1969 | hello world\\ |
|
1972 | 1970 | Out[9]: "print 'hello world'\\n" |
|
1973 | 1971 | |
|
1974 | 1972 | Now we call it with the output #8 (stored in _8, also as Out[8]): |
|
1975 | 1973 | |
|
1976 | 1974 | In [10]: ed _8\\ |
|
1977 | 1975 | Editing... done. Executing edited code...\\ |
|
1978 | 1976 | hello again\\ |
|
1979 | 1977 | Out[10]: "print 'hello again'\\n" |
|
1980 | 1978 | |
|
1981 | 1979 | |
|
1982 | 1980 | Changing the default editor hook: |
|
1983 | 1981 | |
|
1984 | 1982 | If you wish to write your own editor hook, you can put it in a |
|
1985 | 1983 | configuration file which you load at startup time. The default hook |
|
1986 | 1984 | is defined in the IPython.hooks module, and you can use that as a |
|
1987 | 1985 | starting example for further modifications. That file also has |
|
1988 | 1986 | general instructions on how to set a new hook for use once you've |
|
1989 | 1987 | defined it.""" |
|
1990 | 1988 | |
|
1991 | 1989 | # FIXME: This function has become a convoluted mess. It needs a |
|
1992 | 1990 | # ground-up rewrite with clean, simple logic. |
|
1993 | 1991 | |
|
1994 | 1992 | def make_filename(arg): |
|
1995 | 1993 | "Make a filename from the given args" |
|
1996 | 1994 | try: |
|
1997 | 1995 | filename = get_py_filename(arg) |
|
1998 | 1996 | except IOError: |
|
1999 | 1997 | if args.endswith('.py'): |
|
2000 | 1998 | filename = arg |
|
2001 | 1999 | else: |
|
2002 | 2000 | filename = None |
|
2003 | 2001 | return filename |
|
2004 | 2002 | |
|
2005 | 2003 | # custom exceptions |
|
2006 | 2004 | class DataIsObject(Exception): pass |
|
2007 | 2005 | |
|
2008 | opts,args = self.parse_options(parameter_s,'prx') | |
|
2006 | opts,args = self.parse_options(parameter_s,'prxn=i') | |
|
2007 | ||
|
2008 | print 'opt.n: <%r>' % opts.n # dbg | |
|
2009 | ||
|
2009 | 2010 | # Set a few locals from the options for convenience: |
|
2010 | 2011 | opts_p = opts.has_key('p') |
|
2011 | 2012 | opts_r = opts.has_key('r') |
|
2012 | 2013 | |
|
2013 | 2014 | # Default line number value |
|
2014 | 2015 | lineno = None |
|
2015 | 2016 | if opts_p: |
|
2016 | 2017 | args = '_%s' % last_call[0] |
|
2017 | 2018 | if not self.shell.user_ns.has_key(args): |
|
2018 | 2019 | args = last_call[1] |
|
2019 | 2020 | |
|
2020 | 2021 | # use last_call to remember the state of the previous call, but don't |
|
2021 | 2022 | # let it be clobbered by successive '-p' calls. |
|
2022 | 2023 | try: |
|
2023 | 2024 | last_call[0] = self.shell.outputcache.prompt_count |
|
2024 | 2025 | if not opts_p: |
|
2025 | 2026 | last_call[1] = parameter_s |
|
2026 | 2027 | except: |
|
2027 | 2028 | pass |
|
2028 | 2029 | |
|
2029 | 2030 | # by default this is done with temp files, except when the given |
|
2030 | 2031 | # arg is a filename |
|
2031 | 2032 | use_temp = 1 |
|
2032 | 2033 | |
|
2033 | 2034 | if re.match(r'\d',args): |
|
2034 | 2035 | # Mode where user specifies ranges of lines, like in %macro. |
|
2035 | 2036 | # This means that you can't edit files whose names begin with |
|
2036 | 2037 | # numbers this way. Tough. |
|
2037 | 2038 | ranges = args.split() |
|
2038 | 2039 | data = ''.join(self.extract_input_slices(ranges,opts_r)) |
|
2039 | 2040 | elif args.endswith('.py'): |
|
2040 | 2041 | filename = make_filename(args) |
|
2041 | 2042 | data = '' |
|
2042 | 2043 | use_temp = 0 |
|
2043 | 2044 | elif args: |
|
2044 | 2045 | try: |
|
2045 | 2046 | # Load the parameter given as a variable. If not a string, |
|
2046 | 2047 | # process it as an object instead (below) |
|
2047 | 2048 | |
|
2048 | 2049 | #print '*** args',args,'type',type(args) # dbg |
|
2049 | 2050 | data = eval(args,self.shell.user_ns) |
|
2050 | 2051 | if not type(data) in StringTypes: |
|
2051 | 2052 | raise DataIsObject |
|
2052 | 2053 | |
|
2053 | 2054 | except (NameError,SyntaxError): |
|
2054 | 2055 | # given argument is not a variable, try as a filename |
|
2055 | 2056 | filename = make_filename(args) |
|
2056 | 2057 | if filename is None: |
|
2057 | 2058 | warn("Argument given (%s) can't be found as a variable " |
|
2058 | 2059 | "or as a filename." % args) |
|
2059 | 2060 | return |
|
2060 | 2061 | |
|
2061 | 2062 | data = '' |
|
2062 | 2063 | use_temp = 0 |
|
2063 | 2064 | except DataIsObject: |
|
2064 | 2065 | |
|
2065 | 2066 | # macros have a special edit function |
|
2066 | 2067 | if isinstance(data,Macro): |
|
2067 | 2068 | self._edit_macro(args,data) |
|
2068 | 2069 | return |
|
2069 | 2070 | |
|
2070 | 2071 | # For objects, try to edit the file where they are defined |
|
2071 | 2072 | try: |
|
2072 | 2073 | filename = inspect.getabsfile(data) |
|
2073 | 2074 | datafile = 1 |
|
2074 | 2075 | except TypeError: |
|
2075 | 2076 | filename = make_filename(args) |
|
2076 | 2077 | datafile = 1 |
|
2077 | 2078 | warn('Could not find file where `%s` is defined.\n' |
|
2078 | 2079 | 'Opening a file named `%s`' % (args,filename)) |
|
2079 | 2080 | # Now, make sure we can actually read the source (if it was in |
|
2080 | 2081 | # a temp file it's gone by now). |
|
2081 | 2082 | if datafile: |
|
2082 | 2083 | try: |
|
2083 | 2084 | lineno = inspect.getsourcelines(data)[1] |
|
2084 | 2085 | except IOError: |
|
2085 | 2086 | filename = make_filename(args) |
|
2086 | 2087 | if filename is None: |
|
2087 | 2088 | warn('The file `%s` where `%s` was defined cannot ' |
|
2088 | 2089 | 'be read.' % (filename,data)) |
|
2089 | 2090 | return |
|
2090 | 2091 | use_temp = 0 |
|
2091 | 2092 | else: |
|
2092 | 2093 | data = '' |
|
2093 | 2094 | |
|
2094 | 2095 | if use_temp: |
|
2095 | 2096 | filename = self.shell.mktempfile(data) |
|
2096 | 2097 | print 'IPython will make a temporary file named:',filename |
|
2097 | 2098 | |
|
2098 | 2099 | # do actual editing here |
|
2099 | 2100 | print 'Editing...', |
|
2100 | 2101 | sys.stdout.flush() |
|
2101 | 2102 | self.shell.hooks.editor(filename,lineno) |
|
2102 | 2103 | if opts.has_key('x'): # -x prevents actual execution |
|
2103 | 2104 | |
|
2104 | 2105 | else: |
|
2105 | 2106 | print 'done. Executing edited code...' |
|
2106 | 2107 | if opts_r: |
|
2107 | 2108 | self.shell.runlines(file_read(filename)) |
|
2108 | 2109 | else: |
|
2109 | 2110 | self.shell.safe_execfile(filename,self.shell.user_ns) |
|
2110 | 2111 | if use_temp: |
|
2111 | 2112 | try: |
|
2112 | 2113 | return open(filename).read() |
|
2113 | 2114 | except IOError,msg: |
|
2114 | 2115 | if msg.filename == filename: |
|
2115 | 2116 | warn('File not found. Did you forget to save?') |
|
2116 | 2117 | return |
|
2117 | 2118 | else: |
|
2118 | 2119 | self.shell.showtraceback() |
|
2119 | 2120 | |
|
2120 | 2121 | def magic_xmode(self,parameter_s = ''): |
|
2121 | 2122 | """Switch modes for the exception handlers. |
|
2122 | 2123 | |
|
2123 | 2124 | Valid modes: Plain, Context and Verbose. |
|
2124 | 2125 | |
|
2125 | 2126 | If called without arguments, acts as a toggle.""" |
|
2126 | 2127 | |
|
2127 | 2128 | def xmode_switch_err(name): |
|
2128 | 2129 | warn('Error changing %s exception modes.\n%s' % |
|
2129 | 2130 | (name,sys.exc_info()[1])) |
|
2130 | 2131 | |
|
2131 | 2132 | shell = self.shell |
|
2132 | 2133 | new_mode = parameter_s.strip().capitalize() |
|
2133 | 2134 | try: |
|
2134 | 2135 | shell.InteractiveTB.set_mode(mode=new_mode) |
|
2135 | 2136 | print 'Exception reporting mode:',shell.InteractiveTB.mode |
|
2136 | 2137 | except: |
|
2137 | 2138 | xmode_switch_err('user') |
|
2138 | 2139 | |
|
2139 | 2140 | # threaded shells use a special handler in sys.excepthook |
|
2140 | 2141 | if shell.isthreaded: |
|
2141 | 2142 | try: |
|
2142 | 2143 | shell.sys_excepthook.set_mode(mode=new_mode) |
|
2143 | 2144 | except: |
|
2144 | 2145 | xmode_switch_err('threaded') |
|
2145 | 2146 | |
|
2146 | 2147 | def magic_colors(self,parameter_s = ''): |
|
2147 | 2148 | """Switch color scheme for prompts, info system and exception handlers. |
|
2148 | 2149 | |
|
2149 | 2150 | Currently implemented schemes: NoColor, Linux, LightBG. |
|
2150 | 2151 | |
|
2151 | 2152 | Color scheme names are not case-sensitive.""" |
|
2152 | 2153 | |
|
2153 | 2154 | def color_switch_err(name): |
|
2154 | 2155 | warn('Error changing %s color schemes.\n%s' % |
|
2155 | 2156 | (name,sys.exc_info()[1])) |
|
2156 | 2157 | |
|
2157 | 2158 | |
|
2158 | 2159 | new_scheme = parameter_s.strip() |
|
2159 | 2160 | if not new_scheme: |
|
2160 | 2161 | print 'You must specify a color scheme.' |
|
2161 | 2162 | return |
|
2162 | 2163 | import IPython.rlineimpl as readline |
|
2163 | 2164 | if not readline.have_readline: |
|
2164 | 2165 | msg = """\ |
|
2165 | 2166 | Proper color support under MS Windows requires Gary Bishop's readline library. |
|
2166 | 2167 | You can find it at: |
|
2167 | 2168 | http://sourceforge.net/projects/uncpythontools |
|
2168 | 2169 | Gary's readline needs the ctypes module, from: |
|
2169 | 2170 | http://starship.python.net/crew/theller/ctypes |
|
2170 | 2171 | |
|
2171 | 2172 | Defaulting color scheme to 'NoColor'""" |
|
2172 | 2173 | new_scheme = 'NoColor' |
|
2173 | 2174 | warn(msg) |
|
2174 | 2175 | # local shortcut |
|
2175 | 2176 | shell = self.shell |
|
2176 | 2177 | |
|
2177 | 2178 | # Set prompt colors |
|
2178 | 2179 | try: |
|
2179 | 2180 | shell.outputcache.set_colors(new_scheme) |
|
2180 | 2181 | except: |
|
2181 | 2182 | color_switch_err('prompt') |
|
2182 | 2183 | else: |
|
2183 | 2184 | shell.rc.colors = \ |
|
2184 | 2185 | shell.outputcache.color_table.active_scheme_name |
|
2185 | 2186 | # Set exception colors |
|
2186 | 2187 | try: |
|
2187 | 2188 | shell.InteractiveTB.set_colors(scheme = new_scheme) |
|
2188 | 2189 | shell.SyntaxTB.set_colors(scheme = new_scheme) |
|
2189 | 2190 | except: |
|
2190 | 2191 | color_switch_err('exception') |
|
2191 | 2192 | |
|
2192 | 2193 | # threaded shells use a verbose traceback in sys.excepthook |
|
2193 | 2194 | if shell.isthreaded: |
|
2194 | 2195 | try: |
|
2195 | 2196 | shell.sys_excepthook.set_colors(scheme=new_scheme) |
|
2196 | 2197 | except: |
|
2197 | 2198 | color_switch_err('system exception handler') |
|
2198 | 2199 | |
|
2199 | 2200 | # Set info (for 'object?') colors |
|
2200 | 2201 | if shell.rc.color_info: |
|
2201 | 2202 | try: |
|
2202 | 2203 | shell.inspector.set_active_scheme(new_scheme) |
|
2203 | 2204 | except: |
|
2204 | 2205 | color_switch_err('object inspector') |
|
2205 | 2206 | else: |
|
2206 | 2207 | shell.inspector.set_active_scheme('NoColor') |
|
2207 | 2208 | |
|
2208 | 2209 | def magic_color_info(self,parameter_s = ''): |
|
2209 | 2210 | """Toggle color_info. |
|
2210 | 2211 | |
|
2211 | 2212 | The color_info configuration parameter controls whether colors are |
|
2212 | 2213 | used for displaying object details (by things like %psource, %pfile or |
|
2213 | 2214 | the '?' system). This function toggles this value with each call. |
|
2214 | 2215 | |
|
2215 | 2216 | Note that unless you have a fairly recent pager (less works better |
|
2216 | 2217 | than more) in your system, using colored object information displays |
|
2217 | 2218 | will not work properly. Test it and see.""" |
|
2218 | 2219 | |
|
2219 | 2220 | self.shell.rc.color_info = 1 - self.shell.rc.color_info |
|
2220 | 2221 | self.magic_colors(self.shell.rc.colors) |
|
2221 | 2222 | print 'Object introspection functions have now coloring:', |
|
2222 | 2223 | print ['OFF','ON'][self.shell.rc.color_info] |
|
2223 | 2224 | |
|
2224 | 2225 | def magic_Pprint(self, parameter_s=''): |
|
2225 | 2226 | """Toggle pretty printing on/off.""" |
|
2226 | 2227 | |
|
2227 | 2228 | self.shell.rc.pprint = 1 - self.shell.rc.pprint |
|
2228 | 2229 | print 'Pretty printing has been turned', \ |
|
2229 | 2230 | ['OFF','ON'][self.shell.rc.pprint] |
|
2230 | 2231 | |
|
2231 | 2232 | def magic_exit(self, parameter_s=''): |
|
2232 | 2233 | """Exit IPython, confirming if configured to do so. |
|
2233 | 2234 | |
|
2234 | 2235 | You can configure whether IPython asks for confirmation upon exit by |
|
2235 | 2236 | setting the confirm_exit flag in the ipythonrc file.""" |
|
2236 | 2237 | |
|
2237 | 2238 | self.shell.exit() |
|
2238 | 2239 | |
|
2239 | 2240 | def magic_quit(self, parameter_s=''): |
|
2240 | 2241 | """Exit IPython, confirming if configured to do so (like %exit)""" |
|
2241 | 2242 | |
|
2242 | 2243 | self.shell.exit() |
|
2243 | 2244 | |
|
2244 | 2245 | def magic_Exit(self, parameter_s=''): |
|
2245 | 2246 | """Exit IPython without confirmation.""" |
|
2246 | 2247 | |
|
2247 | 2248 | self.shell.exit_now = True |
|
2248 | 2249 | |
|
2249 | 2250 | def magic_Quit(self, parameter_s=''): |
|
2250 | 2251 | """Exit IPython without confirmation (like %Exit).""" |
|
2251 | 2252 | |
|
2252 | 2253 | self.shell.exit_now = True |
|
2253 | 2254 | |
|
2254 | 2255 | #...................................................................... |
|
2255 | 2256 | # Functions to implement unix shell-type things |
|
2256 | 2257 | |
|
2257 | 2258 | def magic_alias(self, parameter_s = ''): |
|
2258 | 2259 | """Define an alias for a system command. |
|
2259 | 2260 | |
|
2260 | 2261 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' |
|
2261 | 2262 | |
|
2262 | 2263 | Then, typing 'alias_name params' will execute the system command 'cmd |
|
2263 | 2264 | params' (from your underlying operating system). |
|
2264 | 2265 | |
|
2265 | 2266 | Aliases have lower precedence than magic functions and Python normal |
|
2266 | 2267 | variables, so if 'foo' is both a Python variable and an alias, the |
|
2267 | 2268 | alias can not be executed until 'del foo' removes the Python variable. |
|
2268 | 2269 | |
|
2269 | 2270 | You can use the %l specifier in an alias definition to represent the |
|
2270 | 2271 | whole line when the alias is called. For example: |
|
2271 | 2272 | |
|
2272 | 2273 | In [2]: alias all echo "Input in brackets: <%l>"\\ |
|
2273 | 2274 | In [3]: all hello world\\ |
|
2274 | 2275 | Input in brackets: <hello world> |
|
2275 | 2276 | |
|
2276 | 2277 | You can also define aliases with parameters using %s specifiers (one |
|
2277 | 2278 | per parameter): |
|
2278 | 2279 | |
|
2279 | 2280 | In [1]: alias parts echo first %s second %s\\ |
|
2280 | 2281 | In [2]: %parts A B\\ |
|
2281 | 2282 | first A second B\\ |
|
2282 | 2283 | In [3]: %parts A\\ |
|
2283 | 2284 | Incorrect number of arguments: 2 expected.\\ |
|
2284 | 2285 | parts is an alias to: 'echo first %s second %s' |
|
2285 | 2286 | |
|
2286 | 2287 | Note that %l and %s are mutually exclusive. You can only use one or |
|
2287 | 2288 | the other in your aliases. |
|
2288 | 2289 | |
|
2289 | 2290 | Aliases expand Python variables just like system calls using ! or !! |
|
2290 | 2291 | do: all expressions prefixed with '$' get expanded. For details of |
|
2291 | 2292 | the semantic rules, see PEP-215: |
|
2292 | 2293 | http://www.python.org/peps/pep-0215.html. This is the library used by |
|
2293 | 2294 | IPython for variable expansion. If you want to access a true shell |
|
2294 | 2295 | variable, an extra $ is necessary to prevent its expansion by IPython: |
|
2295 | 2296 | |
|
2296 | 2297 | In [6]: alias show echo\\ |
|
2297 | 2298 | In [7]: PATH='A Python string'\\ |
|
2298 | 2299 | In [8]: show $PATH\\ |
|
2299 | 2300 | A Python string\\ |
|
2300 | 2301 | In [9]: show $$PATH\\ |
|
2301 | 2302 | /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:... |
|
2302 | 2303 | |
|
2303 | 2304 | You can use the alias facility to acess all of $PATH. See the %rehash |
|
2304 | 2305 | and %rehashx functions, which automatically create aliases for the |
|
2305 | 2306 | contents of your $PATH. |
|
2306 | 2307 | |
|
2307 | 2308 | If called with no parameters, %alias prints the current alias table.""" |
|
2308 | 2309 | |
|
2309 | 2310 | par = parameter_s.strip() |
|
2310 | 2311 | if not par: |
|
2311 | 2312 | if self.shell.rc.automagic: |
|
2312 | 2313 | prechar = '' |
|
2313 | 2314 | else: |
|
2314 | 2315 | prechar = self.shell.ESC_MAGIC |
|
2315 | 2316 | #print 'Alias\t\tSystem Command\n'+'-'*30 |
|
2316 | 2317 | atab = self.shell.alias_table |
|
2317 | 2318 | aliases = atab.keys() |
|
2318 | 2319 | aliases.sort() |
|
2319 | 2320 | res = [] |
|
2320 | 2321 | for alias in aliases: |
|
2321 | 2322 | res.append((alias, atab[alias][1])) |
|
2322 | 2323 | print "Total number of aliases:",len(aliases) |
|
2323 | 2324 | return res |
|
2324 | 2325 | try: |
|
2325 | 2326 | alias,cmd = par.split(None,1) |
|
2326 | 2327 | except: |
|
2327 | 2328 | print OInspect.getdoc(self.magic_alias) |
|
2328 | 2329 | else: |
|
2329 | 2330 | nargs = cmd.count('%s') |
|
2330 | 2331 | if nargs>0 and cmd.find('%l')>=0: |
|
2331 | 2332 | error('The %s and %l specifiers are mutually exclusive ' |
|
2332 | 2333 | 'in alias definitions.') |
|
2333 | 2334 | else: # all looks OK |
|
2334 | 2335 | self.shell.alias_table[alias] = (nargs,cmd) |
|
2335 | 2336 | self.shell.alias_table_validate(verbose=0) |
|
2336 | 2337 | # end magic_alias |
|
2337 | 2338 | |
|
2338 | 2339 | def magic_unalias(self, parameter_s = ''): |
|
2339 | 2340 | """Remove an alias""" |
|
2340 | 2341 | |
|
2341 | 2342 | aname = parameter_s.strip() |
|
2342 | 2343 | if aname in self.shell.alias_table: |
|
2343 | 2344 | del self.shell.alias_table[aname] |
|
2344 | 2345 | |
|
2345 | 2346 | def magic_rehash(self, parameter_s = ''): |
|
2346 | 2347 | """Update the alias table with all entries in $PATH. |
|
2347 | 2348 | |
|
2348 | 2349 | This version does no checks on execute permissions or whether the |
|
2349 | 2350 | contents of $PATH are truly files (instead of directories or something |
|
2350 | 2351 | else). For such a safer (but slower) version, use %rehashx.""" |
|
2351 | 2352 | |
|
2352 | 2353 | # This function (and rehashx) manipulate the alias_table directly |
|
2353 | 2354 | # rather than calling magic_alias, for speed reasons. A rehash on a |
|
2354 | 2355 | # typical Linux box involves several thousand entries, so efficiency |
|
2355 | 2356 | # here is a top concern. |
|
2356 | 2357 | |
|
2357 | 2358 | path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep)) |
|
2358 | 2359 | alias_table = self.shell.alias_table |
|
2359 | 2360 | for pdir in path: |
|
2360 | 2361 | for ff in os.listdir(pdir): |
|
2361 | 2362 | # each entry in the alias table must be (N,name), where |
|
2362 | 2363 | # N is the number of positional arguments of the alias. |
|
2363 | 2364 | alias_table[ff] = (0,ff) |
|
2364 | 2365 | # Make sure the alias table doesn't contain keywords or builtins |
|
2365 | 2366 | self.shell.alias_table_validate() |
|
2366 | 2367 | # Call again init_auto_alias() so we get 'rm -i' and other modified |
|
2367 | 2368 | # aliases since %rehash will probably clobber them |
|
2368 | 2369 | self.shell.init_auto_alias() |
|
2369 | 2370 | |
|
2370 | 2371 | def magic_rehashx(self, parameter_s = ''): |
|
2371 | 2372 | """Update the alias table with all executable files in $PATH. |
|
2372 | 2373 | |
|
2373 | 2374 | This version explicitly checks that every entry in $PATH is a file |
|
2374 | 2375 | with execute access (os.X_OK), so it is much slower than %rehash. |
|
2375 | 2376 | |
|
2376 | 2377 | Under Windows, it checks executability as a match agains a |
|
2377 | 2378 | '|'-separated string of extensions, stored in the IPython config |
|
2378 | 2379 | variable win_exec_ext. This defaults to 'exe|com|bat'. """ |
|
2379 | 2380 | |
|
2380 | 2381 | path = [os.path.abspath(os.path.expanduser(p)) for p in |
|
2381 | 2382 | os.environ['PATH'].split(os.pathsep)] |
|
2382 | 2383 | path = filter(os.path.isdir,path) |
|
2383 | 2384 | |
|
2384 | 2385 | alias_table = self.shell.alias_table |
|
2385 | 2386 | syscmdlist = [] |
|
2386 | 2387 | if os.name == 'posix': |
|
2387 | 2388 | isexec = lambda fname:os.path.isfile(fname) and \ |
|
2388 | 2389 | os.access(fname,os.X_OK) |
|
2389 | 2390 | else: |
|
2390 | 2391 | |
|
2391 | 2392 | try: |
|
2392 | 2393 | winext = os.environ['pathext'].replace(';','|').replace('.','') |
|
2393 | 2394 | except KeyError: |
|
2394 | 2395 | winext = 'exe|com|bat' |
|
2395 | 2396 | |
|
2396 | 2397 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) |
|
2397 | 2398 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) |
|
2398 | 2399 | savedir = os.getcwd() |
|
2399 | 2400 | try: |
|
2400 | 2401 | # write the whole loop for posix/Windows so we don't have an if in |
|
2401 | 2402 | # the innermost part |
|
2402 | 2403 | if os.name == 'posix': |
|
2403 | 2404 | for pdir in path: |
|
2404 | 2405 | os.chdir(pdir) |
|
2405 | 2406 | for ff in os.listdir(pdir): |
|
2406 | 2407 | if isexec(ff): |
|
2407 | 2408 | # each entry in the alias table must be (N,name), |
|
2408 | 2409 | # where N is the number of positional arguments of the |
|
2409 | 2410 | # alias. |
|
2410 | 2411 | alias_table[ff] = (0,ff) |
|
2411 | 2412 | syscmdlist.append(ff) |
|
2412 | 2413 | else: |
|
2413 | 2414 | for pdir in path: |
|
2414 | 2415 | os.chdir(pdir) |
|
2415 | 2416 | for ff in os.listdir(pdir): |
|
2416 | 2417 | if isexec(ff): |
|
2417 | 2418 | alias_table[execre.sub(r'\1',ff)] = (0,ff) |
|
2418 | 2419 | syscmdlist.append(ff) |
|
2419 | 2420 | # Make sure the alias table doesn't contain keywords or builtins |
|
2420 | 2421 | self.shell.alias_table_validate() |
|
2421 | 2422 | # Call again init_auto_alias() so we get 'rm -i' and other |
|
2422 | 2423 | # modified aliases since %rehashx will probably clobber them |
|
2423 | 2424 | self.shell.init_auto_alias() |
|
2424 |
db = self.getapi(). |
|
|
2425 | db = self.getapi().db | |
|
2425 | 2426 | db['syscmdlist'] = syscmdlist |
|
2426 | 2427 | finally: |
|
2427 | 2428 | os.chdir(savedir) |
|
2428 | 2429 | |
|
2429 | 2430 | def magic_pwd(self, parameter_s = ''): |
|
2430 | 2431 | """Return the current working directory path.""" |
|
2431 | 2432 | return os.getcwd() |
|
2432 | 2433 | |
|
2433 | 2434 | def magic_cd(self, parameter_s=''): |
|
2434 | 2435 | """Change the current working directory. |
|
2435 | 2436 | |
|
2436 | 2437 | This command automatically maintains an internal list of directories |
|
2437 | 2438 | you visit during your IPython session, in the variable _dh. The |
|
2438 | 2439 | command %dhist shows this history nicely formatted. |
|
2439 | 2440 | |
|
2440 | 2441 | Usage: |
|
2441 | 2442 | |
|
2442 | 2443 | cd 'dir': changes to directory 'dir'. |
|
2443 | 2444 | |
|
2444 | 2445 | cd -: changes to the last visited directory. |
|
2445 | 2446 | |
|
2446 | 2447 | cd -<n>: changes to the n-th directory in the directory history. |
|
2447 | 2448 | |
|
2448 | 2449 | cd -b <bookmark_name>: jump to a bookmark set by %bookmark |
|
2449 | 2450 | (note: cd <bookmark_name> is enough if there is no |
|
2450 | 2451 | directory <bookmark_name>, but a bookmark with the name exists.) |
|
2451 | 2452 | |
|
2452 | 2453 | Options: |
|
2453 | 2454 | |
|
2454 | 2455 | -q: quiet. Do not print the working directory after the cd command is |
|
2455 | 2456 | executed. By default IPython's cd command does print this directory, |
|
2456 | 2457 | since the default prompts do not display path information. |
|
2457 | 2458 | |
|
2458 | 2459 | Note that !cd doesn't work for this purpose because the shell where |
|
2459 | 2460 | !command runs is immediately discarded after executing 'command'.""" |
|
2460 | 2461 | |
|
2461 | 2462 | parameter_s = parameter_s.strip() |
|
2462 | 2463 | #bkms = self.shell.persist.get("bookmarks",{}) |
|
2463 | 2464 | |
|
2464 | 2465 | numcd = re.match(r'(-)(\d+)$',parameter_s) |
|
2465 | 2466 | # jump in directory history by number |
|
2466 | 2467 | if numcd: |
|
2467 | 2468 | nn = int(numcd.group(2)) |
|
2468 | 2469 | try: |
|
2469 | 2470 | ps = self.shell.user_ns['_dh'][nn] |
|
2470 | 2471 | except IndexError: |
|
2471 | 2472 | print 'The requested directory does not exist in history.' |
|
2472 | 2473 | return |
|
2473 | 2474 | else: |
|
2474 | 2475 | opts = {} |
|
2475 | 2476 | else: |
|
2476 | 2477 | #turn all non-space-escaping backslashes to slashes, |
|
2477 | 2478 | # for c:\windows\directory\names\ |
|
2478 | 2479 | parameter_s = re.sub(r'\\(?! )','/', parameter_s) |
|
2479 | 2480 | opts,ps = self.parse_options(parameter_s,'qb',mode='string') |
|
2480 | 2481 | # jump to previous |
|
2481 | 2482 | if ps == '-': |
|
2482 | 2483 | try: |
|
2483 | 2484 | ps = self.shell.user_ns['_dh'][-2] |
|
2484 | 2485 | except IndexError: |
|
2485 | 2486 | print 'No previous directory to change to.' |
|
2486 | 2487 | return |
|
2487 | 2488 | # jump to bookmark if needed |
|
2488 | 2489 | else: |
|
2489 | 2490 | if not os.path.isdir(ps) or opts.has_key('b'): |
|
2490 | 2491 | bkms = self.db.get('bookmarks', {}) |
|
2491 | 2492 | |
|
2492 | 2493 | if bkms.has_key(ps): |
|
2493 | 2494 | target = bkms[ps] |
|
2494 | 2495 | print '(bookmark:%s) -> %s' % (ps,target) |
|
2495 | 2496 | ps = target |
|
2496 | 2497 | else: |
|
2497 | 2498 | if opts.has_key('b'): |
|
2498 | 2499 | error("Bookmark '%s' not found. " |
|
2499 | 2500 | "Use '%%bookmark -l' to see your bookmarks." % ps) |
|
2500 | 2501 | return |
|
2501 | 2502 | |
|
2502 | 2503 | # at this point ps should point to the target dir |
|
2503 | 2504 | if ps: |
|
2504 | 2505 | try: |
|
2505 | 2506 | os.chdir(os.path.expanduser(ps)) |
|
2506 | 2507 | ttitle = ("IPy:" + ( |
|
2507 | 2508 | os.getcwd() == '/' and '/' or os.path.basename(os.getcwd()))) |
|
2508 | 2509 | platutils.set_term_title(ttitle) |
|
2509 | 2510 | except OSError: |
|
2510 | 2511 | print sys.exc_info()[1] |
|
2511 | 2512 | else: |
|
2512 | 2513 | self.shell.user_ns['_dh'].append(os.getcwd()) |
|
2513 | 2514 | else: |
|
2514 | 2515 | os.chdir(self.shell.home_dir) |
|
2515 | 2516 | platutils.set_term_title("IPy:~") |
|
2516 | 2517 | self.shell.user_ns['_dh'].append(os.getcwd()) |
|
2517 | 2518 | if not 'q' in opts: |
|
2518 | 2519 | print self.shell.user_ns['_dh'][-1] |
|
2519 | 2520 | |
|
2520 | 2521 | def magic_dhist(self, parameter_s=''): |
|
2521 | 2522 | """Print your history of visited directories. |
|
2522 | 2523 | |
|
2523 | 2524 | %dhist -> print full history\\ |
|
2524 | 2525 | %dhist n -> print last n entries only\\ |
|
2525 | 2526 | %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\ |
|
2526 | 2527 | |
|
2527 | 2528 | This history is automatically maintained by the %cd command, and |
|
2528 | 2529 | always available as the global list variable _dh. You can use %cd -<n> |
|
2529 | 2530 | to go to directory number <n>.""" |
|
2530 | 2531 | |
|
2531 | 2532 | dh = self.shell.user_ns['_dh'] |
|
2532 | 2533 | if parameter_s: |
|
2533 | 2534 | try: |
|
2534 | 2535 | args = map(int,parameter_s.split()) |
|
2535 | 2536 | except: |
|
2536 | 2537 | self.arg_err(Magic.magic_dhist) |
|
2537 | 2538 | return |
|
2538 | 2539 | if len(args) == 1: |
|
2539 | 2540 | ini,fin = max(len(dh)-(args[0]),0),len(dh) |
|
2540 | 2541 | elif len(args) == 2: |
|
2541 | 2542 | ini,fin = args |
|
2542 | 2543 | else: |
|
2543 | 2544 | self.arg_err(Magic.magic_dhist) |
|
2544 | 2545 | return |
|
2545 | 2546 | else: |
|
2546 | 2547 | ini,fin = 0,len(dh) |
|
2547 | 2548 | nlprint(dh, |
|
2548 | 2549 | header = 'Directory history (kept in _dh)', |
|
2549 | 2550 | start=ini,stop=fin) |
|
2550 | 2551 | |
|
2551 | 2552 | def magic_env(self, parameter_s=''): |
|
2552 | 2553 | """List environment variables.""" |
|
2553 | 2554 | |
|
2554 | 2555 | return os.environ.data |
|
2555 | 2556 | |
|
2556 | 2557 | def magic_pushd(self, parameter_s=''): |
|
2557 | 2558 | """Place the current dir on stack and change directory. |
|
2558 | 2559 | |
|
2559 | 2560 | Usage:\\ |
|
2560 | 2561 | %pushd ['dirname'] |
|
2561 | 2562 | |
|
2562 | 2563 | %pushd with no arguments does a %pushd to your home directory. |
|
2563 | 2564 | """ |
|
2564 | 2565 | if parameter_s == '': parameter_s = '~' |
|
2565 | 2566 | dir_s = self.shell.dir_stack |
|
2566 | 2567 | if len(dir_s)>0 and os.path.expanduser(parameter_s) != \ |
|
2567 | 2568 | os.path.expanduser(self.shell.dir_stack[0]): |
|
2568 | 2569 | try: |
|
2569 | 2570 | self.magic_cd(parameter_s) |
|
2570 | 2571 | dir_s.insert(0,os.getcwd().replace(self.home_dir,'~')) |
|
2571 | 2572 | self.magic_dirs() |
|
2572 | 2573 | except: |
|
2573 | 2574 | print 'Invalid directory' |
|
2574 | 2575 | else: |
|
2575 | 2576 | print 'You are already there!' |
|
2576 | 2577 | |
|
2577 | 2578 | def magic_popd(self, parameter_s=''): |
|
2578 | 2579 | """Change to directory popped off the top of the stack. |
|
2579 | 2580 | """ |
|
2580 | 2581 | if len (self.shell.dir_stack) > 1: |
|
2581 | 2582 | self.shell.dir_stack.pop(0) |
|
2582 | 2583 | self.magic_cd(self.shell.dir_stack[0]) |
|
2583 | 2584 | print self.shell.dir_stack[0] |
|
2584 | 2585 | else: |
|
2585 | 2586 | print "You can't remove the starting directory from the stack:",\ |
|
2586 | 2587 | self.shell.dir_stack |
|
2587 | 2588 | |
|
2588 | 2589 | def magic_dirs(self, parameter_s=''): |
|
2589 | 2590 | """Return the current directory stack.""" |
|
2590 | 2591 | |
|
2591 | 2592 | return self.shell.dir_stack[:] |
|
2592 | 2593 | |
|
2593 | 2594 | def magic_sc(self, parameter_s=''): |
|
2594 | 2595 | """Shell capture - execute a shell command and capture its output. |
|
2595 | 2596 | |
|
2596 | 2597 | DEPRECATED. Suboptimal, retained for backwards compatibility. |
|
2597 | 2598 | |
|
2598 | 2599 | You should use the form 'var = !command' instead. Example: |
|
2599 | 2600 | |
|
2600 | 2601 | "%sc -l myfiles = ls ~" should now be written as |
|
2601 | 2602 | |
|
2602 | 2603 | "myfiles = !ls ~" |
|
2603 | 2604 | |
|
2604 | 2605 | myfiles.s, myfiles.l and myfiles.n still apply as documented |
|
2605 | 2606 | below. |
|
2606 | 2607 | |
|
2607 | 2608 | -- |
|
2608 | 2609 | %sc [options] varname=command |
|
2609 | 2610 | |
|
2610 | 2611 | IPython will run the given command using commands.getoutput(), and |
|
2611 | 2612 | will then update the user's interactive namespace with a variable |
|
2612 | 2613 | called varname, containing the value of the call. Your command can |
|
2613 | 2614 | contain shell wildcards, pipes, etc. |
|
2614 | 2615 | |
|
2615 | 2616 | The '=' sign in the syntax is mandatory, and the variable name you |
|
2616 | 2617 | supply must follow Python's standard conventions for valid names. |
|
2617 | 2618 | |
|
2618 | 2619 | (A special format without variable name exists for internal use) |
|
2619 | 2620 | |
|
2620 | 2621 | Options: |
|
2621 | 2622 | |
|
2622 | 2623 | -l: list output. Split the output on newlines into a list before |
|
2623 | 2624 | assigning it to the given variable. By default the output is stored |
|
2624 | 2625 | as a single string. |
|
2625 | 2626 | |
|
2626 | 2627 | -v: verbose. Print the contents of the variable. |
|
2627 | 2628 | |
|
2628 | 2629 | In most cases you should not need to split as a list, because the |
|
2629 | 2630 | returned value is a special type of string which can automatically |
|
2630 | 2631 | provide its contents either as a list (split on newlines) or as a |
|
2631 | 2632 | space-separated string. These are convenient, respectively, either |
|
2632 | 2633 | for sequential processing or to be passed to a shell command. |
|
2633 | 2634 | |
|
2634 | 2635 | For example: |
|
2635 | 2636 | |
|
2636 | 2637 | # Capture into variable a |
|
2637 | 2638 | In [9]: sc a=ls *py |
|
2638 | 2639 | |
|
2639 | 2640 | # a is a string with embedded newlines |
|
2640 | 2641 | In [10]: a |
|
2641 | 2642 | Out[10]: 'setup.py\nwin32_manual_post_install.py' |
|
2642 | 2643 | |
|
2643 | 2644 | # which can be seen as a list: |
|
2644 | 2645 | In [11]: a.l |
|
2645 | 2646 | Out[11]: ['setup.py', 'win32_manual_post_install.py'] |
|
2646 | 2647 | |
|
2647 | 2648 | # or as a whitespace-separated string: |
|
2648 | 2649 | In [12]: a.s |
|
2649 | 2650 | Out[12]: 'setup.py win32_manual_post_install.py' |
|
2650 | 2651 | |
|
2651 | 2652 | # a.s is useful to pass as a single command line: |
|
2652 | 2653 | In [13]: !wc -l $a.s |
|
2653 | 2654 | 146 setup.py |
|
2654 | 2655 | 130 win32_manual_post_install.py |
|
2655 | 2656 | 276 total |
|
2656 | 2657 | |
|
2657 | 2658 | # while the list form is useful to loop over: |
|
2658 | 2659 | In [14]: for f in a.l: |
|
2659 | 2660 | ....: !wc -l $f |
|
2660 | 2661 | ....: |
|
2661 | 2662 | 146 setup.py |
|
2662 | 2663 | 130 win32_manual_post_install.py |
|
2663 | 2664 | |
|
2664 | 2665 | Similiarly, the lists returned by the -l option are also special, in |
|
2665 | 2666 | the sense that you can equally invoke the .s attribute on them to |
|
2666 | 2667 | automatically get a whitespace-separated string from their contents: |
|
2667 | 2668 | |
|
2668 | 2669 | In [1]: sc -l b=ls *py |
|
2669 | 2670 | |
|
2670 | 2671 | In [2]: b |
|
2671 | 2672 | Out[2]: ['setup.py', 'win32_manual_post_install.py'] |
|
2672 | 2673 | |
|
2673 | 2674 | In [3]: b.s |
|
2674 | 2675 | Out[3]: 'setup.py win32_manual_post_install.py' |
|
2675 | 2676 | |
|
2676 | 2677 | In summary, both the lists and strings used for ouptut capture have |
|
2677 | 2678 | the following special attributes: |
|
2678 | 2679 | |
|
2679 | 2680 | .l (or .list) : value as list. |
|
2680 | 2681 | .n (or .nlstr): value as newline-separated string. |
|
2681 | 2682 | .s (or .spstr): value as space-separated string. |
|
2682 | 2683 | """ |
|
2683 | 2684 | |
|
2684 | 2685 | opts,args = self.parse_options(parameter_s,'lv') |
|
2685 | 2686 | # Try to get a variable name and command to run |
|
2686 | 2687 | try: |
|
2687 | 2688 | # the variable name must be obtained from the parse_options |
|
2688 | 2689 | # output, which uses shlex.split to strip options out. |
|
2689 | 2690 | var,_ = args.split('=',1) |
|
2690 | 2691 | var = var.strip() |
|
2691 | 2692 | # But the the command has to be extracted from the original input |
|
2692 | 2693 | # parameter_s, not on what parse_options returns, to avoid the |
|
2693 | 2694 | # quote stripping which shlex.split performs on it. |
|
2694 | 2695 | _,cmd = parameter_s.split('=',1) |
|
2695 | 2696 | except ValueError: |
|
2696 | 2697 | var,cmd = '','' |
|
2697 | 2698 | # If all looks ok, proceed |
|
2698 | 2699 | out,err = self.shell.getoutputerror(cmd) |
|
2699 | 2700 | if err: |
|
2700 | 2701 | print >> Term.cerr,err |
|
2701 | 2702 | if opts.has_key('l'): |
|
2702 | 2703 | out = SList(out.split('\n')) |
|
2703 | 2704 | else: |
|
2704 | 2705 | out = LSString(out) |
|
2705 | 2706 | if opts.has_key('v'): |
|
2706 | 2707 | print '%s ==\n%s' % (var,pformat(out)) |
|
2707 | 2708 | if var: |
|
2708 | 2709 | self.shell.user_ns.update({var:out}) |
|
2709 | 2710 | else: |
|
2710 | 2711 | return out |
|
2711 | 2712 | |
|
2712 | 2713 | def magic_sx(self, parameter_s=''): |
|
2713 | 2714 | """Shell execute - run a shell command and capture its output. |
|
2714 | 2715 | |
|
2715 | 2716 | %sx command |
|
2716 | 2717 | |
|
2717 | 2718 | IPython will run the given command using commands.getoutput(), and |
|
2718 | 2719 | return the result formatted as a list (split on '\\n'). Since the |
|
2719 | 2720 | output is _returned_, it will be stored in ipython's regular output |
|
2720 | 2721 | cache Out[N] and in the '_N' automatic variables. |
|
2721 | 2722 | |
|
2722 | 2723 | Notes: |
|
2723 | 2724 | |
|
2724 | 2725 | 1) If an input line begins with '!!', then %sx is automatically |
|
2725 | 2726 | invoked. That is, while: |
|
2726 | 2727 | !ls |
|
2727 | 2728 | causes ipython to simply issue system('ls'), typing |
|
2728 | 2729 | !!ls |
|
2729 | 2730 | is a shorthand equivalent to: |
|
2730 | 2731 | %sx ls |
|
2731 | 2732 | |
|
2732 | 2733 | 2) %sx differs from %sc in that %sx automatically splits into a list, |
|
2733 | 2734 | like '%sc -l'. The reason for this is to make it as easy as possible |
|
2734 | 2735 | to process line-oriented shell output via further python commands. |
|
2735 | 2736 | %sc is meant to provide much finer control, but requires more |
|
2736 | 2737 | typing. |
|
2737 | 2738 | |
|
2738 | 2739 | 3) Just like %sc -l, this is a list with special attributes: |
|
2739 | 2740 | |
|
2740 | 2741 | .l (or .list) : value as list. |
|
2741 | 2742 | .n (or .nlstr): value as newline-separated string. |
|
2742 | 2743 | .s (or .spstr): value as whitespace-separated string. |
|
2743 | 2744 | |
|
2744 | 2745 | This is very useful when trying to use such lists as arguments to |
|
2745 | 2746 | system commands.""" |
|
2746 | 2747 | |
|
2747 | 2748 | if parameter_s: |
|
2748 | 2749 | out,err = self.shell.getoutputerror(parameter_s) |
|
2749 | 2750 | if err: |
|
2750 | 2751 | print >> Term.cerr,err |
|
2751 | 2752 | return SList(out.split('\n')) |
|
2752 | 2753 | |
|
2753 | 2754 | def magic_bg(self, parameter_s=''): |
|
2754 | 2755 | """Run a job in the background, in a separate thread. |
|
2755 | 2756 | |
|
2756 | 2757 | For example, |
|
2757 | 2758 | |
|
2758 | 2759 | %bg myfunc(x,y,z=1) |
|
2759 | 2760 | |
|
2760 | 2761 | will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the |
|
2761 | 2762 | execution starts, a message will be printed indicating the job |
|
2762 | 2763 | number. If your job number is 5, you can use |
|
2763 | 2764 | |
|
2764 | 2765 | myvar = jobs.result(5) or myvar = jobs[5].result |
|
2765 | 2766 | |
|
2766 | 2767 | to assign this result to variable 'myvar'. |
|
2767 | 2768 | |
|
2768 | 2769 | IPython has a job manager, accessible via the 'jobs' object. You can |
|
2769 | 2770 | type jobs? to get more information about it, and use jobs.<TAB> to see |
|
2770 | 2771 | its attributes. All attributes not starting with an underscore are |
|
2771 | 2772 | meant for public use. |
|
2772 | 2773 | |
|
2773 | 2774 | In particular, look at the jobs.new() method, which is used to create |
|
2774 | 2775 | new jobs. This magic %bg function is just a convenience wrapper |
|
2775 | 2776 | around jobs.new(), for expression-based jobs. If you want to create a |
|
2776 | 2777 | new job with an explicit function object and arguments, you must call |
|
2777 | 2778 | jobs.new() directly. |
|
2778 | 2779 | |
|
2779 | 2780 | The jobs.new docstring also describes in detail several important |
|
2780 | 2781 | caveats associated with a thread-based model for background job |
|
2781 | 2782 | execution. Type jobs.new? for details. |
|
2782 | 2783 | |
|
2783 | 2784 | You can check the status of all jobs with jobs.status(). |
|
2784 | 2785 | |
|
2785 | 2786 | The jobs variable is set by IPython into the Python builtin namespace. |
|
2786 | 2787 | If you ever declare a variable named 'jobs', you will shadow this |
|
2787 | 2788 | name. You can either delete your global jobs variable to regain |
|
2788 | 2789 | access to the job manager, or make a new name and assign it manually |
|
2789 | 2790 | to the manager (stored in IPython's namespace). For example, to |
|
2790 | 2791 | assign the job manager to the Jobs name, use: |
|
2791 | 2792 | |
|
2792 | 2793 | Jobs = __builtins__.jobs""" |
|
2793 | 2794 | |
|
2794 | 2795 | self.shell.jobs.new(parameter_s,self.shell.user_ns) |
|
2795 | 2796 | |
|
2796 | 2797 | |
|
2797 | 2798 | def magic_bookmark(self, parameter_s=''): |
|
2798 | 2799 | """Manage IPython's bookmark system. |
|
2799 | 2800 | |
|
2800 | 2801 | %bookmark <name> - set bookmark to current dir |
|
2801 | 2802 | %bookmark <name> <dir> - set bookmark to <dir> |
|
2802 | 2803 | %bookmark -l - list all bookmarks |
|
2803 | 2804 | %bookmark -d <name> - remove bookmark |
|
2804 | 2805 | %bookmark -r - remove all bookmarks |
|
2805 | 2806 | |
|
2806 | 2807 | You can later on access a bookmarked folder with: |
|
2807 | 2808 | %cd -b <name> |
|
2808 | 2809 | or simply '%cd <name>' if there is no directory called <name> AND |
|
2809 | 2810 | there is such a bookmark defined. |
|
2810 | 2811 | |
|
2811 | 2812 | Your bookmarks persist through IPython sessions, but they are |
|
2812 | 2813 | associated with each profile.""" |
|
2813 | 2814 | |
|
2814 | 2815 | opts,args = self.parse_options(parameter_s,'drl',mode='list') |
|
2815 | 2816 | if len(args) > 2: |
|
2816 | 2817 | error('You can only give at most two arguments') |
|
2817 | 2818 | return |
|
2818 | 2819 | |
|
2819 | 2820 | bkms = self.db.get('bookmarks',{}) |
|
2820 | 2821 | |
|
2821 | 2822 | if opts.has_key('d'): |
|
2822 | 2823 | try: |
|
2823 | 2824 | todel = args[0] |
|
2824 | 2825 | except IndexError: |
|
2825 | 2826 | error('You must provide a bookmark to delete') |
|
2826 | 2827 | else: |
|
2827 | 2828 | try: |
|
2828 | 2829 | del bkms[todel] |
|
2829 | 2830 | except: |
|
2830 | 2831 | error("Can't delete bookmark '%s'" % todel) |
|
2831 | 2832 | elif opts.has_key('r'): |
|
2832 | 2833 | bkms = {} |
|
2833 | 2834 | elif opts.has_key('l'): |
|
2834 | 2835 | bks = bkms.keys() |
|
2835 | 2836 | bks.sort() |
|
2836 | 2837 | if bks: |
|
2837 | 2838 | size = max(map(len,bks)) |
|
2838 | 2839 | else: |
|
2839 | 2840 | size = 0 |
|
2840 | 2841 | fmt = '%-'+str(size)+'s -> %s' |
|
2841 | 2842 | print 'Current bookmarks:' |
|
2842 | 2843 | for bk in bks: |
|
2843 | 2844 | print fmt % (bk,bkms[bk]) |
|
2844 | 2845 | else: |
|
2845 | 2846 | if not args: |
|
2846 | 2847 | error("You must specify the bookmark name") |
|
2847 | 2848 | elif len(args)==1: |
|
2848 | 2849 | bkms[args[0]] = os.getcwd() |
|
2849 | 2850 | elif len(args)==2: |
|
2850 | 2851 | bkms[args[0]] = args[1] |
|
2851 | 2852 | self.db['bookmarks'] = bkms |
|
2852 | 2853 | |
|
2853 | 2854 | def magic_pycat(self, parameter_s=''): |
|
2854 | 2855 | """Show a syntax-highlighted file through a pager. |
|
2855 | 2856 | |
|
2856 | 2857 | This magic is similar to the cat utility, but it will assume the file |
|
2857 | 2858 | to be Python source and will show it with syntax highlighting. """ |
|
2858 | 2859 | |
|
2859 | 2860 | try: |
|
2860 | 2861 | filename = get_py_filename(parameter_s) |
|
2861 | 2862 | cont = file_read(filename) |
|
2862 | 2863 | except IOError: |
|
2863 | 2864 | try: |
|
2864 | 2865 | cont = eval(parameter_s,self.user_ns) |
|
2865 | 2866 | except NameError: |
|
2866 | 2867 | cont = None |
|
2867 | 2868 | if cont is None: |
|
2868 | 2869 | print "Error: no such file or variable" |
|
2869 | 2870 | return |
|
2870 | 2871 | |
|
2871 | 2872 | page(self.shell.pycolorize(cont), |
|
2872 | 2873 | screen_lines=self.shell.rc.screen_length) |
|
2873 | 2874 | |
|
2874 | 2875 | def magic_cpaste(self, parameter_s=''): |
|
2875 | 2876 | """Allows you to paste & execute a pre-formatted code block from clipboard |
|
2876 | 2877 | |
|
2877 | 2878 | You must terminate the block with '--' (two minus-signs) alone on the |
|
2878 | 2879 | line. You can also provide your own sentinel with '%paste -s %%' ('%%' |
|
2879 | 2880 | is the new sentinel for this operation) |
|
2880 | 2881 | |
|
2881 | 2882 | The block is dedented prior to execution to enable execution of |
|
2882 | 2883 | method definitions. The executed block is also assigned to variable |
|
2883 | 2884 | named 'pasted_block' for later editing with '%edit pasted_block'. |
|
2884 | 2885 | |
|
2885 | 2886 | You can also pass a variable name as an argument, e.g. '%cpaste foo'. |
|
2886 | 2887 | This assigns the pasted block to variable 'foo' as string, without |
|
2887 | 2888 | dedenting or executing it. |
|
2888 | 2889 | |
|
2889 | 2890 | Do not be alarmed by garbled output on Windows (it's a readline bug). |
|
2890 | 2891 | Just press enter and type -- (and press enter again) and the block |
|
2891 | 2892 | will be what was just pasted. |
|
2892 | 2893 | |
|
2893 | 2894 | IPython statements (magics, shell escapes) are not supported (yet). |
|
2894 | 2895 | """ |
|
2895 | 2896 | opts,args = self.parse_options(parameter_s,'s:',mode='string') |
|
2896 | 2897 | par = args.strip() |
|
2897 | 2898 | sentinel = opts.get('s','--') |
|
2898 | 2899 | |
|
2899 | 2900 | from IPython import iplib |
|
2900 | 2901 | lines = [] |
|
2901 | 2902 | print "Pasting code; enter '%s' alone on the line to stop." % sentinel |
|
2902 | 2903 | while 1: |
|
2903 | 2904 | l = iplib.raw_input_original(':') |
|
2904 | 2905 | if l ==sentinel: |
|
2905 | 2906 | break |
|
2906 | 2907 | lines.append(l) |
|
2907 | 2908 | block = "\n".join(lines) + '\n' |
|
2908 | 2909 | #print "block:\n",block |
|
2909 | 2910 | if not par: |
|
2910 | 2911 | b = textwrap.dedent(block) |
|
2911 | 2912 | exec b in self.user_ns |
|
2912 | 2913 | self.user_ns['pasted_block'] = b |
|
2913 | 2914 | else: |
|
2914 | 2915 | self.user_ns[par] = block |
|
2915 | 2916 | print "Block assigned to '%s'" % par |
|
2916 | 2917 | |
|
2917 | 2918 | def magic_quickref(self,arg): |
|
2918 | 2919 | """ Show a quick reference sheet """ |
|
2919 | 2920 | import IPython.usage |
|
2920 | 2921 | qr = IPython.usage.quick_reference + self.magic_magic('-brief') |
|
2921 | 2922 | |
|
2922 | 2923 | page(qr) |
|
2923 | 2924 | |
|
2924 | 2925 | def magic_upgrade(self,arg): |
|
2925 | 2926 | """ Upgrade your IPython installation |
|
2926 | 2927 | |
|
2927 | 2928 | This will copy the config files that don't yet exist in your |
|
2928 | 2929 | ipython dir from the system config dir. Use this after upgrading |
|
2929 | 2930 | IPython if you don't wish to delete your .ipython dir. |
|
2930 | 2931 | |
|
2931 | 2932 | Call with -nolegacy to get rid of ipythonrc* files (recommended for |
|
2932 | 2933 | new users) |
|
2933 | 2934 | |
|
2934 | 2935 | """ |
|
2935 | 2936 | ip = self.getapi() |
|
2936 | 2937 | ipinstallation = path(IPython.__file__).dirname() |
|
2937 | 2938 | upgrade_script = sys.executable + " " + ipinstallation / 'upgrade_dir.py' |
|
2938 | 2939 | src_config = ipinstallation / 'UserConfig' |
|
2939 |
userdir = path(ip.options |
|
|
2940 | userdir = path(ip.options.ipythondir) | |
|
2940 | 2941 | cmd = upgrade_script + " " + src_config + " " + userdir |
|
2941 | 2942 | print ">",cmd |
|
2942 | 2943 | shell(cmd) |
|
2943 | 2944 | if arg == '-nolegacy': |
|
2944 | 2945 | legacy = userdir.files('ipythonrc*') |
|
2945 | 2946 | print "Nuking legacy files:",legacy |
|
2946 | 2947 | |
|
2947 | 2948 | [p.remove() for p in legacy] |
|
2948 | 2949 | suffix = (sys.platform == 'win32' and '.ini' or '') |
|
2949 | 2950 | (userdir / ('ipythonrc' + suffix)).write_text('# Empty, see ipy_user_conf.py\n') |
|
2950 | 2951 | |
|
2951 | 2952 | |
|
2952 | 2953 | # end Magic |
@@ -1,85 +1,82 b'' | |||
|
1 |
""" |
|
|
1 | """Shell mode for IPython. | |
|
2 | 2 | |
|
3 | 3 | Start ipython in shell mode by invoking "ipython -p sh" |
|
4 | 4 | |
|
5 | 5 | (the old version, "ipython -p pysh" still works but this is the more "modern" |
|
6 | 6 | shell mode and is recommended for users who don't care about pysh-mode |
|
7 | 7 | compatibility) |
|
8 | ||
|
9 | ||
|
10 | 8 | """ |
|
11 | 9 | |
|
12 | ||
|
13 | 10 | from IPython import ipapi |
|
14 | 11 | import os,textwrap |
|
15 | 12 | |
|
16 | 13 | # The import below effectively obsoletes your old-style ipythonrc[.ini], |
|
17 | 14 | # so consider yourself warned! |
|
18 | 15 | |
|
19 |
import ipy_ |
|
|
16 | import ipy_defaults | |
|
20 | 17 | |
|
21 | 18 | def main(): |
|
22 | 19 | ip = ipapi.get() |
|
23 |
o = ip.options |
|
|
20 | o = ip.options | |
|
24 | 21 | # autocall to "full" mode (smart mode is default, I like full mode) |
|
25 | 22 | |
|
26 | 23 | o.autocall = 2 |
|
27 | 24 | |
|
28 | 25 | # Jason Orendorff's path class is handy to have in user namespace |
|
29 | 26 | # if you are doing shell-like stuff |
|
30 | 27 | try: |
|
31 | 28 | ip.ex("from path import path" ) |
|
32 | 29 | except ImportError: |
|
33 | 30 | pass |
|
34 | 31 | |
|
35 | 32 | ip.ex('import os') |
|
36 | 33 | ip.ex("def up(): os.chdir('..')") |
|
37 | 34 | |
|
38 | 35 | # Get pysh-like prompt for all profiles. |
|
39 | 36 | |
|
40 | 37 | o.prompt_in1= '\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> ' |
|
41 | 38 | o.prompt_in2= '\C_Green|\C_LightGreen\D\C_Green> ' |
|
42 | 39 | o.prompt_out= '<\#> ' |
|
43 | 40 | |
|
44 | 41 | from IPython import Release |
|
45 | 42 | |
|
46 | 43 | import sys |
|
47 | 44 | # I like my banner minimal. |
|
48 | 45 | o.banner = "Py %s IPy %s\n" % (sys.version.split('\n')[0],Release.version) |
|
49 | 46 | |
|
50 | 47 | # make 'd' an alias for ls -F |
|
51 | 48 | |
|
52 | 49 | ip.magic('alias d ls -F --color=auto') |
|
53 | 50 | |
|
54 | 51 | # Make available all system commands through "rehashing" immediately. |
|
55 | 52 | # You can comment these lines out to speed up startup on very slow |
|
56 | 53 | # machines, and to conserve a bit of memory. Note that pysh profile does this |
|
57 | 54 | # automatically |
|
58 | 55 | ip.IP.default_option('cd','-q') |
|
59 | 56 | |
|
60 | 57 | |
|
61 | 58 | o.prompts_pad_left="1" |
|
62 | 59 | # Remove all blank lines in between prompts, like a normal shell. |
|
63 | 60 | o.separate_in="0" |
|
64 | 61 | o.separate_out="0" |
|
65 | 62 | o.separate_out2="0" |
|
66 | 63 | |
|
67 | 64 | # now alias all syscommands |
|
68 | 65 | |
|
69 |
db = ip. |
|
|
66 | db = ip.db | |
|
70 | 67 | |
|
71 | 68 | syscmds = db.get("syscmdlist",[] ) |
|
72 | 69 | if not syscmds: |
|
73 | 70 | print textwrap.dedent(""" |
|
74 | 71 | System command list not initialized, probably the first run... |
|
75 | 72 | running %rehashx to refresh the command list. Run %rehashx |
|
76 | 73 | again to refresh command list (after installing new software etc.) |
|
77 | 74 | """) |
|
78 | 75 | ip.magic('rehashx') |
|
79 | 76 | syscmds = db.get("syscmdlist") |
|
80 | 77 | for cmd in syscmds: |
|
81 | 78 | #print "al",cmd |
|
82 | 79 | noext, ext = os.path.splitext(cmd) |
|
83 | 80 | ip.IP.alias_table[noext] = (0,cmd) |
|
84 | 81 | |
|
85 | 82 | main() |
@@ -1,31 +1,31 b'' | |||
|
1 | 1 | """ User configuration file for IPython |
|
2 | 2 | |
|
3 | 3 | This is a more flexible and safe way to configure ipython than *rc files |
|
4 | 4 | (ipythonrc, ipythonrc-pysh etc.) |
|
5 | 5 | |
|
6 | 6 | This file is always imported on ipython startup. You can import the |
|
7 | 7 | ipython extensions you need here (see IPython/Extensions directory). |
|
8 | 8 | |
|
9 | 9 | Feel free to edit this file to customize your ipython experience. |
|
10 | 10 | |
|
11 | 11 | Note that as such this file does nothing, for backwards compatibility. |
|
12 | 12 | Consult e.g. file 'ipy_profile_sh.py' for an example of the things |
|
13 | 13 | you can do here. |
|
14 | 14 | |
|
15 | 15 | """ |
|
16 | 16 | |
|
17 | 17 | # Most of your config files and extensions will probably start with this import |
|
18 | 18 | |
|
19 | 19 | import IPython.ipapi |
|
20 | 20 | ip = IPython.ipapi.get() |
|
21 | 21 | |
|
22 | 22 | # You probably want to uncomment this if you did %upgrade -nolegacy |
|
23 |
# import ipy_ |
|
|
23 | # import ipy_defaults | |
|
24 | 24 | |
|
25 | 25 | def main(): |
|
26 |
o = ip.options |
|
|
26 | o = ip.options | |
|
27 | 27 | # An example on how to set options |
|
28 | 28 | #o.autocall = 1 |
|
29 | 29 | |
|
30 | 30 | main() |
|
31 | 31 |
@@ -1,73 +1,73 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | IPython -- An enhanced Interactive Python |
|
4 | 4 | |
|
5 | 5 | One of Python's nicest features is its interactive interpreter. This allows |
|
6 | 6 | very fast testing of ideas without the overhead of creating test files as is |
|
7 | 7 | typical in most programming languages. However, the interpreter supplied with |
|
8 | 8 | the standard Python distribution is fairly primitive (and IDLE isn't really |
|
9 | 9 | much better). |
|
10 | 10 | |
|
11 | 11 | IPython tries to: |
|
12 | 12 | |
|
13 | 13 | i - provide an efficient environment for interactive work in Python |
|
14 | 14 | programming. It tries to address what we see as shortcomings of the standard |
|
15 | 15 | Python prompt, and adds many features to make interactive work much more |
|
16 | 16 | efficient. |
|
17 | 17 | |
|
18 | 18 | ii - offer a flexible framework so that it can be used as the base |
|
19 | 19 | environment for other projects and problems where Python can be the |
|
20 | 20 | underlying language. Specifically scientific environments like Mathematica, |
|
21 | 21 | IDL and Mathcad inspired its design, but similar ideas can be useful in many |
|
22 | 22 | fields. Python is a fabulous language for implementing this kind of system |
|
23 | 23 | (due to its dynamic and introspective features), and with suitable libraries |
|
24 | 24 | entire systems could be built leveraging Python's power. |
|
25 | 25 | |
|
26 | 26 | iii - serve as an embeddable, ready to go interpreter for your own programs. |
|
27 | 27 | |
|
28 |
IPython requires Python 2. |
|
|
28 | IPython requires Python 2.3 or newer. | |
|
29 | 29 | |
|
30 |
$Id: __init__.py 1 |
|
|
30 | $Id: __init__.py 1314 2006-05-19 18:24:14Z fperez $""" | |
|
31 | 31 | |
|
32 | 32 | #***************************************************************************** |
|
33 | 33 | # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu> |
|
34 | 34 | # |
|
35 | 35 | # Distributed under the terms of the BSD License. The full license is in |
|
36 | 36 | # the file COPYING, distributed as part of this software. |
|
37 | 37 | #***************************************************************************** |
|
38 | 38 | |
|
39 | 39 | # Enforce proper version requirements |
|
40 | 40 | import sys |
|
41 | 41 | |
|
42 | 42 | if sys.version[0:3] < '2.3': |
|
43 | 43 | raise ImportError, 'Python Version 2.3 or above is required.' |
|
44 | 44 | |
|
45 | 45 | # Make it easy to import extensions - they are always directly on pythonpath. |
|
46 | 46 | # Therefore, non-IPython modules can be added to Extensions directory |
|
47 | 47 | |
|
48 | 48 | import os |
|
49 | 49 | sys.path.append(os.path.dirname(__file__) + "/Extensions") |
|
50 | 50 | |
|
51 | 51 | # Define what gets imported with a 'from IPython import *' |
|
52 | 52 | __all__ = ['deep_reload','genutils','ipstruct','ultraTB','DPyGetOpt', |
|
53 | 53 | 'Itpl','hooks','ConfigLoader','OutputTrap','Release','Shell', |
|
54 | 54 | 'platutils','platutils_win32','platutils_posix','platutils_dummy', |
|
55 | 55 | 'ipapi','rlineimpl'] |
|
56 | 56 | |
|
57 | 57 | # Load __all__ in IPython namespace so that a simple 'import IPython' gives |
|
58 | 58 | # access to them via IPython.<name> |
|
59 | 59 | glob,loc = globals(),locals() |
|
60 | 60 | for name in __all__: |
|
61 | 61 | __import__(name,glob,loc,[]) |
|
62 | 62 | |
|
63 | 63 | # Release data |
|
64 | 64 | from IPython import Release # do it explicitly so pydoc can see it - pydoc bug |
|
65 | 65 | __author__ = '%s <%s>\n%s <%s>\n%s <%s>' % \ |
|
66 | 66 | ( Release.authors['Fernando'] + Release.authors['Janko'] + \ |
|
67 | 67 | Release.authors['Nathan'] ) |
|
68 | 68 | __license__ = Release.license |
|
69 | 69 | __version__ = Release.version |
|
70 | 70 | __revision__ = Release.revision |
|
71 | 71 | |
|
72 | 72 | # Namespace cleanup |
|
73 | 73 | del name,glob,loc |
@@ -1,570 +1,570 b'' | |||
|
1 | 1 | """Word completion for IPython. |
|
2 | 2 | |
|
3 | 3 | This module is a fork of the rlcompleter module in the Python standard |
|
4 | 4 | library. The original enhancements made to rlcompleter have been sent |
|
5 | 5 | upstream and were accepted as of Python 2.3, but we need a lot more |
|
6 | 6 | functionality specific to IPython, so this module will continue to live as an |
|
7 | 7 | IPython-specific utility. |
|
8 | 8 | |
|
9 | 9 | --------------------------------------------------------------------------- |
|
10 | 10 | Original rlcompleter documentation: |
|
11 | 11 | |
|
12 | 12 | This requires the latest extension to the readline module (the |
|
13 | 13 | completes keywords, built-ins and globals in __main__; when completing |
|
14 | 14 | NAME.NAME..., it evaluates (!) the expression up to the last dot and |
|
15 | 15 | completes its attributes. |
|
16 | 16 | |
|
17 | 17 | It's very cool to do "import string" type "string.", hit the |
|
18 | 18 | completion key (twice), and see the list of names defined by the |
|
19 | 19 | string module! |
|
20 | 20 | |
|
21 | 21 | Tip: to use the tab key as the completion key, call |
|
22 | 22 | |
|
23 | 23 | readline.parse_and_bind("tab: complete") |
|
24 | 24 | |
|
25 | 25 | Notes: |
|
26 | 26 | |
|
27 | 27 | - Exceptions raised by the completer function are *ignored* (and |
|
28 | 28 | generally cause the completion to fail). This is a feature -- since |
|
29 | 29 | readline sets the tty device in raw (or cbreak) mode, printing a |
|
30 | 30 | traceback wouldn't work well without some complicated hoopla to save, |
|
31 | 31 | reset and restore the tty state. |
|
32 | 32 | |
|
33 | 33 | - The evaluation of the NAME.NAME... form may cause arbitrary |
|
34 | 34 | application defined code to be executed if an object with a |
|
35 | 35 | __getattr__ hook is found. Since it is the responsibility of the |
|
36 | 36 | application (or the user) to enable this feature, I consider this an |
|
37 | 37 | acceptable risk. More complicated expressions (e.g. function calls or |
|
38 | 38 | indexing operations) are *not* evaluated. |
|
39 | 39 | |
|
40 | 40 | - GNU readline is also used by the built-in functions input() and |
|
41 | 41 | raw_input(), and thus these also benefit/suffer from the completer |
|
42 | 42 | features. Clearly an interactive application can benefit by |
|
43 | 43 | specifying its own completer function and using raw_input() for all |
|
44 | 44 | its input. |
|
45 | 45 | |
|
46 | 46 | - When the original stdin is not a tty device, GNU readline is never |
|
47 | 47 | used, and this module (and the readline module) are silently inactive. |
|
48 | 48 | |
|
49 | 49 | """ |
|
50 | 50 | |
|
51 | 51 | #***************************************************************************** |
|
52 | 52 | # |
|
53 | 53 | # Since this file is essentially a minimally modified copy of the rlcompleter |
|
54 | 54 | # module which is part of the standard Python distribution, I assume that the |
|
55 | 55 | # proper procedure is to maintain its copyright as belonging to the Python |
|
56 | 56 | # Software Foundation (in addition to my own, for all new code). |
|
57 | 57 | # |
|
58 | 58 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
59 | 59 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
60 | 60 | # |
|
61 | 61 | # Distributed under the terms of the BSD License. The full license is in |
|
62 | 62 | # the file COPYING, distributed as part of this software. |
|
63 | 63 | # |
|
64 | 64 | #***************************************************************************** |
|
65 | 65 | |
|
66 | 66 | import __builtin__ |
|
67 | 67 | import __main__ |
|
68 | 68 | import glob |
|
69 | 69 | import keyword |
|
70 | 70 | import os |
|
71 | 71 | import re |
|
72 | import shlex | |
|
72 | 73 | import sys |
|
73 | 74 | import IPython.rlineimpl as readline |
|
74 | 75 | |
|
75 | 76 | import types |
|
76 | 77 | |
|
77 | 78 | # Python 2.4 offers sets as a builtin |
|
78 | 79 | try: |
|
79 | 80 | set([1,2]) |
|
80 | 81 | except NameError: |
|
81 | 82 | from sets import Set as set |
|
82 | 83 | |
|
83 | ||
|
84 | from IPython.genutils import shlex_split,debugx | |
|
84 | from IPython.genutils import debugx | |
|
85 | 85 | |
|
86 | 86 | __all__ = ['Completer','IPCompleter'] |
|
87 | 87 | |
|
88 | 88 | def get_class_members(cls): |
|
89 | 89 | ret = dir(cls) |
|
90 | 90 | if hasattr(cls,'__bases__'): |
|
91 | 91 | for base in cls.__bases__: |
|
92 | 92 | ret.extend(get_class_members(base)) |
|
93 | 93 | return ret |
|
94 | 94 | |
|
95 | 95 | class Completer: |
|
96 | 96 | def __init__(self,namespace=None,global_namespace=None): |
|
97 | 97 | """Create a new completer for the command line. |
|
98 | 98 | |
|
99 | 99 | Completer([namespace,global_namespace]) -> completer instance. |
|
100 | 100 | |
|
101 | 101 | If unspecified, the default namespace where completions are performed |
|
102 | 102 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
103 | 103 | given as dictionaries. |
|
104 | 104 | |
|
105 | 105 | An optional second namespace can be given. This allows the completer |
|
106 | 106 | to handle cases where both the local and global scopes need to be |
|
107 | 107 | distinguished. |
|
108 | 108 | |
|
109 | 109 | Completer instances should be used as the completion mechanism of |
|
110 | 110 | readline via the set_completer() call: |
|
111 | 111 | |
|
112 | 112 | readline.set_completer(Completer(my_namespace).complete) |
|
113 | 113 | """ |
|
114 | 114 | |
|
115 | 115 | # some minimal strict typechecks. For some core data structures, I |
|
116 | 116 | # want actual basic python types, not just anything that looks like |
|
117 | 117 | # one. This is especially true for namespaces. |
|
118 | 118 | for ns in (namespace,global_namespace): |
|
119 | 119 | if ns is not None and type(ns) != types.DictType: |
|
120 | 120 | raise TypeError,'namespace must be a dictionary' |
|
121 | 121 | |
|
122 | 122 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
123 | 123 | # specific namespace or to use __main__.__dict__. This will allow us |
|
124 | 124 | # to bind to __main__.__dict__ at completion time, not now. |
|
125 | 125 | if namespace is None: |
|
126 | 126 | self.use_main_ns = 1 |
|
127 | 127 | else: |
|
128 | 128 | self.use_main_ns = 0 |
|
129 | 129 | self.namespace = namespace |
|
130 | 130 | |
|
131 | 131 | # The global namespace, if given, can be bound directly |
|
132 | 132 | if global_namespace is None: |
|
133 | 133 | self.global_namespace = {} |
|
134 | 134 | else: |
|
135 | 135 | self.global_namespace = global_namespace |
|
136 | 136 | |
|
137 | 137 | def complete(self, text, state): |
|
138 | 138 | """Return the next possible completion for 'text'. |
|
139 | 139 | |
|
140 | 140 | This is called successively with state == 0, 1, 2, ... until it |
|
141 | 141 | returns None. The completion should begin with 'text'. |
|
142 | 142 | |
|
143 | 143 | """ |
|
144 | 144 | if self.use_main_ns: |
|
145 | 145 | self.namespace = __main__.__dict__ |
|
146 | 146 | |
|
147 | 147 | if state == 0: |
|
148 | 148 | if "." in text: |
|
149 | 149 | self.matches = self.attr_matches(text) |
|
150 | 150 | else: |
|
151 | 151 | self.matches = self.global_matches(text) |
|
152 | 152 | try: |
|
153 | 153 | return self.matches[state] |
|
154 | 154 | except IndexError: |
|
155 | 155 | return None |
|
156 | 156 | |
|
157 | 157 | def global_matches(self, text): |
|
158 | 158 | """Compute matches when text is a simple name. |
|
159 | 159 | |
|
160 | 160 | Return a list of all keywords, built-in functions and names currently |
|
161 | 161 | defined in self.namespace or self.global_namespace that match. |
|
162 | 162 | |
|
163 | 163 | """ |
|
164 | 164 | matches = [] |
|
165 | 165 | match_append = matches.append |
|
166 | 166 | n = len(text) |
|
167 | 167 | for lst in [keyword.kwlist, |
|
168 | 168 | __builtin__.__dict__.keys(), |
|
169 | 169 | self.namespace.keys(), |
|
170 | 170 | self.global_namespace.keys()]: |
|
171 | 171 | for word in lst: |
|
172 | 172 | if word[:n] == text and word != "__builtins__": |
|
173 | 173 | match_append(word) |
|
174 | 174 | return matches |
|
175 | 175 | |
|
176 | 176 | def attr_matches(self, text): |
|
177 | 177 | """Compute matches when text contains a dot. |
|
178 | 178 | |
|
179 | 179 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
180 | 180 | evaluatable in self.namespace or self.global_namespace, it will be |
|
181 | 181 | evaluated and its attributes (as revealed by dir()) are used as |
|
182 | 182 | possible completions. (For class instances, class members are are |
|
183 | 183 | also considered.) |
|
184 | 184 | |
|
185 | 185 | WARNING: this can still invoke arbitrary C code, if an object |
|
186 | 186 | with a __getattr__ hook is evaluated. |
|
187 | 187 | |
|
188 | 188 | """ |
|
189 | 189 | import re |
|
190 | 190 | |
|
191 | 191 | # Another option, seems to work great. Catches things like ''.<tab> |
|
192 | 192 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
193 | 193 | |
|
194 | 194 | if not m: |
|
195 | 195 | return [] |
|
196 | 196 | |
|
197 | 197 | expr, attr = m.group(1, 3) |
|
198 | 198 | try: |
|
199 | 199 | object = eval(expr, self.namespace) |
|
200 | 200 | except: |
|
201 | 201 | object = eval(expr, self.global_namespace) |
|
202 | 202 | |
|
203 | 203 | # Start building the attribute list via dir(), and then complete it |
|
204 | 204 | # with a few extra special-purpose calls. |
|
205 | 205 | words = dir(object) |
|
206 | 206 | |
|
207 | 207 | if hasattr(object,'__class__'): |
|
208 | 208 | words.append('__class__') |
|
209 | 209 | words.extend(get_class_members(object.__class__)) |
|
210 | 210 | |
|
211 | 211 | # this is the 'dir' function for objects with Enthought's traits |
|
212 | 212 | if hasattr(object, 'trait_names'): |
|
213 | 213 | try: |
|
214 | 214 | words.extend(object.trait_names()) |
|
215 | 215 | # eliminate possible duplicates, as some traits may also |
|
216 | 216 | # appear as normal attributes in the dir() call. |
|
217 | 217 | words = set(words) |
|
218 | 218 | except TypeError: |
|
219 | 219 | # This will happen if `object` is a class and not an instance. |
|
220 | 220 | pass |
|
221 | 221 | |
|
222 | 222 | # filter out non-string attributes which may be stuffed by dir() calls |
|
223 | 223 | # and poor coding in third-party modules |
|
224 | 224 | words = [w for w in words |
|
225 | 225 | if isinstance(w, basestring) and w != "__builtins__"] |
|
226 | 226 | # Build match list to return |
|
227 | 227 | n = len(attr) |
|
228 | 228 | return ["%s.%s" % (expr, w) for w in words if w[:n] == attr ] |
|
229 | 229 | |
|
230 | 230 | class IPCompleter(Completer): |
|
231 | 231 | """Extension of the completer class with IPython-specific features""" |
|
232 | 232 | |
|
233 | 233 | def __init__(self,shell,namespace=None,global_namespace=None, |
|
234 | 234 | omit__names=0,alias_table=None): |
|
235 | 235 | """IPCompleter() -> completer |
|
236 | 236 | |
|
237 | 237 | Return a completer object suitable for use by the readline library |
|
238 | 238 | via readline.set_completer(). |
|
239 | 239 | |
|
240 | 240 | Inputs: |
|
241 | 241 | |
|
242 | 242 | - shell: a pointer to the ipython shell itself. This is needed |
|
243 | 243 | because this completer knows about magic functions, and those can |
|
244 | 244 | only be accessed via the ipython instance. |
|
245 | 245 | |
|
246 | 246 | - namespace: an optional dict where completions are performed. |
|
247 | 247 | |
|
248 | 248 | - global_namespace: secondary optional dict for completions, to |
|
249 | 249 | handle cases (such as IPython embedded inside functions) where |
|
250 | 250 | both Python scopes are visible. |
|
251 | 251 | |
|
252 | 252 | - The optional omit__names parameter sets the completer to omit the |
|
253 | 253 | 'magic' names (__magicname__) for python objects unless the text |
|
254 | 254 | to be completed explicitly starts with one or more underscores. |
|
255 | 255 | |
|
256 | 256 | - If alias_table is supplied, it should be a dictionary of aliases |
|
257 | 257 | to complete. """ |
|
258 | 258 | |
|
259 | 259 | Completer.__init__(self,namespace,global_namespace) |
|
260 | 260 | self.magic_prefix = shell.name+'.magic_' |
|
261 | 261 | self.magic_escape = shell.ESC_MAGIC |
|
262 | 262 | self.readline = readline |
|
263 | 263 | delims = self.readline.get_completer_delims() |
|
264 | 264 | delims = delims.replace(self.magic_escape,'') |
|
265 | 265 | self.readline.set_completer_delims(delims) |
|
266 | 266 | self.get_line_buffer = self.readline.get_line_buffer |
|
267 | 267 | self.omit__names = omit__names |
|
268 | 268 | self.merge_completions = shell.rc.readline_merge_completions |
|
269 | 269 | |
|
270 | 270 | if alias_table is None: |
|
271 | 271 | alias_table = {} |
|
272 | 272 | self.alias_table = alias_table |
|
273 | 273 | # Regexp to split filenames with spaces in them |
|
274 | 274 | self.space_name_re = re.compile(r'([^\\] )') |
|
275 | 275 | # Hold a local ref. to glob.glob for speed |
|
276 | 276 | self.glob = glob.glob |
|
277 | 277 | |
|
278 | 278 | # Determine if we are running on 'dumb' terminals, like (X)Emacs |
|
279 | 279 | # buffers, to avoid completion problems. |
|
280 | 280 | term = os.environ.get('TERM','xterm') |
|
281 | 281 | self.dumb_terminal = term in ['dumb','emacs'] |
|
282 | 282 | |
|
283 | 283 | # Special handling of backslashes needed in win32 platforms |
|
284 | 284 | if sys.platform == "win32": |
|
285 | 285 | self.clean_glob = self._clean_glob_win32 |
|
286 | 286 | else: |
|
287 | 287 | self.clean_glob = self._clean_glob |
|
288 | 288 | self.matchers = [self.python_matches, |
|
289 | 289 | self.file_matches, |
|
290 | 290 | self.alias_matches, |
|
291 | 291 | self.python_func_kw_matches] |
|
292 | 292 | |
|
293 | 293 | # Code contributed by Alex Schmolck, for ipython/emacs integration |
|
294 | 294 | def all_completions(self, text): |
|
295 | 295 | """Return all possible completions for the benefit of emacs.""" |
|
296 | 296 | |
|
297 | 297 | completions = [] |
|
298 | 298 | comp_append = completions.append |
|
299 | 299 | try: |
|
300 | 300 | for i in xrange(sys.maxint): |
|
301 | 301 | res = self.complete(text, i) |
|
302 | 302 | |
|
303 | 303 | if not res: break |
|
304 | 304 | |
|
305 | 305 | comp_append(res) |
|
306 | 306 | #XXX workaround for ``notDefined.<tab>`` |
|
307 | 307 | except NameError: |
|
308 | 308 | pass |
|
309 | 309 | return completions |
|
310 | 310 | # /end Alex Schmolck code. |
|
311 | 311 | |
|
312 | 312 | def _clean_glob(self,text): |
|
313 | 313 | return self.glob("%s*" % text) |
|
314 | 314 | |
|
315 | 315 | def _clean_glob_win32(self,text): |
|
316 | 316 | return [f.replace("\\","/") |
|
317 | 317 | for f in self.glob("%s*" % text)] |
|
318 | 318 | |
|
319 | 319 | def file_matches(self, text): |
|
320 | 320 | """Match filneames, expanding ~USER type strings. |
|
321 | 321 | |
|
322 | 322 | Most of the seemingly convoluted logic in this completer is an |
|
323 | 323 | attempt to handle filenames with spaces in them. And yet it's not |
|
324 | 324 | quite perfect, because Python's readline doesn't expose all of the |
|
325 | 325 | GNU readline details needed for this to be done correctly. |
|
326 | 326 | |
|
327 | 327 | For a filename with a space in it, the printed completions will be |
|
328 | 328 | only the parts after what's already been typed (instead of the |
|
329 | 329 | full completions, as is normally done). I don't think with the |
|
330 | 330 | current (as of Python 2.3) Python readline it's possible to do |
|
331 | 331 | better.""" |
|
332 | 332 | |
|
333 | 333 | #print 'Completer->file_matches: <%s>' % text # dbg |
|
334 | 334 | |
|
335 | 335 | # chars that require escaping with backslash - i.e. chars |
|
336 | 336 | # that readline treats incorrectly as delimiters, but we |
|
337 | 337 | # don't want to treat as delimiters in filename matching |
|
338 | 338 | # when escaped with backslash |
|
339 | 339 | |
|
340 | 340 | protectables = ' ()[]{}' |
|
341 | 341 | |
|
342 | 342 | def protect_filename(s): |
|
343 | 343 | return "".join([(ch in protectables and '\\' + ch or ch) |
|
344 | 344 | for ch in s]) |
|
345 | 345 | |
|
346 | 346 | lbuf = self.lbuf |
|
347 | 347 | open_quotes = 0 # track strings with open quotes |
|
348 | 348 | try: |
|
349 |
lsplit = shlex |
|
|
349 | lsplit = shlex.split(lbuf)[-1] | |
|
350 | 350 | except ValueError: |
|
351 | 351 | # typically an unmatched ", or backslash without escaped char. |
|
352 | 352 | if lbuf.count('"')==1: |
|
353 | 353 | open_quotes = 1 |
|
354 | 354 | lsplit = lbuf.split('"')[-1] |
|
355 | 355 | elif lbuf.count("'")==1: |
|
356 | 356 | open_quotes = 1 |
|
357 | 357 | lsplit = lbuf.split("'")[-1] |
|
358 | 358 | else: |
|
359 | 359 | return None |
|
360 | 360 | except IndexError: |
|
361 | 361 | # tab pressed on empty line |
|
362 | 362 | lsplit = "" |
|
363 | 363 | |
|
364 | 364 | if lsplit != protect_filename(lsplit): |
|
365 | 365 | # if protectables are found, do matching on the whole escaped |
|
366 | 366 | # name |
|
367 | 367 | has_protectables = 1 |
|
368 | 368 | text0,text = text,lsplit |
|
369 | 369 | else: |
|
370 | 370 | has_protectables = 0 |
|
371 | 371 | text = os.path.expanduser(text) |
|
372 | 372 | |
|
373 | 373 | if text == "": |
|
374 | 374 | return [protect_filename(f) for f in self.glob("*")] |
|
375 | 375 | |
|
376 | 376 | m0 = self.clean_glob(text.replace('\\','')) |
|
377 | 377 | if has_protectables: |
|
378 | 378 | # If we had protectables, we need to revert our changes to the |
|
379 | 379 | # beginning of filename so that we don't double-write the part |
|
380 | 380 | # of the filename we have so far |
|
381 | 381 | len_lsplit = len(lsplit) |
|
382 | 382 | matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0] |
|
383 | 383 | else: |
|
384 | 384 | if open_quotes: |
|
385 | 385 | # if we have a string with an open quote, we don't need to |
|
386 | 386 | # protect the names at all (and we _shouldn't_, as it |
|
387 | 387 | # would cause bugs when the filesystem call is made). |
|
388 | 388 | matches = m0 |
|
389 | 389 | else: |
|
390 | 390 | matches = [protect_filename(f) for f in m0] |
|
391 | 391 | if len(matches) == 1 and os.path.isdir(matches[0]): |
|
392 | 392 | # Takes care of links to directories also. Use '/' |
|
393 | 393 | # explicitly, even under Windows, so that name completions |
|
394 | 394 | # don't end up escaped. |
|
395 | 395 | matches[0] += '/' |
|
396 | 396 | return matches |
|
397 | 397 | |
|
398 | 398 | def alias_matches(self, text): |
|
399 | 399 | """Match internal system aliases""" |
|
400 | 400 | #print 'Completer->alias_matches:',text,'lb',self.lbuf # dbg |
|
401 | 401 | |
|
402 | 402 | # if we are not in the first 'item', alias matching |
|
403 | 403 | # doesn't make sense |
|
404 | 404 | if ' ' in self.lbuf: |
|
405 | 405 | return [] |
|
406 | 406 | text = os.path.expanduser(text) |
|
407 | 407 | aliases = self.alias_table.keys() |
|
408 | 408 | if text == "": |
|
409 | 409 | return aliases |
|
410 | 410 | else: |
|
411 | 411 | return [alias for alias in aliases if alias.startswith(text)] |
|
412 | 412 | |
|
413 | 413 | def python_matches(self,text): |
|
414 | 414 | """Match attributes or global python names""" |
|
415 | 415 | |
|
416 | 416 | #print 'Completer->python_matches, txt=<%s>' % text # dbg |
|
417 | 417 | if "." in text: |
|
418 | 418 | try: |
|
419 | 419 | matches = self.attr_matches(text) |
|
420 | 420 | if text.endswith('.') and self.omit__names: |
|
421 | 421 | if self.omit__names == 1: |
|
422 | 422 | # true if txt is _not_ a __ name, false otherwise: |
|
423 | 423 | no__name = (lambda txt: |
|
424 | 424 | re.match(r'.*\.__.*?__',txt) is None) |
|
425 | 425 | else: |
|
426 | 426 | # true if txt is _not_ a _ name, false otherwise: |
|
427 | 427 | no__name = (lambda txt: |
|
428 | 428 | re.match(r'.*\._.*?',txt) is None) |
|
429 | 429 | matches = filter(no__name, matches) |
|
430 | 430 | except NameError: |
|
431 | 431 | # catches <undefined attributes>.<tab> |
|
432 | 432 | matches = [] |
|
433 | 433 | else: |
|
434 | 434 | matches = self.global_matches(text) |
|
435 | 435 | # this is so completion finds magics when automagic is on: |
|
436 | 436 | if (matches == [] and |
|
437 | 437 | not text.startswith(os.sep) and |
|
438 | 438 | not ' ' in self.lbuf): |
|
439 | 439 | matches = self.attr_matches(self.magic_prefix+text) |
|
440 | 440 | return matches |
|
441 | 441 | |
|
442 | 442 | def _default_arguments(self, obj): |
|
443 | 443 | """Return the list of default arguments of obj if it is callable, |
|
444 | 444 | or empty list otherwise.""" |
|
445 | 445 | |
|
446 | 446 | if not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
447 | 447 | # for classes, check for __init__,__new__ |
|
448 | 448 | if inspect.isclass(obj): |
|
449 | 449 | obj = (getattr(obj,'__init__',None) or |
|
450 | 450 | getattr(obj,'__new__',None)) |
|
451 | 451 | # for all others, check if they are __call__able |
|
452 | 452 | elif hasattr(obj, '__call__'): |
|
453 | 453 | obj = obj.__call__ |
|
454 | 454 | # XXX: is there a way to handle the builtins ? |
|
455 | 455 | try: |
|
456 | 456 | args,_,_1,defaults = inspect.getargspec(obj) |
|
457 | 457 | if defaults: |
|
458 | 458 | return args[-len(defaults):] |
|
459 | 459 | except TypeError: pass |
|
460 | 460 | return [] |
|
461 | 461 | |
|
462 | 462 | def python_func_kw_matches(self,text): |
|
463 | 463 | """Match named parameters (kwargs) of the last open function""" |
|
464 | 464 | |
|
465 | 465 | if "." in text: # a parameter cannot be dotted |
|
466 | 466 | return [] |
|
467 | 467 | try: regexp = self.__funcParamsRegex |
|
468 | 468 | except AttributeError: |
|
469 | 469 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
470 | 470 | '.*?' | # single quoted strings or |
|
471 | 471 | ".*?" | # double quoted strings or |
|
472 | 472 | \w+ | # identifier |
|
473 | 473 | \S # other characters |
|
474 | 474 | ''', re.VERBOSE | re.DOTALL) |
|
475 | 475 | # 1. find the nearest identifier that comes before an unclosed |
|
476 | 476 | # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo" |
|
477 | 477 | tokens = regexp.findall(self.get_line_buffer()) |
|
478 | 478 | tokens.reverse() |
|
479 | 479 | iterTokens = iter(tokens); openPar = 0 |
|
480 | 480 | for token in iterTokens: |
|
481 | 481 | if token == ')': |
|
482 | 482 | openPar -= 1 |
|
483 | 483 | elif token == '(': |
|
484 | 484 | openPar += 1 |
|
485 | 485 | if openPar > 0: |
|
486 | 486 | # found the last unclosed parenthesis |
|
487 | 487 | break |
|
488 | 488 | else: |
|
489 | 489 | return [] |
|
490 | 490 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) |
|
491 | 491 | ids = [] |
|
492 | 492 | isId = re.compile(r'\w+$').match |
|
493 | 493 | while True: |
|
494 | 494 | try: |
|
495 | 495 | ids.append(iterTokens.next()) |
|
496 | 496 | if not isId(ids[-1]): |
|
497 | 497 | ids.pop(); break |
|
498 | 498 | if not iterTokens.next() == '.': |
|
499 | 499 | break |
|
500 | 500 | except StopIteration: |
|
501 | 501 | break |
|
502 | 502 | # lookup the candidate callable matches either using global_matches |
|
503 | 503 | # or attr_matches for dotted names |
|
504 | 504 | if len(ids) == 1: |
|
505 | 505 | callableMatches = self.global_matches(ids[0]) |
|
506 | 506 | else: |
|
507 | 507 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
508 | 508 | argMatches = [] |
|
509 | 509 | for callableMatch in callableMatches: |
|
510 | 510 | try: namedArgs = self._default_arguments(eval(callableMatch, |
|
511 | 511 | self.namespace)) |
|
512 | 512 | except: continue |
|
513 | 513 | for namedArg in namedArgs: |
|
514 | 514 | if namedArg.startswith(text): |
|
515 | 515 | argMatches.append("%s=" %namedArg) |
|
516 | 516 | return argMatches |
|
517 | 517 | |
|
518 | 518 | def complete(self, text, state): |
|
519 | 519 | """Return the next possible completion for 'text'. |
|
520 | 520 | |
|
521 | 521 | This is called successively with state == 0, 1, 2, ... until it |
|
522 | 522 | returns None. The completion should begin with 'text'. """ |
|
523 | 523 | |
|
524 | 524 | #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg |
|
525 | 525 | |
|
526 | 526 | # if there is only a tab on a line with only whitespace, instead |
|
527 | 527 | # of the mostly useless 'do you want to see all million |
|
528 | 528 | # completions' message, just do the right thing and give the user |
|
529 | 529 | # his tab! Incidentally, this enables pasting of tabbed text from |
|
530 | 530 | # an editor (as long as autoindent is off). |
|
531 | 531 | |
|
532 | 532 | # don't apply this on 'dumb' terminals, such as emacs buffers, so we |
|
533 | 533 | # don't interfere with their own tab-completion mechanism. |
|
534 | 534 | self.lbuf = self.get_line_buffer()[:self.readline.get_endidx()] |
|
535 | 535 | if not (self.dumb_terminal or self.get_line_buffer().strip()): |
|
536 | 536 | self.readline.insert_text('\t') |
|
537 | 537 | return None |
|
538 | 538 | |
|
539 | 539 | magic_escape = self.magic_escape |
|
540 | 540 | magic_prefix = self.magic_prefix |
|
541 | 541 | |
|
542 | 542 | try: |
|
543 | 543 | if text.startswith(magic_escape): |
|
544 | 544 | text = text.replace(magic_escape,magic_prefix) |
|
545 | 545 | elif text.startswith('~'): |
|
546 | 546 | text = os.path.expanduser(text) |
|
547 | 547 | if state == 0: |
|
548 | 548 | # Extend the list of completions with the results of each |
|
549 | 549 | # matcher, so we return results to the user from all |
|
550 | 550 | # namespaces. |
|
551 | 551 | if self.merge_completions: |
|
552 | 552 | self.matches = [] |
|
553 | 553 | for matcher in self.matchers: |
|
554 | 554 | self.matches.extend(matcher(text)) |
|
555 | 555 | else: |
|
556 | 556 | for matcher in self.matchers: |
|
557 | 557 | self.matches = matcher(text) |
|
558 | 558 | if self.matches: |
|
559 | 559 | break |
|
560 | 560 | |
|
561 | 561 | try: |
|
562 | 562 | return self.matches[state].replace(magic_prefix,magic_escape) |
|
563 | 563 | except IndexError: |
|
564 | 564 | return None |
|
565 | 565 | except: |
|
566 | 566 | #from IPython.ultraTB import AutoFormattedTB; # dbg |
|
567 | 567 | #tb=AutoFormattedTB('Verbose');tb() #dbg |
|
568 | 568 | |
|
569 | 569 | # If completion fails, don't annoy the user. |
|
570 | 570 | return None |
@@ -1,418 +1,419 b'' | |||
|
1 | 1 | """Module for interactive demos using IPython. |
|
2 | 2 | |
|
3 | 3 | This module implements a few classes for running Python scripts interactively |
|
4 | 4 | in IPython for demonstrations. With very simple markup (a few tags in |
|
5 | 5 | comments), you can control points where the script stops executing and returns |
|
6 | 6 | control to IPython. |
|
7 | 7 | |
|
8 | 8 | The classes are (see their docstrings for further details): |
|
9 | 9 | |
|
10 | 10 | - Demo: pure python demos |
|
11 | 11 | |
|
12 | 12 | - IPythonDemo: demos with input to be processed by IPython as if it had been |
|
13 | 13 | typed interactively (so magics work, as well as any other special syntax you |
|
14 | 14 | may have added via input prefilters). |
|
15 | 15 | |
|
16 | 16 | - LineDemo: single-line version of the Demo class. These demos are executed |
|
17 | 17 | one line at a time, and require no markup. |
|
18 | 18 | |
|
19 | 19 | - IPythonLineDemo: IPython version of the LineDemo class (the demo is |
|
20 | 20 | executed a line at a time, but processed via IPython). |
|
21 | 21 | |
|
22 | 22 | |
|
23 | 23 | The file is run in its own empty namespace (though you can pass it a string of |
|
24 | 24 | arguments as if in a command line environment, and it will see those as |
|
25 | 25 | sys.argv). But at each stop, the global IPython namespace is updated with the |
|
26 | 26 | current internal demo namespace, so you can work interactively with the data |
|
27 | 27 | accumulated so far. |
|
28 | 28 | |
|
29 | 29 | By default, each block of code is printed (with syntax highlighting) before |
|
30 | 30 | executing it and you have to confirm execution. This is intended to show the |
|
31 | 31 | code to an audience first so you can discuss it, and only proceed with |
|
32 | 32 | execution once you agree. There are a few tags which allow you to modify this |
|
33 | 33 | behavior. |
|
34 | 34 | |
|
35 | 35 | The supported tags are: |
|
36 | 36 | |
|
37 | 37 | # <demo> --- stop --- |
|
38 | 38 | |
|
39 | 39 | Defines block boundaries, the points where IPython stops execution of the |
|
40 | 40 | file and returns to the interactive prompt. |
|
41 | 41 | |
|
42 | 42 | # <demo> silent |
|
43 | 43 | |
|
44 | 44 | Make a block execute silently (and hence automatically). Typically used in |
|
45 | 45 | cases where you have some boilerplate or initialization code which you need |
|
46 | 46 | executed but do not want to be seen in the demo. |
|
47 | 47 | |
|
48 | 48 | # <demo> auto |
|
49 | 49 | |
|
50 | 50 | Make a block execute automatically, but still being printed. Useful for |
|
51 | 51 | simple code which does not warrant discussion, since it avoids the extra |
|
52 | 52 | manual confirmation. |
|
53 | 53 | |
|
54 | 54 | # <demo> auto_all |
|
55 | 55 | |
|
56 | 56 | This tag can _only_ be in the first block, and if given it overrides the |
|
57 | 57 | individual auto tags to make the whole demo fully automatic (no block asks |
|
58 | 58 | for confirmation). It can also be given at creation time (or the attribute |
|
59 | 59 | set later) to override what's in the file. |
|
60 | 60 | |
|
61 | 61 | While _any_ python file can be run as a Demo instance, if there are no stop |
|
62 | 62 | tags the whole file will run in a single block (no different that calling |
|
63 | 63 | first %pycat and then %run). The minimal markup to make this useful is to |
|
64 | 64 | place a set of stop tags; the other tags are only there to let you fine-tune |
|
65 | 65 | the execution. |
|
66 | 66 | |
|
67 | 67 | This is probably best explained with the simple example file below. You can |
|
68 | 68 | copy this into a file named ex_demo.py, and try running it via: |
|
69 | 69 | |
|
70 | 70 | from IPython.demo import Demo |
|
71 | 71 | d = Demo('ex_demo.py') |
|
72 | 72 | d() <--- Call the d object (omit the parens if you have autocall set to 2). |
|
73 | 73 | |
|
74 | 74 | Each time you call the demo object, it runs the next block. The demo object |
|
75 | 75 | has a few useful methods for navigation, like again(), edit(), jump(), seek() |
|
76 | 76 | and back(). It can be reset for a new run via reset() or reloaded from disk |
|
77 | 77 | (in case you've edited the source) via reload(). See their docstrings below. |
|
78 | 78 | |
|
79 | 79 | #################### EXAMPLE DEMO <ex_demo.py> ############################### |
|
80 | 80 | '''A simple interactive demo to illustrate the use of IPython's Demo class.''' |
|
81 | 81 | |
|
82 | 82 | print 'Hello, welcome to an interactive IPython demo.' |
|
83 | 83 | |
|
84 | 84 | # The mark below defines a block boundary, which is a point where IPython will |
|
85 | 85 | # stop execution and return to the interactive prompt. |
|
86 | 86 | # Note that in actual interactive execution, |
|
87 | 87 | # <demo> --- stop --- |
|
88 | 88 | |
|
89 | 89 | x = 1 |
|
90 | 90 | y = 2 |
|
91 | 91 | |
|
92 | 92 | # <demo> --- stop --- |
|
93 | 93 | |
|
94 | 94 | # the mark below makes this block as silent |
|
95 | 95 | # <demo> silent |
|
96 | 96 | |
|
97 | 97 | print 'This is a silent block, which gets executed but not printed.' |
|
98 | 98 | |
|
99 | 99 | # <demo> --- stop --- |
|
100 | 100 | # <demo> auto |
|
101 | 101 | print 'This is an automatic block.' |
|
102 | 102 | print 'It is executed without asking for confirmation, but printed.' |
|
103 | 103 | z = x+y |
|
104 | 104 | |
|
105 | 105 | print 'z=',x |
|
106 | 106 | |
|
107 | 107 | # <demo> --- stop --- |
|
108 | 108 | # This is just another normal block. |
|
109 | 109 | print 'z is now:', z |
|
110 | 110 | |
|
111 | 111 | print 'bye!' |
|
112 | 112 | ################### END EXAMPLE DEMO <ex_demo.py> ############################ |
|
113 | 113 | """ |
|
114 | 114 | #***************************************************************************** |
|
115 | 115 | # Copyright (C) 2005-2006 Fernando Perez. <Fernando.Perez@colorado.edu> |
|
116 | 116 | # |
|
117 | 117 | # Distributed under the terms of the BSD License. The full license is in |
|
118 | 118 | # the file COPYING, distributed as part of this software. |
|
119 | 119 | # |
|
120 | 120 | #***************************************************************************** |
|
121 | 121 | |
|
122 | 122 | import exceptions |
|
123 | 123 | import os |
|
124 | 124 | import re |
|
125 | import shlex | |
|
125 | 126 | import sys |
|
126 | 127 | |
|
127 | 128 | from IPython.PyColorize import Parser |
|
128 |
from IPython.genutils import marquee, |
|
|
129 | from IPython.genutils import marquee, file_read, file_readlines | |
|
129 | 130 | |
|
130 | 131 | __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError'] |
|
131 | 132 | |
|
132 | 133 | class DemoError(exceptions.Exception): pass |
|
133 | 134 | |
|
134 | 135 | def re_mark(mark): |
|
135 | 136 | return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE) |
|
136 | 137 | |
|
137 | 138 | class Demo: |
|
138 | 139 | |
|
139 | 140 | re_stop = re_mark('---\s?stop\s?---') |
|
140 | 141 | re_silent = re_mark('silent') |
|
141 | 142 | re_auto = re_mark('auto') |
|
142 | 143 | re_auto_all = re_mark('auto_all') |
|
143 | 144 | |
|
144 | 145 | def __init__(self,fname,arg_str='',auto_all=None): |
|
145 | 146 | """Make a new demo object. To run the demo, simply call the object. |
|
146 | 147 | |
|
147 | 148 | See the module docstring for full details and an example (you can use |
|
148 | 149 | IPython.Demo? in IPython to see it). |
|
149 | 150 | |
|
150 | 151 | Inputs: |
|
151 | 152 | |
|
152 | 153 | - fname = filename. |
|
153 | 154 | |
|
154 | 155 | Optional inputs: |
|
155 | 156 | |
|
156 | 157 | - arg_str(''): a string of arguments, internally converted to a list |
|
157 | 158 | just like sys.argv, so the demo script can see a similar |
|
158 | 159 | environment. |
|
159 | 160 | |
|
160 | 161 | - auto_all(None): global flag to run all blocks automatically without |
|
161 | 162 | confirmation. This attribute overrides the block-level tags and |
|
162 | 163 | applies to the whole demo. It is an attribute of the object, and |
|
163 | 164 | can be changed at runtime simply by reassigning it to a boolean |
|
164 | 165 | value. |
|
165 | 166 | """ |
|
166 | 167 | |
|
167 | 168 | self.fname = fname |
|
168 |
self.sys_argv = [fname] + shlex |
|
|
169 | self.sys_argv = [fname] + shlex.split(arg_str) | |
|
169 | 170 | self.auto_all = auto_all |
|
170 | 171 | |
|
171 | 172 | # get a few things from ipython. While it's a bit ugly design-wise, |
|
172 | 173 | # it ensures that things like color scheme and the like are always in |
|
173 | 174 | # sync with the ipython mode being used. This class is only meant to |
|
174 | 175 | # be used inside ipython anyways, so it's OK. |
|
175 | 176 | self.ip_ns = __IPYTHON__.user_ns |
|
176 | 177 | self.ip_colorize = __IPYTHON__.pycolorize |
|
177 | 178 | self.ip_showtb = __IPYTHON__.showtraceback |
|
178 | 179 | self.ip_runlines = __IPYTHON__.runlines |
|
179 | 180 | self.shell = __IPYTHON__ |
|
180 | 181 | |
|
181 | 182 | # load user data and initialize data structures |
|
182 | 183 | self.reload() |
|
183 | 184 | |
|
184 | 185 | def reload(self): |
|
185 | 186 | """Reload source from disk and initialize state.""" |
|
186 | 187 | # read data and parse into blocks |
|
187 | 188 | self.src = file_read(self.fname) |
|
188 | 189 | src_b = [b.strip() for b in self.re_stop.split(self.src) if b] |
|
189 | 190 | self._silent = [bool(self.re_silent.findall(b)) for b in src_b] |
|
190 | 191 | self._auto = [bool(self.re_auto.findall(b)) for b in src_b] |
|
191 | 192 | |
|
192 | 193 | # if auto_all is not given (def. None), we read it from the file |
|
193 | 194 | if self.auto_all is None: |
|
194 | 195 | self.auto_all = bool(self.re_auto_all.findall(src_b[0])) |
|
195 | 196 | else: |
|
196 | 197 | self.auto_all = bool(self.auto_all) |
|
197 | 198 | |
|
198 | 199 | # Clean the sources from all markup so it doesn't get displayed when |
|
199 | 200 | # running the demo |
|
200 | 201 | src_blocks = [] |
|
201 | 202 | auto_strip = lambda s: self.re_auto.sub('',s) |
|
202 | 203 | for i,b in enumerate(src_b): |
|
203 | 204 | if self._auto[i]: |
|
204 | 205 | src_blocks.append(auto_strip(b)) |
|
205 | 206 | else: |
|
206 | 207 | src_blocks.append(b) |
|
207 | 208 | # remove the auto_all marker |
|
208 | 209 | src_blocks[0] = self.re_auto_all.sub('',src_blocks[0]) |
|
209 | 210 | |
|
210 | 211 | self.nblocks = len(src_blocks) |
|
211 | 212 | self.src_blocks = src_blocks |
|
212 | 213 | |
|
213 | 214 | # also build syntax-highlighted source |
|
214 | 215 | self.src_blocks_colored = map(self.ip_colorize,self.src_blocks) |
|
215 | 216 | |
|
216 | 217 | # ensure clean namespace and seek offset |
|
217 | 218 | self.reset() |
|
218 | 219 | |
|
219 | 220 | def reset(self): |
|
220 | 221 | """Reset the namespace and seek pointer to restart the demo""" |
|
221 | 222 | self.user_ns = {} |
|
222 | 223 | self.finished = False |
|
223 | 224 | self.block_index = 0 |
|
224 | 225 | |
|
225 | 226 | def _validate_index(self,index): |
|
226 | 227 | if index<0 or index>=self.nblocks: |
|
227 | 228 | raise ValueError('invalid block index %s' % index) |
|
228 | 229 | |
|
229 | 230 | def _get_index(self,index): |
|
230 | 231 | """Get the current block index, validating and checking status. |
|
231 | 232 | |
|
232 | 233 | Returns None if the demo is finished""" |
|
233 | 234 | |
|
234 | 235 | if index is None: |
|
235 | 236 | if self.finished: |
|
236 | 237 | print 'Demo finished. Use reset() if you want to rerun it.' |
|
237 | 238 | return None |
|
238 | 239 | index = self.block_index |
|
239 | 240 | else: |
|
240 | 241 | self._validate_index(index) |
|
241 | 242 | return index |
|
242 | 243 | |
|
243 | 244 | def seek(self,index): |
|
244 | 245 | """Move the current seek pointer to the given block""" |
|
245 | 246 | self._validate_index(index) |
|
246 | 247 | self.block_index = index |
|
247 | 248 | self.finished = False |
|
248 | 249 | |
|
249 | 250 | def back(self,num=1): |
|
250 | 251 | """Move the seek pointer back num blocks (default is 1).""" |
|
251 | 252 | self.seek(self.block_index-num) |
|
252 | 253 | |
|
253 | 254 | def jump(self,num): |
|
254 | 255 | """Jump a given number of blocks relative to the current one.""" |
|
255 | 256 | self.seek(self.block_index+num) |
|
256 | 257 | |
|
257 | 258 | def again(self): |
|
258 | 259 | """Move the seek pointer back one block and re-execute.""" |
|
259 | 260 | self.back(1) |
|
260 | 261 | self() |
|
261 | 262 | |
|
262 | 263 | def edit(self,index=None): |
|
263 | 264 | """Edit a block. |
|
264 | 265 | |
|
265 | 266 | If no number is given, use the last block executed. |
|
266 | 267 | |
|
267 | 268 | This edits the in-memory copy of the demo, it does NOT modify the |
|
268 | 269 | original source file. If you want to do that, simply open the file in |
|
269 | 270 | an editor and use reload() when you make changes to the file. This |
|
270 | 271 | method is meant to let you change a block during a demonstration for |
|
271 | 272 | explanatory purposes, without damaging your original script.""" |
|
272 | 273 | |
|
273 | 274 | index = self._get_index(index) |
|
274 | 275 | if index is None: |
|
275 | 276 | return |
|
276 | 277 | # decrease the index by one (unless we're at the very beginning), so |
|
277 | 278 | # that the default demo.edit() call opens up the sblock we've last run |
|
278 | 279 | if index>0: |
|
279 | 280 | index -= 1 |
|
280 | 281 | |
|
281 | 282 | filename = self.shell.mktempfile(self.src_blocks[index]) |
|
282 | 283 | self.shell.hooks.editor(filename,1) |
|
283 | 284 | new_block = file_read(filename) |
|
284 | 285 | # update the source and colored block |
|
285 | 286 | self.src_blocks[index] = new_block |
|
286 | 287 | self.src_blocks_colored[index] = self.ip_colorize(new_block) |
|
287 | 288 | self.block_index = index |
|
288 | 289 | # call to run with the newly edited index |
|
289 | 290 | self() |
|
290 | 291 | |
|
291 | 292 | def show(self,index=None): |
|
292 | 293 | """Show a single block on screen""" |
|
293 | 294 | |
|
294 | 295 | index = self._get_index(index) |
|
295 | 296 | if index is None: |
|
296 | 297 | return |
|
297 | 298 | |
|
298 | 299 | print marquee('<%s> block # %s (%s remaining)' % |
|
299 | 300 | (self.fname,index,self.nblocks-index-1)) |
|
300 | 301 | print self.src_blocks_colored[index], |
|
301 | 302 | sys.stdout.flush() |
|
302 | 303 | |
|
303 | 304 | def show_all(self): |
|
304 | 305 | """Show entire demo on screen, block by block""" |
|
305 | 306 | |
|
306 | 307 | fname = self.fname |
|
307 | 308 | nblocks = self.nblocks |
|
308 | 309 | silent = self._silent |
|
309 | 310 | for index,block in enumerate(self.src_blocks_colored): |
|
310 | 311 | if silent[index]: |
|
311 | 312 | print marquee('<%s> SILENT block # %s (%s remaining)' % |
|
312 | 313 | (fname,index,nblocks-index-1)) |
|
313 | 314 | else: |
|
314 | 315 | print marquee('<%s> block # %s (%s remaining)' % |
|
315 | 316 | (fname,index,nblocks-index-1)) |
|
316 | 317 | print block, |
|
317 | 318 | sys.stdout.flush() |
|
318 | 319 | |
|
319 | 320 | def runlines(self,source): |
|
320 | 321 | """Execute a string with one or more lines of code""" |
|
321 | 322 | |
|
322 | 323 | exec source in self.user_ns |
|
323 | 324 | |
|
324 | 325 | def __call__(self,index=None): |
|
325 | 326 | """run a block of the demo. |
|
326 | 327 | |
|
327 | 328 | If index is given, it should be an integer >=1 and <= nblocks. This |
|
328 | 329 | means that the calling convention is one off from typical Python |
|
329 | 330 | lists. The reason for the inconsistency is that the demo always |
|
330 | 331 | prints 'Block n/N, and N is the total, so it would be very odd to use |
|
331 | 332 | zero-indexing here.""" |
|
332 | 333 | |
|
333 | 334 | index = self._get_index(index) |
|
334 | 335 | if index is None: |
|
335 | 336 | return |
|
336 | 337 | try: |
|
337 | 338 | next_block = self.src_blocks[index] |
|
338 | 339 | self.block_index += 1 |
|
339 | 340 | if self._silent[index]: |
|
340 | 341 | print marquee('Executing silent block # %s (%s remaining)' % |
|
341 | 342 | (index,self.nblocks-index-1)) |
|
342 | 343 | else: |
|
343 | 344 | self.show(index) |
|
344 | 345 | if self.auto_all or self._auto[index]: |
|
345 | 346 | print marquee('output') |
|
346 | 347 | else: |
|
347 | 348 | print marquee('Press <q> to quit, <Enter> to execute...'), |
|
348 | 349 | ans = raw_input().strip() |
|
349 | 350 | if ans: |
|
350 | 351 | print marquee('Block NOT executed') |
|
351 | 352 | return |
|
352 | 353 | try: |
|
353 | 354 | save_argv = sys.argv |
|
354 | 355 | sys.argv = self.sys_argv |
|
355 | 356 | self.runlines(next_block) |
|
356 | 357 | finally: |
|
357 | 358 | sys.argv = save_argv |
|
358 | 359 | |
|
359 | 360 | except: |
|
360 | 361 | self.ip_showtb(filename=self.fname) |
|
361 | 362 | else: |
|
362 | 363 | self.ip_ns.update(self.user_ns) |
|
363 | 364 | |
|
364 | 365 | if self.block_index == self.nblocks: |
|
365 | 366 | |
|
366 | 367 | print marquee(' END OF DEMO ') |
|
367 | 368 | print marquee('Use reset() if you want to rerun it.') |
|
368 | 369 | self.finished = True |
|
369 | 370 | |
|
370 | 371 | class IPythonDemo(Demo): |
|
371 | 372 | """Class for interactive demos with IPython's input processing applied. |
|
372 | 373 | |
|
373 | 374 | This subclasses Demo, but instead of executing each block by the Python |
|
374 | 375 | interpreter (via exec), it actually calls IPython on it, so that any input |
|
375 | 376 | filters which may be in place are applied to the input block. |
|
376 | 377 | |
|
377 | 378 | If you have an interactive environment which exposes special input |
|
378 | 379 | processing, you can use this class instead to write demo scripts which |
|
379 | 380 | operate exactly as if you had typed them interactively. The default Demo |
|
380 | 381 | class requires the input to be valid, pure Python code. |
|
381 | 382 | """ |
|
382 | 383 | |
|
383 | 384 | def runlines(self,source): |
|
384 | 385 | """Execute a string with one or more lines of code""" |
|
385 | 386 | |
|
386 | 387 | self.runlines(source) |
|
387 | 388 | |
|
388 | 389 | class LineDemo(Demo): |
|
389 | 390 | """Demo where each line is executed as a separate block. |
|
390 | 391 | |
|
391 | 392 | The input script should be valid Python code. |
|
392 | 393 | |
|
393 | 394 | This class doesn't require any markup at all, and it's meant for simple |
|
394 | 395 | scripts (with no nesting or any kind of indentation) which consist of |
|
395 | 396 | multiple lines of input to be executed, one at a time, as if they had been |
|
396 | 397 | typed in the interactive prompt.""" |
|
397 | 398 | |
|
398 | 399 | def reload(self): |
|
399 | 400 | """Reload source from disk and initialize state.""" |
|
400 | 401 | # read data and parse into blocks |
|
401 | 402 | src_b = [l for l in file_readlines(self.fname) if l.strip()] |
|
402 | 403 | nblocks = len(src_b) |
|
403 | 404 | self.src = os.linesep.join(file_readlines(self.fname)) |
|
404 | 405 | self._silent = [False]*nblocks |
|
405 | 406 | self._auto = [True]*nblocks |
|
406 | 407 | self.auto_all = True |
|
407 | 408 | self.nblocks = nblocks |
|
408 | 409 | self.src_blocks = src_b |
|
409 | 410 | |
|
410 | 411 | # also build syntax-highlighted source |
|
411 | 412 | self.src_blocks_colored = map(self.ip_colorize,self.src_blocks) |
|
412 | 413 | |
|
413 | 414 | # ensure clean namespace and seek offset |
|
414 | 415 | self.reset() |
|
415 | 416 | |
|
416 | 417 | class IPythonLineDemo(IPythonDemo,LineDemo): |
|
417 | 418 | """Variant of the LineDemo class whose input is processed by IPython.""" |
|
418 | 419 | pass |
@@ -1,1776 +1,1703 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | General purpose utilities. |
|
4 | 4 | |
|
5 | 5 | This is a grab-bag of stuff I find useful in most programs I write. Some of |
|
6 | 6 | these things are also convenient when working at the command line. |
|
7 | 7 | |
|
8 |
$Id: genutils.py 1 |
|
|
8 | $Id: genutils.py 1314 2006-05-19 18:24:14Z fperez $""" | |
|
9 | 9 | |
|
10 | 10 | #***************************************************************************** |
|
11 | 11 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
12 | 12 | # |
|
13 | 13 | # Distributed under the terms of the BSD License. The full license is in |
|
14 | 14 | # the file COPYING, distributed as part of this software. |
|
15 | 15 | #***************************************************************************** |
|
16 | 16 | |
|
17 | from __future__ import generators # 2.2 compatibility | |
|
18 | ||
|
19 | 17 | from IPython import Release |
|
20 | 18 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
21 | 19 | __license__ = Release.license |
|
22 | 20 | |
|
23 | 21 | #**************************************************************************** |
|
24 | 22 | # required modules from the Python standard library |
|
25 | 23 | import __main__ |
|
26 | 24 | import commands |
|
27 | 25 | import os |
|
28 | 26 | import re |
|
29 | import shlex | |
|
30 | 27 | import shutil |
|
31 | 28 | import sys |
|
32 | 29 | import tempfile |
|
33 | 30 | import time |
|
34 | 31 | import types |
|
35 | 32 | |
|
36 | 33 | # Other IPython utilities |
|
37 | 34 | from IPython.Itpl import Itpl,itpl,printpl |
|
38 | 35 | from IPython import DPyGetOpt |
|
39 | 36 | from path import path |
|
40 | 37 | if os.name == "nt": |
|
41 | 38 | from IPython.winconsole import get_console_size |
|
42 | 39 | |
|
43 | # Build objects which appeared in Python 2.3 for 2.2, to make ipython | |
|
44 | # 2.2-friendly | |
|
45 | try: | |
|
46 | basestring | |
|
47 | except NameError: | |
|
48 | import types | |
|
49 | basestring = (types.StringType, types.UnicodeType) | |
|
50 | True = 1==1 | |
|
51 | False = 1==0 | |
|
52 | ||
|
53 | def enumerate(obj): | |
|
54 | i = -1 | |
|
55 | for item in obj: | |
|
56 | i += 1 | |
|
57 | yield i, item | |
|
58 | ||
|
59 | # add these to the builtin namespace, so that all modules find them | |
|
60 | import __builtin__ | |
|
61 | __builtin__.basestring = basestring | |
|
62 | __builtin__.True = True | |
|
63 | __builtin__.False = False | |
|
64 | __builtin__.enumerate = enumerate | |
|
65 | ||
|
66 | # Try to use shlex.split for converting an input string into a sys.argv-type | |
|
67 | # list. This appeared in Python 2.3, so here's a quick backport for 2.2. | |
|
68 | try: | |
|
69 | shlex_split = shlex.split | |
|
70 | except AttributeError: | |
|
71 | _quotesre = re.compile(r'[\'"](.*)[\'"]') | |
|
72 | _wordchars = ('abcdfeghijklmnopqrstuvwxyz' | |
|
73 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.~*?' | |
|
74 | 'ßà áâãäåæçèéêëìÃîïðñòóôõöøùúûüýþÿ' | |
|
75 | '�����ÅÆÇ�ÉÊ�Ì�Î��ÑÒÓÔÕÖ�ÙÚ�Ü�Þ%s' | |
|
76 | % os.sep) | |
|
77 | ||
|
78 | def shlex_split(s): | |
|
79 | """Simplified backport to Python 2.2 of shlex.split(). | |
|
80 | ||
|
81 | This is a quick and dirty hack, since the shlex module under 2.2 lacks | |
|
82 | several of the features needed to really match the functionality of | |
|
83 | shlex.split() in 2.3.""" | |
|
84 | ||
|
85 | lex = shlex.shlex(StringIO(s)) | |
|
86 | # Try to get options, extensions and path separators as characters | |
|
87 | lex.wordchars = _wordchars | |
|
88 | lex.commenters = '' | |
|
89 | # Make a list out of the lexer by hand, since in 2.2 it's not an | |
|
90 | # iterator. | |
|
91 | lout = [] | |
|
92 | while 1: | |
|
93 | token = lex.get_token() | |
|
94 | if token == '': | |
|
95 | break | |
|
96 | # Try to handle quoted tokens correctly | |
|
97 | quotes = _quotesre.match(token) | |
|
98 | if quotes: | |
|
99 | token = quotes.group(1) | |
|
100 | lout.append(token) | |
|
101 | return lout | |
|
102 | ||
|
103 | 40 | #**************************************************************************** |
|
104 | 41 | # Exceptions |
|
105 | 42 | class Error(Exception): |
|
106 | 43 | """Base class for exceptions in this module.""" |
|
107 | 44 | pass |
|
108 | 45 | |
|
109 | 46 | #---------------------------------------------------------------------------- |
|
110 | 47 | class IOStream: |
|
111 | 48 | def __init__(self,stream,fallback): |
|
112 | 49 | if not hasattr(stream,'write') or not hasattr(stream,'flush'): |
|
113 | 50 | stream = fallback |
|
114 | 51 | self.stream = stream |
|
115 | 52 | self._swrite = stream.write |
|
116 | 53 | self.flush = stream.flush |
|
117 | 54 | |
|
118 | 55 | def write(self,data): |
|
119 | 56 | try: |
|
120 | 57 | self._swrite(data) |
|
121 | 58 | except: |
|
122 | 59 | try: |
|
123 | 60 | # print handles some unicode issues which may trip a plain |
|
124 | 61 | # write() call. Attempt to emulate write() by using a |
|
125 | 62 | # trailing comma |
|
126 | 63 | print >> self.stream, data, |
|
127 | 64 | except: |
|
128 | 65 | # if we get here, something is seriously broken. |
|
129 | 66 | print >> sys.stderr, \ |
|
130 | 67 | 'ERROR - failed to write data to stream:', self.stream |
|
131 | 68 | |
|
132 | 69 | class IOTerm: |
|
133 | 70 | """ Term holds the file or file-like objects for handling I/O operations. |
|
134 | 71 | |
|
135 | 72 | These are normally just sys.stdin, sys.stdout and sys.stderr but for |
|
136 | 73 | Windows they can can replaced to allow editing the strings before they are |
|
137 | 74 | displayed.""" |
|
138 | 75 | |
|
139 | 76 | # In the future, having IPython channel all its I/O operations through |
|
140 | 77 | # this class will make it easier to embed it into other environments which |
|
141 | 78 | # are not a normal terminal (such as a GUI-based shell) |
|
142 | 79 | def __init__(self,cin=None,cout=None,cerr=None): |
|
143 | 80 | self.cin = IOStream(cin,sys.stdin) |
|
144 | 81 | self.cout = IOStream(cout,sys.stdout) |
|
145 | 82 | self.cerr = IOStream(cerr,sys.stderr) |
|
146 | 83 | |
|
147 | 84 | # Global variable to be used for all I/O |
|
148 | 85 | Term = IOTerm() |
|
149 | 86 | |
|
150 | 87 | import IPython.rlineimpl as readline |
|
151 | 88 | # Remake Term to use the readline i/o facilities |
|
152 | 89 | if sys.platform == 'win32' and readline.have_readline: |
|
153 | 90 | |
|
154 | 91 | Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile) |
|
155 | 92 | |
|
156 | 93 | |
|
157 | 94 | #**************************************************************************** |
|
158 | 95 | # Generic warning/error printer, used by everything else |
|
159 | 96 | def warn(msg,level=2,exit_val=1): |
|
160 | 97 | """Standard warning printer. Gives formatting consistency. |
|
161 | 98 | |
|
162 | 99 | Output is sent to Term.cerr (sys.stderr by default). |
|
163 | 100 | |
|
164 | 101 | Options: |
|
165 | 102 | |
|
166 | 103 | -level(2): allows finer control: |
|
167 | 104 | 0 -> Do nothing, dummy function. |
|
168 | 105 | 1 -> Print message. |
|
169 | 106 | 2 -> Print 'WARNING:' + message. (Default level). |
|
170 | 107 | 3 -> Print 'ERROR:' + message. |
|
171 | 108 | 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val). |
|
172 | 109 | |
|
173 | 110 | -exit_val (1): exit value returned by sys.exit() for a level 4 |
|
174 | 111 | warning. Ignored for all other levels.""" |
|
175 | 112 | |
|
176 | 113 | if level>0: |
|
177 | 114 | header = ['','','WARNING: ','ERROR: ','FATAL ERROR: '] |
|
178 | 115 | print >> Term.cerr, '%s%s' % (header[level],msg) |
|
179 | 116 | if level == 4: |
|
180 | 117 | print >> Term.cerr,'Exiting.\n' |
|
181 | 118 | sys.exit(exit_val) |
|
182 | 119 | |
|
183 | 120 | def info(msg): |
|
184 | 121 | """Equivalent to warn(msg,level=1).""" |
|
185 | 122 | |
|
186 | 123 | warn(msg,level=1) |
|
187 | 124 | |
|
188 | 125 | def error(msg): |
|
189 | 126 | """Equivalent to warn(msg,level=3).""" |
|
190 | 127 | |
|
191 | 128 | warn(msg,level=3) |
|
192 | 129 | |
|
193 | 130 | def fatal(msg,exit_val=1): |
|
194 | 131 | """Equivalent to warn(msg,exit_val=exit_val,level=4).""" |
|
195 | 132 | |
|
196 | 133 | warn(msg,exit_val=exit_val,level=4) |
|
197 | 134 | |
|
198 | 135 | #--------------------------------------------------------------------------- |
|
199 | 136 | # Debugging routines |
|
200 | 137 | # |
|
201 | 138 | def debugx(expr,pre_msg=''): |
|
202 | 139 | """Print the value of an expression from the caller's frame. |
|
203 | 140 | |
|
204 | 141 | Takes an expression, evaluates it in the caller's frame and prints both |
|
205 | 142 | the given expression and the resulting value (as well as a debug mark |
|
206 | 143 | indicating the name of the calling function. The input must be of a form |
|
207 | 144 | suitable for eval(). |
|
208 | 145 | |
|
209 | 146 | An optional message can be passed, which will be prepended to the printed |
|
210 | 147 | expr->value pair.""" |
|
211 | 148 | |
|
212 | 149 | cf = sys._getframe(1) |
|
213 | 150 | print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr, |
|
214 | 151 | eval(expr,cf.f_globals,cf.f_locals)) |
|
215 | 152 | |
|
216 | 153 | # deactivate it by uncommenting the following line, which makes it a no-op |
|
217 | 154 | #def debugx(expr,pre_msg=''): pass |
|
218 | 155 | |
|
219 | 156 | #---------------------------------------------------------------------------- |
|
220 | 157 | StringTypes = types.StringTypes |
|
221 | 158 | |
|
222 | 159 | # Basic timing functionality |
|
223 | 160 | |
|
224 | 161 | # If possible (Unix), use the resource module instead of time.clock() |
|
225 | 162 | try: |
|
226 | 163 | import resource |
|
227 | 164 | def clock(): |
|
228 | 165 | """clock() -> floating point number |
|
229 | 166 | |
|
230 | 167 | Return the CPU time in seconds (user time only, system time is |
|
231 | 168 | ignored) since the start of the process. This is done via a call to |
|
232 | 169 | resource.getrusage, so it avoids the wraparound problems in |
|
233 | 170 | time.clock().""" |
|
234 | 171 | |
|
235 | 172 | return resource.getrusage(resource.RUSAGE_SELF)[0] |
|
236 | 173 | |
|
237 | 174 | def clock2(): |
|
238 | 175 | """clock2() -> (t_user,t_system) |
|
239 | 176 | |
|
240 | 177 | Similar to clock(), but return a tuple of user/system times.""" |
|
241 | 178 | return resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
242 | 179 | |
|
243 | 180 | except ImportError: |
|
244 | 181 | clock = time.clock |
|
245 | 182 | def clock2(): |
|
246 | 183 | """Under windows, system CPU time can't be measured. |
|
247 | 184 | |
|
248 | 185 | This just returns clock() and zero.""" |
|
249 | 186 | return time.clock(),0.0 |
|
250 | 187 | |
|
251 | 188 | def timings_out(reps,func,*args,**kw): |
|
252 | 189 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) |
|
253 | 190 | |
|
254 | 191 | Execute a function reps times, return a tuple with the elapsed total |
|
255 | 192 | CPU time in seconds, the time per call and the function's output. |
|
256 | 193 | |
|
257 | 194 | Under Unix, the return value is the sum of user+system time consumed by |
|
258 | 195 | the process, computed via the resource module. This prevents problems |
|
259 | 196 | related to the wraparound effect which the time.clock() function has. |
|
260 | 197 | |
|
261 | 198 | Under Windows the return value is in wall clock seconds. See the |
|
262 | 199 | documentation for the time module for more details.""" |
|
263 | 200 | |
|
264 | 201 | reps = int(reps) |
|
265 | 202 | assert reps >=1, 'reps must be >= 1' |
|
266 | 203 | if reps==1: |
|
267 | 204 | start = clock() |
|
268 | 205 | out = func(*args,**kw) |
|
269 | 206 | tot_time = clock()-start |
|
270 | 207 | else: |
|
271 | 208 | rng = xrange(reps-1) # the last time is executed separately to store output |
|
272 | 209 | start = clock() |
|
273 | 210 | for dummy in rng: func(*args,**kw) |
|
274 | 211 | out = func(*args,**kw) # one last time |
|
275 | 212 | tot_time = clock()-start |
|
276 | 213 | av_time = tot_time / reps |
|
277 | 214 | return tot_time,av_time,out |
|
278 | 215 | |
|
279 | 216 | def timings(reps,func,*args,**kw): |
|
280 | 217 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) |
|
281 | 218 | |
|
282 | 219 | Execute a function reps times, return a tuple with the elapsed total CPU |
|
283 | 220 | time in seconds and the time per call. These are just the first two values |
|
284 | 221 | in timings_out().""" |
|
285 | 222 | |
|
286 | 223 | return timings_out(reps,func,*args,**kw)[0:2] |
|
287 | 224 | |
|
288 | 225 | def timing(func,*args,**kw): |
|
289 | 226 | """timing(func,*args,**kw) -> t_total |
|
290 | 227 | |
|
291 | 228 | Execute a function once, return the elapsed total CPU time in |
|
292 | 229 | seconds. This is just the first value in timings_out().""" |
|
293 | 230 | |
|
294 | 231 | return timings_out(1,func,*args,**kw)[0] |
|
295 | 232 | |
|
296 | 233 | #**************************************************************************** |
|
297 | 234 | # file and system |
|
298 | 235 | |
|
299 | 236 | def system(cmd,verbose=0,debug=0,header=''): |
|
300 | 237 | """Execute a system command, return its exit status. |
|
301 | 238 | |
|
302 | 239 | Options: |
|
303 | 240 | |
|
304 | 241 | - verbose (0): print the command to be executed. |
|
305 | 242 | |
|
306 | 243 | - debug (0): only print, do not actually execute. |
|
307 | 244 | |
|
308 | 245 | - header (''): Header to print on screen prior to the executed command (it |
|
309 | 246 | is only prepended to the command, no newlines are added). |
|
310 | 247 | |
|
311 | 248 | Note: a stateful version of this function is available through the |
|
312 | 249 | SystemExec class.""" |
|
313 | 250 | |
|
314 | 251 | stat = 0 |
|
315 | 252 | if verbose or debug: print header+cmd |
|
316 | 253 | sys.stdout.flush() |
|
317 | 254 | if not debug: stat = os.system(cmd) |
|
318 | 255 | return stat |
|
319 | 256 | |
|
320 | 257 | # This function is used by ipython in a lot of places to make system calls. |
|
321 | 258 | # We need it to be slightly different under win32, due to the vagaries of |
|
322 | 259 | # 'network shares'. A win32 override is below. |
|
323 | 260 | |
|
324 | 261 | def shell(cmd,verbose=0,debug=0,header=''): |
|
325 | 262 | """Execute a command in the system shell, always return None. |
|
326 | 263 | |
|
327 | 264 | Options: |
|
328 | 265 | |
|
329 | 266 | - verbose (0): print the command to be executed. |
|
330 | 267 | |
|
331 | 268 | - debug (0): only print, do not actually execute. |
|
332 | 269 | |
|
333 | 270 | - header (''): Header to print on screen prior to the executed command (it |
|
334 | 271 | is only prepended to the command, no newlines are added). |
|
335 | 272 | |
|
336 | 273 | Note: this is similar to genutils.system(), but it returns None so it can |
|
337 | 274 | be conveniently used in interactive loops without getting the return value |
|
338 | 275 | (typically 0) printed many times.""" |
|
339 | 276 | |
|
340 | 277 | stat = 0 |
|
341 | 278 | if verbose or debug: print header+cmd |
|
342 | 279 | # flush stdout so we don't mangle python's buffering |
|
343 | 280 | sys.stdout.flush() |
|
344 | 281 | if not debug: |
|
345 | 282 | os.system(cmd) |
|
346 | 283 | |
|
347 | 284 | # override shell() for win32 to deal with network shares |
|
348 | 285 | if os.name in ('nt','dos'): |
|
349 | 286 | |
|
350 | 287 | shell_ori = shell |
|
351 | 288 | |
|
352 | 289 | def shell(cmd,verbose=0,debug=0,header=''): |
|
353 | 290 | if os.getcwd().startswith(r"\\"): |
|
354 | 291 | path = os.getcwd() |
|
355 | 292 | # change to c drive (cannot be on UNC-share when issuing os.system, |
|
356 | 293 | # as cmd.exe cannot handle UNC addresses) |
|
357 | 294 | os.chdir("c:") |
|
358 | 295 | # issue pushd to the UNC-share and then run the command |
|
359 | 296 | try: |
|
360 | 297 | shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header) |
|
361 | 298 | finally: |
|
362 | 299 | os.chdir(path) |
|
363 | 300 | else: |
|
364 | 301 | shell_ori(cmd,verbose,debug,header) |
|
365 | 302 | |
|
366 | 303 | shell.__doc__ = shell_ori.__doc__ |
|
367 | 304 | |
|
368 | 305 | def getoutput(cmd,verbose=0,debug=0,header='',split=0): |
|
369 | 306 | """Dummy substitute for perl's backquotes. |
|
370 | 307 | |
|
371 | 308 | Executes a command and returns the output. |
|
372 | 309 | |
|
373 | 310 | Accepts the same arguments as system(), plus: |
|
374 | 311 | |
|
375 | 312 | - split(0): if true, the output is returned as a list split on newlines. |
|
376 | 313 | |
|
377 | 314 | Note: a stateful version of this function is available through the |
|
378 | 315 | SystemExec class. |
|
379 | 316 | |
|
380 | 317 | This is pretty much deprecated and rarely used, |
|
381 | 318 | genutils.getoutputerror may be what you need. |
|
382 | 319 | |
|
383 | 320 | """ |
|
384 | 321 | |
|
385 | 322 | if verbose or debug: print header+cmd |
|
386 | 323 | if not debug: |
|
387 | 324 | output = os.popen(cmd).read() |
|
388 | 325 | # stipping last \n is here for backwards compat. |
|
389 | 326 | if output.endswith('\n'): |
|
390 | 327 | output = output[:-1] |
|
391 | 328 | if split: |
|
392 | 329 | return output.split('\n') |
|
393 | 330 | else: |
|
394 | 331 | return output |
|
395 | 332 | |
|
396 | 333 | def getoutputerror(cmd,verbose=0,debug=0,header='',split=0): |
|
397 | 334 | """Return (standard output,standard error) of executing cmd in a shell. |
|
398 | 335 | |
|
399 | 336 | Accepts the same arguments as system(), plus: |
|
400 | 337 | |
|
401 | 338 | - split(0): if true, each of stdout/err is returned as a list split on |
|
402 | 339 | newlines. |
|
403 | 340 | |
|
404 | 341 | Note: a stateful version of this function is available through the |
|
405 | 342 | SystemExec class.""" |
|
406 | 343 | |
|
407 | 344 | if verbose or debug: print header+cmd |
|
408 | 345 | if not cmd: |
|
409 | 346 | if split: |
|
410 | 347 | return [],[] |
|
411 | 348 | else: |
|
412 | 349 | return '','' |
|
413 | 350 | if not debug: |
|
414 | 351 | pin,pout,perr = os.popen3(cmd) |
|
415 | 352 | tout = pout.read().rstrip() |
|
416 | 353 | terr = perr.read().rstrip() |
|
417 | 354 | pin.close() |
|
418 | 355 | pout.close() |
|
419 | 356 | perr.close() |
|
420 | 357 | if split: |
|
421 | 358 | return tout.split('\n'),terr.split('\n') |
|
422 | 359 | else: |
|
423 | 360 | return tout,terr |
|
424 | 361 | |
|
425 | 362 | # for compatibility with older naming conventions |
|
426 | 363 | xsys = system |
|
427 | 364 | bq = getoutput |
|
428 | 365 | |
|
429 | 366 | class SystemExec: |
|
430 | 367 | """Access the system and getoutput functions through a stateful interface. |
|
431 | 368 | |
|
432 | 369 | Note: here we refer to the system and getoutput functions from this |
|
433 | 370 | library, not the ones from the standard python library. |
|
434 | 371 | |
|
435 | 372 | This class offers the system and getoutput functions as methods, but the |
|
436 | 373 | verbose, debug and header parameters can be set for the instance (at |
|
437 | 374 | creation time or later) so that they don't need to be specified on each |
|
438 | 375 | call. |
|
439 | 376 | |
|
440 | 377 | For efficiency reasons, there's no way to override the parameters on a |
|
441 | 378 | per-call basis other than by setting instance attributes. If you need |
|
442 | 379 | local overrides, it's best to directly call system() or getoutput(). |
|
443 | 380 | |
|
444 | 381 | The following names are provided as alternate options: |
|
445 | 382 | - xsys: alias to system |
|
446 | 383 | - bq: alias to getoutput |
|
447 | 384 | |
|
448 | 385 | An instance can then be created as: |
|
449 | 386 | >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ') |
|
450 | 387 | |
|
451 | 388 | And used as: |
|
452 | 389 | >>> sysexec.xsys('pwd') |
|
453 | 390 | >>> dirlist = sysexec.bq('ls -l') |
|
454 | 391 | """ |
|
455 | 392 | |
|
456 | 393 | def __init__(self,verbose=0,debug=0,header='',split=0): |
|
457 | 394 | """Specify the instance's values for verbose, debug and header.""" |
|
458 | 395 | setattr_list(self,'verbose debug header split') |
|
459 | 396 | |
|
460 | 397 | def system(self,cmd): |
|
461 | 398 | """Stateful interface to system(), with the same keyword parameters.""" |
|
462 | 399 | |
|
463 | 400 | system(cmd,self.verbose,self.debug,self.header) |
|
464 | 401 | |
|
465 | 402 | def shell(self,cmd): |
|
466 | 403 | """Stateful interface to shell(), with the same keyword parameters.""" |
|
467 | 404 | |
|
468 | 405 | shell(cmd,self.verbose,self.debug,self.header) |
|
469 | 406 | |
|
470 | 407 | xsys = system # alias |
|
471 | 408 | |
|
472 | 409 | def getoutput(self,cmd): |
|
473 | 410 | """Stateful interface to getoutput().""" |
|
474 | 411 | |
|
475 | 412 | return getoutput(cmd,self.verbose,self.debug,self.header,self.split) |
|
476 | 413 | |
|
477 | 414 | def getoutputerror(self,cmd): |
|
478 | 415 | """Stateful interface to getoutputerror().""" |
|
479 | 416 | |
|
480 | 417 | return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split) |
|
481 | 418 | |
|
482 | 419 | bq = getoutput # alias |
|
483 | 420 | |
|
484 | 421 | #----------------------------------------------------------------------------- |
|
485 | 422 | def mutex_opts(dict,ex_op): |
|
486 | 423 | """Check for presence of mutually exclusive keys in a dict. |
|
487 | 424 | |
|
488 | 425 | Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]""" |
|
489 | 426 | for op1,op2 in ex_op: |
|
490 | 427 | if op1 in dict and op2 in dict: |
|
491 | 428 | raise ValueError,'\n*** ERROR in Arguments *** '\ |
|
492 | 429 | 'Options '+op1+' and '+op2+' are mutually exclusive.' |
|
493 | 430 | |
|
494 | 431 | #----------------------------------------------------------------------------- |
|
495 | 432 | def get_py_filename(name): |
|
496 | 433 | """Return a valid python filename in the current directory. |
|
497 | 434 | |
|
498 | 435 | If the given name is not a file, it adds '.py' and searches again. |
|
499 | 436 | Raises IOError with an informative message if the file isn't found.""" |
|
500 | 437 | |
|
501 | 438 | name = os.path.expanduser(name) |
|
502 | 439 | if not os.path.isfile(name) and not name.endswith('.py'): |
|
503 | 440 | name += '.py' |
|
504 | 441 | if os.path.isfile(name): |
|
505 | 442 | return name |
|
506 | 443 | else: |
|
507 | 444 | raise IOError,'File `%s` not found.' % name |
|
508 | 445 | |
|
509 | 446 | #----------------------------------------------------------------------------- |
|
510 | 447 | def filefind(fname,alt_dirs = None): |
|
511 | 448 | """Return the given filename either in the current directory, if it |
|
512 | 449 | exists, or in a specified list of directories. |
|
513 | 450 | |
|
514 | 451 | ~ expansion is done on all file and directory names. |
|
515 | 452 | |
|
516 | 453 | Upon an unsuccessful search, raise an IOError exception.""" |
|
517 | 454 | |
|
518 | 455 | if alt_dirs is None: |
|
519 | 456 | try: |
|
520 | 457 | alt_dirs = get_home_dir() |
|
521 | 458 | except HomeDirError: |
|
522 | 459 | alt_dirs = os.getcwd() |
|
523 | 460 | search = [fname] + list_strings(alt_dirs) |
|
524 | 461 | search = map(os.path.expanduser,search) |
|
525 | 462 | #print 'search list for',fname,'list:',search # dbg |
|
526 | 463 | fname = search[0] |
|
527 | 464 | if os.path.isfile(fname): |
|
528 | 465 | return fname |
|
529 | 466 | for direc in search[1:]: |
|
530 | 467 | testname = os.path.join(direc,fname) |
|
531 | 468 | #print 'testname',testname # dbg |
|
532 | 469 | if os.path.isfile(testname): |
|
533 | 470 | return testname |
|
534 | 471 | raise IOError,'File' + `fname` + \ |
|
535 | 472 | ' not found in current or supplied directories:' + `alt_dirs` |
|
536 | 473 | |
|
537 | 474 | #---------------------------------------------------------------------------- |
|
538 | 475 | def file_read(filename): |
|
539 | 476 | """Read a file and close it. Returns the file source.""" |
|
540 | 477 | fobj = open(filename,'r'); |
|
541 | 478 | source = fobj.read(); |
|
542 | 479 | fobj.close() |
|
543 | 480 | return source |
|
544 | 481 | |
|
545 | 482 | def file_readlines(filename): |
|
546 | 483 | """Read a file and close it. Returns the file source using readlines().""" |
|
547 | 484 | fobj = open(filename,'r'); |
|
548 | 485 | lines = fobj.readlines(); |
|
549 | 486 | fobj.close() |
|
550 | 487 | return lines |
|
551 | 488 | |
|
552 | 489 | #---------------------------------------------------------------------------- |
|
553 | 490 | def target_outdated(target,deps): |
|
554 | 491 | """Determine whether a target is out of date. |
|
555 | 492 | |
|
556 | 493 | target_outdated(target,deps) -> 1/0 |
|
557 | 494 | |
|
558 | 495 | deps: list of filenames which MUST exist. |
|
559 | 496 | target: single filename which may or may not exist. |
|
560 | 497 | |
|
561 | 498 | If target doesn't exist or is older than any file listed in deps, return |
|
562 | 499 | true, otherwise return false. |
|
563 | 500 | """ |
|
564 | 501 | try: |
|
565 | 502 | target_time = os.path.getmtime(target) |
|
566 | 503 | except os.error: |
|
567 | 504 | return 1 |
|
568 | 505 | for dep in deps: |
|
569 | 506 | dep_time = os.path.getmtime(dep) |
|
570 | 507 | if dep_time > target_time: |
|
571 | 508 | #print "For target",target,"Dep failed:",dep # dbg |
|
572 | 509 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
573 | 510 | return 1 |
|
574 | 511 | return 0 |
|
575 | 512 | |
|
576 | 513 | #----------------------------------------------------------------------------- |
|
577 | 514 | def target_update(target,deps,cmd): |
|
578 | 515 | """Update a target with a given command given a list of dependencies. |
|
579 | 516 | |
|
580 | 517 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
581 | 518 | |
|
582 | 519 | This is just a wrapper around target_outdated() which calls the given |
|
583 | 520 | command if target is outdated.""" |
|
584 | 521 | |
|
585 | 522 | if target_outdated(target,deps): |
|
586 | 523 | xsys(cmd) |
|
587 | 524 | |
|
588 | 525 | #---------------------------------------------------------------------------- |
|
589 | 526 | def unquote_ends(istr): |
|
590 | 527 | """Remove a single pair of quotes from the endpoints of a string.""" |
|
591 | 528 | |
|
592 | 529 | if not istr: |
|
593 | 530 | return istr |
|
594 | 531 | if (istr[0]=="'" and istr[-1]=="'") or \ |
|
595 | 532 | (istr[0]=='"' and istr[-1]=='"'): |
|
596 | 533 | return istr[1:-1] |
|
597 | 534 | else: |
|
598 | 535 | return istr |
|
599 | 536 | |
|
600 | 537 | #---------------------------------------------------------------------------- |
|
601 | 538 | def process_cmdline(argv,names=[],defaults={},usage=''): |
|
602 | 539 | """ Process command-line options and arguments. |
|
603 | 540 | |
|
604 | 541 | Arguments: |
|
605 | 542 | |
|
606 | 543 | - argv: list of arguments, typically sys.argv. |
|
607 | 544 | |
|
608 | 545 | - names: list of option names. See DPyGetOpt docs for details on options |
|
609 | 546 | syntax. |
|
610 | 547 | |
|
611 | 548 | - defaults: dict of default values. |
|
612 | 549 | |
|
613 | 550 | - usage: optional usage notice to print if a wrong argument is passed. |
|
614 | 551 | |
|
615 | 552 | Return a dict of options and a list of free arguments.""" |
|
616 | 553 | |
|
617 | 554 | getopt = DPyGetOpt.DPyGetOpt() |
|
618 | 555 | getopt.setIgnoreCase(0) |
|
619 | 556 | getopt.parseConfiguration(names) |
|
620 | 557 | |
|
621 | 558 | try: |
|
622 | 559 | getopt.processArguments(argv) |
|
623 | 560 | except: |
|
624 | 561 | print usage |
|
625 | 562 | warn(`sys.exc_value`,level=4) |
|
626 | 563 | |
|
627 | 564 | defaults.update(getopt.optionValues) |
|
628 | 565 | args = getopt.freeValues |
|
629 | 566 | |
|
630 | 567 | return defaults,args |
|
631 | 568 | |
|
632 | 569 | #---------------------------------------------------------------------------- |
|
633 | 570 | def optstr2types(ostr): |
|
634 | 571 | """Convert a string of option names to a dict of type mappings. |
|
635 | 572 | |
|
636 | 573 | optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'} |
|
637 | 574 | |
|
638 | 575 | This is used to get the types of all the options in a string formatted |
|
639 | 576 | with the conventions of DPyGetOpt. The 'type' None is used for options |
|
640 | 577 | which are strings (they need no further conversion). This function's main |
|
641 | 578 | use is to get a typemap for use with read_dict(). |
|
642 | 579 | """ |
|
643 | 580 | |
|
644 | 581 | typeconv = {None:'',int:'',float:''} |
|
645 | 582 | typemap = {'s':None,'i':int,'f':float} |
|
646 | 583 | opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)') |
|
647 | 584 | |
|
648 | 585 | for w in ostr.split(): |
|
649 | 586 | oname,alias,otype = opt_re.match(w).groups() |
|
650 | 587 | if otype == '' or alias == '!': # simple switches are integers too |
|
651 | 588 | otype = 'i' |
|
652 | 589 | typeconv[typemap[otype]] += oname + ' ' |
|
653 | 590 | return typeconv |
|
654 | 591 | |
|
655 | 592 | #---------------------------------------------------------------------------- |
|
656 | 593 | def read_dict(filename,type_conv=None,**opt): |
|
657 | 594 | |
|
658 | 595 | """Read a dictionary of key=value pairs from an input file, optionally |
|
659 | 596 | performing conversions on the resulting values. |
|
660 | 597 | |
|
661 | 598 | read_dict(filename,type_conv,**opt) -> dict |
|
662 | 599 | |
|
663 | 600 | Only one value per line is accepted, the format should be |
|
664 | 601 | # optional comments are ignored |
|
665 | 602 | key value\n |
|
666 | 603 | |
|
667 | 604 | Args: |
|
668 | 605 | |
|
669 | 606 | - type_conv: A dictionary specifying which keys need to be converted to |
|
670 | 607 | which types. By default all keys are read as strings. This dictionary |
|
671 | 608 | should have as its keys valid conversion functions for strings |
|
672 | 609 | (int,long,float,complex, or your own). The value for each key |
|
673 | 610 | (converter) should be a whitespace separated string containing the names |
|
674 | 611 | of all the entries in the file to be converted using that function. For |
|
675 | 612 | keys to be left alone, use None as the conversion function (only needed |
|
676 | 613 | with purge=1, see below). |
|
677 | 614 | |
|
678 | 615 | - opt: dictionary with extra options as below (default in parens) |
|
679 | 616 | |
|
680 | 617 | purge(0): if set to 1, all keys *not* listed in type_conv are purged out |
|
681 | 618 | of the dictionary to be returned. If purge is going to be used, the |
|
682 | 619 | set of keys to be left as strings also has to be explicitly specified |
|
683 | 620 | using the (non-existent) conversion function None. |
|
684 | 621 | |
|
685 | 622 | fs(None): field separator. This is the key/value separator to be used |
|
686 | 623 | when parsing the file. The None default means any whitespace [behavior |
|
687 | 624 | of string.split()]. |
|
688 | 625 | |
|
689 | 626 | strip(0): if 1, strip string values of leading/trailinig whitespace. |
|
690 | 627 | |
|
691 | 628 | warn(1): warning level if requested keys are not found in file. |
|
692 | 629 | - 0: silently ignore. |
|
693 | 630 | - 1: inform but proceed. |
|
694 | 631 | - 2: raise KeyError exception. |
|
695 | 632 | |
|
696 | 633 | no_empty(0): if 1, remove keys with whitespace strings as a value. |
|
697 | 634 | |
|
698 | 635 | unique([]): list of keys (or space separated string) which can't be |
|
699 | 636 | repeated. If one such key is found in the file, each new instance |
|
700 | 637 | overwrites the previous one. For keys not listed here, the behavior is |
|
701 | 638 | to make a list of all appearances. |
|
702 | 639 | |
|
703 | 640 | Example: |
|
704 | 641 | If the input file test.ini has: |
|
705 | 642 | i 3 |
|
706 | 643 | x 4.5 |
|
707 | 644 | y 5.5 |
|
708 | 645 | s hi ho |
|
709 | 646 | Then: |
|
710 | 647 | |
|
711 | 648 | >>> type_conv={int:'i',float:'x',None:'s'} |
|
712 | 649 | >>> read_dict('test.ini') |
|
713 | 650 | {'i': '3', 's': 'hi ho', 'x': '4.5', 'y': '5.5'} |
|
714 | 651 | >>> read_dict('test.ini',type_conv) |
|
715 | 652 | {'i': 3, 's': 'hi ho', 'x': 4.5, 'y': '5.5'} |
|
716 | 653 | >>> read_dict('test.ini',type_conv,purge=1) |
|
717 | 654 | {'i': 3, 's': 'hi ho', 'x': 4.5} |
|
718 | 655 | """ |
|
719 | 656 | |
|
720 | 657 | # starting config |
|
721 | 658 | opt.setdefault('purge',0) |
|
722 | 659 | opt.setdefault('fs',None) # field sep defaults to any whitespace |
|
723 | 660 | opt.setdefault('strip',0) |
|
724 | 661 | opt.setdefault('warn',1) |
|
725 | 662 | opt.setdefault('no_empty',0) |
|
726 | 663 | opt.setdefault('unique','') |
|
727 | 664 | if type(opt['unique']) in StringTypes: |
|
728 | 665 | unique_keys = qw(opt['unique']) |
|
729 | 666 | elif type(opt['unique']) in (types.TupleType,types.ListType): |
|
730 | 667 | unique_keys = opt['unique'] |
|
731 | 668 | else: |
|
732 | 669 | raise ValueError, 'Unique keys must be given as a string, List or Tuple' |
|
733 | 670 | |
|
734 | 671 | dict = {} |
|
735 | 672 | # first read in table of values as strings |
|
736 | 673 | file = open(filename,'r') |
|
737 | 674 | for line in file.readlines(): |
|
738 | 675 | line = line.strip() |
|
739 | 676 | if len(line) and line[0]=='#': continue |
|
740 | 677 | if len(line)>0: |
|
741 | 678 | lsplit = line.split(opt['fs'],1) |
|
742 | 679 | try: |
|
743 | 680 | key,val = lsplit |
|
744 | 681 | except ValueError: |
|
745 | 682 | key,val = lsplit[0],'' |
|
746 | 683 | key = key.strip() |
|
747 | 684 | if opt['strip']: val = val.strip() |
|
748 | 685 | if val == "''" or val == '""': val = '' |
|
749 | 686 | if opt['no_empty'] and (val=='' or val.isspace()): |
|
750 | 687 | continue |
|
751 | 688 | # if a key is found more than once in the file, build a list |
|
752 | 689 | # unless it's in the 'unique' list. In that case, last found in file |
|
753 | 690 | # takes precedence. User beware. |
|
754 | 691 | try: |
|
755 | 692 | if dict[key] and key in unique_keys: |
|
756 | 693 | dict[key] = val |
|
757 | 694 | elif type(dict[key]) is types.ListType: |
|
758 | 695 | dict[key].append(val) |
|
759 | 696 | else: |
|
760 | 697 | dict[key] = [dict[key],val] |
|
761 | 698 | except KeyError: |
|
762 | 699 | dict[key] = val |
|
763 | 700 | # purge if requested |
|
764 | 701 | if opt['purge']: |
|
765 | 702 | accepted_keys = qwflat(type_conv.values()) |
|
766 | 703 | for key in dict.keys(): |
|
767 | 704 | if key in accepted_keys: continue |
|
768 | 705 | del(dict[key]) |
|
769 | 706 | # now convert if requested |
|
770 | 707 | if type_conv==None: return dict |
|
771 | 708 | conversions = type_conv.keys() |
|
772 | 709 | try: conversions.remove(None) |
|
773 | 710 | except: pass |
|
774 | 711 | for convert in conversions: |
|
775 | 712 | for val in qw(type_conv[convert]): |
|
776 | 713 | try: |
|
777 | 714 | dict[val] = convert(dict[val]) |
|
778 | 715 | except KeyError,e: |
|
779 | 716 | if opt['warn'] == 0: |
|
780 | 717 | pass |
|
781 | 718 | elif opt['warn'] == 1: |
|
782 | 719 | print >>sys.stderr, 'Warning: key',val,\ |
|
783 | 720 | 'not found in file',filename |
|
784 | 721 | elif opt['warn'] == 2: |
|
785 | 722 | raise KeyError,e |
|
786 | 723 | else: |
|
787 | 724 | raise ValueError,'Warning level must be 0,1 or 2' |
|
788 | 725 | |
|
789 | 726 | return dict |
|
790 | 727 | |
|
791 | 728 | #---------------------------------------------------------------------------- |
|
792 | 729 | def flag_calls(func): |
|
793 | 730 | """Wrap a function to detect and flag when it gets called. |
|
794 | 731 | |
|
795 | 732 | This is a decorator which takes a function and wraps it in a function with |
|
796 | 733 | a 'called' attribute. wrapper.called is initialized to False. |
|
797 | 734 | |
|
798 | 735 | The wrapper.called attribute is set to False right before each call to the |
|
799 | 736 | wrapped function, so if the call fails it remains False. After the call |
|
800 | 737 | completes, wrapper.called is set to True and the output is returned. |
|
801 | 738 | |
|
802 | 739 | Testing for truth in wrapper.called allows you to determine if a call to |
|
803 | 740 | func() was attempted and succeeded.""" |
|
804 | 741 | |
|
805 | 742 | def wrapper(*args,**kw): |
|
806 | 743 | wrapper.called = False |
|
807 | 744 | out = func(*args,**kw) |
|
808 | 745 | wrapper.called = True |
|
809 | 746 | return out |
|
810 | 747 | |
|
811 | 748 | wrapper.called = False |
|
812 | 749 | wrapper.__doc__ = func.__doc__ |
|
813 | 750 | return wrapper |
|
814 | 751 | |
|
815 | 752 | #---------------------------------------------------------------------------- |
|
816 | 753 | class HomeDirError(Error): |
|
817 | 754 | pass |
|
818 | 755 | |
|
819 | 756 | def get_home_dir(): |
|
820 | 757 | """Return the closest possible equivalent to a 'home' directory. |
|
821 | 758 | |
|
822 | 759 | We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH. |
|
823 | 760 | |
|
824 | 761 | Currently only Posix and NT are implemented, a HomeDirError exception is |
|
825 | 762 | raised for all other OSes. """ |
|
826 | 763 | |
|
827 | 764 | isdir = os.path.isdir |
|
828 | 765 | env = os.environ |
|
829 | 766 | try: |
|
830 | 767 | homedir = env['HOME'] |
|
831 | 768 | if not isdir(homedir): |
|
832 | 769 | # in case a user stuck some string which does NOT resolve to a |
|
833 | 770 | # valid path, it's as good as if we hadn't foud it |
|
834 | 771 | raise KeyError |
|
835 | 772 | return homedir |
|
836 | 773 | except KeyError: |
|
837 | 774 | if os.name == 'posix': |
|
838 | 775 | raise HomeDirError,'undefined $HOME, IPython can not proceed.' |
|
839 | 776 | elif os.name == 'nt': |
|
840 | 777 | # For some strange reason, win9x returns 'nt' for os.name. |
|
841 | 778 | try: |
|
842 | 779 | homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH']) |
|
843 | 780 | if not isdir(homedir): |
|
844 | 781 | homedir = os.path.join(env['USERPROFILE']) |
|
845 | 782 | if not isdir(homedir): |
|
846 | 783 | raise HomeDirError |
|
847 | 784 | return homedir |
|
848 | 785 | except: |
|
849 | 786 | try: |
|
850 | 787 | # Use the registry to get the 'My Documents' folder. |
|
851 | 788 | import _winreg as wreg |
|
852 | 789 | key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, |
|
853 | 790 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders") |
|
854 | 791 | homedir = wreg.QueryValueEx(key,'Personal')[0] |
|
855 | 792 | key.Close() |
|
856 | 793 | if not isdir(homedir): |
|
857 | 794 | e = ('Invalid "Personal" folder registry key ' |
|
858 | 795 | 'typically "My Documents".\n' |
|
859 | 796 | 'Value: %s\n' |
|
860 | 797 | 'This is not a valid directory on your system.' % |
|
861 | 798 | homedir) |
|
862 | 799 | raise HomeDirError(e) |
|
863 | 800 | return homedir |
|
864 | 801 | except HomeDirError: |
|
865 | 802 | raise |
|
866 | 803 | except: |
|
867 | 804 | return 'C:\\' |
|
868 | 805 | elif os.name == 'dos': |
|
869 | 806 | # Desperate, may do absurd things in classic MacOS. May work under DOS. |
|
870 | 807 | return 'C:\\' |
|
871 | 808 | else: |
|
872 | 809 | raise HomeDirError,'support for your operating system not implemented.' |
|
873 | 810 | |
|
874 | 811 | #**************************************************************************** |
|
875 | 812 | # strings and text |
|
876 | 813 | |
|
877 | 814 | class LSString(str): |
|
878 | 815 | """String derivative with a special access attributes. |
|
879 | 816 | |
|
880 | 817 | These are normal strings, but with the special attributes: |
|
881 | 818 | |
|
882 | 819 | .l (or .list) : value as list (split on newlines). |
|
883 | 820 | .n (or .nlstr): original value (the string itself). |
|
884 | 821 | .s (or .spstr): value as whitespace-separated string. |
|
885 | 822 | |
|
886 | 823 | Any values which require transformations are computed only once and |
|
887 | 824 | cached. |
|
888 | 825 | |
|
889 | 826 | Such strings are very useful to efficiently interact with the shell, which |
|
890 | 827 | typically only understands whitespace-separated options for commands.""" |
|
891 | 828 | |
|
892 | 829 | def get_list(self): |
|
893 | 830 | try: |
|
894 | 831 | return self.__list |
|
895 | 832 | except AttributeError: |
|
896 | 833 | self.__list = self.split('\n') |
|
897 | 834 | return self.__list |
|
898 | 835 | |
|
899 | 836 | l = list = property(get_list) |
|
900 | 837 | |
|
901 | 838 | def get_spstr(self): |
|
902 | 839 | try: |
|
903 | 840 | return self.__spstr |
|
904 | 841 | except AttributeError: |
|
905 | 842 | self.__spstr = self.replace('\n',' ') |
|
906 | 843 | return self.__spstr |
|
907 | 844 | |
|
908 | 845 | s = spstr = property(get_spstr) |
|
909 | 846 | |
|
910 | 847 | def get_nlstr(self): |
|
911 | 848 | return self |
|
912 | 849 | |
|
913 | 850 | n = nlstr = property(get_nlstr) |
|
914 | 851 | |
|
915 | 852 | def get_paths(self): |
|
916 | 853 | try: |
|
917 | 854 | return self.__paths |
|
918 | 855 | except AttributeError: |
|
919 | 856 | self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)] |
|
920 | 857 | return self.__paths |
|
921 | 858 | |
|
922 | 859 | p = paths = property(get_paths) |
|
923 | 860 | |
|
924 | 861 | |
|
925 | 862 | #---------------------------------------------------------------------------- |
|
926 | 863 | class SList(list): |
|
927 | 864 | """List derivative with a special access attributes. |
|
928 | 865 | |
|
929 | 866 | These are normal lists, but with the special attributes: |
|
930 | 867 | |
|
931 | 868 | .l (or .list) : value as list (the list itself). |
|
932 | 869 | .n (or .nlstr): value as a string, joined on newlines. |
|
933 | 870 | .s (or .spstr): value as a string, joined on spaces. |
|
934 | 871 | |
|
935 | 872 | Any values which require transformations are computed only once and |
|
936 | 873 | cached.""" |
|
937 | 874 | |
|
938 | 875 | def get_list(self): |
|
939 | 876 | return self |
|
940 | 877 | |
|
941 | 878 | l = list = property(get_list) |
|
942 | 879 | |
|
943 | 880 | def get_spstr(self): |
|
944 | 881 | try: |
|
945 | 882 | return self.__spstr |
|
946 | 883 | except AttributeError: |
|
947 | 884 | self.__spstr = ' '.join(self) |
|
948 | 885 | return self.__spstr |
|
949 | 886 | |
|
950 | 887 | s = spstr = property(get_spstr) |
|
951 | 888 | |
|
952 | 889 | def get_nlstr(self): |
|
953 | 890 | try: |
|
954 | 891 | return self.__nlstr |
|
955 | 892 | except AttributeError: |
|
956 | 893 | self.__nlstr = '\n'.join(self) |
|
957 | 894 | return self.__nlstr |
|
958 | 895 | |
|
959 | 896 | n = nlstr = property(get_nlstr) |
|
960 | 897 | |
|
961 | 898 | def get_paths(self): |
|
962 | 899 | try: |
|
963 | 900 | return self.__paths |
|
964 | 901 | except AttributeError: |
|
965 | 902 | self.__paths = [path(p) for p in self if os.path.exists(p)] |
|
966 | 903 | return self.__paths |
|
967 | 904 | |
|
968 | 905 | p = paths = property(get_paths) |
|
969 | 906 | |
|
970 | 907 | #---------------------------------------------------------------------------- |
|
971 | 908 | def esc_quotes(strng): |
|
972 | 909 | """Return the input string with single and double quotes escaped out""" |
|
973 | 910 | |
|
974 | 911 | return strng.replace('"','\\"').replace("'","\\'") |
|
975 | 912 | |
|
976 | 913 | #---------------------------------------------------------------------------- |
|
977 | 914 | def make_quoted_expr(s): |
|
978 | 915 | """Return string s in appropriate quotes, using raw string if possible. |
|
979 | 916 | |
|
980 | 917 | Effectively this turns string: cd \ao\ao\ |
|
981 | 918 | to: r"cd \ao\ao\_"[:-1] |
|
982 | 919 | |
|
983 | 920 | Note the use of raw string and padding at the end to allow trailing backslash. |
|
984 | 921 | |
|
985 | 922 | """ |
|
986 | 923 | |
|
987 | 924 | tail = '' |
|
988 | 925 | tailpadding = '' |
|
989 | 926 | raw = '' |
|
990 | 927 | if "\\" in s: |
|
991 | 928 | raw = 'r' |
|
992 | 929 | if s.endswith('\\'): |
|
993 | 930 | tail = '[:-1]' |
|
994 | 931 | tailpadding = '_' |
|
995 | 932 | if '"' not in s: |
|
996 | 933 | quote = '"' |
|
997 | 934 | elif "'" not in s: |
|
998 | 935 | quote = "'" |
|
999 | 936 | elif '"""' not in s and not s.endswith('"'): |
|
1000 | 937 | quote = '"""' |
|
1001 | 938 | elif "'''" not in s and not s.endswith("'"): |
|
1002 | 939 | quote = "'''" |
|
1003 | 940 | else: |
|
1004 | 941 | # give up, backslash-escaped string will do |
|
1005 | 942 | return '"%s"' % esc_quotes(s) |
|
1006 | 943 | res = itpl("$raw$quote$s$tailpadding$quote$tail") |
|
1007 | 944 | return res |
|
1008 | 945 | |
|
1009 | 946 | |
|
1010 | 947 | #---------------------------------------------------------------------------- |
|
1011 | 948 | def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'): |
|
1012 | 949 | """Take multiple lines of input. |
|
1013 | 950 | |
|
1014 | 951 | A list with each line of input as a separate element is returned when a |
|
1015 | 952 | termination string is entered (defaults to a single '.'). Input can also |
|
1016 | 953 | terminate via EOF (^D in Unix, ^Z-RET in Windows). |
|
1017 | 954 | |
|
1018 | 955 | Lines of input which end in \\ are joined into single entries (and a |
|
1019 | 956 | secondary continuation prompt is issued as long as the user terminates |
|
1020 | 957 | lines with \\). This allows entering very long strings which are still |
|
1021 | 958 | meant to be treated as single entities. |
|
1022 | 959 | """ |
|
1023 | 960 | |
|
1024 | 961 | try: |
|
1025 | 962 | if header: |
|
1026 | 963 | header += '\n' |
|
1027 | 964 | lines = [raw_input(header + ps1)] |
|
1028 | 965 | except EOFError: |
|
1029 | 966 | return [] |
|
1030 | 967 | terminate = [terminate_str] |
|
1031 | 968 | try: |
|
1032 | 969 | while lines[-1:] != terminate: |
|
1033 | 970 | new_line = raw_input(ps1) |
|
1034 | 971 | while new_line.endswith('\\'): |
|
1035 | 972 | new_line = new_line[:-1] + raw_input(ps2) |
|
1036 | 973 | lines.append(new_line) |
|
1037 | 974 | |
|
1038 | 975 | return lines[:-1] # don't return the termination command |
|
1039 | 976 | except EOFError: |
|
1040 | 977 | |
|
1041 | 978 | return lines |
|
1042 | 979 | |
|
1043 | 980 | #---------------------------------------------------------------------------- |
|
1044 | 981 | def raw_input_ext(prompt='', ps2='... '): |
|
1045 | 982 | """Similar to raw_input(), but accepts extended lines if input ends with \\.""" |
|
1046 | 983 | |
|
1047 | 984 | line = raw_input(prompt) |
|
1048 | 985 | while line.endswith('\\'): |
|
1049 | 986 | line = line[:-1] + raw_input(ps2) |
|
1050 | 987 | return line |
|
1051 | 988 | |
|
1052 | 989 | #---------------------------------------------------------------------------- |
|
1053 | 990 | def ask_yes_no(prompt,default=None): |
|
1054 | 991 | """Asks a question and returns an integer 1/0 (y/n) answer. |
|
1055 | 992 | |
|
1056 | 993 | If default is given (one of 'y','n'), it is used if the user input is |
|
1057 | 994 | empty. Otherwise the question is repeated until an answer is given. |
|
1058 | 995 | If EOF occurs 20 times consecutively, the default answer is assumed, |
|
1059 | 996 | or if there is no default, an exception is raised to prevent infinite |
|
1060 | 997 | loops. |
|
1061 | 998 | |
|
1062 | 999 | Valid answers are: y/yes/n/no (match is not case sensitive).""" |
|
1063 | 1000 | |
|
1064 | 1001 | answers = {'y':True,'n':False,'yes':True,'no':False} |
|
1065 | 1002 | ans = None |
|
1066 | 1003 | eofs, max_eofs = 0, 20 |
|
1067 | 1004 | while ans not in answers.keys(): |
|
1068 | 1005 | try: |
|
1069 | 1006 | ans = raw_input(prompt+' ').lower() |
|
1070 | 1007 | if not ans: # response was an empty string |
|
1071 | 1008 | ans = default |
|
1072 | 1009 | eofs = 0 |
|
1073 | 1010 | except (EOFError,KeyboardInterrupt): |
|
1074 | 1011 | eofs = eofs + 1 |
|
1075 | 1012 | if eofs >= max_eofs: |
|
1076 | 1013 | if default in answers.keys(): |
|
1077 | 1014 | ans = default |
|
1078 | 1015 | else: |
|
1079 | 1016 | raise |
|
1080 | 1017 | |
|
1081 | 1018 | return answers[ans] |
|
1082 | 1019 | |
|
1083 | 1020 | #---------------------------------------------------------------------------- |
|
1084 | 1021 | def marquee(txt='',width=78,mark='*'): |
|
1085 | 1022 | """Return the input string centered in a 'marquee'.""" |
|
1086 | 1023 | if not txt: |
|
1087 | 1024 | return (mark*width)[:width] |
|
1088 | 1025 | nmark = (width-len(txt)-2)/len(mark)/2 |
|
1089 | 1026 | if nmark < 0: nmark =0 |
|
1090 | 1027 | marks = mark*nmark |
|
1091 | 1028 | return '%s %s %s' % (marks,txt,marks) |
|
1092 | 1029 | |
|
1093 | 1030 | #---------------------------------------------------------------------------- |
|
1094 | 1031 | class EvalDict: |
|
1095 | 1032 | """ |
|
1096 | 1033 | Emulate a dict which evaluates its contents in the caller's frame. |
|
1097 | 1034 | |
|
1098 | 1035 | Usage: |
|
1099 | 1036 | >>>number = 19 |
|
1100 | 1037 | >>>text = "python" |
|
1101 | 1038 | >>>print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict() |
|
1102 | 1039 | """ |
|
1103 | 1040 | |
|
1104 | 1041 | # This version is due to sismex01@hebmex.com on c.l.py, and is basically a |
|
1105 | 1042 | # modified (shorter) version of: |
|
1106 | 1043 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by |
|
1107 | 1044 | # Skip Montanaro (skip@pobox.com). |
|
1108 | 1045 | |
|
1109 | 1046 | def __getitem__(self, name): |
|
1110 | 1047 | frame = sys._getframe(1) |
|
1111 | 1048 | return eval(name, frame.f_globals, frame.f_locals) |
|
1112 | 1049 | |
|
1113 | 1050 | EvalString = EvalDict # for backwards compatibility |
|
1114 | 1051 | #---------------------------------------------------------------------------- |
|
1115 | 1052 | def qw(words,flat=0,sep=None,maxsplit=-1): |
|
1116 | 1053 | """Similar to Perl's qw() operator, but with some more options. |
|
1117 | 1054 | |
|
1118 | 1055 | qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit) |
|
1119 | 1056 | |
|
1120 | 1057 | words can also be a list itself, and with flat=1, the output will be |
|
1121 | 1058 | recursively flattened. Examples: |
|
1122 | 1059 | |
|
1123 | 1060 | >>> qw('1 2') |
|
1124 | 1061 | ['1', '2'] |
|
1125 | 1062 | >>> qw(['a b','1 2',['m n','p q']]) |
|
1126 | 1063 | [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]] |
|
1127 | 1064 | >>> qw(['a b','1 2',['m n','p q']],flat=1) |
|
1128 | 1065 | ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] """ |
|
1129 | 1066 | |
|
1130 | 1067 | if type(words) in StringTypes: |
|
1131 | 1068 | return [word.strip() for word in words.split(sep,maxsplit) |
|
1132 | 1069 | if word and not word.isspace() ] |
|
1133 | 1070 | if flat: |
|
1134 | 1071 | return flatten(map(qw,words,[1]*len(words))) |
|
1135 | 1072 | return map(qw,words) |
|
1136 | 1073 | |
|
1137 | 1074 | #---------------------------------------------------------------------------- |
|
1138 | 1075 | def qwflat(words,sep=None,maxsplit=-1): |
|
1139 | 1076 | """Calls qw(words) in flat mode. It's just a convenient shorthand.""" |
|
1140 | 1077 | return qw(words,1,sep,maxsplit) |
|
1141 | 1078 | |
|
1142 | 1079 | #---------------------------------------------------------------------------- |
|
1143 | 1080 | def qw_lol(indata): |
|
1144 | 1081 | """qw_lol('a b') -> [['a','b']], |
|
1145 | 1082 | otherwise it's just a call to qw(). |
|
1146 | 1083 | |
|
1147 | 1084 | We need this to make sure the modules_some keys *always* end up as a |
|
1148 | 1085 | list of lists.""" |
|
1149 | 1086 | |
|
1150 | 1087 | if type(indata) in StringTypes: |
|
1151 | 1088 | return [qw(indata)] |
|
1152 | 1089 | else: |
|
1153 | 1090 | return qw(indata) |
|
1154 | 1091 | |
|
1155 | 1092 | #----------------------------------------------------------------------------- |
|
1156 | 1093 | def list_strings(arg): |
|
1157 | 1094 | """Always return a list of strings, given a string or list of strings |
|
1158 | 1095 | as input.""" |
|
1159 | 1096 | |
|
1160 | 1097 | if type(arg) in StringTypes: return [arg] |
|
1161 | 1098 | else: return arg |
|
1162 | 1099 | |
|
1163 | 1100 | #---------------------------------------------------------------------------- |
|
1164 | 1101 | def grep(pat,list,case=1): |
|
1165 | 1102 | """Simple minded grep-like function. |
|
1166 | 1103 | grep(pat,list) returns occurrences of pat in list, None on failure. |
|
1167 | 1104 | |
|
1168 | 1105 | It only does simple string matching, with no support for regexps. Use the |
|
1169 | 1106 | option case=0 for case-insensitive matching.""" |
|
1170 | 1107 | |
|
1171 | 1108 | # This is pretty crude. At least it should implement copying only references |
|
1172 | 1109 | # to the original data in case it's big. Now it copies the data for output. |
|
1173 | 1110 | out=[] |
|
1174 | 1111 | if case: |
|
1175 | 1112 | for term in list: |
|
1176 | 1113 | if term.find(pat)>-1: out.append(term) |
|
1177 | 1114 | else: |
|
1178 | 1115 | lpat=pat.lower() |
|
1179 | 1116 | for term in list: |
|
1180 | 1117 | if term.lower().find(lpat)>-1: out.append(term) |
|
1181 | 1118 | |
|
1182 | 1119 | if len(out): return out |
|
1183 | 1120 | else: return None |
|
1184 | 1121 | |
|
1185 | 1122 | #---------------------------------------------------------------------------- |
|
1186 | 1123 | def dgrep(pat,*opts): |
|
1187 | 1124 | """Return grep() on dir()+dir(__builtins__). |
|
1188 | 1125 | |
|
1189 | 1126 | A very common use of grep() when working interactively.""" |
|
1190 | 1127 | |
|
1191 | 1128 | return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts) |
|
1192 | 1129 | |
|
1193 | 1130 | #---------------------------------------------------------------------------- |
|
1194 | 1131 | def idgrep(pat): |
|
1195 | 1132 | """Case-insensitive dgrep()""" |
|
1196 | 1133 | |
|
1197 | 1134 | return dgrep(pat,0) |
|
1198 | 1135 | |
|
1199 | 1136 | #---------------------------------------------------------------------------- |
|
1200 | 1137 | def igrep(pat,list): |
|
1201 | 1138 | """Synonym for case-insensitive grep.""" |
|
1202 | 1139 | |
|
1203 | 1140 | return grep(pat,list,case=0) |
|
1204 | 1141 | |
|
1205 | 1142 | #---------------------------------------------------------------------------- |
|
1206 | 1143 | def indent(str,nspaces=4,ntabs=0): |
|
1207 | 1144 | """Indent a string a given number of spaces or tabstops. |
|
1208 | 1145 | |
|
1209 | 1146 | indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces. |
|
1210 | 1147 | """ |
|
1211 | 1148 | if str is None: |
|
1212 | 1149 | return |
|
1213 | 1150 | ind = '\t'*ntabs+' '*nspaces |
|
1214 | 1151 | outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind)) |
|
1215 | 1152 | if outstr.endswith(os.linesep+ind): |
|
1216 | 1153 | return outstr[:-len(ind)] |
|
1217 | 1154 | else: |
|
1218 | 1155 | return outstr |
|
1219 | 1156 | |
|
1220 | 1157 | #----------------------------------------------------------------------------- |
|
1221 | 1158 | def native_line_ends(filename,backup=1): |
|
1222 | 1159 | """Convert (in-place) a file to line-ends native to the current OS. |
|
1223 | 1160 | |
|
1224 | 1161 | If the optional backup argument is given as false, no backup of the |
|
1225 | 1162 | original file is left. """ |
|
1226 | 1163 | |
|
1227 | 1164 | backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'} |
|
1228 | 1165 | |
|
1229 | 1166 | bak_filename = filename + backup_suffixes[os.name] |
|
1230 | 1167 | |
|
1231 | 1168 | original = open(filename).read() |
|
1232 | 1169 | shutil.copy2(filename,bak_filename) |
|
1233 | 1170 | try: |
|
1234 | 1171 | new = open(filename,'wb') |
|
1235 | 1172 | new.write(os.linesep.join(original.splitlines())) |
|
1236 | 1173 | new.write(os.linesep) # ALWAYS put an eol at the end of the file |
|
1237 | 1174 | new.close() |
|
1238 | 1175 | except: |
|
1239 | 1176 | os.rename(bak_filename,filename) |
|
1240 | 1177 | if not backup: |
|
1241 | 1178 | try: |
|
1242 | 1179 | os.remove(bak_filename) |
|
1243 | 1180 | except: |
|
1244 | 1181 | pass |
|
1245 | 1182 | |
|
1246 | 1183 | #---------------------------------------------------------------------------- |
|
1247 | 1184 | def get_pager_cmd(pager_cmd = None): |
|
1248 | 1185 | """Return a pager command. |
|
1249 | 1186 | |
|
1250 | 1187 | Makes some attempts at finding an OS-correct one.""" |
|
1251 | 1188 | |
|
1252 | 1189 | if os.name == 'posix': |
|
1253 | 1190 | default_pager_cmd = 'less -r' # -r for color control sequences |
|
1254 | 1191 | elif os.name in ['nt','dos']: |
|
1255 | 1192 | default_pager_cmd = 'type' |
|
1256 | 1193 | |
|
1257 | 1194 | if pager_cmd is None: |
|
1258 | 1195 | try: |
|
1259 | 1196 | pager_cmd = os.environ['PAGER'] |
|
1260 | 1197 | except: |
|
1261 | 1198 | pager_cmd = default_pager_cmd |
|
1262 | 1199 | return pager_cmd |
|
1263 | 1200 | |
|
1264 | 1201 | #----------------------------------------------------------------------------- |
|
1265 | 1202 | def get_pager_start(pager,start): |
|
1266 | 1203 | """Return the string for paging files with an offset. |
|
1267 | 1204 | |
|
1268 | 1205 | This is the '+N' argument which less and more (under Unix) accept. |
|
1269 | 1206 | """ |
|
1270 | 1207 | |
|
1271 | 1208 | if pager in ['less','more']: |
|
1272 | 1209 | if start: |
|
1273 | 1210 | start_string = '+' + str(start) |
|
1274 | 1211 | else: |
|
1275 | 1212 | start_string = '' |
|
1276 | 1213 | else: |
|
1277 | 1214 | start_string = '' |
|
1278 | 1215 | return start_string |
|
1279 | 1216 | |
|
1280 | 1217 | #---------------------------------------------------------------------------- |
|
1281 | 1218 | if os.name == "nt": |
|
1282 | 1219 | import msvcrt |
|
1283 | 1220 | def page_more(): |
|
1284 | 1221 | """ Smart pausing between pages |
|
1285 | 1222 | |
|
1286 | 1223 | @return: True if need print more lines, False if quit |
|
1287 | 1224 | """ |
|
1288 | 1225 | Term.cout.write('---Return to continue, q to quit--- ') |
|
1289 | 1226 | ans = msvcrt.getch() |
|
1290 | 1227 | if ans in ("q", "Q"): |
|
1291 | 1228 | result = False |
|
1292 | 1229 | else: |
|
1293 | 1230 | result = True |
|
1294 | 1231 | Term.cout.write("\b"*37 + " "*37 + "\b"*37) |
|
1295 | 1232 | return result |
|
1296 | 1233 | else: |
|
1297 | 1234 | def page_more(): |
|
1298 | 1235 | ans = raw_input('---Return to continue, q to quit--- ') |
|
1299 | 1236 | if ans.lower().startswith('q'): |
|
1300 | 1237 | return False |
|
1301 | 1238 | else: |
|
1302 | 1239 | return True |
|
1303 | 1240 | |
|
1304 | 1241 | esc_re = re.compile(r"(\x1b[^m]+m)") |
|
1305 | 1242 | |
|
1306 | 1243 | def page_dumb(strng,start=0,screen_lines=25): |
|
1307 | 1244 | """Very dumb 'pager' in Python, for when nothing else works. |
|
1308 | 1245 | |
|
1309 | 1246 | Only moves forward, same interface as page(), except for pager_cmd and |
|
1310 | 1247 | mode.""" |
|
1311 | 1248 | |
|
1312 | 1249 | out_ln = strng.splitlines()[start:] |
|
1313 | 1250 | screens = chop(out_ln,screen_lines-1) |
|
1314 | 1251 | if len(screens) == 1: |
|
1315 | 1252 | print >>Term.cout, os.linesep.join(screens[0]) |
|
1316 | 1253 | else: |
|
1317 | 1254 | last_escape = "" |
|
1318 | 1255 | for scr in screens[0:-1]: |
|
1319 | 1256 | hunk = os.linesep.join(scr) |
|
1320 | 1257 | print >>Term.cout, last_escape + hunk |
|
1321 | 1258 | if not page_more(): |
|
1322 | 1259 | return |
|
1323 | 1260 | esc_list = esc_re.findall(hunk) |
|
1324 | 1261 | if len(esc_list) > 0: |
|
1325 | 1262 | last_escape = esc_list[-1] |
|
1326 | 1263 | print >>Term.cout, last_escape + os.linesep.join(screens[-1]) |
|
1327 | 1264 | |
|
1328 | 1265 | #---------------------------------------------------------------------------- |
|
1329 | 1266 | def page(strng,start=0,screen_lines=0,pager_cmd = None): |
|
1330 | 1267 | """Print a string, piping through a pager after a certain length. |
|
1331 | 1268 | |
|
1332 | 1269 | The screen_lines parameter specifies the number of *usable* lines of your |
|
1333 | 1270 | terminal screen (total lines minus lines you need to reserve to show other |
|
1334 | 1271 | information). |
|
1335 | 1272 | |
|
1336 | 1273 | If you set screen_lines to a number <=0, page() will try to auto-determine |
|
1337 | 1274 | your screen size and will only use up to (screen_size+screen_lines) for |
|
1338 | 1275 | printing, paging after that. That is, if you want auto-detection but need |
|
1339 | 1276 | to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for |
|
1340 | 1277 | auto-detection without any lines reserved simply use screen_lines = 0. |
|
1341 | 1278 | |
|
1342 | 1279 | If a string won't fit in the allowed lines, it is sent through the |
|
1343 | 1280 | specified pager command. If none given, look for PAGER in the environment, |
|
1344 | 1281 | and ultimately default to less. |
|
1345 | 1282 | |
|
1346 | 1283 | If no system pager works, the string is sent through a 'dumb pager' |
|
1347 | 1284 | written in python, very simplistic. |
|
1348 | 1285 | """ |
|
1349 | 1286 | |
|
1350 | 1287 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs |
|
1351 | 1288 | TERM = os.environ.get('TERM','dumb') |
|
1352 | 1289 | if TERM in ['dumb','emacs'] and os.name != 'nt': |
|
1353 | 1290 | print strng |
|
1354 | 1291 | return |
|
1355 | 1292 | # chop off the topmost part of the string we don't want to see |
|
1356 | 1293 | str_lines = strng.split(os.linesep)[start:] |
|
1357 | 1294 | str_toprint = os.linesep.join(str_lines) |
|
1358 | 1295 | num_newlines = len(str_lines) |
|
1359 | 1296 | len_str = len(str_toprint) |
|
1360 | 1297 | |
|
1361 | 1298 | # Dumb heuristics to guesstimate number of on-screen lines the string |
|
1362 | 1299 | # takes. Very basic, but good enough for docstrings in reasonable |
|
1363 | 1300 | # terminals. If someone later feels like refining it, it's not hard. |
|
1364 | 1301 | numlines = max(num_newlines,int(len_str/80)+1) |
|
1365 | 1302 | |
|
1366 | 1303 | if os.name == "nt": |
|
1367 | 1304 | screen_lines_def = get_console_size(defaulty=25)[1] |
|
1368 | 1305 | else: |
|
1369 | 1306 | screen_lines_def = 25 # default value if we can't auto-determine |
|
1370 | 1307 | |
|
1371 | 1308 | # auto-determine screen size |
|
1372 | 1309 | if screen_lines <= 0: |
|
1373 | 1310 | if TERM=='xterm': |
|
1374 | 1311 | try: |
|
1375 | 1312 | import curses |
|
1376 | 1313 | if hasattr(curses,'initscr'): |
|
1377 | 1314 | use_curses = 1 |
|
1378 | 1315 | else: |
|
1379 | 1316 | use_curses = 0 |
|
1380 | 1317 | except ImportError: |
|
1381 | 1318 | use_curses = 0 |
|
1382 | 1319 | else: |
|
1383 | 1320 | # curses causes problems on many terminals other than xterm. |
|
1384 | 1321 | use_curses = 0 |
|
1385 | 1322 | if use_curses: |
|
1386 | 1323 | scr = curses.initscr() |
|
1387 | 1324 | screen_lines_real,screen_cols = scr.getmaxyx() |
|
1388 | 1325 | curses.endwin() |
|
1389 | 1326 | screen_lines += screen_lines_real |
|
1390 | 1327 | #print '***Screen size:',screen_lines_real,'lines x',\ |
|
1391 | 1328 | #screen_cols,'columns.' # dbg |
|
1392 | 1329 | else: |
|
1393 | 1330 | screen_lines += screen_lines_def |
|
1394 | 1331 | |
|
1395 | 1332 | #print 'numlines',numlines,'screenlines',screen_lines # dbg |
|
1396 | 1333 | if numlines <= screen_lines : |
|
1397 | 1334 | #print '*** normal print' # dbg |
|
1398 | 1335 | print >>Term.cout, str_toprint |
|
1399 | 1336 | else: |
|
1400 | 1337 | # Try to open pager and default to internal one if that fails. |
|
1401 | 1338 | # All failure modes are tagged as 'retval=1', to match the return |
|
1402 | 1339 | # value of a failed system command. If any intermediate attempt |
|
1403 | 1340 | # sets retval to 1, at the end we resort to our own page_dumb() pager. |
|
1404 | 1341 | pager_cmd = get_pager_cmd(pager_cmd) |
|
1405 | 1342 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
1406 | 1343 | if os.name == 'nt': |
|
1407 | 1344 | if pager_cmd.startswith('type'): |
|
1408 | 1345 | # The default WinXP 'type' command is failing on complex strings. |
|
1409 | 1346 | retval = 1 |
|
1410 | 1347 | else: |
|
1411 | 1348 | tmpname = tempfile.mktemp('.txt') |
|
1412 | 1349 | tmpfile = file(tmpname,'wt') |
|
1413 | 1350 | tmpfile.write(strng) |
|
1414 | 1351 | tmpfile.close() |
|
1415 | 1352 | cmd = "%s < %s" % (pager_cmd,tmpname) |
|
1416 | 1353 | if os.system(cmd): |
|
1417 | 1354 | retval = 1 |
|
1418 | 1355 | else: |
|
1419 | 1356 | retval = None |
|
1420 | 1357 | os.remove(tmpname) |
|
1421 | 1358 | else: |
|
1422 | 1359 | try: |
|
1423 | 1360 | retval = None |
|
1424 | 1361 | # if I use popen4, things hang. No idea why. |
|
1425 | 1362 | #pager,shell_out = os.popen4(pager_cmd) |
|
1426 | 1363 | pager = os.popen(pager_cmd,'w') |
|
1427 | 1364 | pager.write(strng) |
|
1428 | 1365 | pager.close() |
|
1429 | 1366 | retval = pager.close() # success returns None |
|
1430 | 1367 | except IOError,msg: # broken pipe when user quits |
|
1431 | 1368 | if msg.args == (32,'Broken pipe'): |
|
1432 | 1369 | retval = None |
|
1433 | 1370 | else: |
|
1434 | 1371 | retval = 1 |
|
1435 | 1372 | except OSError: |
|
1436 | 1373 | # Other strange problems, sometimes seen in Win2k/cygwin |
|
1437 | 1374 | retval = 1 |
|
1438 | 1375 | if retval is not None: |
|
1439 | 1376 | page_dumb(strng,screen_lines=screen_lines) |
|
1440 | 1377 | |
|
1441 | 1378 | #---------------------------------------------------------------------------- |
|
1442 | 1379 | def page_file(fname,start = 0, pager_cmd = None): |
|
1443 | 1380 | """Page a file, using an optional pager command and starting line. |
|
1444 | 1381 | """ |
|
1445 | 1382 | |
|
1446 | 1383 | pager_cmd = get_pager_cmd(pager_cmd) |
|
1447 | 1384 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
1448 | 1385 | |
|
1449 | 1386 | try: |
|
1450 | 1387 | if os.environ['TERM'] in ['emacs','dumb']: |
|
1451 | 1388 | raise EnvironmentError |
|
1452 | 1389 | xsys(pager_cmd + ' ' + fname) |
|
1453 | 1390 | except: |
|
1454 | 1391 | try: |
|
1455 | 1392 | if start > 0: |
|
1456 | 1393 | start -= 1 |
|
1457 | 1394 | page(open(fname).read(),start) |
|
1458 | 1395 | except: |
|
1459 | 1396 | print 'Unable to show file',`fname` |
|
1460 | 1397 | |
|
1461 | 1398 | #---------------------------------------------------------------------------- |
|
1462 | 1399 | def snip_print(str,width = 75,print_full = 0,header = ''): |
|
1463 | 1400 | """Print a string snipping the midsection to fit in width. |
|
1464 | 1401 | |
|
1465 | 1402 | print_full: mode control: |
|
1466 | 1403 | - 0: only snip long strings |
|
1467 | 1404 | - 1: send to page() directly. |
|
1468 | 1405 | - 2: snip long strings and ask for full length viewing with page() |
|
1469 | 1406 | Return 1 if snipping was necessary, 0 otherwise.""" |
|
1470 | 1407 | |
|
1471 | 1408 | if print_full == 1: |
|
1472 | 1409 | page(header+str) |
|
1473 | 1410 | return 0 |
|
1474 | 1411 | |
|
1475 | 1412 | print header, |
|
1476 | 1413 | if len(str) < width: |
|
1477 | 1414 | print str |
|
1478 | 1415 | snip = 0 |
|
1479 | 1416 | else: |
|
1480 | 1417 | whalf = int((width -5)/2) |
|
1481 | 1418 | print str[:whalf] + ' <...> ' + str[-whalf:] |
|
1482 | 1419 | snip = 1 |
|
1483 | 1420 | if snip and print_full == 2: |
|
1484 | 1421 | if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y': |
|
1485 | 1422 | page(str) |
|
1486 | 1423 | return snip |
|
1487 | 1424 | |
|
1488 | 1425 | #**************************************************************************** |
|
1489 | 1426 | # lists, dicts and structures |
|
1490 | 1427 | |
|
1491 | 1428 | def belong(candidates,checklist): |
|
1492 | 1429 | """Check whether a list of items appear in a given list of options. |
|
1493 | 1430 | |
|
1494 | 1431 | Returns a list of 1 and 0, one for each candidate given.""" |
|
1495 | 1432 | |
|
1496 | 1433 | return [x in checklist for x in candidates] |
|
1497 | 1434 | |
|
1498 | 1435 | #---------------------------------------------------------------------------- |
|
1499 | 1436 | def uniq_stable(elems): |
|
1500 | 1437 | """uniq_stable(elems) -> list |
|
1501 | 1438 | |
|
1502 | 1439 | Return from an iterable, a list of all the unique elements in the input, |
|
1503 | 1440 | but maintaining the order in which they first appear. |
|
1504 | 1441 | |
|
1505 | 1442 | A naive solution to this problem which just makes a dictionary with the |
|
1506 | 1443 | elements as keys fails to respect the stability condition, since |
|
1507 | 1444 | dictionaries are unsorted by nature. |
|
1508 | 1445 | |
|
1509 | 1446 | Note: All elements in the input must be valid dictionary keys for this |
|
1510 | 1447 | routine to work, as it internally uses a dictionary for efficiency |
|
1511 | 1448 | reasons.""" |
|
1512 | 1449 | |
|
1513 | 1450 | unique = [] |
|
1514 | 1451 | unique_dict = {} |
|
1515 | 1452 | for nn in elems: |
|
1516 | 1453 | if nn not in unique_dict: |
|
1517 | 1454 | unique.append(nn) |
|
1518 | 1455 | unique_dict[nn] = None |
|
1519 | 1456 | return unique |
|
1520 | 1457 | |
|
1521 | 1458 | #---------------------------------------------------------------------------- |
|
1522 | 1459 | class NLprinter: |
|
1523 | 1460 | """Print an arbitrarily nested list, indicating index numbers. |
|
1524 | 1461 | |
|
1525 | 1462 | An instance of this class called nlprint is available and callable as a |
|
1526 | 1463 | function. |
|
1527 | 1464 | |
|
1528 | 1465 | nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent' |
|
1529 | 1466 | and using 'sep' to separate the index from the value. """ |
|
1530 | 1467 | |
|
1531 | 1468 | def __init__(self): |
|
1532 | 1469 | self.depth = 0 |
|
1533 | 1470 | |
|
1534 | 1471 | def __call__(self,lst,pos='',**kw): |
|
1535 | 1472 | """Prints the nested list numbering levels.""" |
|
1536 | 1473 | kw.setdefault('indent',' ') |
|
1537 | 1474 | kw.setdefault('sep',': ') |
|
1538 | 1475 | kw.setdefault('start',0) |
|
1539 | 1476 | kw.setdefault('stop',len(lst)) |
|
1540 | 1477 | # we need to remove start and stop from kw so they don't propagate |
|
1541 | 1478 | # into a recursive call for a nested list. |
|
1542 | 1479 | start = kw['start']; del kw['start'] |
|
1543 | 1480 | stop = kw['stop']; del kw['stop'] |
|
1544 | 1481 | if self.depth == 0 and 'header' in kw.keys(): |
|
1545 | 1482 | print kw['header'] |
|
1546 | 1483 | |
|
1547 | 1484 | for idx in range(start,stop): |
|
1548 | 1485 | elem = lst[idx] |
|
1549 | 1486 | if type(elem)==type([]): |
|
1550 | 1487 | self.depth += 1 |
|
1551 | 1488 | self.__call__(elem,itpl('$pos$idx,'),**kw) |
|
1552 | 1489 | self.depth -= 1 |
|
1553 | 1490 | else: |
|
1554 | 1491 | printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem') |
|
1555 | 1492 | |
|
1556 | 1493 | nlprint = NLprinter() |
|
1557 | 1494 | #---------------------------------------------------------------------------- |
|
1558 | 1495 | def all_belong(candidates,checklist): |
|
1559 | 1496 | """Check whether a list of items ALL appear in a given list of options. |
|
1560 | 1497 | |
|
1561 | 1498 | Returns a single 1 or 0 value.""" |
|
1562 | 1499 | |
|
1563 | 1500 | return 1-(0 in [x in checklist for x in candidates]) |
|
1564 | 1501 | |
|
1565 | 1502 | #---------------------------------------------------------------------------- |
|
1566 | 1503 | def sort_compare(lst1,lst2,inplace = 1): |
|
1567 | 1504 | """Sort and compare two lists. |
|
1568 | 1505 | |
|
1569 | 1506 | By default it does it in place, thus modifying the lists. Use inplace = 0 |
|
1570 | 1507 | to avoid that (at the cost of temporary copy creation).""" |
|
1571 | 1508 | if not inplace: |
|
1572 | 1509 | lst1 = lst1[:] |
|
1573 | 1510 | lst2 = lst2[:] |
|
1574 | 1511 | lst1.sort(); lst2.sort() |
|
1575 | 1512 | return lst1 == lst2 |
|
1576 | 1513 | |
|
1577 | 1514 | #---------------------------------------------------------------------------- |
|
1578 | 1515 | def mkdict(**kwargs): |
|
1579 | 1516 | """Return a dict from a keyword list. |
|
1580 | 1517 | |
|
1581 | 1518 | It's just syntactic sugar for making ditcionary creation more convenient: |
|
1582 | 1519 | # the standard way |
|
1583 | 1520 | >>>data = { 'red' : 1, 'green' : 2, 'blue' : 3 } |
|
1584 | 1521 | # a cleaner way |
|
1585 | 1522 | >>>data = dict(red=1, green=2, blue=3) |
|
1586 | 1523 | |
|
1587 | 1524 | If you need more than this, look at the Struct() class.""" |
|
1588 | 1525 | |
|
1589 | 1526 | return kwargs |
|
1590 | 1527 | |
|
1591 | 1528 | #---------------------------------------------------------------------------- |
|
1592 | 1529 | def list2dict(lst): |
|
1593 | 1530 | """Takes a list of (key,value) pairs and turns it into a dict.""" |
|
1594 | 1531 | |
|
1595 | 1532 | dic = {} |
|
1596 | 1533 | for k,v in lst: dic[k] = v |
|
1597 | 1534 | return dic |
|
1598 | 1535 | |
|
1599 | 1536 | #---------------------------------------------------------------------------- |
|
1600 | 1537 | def list2dict2(lst,default=''): |
|
1601 | 1538 | """Takes a list and turns it into a dict. |
|
1602 | 1539 | Much slower than list2dict, but more versatile. This version can take |
|
1603 | 1540 | lists with sublists of arbitrary length (including sclars).""" |
|
1604 | 1541 | |
|
1605 | 1542 | dic = {} |
|
1606 | 1543 | for elem in lst: |
|
1607 | 1544 | if type(elem) in (types.ListType,types.TupleType): |
|
1608 | 1545 | size = len(elem) |
|
1609 | 1546 | if size == 0: |
|
1610 | 1547 | pass |
|
1611 | 1548 | elif size == 1: |
|
1612 | 1549 | dic[elem] = default |
|
1613 | 1550 | else: |
|
1614 | 1551 | k,v = elem[0], elem[1:] |
|
1615 | 1552 | if len(v) == 1: v = v[0] |
|
1616 | 1553 | dic[k] = v |
|
1617 | 1554 | else: |
|
1618 | 1555 | dic[elem] = default |
|
1619 | 1556 | return dic |
|
1620 | 1557 | |
|
1621 | 1558 | #---------------------------------------------------------------------------- |
|
1622 | 1559 | def flatten(seq): |
|
1623 | 1560 | """Flatten a list of lists (NOT recursive, only works for 2d lists).""" |
|
1624 | 1561 | |
|
1625 | # bug in python??? (YES. Fixed in 2.2, let's leave the kludgy fix in). | |
|
1626 | ||
|
1627 | # if the x=0 isn't made, a *global* variable x is left over after calling | |
|
1628 | # this function, with the value of the last element in the return | |
|
1629 | # list. This does seem like a bug big time to me. | |
|
1630 | ||
|
1631 | # the problem is fixed with the x=0, which seems to force the creation of | |
|
1632 | # a local name | |
|
1633 | ||
|
1634 | x = 0 | |
|
1635 | 1562 | return [x for subseq in seq for x in subseq] |
|
1636 | 1563 | |
|
1637 | 1564 | #---------------------------------------------------------------------------- |
|
1638 | 1565 | def get_slice(seq,start=0,stop=None,step=1): |
|
1639 | 1566 | """Get a slice of a sequence with variable step. Specify start,stop,step.""" |
|
1640 | 1567 | if stop == None: |
|
1641 | 1568 | stop = len(seq) |
|
1642 | 1569 | item = lambda i: seq[i] |
|
1643 | 1570 | return map(item,xrange(start,stop,step)) |
|
1644 | 1571 | |
|
1645 | 1572 | #---------------------------------------------------------------------------- |
|
1646 | 1573 | def chop(seq,size): |
|
1647 | 1574 | """Chop a sequence into chunks of the given size.""" |
|
1648 | 1575 | chunk = lambda i: seq[i:i+size] |
|
1649 | 1576 | return map(chunk,xrange(0,len(seq),size)) |
|
1650 | 1577 | |
|
1651 | 1578 | #---------------------------------------------------------------------------- |
|
1652 | 1579 | def with(object, **args): |
|
1653 | 1580 | """Set multiple attributes for an object, similar to Pascal's with. |
|
1654 | 1581 | |
|
1655 | 1582 | Example: |
|
1656 | 1583 | with(jim, |
|
1657 | 1584 | born = 1960, |
|
1658 | 1585 | haircolour = 'Brown', |
|
1659 | 1586 | eyecolour = 'Green') |
|
1660 | 1587 | |
|
1661 | 1588 | Credit: Greg Ewing, in |
|
1662 | 1589 | http://mail.python.org/pipermail/python-list/2001-May/040703.html""" |
|
1663 | 1590 | |
|
1664 | 1591 | object.__dict__.update(args) |
|
1665 | 1592 | |
|
1666 | 1593 | #---------------------------------------------------------------------------- |
|
1667 | 1594 | def setattr_list(obj,alist,nspace = None): |
|
1668 | 1595 | """Set a list of attributes for an object taken from a namespace. |
|
1669 | 1596 | |
|
1670 | 1597 | setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in |
|
1671 | 1598 | alist with their values taken from nspace, which must be a dict (something |
|
1672 | 1599 | like locals() will often do) If nspace isn't given, locals() of the |
|
1673 | 1600 | *caller* is used, so in most cases you can omit it. |
|
1674 | 1601 | |
|
1675 | 1602 | Note that alist can be given as a string, which will be automatically |
|
1676 | 1603 | split into a list on whitespace. If given as a list, it must be a list of |
|
1677 | 1604 | *strings* (the variable names themselves), not of variables.""" |
|
1678 | 1605 | |
|
1679 | 1606 | # this grabs the local variables from the *previous* call frame -- that is |
|
1680 | 1607 | # the locals from the function that called setattr_list(). |
|
1681 | 1608 | # - snipped from weave.inline() |
|
1682 | 1609 | if nspace is None: |
|
1683 | 1610 | call_frame = sys._getframe().f_back |
|
1684 | 1611 | nspace = call_frame.f_locals |
|
1685 | 1612 | |
|
1686 | 1613 | if type(alist) in StringTypes: |
|
1687 | 1614 | alist = alist.split() |
|
1688 | 1615 | for attr in alist: |
|
1689 | 1616 | val = eval(attr,nspace) |
|
1690 | 1617 | setattr(obj,attr,val) |
|
1691 | 1618 | |
|
1692 | 1619 | #---------------------------------------------------------------------------- |
|
1693 | 1620 | def getattr_list(obj,alist,*args): |
|
1694 | 1621 | """getattr_list(obj,alist[, default]) -> attribute list. |
|
1695 | 1622 | |
|
1696 | 1623 | Get a list of named attributes for an object. When a default argument is |
|
1697 | 1624 | given, it is returned when the attribute doesn't exist; without it, an |
|
1698 | 1625 | exception is raised in that case. |
|
1699 | 1626 | |
|
1700 | 1627 | Note that alist can be given as a string, which will be automatically |
|
1701 | 1628 | split into a list on whitespace. If given as a list, it must be a list of |
|
1702 | 1629 | *strings* (the variable names themselves), not of variables.""" |
|
1703 | 1630 | |
|
1704 | 1631 | if type(alist) in StringTypes: |
|
1705 | 1632 | alist = alist.split() |
|
1706 | 1633 | if args: |
|
1707 | 1634 | if len(args)==1: |
|
1708 | 1635 | default = args[0] |
|
1709 | 1636 | return map(lambda attr: getattr(obj,attr,default),alist) |
|
1710 | 1637 | else: |
|
1711 | 1638 | raise ValueError,'getattr_list() takes only one optional argument' |
|
1712 | 1639 | else: |
|
1713 | 1640 | return map(lambda attr: getattr(obj,attr),alist) |
|
1714 | 1641 | |
|
1715 | 1642 | #---------------------------------------------------------------------------- |
|
1716 | 1643 | def map_method(method,object_list,*argseq,**kw): |
|
1717 | 1644 | """map_method(method,object_list,*args,**kw) -> list |
|
1718 | 1645 | |
|
1719 | 1646 | Return a list of the results of applying the methods to the items of the |
|
1720 | 1647 | argument sequence(s). If more than one sequence is given, the method is |
|
1721 | 1648 | called with an argument list consisting of the corresponding item of each |
|
1722 | 1649 | sequence. All sequences must be of the same length. |
|
1723 | 1650 | |
|
1724 | 1651 | Keyword arguments are passed verbatim to all objects called. |
|
1725 | 1652 | |
|
1726 | 1653 | This is Python code, so it's not nearly as fast as the builtin map().""" |
|
1727 | 1654 | |
|
1728 | 1655 | out_list = [] |
|
1729 | 1656 | idx = 0 |
|
1730 | 1657 | for object in object_list: |
|
1731 | 1658 | try: |
|
1732 | 1659 | handler = getattr(object, method) |
|
1733 | 1660 | except AttributeError: |
|
1734 | 1661 | out_list.append(None) |
|
1735 | 1662 | else: |
|
1736 | 1663 | if argseq: |
|
1737 | 1664 | args = map(lambda lst:lst[idx],argseq) |
|
1738 | 1665 | #print 'ob',object,'hand',handler,'ar',args # dbg |
|
1739 | 1666 | out_list.append(handler(args,**kw)) |
|
1740 | 1667 | else: |
|
1741 | 1668 | out_list.append(handler(**kw)) |
|
1742 | 1669 | idx += 1 |
|
1743 | 1670 | return out_list |
|
1744 | 1671 | |
|
1745 | 1672 | #---------------------------------------------------------------------------- |
|
1746 | 1673 | def import_fail_info(mod_name,fns=None): |
|
1747 | 1674 | """Inform load failure for a module.""" |
|
1748 | 1675 | |
|
1749 | 1676 | if fns == None: |
|
1750 | 1677 | warn("Loading of %s failed.\n" % (mod_name,)) |
|
1751 | 1678 | else: |
|
1752 | 1679 | warn("Loading of %s from %s failed.\n" % (fns,mod_name)) |
|
1753 | 1680 | |
|
1754 | 1681 | #---------------------------------------------------------------------------- |
|
1755 | 1682 | # Proposed popitem() extension, written as a method |
|
1756 | 1683 | |
|
1757 | 1684 | class NotGiven: pass |
|
1758 | 1685 | |
|
1759 | 1686 | def popkey(dct,key,default=NotGiven): |
|
1760 | 1687 | """Return dct[key] and delete dct[key]. |
|
1761 | 1688 | |
|
1762 | 1689 | If default is given, return it if dct[key] doesn't exist, otherwise raise |
|
1763 | 1690 | KeyError. """ |
|
1764 | 1691 | |
|
1765 | 1692 | try: |
|
1766 | 1693 | val = dct[key] |
|
1767 | 1694 | except KeyError: |
|
1768 | 1695 | if default is NotGiven: |
|
1769 | 1696 | raise |
|
1770 | 1697 | else: |
|
1771 | 1698 | return default |
|
1772 | 1699 | else: |
|
1773 | 1700 | del dct[key] |
|
1774 | 1701 | return val |
|
1775 | 1702 | #*************************** end of file <genutils.py> ********************** |
|
1776 | 1703 |
@@ -1,203 +1,321 b'' | |||
|
1 | 1 | ''' IPython customization API |
|
2 | 2 | |
|
3 | 3 | Your one-stop module for configuring & extending ipython |
|
4 | 4 | |
|
5 | 5 | The API will probably break when ipython 1.0 is released, but so |
|
6 | 6 | will the other configuration method (rc files). |
|
7 | 7 | |
|
8 | 8 | All names prefixed by underscores are for internal use, not part |
|
9 | 9 | of the public api. |
|
10 | 10 | |
|
11 | 11 | Below is an example that you can just put to a module and import from ipython. |
|
12 | 12 | |
|
13 | 13 | A good practice is to install the config script below as e.g. |
|
14 | 14 | |
|
15 | 15 | ~/.ipython/my_private_conf.py |
|
16 | 16 | |
|
17 | 17 | And do |
|
18 | 18 | |
|
19 | 19 | import_mod my_private_conf |
|
20 | 20 | |
|
21 | 21 | in ~/.ipython/ipythonrc |
|
22 | 22 | |
|
23 | 23 | That way the module is imported at startup and you can have all your |
|
24 | 24 | personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME |
|
25 | 25 | stuff) in there. |
|
26 | 26 | |
|
27 | 27 | ----------------------------------------------- |
|
28 |
import IPython.ipapi |
|
|
28 | import IPython.ipapi | |
|
29 | ip = IPython.ipapi.get() | |
|
29 | 30 | |
|
30 | 31 | def ankka_f(self, arg): |
|
31 | 32 | print "Ankka",self,"says uppercase:",arg.upper() |
|
32 | 33 | |
|
33 | 34 | ip.expose_magic("ankka",ankka_f) |
|
34 | 35 | |
|
35 | 36 | ip.magic('alias sayhi echo "Testing, hi ok"') |
|
36 | 37 | ip.magic('alias helloworld echo "Hello world"') |
|
37 | 38 | ip.system('pwd') |
|
38 | 39 | |
|
39 | 40 | ip.ex('import re') |
|
40 | 41 | ip.ex(""" |
|
41 | 42 | def funcci(a,b): |
|
42 | 43 | print a+b |
|
43 | 44 | print funcci(3,4) |
|
44 | 45 | """) |
|
45 | 46 | ip.ex("funcci(348,9)") |
|
46 | 47 | |
|
47 | 48 | def jed_editor(self,filename, linenum=None): |
|
48 | 49 | print "Calling my own editor, jed ... via hook!" |
|
49 | 50 | import os |
|
50 | 51 | if linenum is None: linenum = 0 |
|
51 | 52 | os.system('jed +%d %s' % (linenum, filename)) |
|
52 | 53 | print "exiting jed" |
|
53 | 54 | |
|
54 | 55 | ip.set_hook('editor',jed_editor) |
|
55 | 56 | |
|
56 |
o = ip.options |
|
|
57 | o = ip.options | |
|
57 | 58 | o.autocall = 2 # FULL autocall mode |
|
58 | 59 | |
|
59 | 60 | print "done!" |
|
60 | ||
|
61 | 61 | ''' |
|
62 | 62 | |
|
63 | # stdlib imports | |
|
64 | import sys | |
|
65 | ||
|
66 | # our own | |
|
67 | from IPython.genutils import warn,error | |
|
63 | 68 | |
|
64 | 69 | class TryNext(Exception): |
|
65 | 70 |
""" |
|
66 | 71 | |
|
67 |
|
|
|
68 |
|
|
|
69 | If you pass arguments to the constructor those arguments will be | |
|
70 | used by the next hook instead of the original ones. | |
|
72 | Raise this in your hook function to indicate that the next hook handler | |
|
73 | should be used to handle the operation. If you pass arguments to the | |
|
74 | constructor those arguments will be used by the next hook instead of the | |
|
75 | original ones. | |
|
71 | 76 | """ |
|
72 | 77 | |
|
73 | 78 | def __init__(self, *args, **kwargs): |
|
74 | 79 | self.args = args |
|
75 | 80 | self.kwargs = kwargs |
|
76 | 81 | |
|
77 | ||
|
78 | 82 | # contains the most recently instantiated IPApi |
|
79 | _recent = None | |
|
83 | ||
|
84 | class IPythonNotRunning: | |
|
85 | """Dummy do-nothing class. | |
|
86 | ||
|
87 | Instances of this class return a dummy attribute on all accesses, which | |
|
88 | can be called and warns. This makes it easier to write scripts which use | |
|
89 | the ipapi.get() object for informational purposes to operate both with and | |
|
90 | without ipython. Obviously code which uses the ipython object for | |
|
91 | computations will not work, but this allows a wider range of code to | |
|
92 | transparently work whether ipython is being used or not.""" | |
|
93 | ||
|
94 | def __str__(self): | |
|
95 | return "<IPythonNotRunning>" | |
|
96 | ||
|
97 | __repr__ = __str__ | |
|
98 | ||
|
99 | def __getattr__(self,name): | |
|
100 | return self.dummy | |
|
101 | ||
|
102 | def dummy(self,*args,**kw): | |
|
103 | """Dummy function, which doesn't do anything but warn.""" | |
|
104 | warn("IPython is not running, this is a dummy no-op function") | |
|
105 | ||
|
106 | _recent = IPythonNotRunning() | |
|
80 | 107 | |
|
81 | 108 | def get(): |
|
82 | """ Get an IPApi object, or None if not running under ipython | |
|
109 | """Get an IPApi object. | |
|
83 | 110 | |
|
84 | Running this should be the first thing you do when writing | |
|
85 | extensions that can be imported as normal modules. You can then | |
|
86 | direct all the configuration operations against the returned | |
|
87 | object. | |
|
111 | Returns an instance of IPythonNotRunning if not running under IPython. | |
|
88 | 112 | |
|
113 | Running this should be the first thing you do when writing extensions that | |
|
114 | can be imported as normal modules. You can then direct all the | |
|
115 | configuration operations against the returned object. | |
|
89 | 116 | """ |
|
90 | 117 | |
|
91 | 118 | return _recent |
|
92 | 119 | |
|
93 | ||
|
94 | ||
|
95 | 120 | class IPApi: |
|
96 | 121 | """ The actual API class for configuring IPython |
|
97 | 122 | |
|
98 | You should do all of the IPython configuration by getting | |
|
99 |
|
|
|
100 | methods. | |
|
123 | You should do all of the IPython configuration by getting an IPApi object | |
|
124 | with IPython.ipapi.get() and using the attributes and methods of the | |
|
125 | returned object.""" | |
|
101 | 126 | |
|
102 | """ | |
|
103 | 127 | def __init__(self,ip): |
|
104 | 128 | |
|
129 | # All attributes exposed here are considered to be the public API of | |
|
130 | # IPython. As needs dictate, some of these may be wrapped as | |
|
131 | # properties. | |
|
132 | ||
|
105 | 133 | self.magic = ip.ipmagic |
|
106 | 134 | |
|
107 | 135 | self.system = ip.ipsystem |
|
108 | 136 | |
|
109 | 137 | self.set_hook = ip.set_hook |
|
110 | 138 | |
|
111 | 139 | self.set_custom_exc = ip.set_custom_exc |
|
112 | 140 | |
|
141 | self.user_ns = ip.user_ns | |
|
142 | ||
|
143 | # Session-specific data store, which can be used to store | |
|
144 | # data that should persist through the ipython session. | |
|
145 | self.meta = ip.meta | |
|
146 | ||
|
147 | # The ipython instance provided | |
|
113 | 148 | self.IP = ip |
|
149 | ||
|
114 | 150 | global _recent |
|
115 | 151 | _recent = self |
|
116 | 152 | |
|
153 | # Use a property for some things which are added to the instance very | |
|
154 | # late. I don't have time right now to disentangle the initialization | |
|
155 | # order issues, so a property lets us delay item extraction while | |
|
156 | # providing a normal attribute API. | |
|
157 | def get_db(self): | |
|
158 | """A handle to persistent dict-like database (a PickleShareDB object)""" | |
|
159 | return self.IP.db | |
|
117 | 160 | |
|
161 | db = property(get_db,None,None,get_db.__doc__) | |
|
118 | 162 | |
|
119 | def options(self): | |
|
120 |
""" |
|
|
163 | def get_options(self): | |
|
164 | """All configurable variables.""" | |
|
121 | 165 | return self.IP.rc |
|
122 | 166 | |
|
123 | def user_ns(self): | |
|
124 | return self.IP.user_ns | |
|
167 | options = property(get_options,None,None,get_options.__doc__) | |
|
125 | 168 | |
|
126 | 169 | def expose_magic(self,magicname, func): |
|
127 | 170 | ''' Expose own function as magic function for ipython |
|
128 | 171 | |
|
129 | 172 | def foo_impl(self,parameter_s=''): |
|
130 | 173 | """My very own magic!. (Use docstrings, IPython reads them).""" |
|
131 | 174 | print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>' |
|
132 | 175 | print 'The self object is:',self |
|
133 | 176 | |
|
134 | 177 | ipapi.expose_magic("foo",foo_impl) |
|
135 | 178 | ''' |
|
136 | 179 | |
|
137 | 180 | import new |
|
138 | 181 | im = new.instancemethod(func,self.IP, self.IP.__class__) |
|
139 | 182 | setattr(self.IP, "magic_" + magicname, im) |
|
140 | 183 | |
|
141 | ||
|
142 | 184 | def ex(self,cmd): |
|
143 | 185 | """ Execute a normal python statement in user namespace """ |
|
144 |
exec cmd in self.user_ns |
|
|
186 | exec cmd in self.user_ns | |
|
145 | 187 | |
|
146 | 188 | def ev(self,expr): |
|
147 | 189 | """ Evaluate python expression expr in user namespace |
|
148 | 190 | |
|
149 | 191 | Returns the result of evaluation""" |
|
150 |
return eval(expr,self.user_ns |
|
|
151 | ||
|
152 | def meta(self): | |
|
153 | """ Get a session-specific data store | |
|
154 | ||
|
155 | Object returned by this method can be used to store | |
|
156 | data that should persist through the ipython session. | |
|
157 | """ | |
|
158 | return self.IP.meta | |
|
159 | ||
|
160 | def getdb(self): | |
|
161 | """ Return a handle to persistent dict-like database | |
|
192 | return eval(expr,self.user_ns) | |
|
162 | 193 |
|
|
163 | Return a PickleShareDB object. | |
|
164 | """ | |
|
165 | return self.IP.db | |
|
166 | 194 | def runlines(self,lines): |
|
167 | 195 | """ Run the specified lines in interpreter, honoring ipython directives. |
|
168 | 196 | |
|
169 | 197 | This allows %magic and !shell escape notations. |
|
170 | 198 | |
|
171 | 199 | Takes either all lines in one string or list of lines. |
|
172 | 200 | """ |
|
173 | 201 | if isinstance(lines,basestring): |
|
174 | 202 | self.IP.runlines(lines) |
|
175 | 203 | else: |
|
176 | 204 | self.IP.runlines('\n'.join(lines)) |
|
177 | 205 | |
|
206 | def to_user_ns(self,*vars): | |
|
207 | """Inject a group of variables into the IPython user namespace. | |
|
208 | ||
|
209 | Inputs: | |
|
210 | ||
|
211 | - *vars: one or more variables from the caller's namespace to be put | |
|
212 | into the interactive IPython namespace. The arguments can be given | |
|
213 | in one of two forms, but ALL arguments must follow the same | |
|
214 | convention (the first is checked and the rest are assumed to follow | |
|
215 | it): | |
|
216 | ||
|
217 | a) All strings, naming variables in the caller. These names are | |
|
218 | evaluated in the caller's frame and put in, with the same name, in | |
|
219 | the IPython namespace. | |
|
220 | ||
|
221 | b) Pairs of (name, value), where the name is a string (a valid | |
|
222 | python identifier). In this case, the value is put into the | |
|
223 | IPython namespace labeled by the given name. This allows you to | |
|
224 | rename your local variables so they don't collide with other names | |
|
225 | you may already be using globally, or elsewhere and which you also | |
|
226 | want to propagate. | |
|
227 | ||
|
228 | ||
|
229 | This utility routine is meant to ease interactive debugging work, | |
|
230 | where you want to easily propagate some internal variable in your code | |
|
231 | up to the interactive namespace for further exploration. | |
|
232 | ||
|
233 | When you run code via %run, globals in your script become visible at | |
|
234 | the interactive prompt, but this doesn't happen for locals inside your | |
|
235 | own functions and methods. Yet when debugging, it is common to want | |
|
236 | to explore some internal variables further at the interactive propmt. | |
|
237 | ||
|
238 | Examples: | |
|
239 | ||
|
240 | To use this, you first must obtain a handle on the ipython object as | |
|
241 | indicated above, via: | |
|
242 | ||
|
243 | import IPython.ipapi | |
|
244 | ip = IPython.ipapi.get() | |
|
245 | ||
|
246 | Once this is done, inside a routine foo() where you want to expose | |
|
247 | variables x and y, you do the following: | |
|
248 | ||
|
249 | def foo(): | |
|
250 | ... | |
|
251 | x = your_computation() | |
|
252 | y = something_else() | |
|
253 | ||
|
254 | # This pushes x and y to the interactive prompt immediately, even | |
|
255 | # if this routine crashes on the next line after: | |
|
256 | ip.to_user_ns('x','y') | |
|
257 | ... | |
|
258 | # return | |
|
259 | ||
|
260 | The following example shows you how to rename variables to avoid | |
|
261 | clashes: | |
|
262 | ||
|
263 | def bar(): | |
|
264 | ... | |
|
265 | x,y,z,w = foo() | |
|
266 | ||
|
267 | # Push these variables with different names, so they don't | |
|
268 | # overwrite x and y from before | |
|
269 | ip.to_user_ns(('x1',x),('y1',y),('z1',z),('w1',w)) | |
|
270 | # which is more conveniently written as: | |
|
271 | ip.to_user_ns(*zip(('x1','y1','z1','w1'),(x,y,z,w))) | |
|
272 | ||
|
273 | ... | |
|
274 | # return """ | |
|
275 | ||
|
276 | # print 'vars given:',vars # dbg | |
|
277 | # Get the caller's frame to evaluate the given names in | |
|
278 | cf = sys._getframe(1) | |
|
279 | ||
|
280 | # XXX fix this after Ville replies... | |
|
281 | user_ns = self.user_ns | |
|
282 | ||
|
283 | if isinstance(vars[0],basestring): | |
|
284 | # assume that all variables are given as strings | |
|
285 | try: | |
|
286 | for name in vars: | |
|
287 | user_ns[name] = eval(name,cf.f_globals,cf.f_locals) | |
|
288 | except: | |
|
289 | error('could not get var. %s from %s' % | |
|
290 | (name,cf.f_code.co_name)) | |
|
291 | ||
|
292 | else: | |
|
293 | # assume they are all given as pairs of name,object | |
|
294 | user_ns.update(dict(vars)) | |
|
295 | ||
|
178 | 296 | |
|
179 | 297 | def launch_new_instance(user_ns = None): |
|
180 | 298 | """ Create and start a new ipython instance. |
|
181 | 299 | |
|
182 | 300 | This can be called even without having an already initialized |
|
183 | 301 | ipython session running. |
|
184 | 302 | |
|
185 | 303 | This is also used as the egg entry point for the 'ipython' script. |
|
186 | 304 | |
|
187 | 305 | """ |
|
188 | 306 | ses = create_session(user_ns) |
|
189 | 307 | ses.mainloop() |
|
190 | 308 | |
|
191 | 309 | |
|
192 | 310 | def create_session(user_ns = None): |
|
193 | 311 | """ Creates, but does not launch an IPython session. |
|
194 | 312 | |
|
195 | 313 | Later on you can call obj.mainloop() on the returned object. |
|
196 | 314 | |
|
197 | 315 | This should *not* be run when a session exists already. |
|
198 | 316 | |
|
199 | 317 | """ |
|
200 | 318 | if user_ns is not None: |
|
201 | 319 | user_ns["__name__"] = user_ns.get("__name__",'ipy_session') |
|
202 | 320 | import IPython |
|
203 | 321 | return IPython.Shell.start(user_ns = user_ns) |
@@ -1,2287 +1,2284 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | IPython -- An enhanced Interactive Python |
|
4 | 4 | |
|
5 | 5 | Requires Python 2.3 or newer. |
|
6 | 6 | |
|
7 | 7 | This file contains all the classes and helper functions specific to IPython. |
|
8 | 8 | |
|
9 |
$Id: iplib.py 1 |
|
|
9 | $Id: iplib.py 1314 2006-05-19 18:24:14Z fperez $ | |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
14 | 14 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
15 | 15 | # |
|
16 | 16 | # Distributed under the terms of the BSD License. The full license is in |
|
17 | 17 | # the file COPYING, distributed as part of this software. |
|
18 | 18 | # |
|
19 | 19 | # Note: this code originally subclassed code.InteractiveConsole from the |
|
20 | 20 | # Python standard library. Over time, all of that class has been copied |
|
21 | 21 | # verbatim here for modifications which could not be accomplished by |
|
22 | 22 | # subclassing. At this point, there are no dependencies at all on the code |
|
23 | 23 | # module anymore (it is not even imported). The Python License (sec. 2) |
|
24 | 24 | # allows for this, but it's always nice to acknowledge credit where credit is |
|
25 | 25 | # due. |
|
26 | 26 | #***************************************************************************** |
|
27 | 27 | |
|
28 | 28 | #**************************************************************************** |
|
29 | 29 | # Modules and globals |
|
30 | 30 | |
|
31 | from __future__ import generators # for 2.2 backwards-compatibility | |
|
32 | ||
|
33 | 31 | from IPython import Release |
|
34 | 32 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
35 | 33 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
36 | 34 | __license__ = Release.license |
|
37 | 35 | __version__ = Release.version |
|
38 | 36 | |
|
39 | 37 | # Python standard modules |
|
40 | 38 | import __main__ |
|
41 | 39 | import __builtin__ |
|
42 | 40 | import StringIO |
|
43 | 41 | import bdb |
|
44 | 42 | import cPickle as pickle |
|
45 | 43 | import codeop |
|
46 | 44 | import exceptions |
|
47 | 45 | import glob |
|
48 | 46 | import inspect |
|
49 | 47 | import keyword |
|
50 | 48 | import new |
|
51 | 49 | import os |
|
52 | 50 | import pdb |
|
53 | 51 | import pydoc |
|
54 | 52 | import re |
|
55 | 53 | import shutil |
|
56 | 54 | import string |
|
57 | 55 | import sys |
|
58 | 56 | import tempfile |
|
59 | 57 | import traceback |
|
60 | 58 | import types |
|
61 | 59 | import pickleshare |
|
62 | 60 | |
|
63 | 61 | from pprint import pprint, pformat |
|
64 | 62 | |
|
65 | 63 | # IPython's own modules |
|
66 | 64 | import IPython |
|
67 | 65 | from IPython import OInspect,PyColorize,ultraTB |
|
68 | 66 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names |
|
69 | 67 | from IPython.FakeModule import FakeModule |
|
70 | 68 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns |
|
71 | 69 | from IPython.Logger import Logger |
|
72 | 70 | from IPython.Magic import Magic |
|
73 | 71 | from IPython.Prompts import CachedOutput |
|
74 | 72 | from IPython.ipstruct import Struct |
|
75 | 73 | from IPython.background_jobs import BackgroundJobManager |
|
76 | 74 | from IPython.usage import cmd_line_usage,interactive_usage |
|
77 | 75 | from IPython.genutils import * |
|
78 | 76 | import IPython.ipapi |
|
79 | 77 | |
|
80 | 78 | # Globals |
|
81 | 79 | |
|
82 | 80 | # store the builtin raw_input globally, and use this always, in case user code |
|
83 | 81 | # overwrites it (like wx.py.PyShell does) |
|
84 | 82 | raw_input_original = raw_input |
|
85 | 83 | |
|
86 | 84 | # compiled regexps for autoindent management |
|
87 | 85 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
88 | 86 | |
|
89 | 87 | |
|
90 | 88 | #**************************************************************************** |
|
91 | 89 | # Some utility function definitions |
|
92 | 90 | |
|
93 | 91 | ini_spaces_re = re.compile(r'^(\s+)') |
|
94 | 92 | |
|
95 | 93 | def num_ini_spaces(strng): |
|
96 | 94 | """Return the number of initial spaces in a string""" |
|
97 | 95 | |
|
98 | 96 | ini_spaces = ini_spaces_re.match(strng) |
|
99 | 97 | if ini_spaces: |
|
100 | 98 | return ini_spaces.end() |
|
101 | 99 | else: |
|
102 | 100 | return 0 |
|
103 | 101 | |
|
104 | 102 | def softspace(file, newvalue): |
|
105 | 103 | """Copied from code.py, to remove the dependency""" |
|
106 | 104 | |
|
107 | 105 | oldvalue = 0 |
|
108 | 106 | try: |
|
109 | 107 | oldvalue = file.softspace |
|
110 | 108 | except AttributeError: |
|
111 | 109 | pass |
|
112 | 110 | try: |
|
113 | 111 | file.softspace = newvalue |
|
114 | 112 | except (AttributeError, TypeError): |
|
115 | 113 | # "attribute-less object" or "read-only attributes" |
|
116 | 114 | pass |
|
117 | 115 | return oldvalue |
|
118 | 116 | |
|
119 | 117 | |
|
120 | 118 | #**************************************************************************** |
|
121 | 119 | # Local use exceptions |
|
122 | 120 | class SpaceInInput(exceptions.Exception): pass |
|
123 | 121 | |
|
124 | 122 | |
|
125 | 123 | #**************************************************************************** |
|
126 | 124 | # Local use classes |
|
127 | 125 | class Bunch: pass |
|
128 | 126 | |
|
129 | 127 | class Undefined: pass |
|
130 | 128 | |
|
131 | 129 | class InputList(list): |
|
132 | 130 | """Class to store user input. |
|
133 | 131 | |
|
134 | 132 | It's basically a list, but slices return a string instead of a list, thus |
|
135 | 133 | allowing things like (assuming 'In' is an instance): |
|
136 | 134 | |
|
137 | 135 | exec In[4:7] |
|
138 | 136 | |
|
139 | 137 | or |
|
140 | 138 | |
|
141 | 139 | exec In[5:9] + In[14] + In[21:25]""" |
|
142 | 140 | |
|
143 | 141 | def __getslice__(self,i,j): |
|
144 | 142 | return ''.join(list.__getslice__(self,i,j)) |
|
145 | 143 | |
|
146 | 144 | class SyntaxTB(ultraTB.ListTB): |
|
147 | 145 | """Extension which holds some state: the last exception value""" |
|
148 | 146 | |
|
149 | 147 | def __init__(self,color_scheme = 'NoColor'): |
|
150 | 148 | ultraTB.ListTB.__init__(self,color_scheme) |
|
151 | 149 | self.last_syntax_error = None |
|
152 | 150 | |
|
153 | 151 | def __call__(self, etype, value, elist): |
|
154 | 152 | self.last_syntax_error = value |
|
155 | 153 | ultraTB.ListTB.__call__(self,etype,value,elist) |
|
156 | 154 | |
|
157 | 155 | def clear_err_state(self): |
|
158 | 156 | """Return the current error state and clear it""" |
|
159 | 157 | e = self.last_syntax_error |
|
160 | 158 | self.last_syntax_error = None |
|
161 | 159 | return e |
|
162 | 160 | |
|
163 | 161 | #**************************************************************************** |
|
164 | 162 | # Main IPython class |
|
165 | 163 | |
|
166 | 164 | # FIXME: the Magic class is a mixin for now, and will unfortunately remain so |
|
167 | 165 | # until a full rewrite is made. I've cleaned all cross-class uses of |
|
168 | 166 | # attributes and methods, but too much user code out there relies on the |
|
169 | 167 | # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage. |
|
170 | 168 | # |
|
171 | 169 | # But at least now, all the pieces have been separated and we could, in |
|
172 | 170 | # principle, stop using the mixin. This will ease the transition to the |
|
173 | 171 | # chainsaw branch. |
|
174 | 172 | |
|
175 | 173 | # For reference, the following is the list of 'self.foo' uses in the Magic |
|
176 | 174 | # class as of 2005-12-28. These are names we CAN'T use in the main ipython |
|
177 | 175 | # class, to prevent clashes. |
|
178 | 176 | |
|
179 | 177 | # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind', |
|
180 | 178 | # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic', |
|
181 | 179 | # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell', |
|
182 | 180 | # 'self.value'] |
|
183 | 181 | |
|
184 | 182 | class InteractiveShell(object,Magic): |
|
185 | 183 | """An enhanced console for Python.""" |
|
186 | 184 | |
|
187 | 185 | # class attribute to indicate whether the class supports threads or not. |
|
188 | 186 | # Subclasses with thread support should override this as needed. |
|
189 | 187 | isthreaded = False |
|
190 | 188 | |
|
191 | 189 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
192 | 190 | user_ns = None,user_global_ns=None,banner2='', |
|
193 | 191 | custom_exceptions=((),None),embedded=False): |
|
194 | 192 | |
|
195 | 193 | |
|
196 | 194 | # log system |
|
197 | 195 | self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate') |
|
198 | 196 | |
|
199 | # Produce a public API instance | |
|
200 | ||
|
201 | self.api = IPython.ipapi.IPApi(self) | |
|
202 | ||
|
203 | 197 | # some minimal strict typechecks. For some core data structures, I |
|
204 | 198 | # want actual basic python types, not just anything that looks like |
|
205 | 199 | # one. This is especially true for namespaces. |
|
206 | 200 | for ns in (user_ns,user_global_ns): |
|
207 | 201 | if ns is not None and type(ns) != types.DictType: |
|
208 | 202 | raise TypeError,'namespace must be a dictionary' |
|
209 | 203 | |
|
210 | 204 | # Job manager (for jobs run as background threads) |
|
211 | 205 | self.jobs = BackgroundJobManager() |
|
212 | 206 | |
|
213 | # track which builtins we add, so we can clean up later | |
|
214 | self.builtins_added = {} | |
|
215 | # This method will add the necessary builtins for operation, but | |
|
216 | # tracking what it did via the builtins_added dict. | |
|
217 | self.add_builtins() | |
|
218 | ||
|
219 | 207 | # Do the intuitively correct thing for quit/exit: we remove the |
|
220 | 208 | # builtins if they exist, and our own magics will deal with this |
|
221 | 209 | try: |
|
222 | 210 | del __builtin__.exit, __builtin__.quit |
|
223 | 211 | except AttributeError: |
|
224 | 212 | pass |
|
225 | 213 | |
|
226 | 214 | # Store the actual shell's name |
|
227 | 215 | self.name = name |
|
228 | 216 | |
|
229 | 217 | # We need to know whether the instance is meant for embedding, since |
|
230 | 218 | # global/local namespaces need to be handled differently in that case |
|
231 | 219 | self.embedded = embedded |
|
232 | 220 | |
|
233 | 221 | # command compiler |
|
234 | 222 | self.compile = codeop.CommandCompiler() |
|
235 | 223 | |
|
236 | 224 | # User input buffer |
|
237 | 225 | self.buffer = [] |
|
238 | 226 | |
|
239 | 227 | # Default name given in compilation of code |
|
240 | 228 | self.filename = '<ipython console>' |
|
241 | 229 | |
|
242 | 230 | # Make an empty namespace, which extension writers can rely on both |
|
243 | 231 | # existing and NEVER being used by ipython itself. This gives them a |
|
244 | 232 | # convenient location for storing additional information and state |
|
245 | 233 | # their extensions may require, without fear of collisions with other |
|
246 | 234 | # ipython names that may develop later. |
|
247 | 235 | self.meta = Struct() |
|
248 | 236 | |
|
249 | 237 | # Create the namespace where the user will operate. user_ns is |
|
250 | 238 | # normally the only one used, and it is passed to the exec calls as |
|
251 | 239 | # the locals argument. But we do carry a user_global_ns namespace |
|
252 | 240 | # given as the exec 'globals' argument, This is useful in embedding |
|
253 | 241 | # situations where the ipython shell opens in a context where the |
|
254 | 242 | # distinction between locals and globals is meaningful. |
|
255 | 243 | |
|
256 | 244 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
257 | 245 | # level as a dict instead of a module. This is a manual fix, but I |
|
258 | 246 | # should really track down where the problem is coming from. Alex |
|
259 | 247 | # Schmolck reported this problem first. |
|
260 | 248 | |
|
261 | 249 | # A useful post by Alex Martelli on this topic: |
|
262 | 250 | # Re: inconsistent value from __builtins__ |
|
263 | 251 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
264 | 252 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
265 | 253 | # Gruppen: comp.lang.python |
|
266 | 254 | |
|
267 | 255 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
268 | 256 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
269 | 257 | # > <type 'dict'> |
|
270 | 258 | # > >>> print type(__builtins__) |
|
271 | 259 | # > <type 'module'> |
|
272 | 260 | # > Is this difference in return value intentional? |
|
273 | 261 | |
|
274 | 262 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
275 | 263 | # or a module, and it's been that way for a long time. Whether it's |
|
276 | 264 | # intentional (or sensible), I don't know. In any case, the idea is |
|
277 | 265 | # that if you need to access the built-in namespace directly, you |
|
278 | 266 | # should start with "import __builtin__" (note, no 's') which will |
|
279 | 267 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
280 | 268 | |
|
281 | 269 | if user_ns is None: |
|
282 | 270 | # Set __name__ to __main__ to better match the behavior of the |
|
283 | 271 | # normal interpreter. |
|
284 | 272 | user_ns = {'__name__' :'__main__', |
|
285 | 273 | '__builtins__' : __builtin__, |
|
286 | 274 | } |
|
287 | 275 | |
|
288 | 276 | if user_global_ns is None: |
|
289 | 277 | user_global_ns = {} |
|
290 | 278 | |
|
291 | 279 | # Assign namespaces |
|
292 | 280 | # This is the namespace where all normal user variables live |
|
293 | 281 | self.user_ns = user_ns |
|
294 | 282 | # Embedded instances require a separate namespace for globals. |
|
295 | 283 | # Normally this one is unused by non-embedded instances. |
|
296 | 284 | self.user_global_ns = user_global_ns |
|
297 | 285 | # A namespace to keep track of internal data structures to prevent |
|
298 | 286 | # them from cluttering user-visible stuff. Will be updated later |
|
299 | 287 | self.internal_ns = {} |
|
300 | 288 | |
|
301 | 289 | # Namespace of system aliases. Each entry in the alias |
|
302 | 290 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
303 | 291 | # of positional arguments of the alias. |
|
304 | 292 | self.alias_table = {} |
|
305 | 293 | |
|
306 | 294 | # A table holding all the namespaces IPython deals with, so that |
|
307 | 295 | # introspection facilities can search easily. |
|
308 | 296 | self.ns_table = {'user':user_ns, |
|
309 | 297 | 'user_global':user_global_ns, |
|
310 | 298 | 'alias':self.alias_table, |
|
311 | 299 | 'internal':self.internal_ns, |
|
312 | 300 | 'builtin':__builtin__.__dict__ |
|
313 | 301 | } |
|
314 | 302 | |
|
315 | 303 | # The user namespace MUST have a pointer to the shell itself. |
|
316 | 304 | self.user_ns[name] = self |
|
317 | 305 | |
|
318 | 306 | # We need to insert into sys.modules something that looks like a |
|
319 | 307 | # module but which accesses the IPython namespace, for shelve and |
|
320 | 308 | # pickle to work interactively. Normally they rely on getting |
|
321 | 309 | # everything out of __main__, but for embedding purposes each IPython |
|
322 | 310 | # instance has its own private namespace, so we can't go shoving |
|
323 | 311 | # everything into __main__. |
|
324 | 312 | |
|
325 | 313 | # note, however, that we should only do this for non-embedded |
|
326 | 314 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
327 | 315 | # namespace. Embedded instances, on the other hand, should not do |
|
328 | 316 | # this because they need to manage the user local/global namespaces |
|
329 | 317 | # only, but they live within a 'normal' __main__ (meaning, they |
|
330 | 318 | # shouldn't overtake the execution environment of the script they're |
|
331 | 319 | # embedded in). |
|
332 | 320 | |
|
333 | 321 | if not embedded: |
|
334 | 322 | try: |
|
335 | 323 | main_name = self.user_ns['__name__'] |
|
336 | 324 | except KeyError: |
|
337 | 325 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' |
|
338 | 326 | else: |
|
339 | 327 | #print "pickle hack in place" # dbg |
|
340 | 328 | #print 'main_name:',main_name # dbg |
|
341 | 329 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
342 | 330 | |
|
343 | 331 | # List of input with multi-line handling. |
|
344 | 332 | # Fill its zero entry, user counter starts at 1 |
|
345 | 333 | self.input_hist = InputList(['\n']) |
|
346 | 334 | # This one will hold the 'raw' input history, without any |
|
347 | 335 | # pre-processing. This will allow users to retrieve the input just as |
|
348 | 336 | # it was exactly typed in by the user, with %hist -r. |
|
349 | 337 | self.input_hist_raw = InputList(['\n']) |
|
350 | 338 | |
|
351 | 339 | # list of visited directories |
|
352 | 340 | try: |
|
353 | 341 | self.dir_hist = [os.getcwd()] |
|
354 | 342 | except IOError, e: |
|
355 | 343 | self.dir_hist = [] |
|
356 | 344 | |
|
357 | 345 | # dict of output history |
|
358 | 346 | self.output_hist = {} |
|
359 | 347 | |
|
360 | 348 | # dict of things NOT to alias (keywords, builtins and some magics) |
|
361 | 349 | no_alias = {} |
|
362 | 350 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] |
|
363 | 351 | for key in keyword.kwlist + no_alias_magics: |
|
364 | 352 | no_alias[key] = 1 |
|
365 | 353 | no_alias.update(__builtin__.__dict__) |
|
366 | 354 | self.no_alias = no_alias |
|
367 | 355 | |
|
368 | 356 | # make global variables for user access to these |
|
369 | 357 | self.user_ns['_ih'] = self.input_hist |
|
370 | 358 | self.user_ns['_oh'] = self.output_hist |
|
371 | 359 | self.user_ns['_dh'] = self.dir_hist |
|
372 | 360 | |
|
373 | 361 | # user aliases to input and output histories |
|
374 | 362 | self.user_ns['In'] = self.input_hist |
|
375 | 363 | self.user_ns['Out'] = self.output_hist |
|
376 | 364 | |
|
377 | 365 | # Object variable to store code object waiting execution. This is |
|
378 | 366 | # used mainly by the multithreaded shells, but it can come in handy in |
|
379 | 367 | # other situations. No need to use a Queue here, since it's a single |
|
380 | 368 | # item which gets cleared once run. |
|
381 | 369 | self.code_to_run = None |
|
382 | 370 | |
|
383 | 371 | # escapes for automatic behavior on the command line |
|
384 | 372 | self.ESC_SHELL = '!' |
|
385 | 373 | self.ESC_HELP = '?' |
|
386 | 374 | self.ESC_MAGIC = '%' |
|
387 | 375 | self.ESC_QUOTE = ',' |
|
388 | 376 | self.ESC_QUOTE2 = ';' |
|
389 | 377 | self.ESC_PAREN = '/' |
|
390 | 378 | |
|
391 | 379 | # And their associated handlers |
|
392 | 380 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, |
|
393 | 381 | self.ESC_QUOTE : self.handle_auto, |
|
394 | 382 | self.ESC_QUOTE2 : self.handle_auto, |
|
395 | 383 | self.ESC_MAGIC : self.handle_magic, |
|
396 | 384 | self.ESC_HELP : self.handle_help, |
|
397 | 385 | self.ESC_SHELL : self.handle_shell_escape, |
|
398 | 386 | } |
|
399 | 387 | |
|
400 | 388 | # class initializations |
|
401 | 389 | Magic.__init__(self,self) |
|
402 | 390 | |
|
403 | 391 | # Python source parser/formatter for syntax highlighting |
|
404 | 392 | pyformat = PyColorize.Parser().format |
|
405 | 393 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) |
|
406 | 394 | |
|
407 | 395 | # hooks holds pointers used for user-side customizations |
|
408 | 396 | self.hooks = Struct() |
|
409 | 397 | |
|
410 | 398 | # Set all default hooks, defined in the IPython.hooks module. |
|
411 | 399 | hooks = IPython.hooks |
|
412 | 400 | for hook_name in hooks.__all__: |
|
413 | 401 | # default hooks have priority 100, i.e. low; user hooks should have 0-100 priority |
|
414 | 402 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
415 | 403 | #print "bound hook",hook_name |
|
416 | 404 | |
|
417 | 405 | # Flag to mark unconditional exit |
|
418 | 406 | self.exit_now = False |
|
419 | 407 | |
|
420 | 408 | self.usage_min = """\ |
|
421 | 409 | An enhanced console for Python. |
|
422 | 410 | Some of its features are: |
|
423 | 411 | - Readline support if the readline library is present. |
|
424 | 412 | - Tab completion in the local namespace. |
|
425 | 413 | - Logging of input, see command-line options. |
|
426 | 414 | - System shell escape via ! , eg !ls. |
|
427 | 415 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) |
|
428 | 416 | - Keeps track of locally defined variables via %who, %whos. |
|
429 | 417 | - Show object information with a ? eg ?x or x? (use ?? for more info). |
|
430 | 418 | """ |
|
431 | 419 | if usage: self.usage = usage |
|
432 | 420 | else: self.usage = self.usage_min |
|
433 | 421 | |
|
434 | 422 | # Storage |
|
435 | 423 | self.rc = rc # This will hold all configuration information |
|
436 | 424 | self.pager = 'less' |
|
437 | 425 | # temporary files used for various purposes. Deleted at exit. |
|
438 | 426 | self.tempfiles = [] |
|
439 | 427 | |
|
440 | 428 | # Keep track of readline usage (later set by init_readline) |
|
441 | 429 | self.has_readline = False |
|
442 | 430 | |
|
443 | 431 | # template for logfile headers. It gets resolved at runtime by the |
|
444 | 432 | # logstart method. |
|
445 | 433 | self.loghead_tpl = \ |
|
446 | 434 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
447 | 435 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
448 | 436 | #log# opts = %s |
|
449 | 437 | #log# args = %s |
|
450 | 438 | #log# It is safe to make manual edits below here. |
|
451 | 439 | #log#----------------------------------------------------------------------- |
|
452 | 440 | """ |
|
453 | 441 | # for pushd/popd management |
|
454 | 442 | try: |
|
455 | 443 | self.home_dir = get_home_dir() |
|
456 | 444 | except HomeDirError,msg: |
|
457 | 445 | fatal(msg) |
|
458 | 446 | |
|
459 | 447 | self.dir_stack = [os.getcwd().replace(self.home_dir,'~')] |
|
460 | 448 | |
|
461 | 449 | # Functions to call the underlying shell. |
|
462 | 450 | |
|
463 | 451 | # utility to expand user variables via Itpl |
|
464 | 452 | self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'), |
|
465 | 453 | self.user_ns)) |
|
466 | 454 | # The first is similar to os.system, but it doesn't return a value, |
|
467 | 455 | # and it allows interpolation of variables in the user's namespace. |
|
468 | 456 | self.system = lambda cmd: shell(self.var_expand(cmd), |
|
469 | 457 | header='IPython system call: ', |
|
470 | 458 | verbose=self.rc.system_verbose) |
|
471 | 459 | # These are for getoutput and getoutputerror: |
|
472 | 460 | self.getoutput = lambda cmd: \ |
|
473 | 461 | getoutput(self.var_expand(cmd), |
|
474 | 462 | header='IPython system call: ', |
|
475 | 463 | verbose=self.rc.system_verbose) |
|
476 | 464 | self.getoutputerror = lambda cmd: \ |
|
477 | 465 | getoutputerror(str(ItplNS(cmd.replace('#','\#'), |
|
478 | 466 | self.user_ns)), |
|
479 | 467 | header='IPython system call: ', |
|
480 | 468 | verbose=self.rc.system_verbose) |
|
481 | 469 | |
|
482 | 470 | # RegExp for splitting line contents into pre-char//first |
|
483 | 471 | # word-method//rest. For clarity, each group in on one line. |
|
484 | 472 | |
|
485 | 473 | # WARNING: update the regexp if the above escapes are changed, as they |
|
486 | 474 | # are hardwired in. |
|
487 | 475 | |
|
488 | 476 | # Don't get carried away with trying to make the autocalling catch too |
|
489 | 477 | # much: it's better to be conservative rather than to trigger hidden |
|
490 | 478 | # evals() somewhere and end up causing side effects. |
|
491 | 479 | |
|
492 | 480 | self.line_split = re.compile(r'^([\s*,;/])' |
|
493 | 481 | r'([\?\w\.]+\w*\s*)' |
|
494 | 482 | r'(\(?.*$)') |
|
495 | 483 | |
|
496 | 484 | # Original re, keep around for a while in case changes break something |
|
497 | 485 | #self.line_split = re.compile(r'(^[\s*!\?%,/]?)' |
|
498 | 486 | # r'(\s*[\?\w\.]+\w*\s*)' |
|
499 | 487 | # r'(\(?.*$)') |
|
500 | 488 | |
|
501 | 489 | # RegExp to identify potential function names |
|
502 | 490 | self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$') |
|
503 | 491 | |
|
504 | 492 | # RegExp to exclude strings with this start from autocalling. In |
|
505 | 493 | # particular, all binary operators should be excluded, so that if foo |
|
506 | 494 | # is callable, foo OP bar doesn't become foo(OP bar), which is |
|
507 | 495 | # invalid. The characters '!=()' don't need to be checked for, as the |
|
508 | 496 | # _prefilter routine explicitely does so, to catch direct calls and |
|
509 | 497 | # rebindings of existing names. |
|
510 | 498 | |
|
511 | 499 | # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise |
|
512 | 500 | # it affects the rest of the group in square brackets. |
|
513 | 501 | self.re_exclude_auto = re.compile(r'^[<>,&^\|\*/\+-]' |
|
514 | 502 | '|^is |^not |^in |^and |^or ') |
|
515 | 503 | |
|
516 | 504 | # try to catch also methods for stuff in lists/tuples/dicts: off |
|
517 | 505 | # (experimental). For this to work, the line_split regexp would need |
|
518 | 506 | # to be modified so it wouldn't break things at '['. That line is |
|
519 | 507 | # nasty enough that I shouldn't change it until I can test it _well_. |
|
520 | 508 | #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$') |
|
521 | 509 | |
|
522 | 510 | # keep track of where we started running (mainly for crash post-mortem) |
|
523 | 511 | self.starting_dir = os.getcwd() |
|
524 | 512 | |
|
525 | 513 | # Various switches which can be set |
|
526 | 514 | self.CACHELENGTH = 5000 # this is cheap, it's just text |
|
527 | 515 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ |
|
528 | 516 | self.banner2 = banner2 |
|
529 | 517 | |
|
530 | 518 | # TraceBack handlers: |
|
531 | 519 | |
|
532 | 520 | # Syntax error handler. |
|
533 | 521 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') |
|
534 | 522 | |
|
535 | 523 | # The interactive one is initialized with an offset, meaning we always |
|
536 | 524 | # want to remove the topmost item in the traceback, which is our own |
|
537 | 525 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
538 | 526 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', |
|
539 | 527 | color_scheme='NoColor', |
|
540 | 528 | tb_offset = 1) |
|
541 | 529 | |
|
542 | 530 | # IPython itself shouldn't crash. This will produce a detailed |
|
543 | 531 | # post-mortem if it does. But we only install the crash handler for |
|
544 | 532 | # non-threaded shells, the threaded ones use a normal verbose reporter |
|
545 | 533 | # and lose the crash handler. This is because exceptions in the main |
|
546 | 534 | # thread (such as in GUI code) propagate directly to sys.excepthook, |
|
547 | 535 | # and there's no point in printing crash dumps for every user exception. |
|
548 | 536 | if self.isthreaded: |
|
549 | 537 | sys.excepthook = ultraTB.FormattedTB() |
|
550 | 538 | else: |
|
551 | 539 | from IPython import CrashHandler |
|
552 | 540 | sys.excepthook = CrashHandler.CrashHandler(self) |
|
553 | 541 | |
|
554 | 542 | # The instance will store a pointer to this, so that runtime code |
|
555 | 543 | # (such as magics) can access it. This is because during the |
|
556 | 544 | # read-eval loop, it gets temporarily overwritten (to deal with GUI |
|
557 | 545 | # frameworks). |
|
558 | 546 | self.sys_excepthook = sys.excepthook |
|
559 | 547 | |
|
560 | 548 | # and add any custom exception handlers the user may have specified |
|
561 | 549 | self.set_custom_exc(*custom_exceptions) |
|
562 | 550 | |
|
563 | 551 | # Object inspector |
|
564 | 552 | self.inspector = OInspect.Inspector(OInspect.InspectColors, |
|
565 | 553 | PyColorize.ANSICodeColors, |
|
566 | 554 | 'NoColor') |
|
567 | 555 | # indentation management |
|
568 | 556 | self.autoindent = False |
|
569 | 557 | self.indent_current_nsp = 0 |
|
570 | 558 | |
|
571 | 559 | # Make some aliases automatically |
|
572 | 560 | # Prepare list of shell aliases to auto-define |
|
573 | 561 | if os.name == 'posix': |
|
574 | 562 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', |
|
575 | 563 | 'mv mv -i','rm rm -i','cp cp -i', |
|
576 | 564 | 'cat cat','less less','clear clear', |
|
577 | 565 | # a better ls |
|
578 | 566 | 'ls ls -F', |
|
579 | 567 | # long ls |
|
580 | 568 | 'll ls -lF', |
|
581 | 569 | # color ls |
|
582 | 570 | 'lc ls -F -o --color', |
|
583 | 571 | # ls normal files only |
|
584 | 572 | 'lf ls -F -o --color %l | grep ^-', |
|
585 | 573 | # ls symbolic links |
|
586 | 574 | 'lk ls -F -o --color %l | grep ^l', |
|
587 | 575 | # directories or links to directories, |
|
588 | 576 | 'ldir ls -F -o --color %l | grep /$', |
|
589 | 577 | # things which are executable |
|
590 | 578 | 'lx ls -F -o --color %l | grep ^-..x', |
|
591 | 579 | ) |
|
592 | 580 | elif os.name in ['nt','dos']: |
|
593 | 581 | auto_alias = ('dir dir /on', 'ls dir /on', |
|
594 | 582 | 'ddir dir /ad /on', 'ldir dir /ad /on', |
|
595 | 583 | 'mkdir mkdir','rmdir rmdir','echo echo', |
|
596 | 584 | 'ren ren','cls cls','copy copy') |
|
597 | 585 | else: |
|
598 | 586 | auto_alias = () |
|
599 | 587 | self.auto_alias = map(lambda s:s.split(None,1),auto_alias) |
|
600 | 588 | # Call the actual (public) initializer |
|
601 | 589 | self.init_auto_alias() |
|
590 | ||
|
591 | # Produce a public API instance | |
|
592 | self.api = IPython.ipapi.IPApi(self) | |
|
593 | ||
|
594 | # track which builtins we add, so we can clean up later | |
|
595 | self.builtins_added = {} | |
|
596 | # This method will add the necessary builtins for operation, but | |
|
597 | # tracking what it did via the builtins_added dict. | |
|
598 | self.add_builtins() | |
|
599 | ||
|
602 | 600 | # end __init__ |
|
603 | 601 | |
|
604 | 602 | def pre_config_initialization(self): |
|
605 | 603 | """Pre-configuration init method |
|
606 | 604 | |
|
607 | 605 | This is called before the configuration files are processed to |
|
608 | 606 | prepare the services the config files might need. |
|
609 | 607 | |
|
610 | 608 | self.rc already has reasonable default values at this point. |
|
611 | 609 | """ |
|
612 | 610 | rc = self.rc |
|
613 | 611 | |
|
614 | 612 | self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db") |
|
615 | 613 | |
|
616 | ||
|
617 | 614 | def post_config_initialization(self): |
|
618 | 615 | """Post configuration init method |
|
619 | 616 | |
|
620 | 617 | This is called after the configuration files have been processed to |
|
621 | 618 | 'finalize' the initialization.""" |
|
622 | 619 | |
|
623 | 620 | rc = self.rc |
|
624 | 621 | |
|
625 | 622 | # Load readline proper |
|
626 | 623 | if rc.readline: |
|
627 | 624 | self.init_readline() |
|
628 | 625 | |
|
629 | 626 | # local shortcut, this is used a LOT |
|
630 | 627 | self.log = self.logger.log |
|
631 | 628 | |
|
632 | 629 | # Initialize cache, set in/out prompts and printing system |
|
633 | 630 | self.outputcache = CachedOutput(self, |
|
634 | 631 | rc.cache_size, |
|
635 | 632 | rc.pprint, |
|
636 | 633 | input_sep = rc.separate_in, |
|
637 | 634 | output_sep = rc.separate_out, |
|
638 | 635 | output_sep2 = rc.separate_out2, |
|
639 | 636 | ps1 = rc.prompt_in1, |
|
640 | 637 | ps2 = rc.prompt_in2, |
|
641 | 638 | ps_out = rc.prompt_out, |
|
642 | 639 | pad_left = rc.prompts_pad_left) |
|
643 | 640 | |
|
644 | 641 | # user may have over-ridden the default print hook: |
|
645 | 642 | try: |
|
646 | 643 | self.outputcache.__class__.display = self.hooks.display |
|
647 | 644 | except AttributeError: |
|
648 | 645 | pass |
|
649 | 646 | |
|
650 | 647 | # I don't like assigning globally to sys, because it means when embedding |
|
651 | 648 | # instances, each embedded instance overrides the previous choice. But |
|
652 | 649 | # sys.displayhook seems to be called internally by exec, so I don't see a |
|
653 | 650 | # way around it. |
|
654 | 651 | sys.displayhook = self.outputcache |
|
655 | 652 | |
|
656 | 653 | # Set user colors (don't do it in the constructor above so that it |
|
657 | 654 | # doesn't crash if colors option is invalid) |
|
658 | 655 | self.magic_colors(rc.colors) |
|
659 | 656 | |
|
660 | 657 | # Set calling of pdb on exceptions |
|
661 | 658 | self.call_pdb = rc.pdb |
|
662 | 659 | |
|
663 | 660 | # Load user aliases |
|
664 | 661 | for alias in rc.alias: |
|
665 | 662 | self.magic_alias(alias) |
|
666 | 663 | self.hooks.late_startup_hook() |
|
667 | 664 | |
|
668 | 665 | for batchfile in [path(arg) for arg in self.rc.args |
|
669 | 666 | if arg.lower().endswith('.ipy')]: |
|
670 | 667 | if not batchfile.isfile(): |
|
671 | 668 | print "No such batch file:", batchfile |
|
672 | 669 | continue |
|
673 | 670 | self.api.runlines(batchfile.text()) |
|
674 | 671 | |
|
675 | 672 | def add_builtins(self): |
|
676 | 673 | """Store ipython references into the builtin namespace. |
|
677 | 674 | |
|
678 | 675 | Some parts of ipython operate via builtins injected here, which hold a |
|
679 | 676 | reference to IPython itself.""" |
|
680 | 677 | |
|
681 | 678 | # TODO: deprecate all except _ip; 'jobs' should be installed |
|
682 | 679 | # by an extension and the rest are under _ip, ipalias is redundant |
|
683 | 680 | builtins_new = dict(__IPYTHON__ = self, |
|
684 | 681 | ip_set_hook = self.set_hook, |
|
685 | 682 | jobs = self.jobs, |
|
686 | 683 | ipmagic = self.ipmagic, |
|
687 | 684 | ipalias = self.ipalias, |
|
688 | 685 | ipsystem = self.ipsystem, |
|
689 | 686 | _ip = self.api |
|
690 | 687 | ) |
|
691 | 688 | for biname,bival in builtins_new.items(): |
|
692 | 689 | try: |
|
693 | 690 | # store the orignal value so we can restore it |
|
694 | 691 | self.builtins_added[biname] = __builtin__.__dict__[biname] |
|
695 | 692 | except KeyError: |
|
696 | 693 | # or mark that it wasn't defined, and we'll just delete it at |
|
697 | 694 | # cleanup |
|
698 | 695 | self.builtins_added[biname] = Undefined |
|
699 | 696 | __builtin__.__dict__[biname] = bival |
|
700 | 697 | |
|
701 | 698 | # Keep in the builtins a flag for when IPython is active. We set it |
|
702 | 699 | # with setdefault so that multiple nested IPythons don't clobber one |
|
703 | 700 | # another. Each will increase its value by one upon being activated, |
|
704 | 701 | # which also gives us a way to determine the nesting level. |
|
705 | 702 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
706 | 703 | |
|
707 | 704 | def clean_builtins(self): |
|
708 | 705 | """Remove any builtins which might have been added by add_builtins, or |
|
709 | 706 | restore overwritten ones to their previous values.""" |
|
710 | 707 | for biname,bival in self.builtins_added.items(): |
|
711 | 708 | if bival is Undefined: |
|
712 | 709 | del __builtin__.__dict__[biname] |
|
713 | 710 | else: |
|
714 | 711 | __builtin__.__dict__[biname] = bival |
|
715 | 712 | self.builtins_added.clear() |
|
716 | 713 | |
|
717 | 714 | def set_hook(self,name,hook, priority = 50): |
|
718 | 715 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
719 | 716 | |
|
720 | 717 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
721 | 718 | adding your function to one of these hooks, you can modify IPython's |
|
722 | 719 | behavior to call at runtime your own routines.""" |
|
723 | 720 | |
|
724 | 721 | # At some point in the future, this should validate the hook before it |
|
725 | 722 | # accepts it. Probably at least check that the hook takes the number |
|
726 | 723 | # of args it's supposed to. |
|
727 | 724 | dp = getattr(self.hooks, name, None) |
|
728 | 725 | if name not in IPython.hooks.__all__: |
|
729 | 726 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ ) |
|
730 | 727 | if not dp: |
|
731 | 728 | dp = IPython.hooks.CommandChainDispatcher() |
|
732 | 729 | |
|
733 | 730 | f = new.instancemethod(hook,self,self.__class__) |
|
734 | 731 | try: |
|
735 | 732 | dp.add(f,priority) |
|
736 | 733 | except AttributeError: |
|
737 | 734 | # it was not commandchain, plain old func - replace |
|
738 | 735 | dp = f |
|
739 | 736 | |
|
740 | 737 | setattr(self.hooks,name, dp) |
|
741 | 738 | |
|
742 | 739 | |
|
743 | 740 | #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) |
|
744 | 741 | |
|
745 | 742 | def set_custom_exc(self,exc_tuple,handler): |
|
746 | 743 | """set_custom_exc(exc_tuple,handler) |
|
747 | 744 | |
|
748 | 745 | Set a custom exception handler, which will be called if any of the |
|
749 | 746 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
750 | 747 | runcode() method. |
|
751 | 748 | |
|
752 | 749 | Inputs: |
|
753 | 750 | |
|
754 | 751 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
755 | 752 | handler for. It is very important that you use a tuple, and NOT A |
|
756 | 753 | LIST here, because of the way Python's except statement works. If |
|
757 | 754 | you only want to trap a single exception, use a singleton tuple: |
|
758 | 755 | |
|
759 | 756 | exc_tuple == (MyCustomException,) |
|
760 | 757 | |
|
761 | 758 | - handler: this must be defined as a function with the following |
|
762 | 759 | basic interface: def my_handler(self,etype,value,tb). |
|
763 | 760 | |
|
764 | 761 | This will be made into an instance method (via new.instancemethod) |
|
765 | 762 | of IPython itself, and it will be called if any of the exceptions |
|
766 | 763 | listed in the exc_tuple are caught. If the handler is None, an |
|
767 | 764 | internal basic one is used, which just prints basic info. |
|
768 | 765 | |
|
769 | 766 | WARNING: by putting in your own exception handler into IPython's main |
|
770 | 767 | execution loop, you run a very good chance of nasty crashes. This |
|
771 | 768 | facility should only be used if you really know what you are doing.""" |
|
772 | 769 | |
|
773 | 770 | assert type(exc_tuple)==type(()) , \ |
|
774 | 771 | "The custom exceptions must be given AS A TUPLE." |
|
775 | 772 | |
|
776 | 773 | def dummy_handler(self,etype,value,tb): |
|
777 | 774 | print '*** Simple custom exception handler ***' |
|
778 | 775 | print 'Exception type :',etype |
|
779 | 776 | print 'Exception value:',value |
|
780 | 777 | print 'Traceback :',tb |
|
781 | 778 | print 'Source code :','\n'.join(self.buffer) |
|
782 | 779 | |
|
783 | 780 | if handler is None: handler = dummy_handler |
|
784 | 781 | |
|
785 | 782 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
786 | 783 | self.custom_exceptions = exc_tuple |
|
787 | 784 | |
|
788 | 785 | def set_custom_completer(self,completer,pos=0): |
|
789 | 786 | """set_custom_completer(completer,pos=0) |
|
790 | 787 | |
|
791 | 788 | Adds a new custom completer function. |
|
792 | 789 | |
|
793 | 790 | The position argument (defaults to 0) is the index in the completers |
|
794 | 791 | list where you want the completer to be inserted.""" |
|
795 | 792 | |
|
796 | 793 | newcomp = new.instancemethod(completer,self.Completer, |
|
797 | 794 | self.Completer.__class__) |
|
798 | 795 | self.Completer.matchers.insert(pos,newcomp) |
|
799 | 796 | |
|
800 | 797 | def _get_call_pdb(self): |
|
801 | 798 | return self._call_pdb |
|
802 | 799 | |
|
803 | 800 | def _set_call_pdb(self,val): |
|
804 | 801 | |
|
805 | 802 | if val not in (0,1,False,True): |
|
806 | 803 | raise ValueError,'new call_pdb value must be boolean' |
|
807 | 804 | |
|
808 | 805 | # store value in instance |
|
809 | 806 | self._call_pdb = val |
|
810 | 807 | |
|
811 | 808 | # notify the actual exception handlers |
|
812 | 809 | self.InteractiveTB.call_pdb = val |
|
813 | 810 | if self.isthreaded: |
|
814 | 811 | try: |
|
815 | 812 | self.sys_excepthook.call_pdb = val |
|
816 | 813 | except: |
|
817 | 814 | warn('Failed to activate pdb for threaded exception handler') |
|
818 | 815 | |
|
819 | 816 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
820 | 817 | 'Control auto-activation of pdb at exceptions') |
|
821 | 818 | |
|
822 | 819 | |
|
823 | 820 | # These special functions get installed in the builtin namespace, to |
|
824 | 821 | # provide programmatic (pure python) access to magics, aliases and system |
|
825 | 822 | # calls. This is important for logging, user scripting, and more. |
|
826 | 823 | |
|
827 | 824 | # We are basically exposing, via normal python functions, the three |
|
828 | 825 | # mechanisms in which ipython offers special call modes (magics for |
|
829 | 826 | # internal control, aliases for direct system access via pre-selected |
|
830 | 827 | # names, and !cmd for calling arbitrary system commands). |
|
831 | 828 | |
|
832 | 829 | def ipmagic(self,arg_s): |
|
833 | 830 | """Call a magic function by name. |
|
834 | 831 | |
|
835 | 832 | Input: a string containing the name of the magic function to call and any |
|
836 | 833 | additional arguments to be passed to the magic. |
|
837 | 834 | |
|
838 | 835 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython |
|
839 | 836 | prompt: |
|
840 | 837 | |
|
841 | 838 | In[1]: %name -opt foo bar |
|
842 | 839 | |
|
843 | 840 | To call a magic without arguments, simply use ipmagic('name'). |
|
844 | 841 | |
|
845 | 842 | This provides a proper Python function to call IPython's magics in any |
|
846 | 843 | valid Python code you can type at the interpreter, including loops and |
|
847 | 844 | compound statements. It is added by IPython to the Python builtin |
|
848 | 845 | namespace upon initialization.""" |
|
849 | 846 | |
|
850 | 847 | args = arg_s.split(' ',1) |
|
851 | 848 | magic_name = args[0] |
|
852 | 849 | magic_name = magic_name.lstrip(self.ESC_MAGIC) |
|
853 | 850 | |
|
854 | 851 | try: |
|
855 | 852 | magic_args = args[1] |
|
856 | 853 | except IndexError: |
|
857 | 854 | magic_args = '' |
|
858 | 855 | fn = getattr(self,'magic_'+magic_name,None) |
|
859 | 856 | if fn is None: |
|
860 | 857 | error("Magic function `%s` not found." % magic_name) |
|
861 | 858 | else: |
|
862 | 859 | magic_args = self.var_expand(magic_args) |
|
863 | 860 | return fn(magic_args) |
|
864 | 861 | |
|
865 | 862 | def ipalias(self,arg_s): |
|
866 | 863 | """Call an alias by name. |
|
867 | 864 | |
|
868 | 865 | Input: a string containing the name of the alias to call and any |
|
869 | 866 | additional arguments to be passed to the magic. |
|
870 | 867 | |
|
871 | 868 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
872 | 869 | prompt: |
|
873 | 870 | |
|
874 | 871 | In[1]: name -opt foo bar |
|
875 | 872 | |
|
876 | 873 | To call an alias without arguments, simply use ipalias('name'). |
|
877 | 874 | |
|
878 | 875 | This provides a proper Python function to call IPython's aliases in any |
|
879 | 876 | valid Python code you can type at the interpreter, including loops and |
|
880 | 877 | compound statements. It is added by IPython to the Python builtin |
|
881 | 878 | namespace upon initialization.""" |
|
882 | 879 | |
|
883 | 880 | args = arg_s.split(' ',1) |
|
884 | 881 | alias_name = args[0] |
|
885 | 882 | try: |
|
886 | 883 | alias_args = args[1] |
|
887 | 884 | except IndexError: |
|
888 | 885 | alias_args = '' |
|
889 | 886 | if alias_name in self.alias_table: |
|
890 | 887 | self.call_alias(alias_name,alias_args) |
|
891 | 888 | else: |
|
892 | 889 | error("Alias `%s` not found." % alias_name) |
|
893 | 890 | |
|
894 | 891 | def ipsystem(self,arg_s): |
|
895 | 892 | """Make a system call, using IPython.""" |
|
896 | 893 | |
|
897 | 894 | self.system(arg_s) |
|
898 | 895 | |
|
899 | 896 | def complete(self,text): |
|
900 | 897 | """Return a sorted list of all possible completions on text. |
|
901 | 898 | |
|
902 | 899 | Inputs: |
|
903 | 900 | |
|
904 | 901 | - text: a string of text to be completed on. |
|
905 | 902 | |
|
906 | 903 | This is a wrapper around the completion mechanism, similar to what |
|
907 | 904 | readline does at the command line when the TAB key is hit. By |
|
908 | 905 | exposing it as a method, it can be used by other non-readline |
|
909 | 906 | environments (such as GUIs) for text completion. |
|
910 | 907 | |
|
911 | 908 | Simple usage example: |
|
912 | 909 | |
|
913 | 910 | In [1]: x = 'hello' |
|
914 | 911 | |
|
915 | 912 | In [2]: __IP.complete('x.l') |
|
916 | 913 | Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']""" |
|
917 | 914 | |
|
918 | 915 | complete = self.Completer.complete |
|
919 | 916 | state = 0 |
|
920 | 917 | # use a dict so we get unique keys, since ipyhton's multiple |
|
921 | 918 | # completers can return duplicates. |
|
922 | 919 | comps = {} |
|
923 | 920 | while True: |
|
924 | 921 | newcomp = complete(text,state) |
|
925 | 922 | if newcomp is None: |
|
926 | 923 | break |
|
927 | 924 | comps[newcomp] = 1 |
|
928 | 925 | state += 1 |
|
929 | 926 | outcomps = comps.keys() |
|
930 | 927 | outcomps.sort() |
|
931 | 928 | return outcomps |
|
932 | 929 | |
|
933 | 930 | def set_completer_frame(self, frame=None): |
|
934 | 931 | if frame: |
|
935 | 932 | self.Completer.namespace = frame.f_locals |
|
936 | 933 | self.Completer.global_namespace = frame.f_globals |
|
937 | 934 | else: |
|
938 | 935 | self.Completer.namespace = self.user_ns |
|
939 | 936 | self.Completer.global_namespace = self.user_global_ns |
|
940 | 937 | |
|
941 | 938 | def init_auto_alias(self): |
|
942 | 939 | """Define some aliases automatically. |
|
943 | 940 | |
|
944 | 941 | These are ALL parameter-less aliases""" |
|
945 | 942 | |
|
946 | 943 | for alias,cmd in self.auto_alias: |
|
947 | 944 | self.alias_table[alias] = (0,cmd) |
|
948 | 945 | |
|
949 | 946 | def alias_table_validate(self,verbose=0): |
|
950 | 947 | """Update information about the alias table. |
|
951 | 948 | |
|
952 | 949 | In particular, make sure no Python keywords/builtins are in it.""" |
|
953 | 950 | |
|
954 | 951 | no_alias = self.no_alias |
|
955 | 952 | for k in self.alias_table.keys(): |
|
956 | 953 | if k in no_alias: |
|
957 | 954 | del self.alias_table[k] |
|
958 | 955 | if verbose: |
|
959 | 956 | print ("Deleting alias <%s>, it's a Python " |
|
960 | 957 | "keyword or builtin." % k) |
|
961 | 958 | |
|
962 | 959 | def set_autoindent(self,value=None): |
|
963 | 960 | """Set the autoindent flag, checking for readline support. |
|
964 | 961 | |
|
965 | 962 | If called with no arguments, it acts as a toggle.""" |
|
966 | 963 | |
|
967 | 964 | if not self.has_readline: |
|
968 | 965 | if os.name == 'posix': |
|
969 | 966 | warn("The auto-indent feature requires the readline library") |
|
970 | 967 | self.autoindent = 0 |
|
971 | 968 | return |
|
972 | 969 | if value is None: |
|
973 | 970 | self.autoindent = not self.autoindent |
|
974 | 971 | else: |
|
975 | 972 | self.autoindent = value |
|
976 | 973 | |
|
977 | 974 | def rc_set_toggle(self,rc_field,value=None): |
|
978 | 975 | """Set or toggle a field in IPython's rc config. structure. |
|
979 | 976 | |
|
980 | 977 | If called with no arguments, it acts as a toggle. |
|
981 | 978 | |
|
982 | 979 | If called with a non-existent field, the resulting AttributeError |
|
983 | 980 | exception will propagate out.""" |
|
984 | 981 | |
|
985 | 982 | rc_val = getattr(self.rc,rc_field) |
|
986 | 983 | if value is None: |
|
987 | 984 | value = not rc_val |
|
988 | 985 | setattr(self.rc,rc_field,value) |
|
989 | 986 | |
|
990 | 987 | def user_setup(self,ipythondir,rc_suffix,mode='install'): |
|
991 | 988 | """Install the user configuration directory. |
|
992 | 989 | |
|
993 | 990 | Can be called when running for the first time or to upgrade the user's |
|
994 | 991 | .ipython/ directory with the mode parameter. Valid modes are 'install' |
|
995 | 992 | and 'upgrade'.""" |
|
996 | 993 | |
|
997 | 994 | def wait(): |
|
998 | 995 | try: |
|
999 | 996 | raw_input("Please press <RETURN> to start IPython.") |
|
1000 | 997 | except EOFError: |
|
1001 | 998 | print >> Term.cout |
|
1002 | 999 | print '*'*70 |
|
1003 | 1000 | |
|
1004 | 1001 | cwd = os.getcwd() # remember where we started |
|
1005 | 1002 | glb = glob.glob |
|
1006 | 1003 | print '*'*70 |
|
1007 | 1004 | if mode == 'install': |
|
1008 | 1005 | print \ |
|
1009 | 1006 | """Welcome to IPython. I will try to create a personal configuration directory |
|
1010 | 1007 | where you can customize many aspects of IPython's functionality in:\n""" |
|
1011 | 1008 | else: |
|
1012 | 1009 | print 'I am going to upgrade your configuration in:' |
|
1013 | 1010 | |
|
1014 | 1011 | print ipythondir |
|
1015 | 1012 | |
|
1016 | 1013 | rcdirend = os.path.join('IPython','UserConfig') |
|
1017 | 1014 | cfg = lambda d: os.path.join(d,rcdirend) |
|
1018 | 1015 | try: |
|
1019 | 1016 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] |
|
1020 | 1017 | except IOError: |
|
1021 | 1018 | warning = """ |
|
1022 | 1019 | Installation error. IPython's directory was not found. |
|
1023 | 1020 | |
|
1024 | 1021 | Check the following: |
|
1025 | 1022 | |
|
1026 | 1023 | The ipython/IPython directory should be in a directory belonging to your |
|
1027 | 1024 | PYTHONPATH environment variable (that is, it should be in a directory |
|
1028 | 1025 | belonging to sys.path). You can copy it explicitly there or just link to it. |
|
1029 | 1026 | |
|
1030 | 1027 | IPython will proceed with builtin defaults. |
|
1031 | 1028 | """ |
|
1032 | 1029 | warn(warning) |
|
1033 | 1030 | wait() |
|
1034 | 1031 | return |
|
1035 | 1032 | |
|
1036 | 1033 | if mode == 'install': |
|
1037 | 1034 | try: |
|
1038 | 1035 | shutil.copytree(rcdir,ipythondir) |
|
1039 | 1036 | os.chdir(ipythondir) |
|
1040 | 1037 | rc_files = glb("ipythonrc*") |
|
1041 | 1038 | for rc_file in rc_files: |
|
1042 | 1039 | os.rename(rc_file,rc_file+rc_suffix) |
|
1043 | 1040 | except: |
|
1044 | 1041 | warning = """ |
|
1045 | 1042 | |
|
1046 | 1043 | There was a problem with the installation: |
|
1047 | 1044 | %s |
|
1048 | 1045 | Try to correct it or contact the developers if you think it's a bug. |
|
1049 | 1046 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] |
|
1050 | 1047 | warn(warning) |
|
1051 | 1048 | wait() |
|
1052 | 1049 | return |
|
1053 | 1050 | |
|
1054 | 1051 | elif mode == 'upgrade': |
|
1055 | 1052 | try: |
|
1056 | 1053 | os.chdir(ipythondir) |
|
1057 | 1054 | except: |
|
1058 | 1055 | print """ |
|
1059 | 1056 | Can not upgrade: changing to directory %s failed. Details: |
|
1060 | 1057 | %s |
|
1061 | 1058 | """ % (ipythondir,sys.exc_info()[1]) |
|
1062 | 1059 | wait() |
|
1063 | 1060 | return |
|
1064 | 1061 | else: |
|
1065 | 1062 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) |
|
1066 | 1063 | for new_full_path in sources: |
|
1067 | 1064 | new_filename = os.path.basename(new_full_path) |
|
1068 | 1065 | if new_filename.startswith('ipythonrc'): |
|
1069 | 1066 | new_filename = new_filename + rc_suffix |
|
1070 | 1067 | # The config directory should only contain files, skip any |
|
1071 | 1068 | # directories which may be there (like CVS) |
|
1072 | 1069 | if os.path.isdir(new_full_path): |
|
1073 | 1070 | continue |
|
1074 | 1071 | if os.path.exists(new_filename): |
|
1075 | 1072 | old_file = new_filename+'.old' |
|
1076 | 1073 | if os.path.exists(old_file): |
|
1077 | 1074 | os.remove(old_file) |
|
1078 | 1075 | os.rename(new_filename,old_file) |
|
1079 | 1076 | shutil.copy(new_full_path,new_filename) |
|
1080 | 1077 | else: |
|
1081 | 1078 | raise ValueError,'unrecognized mode for install:',`mode` |
|
1082 | 1079 | |
|
1083 | 1080 | # Fix line-endings to those native to each platform in the config |
|
1084 | 1081 | # directory. |
|
1085 | 1082 | try: |
|
1086 | 1083 | os.chdir(ipythondir) |
|
1087 | 1084 | except: |
|
1088 | 1085 | print """ |
|
1089 | 1086 | Problem: changing to directory %s failed. |
|
1090 | 1087 | Details: |
|
1091 | 1088 | %s |
|
1092 | 1089 | |
|
1093 | 1090 | Some configuration files may have incorrect line endings. This should not |
|
1094 | 1091 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) |
|
1095 | 1092 | wait() |
|
1096 | 1093 | else: |
|
1097 | 1094 | for fname in glb('ipythonrc*'): |
|
1098 | 1095 | try: |
|
1099 | 1096 | native_line_ends(fname,backup=0) |
|
1100 | 1097 | except IOError: |
|
1101 | 1098 | pass |
|
1102 | 1099 | |
|
1103 | 1100 | if mode == 'install': |
|
1104 | 1101 | print """ |
|
1105 | 1102 | Successful installation! |
|
1106 | 1103 | |
|
1107 | 1104 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the |
|
1108 | 1105 | IPython manual (there are both HTML and PDF versions supplied with the |
|
1109 | 1106 | distribution) to make sure that your system environment is properly configured |
|
1110 | 1107 | to take advantage of IPython's features. |
|
1111 | 1108 | |
|
1112 | 1109 | Important note: the configuration system has changed! The old system is |
|
1113 | 1110 | still in place, but its setting may be partly overridden by the settings in |
|
1114 | 1111 | "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file |
|
1115 | 1112 | if some of the new settings bother you. |
|
1116 | 1113 | |
|
1117 | 1114 | """ |
|
1118 | 1115 | else: |
|
1119 | 1116 | print """ |
|
1120 | 1117 | Successful upgrade! |
|
1121 | 1118 | |
|
1122 | 1119 | All files in your directory: |
|
1123 | 1120 | %(ipythondir)s |
|
1124 | 1121 | which would have been overwritten by the upgrade were backed up with a .old |
|
1125 | 1122 | extension. If you had made particular customizations in those files you may |
|
1126 | 1123 | want to merge them back into the new files.""" % locals() |
|
1127 | 1124 | wait() |
|
1128 | 1125 | os.chdir(cwd) |
|
1129 | 1126 | # end user_setup() |
|
1130 | 1127 | |
|
1131 | 1128 | def atexit_operations(self): |
|
1132 | 1129 | """This will be executed at the time of exit. |
|
1133 | 1130 | |
|
1134 | 1131 | Saving of persistent data should be performed here. """ |
|
1135 | 1132 | |
|
1136 | 1133 | #print '*** IPython exit cleanup ***' # dbg |
|
1137 | 1134 | # input history |
|
1138 | 1135 | self.savehist() |
|
1139 | 1136 | |
|
1140 | 1137 | # Cleanup all tempfiles left around |
|
1141 | 1138 | for tfile in self.tempfiles: |
|
1142 | 1139 | try: |
|
1143 | 1140 | os.unlink(tfile) |
|
1144 | 1141 | except OSError: |
|
1145 | 1142 | pass |
|
1146 | 1143 | |
|
1147 | 1144 | # save the "persistent data" catch-all dictionary |
|
1148 | 1145 | self.hooks.shutdown_hook() |
|
1149 | 1146 | |
|
1150 | 1147 | def savehist(self): |
|
1151 | 1148 | """Save input history to a file (via readline library).""" |
|
1152 | 1149 | try: |
|
1153 | 1150 | self.readline.write_history_file(self.histfile) |
|
1154 | 1151 | except: |
|
1155 | 1152 | print 'Unable to save IPython command history to file: ' + \ |
|
1156 | 1153 | `self.histfile` |
|
1157 | 1154 | |
|
1158 | 1155 | def pre_readline(self): |
|
1159 | 1156 | """readline hook to be used at the start of each line. |
|
1160 | 1157 | |
|
1161 | 1158 | Currently it handles auto-indent only.""" |
|
1162 | 1159 | |
|
1163 | 1160 | #debugx('self.indent_current_nsp','pre_readline:') |
|
1164 | 1161 | self.readline.insert_text(self.indent_current_str()) |
|
1165 | 1162 | |
|
1166 | 1163 | def init_readline(self): |
|
1167 | 1164 | """Command history completion/saving/reloading.""" |
|
1168 | 1165 | |
|
1169 | 1166 | import IPython.rlineimpl as readline |
|
1170 | 1167 | if not readline.have_readline: |
|
1171 | 1168 | self.has_readline = 0 |
|
1172 | 1169 | self.readline = None |
|
1173 | 1170 | # no point in bugging windows users with this every time: |
|
1174 | 1171 | warn('Readline services not available on this platform.') |
|
1175 | 1172 | else: |
|
1176 | 1173 | sys.modules['readline'] = readline |
|
1177 | 1174 | import atexit |
|
1178 | 1175 | from IPython.completer import IPCompleter |
|
1179 | 1176 | self.Completer = IPCompleter(self, |
|
1180 | 1177 | self.user_ns, |
|
1181 | 1178 | self.user_global_ns, |
|
1182 | 1179 | self.rc.readline_omit__names, |
|
1183 | 1180 | self.alias_table) |
|
1184 | 1181 | |
|
1185 | 1182 | # Platform-specific configuration |
|
1186 | 1183 | if os.name == 'nt': |
|
1187 | 1184 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1188 | 1185 | else: |
|
1189 | 1186 | self.readline_startup_hook = readline.set_startup_hook |
|
1190 | 1187 | |
|
1191 | 1188 | # Load user's initrc file (readline config) |
|
1192 | 1189 | inputrc_name = os.environ.get('INPUTRC') |
|
1193 | 1190 | if inputrc_name is None: |
|
1194 | 1191 | home_dir = get_home_dir() |
|
1195 | 1192 | if home_dir is not None: |
|
1196 | 1193 | inputrc_name = os.path.join(home_dir,'.inputrc') |
|
1197 | 1194 | if os.path.isfile(inputrc_name): |
|
1198 | 1195 | try: |
|
1199 | 1196 | readline.read_init_file(inputrc_name) |
|
1200 | 1197 | except: |
|
1201 | 1198 | warn('Problems reading readline initialization file <%s>' |
|
1202 | 1199 | % inputrc_name) |
|
1203 | 1200 | |
|
1204 | 1201 | self.has_readline = 1 |
|
1205 | 1202 | self.readline = readline |
|
1206 | 1203 | # save this in sys so embedded copies can restore it properly |
|
1207 | 1204 | sys.ipcompleter = self.Completer.complete |
|
1208 | 1205 | readline.set_completer(self.Completer.complete) |
|
1209 | 1206 | |
|
1210 | 1207 | # Configure readline according to user's prefs |
|
1211 | 1208 | for rlcommand in self.rc.readline_parse_and_bind: |
|
1212 | 1209 | readline.parse_and_bind(rlcommand) |
|
1213 | 1210 | |
|
1214 | 1211 | # remove some chars from the delimiters list |
|
1215 | 1212 | delims = readline.get_completer_delims() |
|
1216 | 1213 | delims = delims.translate(string._idmap, |
|
1217 | 1214 | self.rc.readline_remove_delims) |
|
1218 | 1215 | readline.set_completer_delims(delims) |
|
1219 | 1216 | # otherwise we end up with a monster history after a while: |
|
1220 | 1217 | readline.set_history_length(1000) |
|
1221 | 1218 | try: |
|
1222 | 1219 | #print '*** Reading readline history' # dbg |
|
1223 | 1220 | readline.read_history_file(self.histfile) |
|
1224 | 1221 | except IOError: |
|
1225 | 1222 | pass # It doesn't exist yet. |
|
1226 | 1223 | |
|
1227 | 1224 | atexit.register(self.atexit_operations) |
|
1228 | 1225 | del atexit |
|
1229 | 1226 | |
|
1230 | 1227 | # Configure auto-indent for all platforms |
|
1231 | 1228 | self.set_autoindent(self.rc.autoindent) |
|
1232 | 1229 | |
|
1233 | 1230 | def _should_recompile(self,e): |
|
1234 | 1231 | """Utility routine for edit_syntax_error""" |
|
1235 | 1232 | |
|
1236 | 1233 | if e.filename in ('<ipython console>','<input>','<string>', |
|
1237 | 1234 | '<console>',None): |
|
1238 | 1235 | |
|
1239 | 1236 | return False |
|
1240 | 1237 | try: |
|
1241 | 1238 | if (self.rc.autoedit_syntax and |
|
1242 | 1239 | not ask_yes_no('Return to editor to correct syntax error? ' |
|
1243 | 1240 | '[Y/n] ','y')): |
|
1244 | 1241 | return False |
|
1245 | 1242 | except EOFError: |
|
1246 | 1243 | return False |
|
1247 | 1244 | |
|
1248 | 1245 | def int0(x): |
|
1249 | 1246 | try: |
|
1250 | 1247 | return int(x) |
|
1251 | 1248 | except TypeError: |
|
1252 | 1249 | return 0 |
|
1253 | 1250 | # always pass integer line and offset values to editor hook |
|
1254 | 1251 | self.hooks.fix_error_editor(e.filename, |
|
1255 | 1252 | int0(e.lineno),int0(e.offset),e.msg) |
|
1256 | 1253 | return True |
|
1257 | 1254 | |
|
1258 | 1255 | def edit_syntax_error(self): |
|
1259 | 1256 | """The bottom half of the syntax error handler called in the main loop. |
|
1260 | 1257 | |
|
1261 | 1258 | Loop until syntax error is fixed or user cancels. |
|
1262 | 1259 | """ |
|
1263 | 1260 | |
|
1264 | 1261 | while self.SyntaxTB.last_syntax_error: |
|
1265 | 1262 | # copy and clear last_syntax_error |
|
1266 | 1263 | err = self.SyntaxTB.clear_err_state() |
|
1267 | 1264 | if not self._should_recompile(err): |
|
1268 | 1265 | return |
|
1269 | 1266 | try: |
|
1270 | 1267 | # may set last_syntax_error again if a SyntaxError is raised |
|
1271 | 1268 | self.safe_execfile(err.filename,self.shell.user_ns) |
|
1272 | 1269 | except: |
|
1273 | 1270 | self.showtraceback() |
|
1274 | 1271 | else: |
|
1275 | 1272 | f = file(err.filename) |
|
1276 | 1273 | try: |
|
1277 | 1274 | sys.displayhook(f.read()) |
|
1278 | 1275 | finally: |
|
1279 | 1276 | f.close() |
|
1280 | 1277 | |
|
1281 | 1278 | def showsyntaxerror(self, filename=None): |
|
1282 | 1279 | """Display the syntax error that just occurred. |
|
1283 | 1280 | |
|
1284 | 1281 | This doesn't display a stack trace because there isn't one. |
|
1285 | 1282 | |
|
1286 | 1283 | If a filename is given, it is stuffed in the exception instead |
|
1287 | 1284 | of what was there before (because Python's parser always uses |
|
1288 | 1285 | "<string>" when reading from a string). |
|
1289 | 1286 | """ |
|
1290 | 1287 | etype, value, last_traceback = sys.exc_info() |
|
1291 | 1288 | |
|
1292 | 1289 | # See note about these variables in showtraceback() below |
|
1293 | 1290 | sys.last_type = etype |
|
1294 | 1291 | sys.last_value = value |
|
1295 | 1292 | sys.last_traceback = last_traceback |
|
1296 | 1293 | |
|
1297 | 1294 | if filename and etype is SyntaxError: |
|
1298 | 1295 | # Work hard to stuff the correct filename in the exception |
|
1299 | 1296 | try: |
|
1300 | 1297 | msg, (dummy_filename, lineno, offset, line) = value |
|
1301 | 1298 | except: |
|
1302 | 1299 | # Not the format we expect; leave it alone |
|
1303 | 1300 | pass |
|
1304 | 1301 | else: |
|
1305 | 1302 | # Stuff in the right filename |
|
1306 | 1303 | try: |
|
1307 | 1304 | # Assume SyntaxError is a class exception |
|
1308 | 1305 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1309 | 1306 | except: |
|
1310 | 1307 | # If that failed, assume SyntaxError is a string |
|
1311 | 1308 | value = msg, (filename, lineno, offset, line) |
|
1312 | 1309 | self.SyntaxTB(etype,value,[]) |
|
1313 | 1310 | |
|
1314 | 1311 | def debugger(self): |
|
1315 | 1312 | """Call the pdb debugger.""" |
|
1316 | 1313 | |
|
1317 | 1314 | if not self.rc.pdb: |
|
1318 | 1315 | return |
|
1319 | 1316 | pdb.pm() |
|
1320 | 1317 | |
|
1321 | 1318 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): |
|
1322 | 1319 | """Display the exception that just occurred.""" |
|
1323 | 1320 | |
|
1324 | 1321 | # Though this won't be called by syntax errors in the input line, |
|
1325 | 1322 | # there may be SyntaxError cases whith imported code. |
|
1326 | 1323 | if exc_tuple is None: |
|
1327 | 1324 | etype, value, tb = sys.exc_info() |
|
1328 | 1325 | else: |
|
1329 | 1326 | etype, value, tb = exc_tuple |
|
1330 | 1327 | if etype is SyntaxError: |
|
1331 | 1328 | self.showsyntaxerror(filename) |
|
1332 | 1329 | else: |
|
1333 | 1330 | # WARNING: these variables are somewhat deprecated and not |
|
1334 | 1331 | # necessarily safe to use in a threaded environment, but tools |
|
1335 | 1332 | # like pdb depend on their existence, so let's set them. If we |
|
1336 | 1333 | # find problems in the field, we'll need to revisit their use. |
|
1337 | 1334 | sys.last_type = etype |
|
1338 | 1335 | sys.last_value = value |
|
1339 | 1336 | sys.last_traceback = tb |
|
1340 | 1337 | |
|
1341 | 1338 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) |
|
1342 | 1339 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1343 | 1340 | # pdb mucks up readline, fix it back |
|
1344 | 1341 | self.readline.set_completer(self.Completer.complete) |
|
1345 | 1342 | |
|
1346 | 1343 | def mainloop(self,banner=None): |
|
1347 | 1344 | """Creates the local namespace and starts the mainloop. |
|
1348 | 1345 | |
|
1349 | 1346 | If an optional banner argument is given, it will override the |
|
1350 | 1347 | internally created default banner.""" |
|
1351 | 1348 | |
|
1352 | 1349 | if self.rc.c: # Emulate Python's -c option |
|
1353 | 1350 | self.exec_init_cmd() |
|
1354 | 1351 | if banner is None: |
|
1355 | 1352 | if not self.rc.banner: |
|
1356 | 1353 | banner = '' |
|
1357 | 1354 | # banner is string? Use it directly! |
|
1358 | 1355 | elif isinstance(self.rc.banner,basestring): |
|
1359 | 1356 | banner = self.rc.banner |
|
1360 | 1357 | else: |
|
1361 | 1358 | banner = self.BANNER+self.banner2 |
|
1362 | 1359 | |
|
1363 | 1360 | self.interact(banner) |
|
1364 | 1361 | |
|
1365 | 1362 | def exec_init_cmd(self): |
|
1366 | 1363 | """Execute a command given at the command line. |
|
1367 | 1364 | |
|
1368 | 1365 | This emulates Python's -c option.""" |
|
1369 | 1366 | |
|
1370 | 1367 | #sys.argv = ['-c'] |
|
1371 | 1368 | self.push(self.rc.c) |
|
1372 | 1369 | |
|
1373 | 1370 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): |
|
1374 | 1371 | """Embeds IPython into a running python program. |
|
1375 | 1372 | |
|
1376 | 1373 | Input: |
|
1377 | 1374 | |
|
1378 | 1375 | - header: An optional header message can be specified. |
|
1379 | 1376 | |
|
1380 | 1377 | - local_ns, global_ns: working namespaces. If given as None, the |
|
1381 | 1378 | IPython-initialized one is updated with __main__.__dict__, so that |
|
1382 | 1379 | program variables become visible but user-specific configuration |
|
1383 | 1380 | remains possible. |
|
1384 | 1381 | |
|
1385 | 1382 | - stack_depth: specifies how many levels in the stack to go to |
|
1386 | 1383 | looking for namespaces (when local_ns and global_ns are None). This |
|
1387 | 1384 | allows an intermediate caller to make sure that this function gets |
|
1388 | 1385 | the namespace from the intended level in the stack. By default (0) |
|
1389 | 1386 | it will get its locals and globals from the immediate caller. |
|
1390 | 1387 | |
|
1391 | 1388 | Warning: it's possible to use this in a program which is being run by |
|
1392 | 1389 | IPython itself (via %run), but some funny things will happen (a few |
|
1393 | 1390 | globals get overwritten). In the future this will be cleaned up, as |
|
1394 | 1391 | there is no fundamental reason why it can't work perfectly.""" |
|
1395 | 1392 | |
|
1396 | 1393 | # Get locals and globals from caller |
|
1397 | 1394 | if local_ns is None or global_ns is None: |
|
1398 | 1395 | call_frame = sys._getframe(stack_depth).f_back |
|
1399 | 1396 | |
|
1400 | 1397 | if local_ns is None: |
|
1401 | 1398 | local_ns = call_frame.f_locals |
|
1402 | 1399 | if global_ns is None: |
|
1403 | 1400 | global_ns = call_frame.f_globals |
|
1404 | 1401 | |
|
1405 | 1402 | # Update namespaces and fire up interpreter |
|
1406 | 1403 | |
|
1407 | 1404 | # The global one is easy, we can just throw it in |
|
1408 | 1405 | self.user_global_ns = global_ns |
|
1409 | 1406 | |
|
1410 | 1407 | # but the user/local one is tricky: ipython needs it to store internal |
|
1411 | 1408 | # data, but we also need the locals. We'll copy locals in the user |
|
1412 | 1409 | # one, but will track what got copied so we can delete them at exit. |
|
1413 | 1410 | # This is so that a later embedded call doesn't see locals from a |
|
1414 | 1411 | # previous call (which most likely existed in a separate scope). |
|
1415 | 1412 | local_varnames = local_ns.keys() |
|
1416 | 1413 | self.user_ns.update(local_ns) |
|
1417 | 1414 | |
|
1418 | 1415 | # Patch for global embedding to make sure that things don't overwrite |
|
1419 | 1416 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
1420 | 1417 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
1421 | 1418 | if local_ns is None and global_ns is None: |
|
1422 | 1419 | self.user_global_ns.update(__main__.__dict__) |
|
1423 | 1420 | |
|
1424 | 1421 | # make sure the tab-completer has the correct frame information, so it |
|
1425 | 1422 | # actually completes using the frame's locals/globals |
|
1426 | 1423 | self.set_completer_frame() |
|
1427 | 1424 | |
|
1428 | 1425 | # before activating the interactive mode, we need to make sure that |
|
1429 | 1426 | # all names in the builtin namespace needed by ipython point to |
|
1430 | 1427 | # ourselves, and not to other instances. |
|
1431 | 1428 | self.add_builtins() |
|
1432 | 1429 | |
|
1433 | 1430 | self.interact(header) |
|
1434 | 1431 | |
|
1435 | 1432 | # now, purge out the user namespace from anything we might have added |
|
1436 | 1433 | # from the caller's local namespace |
|
1437 | 1434 | delvar = self.user_ns.pop |
|
1438 | 1435 | for var in local_varnames: |
|
1439 | 1436 | delvar(var,None) |
|
1440 | 1437 | # and clean builtins we may have overridden |
|
1441 | 1438 | self.clean_builtins() |
|
1442 | 1439 | |
|
1443 | 1440 | def interact(self, banner=None): |
|
1444 | 1441 | """Closely emulate the interactive Python console. |
|
1445 | 1442 | |
|
1446 | 1443 | The optional banner argument specify the banner to print |
|
1447 | 1444 | before the first interaction; by default it prints a banner |
|
1448 | 1445 | similar to the one printed by the real Python interpreter, |
|
1449 | 1446 | followed by the current class name in parentheses (so as not |
|
1450 | 1447 | to confuse this with the real interpreter -- since it's so |
|
1451 | 1448 | close!). |
|
1452 | 1449 | |
|
1453 | 1450 | """ |
|
1454 | 1451 | cprt = 'Type "copyright", "credits" or "license" for more information.' |
|
1455 | 1452 | if banner is None: |
|
1456 | 1453 | self.write("Python %s on %s\n%s\n(%s)\n" % |
|
1457 | 1454 | (sys.version, sys.platform, cprt, |
|
1458 | 1455 | self.__class__.__name__)) |
|
1459 | 1456 | else: |
|
1460 | 1457 | self.write(banner) |
|
1461 | 1458 | |
|
1462 | 1459 | more = 0 |
|
1463 | 1460 | |
|
1464 | 1461 | # Mark activity in the builtins |
|
1465 | 1462 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1466 | 1463 | |
|
1467 | 1464 | # exit_now is set by a call to %Exit or %Quit |
|
1468 | 1465 | self.exit_now = False |
|
1469 | 1466 | while not self.exit_now: |
|
1470 | 1467 | if more: |
|
1471 | 1468 | prompt = self.outputcache.prompt2 |
|
1472 | 1469 | if self.autoindent: |
|
1473 | 1470 | self.readline_startup_hook(self.pre_readline) |
|
1474 | 1471 | else: |
|
1475 | 1472 | prompt = self.outputcache.prompt1 |
|
1476 | 1473 | try: |
|
1477 | 1474 | line = self.raw_input(prompt,more) |
|
1478 | 1475 | if self.autoindent: |
|
1479 | 1476 | self.readline_startup_hook(None) |
|
1480 | 1477 | except KeyboardInterrupt: |
|
1481 | 1478 | self.write('\nKeyboardInterrupt\n') |
|
1482 | 1479 | self.resetbuffer() |
|
1483 | 1480 | # keep cache in sync with the prompt counter: |
|
1484 | 1481 | self.outputcache.prompt_count -= 1 |
|
1485 | 1482 | |
|
1486 | 1483 | if self.autoindent: |
|
1487 | 1484 | self.indent_current_nsp = 0 |
|
1488 | 1485 | more = 0 |
|
1489 | 1486 | except EOFError: |
|
1490 | 1487 | if self.autoindent: |
|
1491 | 1488 | self.readline_startup_hook(None) |
|
1492 | 1489 | self.write('\n') |
|
1493 | 1490 | self.exit() |
|
1494 | 1491 | except bdb.BdbQuit: |
|
1495 | 1492 | warn('The Python debugger has exited with a BdbQuit exception.\n' |
|
1496 | 1493 | 'Because of how pdb handles the stack, it is impossible\n' |
|
1497 | 1494 | 'for IPython to properly format this particular exception.\n' |
|
1498 | 1495 | 'IPython will resume normal operation.') |
|
1499 | 1496 | except: |
|
1500 | 1497 | # exceptions here are VERY RARE, but they can be triggered |
|
1501 | 1498 | # asynchronously by signal handlers, for example. |
|
1502 | 1499 | self.showtraceback() |
|
1503 | 1500 | else: |
|
1504 | 1501 | more = self.push(line) |
|
1505 | 1502 | if (self.SyntaxTB.last_syntax_error and |
|
1506 | 1503 | self.rc.autoedit_syntax): |
|
1507 | 1504 | self.edit_syntax_error() |
|
1508 | 1505 | |
|
1509 | 1506 | # We are off again... |
|
1510 | 1507 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
1511 | 1508 | |
|
1512 | 1509 | def excepthook(self, etype, value, tb): |
|
1513 | 1510 | """One more defense for GUI apps that call sys.excepthook. |
|
1514 | 1511 | |
|
1515 | 1512 | GUI frameworks like wxPython trap exceptions and call |
|
1516 | 1513 | sys.excepthook themselves. I guess this is a feature that |
|
1517 | 1514 | enables them to keep running after exceptions that would |
|
1518 | 1515 | otherwise kill their mainloop. This is a bother for IPython |
|
1519 | 1516 | which excepts to catch all of the program exceptions with a try: |
|
1520 | 1517 | except: statement. |
|
1521 | 1518 | |
|
1522 | 1519 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1523 | 1520 | any app directly invokes sys.excepthook, it will look to the user like |
|
1524 | 1521 | IPython crashed. In order to work around this, we can disable the |
|
1525 | 1522 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1526 | 1523 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1527 | 1524 | call sys.excepthook will generate a regular-looking exception from |
|
1528 | 1525 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1529 | 1526 | crashes. |
|
1530 | 1527 | |
|
1531 | 1528 | This hook should be used sparingly, only in places which are not likely |
|
1532 | 1529 | to be true IPython errors. |
|
1533 | 1530 | """ |
|
1534 | 1531 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1535 | 1532 | |
|
1536 | 1533 | def transform_alias(self, alias,rest=''): |
|
1537 | 1534 | """ Transform alias to system command string |
|
1538 | 1535 | |
|
1539 | 1536 | """ |
|
1540 | 1537 | nargs,cmd = self.alias_table[alias] |
|
1541 | 1538 | if ' ' in cmd and os.path.isfile(cmd): |
|
1542 | 1539 | cmd = '"%s"' % cmd |
|
1543 | 1540 | |
|
1544 | 1541 | # Expand the %l special to be the user's input line |
|
1545 | 1542 | if cmd.find('%l') >= 0: |
|
1546 | 1543 | cmd = cmd.replace('%l',rest) |
|
1547 | 1544 | rest = '' |
|
1548 | 1545 | if nargs==0: |
|
1549 | 1546 | # Simple, argument-less aliases |
|
1550 | 1547 | cmd = '%s %s' % (cmd,rest) |
|
1551 | 1548 | else: |
|
1552 | 1549 | # Handle aliases with positional arguments |
|
1553 | 1550 | args = rest.split(None,nargs) |
|
1554 | 1551 | if len(args)< nargs: |
|
1555 | 1552 | error('Alias <%s> requires %s arguments, %s given.' % |
|
1556 | 1553 | (alias,nargs,len(args))) |
|
1557 | 1554 | return None |
|
1558 | 1555 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
1559 | 1556 | # Now call the macro, evaluating in the user's namespace |
|
1560 | 1557 | |
|
1561 | 1558 | return cmd |
|
1562 | 1559 | |
|
1563 | 1560 | def call_alias(self,alias,rest=''): |
|
1564 | 1561 | """Call an alias given its name and the rest of the line. |
|
1565 | 1562 | |
|
1566 | 1563 | This is only used to provide backwards compatibility for users of |
|
1567 | 1564 | ipalias(), use of which is not recommended for anymore.""" |
|
1568 | 1565 | |
|
1569 | 1566 | # Now call the macro, evaluating in the user's namespace |
|
1570 | 1567 | cmd = self.transform_alias(alias, rest) |
|
1571 | 1568 | try: |
|
1572 | 1569 | self.system(cmd) |
|
1573 | 1570 | except: |
|
1574 | 1571 | self.showtraceback() |
|
1575 | 1572 | |
|
1576 | 1573 | def indent_current_str(self): |
|
1577 | 1574 | """return the current level of indentation as a string""" |
|
1578 | 1575 | return self.indent_current_nsp * ' ' |
|
1579 | 1576 | |
|
1580 | 1577 | def autoindent_update(self,line): |
|
1581 | 1578 | """Keep track of the indent level.""" |
|
1582 | 1579 | |
|
1583 | 1580 | #debugx('line') |
|
1584 | 1581 | #debugx('self.indent_current_nsp') |
|
1585 | 1582 | if self.autoindent: |
|
1586 | 1583 | if line: |
|
1587 | 1584 | inisp = num_ini_spaces(line) |
|
1588 | 1585 | if inisp < self.indent_current_nsp: |
|
1589 | 1586 | self.indent_current_nsp = inisp |
|
1590 | 1587 | |
|
1591 | 1588 | if line[-1] == ':': |
|
1592 | 1589 | self.indent_current_nsp += 4 |
|
1593 | 1590 | elif dedent_re.match(line): |
|
1594 | 1591 | self.indent_current_nsp -= 4 |
|
1595 | 1592 | else: |
|
1596 | 1593 | self.indent_current_nsp = 0 |
|
1597 | 1594 | |
|
1598 | 1595 | def runlines(self,lines): |
|
1599 | 1596 | """Run a string of one or more lines of source. |
|
1600 | 1597 | |
|
1601 | 1598 | This method is capable of running a string containing multiple source |
|
1602 | 1599 | lines, as if they had been entered at the IPython prompt. Since it |
|
1603 | 1600 | exposes IPython's processing machinery, the given strings can contain |
|
1604 | 1601 | magic calls (%magic), special shell access (!cmd), etc.""" |
|
1605 | 1602 | |
|
1606 | 1603 | # We must start with a clean buffer, in case this is run from an |
|
1607 | 1604 | # interactive IPython session (via a magic, for example). |
|
1608 | 1605 | self.resetbuffer() |
|
1609 | 1606 | lines = lines.split('\n') |
|
1610 | 1607 | more = 0 |
|
1611 | 1608 | for line in lines: |
|
1612 | 1609 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1613 | 1610 | # NOT skip even a blank line if we are in a code block (more is |
|
1614 | 1611 | # true) |
|
1615 | 1612 | if line or more: |
|
1616 | 1613 | more = self.push(self.prefilter(line,more)) |
|
1617 | 1614 | # IPython's runsource returns None if there was an error |
|
1618 | 1615 | # compiling the code. This allows us to stop processing right |
|
1619 | 1616 | # away, so the user gets the error message at the right place. |
|
1620 | 1617 | if more is None: |
|
1621 | 1618 | break |
|
1622 | 1619 | # final newline in case the input didn't have it, so that the code |
|
1623 | 1620 | # actually does get executed |
|
1624 | 1621 | if more: |
|
1625 | 1622 | self.push('\n') |
|
1626 | 1623 | |
|
1627 | 1624 | def runsource(self, source, filename='<input>', symbol='single'): |
|
1628 | 1625 | """Compile and run some source in the interpreter. |
|
1629 | 1626 | |
|
1630 | 1627 | Arguments are as for compile_command(). |
|
1631 | 1628 | |
|
1632 | 1629 | One several things can happen: |
|
1633 | 1630 | |
|
1634 | 1631 | 1) The input is incorrect; compile_command() raised an |
|
1635 | 1632 | exception (SyntaxError or OverflowError). A syntax traceback |
|
1636 | 1633 | will be printed by calling the showsyntaxerror() method. |
|
1637 | 1634 | |
|
1638 | 1635 | 2) The input is incomplete, and more input is required; |
|
1639 | 1636 | compile_command() returned None. Nothing happens. |
|
1640 | 1637 | |
|
1641 | 1638 | 3) The input is complete; compile_command() returned a code |
|
1642 | 1639 | object. The code is executed by calling self.runcode() (which |
|
1643 | 1640 | also handles run-time exceptions, except for SystemExit). |
|
1644 | 1641 | |
|
1645 | 1642 | The return value is: |
|
1646 | 1643 | |
|
1647 | 1644 | - True in case 2 |
|
1648 | 1645 | |
|
1649 | 1646 | - False in the other cases, unless an exception is raised, where |
|
1650 | 1647 | None is returned instead. This can be used by external callers to |
|
1651 | 1648 | know whether to continue feeding input or not. |
|
1652 | 1649 | |
|
1653 | 1650 | The return value can be used to decide whether to use sys.ps1 or |
|
1654 | 1651 | sys.ps2 to prompt the next line.""" |
|
1655 | 1652 | |
|
1656 | 1653 | try: |
|
1657 | 1654 | code = self.compile(source,filename,symbol) |
|
1658 | 1655 | except (OverflowError, SyntaxError, ValueError): |
|
1659 | 1656 | # Case 1 |
|
1660 | 1657 | self.showsyntaxerror(filename) |
|
1661 | 1658 | return None |
|
1662 | 1659 | |
|
1663 | 1660 | if code is None: |
|
1664 | 1661 | # Case 2 |
|
1665 | 1662 | return True |
|
1666 | 1663 | |
|
1667 | 1664 | # Case 3 |
|
1668 | 1665 | # We store the code object so that threaded shells and |
|
1669 | 1666 | # custom exception handlers can access all this info if needed. |
|
1670 | 1667 | # The source corresponding to this can be obtained from the |
|
1671 | 1668 | # buffer attribute as '\n'.join(self.buffer). |
|
1672 | 1669 | self.code_to_run = code |
|
1673 | 1670 | # now actually execute the code object |
|
1674 | 1671 | if self.runcode(code) == 0: |
|
1675 | 1672 | return False |
|
1676 | 1673 | else: |
|
1677 | 1674 | return None |
|
1678 | 1675 | |
|
1679 | 1676 | def runcode(self,code_obj): |
|
1680 | 1677 | """Execute a code object. |
|
1681 | 1678 | |
|
1682 | 1679 | When an exception occurs, self.showtraceback() is called to display a |
|
1683 | 1680 | traceback. |
|
1684 | 1681 | |
|
1685 | 1682 | Return value: a flag indicating whether the code to be run completed |
|
1686 | 1683 | successfully: |
|
1687 | 1684 | |
|
1688 | 1685 | - 0: successful execution. |
|
1689 | 1686 | - 1: an error occurred. |
|
1690 | 1687 | """ |
|
1691 | 1688 | |
|
1692 | 1689 | # Set our own excepthook in case the user code tries to call it |
|
1693 | 1690 | # directly, so that the IPython crash handler doesn't get triggered |
|
1694 | 1691 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
1695 | 1692 | |
|
1696 | 1693 | # we save the original sys.excepthook in the instance, in case config |
|
1697 | 1694 | # code (such as magics) needs access to it. |
|
1698 | 1695 | self.sys_excepthook = old_excepthook |
|
1699 | 1696 | outflag = 1 # happens in more places, so it's easier as default |
|
1700 | 1697 | try: |
|
1701 | 1698 | try: |
|
1702 | 1699 | # Embedded instances require separate global/local namespaces |
|
1703 | 1700 | # so they can see both the surrounding (local) namespace and |
|
1704 | 1701 | # the module-level globals when called inside another function. |
|
1705 | 1702 | if self.embedded: |
|
1706 | 1703 | exec code_obj in self.user_global_ns, self.user_ns |
|
1707 | 1704 | # Normal (non-embedded) instances should only have a single |
|
1708 | 1705 | # namespace for user code execution, otherwise functions won't |
|
1709 | 1706 | # see interactive top-level globals. |
|
1710 | 1707 | else: |
|
1711 | 1708 | exec code_obj in self.user_ns |
|
1712 | 1709 | finally: |
|
1713 | 1710 | # Reset our crash handler in place |
|
1714 | 1711 | sys.excepthook = old_excepthook |
|
1715 | 1712 | except SystemExit: |
|
1716 | 1713 | self.resetbuffer() |
|
1717 | 1714 | self.showtraceback() |
|
1718 | 1715 | warn("Type exit or quit to exit IPython " |
|
1719 | 1716 | "(%Exit or %Quit do so unconditionally).",level=1) |
|
1720 | 1717 | except self.custom_exceptions: |
|
1721 | 1718 | etype,value,tb = sys.exc_info() |
|
1722 | 1719 | self.CustomTB(etype,value,tb) |
|
1723 | 1720 | except: |
|
1724 | 1721 | self.showtraceback() |
|
1725 | 1722 | else: |
|
1726 | 1723 | outflag = 0 |
|
1727 | 1724 | if softspace(sys.stdout, 0): |
|
1728 | 1725 | |
|
1729 | 1726 | # Flush out code object which has been run (and source) |
|
1730 | 1727 | self.code_to_run = None |
|
1731 | 1728 | return outflag |
|
1732 | 1729 | |
|
1733 | 1730 | def push(self, line): |
|
1734 | 1731 | """Push a line to the interpreter. |
|
1735 | 1732 | |
|
1736 | 1733 | The line should not have a trailing newline; it may have |
|
1737 | 1734 | internal newlines. The line is appended to a buffer and the |
|
1738 | 1735 | interpreter's runsource() method is called with the |
|
1739 | 1736 | concatenated contents of the buffer as source. If this |
|
1740 | 1737 | indicates that the command was executed or invalid, the buffer |
|
1741 | 1738 | is reset; otherwise, the command is incomplete, and the buffer |
|
1742 | 1739 | is left as it was after the line was appended. The return |
|
1743 | 1740 | value is 1 if more input is required, 0 if the line was dealt |
|
1744 | 1741 | with in some way (this is the same as runsource()). |
|
1745 | 1742 | """ |
|
1746 | 1743 | |
|
1747 | 1744 | # autoindent management should be done here, and not in the |
|
1748 | 1745 | # interactive loop, since that one is only seen by keyboard input. We |
|
1749 | 1746 | # need this done correctly even for code run via runlines (which uses |
|
1750 | 1747 | # push). |
|
1751 | 1748 | |
|
1752 | 1749 | #print 'push line: <%s>' % line # dbg |
|
1753 | 1750 | self.autoindent_update(line) |
|
1754 | 1751 | |
|
1755 | 1752 | self.buffer.append(line) |
|
1756 | 1753 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
1757 | 1754 | if not more: |
|
1758 | 1755 | self.resetbuffer() |
|
1759 | 1756 | return more |
|
1760 | 1757 | |
|
1761 | 1758 | def resetbuffer(self): |
|
1762 | 1759 | """Reset the input buffer.""" |
|
1763 | 1760 | self.buffer[:] = [] |
|
1764 | 1761 | |
|
1765 | 1762 | def raw_input(self,prompt='',continue_prompt=False): |
|
1766 | 1763 | """Write a prompt and read a line. |
|
1767 | 1764 | |
|
1768 | 1765 | The returned line does not include the trailing newline. |
|
1769 | 1766 | When the user enters the EOF key sequence, EOFError is raised. |
|
1770 | 1767 | |
|
1771 | 1768 | Optional inputs: |
|
1772 | 1769 | |
|
1773 | 1770 | - prompt(''): a string to be printed to prompt the user. |
|
1774 | 1771 | |
|
1775 | 1772 | - continue_prompt(False): whether this line is the first one or a |
|
1776 | 1773 | continuation in a sequence of inputs. |
|
1777 | 1774 | """ |
|
1778 | 1775 | |
|
1779 | 1776 | line = raw_input_original(prompt) |
|
1780 | 1777 | |
|
1781 | 1778 | # Try to be reasonably smart about not re-indenting pasted input more |
|
1782 | 1779 | # than necessary. We do this by trimming out the auto-indent initial |
|
1783 | 1780 | # spaces, if the user's actual input started itself with whitespace. |
|
1784 | 1781 | #debugx('self.buffer[-1]') |
|
1785 | 1782 | |
|
1786 | 1783 | if self.autoindent: |
|
1787 | 1784 | if num_ini_spaces(line) > self.indent_current_nsp: |
|
1788 | 1785 | line = line[self.indent_current_nsp:] |
|
1789 | 1786 | self.indent_current_nsp = 0 |
|
1790 | 1787 | |
|
1791 | 1788 | # store the unfiltered input before the user has any chance to modify |
|
1792 | 1789 | # it. |
|
1793 | 1790 | if line.strip(): |
|
1794 | 1791 | if continue_prompt: |
|
1795 | 1792 | self.input_hist_raw[-1] += '%s\n' % line |
|
1796 | 1793 | else: |
|
1797 | 1794 | self.input_hist_raw.append('%s\n' % line) |
|
1798 | 1795 | |
|
1799 | 1796 | lineout = self.prefilter(line,continue_prompt) |
|
1800 | 1797 | return lineout |
|
1801 | 1798 | |
|
1802 | 1799 | def split_user_input(self,line): |
|
1803 | 1800 | """Split user input into pre-char, function part and rest.""" |
|
1804 | 1801 | |
|
1805 | 1802 | lsplit = self.line_split.match(line) |
|
1806 | 1803 | if lsplit is None: # no regexp match returns None |
|
1807 | 1804 | try: |
|
1808 | 1805 | iFun,theRest = line.split(None,1) |
|
1809 | 1806 | except ValueError: |
|
1810 | 1807 | iFun,theRest = line,'' |
|
1811 | 1808 | pre = re.match('^(\s*)(.*)',line).groups()[0] |
|
1812 | 1809 | else: |
|
1813 | 1810 | pre,iFun,theRest = lsplit.groups() |
|
1814 | 1811 | |
|
1815 | 1812 | #print 'line:<%s>' % line # dbg |
|
1816 | 1813 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg |
|
1817 | 1814 | return pre,iFun.strip(),theRest |
|
1818 | 1815 | |
|
1819 | 1816 | def _prefilter(self, line, continue_prompt): |
|
1820 | 1817 | """Calls different preprocessors, depending on the form of line.""" |
|
1821 | 1818 | |
|
1822 | 1819 | # All handlers *must* return a value, even if it's blank (''). |
|
1823 | 1820 | |
|
1824 | 1821 | # Lines are NOT logged here. Handlers should process the line as |
|
1825 | 1822 | # needed, update the cache AND log it (so that the input cache array |
|
1826 | 1823 | # stays synced). |
|
1827 | 1824 | |
|
1828 | 1825 | # This function is _very_ delicate, and since it's also the one which |
|
1829 | 1826 | # determines IPython's response to user input, it must be as efficient |
|
1830 | 1827 | # as possible. For this reason it has _many_ returns in it, trying |
|
1831 | 1828 | # always to exit as quickly as it can figure out what it needs to do. |
|
1832 | 1829 | |
|
1833 | 1830 | # This function is the main responsible for maintaining IPython's |
|
1834 | 1831 | # behavior respectful of Python's semantics. So be _very_ careful if |
|
1835 | 1832 | # making changes to anything here. |
|
1836 | 1833 | |
|
1837 | 1834 | #..................................................................... |
|
1838 | 1835 | # Code begins |
|
1839 | 1836 | |
|
1840 | 1837 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg |
|
1841 | 1838 | |
|
1842 | 1839 | # save the line away in case we crash, so the post-mortem handler can |
|
1843 | 1840 | # record it |
|
1844 | 1841 | self._last_input_line = line |
|
1845 | 1842 | |
|
1846 | 1843 | #print '***line: <%s>' % line # dbg |
|
1847 | 1844 | |
|
1848 | 1845 | # the input history needs to track even empty lines |
|
1849 | 1846 | stripped = line.strip() |
|
1850 | 1847 | |
|
1851 | 1848 | if not stripped: |
|
1852 | 1849 | if not continue_prompt: |
|
1853 | 1850 | self.outputcache.prompt_count -= 1 |
|
1854 | 1851 | return self.handle_normal(line,continue_prompt) |
|
1855 | 1852 | #return self.handle_normal('',continue_prompt) |
|
1856 | 1853 | |
|
1857 | 1854 | # print '***cont',continue_prompt # dbg |
|
1858 | 1855 | # special handlers are only allowed for single line statements |
|
1859 | 1856 | if continue_prompt and not self.rc.multi_line_specials: |
|
1860 | 1857 | return self.handle_normal(line,continue_prompt) |
|
1861 | 1858 | |
|
1862 | 1859 | |
|
1863 | 1860 | # For the rest, we need the structure of the input |
|
1864 | 1861 | pre,iFun,theRest = self.split_user_input(line) |
|
1865 | 1862 | |
|
1866 | 1863 | # See whether any pre-existing handler can take care of it |
|
1867 | 1864 | |
|
1868 | 1865 | rewritten = self.hooks.input_prefilter(stripped) |
|
1869 | 1866 | if rewritten != stripped: # ok, some prefilter did something |
|
1870 | 1867 | rewritten = pre + rewritten # add indentation |
|
1871 | 1868 | return self.handle_normal(rewritten) |
|
1872 | 1869 | |
|
1873 | 1870 | |
|
1874 | 1871 | |
|
1875 | 1872 | |
|
1876 | 1873 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
1877 | 1874 | |
|
1878 | 1875 | # First check for explicit escapes in the last/first character |
|
1879 | 1876 | handler = None |
|
1880 | 1877 | if line[-1] == self.ESC_HELP: |
|
1881 | 1878 | handler = self.esc_handlers.get(line[-1]) # the ? can be at the end |
|
1882 | 1879 | if handler is None: |
|
1883 | 1880 | # look at the first character of iFun, NOT of line, so we skip |
|
1884 | 1881 | # leading whitespace in multiline input |
|
1885 | 1882 | handler = self.esc_handlers.get(iFun[0:1]) |
|
1886 | 1883 | if handler is not None: |
|
1887 | 1884 | return handler(line,continue_prompt,pre,iFun,theRest) |
|
1888 | 1885 | # Emacs ipython-mode tags certain input lines |
|
1889 | 1886 | if line.endswith('# PYTHON-MODE'): |
|
1890 | 1887 | return self.handle_emacs(line,continue_prompt) |
|
1891 | 1888 | |
|
1892 | 1889 | # Next, check if we can automatically execute this thing |
|
1893 | 1890 | |
|
1894 | 1891 | # Allow ! in multi-line statements if multi_line_specials is on: |
|
1895 | 1892 | if continue_prompt and self.rc.multi_line_specials and \ |
|
1896 | 1893 | iFun.startswith(self.ESC_SHELL): |
|
1897 | 1894 | return self.handle_shell_escape(line,continue_prompt, |
|
1898 | 1895 | pre=pre,iFun=iFun, |
|
1899 | 1896 | theRest=theRest) |
|
1900 | 1897 | |
|
1901 | 1898 | # Let's try to find if the input line is a magic fn |
|
1902 | 1899 | oinfo = None |
|
1903 | 1900 | if hasattr(self,'magic_'+iFun): |
|
1904 | 1901 | # WARNING: _ofind uses getattr(), so it can consume generators and |
|
1905 | 1902 | # cause other side effects. |
|
1906 | 1903 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
1907 | 1904 | if oinfo['ismagic']: |
|
1908 | 1905 | # Be careful not to call magics when a variable assignment is |
|
1909 | 1906 | # being made (ls='hi', for example) |
|
1910 | 1907 | if self.rc.automagic and \ |
|
1911 | 1908 | (len(theRest)==0 or theRest[0] not in '!=()<>,') and \ |
|
1912 | 1909 | (self.rc.multi_line_specials or not continue_prompt): |
|
1913 | 1910 | return self.handle_magic(line,continue_prompt, |
|
1914 | 1911 | pre,iFun,theRest) |
|
1915 | 1912 | else: |
|
1916 | 1913 | return self.handle_normal(line,continue_prompt) |
|
1917 | 1914 | |
|
1918 | 1915 | # If the rest of the line begins with an (in)equality, assginment or |
|
1919 | 1916 | # function call, we should not call _ofind but simply execute it. |
|
1920 | 1917 | # This avoids spurious geattr() accesses on objects upon assignment. |
|
1921 | 1918 | # |
|
1922 | 1919 | # It also allows users to assign to either alias or magic names true |
|
1923 | 1920 | # python variables (the magic/alias systems always take second seat to |
|
1924 | 1921 | # true python code). |
|
1925 | 1922 | if theRest and theRest[0] in '!=()': |
|
1926 | 1923 | return self.handle_normal(line,continue_prompt) |
|
1927 | 1924 | |
|
1928 | 1925 | if oinfo is None: |
|
1929 | 1926 | # let's try to ensure that _oinfo is ONLY called when autocall is |
|
1930 | 1927 | # on. Since it has inevitable potential side effects, at least |
|
1931 | 1928 | # having autocall off should be a guarantee to the user that no |
|
1932 | 1929 | # weird things will happen. |
|
1933 | 1930 | |
|
1934 | 1931 | if self.rc.autocall: |
|
1935 | 1932 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
1936 | 1933 | else: |
|
1937 | 1934 | # in this case, all that's left is either an alias or |
|
1938 | 1935 | # processing the line normally. |
|
1939 | 1936 | if iFun in self.alias_table: |
|
1940 | 1937 | return self.handle_alias(line,continue_prompt, |
|
1941 | 1938 | pre,iFun,theRest) |
|
1942 | 1939 | |
|
1943 | 1940 | else: |
|
1944 | 1941 | return self.handle_normal(line,continue_prompt) |
|
1945 | 1942 | |
|
1946 | 1943 | if not oinfo['found']: |
|
1947 | 1944 | return self.handle_normal(line,continue_prompt) |
|
1948 | 1945 | else: |
|
1949 | 1946 | #print 'pre<%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
1950 | 1947 | if oinfo['isalias']: |
|
1951 | 1948 | return self.handle_alias(line,continue_prompt, |
|
1952 | 1949 | pre,iFun,theRest) |
|
1953 | 1950 | |
|
1954 | 1951 | if (self.rc.autocall |
|
1955 | 1952 | and |
|
1956 | 1953 | ( |
|
1957 | 1954 | #only consider exclusion re if not "," or ";" autoquoting |
|
1958 | 1955 | (pre == self.ESC_QUOTE or pre == self.ESC_QUOTE2 |
|
1959 | 1956 | or pre == self.ESC_PAREN) or |
|
1960 | 1957 | (not self.re_exclude_auto.match(theRest))) |
|
1961 | 1958 | and |
|
1962 | 1959 | self.re_fun_name.match(iFun) and |
|
1963 | 1960 | callable(oinfo['obj'])) : |
|
1964 | 1961 | #print 'going auto' # dbg |
|
1965 | 1962 | return self.handle_auto(line,continue_prompt, |
|
1966 | 1963 | pre,iFun,theRest,oinfo['obj']) |
|
1967 | 1964 | else: |
|
1968 | 1965 | #print 'was callable?', callable(oinfo['obj']) # dbg |
|
1969 | 1966 | return self.handle_normal(line,continue_prompt) |
|
1970 | 1967 | |
|
1971 | 1968 | # If we get here, we have a normal Python line. Log and return. |
|
1972 | 1969 | return self.handle_normal(line,continue_prompt) |
|
1973 | 1970 | |
|
1974 | 1971 | def _prefilter_dumb(self, line, continue_prompt): |
|
1975 | 1972 | """simple prefilter function, for debugging""" |
|
1976 | 1973 | return self.handle_normal(line,continue_prompt) |
|
1977 | 1974 | |
|
1978 | 1975 | # Set the default prefilter() function (this can be user-overridden) |
|
1979 | 1976 | prefilter = _prefilter |
|
1980 | 1977 | |
|
1981 | 1978 | def handle_normal(self,line,continue_prompt=None, |
|
1982 | 1979 | pre=None,iFun=None,theRest=None): |
|
1983 | 1980 | """Handle normal input lines. Use as a template for handlers.""" |
|
1984 | 1981 | |
|
1985 | 1982 | # With autoindent on, we need some way to exit the input loop, and I |
|
1986 | 1983 | # don't want to force the user to have to backspace all the way to |
|
1987 | 1984 | # clear the line. The rule will be in this case, that either two |
|
1988 | 1985 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
1989 | 1986 | # of a size different to the indent level, will exit the input loop. |
|
1990 | 1987 | |
|
1991 | 1988 | if (continue_prompt and self.autoindent and line.isspace() and |
|
1992 | 1989 | (0 < abs(len(line) - self.indent_current_nsp) <= 2 or |
|
1993 | 1990 | (self.buffer[-1]).isspace() )): |
|
1994 | 1991 | line = '' |
|
1995 | 1992 | |
|
1996 | 1993 | self.log(line,continue_prompt) |
|
1997 | 1994 | return line |
|
1998 | 1995 | |
|
1999 | 1996 | def handle_alias(self,line,continue_prompt=None, |
|
2000 | 1997 | pre=None,iFun=None,theRest=None): |
|
2001 | 1998 | """Handle alias input lines. """ |
|
2002 | 1999 | |
|
2003 | 2000 | # pre is needed, because it carries the leading whitespace. Otherwise |
|
2004 | 2001 | # aliases won't work in indented sections. |
|
2005 | 2002 | transformed = self.transform_alias(iFun, theRest) |
|
2006 | 2003 | line_out = '%s_ip.system(%s)' % (pre, make_quoted_expr( transformed )) |
|
2007 | 2004 | self.log(line_out,continue_prompt) |
|
2008 | 2005 | return line_out |
|
2009 | 2006 | |
|
2010 | 2007 | def handle_shell_escape(self, line, continue_prompt=None, |
|
2011 | 2008 | pre=None,iFun=None,theRest=None): |
|
2012 | 2009 | """Execute the line in a shell, empty return value""" |
|
2013 | 2010 | |
|
2014 | 2011 | #print 'line in :', `line` # dbg |
|
2015 | 2012 | # Example of a special handler. Others follow a similar pattern. |
|
2016 | 2013 | if line.lstrip().startswith('!!'): |
|
2017 | 2014 | # rewrite iFun/theRest to properly hold the call to %sx and |
|
2018 | 2015 | # the actual command to be executed, so handle_magic can work |
|
2019 | 2016 | # correctly |
|
2020 | 2017 | theRest = '%s %s' % (iFun[2:],theRest) |
|
2021 | 2018 | iFun = 'sx' |
|
2022 | 2019 | return self.handle_magic('%ssx %s' % (self.ESC_MAGIC, |
|
2023 | 2020 | line.lstrip()[2:]), |
|
2024 | 2021 | continue_prompt,pre,iFun,theRest) |
|
2025 | 2022 | else: |
|
2026 | 2023 | cmd=line.lstrip().lstrip('!') |
|
2027 | 2024 | line_out = '%s_ip.system(%s)' % (pre,make_quoted_expr(cmd)) |
|
2028 | 2025 | # update cache/log and return |
|
2029 | 2026 | self.log(line_out,continue_prompt) |
|
2030 | 2027 | return line_out |
|
2031 | 2028 | |
|
2032 | 2029 | def handle_magic(self, line, continue_prompt=None, |
|
2033 | 2030 | pre=None,iFun=None,theRest=None): |
|
2034 | 2031 | """Execute magic functions.""" |
|
2035 | 2032 | |
|
2036 | 2033 | |
|
2037 | 2034 | cmd = '%s_ip.magic(%s)' % (pre,make_quoted_expr(iFun + " " + theRest)) |
|
2038 | 2035 | self.log(cmd,continue_prompt) |
|
2039 | 2036 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg |
|
2040 | 2037 | return cmd |
|
2041 | 2038 | |
|
2042 | 2039 | def handle_auto(self, line, continue_prompt=None, |
|
2043 | 2040 | pre=None,iFun=None,theRest=None,obj=None): |
|
2044 | 2041 | """Hande lines which can be auto-executed, quoting if requested.""" |
|
2045 | 2042 | |
|
2046 | 2043 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2047 | 2044 | |
|
2048 | 2045 | # This should only be active for single-line input! |
|
2049 | 2046 | if continue_prompt: |
|
2050 | 2047 | self.log(line,continue_prompt) |
|
2051 | 2048 | return line |
|
2052 | 2049 | |
|
2053 | 2050 | auto_rewrite = True |
|
2054 | 2051 | |
|
2055 | 2052 | if pre == self.ESC_QUOTE: |
|
2056 | 2053 | # Auto-quote splitting on whitespace |
|
2057 | 2054 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) |
|
2058 | 2055 | elif pre == self.ESC_QUOTE2: |
|
2059 | 2056 | # Auto-quote whole string |
|
2060 | 2057 | newcmd = '%s("%s")' % (iFun,theRest) |
|
2061 | 2058 | elif pre == self.ESC_PAREN: |
|
2062 | 2059 | newcmd = '%s(%s)' % (iFun,",".join(theRest.split())) |
|
2063 | 2060 | else: |
|
2064 | 2061 | # Auto-paren. |
|
2065 | 2062 | # We only apply it to argument-less calls if the autocall |
|
2066 | 2063 | # parameter is set to 2. We only need to check that autocall is < |
|
2067 | 2064 | # 2, since this function isn't called unless it's at least 1. |
|
2068 | 2065 | if not theRest and (self.rc.autocall < 2): |
|
2069 | 2066 | newcmd = '%s %s' % (iFun,theRest) |
|
2070 | 2067 | auto_rewrite = False |
|
2071 | 2068 | else: |
|
2072 | 2069 | if theRest.startswith('['): |
|
2073 | 2070 | if hasattr(obj,'__getitem__'): |
|
2074 | 2071 | # Don't autocall in this case: item access for an object |
|
2075 | 2072 | # which is BOTH callable and implements __getitem__. |
|
2076 | 2073 | newcmd = '%s %s' % (iFun,theRest) |
|
2077 | 2074 | auto_rewrite = False |
|
2078 | 2075 | else: |
|
2079 | 2076 | # if the object doesn't support [] access, go ahead and |
|
2080 | 2077 | # autocall |
|
2081 | 2078 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
2082 | 2079 | elif theRest.endswith(';'): |
|
2083 | 2080 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) |
|
2084 | 2081 | else: |
|
2085 | 2082 | newcmd = '%s(%s)' % (iFun.rstrip(), theRest) |
|
2086 | 2083 | |
|
2087 | 2084 | if auto_rewrite: |
|
2088 | 2085 | print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd |
|
2089 | 2086 | # log what is now valid Python, not the actual user input (without the |
|
2090 | 2087 | # final newline) |
|
2091 | 2088 | self.log(newcmd,continue_prompt) |
|
2092 | 2089 | return newcmd |
|
2093 | 2090 | |
|
2094 | 2091 | def handle_help(self, line, continue_prompt=None, |
|
2095 | 2092 | pre=None,iFun=None,theRest=None): |
|
2096 | 2093 | """Try to get some help for the object. |
|
2097 | 2094 | |
|
2098 | 2095 | obj? or ?obj -> basic information. |
|
2099 | 2096 | obj?? or ??obj -> more details. |
|
2100 | 2097 | """ |
|
2101 | 2098 | |
|
2102 | 2099 | # We need to make sure that we don't process lines which would be |
|
2103 | 2100 | # otherwise valid python, such as "x=1 # what?" |
|
2104 | 2101 | try: |
|
2105 | 2102 | codeop.compile_command(line) |
|
2106 | 2103 | except SyntaxError: |
|
2107 | 2104 | # We should only handle as help stuff which is NOT valid syntax |
|
2108 | 2105 | if line[0]==self.ESC_HELP: |
|
2109 | 2106 | line = line[1:] |
|
2110 | 2107 | elif line[-1]==self.ESC_HELP: |
|
2111 | 2108 | line = line[:-1] |
|
2112 | 2109 | self.log('#?'+line) |
|
2113 | 2110 | if line: |
|
2114 | 2111 | self.magic_pinfo(line) |
|
2115 | 2112 | else: |
|
2116 | 2113 | page(self.usage,screen_lines=self.rc.screen_length) |
|
2117 | 2114 | return '' # Empty string is needed here! |
|
2118 | 2115 | except: |
|
2119 | 2116 | # Pass any other exceptions through to the normal handler |
|
2120 | 2117 | return self.handle_normal(line,continue_prompt) |
|
2121 | 2118 | else: |
|
2122 | 2119 | # If the code compiles ok, we should handle it normally |
|
2123 | 2120 | return self.handle_normal(line,continue_prompt) |
|
2124 | 2121 | |
|
2125 | 2122 | def getapi(self): |
|
2126 | 2123 | """ Get an IPApi object for this shell instance |
|
2127 | 2124 | |
|
2128 | 2125 | Getting an IPApi object is always preferable to accessing the shell |
|
2129 | 2126 | directly, but this holds true especially for extensions. |
|
2130 | 2127 | |
|
2131 | 2128 | It should always be possible to implement an extension with IPApi |
|
2132 | 2129 | alone. If not, contact maintainer to request an addition. |
|
2133 | 2130 | |
|
2134 | 2131 | """ |
|
2135 | 2132 | return self.api |
|
2136 | 2133 | |
|
2137 | 2134 | def handle_emacs(self,line,continue_prompt=None, |
|
2138 | 2135 | pre=None,iFun=None,theRest=None): |
|
2139 | 2136 | """Handle input lines marked by python-mode.""" |
|
2140 | 2137 | |
|
2141 | 2138 | # Currently, nothing is done. Later more functionality can be added |
|
2142 | 2139 | # here if needed. |
|
2143 | 2140 | |
|
2144 | 2141 | # The input cache shouldn't be updated |
|
2145 | 2142 | |
|
2146 | 2143 | return line |
|
2147 | 2144 | |
|
2148 | 2145 | def mktempfile(self,data=None): |
|
2149 | 2146 | """Make a new tempfile and return its filename. |
|
2150 | 2147 | |
|
2151 | 2148 | This makes a call to tempfile.mktemp, but it registers the created |
|
2152 | 2149 | filename internally so ipython cleans it up at exit time. |
|
2153 | 2150 | |
|
2154 | 2151 | Optional inputs: |
|
2155 | 2152 | |
|
2156 | 2153 | - data(None): if data is given, it gets written out to the temp file |
|
2157 | 2154 | immediately, and the file is closed again.""" |
|
2158 | 2155 | |
|
2159 | 2156 | filename = tempfile.mktemp('.py','ipython_edit_') |
|
2160 | 2157 | self.tempfiles.append(filename) |
|
2161 | 2158 | |
|
2162 | 2159 | if data: |
|
2163 | 2160 | tmp_file = open(filename,'w') |
|
2164 | 2161 | tmp_file.write(data) |
|
2165 | 2162 | tmp_file.close() |
|
2166 | 2163 | return filename |
|
2167 | 2164 | |
|
2168 | 2165 | def write(self,data): |
|
2169 | 2166 | """Write a string to the default output""" |
|
2170 | 2167 | Term.cout.write(data) |
|
2171 | 2168 | |
|
2172 | 2169 | def write_err(self,data): |
|
2173 | 2170 | """Write a string to the default error output""" |
|
2174 | 2171 | Term.cerr.write(data) |
|
2175 | 2172 | |
|
2176 | 2173 | def exit(self): |
|
2177 | 2174 | """Handle interactive exit. |
|
2178 | 2175 | |
|
2179 | 2176 | This method sets the exit_now attribute.""" |
|
2180 | 2177 | |
|
2181 | 2178 | if self.rc.confirm_exit: |
|
2182 | 2179 | if ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
2183 | 2180 | self.exit_now = True |
|
2184 | 2181 | else: |
|
2185 | 2182 | self.exit_now = True |
|
2186 | 2183 | return self.exit_now |
|
2187 | 2184 | |
|
2188 | 2185 | def safe_execfile(self,fname,*where,**kw): |
|
2189 | 2186 | fname = os.path.expanduser(fname) |
|
2190 | 2187 | |
|
2191 | 2188 | # find things also in current directory |
|
2192 | 2189 | dname = os.path.dirname(fname) |
|
2193 | 2190 | if not sys.path.count(dname): |
|
2194 | 2191 | sys.path.append(dname) |
|
2195 | 2192 | |
|
2196 | 2193 | try: |
|
2197 | 2194 | xfile = open(fname) |
|
2198 | 2195 | except: |
|
2199 | 2196 | print >> Term.cerr, \ |
|
2200 | 2197 | 'Could not open file <%s> for safe execution.' % fname |
|
2201 | 2198 | return None |
|
2202 | 2199 | |
|
2203 | 2200 | kw.setdefault('islog',0) |
|
2204 | 2201 | kw.setdefault('quiet',1) |
|
2205 | 2202 | kw.setdefault('exit_ignore',0) |
|
2206 | 2203 | first = xfile.readline() |
|
2207 | 2204 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() |
|
2208 | 2205 | xfile.close() |
|
2209 | 2206 | # line by line execution |
|
2210 | 2207 | if first.startswith(loghead) or kw['islog']: |
|
2211 | 2208 | print 'Loading log file <%s> one line at a time...' % fname |
|
2212 | 2209 | if kw['quiet']: |
|
2213 | 2210 | stdout_save = sys.stdout |
|
2214 | 2211 | sys.stdout = StringIO.StringIO() |
|
2215 | 2212 | try: |
|
2216 | 2213 | globs,locs = where[0:2] |
|
2217 | 2214 | except: |
|
2218 | 2215 | try: |
|
2219 | 2216 | globs = locs = where[0] |
|
2220 | 2217 | except: |
|
2221 | 2218 | globs = locs = globals() |
|
2222 | 2219 | badblocks = [] |
|
2223 | 2220 | |
|
2224 | 2221 | # we also need to identify indented blocks of code when replaying |
|
2225 | 2222 | # logs and put them together before passing them to an exec |
|
2226 | 2223 | # statement. This takes a bit of regexp and look-ahead work in the |
|
2227 | 2224 | # file. It's easiest if we swallow the whole thing in memory |
|
2228 | 2225 | # first, and manually walk through the lines list moving the |
|
2229 | 2226 | # counter ourselves. |
|
2230 | 2227 | indent_re = re.compile('\s+\S') |
|
2231 | 2228 | xfile = open(fname) |
|
2232 | 2229 | filelines = xfile.readlines() |
|
2233 | 2230 | xfile.close() |
|
2234 | 2231 | nlines = len(filelines) |
|
2235 | 2232 | lnum = 0 |
|
2236 | 2233 | while lnum < nlines: |
|
2237 | 2234 | line = filelines[lnum] |
|
2238 | 2235 | lnum += 1 |
|
2239 | 2236 | # don't re-insert logger status info into cache |
|
2240 | 2237 | if line.startswith('#log#'): |
|
2241 | 2238 | continue |
|
2242 | 2239 | else: |
|
2243 | 2240 | # build a block of code (maybe a single line) for execution |
|
2244 | 2241 | block = line |
|
2245 | 2242 | try: |
|
2246 | 2243 | next = filelines[lnum] # lnum has already incremented |
|
2247 | 2244 | except: |
|
2248 | 2245 | next = None |
|
2249 | 2246 | while next and indent_re.match(next): |
|
2250 | 2247 | block += next |
|
2251 | 2248 | lnum += 1 |
|
2252 | 2249 | try: |
|
2253 | 2250 | next = filelines[lnum] |
|
2254 | 2251 | except: |
|
2255 | 2252 | next = None |
|
2256 | 2253 | # now execute the block of one or more lines |
|
2257 | 2254 | try: |
|
2258 | 2255 | exec block in globs,locs |
|
2259 | 2256 | except SystemExit: |
|
2260 | 2257 | pass |
|
2261 | 2258 | except: |
|
2262 | 2259 | badblocks.append(block.rstrip()) |
|
2263 | 2260 | if kw['quiet']: # restore stdout |
|
2264 | 2261 | sys.stdout.close() |
|
2265 | 2262 | sys.stdout = stdout_save |
|
2266 | 2263 | print 'Finished replaying log file <%s>' % fname |
|
2267 | 2264 | if badblocks: |
|
2268 | 2265 | print >> sys.stderr, ('\nThe following lines/blocks in file ' |
|
2269 | 2266 | '<%s> reported errors:' % fname) |
|
2270 | 2267 | |
|
2271 | 2268 | for badline in badblocks: |
|
2272 | 2269 | print >> sys.stderr, badline |
|
2273 | 2270 | else: # regular file execution |
|
2274 | 2271 | try: |
|
2275 | 2272 | execfile(fname,*where) |
|
2276 | 2273 | except SyntaxError: |
|
2277 | 2274 | self.showsyntaxerror() |
|
2278 | 2275 | warn('Failure executing file: <%s>' % fname) |
|
2279 | 2276 | except SystemExit,status: |
|
2280 | 2277 | if not kw['exit_ignore']: |
|
2281 | 2278 | self.showtraceback() |
|
2282 | 2279 | warn('Failure executing file: <%s>' % fname) |
|
2283 | 2280 | except: |
|
2284 | 2281 | self.showtraceback() |
|
2285 | 2282 | warn('Failure executing file: <%s>' % fname) |
|
2286 | 2283 | |
|
2287 | 2284 | #************************* end of file <iplib.py> ***************************** |
General Comments 0
You need to be logged in to leave comments.
Login now