##// END OF EJS Templates
Defaults rename, clean up api to use properties or direct access rather than...
fperez -
Show More
@@ -1,46 +1,46 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """ IPython extension: add %clear magic """
2 """ IPython extension: add %clear magic """
3
3
4 import IPython.ipapi
4 import IPython.ipapi
5 import gc
5 import gc
6 ip = IPython.ipapi.get()
6 ip = IPython.ipapi.get()
7
7
8
8
9 def clear_f(self,arg):
9 def clear_f(self,arg):
10 """ Clear various data (e.g. stored history data)
10 """ Clear various data (e.g. stored history data)
11
11
12 %clear out - clear output history
12 %clear out - clear output history
13 %clear in - clear input history
13 %clear in - clear input history
14 """
14 """
15
15
16 api = self.getapi()
16 api = self.getapi()
17 for target in arg.split():
17 for target in arg.split():
18 if target == 'out':
18 if target == 'out':
19 print "Flushing output cache (%d entries)" % len(api.user_ns()['_oh'])
19 print "Flushing output cache (%d entries)" % len(api.user_ns['_oh'])
20 self.outputcache.flush()
20 self.outputcache.flush()
21 elif target == 'in':
21 elif target == 'in':
22 print "Flushing input history"
22 print "Flushing input history"
23 from IPython import iplib
23 from IPython import iplib
24 del self.input_hist[:]
24 del self.input_hist[:]
25 del self.input_hist_raw[:]
25 del self.input_hist_raw[:]
26 for n in range(1,self.outputcache.prompt_count + 1):
26 for n in range(1,self.outputcache.prompt_count + 1):
27 key = '_i'+`n`
27 key = '_i'+`n`
28 try:
28 try:
29 del self.user_ns[key]
29 del self.user_ns[key]
30 except: pass
30 except: pass
31 elif target == 'array':
31 elif target == 'array':
32 try:
32 try:
33 pylab=ip.IP.pylab
33 pylab=ip.IP.pylab
34 for x in self.user_ns.keys():
34 for x in self.user_ns.keys():
35 if isinstance(self.user_ns[x],pylab.arraytype):
35 if isinstance(self.user_ns[x],pylab.arraytype):
36 del self.user_ns[x]
36 del self.user_ns[x]
37 except AttributeError:
37 except AttributeError:
38 print "Clear array only available in -pylab mode"
38 print "Clear array only available in -pylab mode"
39 gc.collect()
39 gc.collect()
40
40
41
41
42 ip.expose_magic("clear",clear_f)
42 ip.expose_magic("clear",clear_f)
43
43
44
44
45
45
46
46
@@ -1,66 +1,66 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """ IPython extension: new prefilters for output grabbing
2 """ IPython extension: new prefilters for output grabbing
3
3
4 Provides
4 Provides
5
5
6 var = %magic blah blah
6 var = %magic blah blah
7
7
8 var = !ls
8 var = !ls
9
9
10 $Id: genutils.py 1077 2006-01-24 18:15:27Z vivainio $
10 $Id: genutils.py 1077 2006-01-24 18:15:27Z vivainio $
11
11
12 """
12 """
13
13
14 import IPython.ipapi
14 import IPython.ipapi
15 from IPython.genutils import *
15 from IPython.genutils import *
16
16
17 ip = IPython.ipapi.get()
17 ip = IPython.ipapi.get()
18
18
19 import re
19 import re
20
20
21 def hnd_magic(line,mo):
21 def hnd_magic(line,mo):
22 """ Handle a = %mymagic blah blah """
22 """ Handle a = %mymagic blah blah """
23 #cmd = genutils.make_quoted_expr(mo.group('syscmd'))
23 #cmd = genutils.make_quoted_expr(mo.group('syscmd'))
24 #mag = 'ipmagic
24 #mag = 'ipmagic
25 #return "%s = %s"
25 #return "%s = %s"
26 var = mo.group('varname')
26 var = mo.group('varname')
27 cmd = mo.group('cmd')
27 cmd = mo.group('cmd')
28 expr = make_quoted_expr(cmd)
28 expr = make_quoted_expr(cmd)
29 return itpl('$var = _ip.magic($expr)')
29 return itpl('$var = _ip.magic($expr)')
30
30
31 def hnd_syscmd(line,mo):
31 def hnd_syscmd(line,mo):
32 """ Handle a = !ls """
32 """ Handle a = !ls """
33 #cmd = genutils.make_quoted_expr(mo.group('syscmd'))
33 #cmd = genutils.make_quoted_expr(mo.group('syscmd'))
34 #mag = 'ipmagic
34 #mag = 'ipmagic
35 #return "%s = %s"
35 #return "%s = %s"
36 var = mo.group('varname')
36 var = mo.group('varname')
37 cmd = mo.group('cmd')
37 cmd = mo.group('cmd')
38 expr = make_quoted_expr(itpl("sc -lv =$cmd"))
38 expr = make_quoted_expr(itpl("sc -lv =$cmd"))
39 return itpl('$var = _ip.magic($expr)')
39 return itpl('$var = _ip.magic($expr)')
40
40
41 def install_re_handler(pat, hnd):
41 def install_re_handler(pat, hnd):
42 ip.meta().re_prefilters.append((re.compile(pat), hnd))
42 ip.meta.re_prefilters.append((re.compile(pat), hnd))
43
43
44 def init_handlers():
44 def init_handlers():
45
45
46 ip.meta().re_prefilters = []
46 ip.meta.re_prefilters = []
47
47
48 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*%(?P<cmd>.*)',
48 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*%(?P<cmd>.*)',
49 hnd_magic
49 hnd_magic
50 )
50 )
51
51
52 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*!(?P<cmd>.*)',
52 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*!(?P<cmd>.*)',
53 hnd_syscmd
53 hnd_syscmd
54 )
54 )
55
55
56 init_handlers()
56 init_handlers()
57
57
58 def regex_prefilter_f(self,line):
58 def regex_prefilter_f(self,line):
59 for pat, handler in ip.meta().re_prefilters:
59 for pat, handler in ip.meta.re_prefilters:
60 mo = pat.match(line)
60 mo = pat.match(line)
61 if mo:
61 if mo:
62 return handler(line,mo)
62 return handler(line,mo)
63
63
64 raise IPython.ipapi.TryNext
64 raise IPython.ipapi.TryNext
65
65
66 ip.set_hook('input_prefilter', regex_prefilter_f) No newline at end of file
66 ip.set_hook('input_prefilter', regex_prefilter_f)
@@ -1,1841 +1,1841 b''
1 # -*- coding: iso-8859-1 -*-
1 # -*- coding: iso-8859-1 -*-
2
2
3 """
3 """
4 ``ipipe`` provides classes to be used in an interactive Python session. Doing a
4 ``ipipe`` provides classes to be used in an interactive Python session. Doing a
5 ``from ipipe import *`` is the preferred way to do this. The name of all
5 ``from ipipe import *`` is the preferred way to do this. The name of all
6 objects imported this way starts with ``i`` to minimize collisions.
6 objects imported this way starts with ``i`` to minimize collisions.
7
7
8 ``ipipe`` supports "pipeline expressions", which is something resembling Unix
8 ``ipipe`` supports "pipeline expressions", which is something resembling Unix
9 pipes. An example is:
9 pipes. An example is:
10
10
11 >>> ienv | isort("key.lower()")
11 >>> ienv | isort("key.lower()")
12
12
13 This gives a listing of all environment variables sorted by name.
13 This gives a listing of all environment variables sorted by name.
14
14
15
15
16 There are three types of objects in a pipeline expression:
16 There are three types of objects in a pipeline expression:
17
17
18 * ``Table``s: These objects produce items. Examples are ``ls`` (listing the
18 * ``Table``s: These objects produce items. Examples are ``ls`` (listing the
19 current directory, ``ienv`` (listing environment variables), ``ipwd`` (listing
19 current directory, ``ienv`` (listing environment variables), ``ipwd`` (listing
20 user account) and ``igrp`` (listing user groups). A ``Table`` must be the
20 user account) and ``igrp`` (listing user groups). A ``Table`` must be the
21 first object in a pipe expression.
21 first object in a pipe expression.
22
22
23 * ``Pipe``s: These objects sit in the middle of a pipe expression. They
23 * ``Pipe``s: These objects sit in the middle of a pipe expression. They
24 transform the input in some way (e.g. filtering or sorting it). Examples are:
24 transform the input in some way (e.g. filtering or sorting it). Examples are:
25 ``ifilter`` (which filters the input pipe), ``isort`` (which sorts the input
25 ``ifilter`` (which filters the input pipe), ``isort`` (which sorts the input
26 pipe) and ``ieval`` (which evaluates a function or expression for each object
26 pipe) and ``ieval`` (which evaluates a function or expression for each object
27 in the input pipe).
27 in the input pipe).
28
28
29 * ``Display``s: These objects can be put as the last object in a pipeline
29 * ``Display``s: These objects can be put as the last object in a pipeline
30 expression. There are responsible for displaying the result of the pipeline
30 expression. There are responsible for displaying the result of the pipeline
31 expression. If a pipeline expression doesn't end in a display object a default
31 expression. If a pipeline expression doesn't end in a display object a default
32 display objects will be used. One example is ``browse`` which is a ``curses``
32 display objects will be used. One example is ``browse`` which is a ``curses``
33 based browser.
33 based browser.
34
34
35
35
36 Adding support for pipeline expressions to your own objects can be done through
36 Adding support for pipeline expressions to your own objects can be done through
37 three extensions points (all of them optional):
37 three extensions points (all of them optional):
38
38
39 * An object that will be displayed as a row by a ``Display`` object should
39 * An object that will be displayed as a row by a ``Display`` object should
40 implement the method ``__xattrs__(self, mode)``. This method must return a
40 implement the method ``__xattrs__(self, mode)``. This method must return a
41 sequence of attribute names. This sequence may also contain integers, which
41 sequence of attribute names. This sequence may also contain integers, which
42 will be treated as sequence indizes. Also supported is ``None``, which uses
42 will be treated as sequence indizes. Also supported is ``None``, which uses
43 the object itself and callables which will be called with the object as the
43 the object itself and callables which will be called with the object as the
44 an argument. If ``__xattrs__()`` isn't implemented ``(None,)`` will be used as
44 an argument. If ``__xattrs__()`` isn't implemented ``(None,)`` will be used as
45 the attribute sequence (i.e. the object itself (it's ``repr()`` format) will
45 the attribute sequence (i.e. the object itself (it's ``repr()`` format) will
46 be being displayed. The global function ``xattrs()`` implements this
46 be being displayed. The global function ``xattrs()`` implements this
47 functionality.
47 functionality.
48
48
49 * When an object ``foo`` is displayed in the header, footer or table cell of the
49 * When an object ``foo`` is displayed in the header, footer or table cell of the
50 browser ``foo.__xrepr__(mode)`` is called. Mode can be ``"header"`` or
50 browser ``foo.__xrepr__(mode)`` is called. Mode can be ``"header"`` or
51 ``"footer"`` for the header or footer line and ``"cell"`` for a table cell.
51 ``"footer"`` for the header or footer line and ``"cell"`` for a table cell.
52 ``__xrepr__()```must return an iterable (e.g. by being a generator) which
52 ``__xrepr__()```must return an iterable (e.g. by being a generator) which
53 produces the following items: The first item should be a tuple containing
53 produces the following items: The first item should be a tuple containing
54 the alignment (-1 left aligned, 0 centered and 1 right aligned) and whether
54 the alignment (-1 left aligned, 0 centered and 1 right aligned) and whether
55 the complete output must be displayed or if the browser is allowed to stop
55 the complete output must be displayed or if the browser is allowed to stop
56 output after enough text has been produced (e.g. a syntax highlighted text
56 output after enough text has been produced (e.g. a syntax highlighted text
57 line would use ``True``, but for a large data structure (i.e. a nested list,
57 line would use ``True``, but for a large data structure (i.e. a nested list,
58 tuple or dictionary) ``False`` would be used). The other output ``__xrepr__()``
58 tuple or dictionary) ``False`` would be used). The other output ``__xrepr__()``
59 may produce is tuples of ``Style```objects and text (which contain the text
59 may produce is tuples of ``Style```objects and text (which contain the text
60 representation of the object; see the ``astyle`` module). If ``__xrepr__()``
60 representation of the object; see the ``astyle`` module). If ``__xrepr__()``
61 recursively outputs a data structure the function ``xrepr(object, mode)`` can
61 recursively outputs a data structure the function ``xrepr(object, mode)`` can
62 be used and ``"default"`` must be passed as the mode in these calls. This in
62 be used and ``"default"`` must be passed as the mode in these calls. This in
63 turn calls the ``__xrepr__()`` method on ``object`` (or uses ``repr(object)``
63 turn calls the ``__xrepr__()`` method on ``object`` (or uses ``repr(object)``
64 as the string representation if ``__xrepr__()`` doesn't exist).
64 as the string representation if ``__xrepr__()`` doesn't exist).
65
65
66 * Objects that can be iterated by ``Pipe``s must implement the method
66 * Objects that can be iterated by ``Pipe``s must implement the method
67 ``__xiter__(self, mode)``. ``mode`` can take the following values:
67 ``__xiter__(self, mode)``. ``mode`` can take the following values:
68
68
69 - ``"default"``: This is the default value and ist always used by pipeline
69 - ``"default"``: This is the default value and ist always used by pipeline
70 expressions. Other values are only used in the browser.
70 expressions. Other values are only used in the browser.
71 - ``None``: This value is passed by the browser. The object must return an
71 - ``None``: This value is passed by the browser. The object must return an
72 iterable of ``XMode`` objects describing all modes supported by the object.
72 iterable of ``XMode`` objects describing all modes supported by the object.
73 (This should never include ``"default"`` or ``None``).
73 (This should never include ``"default"`` or ``None``).
74 - Any other value that the object supports.
74 - Any other value that the object supports.
75
75
76 The global function ``xiter()`` can be called to get such an iterator. If
76 The global function ``xiter()`` can be called to get such an iterator. If
77 the method ``_xiter__`` isn't implemented, ``xiter()`` falls back to
77 the method ``_xiter__`` isn't implemented, ``xiter()`` falls back to
78 ``__iter__``. In addition to that, dictionaries and modules receive special
78 ``__iter__``. In addition to that, dictionaries and modules receive special
79 treatment (returning an iterator over ``(key, value)`` pairs). This makes it
79 treatment (returning an iterator over ``(key, value)`` pairs). This makes it
80 possible to use dictionaries and modules in pipeline expressions, for example:
80 possible to use dictionaries and modules in pipeline expressions, for example:
81
81
82 >>> import sys
82 >>> import sys
83 >>> sys | ifilter("isinstance(value, int)") | idump
83 >>> sys | ifilter("isinstance(value, int)") | idump
84 key |value
84 key |value
85 api_version| 1012
85 api_version| 1012
86 dllhandle | 503316480
86 dllhandle | 503316480
87 hexversion | 33817328
87 hexversion | 33817328
88 maxint |2147483647
88 maxint |2147483647
89 maxunicode | 65535
89 maxunicode | 65535
90 >>> sys.modules | ifilter("_.value is not None") | isort("_.key.lower()")
90 >>> sys.modules | ifilter("_.value is not None") | isort("_.key.lower()")
91 ...
91 ...
92
92
93 Note: The expression strings passed to ``ifilter()`` and ``isort()`` can
93 Note: The expression strings passed to ``ifilter()`` and ``isort()`` can
94 refer to the object to be filtered or sorted via the variable ``_`` and to any
94 refer to the object to be filtered or sorted via the variable ``_`` and to any
95 of the attributes of the object, i.e.:
95 of the attributes of the object, i.e.:
96
96
97 >>> sys.modules | ifilter("_.value is not None") | isort("_.key.lower()")
97 >>> sys.modules | ifilter("_.value is not None") | isort("_.key.lower()")
98
98
99 does the same as
99 does the same as
100
100
101 >>> sys.modules | ifilter("value is not None") | isort("key.lower()")
101 >>> sys.modules | ifilter("value is not None") | isort("key.lower()")
102
102
103 In addition to expression strings, it's possible to pass callables (taking
103 In addition to expression strings, it's possible to pass callables (taking
104 the object as an argument) to ``ifilter()``, ``isort()`` and ``ieval()``:
104 the object as an argument) to ``ifilter()``, ``isort()`` and ``ieval()``:
105
105
106 >>> sys | ifilter(lambda _:isinstance(_.value, int)) \
106 >>> sys | ifilter(lambda _:isinstance(_.value, int)) \
107 ... | ieval(lambda _: (_.key, hex(_.value))) | idump
107 ... | ieval(lambda _: (_.key, hex(_.value))) | idump
108 0 |1
108 0 |1
109 api_version|0x3f4
109 api_version|0x3f4
110 dllhandle |0x1e000000
110 dllhandle |0x1e000000
111 hexversion |0x20402f0
111 hexversion |0x20402f0
112 maxint |0x7fffffff
112 maxint |0x7fffffff
113 maxunicode |0xffff
113 maxunicode |0xffff
114 """
114 """
115
115
116 import sys, os, os.path, stat, glob, new, csv, datetime, types
116 import sys, os, os.path, stat, glob, new, csv, datetime, types
117 import itertools, mimetypes
117 import itertools, mimetypes
118
118
119 try: # Python 2.3 compatibility
119 try: # Python 2.3 compatibility
120 import collections
120 import collections
121 except ImportError:
121 except ImportError:
122 deque = list
122 deque = list
123 else:
123 else:
124 deque = collections.deque
124 deque = collections.deque
125
125
126 try: # Python 2.3 compatibility
126 try: # Python 2.3 compatibility
127 set
127 set
128 except NameError:
128 except NameError:
129 import sets
129 import sets
130 set = sets.Set
130 set = sets.Set
131
131
132 try: # Python 2.3 compatibility
132 try: # Python 2.3 compatibility
133 sorted
133 sorted
134 except NameError:
134 except NameError:
135 def sorted(iterator, key=None, reverse=False):
135 def sorted(iterator, key=None, reverse=False):
136 items = list(iterator)
136 items = list(iterator)
137 if key is not None:
137 if key is not None:
138 items.sort(lambda i1, i2: cmp(key(i1), key(i2)))
138 items.sort(lambda i1, i2: cmp(key(i1), key(i2)))
139 else:
139 else:
140 items.sort()
140 items.sort()
141 if reverse:
141 if reverse:
142 items.reverse()
142 items.reverse()
143 return items
143 return items
144
144
145 try:
145 try:
146 import pwd
146 import pwd
147 except ImportError:
147 except ImportError:
148 pwd = None
148 pwd = None
149
149
150 try:
150 try:
151 import grp
151 import grp
152 except ImportError:
152 except ImportError:
153 grp = None
153 grp = None
154
154
155 import path
155 import path
156 try:
156 try:
157 from IPython import genutils, ipapi
157 from IPython import genutils, ipapi
158 except ImportError:
158 except ImportError:
159 genutils = None
159 genutils = None
160 ipapi = None
160 ipapi = None
161
161
162 import astyle
162 import astyle
163
163
164
164
165 __all__ = [
165 __all__ = [
166 "ifile", "ils", "iglob", "iwalk", "ipwdentry", "ipwd", "igrpentry", "igrp",
166 "ifile", "ils", "iglob", "iwalk", "ipwdentry", "ipwd", "igrpentry", "igrp",
167 "icsv", "ix", "ichain", "isort", "ifilter", "ieval", "ienum", "ienv",
167 "icsv", "ix", "ichain", "isort", "ifilter", "ieval", "ienum", "ienv",
168 "idump", "iless"
168 "idump", "iless"
169 ]
169 ]
170
170
171
171
172 os.stat_float_times(True) # enable microseconds
172 os.stat_float_times(True) # enable microseconds
173
173
174
174
175 class AttrNamespace(object):
175 class AttrNamespace(object):
176 """
176 """
177 Helper class that is used for providing a namespace for evaluating
177 Helper class that is used for providing a namespace for evaluating
178 expressions containing attribute names of an object.
178 expressions containing attribute names of an object.
179 """
179 """
180 def __init__(self, wrapped):
180 def __init__(self, wrapped):
181 self.wrapped = wrapped
181 self.wrapped = wrapped
182
182
183 def __getitem__(self, name):
183 def __getitem__(self, name):
184 if name == "_":
184 if name == "_":
185 return self.wrapped
185 return self.wrapped
186 try:
186 try:
187 return getattr(self.wrapped, name)
187 return getattr(self.wrapped, name)
188 except AttributeError:
188 except AttributeError:
189 raise KeyError(name)
189 raise KeyError(name)
190
190
191 # Python 2.3 compatibility
191 # Python 2.3 compatibility
192 # use eval workaround to find out which names are used in the
192 # use eval workaround to find out which names are used in the
193 # eval string and put them into the locals. This works for most
193 # eval string and put them into the locals. This works for most
194 # normal uses case, bizarre ones like accessing the locals()
194 # normal uses case, bizarre ones like accessing the locals()
195 # will fail
195 # will fail
196 try:
196 try:
197 eval("_", None, AttrNamespace(None))
197 eval("_", None, AttrNamespace(None))
198 except TypeError:
198 except TypeError:
199 real_eval = eval
199 real_eval = eval
200 def eval(codestring, _globals, _locals):
200 def eval(codestring, _globals, _locals):
201 """
201 """
202 eval(source[, globals[, locals]]) -> value
202 eval(source[, globals[, locals]]) -> value
203
203
204 Evaluate the source in the context of globals and locals.
204 Evaluate the source in the context of globals and locals.
205 The source may be a string representing a Python expression
205 The source may be a string representing a Python expression
206 or a code object as returned by compile().
206 or a code object as returned by compile().
207 The globals must be a dictionary and locals can be any mappping.
207 The globals must be a dictionary and locals can be any mappping.
208
208
209 This function is a workaround for the shortcomings of
209 This function is a workaround for the shortcomings of
210 Python 2.3's eval.
210 Python 2.3's eval.
211 """
211 """
212
212
213 code = compile(codestring, "_eval", "eval")
213 code = compile(codestring, "_eval", "eval")
214 newlocals = {}
214 newlocals = {}
215 for name in code.co_names:
215 for name in code.co_names:
216 try:
216 try:
217 newlocals[name] = _locals[name]
217 newlocals[name] = _locals[name]
218 except KeyError:
218 except KeyError:
219 pass
219 pass
220 return real_eval(code, _globals, newlocals)
220 return real_eval(code, _globals, newlocals)
221
221
222
222
223 noitem = object()
223 noitem = object()
224
224
225 def item(iterator, index, default=noitem):
225 def item(iterator, index, default=noitem):
226 """
226 """
227 Return the ``index``th item from the iterator ``iterator``.
227 Return the ``index``th item from the iterator ``iterator``.
228 ``index`` must be an integer (negative integers are relative to the
228 ``index`` must be an integer (negative integers are relative to the
229 end (i.e. the last item produced by the iterator)).
229 end (i.e. the last item produced by the iterator)).
230
230
231 If ``default`` is given, this will be the default value when
231 If ``default`` is given, this will be the default value when
232 the iterator doesn't contain an item at this position. Otherwise an
232 the iterator doesn't contain an item at this position. Otherwise an
233 ``IndexError`` will be raised.
233 ``IndexError`` will be raised.
234
234
235 Note that using this function will partially or totally exhaust the
235 Note that using this function will partially or totally exhaust the
236 iterator.
236 iterator.
237 """
237 """
238 i = index
238 i = index
239 if i>=0:
239 if i>=0:
240 for item in iterator:
240 for item in iterator:
241 if not i:
241 if not i:
242 return item
242 return item
243 i -= 1
243 i -= 1
244 else:
244 else:
245 i = -index
245 i = -index
246 cache = deque()
246 cache = deque()
247 for item in iterator:
247 for item in iterator:
248 cache.append(item)
248 cache.append(item)
249 if len(cache)>i:
249 if len(cache)>i:
250 cache.popleft()
250 cache.popleft()
251 if len(cache)==i:
251 if len(cache)==i:
252 return cache.popleft()
252 return cache.popleft()
253 if default is noitem:
253 if default is noitem:
254 raise IndexError(index)
254 raise IndexError(index)
255 else:
255 else:
256 return default
256 return default
257
257
258
258
259 def getglobals(g):
259 def getglobals(g):
260 if g is None:
260 if g is None:
261 if ipapi is not None:
261 if ipapi is not None:
262 return ipapi.get().user_ns()
262 return ipapi.get().user_ns
263 else:
263 else:
264 return globals()
264 return globals()
265 return g
265 return g
266
266
267
267
268 class Table(object):
268 class Table(object):
269 """
269 """
270 A ``Table`` is an object that produces items (just like a normal Python
270 A ``Table`` is an object that produces items (just like a normal Python
271 iterator/generator does) and can be used as the first object in a pipeline
271 iterator/generator does) and can be used as the first object in a pipeline
272 expression. The displayhook will open the default browser for such an object
272 expression. The displayhook will open the default browser for such an object
273 (instead of simply printing the ``repr()`` result).
273 (instead of simply printing the ``repr()`` result).
274 """
274 """
275
275
276 # We want to support ``foo`` and ``foo()`` in pipeline expression:
276 # We want to support ``foo`` and ``foo()`` in pipeline expression:
277 # So we implement the required operators (``|`` and ``+``) in the metaclass,
277 # So we implement the required operators (``|`` and ``+``) in the metaclass,
278 # instantiate the class and forward the operator to the instance
278 # instantiate the class and forward the operator to the instance
279 class __metaclass__(type):
279 class __metaclass__(type):
280 def __iter__(self):
280 def __iter__(self):
281 return iter(self())
281 return iter(self())
282
282
283 def __or__(self, other):
283 def __or__(self, other):
284 return self() | other
284 return self() | other
285
285
286 def __add__(self, other):
286 def __add__(self, other):
287 return self() + other
287 return self() + other
288
288
289 def __radd__(self, other):
289 def __radd__(self, other):
290 return other + self()
290 return other + self()
291
291
292 def __getitem__(self, index):
292 def __getitem__(self, index):
293 return self()[index]
293 return self()[index]
294
294
295 def __getitem__(self, index):
295 def __getitem__(self, index):
296 return item(self, index)
296 return item(self, index)
297
297
298 def __contains__(self, item):
298 def __contains__(self, item):
299 for haveitem in self:
299 for haveitem in self:
300 if item == haveitem:
300 if item == haveitem:
301 return True
301 return True
302 return False
302 return False
303
303
304 def __or__(self, other):
304 def __or__(self, other):
305 # autoinstantiate right hand side
305 # autoinstantiate right hand side
306 if isinstance(other, type) and issubclass(other, (Table, Display)):
306 if isinstance(other, type) and issubclass(other, (Table, Display)):
307 other = other()
307 other = other()
308 # treat simple strings and functions as ``ieval`` instances
308 # treat simple strings and functions as ``ieval`` instances
309 elif not isinstance(other, Display) and not isinstance(other, Table):
309 elif not isinstance(other, Display) and not isinstance(other, Table):
310 other = ieval(other)
310 other = ieval(other)
311 # forward operations to the right hand side
311 # forward operations to the right hand side
312 return other.__ror__(self)
312 return other.__ror__(self)
313
313
314 def __add__(self, other):
314 def __add__(self, other):
315 # autoinstantiate right hand side
315 # autoinstantiate right hand side
316 if isinstance(other, type) and issubclass(other, Table):
316 if isinstance(other, type) and issubclass(other, Table):
317 other = other()
317 other = other()
318 return ichain(self, other)
318 return ichain(self, other)
319
319
320 def __radd__(self, other):
320 def __radd__(self, other):
321 # autoinstantiate left hand side
321 # autoinstantiate left hand side
322 if isinstance(other, type) and issubclass(other, Table):
322 if isinstance(other, type) and issubclass(other, Table):
323 other = other()
323 other = other()
324 return ichain(other, self)
324 return ichain(other, self)
325
325
326 def __iter__(self):
326 def __iter__(self):
327 return xiter(self, "default")
327 return xiter(self, "default")
328
328
329
329
330 class Pipe(Table):
330 class Pipe(Table):
331 """
331 """
332 A ``Pipe`` is an object that can be used in a pipeline expression. It
332 A ``Pipe`` is an object that can be used in a pipeline expression. It
333 processes the objects it gets from its input ``Table``/``Pipe``. Note that
333 processes the objects it gets from its input ``Table``/``Pipe``. Note that
334 a ``Pipe`` object can't be used as the first object in a pipeline
334 a ``Pipe`` object can't be used as the first object in a pipeline
335 expression, as it doesn't produces items itself.
335 expression, as it doesn't produces items itself.
336 """
336 """
337 class __metaclass__(Table.__metaclass__):
337 class __metaclass__(Table.__metaclass__):
338 def __ror__(self, input):
338 def __ror__(self, input):
339 return input | self()
339 return input | self()
340
340
341 def __ror__(self, input):
341 def __ror__(self, input):
342 # autoinstantiate left hand side
342 # autoinstantiate left hand side
343 if isinstance(input, type) and issubclass(input, Table):
343 if isinstance(input, type) and issubclass(input, Table):
344 input = input()
344 input = input()
345 self.input = input
345 self.input = input
346 return self
346 return self
347
347
348
348
349 def _getattr(obj, name, default=noitem):
349 def _getattr(obj, name, default=noitem):
350 """
350 """
351 Internal helper for getting an attribute of an item. If ``name`` is ``None``
351 Internal helper for getting an attribute of an item. If ``name`` is ``None``
352 return the object itself. If ``name`` is an integer, use ``__getitem__``
352 return the object itself. If ``name`` is an integer, use ``__getitem__``
353 instead. If the attribute or item does not exist, return ``default``.
353 instead. If the attribute or item does not exist, return ``default``.
354 """
354 """
355 if name is None:
355 if name is None:
356 return obj
356 return obj
357 elif isinstance(name, basestring):
357 elif isinstance(name, basestring):
358 if name.endswith("()"):
358 if name.endswith("()"):
359 return getattr(obj, name[:-2], default)()
359 return getattr(obj, name[:-2], default)()
360 else:
360 else:
361 return getattr(obj, name, default)
361 return getattr(obj, name, default)
362 elif callable(name):
362 elif callable(name):
363 try:
363 try:
364 return name(obj)
364 return name(obj)
365 except AttributeError:
365 except AttributeError:
366 return default
366 return default
367 else:
367 else:
368 try:
368 try:
369 return obj[name]
369 return obj[name]
370 except IndexError:
370 except IndexError:
371 return default
371 return default
372
372
373
373
374 def _attrname(name):
374 def _attrname(name):
375 """
375 """
376 Internal helper that gives a proper name for the attribute ``name``
376 Internal helper that gives a proper name for the attribute ``name``
377 (which might be ``None`` or an ``int``).
377 (which might be ``None`` or an ``int``).
378 """
378 """
379 if name is None:
379 if name is None:
380 return "_"
380 return "_"
381 elif isinstance(name, basestring):
381 elif isinstance(name, basestring):
382 return name
382 return name
383 elif callable(name):
383 elif callable(name):
384 return getattr(name, "__xname__", name.__name__)
384 return getattr(name, "__xname__", name.__name__)
385 else:
385 else:
386 return str(name)
386 return str(name)
387
387
388
388
389 def xrepr(item, mode):
389 def xrepr(item, mode):
390 try:
390 try:
391 func = item.__xrepr__
391 func = item.__xrepr__
392 except AttributeError:
392 except AttributeError:
393 pass
393 pass
394 else:
394 else:
395 try:
395 try:
396 for x in func(mode):
396 for x in func(mode):
397 yield x
397 yield x
398 except (KeyboardInterrupt, SystemExit):
398 except (KeyboardInterrupt, SystemExit):
399 raise
399 raise
400 except Exception:
400 except Exception:
401 yield (astyle.style_default, repr(item))
401 yield (astyle.style_default, repr(item))
402 return
402 return
403 if item is None:
403 if item is None:
404 yield (astyle.style_type_none, repr(item))
404 yield (astyle.style_type_none, repr(item))
405 elif isinstance(item, bool):
405 elif isinstance(item, bool):
406 yield (astyle.style_type_bool, repr(item))
406 yield (astyle.style_type_bool, repr(item))
407 elif isinstance(item, str):
407 elif isinstance(item, str):
408 if mode == "cell":
408 if mode == "cell":
409 yield (astyle.style_default, repr(item.expandtabs(tab))[1:-1])
409 yield (astyle.style_default, repr(item.expandtabs(tab))[1:-1])
410 else:
410 else:
411 yield (astyle.style_default, repr(item))
411 yield (astyle.style_default, repr(item))
412 elif isinstance(item, unicode):
412 elif isinstance(item, unicode):
413 if mode == "cell":
413 if mode == "cell":
414 yield (astyle.style_default, repr(item.expandtabs(tab))[2:-1])
414 yield (astyle.style_default, repr(item.expandtabs(tab))[2:-1])
415 else:
415 else:
416 yield (astyle.style_default, repr(item))
416 yield (astyle.style_default, repr(item))
417 elif isinstance(item, (int, long, float)):
417 elif isinstance(item, (int, long, float)):
418 yield (1, True)
418 yield (1, True)
419 yield (astyle.style_type_number, repr(item))
419 yield (astyle.style_type_number, repr(item))
420 elif isinstance(item, complex):
420 elif isinstance(item, complex):
421 yield (astyle.style_type_number, repr(item))
421 yield (astyle.style_type_number, repr(item))
422 elif isinstance(item, datetime.datetime):
422 elif isinstance(item, datetime.datetime):
423 if mode == "cell":
423 if mode == "cell":
424 # Don't use strftime() here, as this requires year >= 1900
424 # Don't use strftime() here, as this requires year >= 1900
425 yield (astyle.style_type_datetime,
425 yield (astyle.style_type_datetime,
426 "%04d-%02d-%02d %02d:%02d:%02d.%06d" % \
426 "%04d-%02d-%02d %02d:%02d:%02d.%06d" % \
427 (item.year, item.month, item.day,
427 (item.year, item.month, item.day,
428 item.hour, item.minute, item.second,
428 item.hour, item.minute, item.second,
429 item.microsecond),
429 item.microsecond),
430 )
430 )
431 else:
431 else:
432 yield (astyle.style_type_datetime, repr(item))
432 yield (astyle.style_type_datetime, repr(item))
433 elif isinstance(item, datetime.date):
433 elif isinstance(item, datetime.date):
434 if mode == "cell":
434 if mode == "cell":
435 yield (astyle.style_type_datetime,
435 yield (astyle.style_type_datetime,
436 "%04d-%02d-%02d" % (item.year, item.month, item.day))
436 "%04d-%02d-%02d" % (item.year, item.month, item.day))
437 else:
437 else:
438 yield (astyle.style_type_datetime, repr(item))
438 yield (astyle.style_type_datetime, repr(item))
439 elif isinstance(item, datetime.time):
439 elif isinstance(item, datetime.time):
440 if mode == "cell":
440 if mode == "cell":
441 yield (astyle.style_type_datetime,
441 yield (astyle.style_type_datetime,
442 "%02d:%02d:%02d.%06d" % \
442 "%02d:%02d:%02d.%06d" % \
443 (item.hour, item.minute, item.second, item.microsecond))
443 (item.hour, item.minute, item.second, item.microsecond))
444 else:
444 else:
445 yield (astyle.style_type_datetime, repr(item))
445 yield (astyle.style_type_datetime, repr(item))
446 elif isinstance(item, datetime.timedelta):
446 elif isinstance(item, datetime.timedelta):
447 yield (astyle.style_type_datetime, repr(item))
447 yield (astyle.style_type_datetime, repr(item))
448 elif isinstance(item, Exception):
448 elif isinstance(item, Exception):
449 if item.__class__.__module__ == "exceptions":
449 if item.__class__.__module__ == "exceptions":
450 classname = item.__class__.__name__
450 classname = item.__class__.__name__
451 else:
451 else:
452 classname = "%s.%s" % \
452 classname = "%s.%s" % \
453 (item.__class__.__module__, item.__class__.__name__)
453 (item.__class__.__module__, item.__class__.__name__)
454 if mode == "header" or mode == "footer":
454 if mode == "header" or mode == "footer":
455 yield (astyle.style_error, "%s: %s" % (classname, item))
455 yield (astyle.style_error, "%s: %s" % (classname, item))
456 else:
456 else:
457 yield (astyle.style_error, classname)
457 yield (astyle.style_error, classname)
458 elif isinstance(item, (list, tuple)):
458 elif isinstance(item, (list, tuple)):
459 if mode == "header" or mode == "footer":
459 if mode == "header" or mode == "footer":
460 if item.__class__.__module__ == "__builtin__":
460 if item.__class__.__module__ == "__builtin__":
461 classname = item.__class__.__name__
461 classname = item.__class__.__name__
462 else:
462 else:
463 classname = "%s.%s" % \
463 classname = "%s.%s" % \
464 (item.__class__.__module__,item.__class__.__name__)
464 (item.__class__.__module__,item.__class__.__name__)
465 yield (astyle.style_default,
465 yield (astyle.style_default,
466 "<%s object with %d items at 0x%x>" % \
466 "<%s object with %d items at 0x%x>" % \
467 (classname, len(item), id(item)))
467 (classname, len(item), id(item)))
468 else:
468 else:
469 yield (-1, False)
469 yield (-1, False)
470 if isinstance(item, list):
470 if isinstance(item, list):
471 yield (astyle.style_default, "[")
471 yield (astyle.style_default, "[")
472 end = "]"
472 end = "]"
473 else:
473 else:
474 yield (astyle.style_default, "(")
474 yield (astyle.style_default, "(")
475 end = ")"
475 end = ")"
476 for (i, subitem) in enumerate(item):
476 for (i, subitem) in enumerate(item):
477 if i:
477 if i:
478 yield (astyle.style_default, ", ")
478 yield (astyle.style_default, ", ")
479 for part in xrepr(subitem, "default"):
479 for part in xrepr(subitem, "default"):
480 yield part
480 yield part
481 yield (astyle.style_default, end)
481 yield (astyle.style_default, end)
482 elif isinstance(item, (dict, types.DictProxyType)):
482 elif isinstance(item, (dict, types.DictProxyType)):
483 if mode == "header" or mode == "footer":
483 if mode == "header" or mode == "footer":
484 if item.__class__.__module__ == "__builtin__":
484 if item.__class__.__module__ == "__builtin__":
485 classname = item.__class__.__name__
485 classname = item.__class__.__name__
486 else:
486 else:
487 classname = "%s.%s" % \
487 classname = "%s.%s" % \
488 (item.__class__.__module__,item.__class__.__name__)
488 (item.__class__.__module__,item.__class__.__name__)
489 yield (astyle.style_default,
489 yield (astyle.style_default,
490 "<%s object with %d items at 0x%x>" % \
490 "<%s object with %d items at 0x%x>" % \
491 (classname, len(item), id(item)))
491 (classname, len(item), id(item)))
492 else:
492 else:
493 yield (-1, False)
493 yield (-1, False)
494 if isinstance(item, dict):
494 if isinstance(item, dict):
495 yield (astyle.style_default, "{")
495 yield (astyle.style_default, "{")
496 end = "}"
496 end = "}"
497 else:
497 else:
498 yield (astyle.style_default, "dictproxy((")
498 yield (astyle.style_default, "dictproxy((")
499 end = "})"
499 end = "})"
500 for (i, (key, value)) in enumerate(item.iteritems()):
500 for (i, (key, value)) in enumerate(item.iteritems()):
501 if i:
501 if i:
502 yield (astyle.style_default, ", ")
502 yield (astyle.style_default, ", ")
503 for part in xrepr(key, "default"):
503 for part in xrepr(key, "default"):
504 yield part
504 yield part
505 yield (astyle.style_default, ": ")
505 yield (astyle.style_default, ": ")
506 for part in xrepr(value, "default"):
506 for part in xrepr(value, "default"):
507 yield part
507 yield part
508 yield (astyle.style_default, end)
508 yield (astyle.style_default, end)
509 else:
509 else:
510 yield (astyle.style_default, repr(item))
510 yield (astyle.style_default, repr(item))
511
511
512
512
513 def xattrs(item, mode):
513 def xattrs(item, mode):
514 try:
514 try:
515 func = item.__xattrs__
515 func = item.__xattrs__
516 except AttributeError:
516 except AttributeError:
517 if mode == "detail":
517 if mode == "detail":
518 return dir(item)
518 return dir(item)
519 else:
519 else:
520 return (None,)
520 return (None,)
521 else:
521 else:
522 try:
522 try:
523 return func(mode)
523 return func(mode)
524 except (KeyboardInterrupt, SystemExit):
524 except (KeyboardInterrupt, SystemExit):
525 raise
525 raise
526 except Exception:
526 except Exception:
527 return (None,)
527 return (None,)
528
528
529
529
530 def xiter(item, mode):
530 def xiter(item, mode):
531 if mode == "detail":
531 if mode == "detail":
532 def items():
532 def items():
533 for name in xattrs(item, mode):
533 for name in xattrs(item, mode):
534 yield XAttr(item, name)
534 yield XAttr(item, name)
535 return items()
535 return items()
536 try:
536 try:
537 func = item.__xiter__
537 func = item.__xiter__
538 except AttributeError:
538 except AttributeError:
539 if isinstance(item, (dict, types.DictProxyType)):
539 if isinstance(item, (dict, types.DictProxyType)):
540 def items(item):
540 def items(item):
541 fields = ("key", "value")
541 fields = ("key", "value")
542 for (key, value) in item.iteritems():
542 for (key, value) in item.iteritems():
543 yield Fields(fields, key=key, value=value)
543 yield Fields(fields, key=key, value=value)
544 return items(item)
544 return items(item)
545 elif isinstance(item, new.module):
545 elif isinstance(item, new.module):
546 def items(item):
546 def items(item):
547 fields = ("key", "value")
547 fields = ("key", "value")
548 for key in sorted(item.__dict__):
548 for key in sorted(item.__dict__):
549 yield Fields(fields, key=key, value=getattr(item, key))
549 yield Fields(fields, key=key, value=getattr(item, key))
550 return items(item)
550 return items(item)
551 elif isinstance(item, basestring):
551 elif isinstance(item, basestring):
552 if not len(item):
552 if not len(item):
553 raise ValueError("can't enter empty string")
553 raise ValueError("can't enter empty string")
554 lines = item.splitlines()
554 lines = item.splitlines()
555 if len(lines) <= 1:
555 if len(lines) <= 1:
556 raise ValueError("can't enter one line string")
556 raise ValueError("can't enter one line string")
557 return iter(lines)
557 return iter(lines)
558 return iter(item)
558 return iter(item)
559 else:
559 else:
560 return iter(func(mode)) # iter() just to be safe
560 return iter(func(mode)) # iter() just to be safe
561
561
562
562
563 class ichain(Pipe):
563 class ichain(Pipe):
564 """
564 """
565 Chains multiple ``Table``s into one.
565 Chains multiple ``Table``s into one.
566 """
566 """
567
567
568 def __init__(self, *iters):
568 def __init__(self, *iters):
569 self.iters = iters
569 self.iters = iters
570
570
571 def __xiter__(self, mode):
571 def __xiter__(self, mode):
572 return itertools.chain(*self.iters)
572 return itertools.chain(*self.iters)
573
573
574 def __xrepr__(self, mode):
574 def __xrepr__(self, mode):
575 if mode == "header" or mode == "footer":
575 if mode == "header" or mode == "footer":
576 for (i, item) in enumerate(self.iters):
576 for (i, item) in enumerate(self.iters):
577 if i:
577 if i:
578 yield (astyle.style_default, "+")
578 yield (astyle.style_default, "+")
579 if isinstance(item, Pipe):
579 if isinstance(item, Pipe):
580 yield (astyle.style_default, "(")
580 yield (astyle.style_default, "(")
581 for part in xrepr(item, mode):
581 for part in xrepr(item, mode):
582 yield part
582 yield part
583 if isinstance(item, Pipe):
583 if isinstance(item, Pipe):
584 yield (astyle.style_default, ")")
584 yield (astyle.style_default, ")")
585 else:
585 else:
586 yield (astyle.style_default, repr(self))
586 yield (astyle.style_default, repr(self))
587
587
588 def __repr__(self):
588 def __repr__(self):
589 args = ", ".join([repr(it) for it in self.iters])
589 args = ", ".join([repr(it) for it in self.iters])
590 return "%s.%s(%s)" % \
590 return "%s.%s(%s)" % \
591 (self.__class__.__module__, self.__class__.__name__, args)
591 (self.__class__.__module__, self.__class__.__name__, args)
592
592
593
593
594 class ifile(path.path):
594 class ifile(path.path):
595 """
595 """
596 file (or directory) object.
596 file (or directory) object.
597 """
597 """
598
598
599 def __add_(self, other):
599 def __add_(self, other):
600 return ifile(path._base(self) + other)
600 return ifile(path._base(self) + other)
601
601
602 def __radd_(self, other):
602 def __radd_(self, other):
603 return ifile(other + path._base(self))
603 return ifile(other + path._base(self))
604
604
605 def __div_(self, other):
605 def __div_(self, other):
606 return ifile(path.__div__(self, other))
606 return ifile(path.__div__(self, other))
607
607
608 def getcwd():
608 def getcwd():
609 return ifile(path.path.getcwd())
609 return ifile(path.path.getcwd())
610 getcwd.__doc__ = path.path.getcwd.__doc__
610 getcwd.__doc__ = path.path.getcwd.__doc__
611 getcwd = staticmethod(getcwd)
611 getcwd = staticmethod(getcwd)
612
612
613 def abspath(self):
613 def abspath(self):
614 return ifile(path.path.abspath(self))
614 return ifile(path.path.abspath(self))
615 abspath.__doc__ = path.path.abspath.__doc__
615 abspath.__doc__ = path.path.abspath.__doc__
616
616
617 def normcase(self):
617 def normcase(self):
618 return ifile(path.path.normcase(self))
618 return ifile(path.path.normcase(self))
619 normcase.__doc__ = path.path.normcase.__doc__
619 normcase.__doc__ = path.path.normcase.__doc__
620
620
621 def normpath(self):
621 def normpath(self):
622 return ifile(path.path.normpath(self))
622 return ifile(path.path.normpath(self))
623 normpath.__doc__ = path.path.normpath.__doc__
623 normpath.__doc__ = path.path.normpath.__doc__
624
624
625 def realpath(self):
625 def realpath(self):
626 return ifile(path.path.realpath(self))
626 return ifile(path.path.realpath(self))
627 realpath.__doc__ = path.path.realpath.__doc__
627 realpath.__doc__ = path.path.realpath.__doc__
628
628
629 def expanduser(self):
629 def expanduser(self):
630 return ifile(path.path.expanduser(self))
630 return ifile(path.path.expanduser(self))
631 expanduser.__doc__ = path.path.expanduser.__doc__
631 expanduser.__doc__ = path.path.expanduser.__doc__
632
632
633 def expandvars(self):
633 def expandvars(self):
634 return ifile(path.path.expandvars(self))
634 return ifile(path.path.expandvars(self))
635 expandvars.__doc__ = path.path.expandvars.__doc__
635 expandvars.__doc__ = path.path.expandvars.__doc__
636
636
637 def dirname(self):
637 def dirname(self):
638 return ifile(path.path.dirname(self))
638 return ifile(path.path.dirname(self))
639 dirname.__doc__ = path.path.dirname.__doc__
639 dirname.__doc__ = path.path.dirname.__doc__
640
640
641 parent = property(dirname, None, None, path.path.parent.__doc__)
641 parent = property(dirname, None, None, path.path.parent.__doc__)
642
642
643 def splitpath(self):
643 def splitpath(self):
644 (parent, child) = path.path.splitpath(self)
644 (parent, child) = path.path.splitpath(self)
645 return (ifile(parent), child)
645 return (ifile(parent), child)
646 splitpath.__doc__ = path.path.splitpath.__doc__
646 splitpath.__doc__ = path.path.splitpath.__doc__
647
647
648 def splitdrive(self):
648 def splitdrive(self):
649 (drive, rel) = path.path.splitdrive(self)
649 (drive, rel) = path.path.splitdrive(self)
650 return (ifile(drive), rel)
650 return (ifile(drive), rel)
651 splitdrive.__doc__ = path.path.splitdrive.__doc__
651 splitdrive.__doc__ = path.path.splitdrive.__doc__
652
652
653 def splitext(self):
653 def splitext(self):
654 (filename, ext) = path.path.splitext(self)
654 (filename, ext) = path.path.splitext(self)
655 return (ifile(filename), ext)
655 return (ifile(filename), ext)
656 splitext.__doc__ = path.path.splitext.__doc__
656 splitext.__doc__ = path.path.splitext.__doc__
657
657
658 if hasattr(path.path, "splitunc"):
658 if hasattr(path.path, "splitunc"):
659 def splitunc(self):
659 def splitunc(self):
660 (unc, rest) = path.path.splitunc(self)
660 (unc, rest) = path.path.splitunc(self)
661 return (ifile(unc), rest)
661 return (ifile(unc), rest)
662 splitunc.__doc__ = path.path.splitunc.__doc__
662 splitunc.__doc__ = path.path.splitunc.__doc__
663
663
664 def _get_uncshare(self):
664 def _get_uncshare(self):
665 unc, r = os.path.splitunc(self)
665 unc, r = os.path.splitunc(self)
666 return ifile(unc)
666 return ifile(unc)
667
667
668 uncshare = property(
668 uncshare = property(
669 _get_uncshare, None, None,
669 _get_uncshare, None, None,
670 """ The UNC mount point for this path.
670 """ The UNC mount point for this path.
671 This is empty for paths on local drives. """)
671 This is empty for paths on local drives. """)
672
672
673 def joinpath(self, *args):
673 def joinpath(self, *args):
674 return ifile(path.path.joinpath(self, *args))
674 return ifile(path.path.joinpath(self, *args))
675 joinpath.__doc__ = path.path.joinpath.__doc__
675 joinpath.__doc__ = path.path.joinpath.__doc__
676
676
677 def splitall(self):
677 def splitall(self):
678 return map(ifile, path.path.splitall(self))
678 return map(ifile, path.path.splitall(self))
679 splitall.__doc__ = path.path.splitall.__doc__
679 splitall.__doc__ = path.path.splitall.__doc__
680
680
681 def relpath(self):
681 def relpath(self):
682 return ifile(path.path.relpath(self))
682 return ifile(path.path.relpath(self))
683 relpath.__doc__ = path.path.relpath.__doc__
683 relpath.__doc__ = path.path.relpath.__doc__
684
684
685 def relpathto(self, dest):
685 def relpathto(self, dest):
686 return ifile(path.path.relpathto(self, dest))
686 return ifile(path.path.relpathto(self, dest))
687 relpathto.__doc__ = path.path.relpathto.__doc__
687 relpathto.__doc__ = path.path.relpathto.__doc__
688
688
689 def listdir(self, pattern=None):
689 def listdir(self, pattern=None):
690 return [ifile(child) for child in path.path.listdir(self, pattern)]
690 return [ifile(child) for child in path.path.listdir(self, pattern)]
691 listdir.__doc__ = path.path.listdir.__doc__
691 listdir.__doc__ = path.path.listdir.__doc__
692
692
693 def dirs(self, pattern=None):
693 def dirs(self, pattern=None):
694 return [ifile(child) for child in path.path.dirs(self, pattern)]
694 return [ifile(child) for child in path.path.dirs(self, pattern)]
695 dirs.__doc__ = path.path.dirs.__doc__
695 dirs.__doc__ = path.path.dirs.__doc__
696
696
697 def files(self, pattern=None):
697 def files(self, pattern=None):
698 return [ifile(child) for child in path.path.files(self, pattern)]
698 return [ifile(child) for child in path.path.files(self, pattern)]
699 files.__doc__ = path.path.files.__doc__
699 files.__doc__ = path.path.files.__doc__
700
700
701 def walk(self, pattern=None):
701 def walk(self, pattern=None):
702 for child in path.path.walk(self, pattern):
702 for child in path.path.walk(self, pattern):
703 yield ifile(child)
703 yield ifile(child)
704 walk.__doc__ = path.path.walk.__doc__
704 walk.__doc__ = path.path.walk.__doc__
705
705
706 def walkdirs(self, pattern=None):
706 def walkdirs(self, pattern=None):
707 for child in path.path.walkdirs(self, pattern):
707 for child in path.path.walkdirs(self, pattern):
708 yield ifile(child)
708 yield ifile(child)
709 walkdirs.__doc__ = path.path.walkdirs.__doc__
709 walkdirs.__doc__ = path.path.walkdirs.__doc__
710
710
711 def walkfiles(self, pattern=None):
711 def walkfiles(self, pattern=None):
712 for child in path.path.walkfiles(self, pattern):
712 for child in path.path.walkfiles(self, pattern):
713 yield ifile(child)
713 yield ifile(child)
714 walkfiles.__doc__ = path.path.walkfiles.__doc__
714 walkfiles.__doc__ = path.path.walkfiles.__doc__
715
715
716 def glob(self, pattern):
716 def glob(self, pattern):
717 return map(ifile, path.path.glob(self, pattern))
717 return map(ifile, path.path.glob(self, pattern))
718 glob.__doc__ = path.path.glob.__doc__
718 glob.__doc__ = path.path.glob.__doc__
719
719
720 if hasattr(os, 'readlink'):
720 if hasattr(os, 'readlink'):
721 def readlink(self):
721 def readlink(self):
722 return ifile(path.path.readlink(self))
722 return ifile(path.path.readlink(self))
723 readlink.__doc__ = path.path.readlink.__doc__
723 readlink.__doc__ = path.path.readlink.__doc__
724
724
725 def readlinkabs(self):
725 def readlinkabs(self):
726 return ifile(path.path.readlinkabs(self))
726 return ifile(path.path.readlinkabs(self))
727 readlinkabs.__doc__ = path.path.readlinkabs.__doc__
727 readlinkabs.__doc__ = path.path.readlinkabs.__doc__
728
728
729 def getmode(self):
729 def getmode(self):
730 return self.stat().st_mode
730 return self.stat().st_mode
731 mode = property(getmode, None, None, "Access mode")
731 mode = property(getmode, None, None, "Access mode")
732
732
733 def gettype(self):
733 def gettype(self):
734 data = [
734 data = [
735 (stat.S_ISREG, "file"),
735 (stat.S_ISREG, "file"),
736 (stat.S_ISDIR, "dir"),
736 (stat.S_ISDIR, "dir"),
737 (stat.S_ISCHR, "chardev"),
737 (stat.S_ISCHR, "chardev"),
738 (stat.S_ISBLK, "blockdev"),
738 (stat.S_ISBLK, "blockdev"),
739 (stat.S_ISFIFO, "fifo"),
739 (stat.S_ISFIFO, "fifo"),
740 (stat.S_ISLNK, "symlink"),
740 (stat.S_ISLNK, "symlink"),
741 (stat.S_ISSOCK,"socket"),
741 (stat.S_ISSOCK,"socket"),
742 ]
742 ]
743 lstat = self.lstat()
743 lstat = self.lstat()
744 if lstat is not None:
744 if lstat is not None:
745 types = set([text for (func, text) in data if func(lstat.st_mode)])
745 types = set([text for (func, text) in data if func(lstat.st_mode)])
746 else:
746 else:
747 types = set()
747 types = set()
748 m = self.mode
748 m = self.mode
749 types.update([text for (func, text) in data if func(m)])
749 types.update([text for (func, text) in data if func(m)])
750 return ", ".join(types)
750 return ", ".join(types)
751 type = property(gettype, None, None, "file type (file, directory, link, etc.)")
751 type = property(gettype, None, None, "file type (file, directory, link, etc.)")
752
752
753 def getmodestr(self):
753 def getmodestr(self):
754 m = self.mode
754 m = self.mode
755 data = [
755 data = [
756 (stat.S_IRUSR, "-r"),
756 (stat.S_IRUSR, "-r"),
757 (stat.S_IWUSR, "-w"),
757 (stat.S_IWUSR, "-w"),
758 (stat.S_IXUSR, "-x"),
758 (stat.S_IXUSR, "-x"),
759 (stat.S_IRGRP, "-r"),
759 (stat.S_IRGRP, "-r"),
760 (stat.S_IWGRP, "-w"),
760 (stat.S_IWGRP, "-w"),
761 (stat.S_IXGRP, "-x"),
761 (stat.S_IXGRP, "-x"),
762 (stat.S_IROTH, "-r"),
762 (stat.S_IROTH, "-r"),
763 (stat.S_IWOTH, "-w"),
763 (stat.S_IWOTH, "-w"),
764 (stat.S_IXOTH, "-x"),
764 (stat.S_IXOTH, "-x"),
765 ]
765 ]
766 return "".join([text[bool(m&bit)] for (bit, text) in data])
766 return "".join([text[bool(m&bit)] for (bit, text) in data])
767
767
768 modestr = property(getmodestr, None, None, "Access mode as string")
768 modestr = property(getmodestr, None, None, "Access mode as string")
769
769
770 def getblocks(self):
770 def getblocks(self):
771 return self.stat().st_blocks
771 return self.stat().st_blocks
772 blocks = property(getblocks, None, None, "File size in blocks")
772 blocks = property(getblocks, None, None, "File size in blocks")
773
773
774 def getblksize(self):
774 def getblksize(self):
775 return self.stat().st_blksize
775 return self.stat().st_blksize
776 blksize = property(getblksize, None, None, "Filesystem block size")
776 blksize = property(getblksize, None, None, "Filesystem block size")
777
777
778 def getdev(self):
778 def getdev(self):
779 return self.stat().st_dev
779 return self.stat().st_dev
780 dev = property(getdev)
780 dev = property(getdev)
781
781
782 def getnlink(self):
782 def getnlink(self):
783 return self.stat().st_nlink
783 return self.stat().st_nlink
784 nlink = property(getnlink, None, None, "Number of links")
784 nlink = property(getnlink, None, None, "Number of links")
785
785
786 def getuid(self):
786 def getuid(self):
787 return self.stat().st_uid
787 return self.stat().st_uid
788 uid = property(getuid, None, None, "User id of file owner")
788 uid = property(getuid, None, None, "User id of file owner")
789
789
790 def getgid(self):
790 def getgid(self):
791 return self.stat().st_gid
791 return self.stat().st_gid
792 gid = property(getgid, None, None, "Group id of file owner")
792 gid = property(getgid, None, None, "Group id of file owner")
793
793
794 def getowner(self):
794 def getowner(self):
795 stat = self.stat()
795 stat = self.stat()
796 try:
796 try:
797 return pwd.getpwuid(stat.st_uid).pw_name
797 return pwd.getpwuid(stat.st_uid).pw_name
798 except KeyError:
798 except KeyError:
799 return stat.st_uid
799 return stat.st_uid
800 owner = property(getowner, None, None, "Owner name (or id)")
800 owner = property(getowner, None, None, "Owner name (or id)")
801
801
802 def getgroup(self):
802 def getgroup(self):
803 stat = self.stat()
803 stat = self.stat()
804 try:
804 try:
805 return grp.getgrgid(stat.st_gid).gr_name
805 return grp.getgrgid(stat.st_gid).gr_name
806 except KeyError:
806 except KeyError:
807 return stat.st_gid
807 return stat.st_gid
808 group = property(getgroup, None, None, "Group name (or id)")
808 group = property(getgroup, None, None, "Group name (or id)")
809
809
810 def getadate(self):
810 def getadate(self):
811 return datetime.datetime.utcfromtimestamp(self.atime)
811 return datetime.datetime.utcfromtimestamp(self.atime)
812 adate = property(getadate, None, None, "Access date")
812 adate = property(getadate, None, None, "Access date")
813
813
814 def getcdate(self):
814 def getcdate(self):
815 return datetime.datetime.utcfromtimestamp(self.ctime)
815 return datetime.datetime.utcfromtimestamp(self.ctime)
816 cdate = property(getcdate, None, None, "Creation date")
816 cdate = property(getcdate, None, None, "Creation date")
817
817
818 def getmdate(self):
818 def getmdate(self):
819 return datetime.datetime.utcfromtimestamp(self.mtime)
819 return datetime.datetime.utcfromtimestamp(self.mtime)
820 mdate = property(getmdate, None, None, "Modification date")
820 mdate = property(getmdate, None, None, "Modification date")
821
821
822 def getmimetype(self):
822 def getmimetype(self):
823 return mimetypes.guess_type(self.basename())[0]
823 return mimetypes.guess_type(self.basename())[0]
824 mimetype = property(getmimetype, None, None, "MIME type")
824 mimetype = property(getmimetype, None, None, "MIME type")
825
825
826 def getencoding(self):
826 def getencoding(self):
827 return mimetypes.guess_type(self.basename())[1]
827 return mimetypes.guess_type(self.basename())[1]
828 encoding = property(getencoding, None, None, "Compression")
828 encoding = property(getencoding, None, None, "Compression")
829
829
830 def __repr__(self):
830 def __repr__(self):
831 return "ifile(%s)" % path._base.__repr__(self)
831 return "ifile(%s)" % path._base.__repr__(self)
832
832
833 defaultattrs = (None, "type", "size", "modestr", "owner", "group", "mdate")
833 defaultattrs = (None, "type", "size", "modestr", "owner", "group", "mdate")
834
834
835 def __xattrs__(self, mode):
835 def __xattrs__(self, mode):
836 if mode == "detail":
836 if mode == "detail":
837 return (
837 return (
838 "name", "basename()", "abspath()", "realpath()",
838 "name", "basename()", "abspath()", "realpath()",
839 "type", "mode", "modestr", "stat()", "lstat()",
839 "type", "mode", "modestr", "stat()", "lstat()",
840 "uid", "gid", "owner", "group", "dev", "nlink",
840 "uid", "gid", "owner", "group", "dev", "nlink",
841 "ctime", "mtime", "atime", "cdate", "mdate", "adate",
841 "ctime", "mtime", "atime", "cdate", "mdate", "adate",
842 "size", "blocks", "blksize", "isdir()", "islink()",
842 "size", "blocks", "blksize", "isdir()", "islink()",
843 "mimetype", "encoding"
843 "mimetype", "encoding"
844 )
844 )
845 return self.defaultattrs
845 return self.defaultattrs
846
846
847 def __xrepr__(self, mode):
847 def __xrepr__(self, mode):
848 try:
848 try:
849 if self.isdir():
849 if self.isdir():
850 name = "idir"
850 name = "idir"
851 style = astyle.style_dir
851 style = astyle.style_dir
852 else:
852 else:
853 name = "ifile"
853 name = "ifile"
854 style = astyle.style_file
854 style = astyle.style_file
855 except IOError:
855 except IOError:
856 name = "ifile"
856 name = "ifile"
857 style = astyle.style_default
857 style = astyle.style_default
858 if mode == "cell" or mode in "header" or mode == "footer":
858 if mode == "cell" or mode in "header" or mode == "footer":
859 abspath = repr(path._base(self.normpath()))
859 abspath = repr(path._base(self.normpath()))
860 if abspath.startswith("u"):
860 if abspath.startswith("u"):
861 abspath = abspath[2:-1]
861 abspath = abspath[2:-1]
862 else:
862 else:
863 abspath = abspath[1:-1]
863 abspath = abspath[1:-1]
864 if mode == "cell":
864 if mode == "cell":
865 yield (style, abspath)
865 yield (style, abspath)
866 else:
866 else:
867 yield (style, "%s(%s)" % (name, abspath))
867 yield (style, "%s(%s)" % (name, abspath))
868 else:
868 else:
869 yield (style, repr(self))
869 yield (style, repr(self))
870
870
871 def __xiter__(self, mode):
871 def __xiter__(self, mode):
872 if self.isdir():
872 if self.isdir():
873 yield iparentdir(self / os.pardir)
873 yield iparentdir(self / os.pardir)
874 for child in sorted(self.listdir()):
874 for child in sorted(self.listdir()):
875 yield child
875 yield child
876 else:
876 else:
877 f = self.open("rb")
877 f = self.open("rb")
878 for line in f:
878 for line in f:
879 yield line
879 yield line
880 f.close()
880 f.close()
881
881
882
882
883 class iparentdir(ifile):
883 class iparentdir(ifile):
884 def __xrepr__(self, mode):
884 def __xrepr__(self, mode):
885 if mode == "cell":
885 if mode == "cell":
886 yield (astyle.style_dir, os.pardir)
886 yield (astyle.style_dir, os.pardir)
887 else:
887 else:
888 for part in ifile.__xrepr__(self, mode):
888 for part in ifile.__xrepr__(self, mode):
889 yield part
889 yield part
890
890
891
891
892 class ils(Table):
892 class ils(Table):
893 """
893 """
894 List the current (or a specific) directory.
894 List the current (or a specific) directory.
895
895
896 Examples:
896 Examples:
897
897
898 >>> ils
898 >>> ils
899 >>> ils("/usr/local/lib/python2.4")
899 >>> ils("/usr/local/lib/python2.4")
900 >>> ils("~")
900 >>> ils("~")
901 """
901 """
902 def __init__(self, base=os.curdir):
902 def __init__(self, base=os.curdir):
903 self.base = os.path.expanduser(base)
903 self.base = os.path.expanduser(base)
904
904
905 def __xiter__(self, mode):
905 def __xiter__(self, mode):
906 return xiter(ifile(self.base), mode)
906 return xiter(ifile(self.base), mode)
907
907
908 def __xrepr__(self, mode):
908 def __xrepr__(self, mode):
909 return ifile(self.base).__xrepr__(mode)
909 return ifile(self.base).__xrepr__(mode)
910
910
911 def __repr__(self):
911 def __repr__(self):
912 return "%s.%s(%r)" % \
912 return "%s.%s(%r)" % \
913 (self.__class__.__module__, self.__class__.__name__, self.base)
913 (self.__class__.__module__, self.__class__.__name__, self.base)
914
914
915
915
916 class iglob(Table):
916 class iglob(Table):
917 """
917 """
918 List all files and directories matching a specified pattern.
918 List all files and directories matching a specified pattern.
919 (See ``glob.glob()`` for more info.).
919 (See ``glob.glob()`` for more info.).
920
920
921 Examples:
921 Examples:
922
922
923 >>> iglob("*.py")
923 >>> iglob("*.py")
924 """
924 """
925 def __init__(self, glob):
925 def __init__(self, glob):
926 self.glob = glob
926 self.glob = glob
927
927
928 def __xiter__(self, mode):
928 def __xiter__(self, mode):
929 for name in glob.glob(self.glob):
929 for name in glob.glob(self.glob):
930 yield ifile(name)
930 yield ifile(name)
931
931
932 def __xrepr__(self, mode):
932 def __xrepr__(self, mode):
933 if mode == "header" or mode == "footer" or mode == "cell":
933 if mode == "header" or mode == "footer" or mode == "cell":
934 yield (astyle.style_default,
934 yield (astyle.style_default,
935 "%s(%r)" % (self.__class__.__name__, self.glob))
935 "%s(%r)" % (self.__class__.__name__, self.glob))
936 else:
936 else:
937 yield (astyle.style_default, repr(self))
937 yield (astyle.style_default, repr(self))
938
938
939 def __repr__(self):
939 def __repr__(self):
940 return "%s.%s(%r)" % \
940 return "%s.%s(%r)" % \
941 (self.__class__.__module__, self.__class__.__name__, self.glob)
941 (self.__class__.__module__, self.__class__.__name__, self.glob)
942
942
943
943
944 class iwalk(Table):
944 class iwalk(Table):
945 """
945 """
946 List all files and directories in a directory and it's subdirectory.
946 List all files and directories in a directory and it's subdirectory.
947
947
948 >>> iwalk
948 >>> iwalk
949 >>> iwalk("/usr/local/lib/python2.4")
949 >>> iwalk("/usr/local/lib/python2.4")
950 >>> iwalk("~")
950 >>> iwalk("~")
951 """
951 """
952 def __init__(self, base=os.curdir, dirs=True, files=True):
952 def __init__(self, base=os.curdir, dirs=True, files=True):
953 self.base = os.path.expanduser(base)
953 self.base = os.path.expanduser(base)
954 self.dirs = dirs
954 self.dirs = dirs
955 self.files = files
955 self.files = files
956
956
957 def __xiter__(self, mode):
957 def __xiter__(self, mode):
958 for (dirpath, dirnames, filenames) in os.walk(self.base):
958 for (dirpath, dirnames, filenames) in os.walk(self.base):
959 if self.dirs:
959 if self.dirs:
960 for name in sorted(dirnames):
960 for name in sorted(dirnames):
961 yield ifile(os.path.join(dirpath, name))
961 yield ifile(os.path.join(dirpath, name))
962 if self.files:
962 if self.files:
963 for name in sorted(filenames):
963 for name in sorted(filenames):
964 yield ifile(os.path.join(dirpath, name))
964 yield ifile(os.path.join(dirpath, name))
965
965
966 def __xrepr__(self, mode):
966 def __xrepr__(self, mode):
967 if mode == "header" or mode == "footer" or mode == "cell":
967 if mode == "header" or mode == "footer" or mode == "cell":
968 yield (astyle.style_default,
968 yield (astyle.style_default,
969 "%s(%r)" % (self.__class__.__name__, self.base))
969 "%s(%r)" % (self.__class__.__name__, self.base))
970 else:
970 else:
971 yield (astyle.style_default, repr(self))
971 yield (astyle.style_default, repr(self))
972
972
973 def __repr__(self):
973 def __repr__(self):
974 return "%s.%s(%r)" % \
974 return "%s.%s(%r)" % \
975 (self.__class__.__module__, self.__class__.__name__, self.base)
975 (self.__class__.__module__, self.__class__.__name__, self.base)
976
976
977
977
978 class ipwdentry(object):
978 class ipwdentry(object):
979 """
979 """
980 ``ipwdentry`` objects encapsulate entries in the Unix user account and
980 ``ipwdentry`` objects encapsulate entries in the Unix user account and
981 password database.
981 password database.
982 """
982 """
983 def __init__(self, id):
983 def __init__(self, id):
984 self._id = id
984 self._id = id
985 self._entry = None
985 self._entry = None
986
986
987 def _getentry(self):
987 def _getentry(self):
988 if self._entry is None:
988 if self._entry is None:
989 if isinstance(self._id, basestring):
989 if isinstance(self._id, basestring):
990 self._entry = pwd.getpwnam(self._id)
990 self._entry = pwd.getpwnam(self._id)
991 else:
991 else:
992 self._entry = pwd.getpwuid(self._id)
992 self._entry = pwd.getpwuid(self._id)
993 return self._entry
993 return self._entry
994
994
995 def getname(self):
995 def getname(self):
996 if isinstance(self._id, basestring):
996 if isinstance(self._id, basestring):
997 return self._id
997 return self._id
998 else:
998 else:
999 return self._getentry().pw_name
999 return self._getentry().pw_name
1000 name = property(getname, None, None, "User name")
1000 name = property(getname, None, None, "User name")
1001
1001
1002 def getpasswd(self):
1002 def getpasswd(self):
1003 return self._getentry().pw_passwd
1003 return self._getentry().pw_passwd
1004 passwd = property(getpasswd, None, None, "Password")
1004 passwd = property(getpasswd, None, None, "Password")
1005
1005
1006 def getuid(self):
1006 def getuid(self):
1007 if isinstance(self._id, basestring):
1007 if isinstance(self._id, basestring):
1008 return self._getentry().pw_uid
1008 return self._getentry().pw_uid
1009 else:
1009 else:
1010 return self._id
1010 return self._id
1011 uid = property(getuid, None, None, "User id")
1011 uid = property(getuid, None, None, "User id")
1012
1012
1013 def getgid(self):
1013 def getgid(self):
1014 return self._getentry().pw_gid
1014 return self._getentry().pw_gid
1015 gid = property(getgid, None, None, "Primary group id")
1015 gid = property(getgid, None, None, "Primary group id")
1016
1016
1017 def getgroup(self):
1017 def getgroup(self):
1018 return igrpentry(self.gid)
1018 return igrpentry(self.gid)
1019 group = property(getgroup, None, None, "Group")
1019 group = property(getgroup, None, None, "Group")
1020
1020
1021 def getgecos(self):
1021 def getgecos(self):
1022 return self._getentry().pw_gecos
1022 return self._getentry().pw_gecos
1023 gecos = property(getgecos, None, None, "Information (e.g. full user name)")
1023 gecos = property(getgecos, None, None, "Information (e.g. full user name)")
1024
1024
1025 def getdir(self):
1025 def getdir(self):
1026 return self._getentry().pw_dir
1026 return self._getentry().pw_dir
1027 dir = property(getdir, None, None, "$HOME directory")
1027 dir = property(getdir, None, None, "$HOME directory")
1028
1028
1029 def getshell(self):
1029 def getshell(self):
1030 return self._getentry().pw_shell
1030 return self._getentry().pw_shell
1031 shell = property(getshell, None, None, "Login shell")
1031 shell = property(getshell, None, None, "Login shell")
1032
1032
1033 def __xattrs__(self, mode):
1033 def __xattrs__(self, mode):
1034 return ("name", "passwd", "uid", "gid", "gecos", "dir", "shell")
1034 return ("name", "passwd", "uid", "gid", "gecos", "dir", "shell")
1035
1035
1036 def __repr__(self):
1036 def __repr__(self):
1037 return "%s.%s(%r)" % \
1037 return "%s.%s(%r)" % \
1038 (self.__class__.__module__, self.__class__.__name__, self._id)
1038 (self.__class__.__module__, self.__class__.__name__, self._id)
1039
1039
1040
1040
1041 class ipwd(Table):
1041 class ipwd(Table):
1042 """
1042 """
1043 List all entries in the Unix user account and password database.
1043 List all entries in the Unix user account and password database.
1044
1044
1045 Example:
1045 Example:
1046
1046
1047 >>> ipwd | isort("uid")
1047 >>> ipwd | isort("uid")
1048 """
1048 """
1049 def __iter__(self):
1049 def __iter__(self):
1050 for entry in pwd.getpwall():
1050 for entry in pwd.getpwall():
1051 yield ipwdentry(entry.pw_name)
1051 yield ipwdentry(entry.pw_name)
1052
1052
1053 def __xrepr__(self, mode):
1053 def __xrepr__(self, mode):
1054 if mode == "header" or mode == "footer" or mode == "cell":
1054 if mode == "header" or mode == "footer" or mode == "cell":
1055 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1055 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1056 else:
1056 else:
1057 yield (astyle.style_default, repr(self))
1057 yield (astyle.style_default, repr(self))
1058
1058
1059
1059
1060 class igrpentry(object):
1060 class igrpentry(object):
1061 """
1061 """
1062 ``igrpentry`` objects encapsulate entries in the Unix group database.
1062 ``igrpentry`` objects encapsulate entries in the Unix group database.
1063 """
1063 """
1064 def __init__(self, id):
1064 def __init__(self, id):
1065 self._id = id
1065 self._id = id
1066 self._entry = None
1066 self._entry = None
1067
1067
1068 def _getentry(self):
1068 def _getentry(self):
1069 if self._entry is None:
1069 if self._entry is None:
1070 if isinstance(self._id, basestring):
1070 if isinstance(self._id, basestring):
1071 self._entry = grp.getgrnam(self._id)
1071 self._entry = grp.getgrnam(self._id)
1072 else:
1072 else:
1073 self._entry = grp.getgrgid(self._id)
1073 self._entry = grp.getgrgid(self._id)
1074 return self._entry
1074 return self._entry
1075
1075
1076 def getname(self):
1076 def getname(self):
1077 if isinstance(self._id, basestring):
1077 if isinstance(self._id, basestring):
1078 return self._id
1078 return self._id
1079 else:
1079 else:
1080 return self._getentry().gr_name
1080 return self._getentry().gr_name
1081 name = property(getname, None, None, "Group name")
1081 name = property(getname, None, None, "Group name")
1082
1082
1083 def getpasswd(self):
1083 def getpasswd(self):
1084 return self._getentry().gr_passwd
1084 return self._getentry().gr_passwd
1085 passwd = property(getpasswd, None, None, "Password")
1085 passwd = property(getpasswd, None, None, "Password")
1086
1086
1087 def getgid(self):
1087 def getgid(self):
1088 if isinstance(self._id, basestring):
1088 if isinstance(self._id, basestring):
1089 return self._getentry().gr_gid
1089 return self._getentry().gr_gid
1090 else:
1090 else:
1091 return self._id
1091 return self._id
1092 gid = property(getgid, None, None, "Group id")
1092 gid = property(getgid, None, None, "Group id")
1093
1093
1094 def getmem(self):
1094 def getmem(self):
1095 return self._getentry().gr_mem
1095 return self._getentry().gr_mem
1096 mem = property(getmem, None, None, "Members")
1096 mem = property(getmem, None, None, "Members")
1097
1097
1098 def __xattrs__(self, mode):
1098 def __xattrs__(self, mode):
1099 return ("name", "passwd", "gid", "mem")
1099 return ("name", "passwd", "gid", "mem")
1100
1100
1101 def __xrepr__(self, mode):
1101 def __xrepr__(self, mode):
1102 if mode == "header" or mode == "footer" or mode == "cell":
1102 if mode == "header" or mode == "footer" or mode == "cell":
1103 yield (astyle.style_default, "group ")
1103 yield (astyle.style_default, "group ")
1104 try:
1104 try:
1105 yield (astyle.style_default, self.name)
1105 yield (astyle.style_default, self.name)
1106 except KeyError:
1106 except KeyError:
1107 if isinstance(self._id, basestring):
1107 if isinstance(self._id, basestring):
1108 yield (astyle.style_default, self.name_id)
1108 yield (astyle.style_default, self.name_id)
1109 else:
1109 else:
1110 yield (astyle.style_type_number, str(self._id))
1110 yield (astyle.style_type_number, str(self._id))
1111 else:
1111 else:
1112 yield (astyle.style_default, repr(self))
1112 yield (astyle.style_default, repr(self))
1113
1113
1114 def __xiter__(self, mode):
1114 def __xiter__(self, mode):
1115 for member in self.mem:
1115 for member in self.mem:
1116 yield ipwdentry(member)
1116 yield ipwdentry(member)
1117
1117
1118 def __repr__(self):
1118 def __repr__(self):
1119 return "%s.%s(%r)" % \
1119 return "%s.%s(%r)" % \
1120 (self.__class__.__module__, self.__class__.__name__, self._id)
1120 (self.__class__.__module__, self.__class__.__name__, self._id)
1121
1121
1122
1122
1123 class igrp(Table):
1123 class igrp(Table):
1124 """
1124 """
1125 This ``Table`` lists all entries in the Unix group database.
1125 This ``Table`` lists all entries in the Unix group database.
1126 """
1126 """
1127 def __xiter__(self, mode):
1127 def __xiter__(self, mode):
1128 for entry in grp.getgrall():
1128 for entry in grp.getgrall():
1129 yield igrpentry(entry.gr_name)
1129 yield igrpentry(entry.gr_name)
1130
1130
1131 def __xrepr__(self, mode):
1131 def __xrepr__(self, mode):
1132 if mode == "header" or mode == "footer":
1132 if mode == "header" or mode == "footer":
1133 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1133 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1134 else:
1134 else:
1135 yield (astyle.style_default, repr(self))
1135 yield (astyle.style_default, repr(self))
1136
1136
1137
1137
1138 class Fields(object):
1138 class Fields(object):
1139 def __init__(self, fieldnames, **fields):
1139 def __init__(self, fieldnames, **fields):
1140 self.__fieldnames = fieldnames
1140 self.__fieldnames = fieldnames
1141 for (key, value) in fields.iteritems():
1141 for (key, value) in fields.iteritems():
1142 setattr(self, key, value)
1142 setattr(self, key, value)
1143
1143
1144 def __xattrs__(self, mode):
1144 def __xattrs__(self, mode):
1145 return self.__fieldnames
1145 return self.__fieldnames
1146
1146
1147 def __xrepr__(self, mode):
1147 def __xrepr__(self, mode):
1148 yield (-1, False)
1148 yield (-1, False)
1149 if mode == "header" or mode == "cell":
1149 if mode == "header" or mode == "cell":
1150 yield (astyle.style_default, self.__class__.__name__)
1150 yield (astyle.style_default, self.__class__.__name__)
1151 yield (astyle.style_default, "(")
1151 yield (astyle.style_default, "(")
1152 for (i, f) in enumerate(self.__fieldnames):
1152 for (i, f) in enumerate(self.__fieldnames):
1153 if i:
1153 if i:
1154 yield (astyle.style_default, ", ")
1154 yield (astyle.style_default, ", ")
1155 yield (astyle.style_default, f)
1155 yield (astyle.style_default, f)
1156 yield (astyle.style_default, "=")
1156 yield (astyle.style_default, "=")
1157 for part in xrepr(getattr(self, f), "default"):
1157 for part in xrepr(getattr(self, f), "default"):
1158 yield part
1158 yield part
1159 yield (astyle.style_default, ")")
1159 yield (astyle.style_default, ")")
1160 elif mode == "footer":
1160 elif mode == "footer":
1161 yield (astyle.style_default, self.__class__.__name__)
1161 yield (astyle.style_default, self.__class__.__name__)
1162 yield (astyle.style_default, "(")
1162 yield (astyle.style_default, "(")
1163 for (i, f) in enumerate(self.__fieldnames):
1163 for (i, f) in enumerate(self.__fieldnames):
1164 if i:
1164 if i:
1165 yield (astyle.style_default, ", ")
1165 yield (astyle.style_default, ", ")
1166 yield (astyle.style_default, f)
1166 yield (astyle.style_default, f)
1167 yield (astyle.style_default, ")")
1167 yield (astyle.style_default, ")")
1168 else:
1168 else:
1169 yield (astyle.style_default, repr(self))
1169 yield (astyle.style_default, repr(self))
1170
1170
1171
1171
1172 class FieldTable(Table, list):
1172 class FieldTable(Table, list):
1173 def __init__(self, *fields):
1173 def __init__(self, *fields):
1174 Table.__init__(self)
1174 Table.__init__(self)
1175 list.__init__(self)
1175 list.__init__(self)
1176 self.fields = fields
1176 self.fields = fields
1177
1177
1178 def add(self, **fields):
1178 def add(self, **fields):
1179 self.append(Fields(self.fields, **fields))
1179 self.append(Fields(self.fields, **fields))
1180
1180
1181 def __xiter__(self, mode):
1181 def __xiter__(self, mode):
1182 return list.__iter__(self)
1182 return list.__iter__(self)
1183
1183
1184 def __xrepr__(self, mode):
1184 def __xrepr__(self, mode):
1185 yield (-1, False)
1185 yield (-1, False)
1186 if mode == "header" or mode == "footer":
1186 if mode == "header" or mode == "footer":
1187 yield (astyle.style_default, self.__class__.__name__)
1187 yield (astyle.style_default, self.__class__.__name__)
1188 yield (astyle.style_default, "(")
1188 yield (astyle.style_default, "(")
1189 for (i, f) in enumerate(self.__fieldnames):
1189 for (i, f) in enumerate(self.__fieldnames):
1190 if i:
1190 if i:
1191 yield (astyle.style_default, ", ")
1191 yield (astyle.style_default, ", ")
1192 yield (astyle.style_default, f)
1192 yield (astyle.style_default, f)
1193 yield (astyle.style_default, ")")
1193 yield (astyle.style_default, ")")
1194 else:
1194 else:
1195 yield (astyle.style_default, repr(self))
1195 yield (astyle.style_default, repr(self))
1196
1196
1197 def __repr__(self):
1197 def __repr__(self):
1198 return "<%s.%s object with fields=%r at 0x%x>" % \
1198 return "<%s.%s object with fields=%r at 0x%x>" % \
1199 (self.__class__.__module__, self.__class__.__name__,
1199 (self.__class__.__module__, self.__class__.__name__,
1200 ", ".join(map(repr, self.fields)), id(self))
1200 ", ".join(map(repr, self.fields)), id(self))
1201
1201
1202
1202
1203 class List(list):
1203 class List(list):
1204 def __xattrs__(self, mode):
1204 def __xattrs__(self, mode):
1205 return xrange(len(self))
1205 return xrange(len(self))
1206
1206
1207 def __xrepr__(self, mode):
1207 def __xrepr__(self, mode):
1208 yield (-1, False)
1208 yield (-1, False)
1209 if mode == "header" or mode == "cell" or mode == "footer" or mode == "default":
1209 if mode == "header" or mode == "cell" or mode == "footer" or mode == "default":
1210 yield (astyle.style_default, self.__class__.__name__)
1210 yield (astyle.style_default, self.__class__.__name__)
1211 yield (astyle.style_default, "(")
1211 yield (astyle.style_default, "(")
1212 for (i, item) in enumerate(self):
1212 for (i, item) in enumerate(self):
1213 if i:
1213 if i:
1214 yield (astyle.style_default, ", ")
1214 yield (astyle.style_default, ", ")
1215 for part in xrepr(item, "default"):
1215 for part in xrepr(item, "default"):
1216 yield part
1216 yield part
1217 yield (astyle.style_default, ")")
1217 yield (astyle.style_default, ")")
1218 else:
1218 else:
1219 yield (astyle.style_default, repr(self))
1219 yield (astyle.style_default, repr(self))
1220
1220
1221
1221
1222 class ienv(Table):
1222 class ienv(Table):
1223 """
1223 """
1224 List environment variables.
1224 List environment variables.
1225
1225
1226 Example:
1226 Example:
1227
1227
1228 >>> ienv
1228 >>> ienv
1229 """
1229 """
1230
1230
1231 def __xiter__(self, mode):
1231 def __xiter__(self, mode):
1232 fields = ("key", "value")
1232 fields = ("key", "value")
1233 for (key, value) in os.environ.iteritems():
1233 for (key, value) in os.environ.iteritems():
1234 yield Fields(fields, key=key, value=value)
1234 yield Fields(fields, key=key, value=value)
1235
1235
1236 def __xrepr__(self, mode):
1236 def __xrepr__(self, mode):
1237 if mode == "header" or mode == "cell":
1237 if mode == "header" or mode == "cell":
1238 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1238 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1239 else:
1239 else:
1240 yield (astyle.style_default, repr(self))
1240 yield (astyle.style_default, repr(self))
1241
1241
1242
1242
1243 class icsv(Pipe):
1243 class icsv(Pipe):
1244 """
1244 """
1245 This ``Pipe`` lists turn the input (with must be a pipe outputting lines
1245 This ``Pipe`` lists turn the input (with must be a pipe outputting lines
1246 or an ``ifile``) into lines of CVS columns.
1246 or an ``ifile``) into lines of CVS columns.
1247 """
1247 """
1248 def __init__(self, **csvargs):
1248 def __init__(self, **csvargs):
1249 """
1249 """
1250 Create an ``icsv`` object. ``cvsargs`` will be passed through as
1250 Create an ``icsv`` object. ``cvsargs`` will be passed through as
1251 keyword arguments to ``cvs.reader()``.
1251 keyword arguments to ``cvs.reader()``.
1252 """
1252 """
1253 self.csvargs = csvargs
1253 self.csvargs = csvargs
1254
1254
1255 def __xiter__(self, mode):
1255 def __xiter__(self, mode):
1256 input = self.input
1256 input = self.input
1257 if isinstance(input, ifile):
1257 if isinstance(input, ifile):
1258 input = input.open("rb")
1258 input = input.open("rb")
1259 reader = csv.reader(input, **self.csvargs)
1259 reader = csv.reader(input, **self.csvargs)
1260 for line in reader:
1260 for line in reader:
1261 yield List(line)
1261 yield List(line)
1262
1262
1263 def __xrepr__(self, mode):
1263 def __xrepr__(self, mode):
1264 yield (-1, False)
1264 yield (-1, False)
1265 if mode == "header" or mode == "footer":
1265 if mode == "header" or mode == "footer":
1266 input = getattr(self, "input", None)
1266 input = getattr(self, "input", None)
1267 if input is not None:
1267 if input is not None:
1268 for part in xrepr(input, mode):
1268 for part in xrepr(input, mode):
1269 yield part
1269 yield part
1270 yield (astyle.style_default, " | ")
1270 yield (astyle.style_default, " | ")
1271 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1271 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1272 for (i, (name, value)) in enumerate(self.csvargs.iteritems()):
1272 for (i, (name, value)) in enumerate(self.csvargs.iteritems()):
1273 if i:
1273 if i:
1274 yield (astyle.style_default, ", ")
1274 yield (astyle.style_default, ", ")
1275 yield (astyle.style_default, name)
1275 yield (astyle.style_default, name)
1276 yield (astyle.style_default, "=")
1276 yield (astyle.style_default, "=")
1277 for part in xrepr(value, "default"):
1277 for part in xrepr(value, "default"):
1278 yield part
1278 yield part
1279 yield (astyle.style_default, ")")
1279 yield (astyle.style_default, ")")
1280 else:
1280 else:
1281 yield (astyle.style_default, repr(self))
1281 yield (astyle.style_default, repr(self))
1282
1282
1283 def __repr__(self):
1283 def __repr__(self):
1284 args = ", ".join(["%s=%r" % item for item in self.csvargs.iteritems()])
1284 args = ", ".join(["%s=%r" % item for item in self.csvargs.iteritems()])
1285 return "<%s.%s %s at 0x%x>" % \
1285 return "<%s.%s %s at 0x%x>" % \
1286 (self.__class__.__module__, self.__class__.__name__, args, id(self))
1286 (self.__class__.__module__, self.__class__.__name__, args, id(self))
1287
1287
1288
1288
1289 class ix(Table):
1289 class ix(Table):
1290 """
1290 """
1291 Execute a system command and list its output as lines
1291 Execute a system command and list its output as lines
1292 (similar to ``os.popen()``).
1292 (similar to ``os.popen()``).
1293
1293
1294 Examples:
1294 Examples:
1295
1295
1296 >>> ix("ps x")
1296 >>> ix("ps x")
1297 >>> ix("find .") | ifile
1297 >>> ix("find .") | ifile
1298 """
1298 """
1299 def __init__(self, cmd):
1299 def __init__(self, cmd):
1300 self.cmd = cmd
1300 self.cmd = cmd
1301 self._pipe = None
1301 self._pipe = None
1302
1302
1303 def __xiter__(self, mode):
1303 def __xiter__(self, mode):
1304 self._pipe = os.popen(self.cmd)
1304 self._pipe = os.popen(self.cmd)
1305 for l in self._pipe:
1305 for l in self._pipe:
1306 yield l.rstrip("\r\n")
1306 yield l.rstrip("\r\n")
1307 self._pipe.close()
1307 self._pipe.close()
1308 self._pipe = None
1308 self._pipe = None
1309
1309
1310 def __del__(self):
1310 def __del__(self):
1311 if self._pipe is not None and not self._pipe.closed:
1311 if self._pipe is not None and not self._pipe.closed:
1312 self._pipe.close()
1312 self._pipe.close()
1313 self._pipe = None
1313 self._pipe = None
1314
1314
1315 def __xrepr__(self, mode):
1315 def __xrepr__(self, mode):
1316 if mode == "header" or mode == "footer":
1316 if mode == "header" or mode == "footer":
1317 yield (astyle.style_default,
1317 yield (astyle.style_default,
1318 "%s(%r)" % (self.__class__.__name__, self.cmd))
1318 "%s(%r)" % (self.__class__.__name__, self.cmd))
1319 else:
1319 else:
1320 yield (astyle.style_default, repr(self))
1320 yield (astyle.style_default, repr(self))
1321
1321
1322 def __repr__(self):
1322 def __repr__(self):
1323 return "%s.%s(%r)" % \
1323 return "%s.%s(%r)" % \
1324 (self.__class__.__module__, self.__class__.__name__, self.cmd)
1324 (self.__class__.__module__, self.__class__.__name__, self.cmd)
1325
1325
1326
1326
1327 class ifilter(Pipe):
1327 class ifilter(Pipe):
1328 """
1328 """
1329 Filter an input pipe. Only objects where an expression evaluates to true
1329 Filter an input pipe. Only objects where an expression evaluates to true
1330 (and doesn't raise an exception) are listed.
1330 (and doesn't raise an exception) are listed.
1331
1331
1332 Examples:
1332 Examples:
1333
1333
1334 >>> ils | ifilter("_.isfile() and size>1000")
1334 >>> ils | ifilter("_.isfile() and size>1000")
1335 >>> igrp | ifilter("len(mem)")
1335 >>> igrp | ifilter("len(mem)")
1336 >>> sys.modules | ifilter(lambda _:_.value is not None)
1336 >>> sys.modules | ifilter(lambda _:_.value is not None)
1337 """
1337 """
1338
1338
1339 def __init__(self, expr, globals=None, errors="raiseifallfail"):
1339 def __init__(self, expr, globals=None, errors="raiseifallfail"):
1340 """
1340 """
1341 Create an ``ifilter`` object. ``expr`` can be a callable or a string
1341 Create an ``ifilter`` object. ``expr`` can be a callable or a string
1342 containing an expression. ``globals`` will be used as the global
1342 containing an expression. ``globals`` will be used as the global
1343 namespace for calling string expressions (defaulting to IPython's
1343 namespace for calling string expressions (defaulting to IPython's
1344 user namespace). ``errors`` specifies how exception during evaluation
1344 user namespace). ``errors`` specifies how exception during evaluation
1345 of ``expr`` are handled:
1345 of ``expr`` are handled:
1346
1346
1347 * ``drop``: drop all items that have errors;
1347 * ``drop``: drop all items that have errors;
1348
1348
1349 * ``keep``: keep all items that have errors;
1349 * ``keep``: keep all items that have errors;
1350
1350
1351 * ``keeperror``: keep the exception of all items that have errors;
1351 * ``keeperror``: keep the exception of all items that have errors;
1352
1352
1353 * ``raise``: raise the exception;
1353 * ``raise``: raise the exception;
1354
1354
1355 * ``raiseifallfail``: raise the first exception if all items have errors;
1355 * ``raiseifallfail``: raise the first exception if all items have errors;
1356 otherwise drop those with errors (this is the default).
1356 otherwise drop those with errors (this is the default).
1357 """
1357 """
1358 self.expr = expr
1358 self.expr = expr
1359 self.globals = globals
1359 self.globals = globals
1360 self.errors = errors
1360 self.errors = errors
1361
1361
1362 def __xiter__(self, mode):
1362 def __xiter__(self, mode):
1363 if callable(self.expr):
1363 if callable(self.expr):
1364 def test(item):
1364 def test(item):
1365 return self.expr(item)
1365 return self.expr(item)
1366 else:
1366 else:
1367 g = getglobals(self.globals)
1367 g = getglobals(self.globals)
1368 def test(item):
1368 def test(item):
1369 return eval(self.expr, g, AttrNamespace(item))
1369 return eval(self.expr, g, AttrNamespace(item))
1370
1370
1371 ok = 0
1371 ok = 0
1372 exc_info = None
1372 exc_info = None
1373 for item in xiter(self.input, mode):
1373 for item in xiter(self.input, mode):
1374 try:
1374 try:
1375 if test(item):
1375 if test(item):
1376 yield item
1376 yield item
1377 ok += 1
1377 ok += 1
1378 except (KeyboardInterrupt, SystemExit):
1378 except (KeyboardInterrupt, SystemExit):
1379 raise
1379 raise
1380 except Exception, exc:
1380 except Exception, exc:
1381 if self.errors == "drop":
1381 if self.errors == "drop":
1382 pass # Ignore errors
1382 pass # Ignore errors
1383 elif self.errors == "keep":
1383 elif self.errors == "keep":
1384 yield item
1384 yield item
1385 elif self.errors == "keeperror":
1385 elif self.errors == "keeperror":
1386 yield exc
1386 yield exc
1387 elif self.errors == "raise":
1387 elif self.errors == "raise":
1388 raise
1388 raise
1389 elif self.errors == "raiseifallfail":
1389 elif self.errors == "raiseifallfail":
1390 if exc_info is None:
1390 if exc_info is None:
1391 exc_info = sys.exc_info()
1391 exc_info = sys.exc_info()
1392 if not ok and exc_info is not None:
1392 if not ok and exc_info is not None:
1393 raise exc_info[0], exc_info[1], exc_info[2]
1393 raise exc_info[0], exc_info[1], exc_info[2]
1394
1394
1395 def __xrepr__(self, mode):
1395 def __xrepr__(self, mode):
1396 if mode == "header" or mode == "footer":
1396 if mode == "header" or mode == "footer":
1397 input = getattr(self, "input", None)
1397 input = getattr(self, "input", None)
1398 if input is not None:
1398 if input is not None:
1399 for part in xrepr(input, mode):
1399 for part in xrepr(input, mode):
1400 yield part
1400 yield part
1401 yield (astyle.style_default, " | ")
1401 yield (astyle.style_default, " | ")
1402 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1402 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1403 for part in xrepr(self.expr, "default"):
1403 for part in xrepr(self.expr, "default"):
1404 yield part
1404 yield part
1405 yield (astyle.style_default, ")")
1405 yield (astyle.style_default, ")")
1406 else:
1406 else:
1407 yield (astyle.style_default, repr(self))
1407 yield (astyle.style_default, repr(self))
1408
1408
1409 def __repr__(self):
1409 def __repr__(self):
1410 return "<%s.%s expr=%r at 0x%x>" % \
1410 return "<%s.%s expr=%r at 0x%x>" % \
1411 (self.__class__.__module__, self.__class__.__name__,
1411 (self.__class__.__module__, self.__class__.__name__,
1412 self.expr, id(self))
1412 self.expr, id(self))
1413
1413
1414
1414
1415 class ieval(Pipe):
1415 class ieval(Pipe):
1416 """
1416 """
1417 Evaluate an expression for each object in the input pipe.
1417 Evaluate an expression for each object in the input pipe.
1418
1418
1419 Examples:
1419 Examples:
1420
1420
1421 >>> ils | ieval("_.abspath()")
1421 >>> ils | ieval("_.abspath()")
1422 >>> sys.path | ieval(ifile)
1422 >>> sys.path | ieval(ifile)
1423 """
1423 """
1424
1424
1425 def __init__(self, expr, globals=None, errors="raiseifallfail"):
1425 def __init__(self, expr, globals=None, errors="raiseifallfail"):
1426 """
1426 """
1427 Create an ``ieval`` object. ``expr`` can be a callable or a string
1427 Create an ``ieval`` object. ``expr`` can be a callable or a string
1428 containing an expression. For the meaning of ``globals`` and
1428 containing an expression. For the meaning of ``globals`` and
1429 ``errors`` see ``ifilter``.
1429 ``errors`` see ``ifilter``.
1430 """
1430 """
1431 self.expr = expr
1431 self.expr = expr
1432 self.globals = globals
1432 self.globals = globals
1433 self.errors = errors
1433 self.errors = errors
1434
1434
1435 def __xiter__(self, mode):
1435 def __xiter__(self, mode):
1436 if callable(self.expr):
1436 if callable(self.expr):
1437 def do(item):
1437 def do(item):
1438 return self.expr(item)
1438 return self.expr(item)
1439 else:
1439 else:
1440 g = getglobals(self.globals)
1440 g = getglobals(self.globals)
1441 def do(item):
1441 def do(item):
1442 return eval(self.expr, g, AttrNamespace(item))
1442 return eval(self.expr, g, AttrNamespace(item))
1443
1443
1444 ok = 0
1444 ok = 0
1445 exc_info = None
1445 exc_info = None
1446 for item in xiter(self.input, mode):
1446 for item in xiter(self.input, mode):
1447 try:
1447 try:
1448 yield do(item)
1448 yield do(item)
1449 except (KeyboardInterrupt, SystemExit):
1449 except (KeyboardInterrupt, SystemExit):
1450 raise
1450 raise
1451 except Exception, exc:
1451 except Exception, exc:
1452 if self.errors == "drop":
1452 if self.errors == "drop":
1453 pass # Ignore errors
1453 pass # Ignore errors
1454 elif self.errors == "keep":
1454 elif self.errors == "keep":
1455 yield item
1455 yield item
1456 elif self.errors == "keeperror":
1456 elif self.errors == "keeperror":
1457 yield exc
1457 yield exc
1458 elif self.errors == "raise":
1458 elif self.errors == "raise":
1459 raise
1459 raise
1460 elif self.errors == "raiseifallfail":
1460 elif self.errors == "raiseifallfail":
1461 if exc_info is None:
1461 if exc_info is None:
1462 exc_info = sys.exc_info()
1462 exc_info = sys.exc_info()
1463 if not ok and exc_info is not None:
1463 if not ok and exc_info is not None:
1464 raise exc_info[0], exc_info[1], exc_info[2]
1464 raise exc_info[0], exc_info[1], exc_info[2]
1465
1465
1466 def __xrepr__(self, mode):
1466 def __xrepr__(self, mode):
1467 if mode == "header" or mode == "footer":
1467 if mode == "header" or mode == "footer":
1468 input = getattr(self, "input", None)
1468 input = getattr(self, "input", None)
1469 if input is not None:
1469 if input is not None:
1470 for part in xrepr(input, mode):
1470 for part in xrepr(input, mode):
1471 yield part
1471 yield part
1472 yield (astyle.style_default, " | ")
1472 yield (astyle.style_default, " | ")
1473 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1473 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1474 for part in xrepr(self.expr, "default"):
1474 for part in xrepr(self.expr, "default"):
1475 yield part
1475 yield part
1476 yield (astyle.style_default, ")")
1476 yield (astyle.style_default, ")")
1477 else:
1477 else:
1478 yield (astyle.style_default, repr(self))
1478 yield (astyle.style_default, repr(self))
1479
1479
1480 def __repr__(self):
1480 def __repr__(self):
1481 return "<%s.%s expr=%r at 0x%x>" % \
1481 return "<%s.%s expr=%r at 0x%x>" % \
1482 (self.__class__.__module__, self.__class__.__name__,
1482 (self.__class__.__module__, self.__class__.__name__,
1483 self.expr, id(self))
1483 self.expr, id(self))
1484
1484
1485
1485
1486 class ienum(Pipe):
1486 class ienum(Pipe):
1487 """
1487 """
1488 Enumerate the input pipe (i.e. wrap each input object in an object
1488 Enumerate the input pipe (i.e. wrap each input object in an object
1489 with ``index`` and ``object`` attributes).
1489 with ``index`` and ``object`` attributes).
1490
1490
1491 Examples:
1491 Examples:
1492
1492
1493 >>> xrange(20) | ieval("_,_*_") | ienum | ifilter("index % 2 == 0") | ieval("object")
1493 >>> xrange(20) | ieval("_,_*_") | ienum | ifilter("index % 2 == 0") | ieval("object")
1494 """
1494 """
1495 def __xiter__(self, mode):
1495 def __xiter__(self, mode):
1496 fields = ("index", "object")
1496 fields = ("index", "object")
1497 for (index, object) in enumerate(xiter(self.input, mode)):
1497 for (index, object) in enumerate(xiter(self.input, mode)):
1498 yield Fields(fields, index=index, object=object)
1498 yield Fields(fields, index=index, object=object)
1499
1499
1500
1500
1501 class isort(Pipe):
1501 class isort(Pipe):
1502 """
1502 """
1503 Sorts the input pipe.
1503 Sorts the input pipe.
1504
1504
1505 Examples:
1505 Examples:
1506
1506
1507 >>> ils | isort("size")
1507 >>> ils | isort("size")
1508 >>> ils | isort("_.isdir(), _.lower()", reverse=True)
1508 >>> ils | isort("_.isdir(), _.lower()", reverse=True)
1509 """
1509 """
1510
1510
1511 def __init__(self, key, globals=None, reverse=False):
1511 def __init__(self, key, globals=None, reverse=False):
1512 """
1512 """
1513 Create an ``isort`` object. ``key`` can be a callable or a string
1513 Create an ``isort`` object. ``key`` can be a callable or a string
1514 containing an expression. If ``reverse`` is true the sort order will
1514 containing an expression. If ``reverse`` is true the sort order will
1515 be reversed. For the meaning of ``globals`` see ``ifilter``.
1515 be reversed. For the meaning of ``globals`` see ``ifilter``.
1516 """
1516 """
1517 self.key = key
1517 self.key = key
1518 self.globals = globals
1518 self.globals = globals
1519 self.reverse = reverse
1519 self.reverse = reverse
1520
1520
1521 def __xiter__(self, mode):
1521 def __xiter__(self, mode):
1522 if callable(self.key):
1522 if callable(self.key):
1523 items = sorted(
1523 items = sorted(
1524 xiter(self.input, mode),
1524 xiter(self.input, mode),
1525 key=self.key,
1525 key=self.key,
1526 reverse=self.reverse
1526 reverse=self.reverse
1527 )
1527 )
1528 else:
1528 else:
1529 g = getglobals(self.globals)
1529 g = getglobals(self.globals)
1530 def key(item):
1530 def key(item):
1531 return eval(self.key, g, AttrNamespace(item))
1531 return eval(self.key, g, AttrNamespace(item))
1532 items = sorted(
1532 items = sorted(
1533 xiter(self.input, mode),
1533 xiter(self.input, mode),
1534 key=key,
1534 key=key,
1535 reverse=self.reverse
1535 reverse=self.reverse
1536 )
1536 )
1537 for item in items:
1537 for item in items:
1538 yield item
1538 yield item
1539
1539
1540 def __xrepr__(self, mode):
1540 def __xrepr__(self, mode):
1541 if mode == "header" or mode == "footer":
1541 if mode == "header" or mode == "footer":
1542 input = getattr(self, "input", None)
1542 input = getattr(self, "input", None)
1543 if input is not None:
1543 if input is not None:
1544 for part in xrepr(input, mode):
1544 for part in xrepr(input, mode):
1545 yield part
1545 yield part
1546 yield (astyle.style_default, " | ")
1546 yield (astyle.style_default, " | ")
1547 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1547 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1548 for part in xrepr(self.key, "default"):
1548 for part in xrepr(self.key, "default"):
1549 yield part
1549 yield part
1550 if self.reverse:
1550 if self.reverse:
1551 yield (astyle.style_default, ", ")
1551 yield (astyle.style_default, ", ")
1552 for part in xrepr(True, "default"):
1552 for part in xrepr(True, "default"):
1553 yield part
1553 yield part
1554 yield (astyle.style_default, ")")
1554 yield (astyle.style_default, ")")
1555 else:
1555 else:
1556 yield (astyle.style_default, repr(self))
1556 yield (astyle.style_default, repr(self))
1557
1557
1558 def __repr__(self):
1558 def __repr__(self):
1559 return "<%s.%s key=%r reverse=%r at 0x%x>" % \
1559 return "<%s.%s key=%r reverse=%r at 0x%x>" % \
1560 (self.__class__.__module__, self.__class__.__name__,
1560 (self.__class__.__module__, self.__class__.__name__,
1561 self.key, self.reverse, id(self))
1561 self.key, self.reverse, id(self))
1562
1562
1563
1563
1564 tab = 3 # for expandtabs()
1564 tab = 3 # for expandtabs()
1565
1565
1566 def _format(field):
1566 def _format(field):
1567 if isinstance(field, str):
1567 if isinstance(field, str):
1568 text = repr(field.expandtabs(tab))[1:-1]
1568 text = repr(field.expandtabs(tab))[1:-1]
1569 elif isinstance(field, unicode):
1569 elif isinstance(field, unicode):
1570 text = repr(field.expandtabs(tab))[2:-1]
1570 text = repr(field.expandtabs(tab))[2:-1]
1571 elif isinstance(field, datetime.datetime):
1571 elif isinstance(field, datetime.datetime):
1572 # Don't use strftime() here, as this requires year >= 1900
1572 # Don't use strftime() here, as this requires year >= 1900
1573 text = "%04d-%02d-%02d %02d:%02d:%02d.%06d" % \
1573 text = "%04d-%02d-%02d %02d:%02d:%02d.%06d" % \
1574 (field.year, field.month, field.day,
1574 (field.year, field.month, field.day,
1575 field.hour, field.minute, field.second, field.microsecond)
1575 field.hour, field.minute, field.second, field.microsecond)
1576 elif isinstance(field, datetime.date):
1576 elif isinstance(field, datetime.date):
1577 text = "%04d-%02d-%02d" % (field.year, field.month, field.day)
1577 text = "%04d-%02d-%02d" % (field.year, field.month, field.day)
1578 else:
1578 else:
1579 text = repr(field)
1579 text = repr(field)
1580 return text
1580 return text
1581
1581
1582
1582
1583 class Display(object):
1583 class Display(object):
1584 class __metaclass__(type):
1584 class __metaclass__(type):
1585 def __ror__(self, input):
1585 def __ror__(self, input):
1586 return input | self()
1586 return input | self()
1587
1587
1588 def __ror__(self, input):
1588 def __ror__(self, input):
1589 self.input = input
1589 self.input = input
1590 return self
1590 return self
1591
1591
1592 def display(self):
1592 def display(self):
1593 pass
1593 pass
1594
1594
1595
1595
1596 class iless(Display):
1596 class iless(Display):
1597 cmd = "less --quit-if-one-screen --LONG-PROMPT --LINE-NUMBERS --chop-long-lines --shift=8 --RAW-CONTROL-CHARS"
1597 cmd = "less --quit-if-one-screen --LONG-PROMPT --LINE-NUMBERS --chop-long-lines --shift=8 --RAW-CONTROL-CHARS"
1598
1598
1599 def display(self):
1599 def display(self):
1600 try:
1600 try:
1601 pager = os.popen(self.cmd, "w")
1601 pager = os.popen(self.cmd, "w")
1602 try:
1602 try:
1603 for item in xiter(self.input, "default"):
1603 for item in xiter(self.input, "default"):
1604 attrs = xattrs(item, "default")
1604 attrs = xattrs(item, "default")
1605 attrs = ["%s=%s" % (a, _format(_getattr(item, a))) for a in attrs]
1605 attrs = ["%s=%s" % (a, _format(_getattr(item, a))) for a in attrs]
1606 pager.write(" ".join(attrs))
1606 pager.write(" ".join(attrs))
1607 pager.write("\n")
1607 pager.write("\n")
1608 finally:
1608 finally:
1609 pager.close()
1609 pager.close()
1610 except Exception, exc:
1610 except Exception, exc:
1611 print "%s: %s" % (exc.__class__.__name__, str(exc))
1611 print "%s: %s" % (exc.__class__.__name__, str(exc))
1612
1612
1613
1613
1614 def xformat(value, mode, maxlength):
1614 def xformat(value, mode, maxlength):
1615 align = None
1615 align = None
1616 full = True
1616 full = True
1617 width = 0
1617 width = 0
1618 text = astyle.Text()
1618 text = astyle.Text()
1619 for (style, part) in xrepr(value, mode):
1619 for (style, part) in xrepr(value, mode):
1620 # only consider the first result
1620 # only consider the first result
1621 if align is None:
1621 if align is None:
1622 if isinstance(style, int):
1622 if isinstance(style, int):
1623 # (style, text) really is (alignment, stop)
1623 # (style, text) really is (alignment, stop)
1624 align = style
1624 align = style
1625 full = part
1625 full = part
1626 continue
1626 continue
1627 else:
1627 else:
1628 align = -1
1628 align = -1
1629 full = True
1629 full = True
1630 if not isinstance(style, int):
1630 if not isinstance(style, int):
1631 text.append((style, part))
1631 text.append((style, part))
1632 width += len(part)
1632 width += len(part)
1633 if width >= maxlength and not full:
1633 if width >= maxlength and not full:
1634 text.append((astyle.style_ellisis, "..."))
1634 text.append((astyle.style_ellisis, "..."))
1635 width += 3
1635 width += 3
1636 break
1636 break
1637 if align is None: # default to left alignment
1637 if align is None: # default to left alignment
1638 align = -1
1638 align = -1
1639 return (align, width, text)
1639 return (align, width, text)
1640
1640
1641
1641
1642 class idump(Display):
1642 class idump(Display):
1643 # The approximate maximum length of a column entry
1643 # The approximate maximum length of a column entry
1644 maxattrlength = 200
1644 maxattrlength = 200
1645
1645
1646 # Style for column names
1646 # Style for column names
1647 style_header = astyle.Style.fromstr("white:black:bold")
1647 style_header = astyle.Style.fromstr("white:black:bold")
1648
1648
1649 def __init__(self, *attrs):
1649 def __init__(self, *attrs):
1650 self.attrs = attrs
1650 self.attrs = attrs
1651 self.headerpadchar = " "
1651 self.headerpadchar = " "
1652 self.headersepchar = "|"
1652 self.headersepchar = "|"
1653 self.datapadchar = " "
1653 self.datapadchar = " "
1654 self.datasepchar = "|"
1654 self.datasepchar = "|"
1655
1655
1656 def display(self):
1656 def display(self):
1657 stream = genutils.Term.cout
1657 stream = genutils.Term.cout
1658 allattrs = []
1658 allattrs = []
1659 allattrset = set()
1659 allattrset = set()
1660 colwidths = {}
1660 colwidths = {}
1661 rows = []
1661 rows = []
1662 for item in xiter(self.input, "default"):
1662 for item in xiter(self.input, "default"):
1663 row = {}
1663 row = {}
1664 attrs = self.attrs
1664 attrs = self.attrs
1665 if not attrs:
1665 if not attrs:
1666 attrs = xattrs(item, "default")
1666 attrs = xattrs(item, "default")
1667 for attrname in attrs:
1667 for attrname in attrs:
1668 if attrname not in allattrset:
1668 if attrname not in allattrset:
1669 allattrs.append(attrname)
1669 allattrs.append(attrname)
1670 allattrset.add(attrname)
1670 allattrset.add(attrname)
1671 colwidths[attrname] = len(_attrname(attrname))
1671 colwidths[attrname] = len(_attrname(attrname))
1672 try:
1672 try:
1673 value = _getattr(item, attrname, None)
1673 value = _getattr(item, attrname, None)
1674 except (KeyboardInterrupt, SystemExit):
1674 except (KeyboardInterrupt, SystemExit):
1675 raise
1675 raise
1676 except Exception, exc:
1676 except Exception, exc:
1677 value = exc
1677 value = exc
1678 (align, width, text) = xformat(value, "cell", self.maxattrlength)
1678 (align, width, text) = xformat(value, "cell", self.maxattrlength)
1679 colwidths[attrname] = max(colwidths[attrname], width)
1679 colwidths[attrname] = max(colwidths[attrname], width)
1680 # remember alignment, length and colored parts
1680 # remember alignment, length and colored parts
1681 row[attrname] = (align, width, text)
1681 row[attrname] = (align, width, text)
1682 rows.append(row)
1682 rows.append(row)
1683
1683
1684 stream.write("\n")
1684 stream.write("\n")
1685 for (i, attrname) in enumerate(allattrs):
1685 for (i, attrname) in enumerate(allattrs):
1686 self.style_header(_attrname(attrname)).write(stream)
1686 self.style_header(_attrname(attrname)).write(stream)
1687 spc = colwidths[attrname] - len(_attrname(attrname))
1687 spc = colwidths[attrname] - len(_attrname(attrname))
1688 if i < len(colwidths)-1:
1688 if i < len(colwidths)-1:
1689 stream.write(self.headerpadchar*spc)
1689 stream.write(self.headerpadchar*spc)
1690 stream.write(self.headersepchar)
1690 stream.write(self.headersepchar)
1691 stream.write("\n")
1691 stream.write("\n")
1692
1692
1693 for row in rows:
1693 for row in rows:
1694 for (i, attrname) in enumerate(allattrs):
1694 for (i, attrname) in enumerate(allattrs):
1695 (align, width, text) = row[attrname]
1695 (align, width, text) = row[attrname]
1696 spc = colwidths[attrname] - width
1696 spc = colwidths[attrname] - width
1697 if align == -1:
1697 if align == -1:
1698 text.write(stream)
1698 text.write(stream)
1699 if i < len(colwidths)-1:
1699 if i < len(colwidths)-1:
1700 stream.write(self.datapadchar*spc)
1700 stream.write(self.datapadchar*spc)
1701 elif align == 0:
1701 elif align == 0:
1702 spc = colwidths[attrname] - width
1702 spc = colwidths[attrname] - width
1703 spc1 = spc//2
1703 spc1 = spc//2
1704 spc2 = spc-spc1
1704 spc2 = spc-spc1
1705 stream.write(self.datapadchar*spc1)
1705 stream.write(self.datapadchar*spc1)
1706 text.write(stream)
1706 text.write(stream)
1707 if i < len(colwidths)-1:
1707 if i < len(colwidths)-1:
1708 stream.write(self.datapadchar*spc2)
1708 stream.write(self.datapadchar*spc2)
1709 else:
1709 else:
1710 stream.write(self.datapadchar*spc)
1710 stream.write(self.datapadchar*spc)
1711 text.write(stream)
1711 text.write(stream)
1712 if i < len(colwidths)-1:
1712 if i < len(colwidths)-1:
1713 stream.write(self.datasepchar)
1713 stream.write(self.datasepchar)
1714 stream.write("\n")
1714 stream.write("\n")
1715
1715
1716
1716
1717 class XMode(object):
1717 class XMode(object):
1718 """
1718 """
1719 An ``XMode`` object describes one enter mode available for an object
1719 An ``XMode`` object describes one enter mode available for an object
1720 """
1720 """
1721 def __init__(self, object, mode, title=None, description=None):
1721 def __init__(self, object, mode, title=None, description=None):
1722 """
1722 """
1723 Create a new ``XMode`` object for the object ``object``. This object
1723 Create a new ``XMode`` object for the object ``object``. This object
1724 must support the enter mode ``mode`` (i.e. ``object.__xiter__(mode)``
1724 must support the enter mode ``mode`` (i.e. ``object.__xiter__(mode)``
1725 must return an iterable). ``title`` and ``description`` will be
1725 must return an iterable). ``title`` and ``description`` will be
1726 displayed in the browser when selecting among the available modes.
1726 displayed in the browser when selecting among the available modes.
1727 """
1727 """
1728 self.object = object
1728 self.object = object
1729 self.mode = mode
1729 self.mode = mode
1730 self.title = title
1730 self.title = title
1731 self.description = description
1731 self.description = description
1732
1732
1733 def __repr__(self):
1733 def __repr__(self):
1734 return "<%s.%s object mode=%r at 0x%x>" % \
1734 return "<%s.%s object mode=%r at 0x%x>" % \
1735 (self.__class__.__module__, self.__class__.__name__,
1735 (self.__class__.__module__, self.__class__.__name__,
1736 self.mode, id(self))
1736 self.mode, id(self))
1737
1737
1738 def __xrepr__(self, mode):
1738 def __xrepr__(self, mode):
1739 if mode == "header" or mode == "footer":
1739 if mode == "header" or mode == "footer":
1740 yield (astyle.style_default, self.title)
1740 yield (astyle.style_default, self.title)
1741 else:
1741 else:
1742 yield (astyle.style_default, repr(self))
1742 yield (astyle.style_default, repr(self))
1743
1743
1744 def __xattrs__(self, mode):
1744 def __xattrs__(self, mode):
1745 if mode == "detail":
1745 if mode == "detail":
1746 return ("object", "mode", "title", "description")
1746 return ("object", "mode", "title", "description")
1747 return ("title", "description")
1747 return ("title", "description")
1748
1748
1749 def __xiter__(self, mode):
1749 def __xiter__(self, mode):
1750 return xiter(self.object, self.mode)
1750 return xiter(self.object, self.mode)
1751
1751
1752
1752
1753 class XAttr(object):
1753 class XAttr(object):
1754 def __init__(self, object, name):
1754 def __init__(self, object, name):
1755 self.name = _attrname(name)
1755 self.name = _attrname(name)
1756
1756
1757 try:
1757 try:
1758 self.value = _getattr(object, name)
1758 self.value = _getattr(object, name)
1759 except (KeyboardInterrupt, SystemExit):
1759 except (KeyboardInterrupt, SystemExit):
1760 raise
1760 raise
1761 except Exception, exc:
1761 except Exception, exc:
1762 if exc.__class__.__module__ == "exceptions":
1762 if exc.__class__.__module__ == "exceptions":
1763 self.value = exc.__class__.__name__
1763 self.value = exc.__class__.__name__
1764 else:
1764 else:
1765 self.value = "%s.%s" % \
1765 self.value = "%s.%s" % \
1766 (exc.__class__.__module__, exc.__class__.__name__)
1766 (exc.__class__.__module__, exc.__class__.__name__)
1767 self.type = self.value
1767 self.type = self.value
1768 else:
1768 else:
1769 t = type(self.value)
1769 t = type(self.value)
1770 if t.__module__ == "__builtin__":
1770 if t.__module__ == "__builtin__":
1771 self.type = t.__name__
1771 self.type = t.__name__
1772 else:
1772 else:
1773 self.type = "%s.%s" % (t.__module__, t.__name__)
1773 self.type = "%s.%s" % (t.__module__, t.__name__)
1774
1774
1775 doc = None
1775 doc = None
1776 if isinstance(name, basestring):
1776 if isinstance(name, basestring):
1777 if name.endswith("()"):
1777 if name.endswith("()"):
1778 doc = getattr(getattr(object, name[:-2]), "__doc__", None)
1778 doc = getattr(getattr(object, name[:-2]), "__doc__", None)
1779 else:
1779 else:
1780 try:
1780 try:
1781 meta = getattr(type(object), name)
1781 meta = getattr(type(object), name)
1782 except AttributeError:
1782 except AttributeError:
1783 pass
1783 pass
1784 else:
1784 else:
1785 if isinstance(meta, property):
1785 if isinstance(meta, property):
1786 doc = getattr(meta, "__doc__", None)
1786 doc = getattr(meta, "__doc__", None)
1787 elif callable(name):
1787 elif callable(name):
1788 doc = getattr(name, "__doc__", None)
1788 doc = getattr(name, "__doc__", None)
1789 if isinstance(doc, basestring):
1789 if isinstance(doc, basestring):
1790 doc = doc.strip()
1790 doc = doc.strip()
1791 self.doc = doc
1791 self.doc = doc
1792
1792
1793 def __xattrs__(self, mode):
1793 def __xattrs__(self, mode):
1794 return ("name", "type", "doc", "value")
1794 return ("name", "type", "doc", "value")
1795
1795
1796
1796
1797 try:
1797 try:
1798 from ibrowse import ibrowse
1798 from ibrowse import ibrowse
1799 except ImportError:
1799 except ImportError:
1800 # No curses (probably Windows) => use ``idump`` as the default display.
1800 # No curses (probably Windows) => use ``idump`` as the default display.
1801 defaultdisplay = idump
1801 defaultdisplay = idump
1802 else:
1802 else:
1803 defaultdisplay = ibrowse
1803 defaultdisplay = ibrowse
1804 __all__.append("ibrowse")
1804 __all__.append("ibrowse")
1805
1805
1806
1806
1807 # If we're running under IPython, install an IPython displayhook that
1807 # If we're running under IPython, install an IPython displayhook that
1808 # returns the object from Display.display(), else install a displayhook
1808 # returns the object from Display.display(), else install a displayhook
1809 # directly as sys.displayhook
1809 # directly as sys.displayhook
1810 api = None
1810 api = None
1811 if ipapi is not None:
1811 if ipapi is not None:
1812 try:
1812 try:
1813 api = ipapi.get()
1813 api = ipapi.get()
1814 except AttributeError:
1814 except AttributeError:
1815 pass
1815 pass
1816
1816
1817 if api is not None:
1817 if api is not None:
1818 def displayhook(self, obj):
1818 def displayhook(self, obj):
1819 if isinstance(obj, type) and issubclass(obj, Table):
1819 if isinstance(obj, type) and issubclass(obj, Table):
1820 obj = obj()
1820 obj = obj()
1821 if isinstance(obj, Table):
1821 if isinstance(obj, Table):
1822 obj = obj | defaultdisplay
1822 obj = obj | defaultdisplay
1823 if isinstance(obj, Display):
1823 if isinstance(obj, Display):
1824 return obj.display()
1824 return obj.display()
1825 else:
1825 else:
1826 raise ipapi.TryNext
1826 raise ipapi.TryNext
1827 api.set_hook("result_display", displayhook)
1827 api.set_hook("result_display", displayhook)
1828 else:
1828 else:
1829 def installdisplayhook():
1829 def installdisplayhook():
1830 _originalhook = sys.displayhook
1830 _originalhook = sys.displayhook
1831 def displayhook(obj):
1831 def displayhook(obj):
1832 if isinstance(obj, type) and issubclass(obj, Table):
1832 if isinstance(obj, type) and issubclass(obj, Table):
1833 obj = obj()
1833 obj = obj()
1834 if isinstance(obj, Table):
1834 if isinstance(obj, Table):
1835 obj = obj | defaultdisplay
1835 obj = obj | defaultdisplay
1836 if isinstance(obj, Display):
1836 if isinstance(obj, Display):
1837 return obj.display()
1837 return obj.display()
1838 else:
1838 else:
1839 _originalhook(obj)
1839 _originalhook(obj)
1840 sys.displayhook = displayhook
1840 sys.displayhook = displayhook
1841 installdisplayhook()
1841 installdisplayhook()
1 NO CONTENT: file renamed from IPython/Extensions/ipy_sane_defaults.py to IPython/Extensions/ipy_defaults.py
NO CONTENT: file renamed from IPython/Extensions/ipy_sane_defaults.py to IPython/Extensions/ipy_defaults.py
@@ -1,24 +1,24 b''
1 """ System wide configuration file for IPython.
1 """ System wide configuration file for IPython.
2
2
3 This will be imported by ipython for all users.
3 This will be imported by ipython for all users.
4
4
5 After this ipy_user_conf.py is imported, user specific configuration
5 After this ipy_user_conf.py is imported, user specific configuration
6 should reside there.
6 should reside there.
7
7
8 """
8 """
9
9
10 import IPython.ipapi
10 import IPython.ipapi
11 ip = IPython.ipapi.get()
11 ip = IPython.ipapi.get()
12
12
13 # add system wide configuration information, import extensions etc. here.
13 # add system wide configuration information, import extensions etc. here.
14 # nothing here is essential
14 # nothing here is essential
15
15
16 import sys
16 import sys
17
17
18 import ext_rehashdir # %rehashdir magic
18 import ext_rehashdir # %rehashdir magic
19 import ext_rescapture # var = !ls and var = %magic
19 import ext_rescapture # var = !ls and var = %magic
20 import pspersistence # %store magic
20 import pspersistence # %store magic
21 import clearcmd # %clear
21 import clearcmd # %clear
22 # Basic readline config
22 # Basic readline config
23
23
24 o = ip.options() No newline at end of file
24 o = ip.options
@@ -1,176 +1,176 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 %store magic for lightweight persistence.
3 %store magic for lightweight persistence.
4
4
5 Stores variables, aliases etc. in PickleShare database.
5 Stores variables, aliases etc. in PickleShare database.
6
6
7 $Id: iplib.py 1107 2006-01-30 19:02:20Z vivainio $
7 $Id: iplib.py 1107 2006-01-30 19:02:20Z vivainio $
8 """
8 """
9
9
10 import IPython.ipapi
10 import IPython.ipapi
11 ip = IPython.ipapi.get()
11 ip = IPython.ipapi.get()
12
12
13 import pickleshare
13 import pickleshare
14
14
15 import inspect,pickle,os,sys,textwrap
15 import inspect,pickle,os,sys,textwrap
16 from IPython.FakeModule import FakeModule
16 from IPython.FakeModule import FakeModule
17
17
18 def restore_aliases(self):
18 def restore_aliases(self):
19 ip = self.getapi()
19 ip = self.getapi()
20 staliases = ip.getdb().get('stored_aliases', {})
20 staliases = ip.db.get('stored_aliases', {})
21 for k,v in staliases.items():
21 for k,v in staliases.items():
22 #print "restore alias",k,v # dbg
22 #print "restore alias",k,v # dbg
23 self.alias_table[k] = v
23 self.alias_table[k] = v
24
24
25
25
26 def refresh_variables(ip):
26 def refresh_variables(ip):
27 db = ip.getdb()
27 db = ip.db
28 for key in db.keys('autorestore/*'):
28 for key in db.keys('autorestore/*'):
29 # strip autorestore
29 # strip autorestore
30 justkey = os.path.basename(key)
30 justkey = os.path.basename(key)
31 try:
31 try:
32 obj = db[key]
32 obj = db[key]
33 except KeyError:
33 except KeyError:
34 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey
34 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey
35 print "The error was:",sys.exc_info()[0]
35 print "The error was:",sys.exc_info()[0]
36 else:
36 else:
37 #print "restored",justkey,"=",obj #dbg
37 #print "restored",justkey,"=",obj #dbg
38 ip.user_ns()[justkey] = obj
38 ip.user_ns[justkey] = obj
39
39
40
40
41
41
42 def restore_data(self):
42 def restore_data(self):
43 ip = self.getapi()
43 ip = self.getapi()
44 refresh_variables(ip)
44 refresh_variables(ip)
45 restore_aliases(self)
45 restore_aliases(self)
46 raise IPython.ipapi.TryNext
46 raise IPython.ipapi.TryNext
47
47
48 ip.set_hook('late_startup_hook', restore_data)
48 ip.set_hook('late_startup_hook', restore_data)
49
49
50 def magic_store(self, parameter_s=''):
50 def magic_store(self, parameter_s=''):
51 """Lightweight persistence for python variables.
51 """Lightweight persistence for python variables.
52
52
53 Example:
53 Example:
54
54
55 ville@badger[~]|1> A = ['hello',10,'world']\\
55 ville@badger[~]|1> A = ['hello',10,'world']\\
56 ville@badger[~]|2> %store A\\
56 ville@badger[~]|2> %store A\\
57 ville@badger[~]|3> Exit
57 ville@badger[~]|3> Exit
58
58
59 (IPython session is closed and started again...)
59 (IPython session is closed and started again...)
60
60
61 ville@badger:~$ ipython -p pysh\\
61 ville@badger:~$ ipython -p pysh\\
62 ville@badger[~]|1> print A
62 ville@badger[~]|1> print A
63
63
64 ['hello', 10, 'world']
64 ['hello', 10, 'world']
65
65
66 Usage:
66 Usage:
67
67
68 %store - Show list of all variables and their current values\\
68 %store - Show list of all variables and their current values\\
69 %store <var> - Store the *current* value of the variable to disk\\
69 %store <var> - Store the *current* value of the variable to disk\\
70 %store -d <var> - Remove the variable and its value from storage\\
70 %store -d <var> - Remove the variable and its value from storage\\
71 %store -z - Remove all variables from storage\\
71 %store -z - Remove all variables from storage\\
72 %store -r - Refresh all variables from store (delete current vals)\\
72 %store -r - Refresh all variables from store (delete current vals)\\
73 %store foo >a.txt - Store value of foo to new file a.txt\\
73 %store foo >a.txt - Store value of foo to new file a.txt\\
74 %store foo >>a.txt - Append value of foo to file a.txt\\
74 %store foo >>a.txt - Append value of foo to file a.txt\\
75
75
76 It should be noted that if you change the value of a variable, you
76 It should be noted that if you change the value of a variable, you
77 need to %store it again if you want to persist the new value.
77 need to %store it again if you want to persist the new value.
78
78
79 Note also that the variables will need to be pickleable; most basic
79 Note also that the variables will need to be pickleable; most basic
80 python types can be safely %stored.
80 python types can be safely %stored.
81 """
81 """
82
82
83 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
83 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
84 args = argsl.split(None,1)
84 args = argsl.split(None,1)
85 ip = self.getapi()
85 ip = self.getapi()
86 db = ip.getdb()
86 db = ip.db
87 # delete
87 # delete
88 if opts.has_key('d'):
88 if opts.has_key('d'):
89 try:
89 try:
90 todel = args[0]
90 todel = args[0]
91 except IndexError:
91 except IndexError:
92 error('You must provide the variable to forget')
92 error('You must provide the variable to forget')
93 else:
93 else:
94 try:
94 try:
95 del db['autorestore/' + todel]
95 del db['autorestore/' + todel]
96 except:
96 except:
97 error("Can't delete variable '%s'" % todel)
97 error("Can't delete variable '%s'" % todel)
98 # reset
98 # reset
99 elif opts.has_key('z'):
99 elif opts.has_key('z'):
100 for k in db.keys('autorestore/*'):
100 for k in db.keys('autorestore/*'):
101 del db[k]
101 del db[k]
102
102
103 elif opts.has_key('r'):
103 elif opts.has_key('r'):
104 refresh_variables(ip)
104 refresh_variables(ip)
105
105
106
106
107 # run without arguments -> list variables & values
107 # run without arguments -> list variables & values
108 elif not args:
108 elif not args:
109 vars = self.db.keys('autorestore/*')
109 vars = self.db.keys('autorestore/*')
110 vars.sort()
110 vars.sort()
111 if vars:
111 if vars:
112 size = max(map(len,vars))
112 size = max(map(len,vars))
113 else:
113 else:
114 size = 0
114 size = 0
115
115
116 print 'Stored variables and their in-db values:'
116 print 'Stored variables and their in-db values:'
117 fmt = '%-'+str(size)+'s -> %s'
117 fmt = '%-'+str(size)+'s -> %s'
118 get = db.get
118 get = db.get
119 for var in vars:
119 for var in vars:
120 justkey = os.path.basename(var)
120 justkey = os.path.basename(var)
121 # print 30 first characters from every var
121 # print 30 first characters from every var
122 print fmt % (justkey,repr(get(var,'<unavailable>'))[:50])
122 print fmt % (justkey,repr(get(var,'<unavailable>'))[:50])
123
123
124 # default action - store the variable
124 # default action - store the variable
125 else:
125 else:
126 # %store foo >file.txt or >>file.txt
126 # %store foo >file.txt or >>file.txt
127 if len(args) > 1 and args[1].startswith('>'):
127 if len(args) > 1 and args[1].startswith('>'):
128 fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
128 fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
129 if args[1].startswith('>>'):
129 if args[1].startswith('>>'):
130 fil = open(fnam,'a')
130 fil = open(fnam,'a')
131 else:
131 else:
132 fil = open(fnam,'w')
132 fil = open(fnam,'w')
133 obj = ip.ev(args[0])
133 obj = ip.ev(args[0])
134 print "Writing '%s' (%s) to file '%s'." % (args[0],
134 print "Writing '%s' (%s) to file '%s'." % (args[0],
135 obj.__class__.__name__, fnam)
135 obj.__class__.__name__, fnam)
136
136
137
137
138 if not isinstance (obj,basestring):
138 if not isinstance (obj,basestring):
139 from pprint import pprint
139 from pprint import pprint
140 pprint(obj,fil)
140 pprint(obj,fil)
141 else:
141 else:
142 fil.write(obj)
142 fil.write(obj)
143 if not obj.endswith('\n'):
143 if not obj.endswith('\n'):
144 fil.write('\n')
144 fil.write('\n')
145
145
146 fil.close()
146 fil.close()
147 return
147 return
148
148
149 # %store foo
149 # %store foo
150 try:
150 try:
151 obj = ip.user_ns()[args[0]]
151 obj = ip.user_ns[args[0]]
152 except KeyError:
152 except KeyError:
153 # it might be an alias
153 # it might be an alias
154 if args[0] in self.alias_table:
154 if args[0] in self.alias_table:
155 staliases = db.get('stored_aliases',{})
155 staliases = db.get('stored_aliases',{})
156 staliases[ args[0] ] = self.alias_table[ args[0] ]
156 staliases[ args[0] ] = self.alias_table[ args[0] ]
157 db['stored_aliases'] = staliases
157 db['stored_aliases'] = staliases
158 print "Alias stored:", args[0], self.alias_table[ args[0] ]
158 print "Alias stored:", args[0], self.alias_table[ args[0] ]
159 return
159 return
160 else:
160 else:
161 print "Error: unknown variable '%s'" % args[0]
161 print "Error: unknown variable '%s'" % args[0]
162
162
163 else:
163 else:
164 if isinstance(inspect.getmodule(obj), FakeModule):
164 if isinstance(inspect.getmodule(obj), FakeModule):
165 print textwrap.dedent("""\
165 print textwrap.dedent("""\
166 Warning:%s is %s
166 Warning:%s is %s
167 Proper storage of interactively declared classes (or instances
167 Proper storage of interactively declared classes (or instances
168 of those classes) is not possible! Only instances
168 of those classes) is not possible! Only instances
169 of classes in real modules on file system can be %%store'd.
169 of classes in real modules on file system can be %%store'd.
170 """ % (args[0], obj) )
170 """ % (args[0], obj) )
171 return
171 return
172 #pickled = pickle.dumps(obj)
172 #pickled = pickle.dumps(obj)
173 self.db[ 'autorestore/' + args[0] ] = obj
173 self.db[ 'autorestore/' + args[0] ] = obj
174 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
174 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
175
175
176 ip.expose_magic('store',magic_store)
176 ip.expose_magic('store',magic_store)
@@ -1,2952 +1,2953 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3
3
4 $Id: Magic.py 1209 2006-03-12 20:34:28Z vivainio $"""
4 $Id: Magic.py 1314 2006-05-19 18:24:14Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #*****************************************************************************
12 #*****************************************************************************
13
13
14 #****************************************************************************
14 #****************************************************************************
15 # Modules and globals
15 # Modules and globals
16
16
17 from IPython import Release
17 from IPython import Release
18 __author__ = '%s <%s>\n%s <%s>' % \
18 __author__ = '%s <%s>\n%s <%s>' % \
19 ( Release.authors['Janko'] + Release.authors['Fernando'] )
19 ( Release.authors['Janko'] + Release.authors['Fernando'] )
20 __license__ = Release.license
20 __license__ = Release.license
21
21
22 # Python standard modules
22 # Python standard modules
23 import __builtin__
23 import __builtin__
24 import bdb
24 import bdb
25 import inspect
25 import inspect
26 import os
26 import os
27 import pdb
27 import pdb
28 import pydoc
28 import pydoc
29 import shlex
29 import sys
30 import sys
30 import re
31 import re
31 import tempfile
32 import tempfile
32 import time
33 import time
33 import cPickle as pickle
34 import cPickle as pickle
34 import textwrap
35 import textwrap
35 from cStringIO import StringIO
36 from cStringIO import StringIO
36 from getopt import getopt,GetoptError
37 from getopt import getopt,GetoptError
37 from pprint import pprint, pformat
38 from pprint import pprint, pformat
38
39
39 # profile isn't bundled by default in Debian for license reasons
40 # profile isn't bundled by default in Debian for license reasons
40 try:
41 try:
41 import profile,pstats
42 import profile,pstats
42 except ImportError:
43 except ImportError:
43 profile = pstats = None
44 profile = pstats = None
44
45
45 # Homebrewed
46 # Homebrewed
46 import IPython
47 import IPython
47 from IPython import Debugger, OInspect, wildcard
48 from IPython import Debugger, OInspect, wildcard
48 from IPython.FakeModule import FakeModule
49 from IPython.FakeModule import FakeModule
49 from IPython.Itpl import Itpl, itpl, printpl,itplns
50 from IPython.Itpl import Itpl, itpl, printpl,itplns
50 from IPython.PyColorize import Parser
51 from IPython.PyColorize import Parser
51 from IPython.ipstruct import Struct
52 from IPython.ipstruct import Struct
52 from IPython.macro import Macro
53 from IPython.macro import Macro
53 from IPython.genutils import *
54 from IPython.genutils import *
54 from IPython import platutils
55 from IPython import platutils
55
56
56 #***************************************************************************
57 #***************************************************************************
57 # Utility functions
58 # Utility functions
58 def on_off(tag):
59 def on_off(tag):
59 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
60 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
60 return ['OFF','ON'][tag]
61 return ['OFF','ON'][tag]
61
62
62 class Bunch: pass
63 class Bunch: pass
63
64
64 #***************************************************************************
65 #***************************************************************************
65 # Main class implementing Magic functionality
66 # Main class implementing Magic functionality
66 class Magic:
67 class Magic:
67 """Magic functions for InteractiveShell.
68 """Magic functions for InteractiveShell.
68
69
69 Shell functions which can be reached as %function_name. All magic
70 Shell functions which can be reached as %function_name. All magic
70 functions should accept a string, which they can parse for their own
71 functions should accept a string, which they can parse for their own
71 needs. This can make some functions easier to type, eg `%cd ../`
72 needs. This can make some functions easier to type, eg `%cd ../`
72 vs. `%cd("../")`
73 vs. `%cd("../")`
73
74
74 ALL definitions MUST begin with the prefix magic_. The user won't need it
75 ALL definitions MUST begin with the prefix magic_. The user won't need it
75 at the command line, but it is is needed in the definition. """
76 at the command line, but it is is needed in the definition. """
76
77
77 # class globals
78 # class globals
78 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
79 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
79 'Automagic is ON, % prefix NOT needed for magic functions.']
80 'Automagic is ON, % prefix NOT needed for magic functions.']
80
81
81 #......................................................................
82 #......................................................................
82 # some utility functions
83 # some utility functions
83
84
84 def __init__(self,shell):
85 def __init__(self,shell):
85
86
86 self.options_table = {}
87 self.options_table = {}
87 if profile is None:
88 if profile is None:
88 self.magic_prun = self.profile_missing_notice
89 self.magic_prun = self.profile_missing_notice
89 self.shell = shell
90 self.shell = shell
90
91
91 # namespace for holding state we may need
92 # namespace for holding state we may need
92 self._magic_state = Bunch()
93 self._magic_state = Bunch()
93
94
94 def profile_missing_notice(self, *args, **kwargs):
95 def profile_missing_notice(self, *args, **kwargs):
95 error("""\
96 error("""\
96 The profile module could not be found. If you are a Debian user,
97 The profile module could not be found. If you are a Debian user,
97 it has been removed from the standard Debian package because of its non-free
98 it has been removed from the standard Debian package because of its non-free
98 license. To use profiling, please install"python2.3-profiler" from non-free.""")
99 license. To use profiling, please install"python2.3-profiler" from non-free.""")
99
100
100 def default_option(self,fn,optstr):
101 def default_option(self,fn,optstr):
101 """Make an entry in the options_table for fn, with value optstr"""
102 """Make an entry in the options_table for fn, with value optstr"""
102
103
103 if fn not in self.lsmagic():
104 if fn not in self.lsmagic():
104 error("%s is not a magic function" % fn)
105 error("%s is not a magic function" % fn)
105 self.options_table[fn] = optstr
106 self.options_table[fn] = optstr
106
107
107 def lsmagic(self):
108 def lsmagic(self):
108 """Return a list of currently available magic functions.
109 """Return a list of currently available magic functions.
109
110
110 Gives a list of the bare names after mangling (['ls','cd', ...], not
111 Gives a list of the bare names after mangling (['ls','cd', ...], not
111 ['magic_ls','magic_cd',...]"""
112 ['magic_ls','magic_cd',...]"""
112
113
113 # FIXME. This needs a cleanup, in the way the magics list is built.
114 # FIXME. This needs a cleanup, in the way the magics list is built.
114
115
115 # magics in class definition
116 # magics in class definition
116 class_magic = lambda fn: fn.startswith('magic_') and \
117 class_magic = lambda fn: fn.startswith('magic_') and \
117 callable(Magic.__dict__[fn])
118 callable(Magic.__dict__[fn])
118 # in instance namespace (run-time user additions)
119 # in instance namespace (run-time user additions)
119 inst_magic = lambda fn: fn.startswith('magic_') and \
120 inst_magic = lambda fn: fn.startswith('magic_') and \
120 callable(self.__dict__[fn])
121 callable(self.__dict__[fn])
121 # and bound magics by user (so they can access self):
122 # and bound magics by user (so they can access self):
122 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
123 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
123 callable(self.__class__.__dict__[fn])
124 callable(self.__class__.__dict__[fn])
124 magics = filter(class_magic,Magic.__dict__.keys()) + \
125 magics = filter(class_magic,Magic.__dict__.keys()) + \
125 filter(inst_magic,self.__dict__.keys()) + \
126 filter(inst_magic,self.__dict__.keys()) + \
126 filter(inst_bound_magic,self.__class__.__dict__.keys())
127 filter(inst_bound_magic,self.__class__.__dict__.keys())
127 out = []
128 out = []
128 for fn in magics:
129 for fn in magics:
129 out.append(fn.replace('magic_','',1))
130 out.append(fn.replace('magic_','',1))
130 out.sort()
131 out.sort()
131 return out
132 return out
132
133
133 def extract_input_slices(self,slices,raw=False):
134 def extract_input_slices(self,slices,raw=False):
134 """Return as a string a set of input history slices.
135 """Return as a string a set of input history slices.
135
136
136 Inputs:
137 Inputs:
137
138
138 - slices: the set of slices is given as a list of strings (like
139 - slices: the set of slices is given as a list of strings (like
139 ['1','4:8','9'], since this function is for use by magic functions
140 ['1','4:8','9'], since this function is for use by magic functions
140 which get their arguments as strings.
141 which get their arguments as strings.
141
142
142 Optional inputs:
143 Optional inputs:
143
144
144 - raw(False): by default, the processed input is used. If this is
145 - raw(False): by default, the processed input is used. If this is
145 true, the raw input history is used instead.
146 true, the raw input history is used instead.
146
147
147 Note that slices can be called with two notations:
148 Note that slices can be called with two notations:
148
149
149 N:M -> standard python form, means including items N...(M-1).
150 N:M -> standard python form, means including items N...(M-1).
150
151
151 N-M -> include items N..M (closed endpoint)."""
152 N-M -> include items N..M (closed endpoint)."""
152
153
153 if raw:
154 if raw:
154 hist = self.shell.input_hist_raw
155 hist = self.shell.input_hist_raw
155 else:
156 else:
156 hist = self.shell.input_hist
157 hist = self.shell.input_hist
157
158
158 cmds = []
159 cmds = []
159 for chunk in slices:
160 for chunk in slices:
160 if ':' in chunk:
161 if ':' in chunk:
161 ini,fin = map(int,chunk.split(':'))
162 ini,fin = map(int,chunk.split(':'))
162 elif '-' in chunk:
163 elif '-' in chunk:
163 ini,fin = map(int,chunk.split('-'))
164 ini,fin = map(int,chunk.split('-'))
164 fin += 1
165 fin += 1
165 else:
166 else:
166 ini = int(chunk)
167 ini = int(chunk)
167 fin = ini+1
168 fin = ini+1
168 cmds.append(hist[ini:fin])
169 cmds.append(hist[ini:fin])
169 return cmds
170 return cmds
170
171
171 def _ofind(self,oname):
172 def _ofind(self,oname):
172 """Find an object in the available namespaces.
173 """Find an object in the available namespaces.
173
174
174 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
175 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
175
176
176 Has special code to detect magic functions.
177 Has special code to detect magic functions.
177 """
178 """
178
179
179 oname = oname.strip()
180 oname = oname.strip()
180
181
181 # Namespaces to search in:
182 # Namespaces to search in:
182 user_ns = self.shell.user_ns
183 user_ns = self.shell.user_ns
183 internal_ns = self.shell.internal_ns
184 internal_ns = self.shell.internal_ns
184 builtin_ns = __builtin__.__dict__
185 builtin_ns = __builtin__.__dict__
185 alias_ns = self.shell.alias_table
186 alias_ns = self.shell.alias_table
186
187
187 # Put them in a list. The order is important so that we find things in
188 # Put them in a list. The order is important so that we find things in
188 # the same order that Python finds them.
189 # the same order that Python finds them.
189 namespaces = [ ('Interactive',user_ns),
190 namespaces = [ ('Interactive',user_ns),
190 ('IPython internal',internal_ns),
191 ('IPython internal',internal_ns),
191 ('Python builtin',builtin_ns),
192 ('Python builtin',builtin_ns),
192 ('Alias',alias_ns),
193 ('Alias',alias_ns),
193 ]
194 ]
194
195
195 # initialize results to 'null'
196 # initialize results to 'null'
196 found = 0; obj = None; ospace = None; ds = None;
197 found = 0; obj = None; ospace = None; ds = None;
197 ismagic = 0; isalias = 0
198 ismagic = 0; isalias = 0
198
199
199 # Look for the given name by splitting it in parts. If the head is
200 # Look for the given name by splitting it in parts. If the head is
200 # found, then we look for all the remaining parts as members, and only
201 # found, then we look for all the remaining parts as members, and only
201 # declare success if we can find them all.
202 # declare success if we can find them all.
202 oname_parts = oname.split('.')
203 oname_parts = oname.split('.')
203 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
204 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
204 for nsname,ns in namespaces:
205 for nsname,ns in namespaces:
205 try:
206 try:
206 obj = ns[oname_head]
207 obj = ns[oname_head]
207 except KeyError:
208 except KeyError:
208 continue
209 continue
209 else:
210 else:
210 for part in oname_rest:
211 for part in oname_rest:
211 try:
212 try:
212 obj = getattr(obj,part)
213 obj = getattr(obj,part)
213 except:
214 except:
214 # Blanket except b/c some badly implemented objects
215 # Blanket except b/c some badly implemented objects
215 # allow __getattr__ to raise exceptions other than
216 # allow __getattr__ to raise exceptions other than
216 # AttributeError, which then crashes IPython.
217 # AttributeError, which then crashes IPython.
217 break
218 break
218 else:
219 else:
219 # If we finish the for loop (no break), we got all members
220 # If we finish the for loop (no break), we got all members
220 found = 1
221 found = 1
221 ospace = nsname
222 ospace = nsname
222 if ns == alias_ns:
223 if ns == alias_ns:
223 isalias = 1
224 isalias = 1
224 break # namespace loop
225 break # namespace loop
225
226
226 # Try to see if it's magic
227 # Try to see if it's magic
227 if not found:
228 if not found:
228 if oname.startswith(self.shell.ESC_MAGIC):
229 if oname.startswith(self.shell.ESC_MAGIC):
229 oname = oname[1:]
230 oname = oname[1:]
230 obj = getattr(self,'magic_'+oname,None)
231 obj = getattr(self,'magic_'+oname,None)
231 if obj is not None:
232 if obj is not None:
232 found = 1
233 found = 1
233 ospace = 'IPython internal'
234 ospace = 'IPython internal'
234 ismagic = 1
235 ismagic = 1
235
236
236 # Last try: special-case some literals like '', [], {}, etc:
237 # Last try: special-case some literals like '', [], {}, etc:
237 if not found and oname_head in ["''",'""','[]','{}','()']:
238 if not found and oname_head in ["''",'""','[]','{}','()']:
238 obj = eval(oname_head)
239 obj = eval(oname_head)
239 found = 1
240 found = 1
240 ospace = 'Interactive'
241 ospace = 'Interactive'
241
242
242 return {'found':found, 'obj':obj, 'namespace':ospace,
243 return {'found':found, 'obj':obj, 'namespace':ospace,
243 'ismagic':ismagic, 'isalias':isalias}
244 'ismagic':ismagic, 'isalias':isalias}
244
245
245 def arg_err(self,func):
246 def arg_err(self,func):
246 """Print docstring if incorrect arguments were passed"""
247 """Print docstring if incorrect arguments were passed"""
247 print 'Error in arguments:'
248 print 'Error in arguments:'
248 print OInspect.getdoc(func)
249 print OInspect.getdoc(func)
249
250
250 def format_latex(self,strng):
251 def format_latex(self,strng):
251 """Format a string for latex inclusion."""
252 """Format a string for latex inclusion."""
252
253
253 # Characters that need to be escaped for latex:
254 # Characters that need to be escaped for latex:
254 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
255 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
255 # Magic command names as headers:
256 # Magic command names as headers:
256 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
257 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
257 re.MULTILINE)
258 re.MULTILINE)
258 # Magic commands
259 # Magic commands
259 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
260 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
260 re.MULTILINE)
261 re.MULTILINE)
261 # Paragraph continue
262 # Paragraph continue
262 par_re = re.compile(r'\\$',re.MULTILINE)
263 par_re = re.compile(r'\\$',re.MULTILINE)
263
264
264 # The "\n" symbol
265 # The "\n" symbol
265 newline_re = re.compile(r'\\n')
266 newline_re = re.compile(r'\\n')
266
267
267 # Now build the string for output:
268 # Now build the string for output:
268 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
269 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
269 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
270 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
270 strng)
271 strng)
271 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
272 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
272 strng = par_re.sub(r'\\\\',strng)
273 strng = par_re.sub(r'\\\\',strng)
273 strng = escape_re.sub(r'\\\1',strng)
274 strng = escape_re.sub(r'\\\1',strng)
274 strng = newline_re.sub(r'\\textbackslash{}n',strng)
275 strng = newline_re.sub(r'\\textbackslash{}n',strng)
275 return strng
276 return strng
276
277
277 def format_screen(self,strng):
278 def format_screen(self,strng):
278 """Format a string for screen printing.
279 """Format a string for screen printing.
279
280
280 This removes some latex-type format codes."""
281 This removes some latex-type format codes."""
281 # Paragraph continue
282 # Paragraph continue
282 par_re = re.compile(r'\\$',re.MULTILINE)
283 par_re = re.compile(r'\\$',re.MULTILINE)
283 strng = par_re.sub('',strng)
284 strng = par_re.sub('',strng)
284 return strng
285 return strng
285
286
286 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
287 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
287 """Parse options passed to an argument string.
288 """Parse options passed to an argument string.
288
289
289 The interface is similar to that of getopt(), but it returns back a
290 The interface is similar to that of getopt(), but it returns back a
290 Struct with the options as keys and the stripped argument string still
291 Struct with the options as keys and the stripped argument string still
291 as a string.
292 as a string.
292
293
293 arg_str is quoted as a true sys.argv vector by using shlex.split.
294 arg_str is quoted as a true sys.argv vector by using shlex.split.
294 This allows us to easily expand variables, glob files, quote
295 This allows us to easily expand variables, glob files, quote
295 arguments, etc.
296 arguments, etc.
296
297
297 Options:
298 Options:
298 -mode: default 'string'. If given as 'list', the argument string is
299 -mode: default 'string'. If given as 'list', the argument string is
299 returned as a list (split on whitespace) instead of a string.
300 returned as a list (split on whitespace) instead of a string.
300
301
301 -list_all: put all option values in lists. Normally only options
302 -list_all: put all option values in lists. Normally only options
302 appearing more than once are put in a list."""
303 appearing more than once are put in a list."""
303
304
304 # inject default options at the beginning of the input line
305 # inject default options at the beginning of the input line
305 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
306 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
306 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
307 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
307
308
308 mode = kw.get('mode','string')
309 mode = kw.get('mode','string')
309 if mode not in ['string','list']:
310 if mode not in ['string','list']:
310 raise ValueError,'incorrect mode given: %s' % mode
311 raise ValueError,'incorrect mode given: %s' % mode
311 # Get options
312 # Get options
312 list_all = kw.get('list_all',0)
313 list_all = kw.get('list_all',0)
313
314
314 # Check if we have more than one argument to warrant extra processing:
315 # Check if we have more than one argument to warrant extra processing:
315 odict = {} # Dictionary with options
316 odict = {} # Dictionary with options
316 args = arg_str.split()
317 args = arg_str.split()
317 if len(args) >= 1:
318 if len(args) >= 1:
318 # If the list of inputs only has 0 or 1 thing in it, there's no
319 # If the list of inputs only has 0 or 1 thing in it, there's no
319 # need to look for options
320 # need to look for options
320 argv = shlex_split(arg_str)
321 argv = shlex.split(arg_str)
321 # Do regular option processing
322 # Do regular option processing
322 try:
323 try:
323 opts,args = getopt(argv,opt_str,*long_opts)
324 opts,args = getopt(argv,opt_str,*long_opts)
324 except GetoptError,e:
325 except GetoptError,e:
325 raise GetoptError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
326 raise GetoptError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
326 " ".join(long_opts)))
327 " ".join(long_opts)))
327 for o,a in opts:
328 for o,a in opts:
328 if o.startswith('--'):
329 if o.startswith('--'):
329 o = o[2:]
330 o = o[2:]
330 else:
331 else:
331 o = o[1:]
332 o = o[1:]
332 try:
333 try:
333 odict[o].append(a)
334 odict[o].append(a)
334 except AttributeError:
335 except AttributeError:
335 odict[o] = [odict[o],a]
336 odict[o] = [odict[o],a]
336 except KeyError:
337 except KeyError:
337 if list_all:
338 if list_all:
338 odict[o] = [a]
339 odict[o] = [a]
339 else:
340 else:
340 odict[o] = a
341 odict[o] = a
341
342
342 # Prepare opts,args for return
343 # Prepare opts,args for return
343 opts = Struct(odict)
344 opts = Struct(odict)
344 if mode == 'string':
345 if mode == 'string':
345 args = ' '.join(args)
346 args = ' '.join(args)
346
347
347 return opts,args
348 return opts,args
348
349
349 #......................................................................
350 #......................................................................
350 # And now the actual magic functions
351 # And now the actual magic functions
351
352
352 # Functions for IPython shell work (vars,funcs, config, etc)
353 # Functions for IPython shell work (vars,funcs, config, etc)
353 def magic_lsmagic(self, parameter_s = ''):
354 def magic_lsmagic(self, parameter_s = ''):
354 """List currently available magic functions."""
355 """List currently available magic functions."""
355 mesc = self.shell.ESC_MAGIC
356 mesc = self.shell.ESC_MAGIC
356 print 'Available magic functions:\n'+mesc+\
357 print 'Available magic functions:\n'+mesc+\
357 (' '+mesc).join(self.lsmagic())
358 (' '+mesc).join(self.lsmagic())
358 print '\n' + Magic.auto_status[self.shell.rc.automagic]
359 print '\n' + Magic.auto_status[self.shell.rc.automagic]
359 return None
360 return None
360
361
361 def magic_magic(self, parameter_s = ''):
362 def magic_magic(self, parameter_s = ''):
362 """Print information about the magic function system."""
363 """Print information about the magic function system."""
363
364
364 mode = ''
365 mode = ''
365 try:
366 try:
366 if parameter_s.split()[0] == '-latex':
367 if parameter_s.split()[0] == '-latex':
367 mode = 'latex'
368 mode = 'latex'
368 if parameter_s.split()[0] == '-brief':
369 if parameter_s.split()[0] == '-brief':
369 mode = 'brief'
370 mode = 'brief'
370 except:
371 except:
371 pass
372 pass
372
373
373 magic_docs = []
374 magic_docs = []
374 for fname in self.lsmagic():
375 for fname in self.lsmagic():
375 mname = 'magic_' + fname
376 mname = 'magic_' + fname
376 for space in (Magic,self,self.__class__):
377 for space in (Magic,self,self.__class__):
377 try:
378 try:
378 fn = space.__dict__[mname]
379 fn = space.__dict__[mname]
379 except KeyError:
380 except KeyError:
380 pass
381 pass
381 else:
382 else:
382 break
383 break
383 if mode == 'brief':
384 if mode == 'brief':
384 # only first line
385 # only first line
385 fndoc = fn.__doc__.split('\n',1)[0]
386 fndoc = fn.__doc__.split('\n',1)[0]
386 else:
387 else:
387 fndoc = fn.__doc__
388 fndoc = fn.__doc__
388
389
389 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
390 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
390 fname,fndoc))
391 fname,fndoc))
391 magic_docs = ''.join(magic_docs)
392 magic_docs = ''.join(magic_docs)
392
393
393 if mode == 'latex':
394 if mode == 'latex':
394 print self.format_latex(magic_docs)
395 print self.format_latex(magic_docs)
395 return
396 return
396 else:
397 else:
397 magic_docs = self.format_screen(magic_docs)
398 magic_docs = self.format_screen(magic_docs)
398 if mode == 'brief':
399 if mode == 'brief':
399 return magic_docs
400 return magic_docs
400
401
401 outmsg = """
402 outmsg = """
402 IPython's 'magic' functions
403 IPython's 'magic' functions
403 ===========================
404 ===========================
404
405
405 The magic function system provides a series of functions which allow you to
406 The magic function system provides a series of functions which allow you to
406 control the behavior of IPython itself, plus a lot of system-type
407 control the behavior of IPython itself, plus a lot of system-type
407 features. All these functions are prefixed with a % character, but parameters
408 features. All these functions are prefixed with a % character, but parameters
408 are given without parentheses or quotes.
409 are given without parentheses or quotes.
409
410
410 NOTE: If you have 'automagic' enabled (via the command line option or with the
411 NOTE: If you have 'automagic' enabled (via the command line option or with the
411 %automagic function), you don't need to type in the % explicitly. By default,
412 %automagic function), you don't need to type in the % explicitly. By default,
412 IPython ships with automagic on, so you should only rarely need the % escape.
413 IPython ships with automagic on, so you should only rarely need the % escape.
413
414
414 Example: typing '%cd mydir' (without the quotes) changes you working directory
415 Example: typing '%cd mydir' (without the quotes) changes you working directory
415 to 'mydir', if it exists.
416 to 'mydir', if it exists.
416
417
417 You can define your own magic functions to extend the system. See the supplied
418 You can define your own magic functions to extend the system. See the supplied
418 ipythonrc and example-magic.py files for details (in your ipython
419 ipythonrc and example-magic.py files for details (in your ipython
419 configuration directory, typically $HOME/.ipython/).
420 configuration directory, typically $HOME/.ipython/).
420
421
421 You can also define your own aliased names for magic functions. In your
422 You can also define your own aliased names for magic functions. In your
422 ipythonrc file, placing a line like:
423 ipythonrc file, placing a line like:
423
424
424 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
425 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
425
426
426 will define %pf as a new name for %profile.
427 will define %pf as a new name for %profile.
427
428
428 You can also call magics in code using the ipmagic() function, which IPython
429 You can also call magics in code using the ipmagic() function, which IPython
429 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
430 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
430
431
431 For a list of the available magic functions, use %lsmagic. For a description
432 For a list of the available magic functions, use %lsmagic. For a description
432 of any of them, type %magic_name?, e.g. '%cd?'.
433 of any of them, type %magic_name?, e.g. '%cd?'.
433
434
434 Currently the magic system has the following functions:\n"""
435 Currently the magic system has the following functions:\n"""
435
436
436 mesc = self.shell.ESC_MAGIC
437 mesc = self.shell.ESC_MAGIC
437 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
438 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
438 "\n\n%s%s\n\n%s" % (outmsg,
439 "\n\n%s%s\n\n%s" % (outmsg,
439 magic_docs,mesc,mesc,
440 magic_docs,mesc,mesc,
440 (' '+mesc).join(self.lsmagic()),
441 (' '+mesc).join(self.lsmagic()),
441 Magic.auto_status[self.shell.rc.automagic] ) )
442 Magic.auto_status[self.shell.rc.automagic] ) )
442
443
443 page(outmsg,screen_lines=self.shell.rc.screen_length)
444 page(outmsg,screen_lines=self.shell.rc.screen_length)
444
445
445 def magic_automagic(self, parameter_s = ''):
446 def magic_automagic(self, parameter_s = ''):
446 """Make magic functions callable without having to type the initial %.
447 """Make magic functions callable without having to type the initial %.
447
448
448 Toggles on/off (when off, you must call it as %automagic, of
449 Toggles on/off (when off, you must call it as %automagic, of
449 course). Note that magic functions have lowest priority, so if there's
450 course). Note that magic functions have lowest priority, so if there's
450 a variable whose name collides with that of a magic fn, automagic
451 a variable whose name collides with that of a magic fn, automagic
451 won't work for that function (you get the variable instead). However,
452 won't work for that function (you get the variable instead). However,
452 if you delete the variable (del var), the previously shadowed magic
453 if you delete the variable (del var), the previously shadowed magic
453 function becomes visible to automagic again."""
454 function becomes visible to automagic again."""
454
455
455 rc = self.shell.rc
456 rc = self.shell.rc
456 rc.automagic = not rc.automagic
457 rc.automagic = not rc.automagic
457 print '\n' + Magic.auto_status[rc.automagic]
458 print '\n' + Magic.auto_status[rc.automagic]
458
459
459 def magic_autocall(self, parameter_s = ''):
460 def magic_autocall(self, parameter_s = ''):
460 """Make functions callable without having to type parentheses.
461 """Make functions callable without having to type parentheses.
461
462
462 Usage:
463 Usage:
463
464
464 %autocall [mode]
465 %autocall [mode]
465
466
466 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
467 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
467 value is toggled on and off (remembering the previous state)."""
468 value is toggled on and off (remembering the previous state)."""
468
469
469 rc = self.shell.rc
470 rc = self.shell.rc
470
471
471 if parameter_s:
472 if parameter_s:
472 arg = int(parameter_s)
473 arg = int(parameter_s)
473 else:
474 else:
474 arg = 'toggle'
475 arg = 'toggle'
475
476
476 if not arg in (0,1,2,'toggle'):
477 if not arg in (0,1,2,'toggle'):
477 error('Valid modes: (0->Off, 1->Smart, 2->Full')
478 error('Valid modes: (0->Off, 1->Smart, 2->Full')
478 return
479 return
479
480
480 if arg in (0,1,2):
481 if arg in (0,1,2):
481 rc.autocall = arg
482 rc.autocall = arg
482 else: # toggle
483 else: # toggle
483 if rc.autocall:
484 if rc.autocall:
484 self._magic_state.autocall_save = rc.autocall
485 self._magic_state.autocall_save = rc.autocall
485 rc.autocall = 0
486 rc.autocall = 0
486 else:
487 else:
487 try:
488 try:
488 rc.autocall = self._magic_state.autocall_save
489 rc.autocall = self._magic_state.autocall_save
489 except AttributeError:
490 except AttributeError:
490 rc.autocall = self._magic_state.autocall_save = 1
491 rc.autocall = self._magic_state.autocall_save = 1
491
492
492 print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall]
493 print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall]
493
494
494 def magic_autoindent(self, parameter_s = ''):
495 def magic_autoindent(self, parameter_s = ''):
495 """Toggle autoindent on/off (if available)."""
496 """Toggle autoindent on/off (if available)."""
496
497
497 self.shell.set_autoindent()
498 self.shell.set_autoindent()
498 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
499 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
499
500
500 def magic_system_verbose(self, parameter_s = ''):
501 def magic_system_verbose(self, parameter_s = ''):
501 """Toggle verbose printing of system calls on/off."""
502 """Toggle verbose printing of system calls on/off."""
502
503
503 self.shell.rc_set_toggle('system_verbose')
504 self.shell.rc_set_toggle('system_verbose')
504 print "System verbose printing is:",\
505 print "System verbose printing is:",\
505 ['OFF','ON'][self.shell.rc.system_verbose]
506 ['OFF','ON'][self.shell.rc.system_verbose]
506
507
507 def magic_history(self, parameter_s = ''):
508 def magic_history(self, parameter_s = ''):
508 """Print input history (_i<n> variables), with most recent last.
509 """Print input history (_i<n> variables), with most recent last.
509
510
510 %history -> print at most 40 inputs (some may be multi-line)\\
511 %history -> print at most 40 inputs (some may be multi-line)\\
511 %history n -> print at most n inputs\\
512 %history n -> print at most n inputs\\
512 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
513 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
513
514
514 Each input's number <n> is shown, and is accessible as the
515 Each input's number <n> is shown, and is accessible as the
515 automatically generated variable _i<n>. Multi-line statements are
516 automatically generated variable _i<n>. Multi-line statements are
516 printed starting at a new line for easy copy/paste.
517 printed starting at a new line for easy copy/paste.
517
518
518
519
519 Options:
520 Options:
520
521
521 -n: do NOT print line numbers. This is useful if you want to get a
522 -n: do NOT print line numbers. This is useful if you want to get a
522 printout of many lines which can be directly pasted into a text
523 printout of many lines which can be directly pasted into a text
523 editor.
524 editor.
524
525
525 This feature is only available if numbered prompts are in use.
526 This feature is only available if numbered prompts are in use.
526
527
527 -r: print the 'raw' history. IPython filters your input and
528 -r: print the 'raw' history. IPython filters your input and
528 converts it all into valid Python source before executing it (things
529 converts it all into valid Python source before executing it (things
529 like magics or aliases are turned into function calls, for
530 like magics or aliases are turned into function calls, for
530 example). With this option, you'll see the unfiltered history
531 example). With this option, you'll see the unfiltered history
531 instead of the filtered version: '%cd /' will be seen as '%cd /'
532 instead of the filtered version: '%cd /' will be seen as '%cd /'
532 instead of '_ip.magic("%cd /")'.
533 instead of '_ip.magic("%cd /")'.
533 """
534 """
534
535
535 shell = self.shell
536 shell = self.shell
536 if not shell.outputcache.do_full_cache:
537 if not shell.outputcache.do_full_cache:
537 print 'This feature is only available if numbered prompts are in use.'
538 print 'This feature is only available if numbered prompts are in use.'
538 return
539 return
539 opts,args = self.parse_options(parameter_s,'nr',mode='list')
540 opts,args = self.parse_options(parameter_s,'nr',mode='list')
540
541
541 if opts.has_key('r'):
542 if opts.has_key('r'):
542 input_hist = shell.input_hist_raw
543 input_hist = shell.input_hist_raw
543 else:
544 else:
544 input_hist = shell.input_hist
545 input_hist = shell.input_hist
545
546
546 default_length = 40
547 default_length = 40
547 if len(args) == 0:
548 if len(args) == 0:
548 final = len(input_hist)
549 final = len(input_hist)
549 init = max(1,final-default_length)
550 init = max(1,final-default_length)
550 elif len(args) == 1:
551 elif len(args) == 1:
551 final = len(input_hist)
552 final = len(input_hist)
552 init = max(1,final-int(args[0]))
553 init = max(1,final-int(args[0]))
553 elif len(args) == 2:
554 elif len(args) == 2:
554 init,final = map(int,args)
555 init,final = map(int,args)
555 else:
556 else:
556 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
557 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
557 print self.magic_hist.__doc__
558 print self.magic_hist.__doc__
558 return
559 return
559 width = len(str(final))
560 width = len(str(final))
560 line_sep = ['','\n']
561 line_sep = ['','\n']
561 print_nums = not opts.has_key('n')
562 print_nums = not opts.has_key('n')
562 for in_num in range(init,final):
563 for in_num in range(init,final):
563 inline = input_hist[in_num]
564 inline = input_hist[in_num]
564 multiline = int(inline.count('\n') > 1)
565 multiline = int(inline.count('\n') > 1)
565 if print_nums:
566 if print_nums:
566 print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
567 print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
567 print inline,
568 print inline,
568
569
569 def magic_hist(self, parameter_s=''):
570 def magic_hist(self, parameter_s=''):
570 """Alternate name for %history."""
571 """Alternate name for %history."""
571 return self.magic_history(parameter_s)
572 return self.magic_history(parameter_s)
572
573
573 def magic_p(self, parameter_s=''):
574 def magic_p(self, parameter_s=''):
574 """Just a short alias for Python's 'print'."""
575 """Just a short alias for Python's 'print'."""
575 exec 'print ' + parameter_s in self.shell.user_ns
576 exec 'print ' + parameter_s in self.shell.user_ns
576
577
577 def magic_r(self, parameter_s=''):
578 def magic_r(self, parameter_s=''):
578 """Repeat previous input.
579 """Repeat previous input.
579
580
580 If given an argument, repeats the previous command which starts with
581 If given an argument, repeats the previous command which starts with
581 the same string, otherwise it just repeats the previous input.
582 the same string, otherwise it just repeats the previous input.
582
583
583 Shell escaped commands (with ! as first character) are not recognized
584 Shell escaped commands (with ! as first character) are not recognized
584 by this system, only pure python code and magic commands.
585 by this system, only pure python code and magic commands.
585 """
586 """
586
587
587 start = parameter_s.strip()
588 start = parameter_s.strip()
588 esc_magic = self.shell.ESC_MAGIC
589 esc_magic = self.shell.ESC_MAGIC
589 # Identify magic commands even if automagic is on (which means
590 # Identify magic commands even if automagic is on (which means
590 # the in-memory version is different from that typed by the user).
591 # the in-memory version is different from that typed by the user).
591 if self.shell.rc.automagic:
592 if self.shell.rc.automagic:
592 start_magic = esc_magic+start
593 start_magic = esc_magic+start
593 else:
594 else:
594 start_magic = start
595 start_magic = start
595 # Look through the input history in reverse
596 # Look through the input history in reverse
596 for n in range(len(self.shell.input_hist)-2,0,-1):
597 for n in range(len(self.shell.input_hist)-2,0,-1):
597 input = self.shell.input_hist[n]
598 input = self.shell.input_hist[n]
598 # skip plain 'r' lines so we don't recurse to infinity
599 # skip plain 'r' lines so we don't recurse to infinity
599 if input != '_ip.magic("r")\n' and \
600 if input != '_ip.magic("r")\n' and \
600 (input.startswith(start) or input.startswith(start_magic)):
601 (input.startswith(start) or input.startswith(start_magic)):
601 #print 'match',`input` # dbg
602 #print 'match',`input` # dbg
602 print 'Executing:',input,
603 print 'Executing:',input,
603 self.shell.runlines(input)
604 self.shell.runlines(input)
604 return
605 return
605 print 'No previous input matching `%s` found.' % start
606 print 'No previous input matching `%s` found.' % start
606
607
607 def magic_page(self, parameter_s=''):
608 def magic_page(self, parameter_s=''):
608 """Pretty print the object and display it through a pager.
609 """Pretty print the object and display it through a pager.
609
610
610 If no parameter is given, use _ (last output)."""
611 If no parameter is given, use _ (last output)."""
611 # After a function contributed by Olivier Aubert, slightly modified.
612 # After a function contributed by Olivier Aubert, slightly modified.
612
613
613 oname = parameter_s and parameter_s or '_'
614 oname = parameter_s and parameter_s or '_'
614 info = self._ofind(oname)
615 info = self._ofind(oname)
615 if info['found']:
616 if info['found']:
616 page(pformat(info['obj']))
617 page(pformat(info['obj']))
617 else:
618 else:
618 print 'Object `%s` not found' % oname
619 print 'Object `%s` not found' % oname
619
620
620 def magic_profile(self, parameter_s=''):
621 def magic_profile(self, parameter_s=''):
621 """Print your currently active IPyhton profile."""
622 """Print your currently active IPyhton profile."""
622 if self.shell.rc.profile:
623 if self.shell.rc.profile:
623 printpl('Current IPython profile: $self.shell.rc.profile.')
624 printpl('Current IPython profile: $self.shell.rc.profile.')
624 else:
625 else:
625 print 'No profile active.'
626 print 'No profile active.'
626
627
627 def _inspect(self,meth,oname,**kw):
628 def _inspect(self,meth,oname,**kw):
628 """Generic interface to the inspector system.
629 """Generic interface to the inspector system.
629
630
630 This function is meant to be called by pdef, pdoc & friends."""
631 This function is meant to be called by pdef, pdoc & friends."""
631
632
632 oname = oname.strip()
633 oname = oname.strip()
633 info = Struct(self._ofind(oname))
634 info = Struct(self._ofind(oname))
634 if info.found:
635 if info.found:
635 pmethod = getattr(self.shell.inspector,meth)
636 pmethod = getattr(self.shell.inspector,meth)
636 formatter = info.ismagic and self.format_screen or None
637 formatter = info.ismagic and self.format_screen or None
637 if meth == 'pdoc':
638 if meth == 'pdoc':
638 pmethod(info.obj,oname,formatter)
639 pmethod(info.obj,oname,formatter)
639 elif meth == 'pinfo':
640 elif meth == 'pinfo':
640 pmethod(info.obj,oname,formatter,info,**kw)
641 pmethod(info.obj,oname,formatter,info,**kw)
641 else:
642 else:
642 pmethod(info.obj,oname)
643 pmethod(info.obj,oname)
643 else:
644 else:
644 print 'Object `%s` not found.' % oname
645 print 'Object `%s` not found.' % oname
645 return 'not found' # so callers can take other action
646 return 'not found' # so callers can take other action
646
647
647 def magic_pdef(self, parameter_s=''):
648 def magic_pdef(self, parameter_s=''):
648 """Print the definition header for any callable object.
649 """Print the definition header for any callable object.
649
650
650 If the object is a class, print the constructor information."""
651 If the object is a class, print the constructor information."""
651 self._inspect('pdef',parameter_s)
652 self._inspect('pdef',parameter_s)
652
653
653 def magic_pdoc(self, parameter_s=''):
654 def magic_pdoc(self, parameter_s=''):
654 """Print the docstring for an object.
655 """Print the docstring for an object.
655
656
656 If the given object is a class, it will print both the class and the
657 If the given object is a class, it will print both the class and the
657 constructor docstrings."""
658 constructor docstrings."""
658 self._inspect('pdoc',parameter_s)
659 self._inspect('pdoc',parameter_s)
659
660
660 def magic_psource(self, parameter_s=''):
661 def magic_psource(self, parameter_s=''):
661 """Print (or run through pager) the source code for an object."""
662 """Print (or run through pager) the source code for an object."""
662 self._inspect('psource',parameter_s)
663 self._inspect('psource',parameter_s)
663
664
664 def magic_pfile(self, parameter_s=''):
665 def magic_pfile(self, parameter_s=''):
665 """Print (or run through pager) the file where an object is defined.
666 """Print (or run through pager) the file where an object is defined.
666
667
667 The file opens at the line where the object definition begins. IPython
668 The file opens at the line where the object definition begins. IPython
668 will honor the environment variable PAGER if set, and otherwise will
669 will honor the environment variable PAGER if set, and otherwise will
669 do its best to print the file in a convenient form.
670 do its best to print the file in a convenient form.
670
671
671 If the given argument is not an object currently defined, IPython will
672 If the given argument is not an object currently defined, IPython will
672 try to interpret it as a filename (automatically adding a .py extension
673 try to interpret it as a filename (automatically adding a .py extension
673 if needed). You can thus use %pfile as a syntax highlighting code
674 if needed). You can thus use %pfile as a syntax highlighting code
674 viewer."""
675 viewer."""
675
676
676 # first interpret argument as an object name
677 # first interpret argument as an object name
677 out = self._inspect('pfile',parameter_s)
678 out = self._inspect('pfile',parameter_s)
678 # if not, try the input as a filename
679 # if not, try the input as a filename
679 if out == 'not found':
680 if out == 'not found':
680 try:
681 try:
681 filename = get_py_filename(parameter_s)
682 filename = get_py_filename(parameter_s)
682 except IOError,msg:
683 except IOError,msg:
683 print msg
684 print msg
684 return
685 return
685 page(self.shell.inspector.format(file(filename).read()))
686 page(self.shell.inspector.format(file(filename).read()))
686
687
687 def magic_pinfo(self, parameter_s=''):
688 def magic_pinfo(self, parameter_s=''):
688 """Provide detailed information about an object.
689 """Provide detailed information about an object.
689
690
690 '%pinfo object' is just a synonym for object? or ?object."""
691 '%pinfo object' is just a synonym for object? or ?object."""
691
692
692 #print 'pinfo par: <%s>' % parameter_s # dbg
693 #print 'pinfo par: <%s>' % parameter_s # dbg
693
694
694 # detail_level: 0 -> obj? , 1 -> obj??
695 # detail_level: 0 -> obj? , 1 -> obj??
695 detail_level = 0
696 detail_level = 0
696 # We need to detect if we got called as 'pinfo pinfo foo', which can
697 # We need to detect if we got called as 'pinfo pinfo foo', which can
697 # happen if the user types 'pinfo foo?' at the cmd line.
698 # happen if the user types 'pinfo foo?' at the cmd line.
698 pinfo,qmark1,oname,qmark2 = \
699 pinfo,qmark1,oname,qmark2 = \
699 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
700 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
700 if pinfo or qmark1 or qmark2:
701 if pinfo or qmark1 or qmark2:
701 detail_level = 1
702 detail_level = 1
702 if "*" in oname:
703 if "*" in oname:
703 self.magic_psearch(oname)
704 self.magic_psearch(oname)
704 else:
705 else:
705 self._inspect('pinfo',oname,detail_level=detail_level)
706 self._inspect('pinfo',oname,detail_level=detail_level)
706
707
707 def magic_psearch(self, parameter_s=''):
708 def magic_psearch(self, parameter_s=''):
708 """Search for object in namespaces by wildcard.
709 """Search for object in namespaces by wildcard.
709
710
710 %psearch [options] PATTERN [OBJECT TYPE]
711 %psearch [options] PATTERN [OBJECT TYPE]
711
712
712 Note: ? can be used as a synonym for %psearch, at the beginning or at
713 Note: ? can be used as a synonym for %psearch, at the beginning or at
713 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
714 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
714 rest of the command line must be unchanged (options come first), so
715 rest of the command line must be unchanged (options come first), so
715 for example the following forms are equivalent
716 for example the following forms are equivalent
716
717
717 %psearch -i a* function
718 %psearch -i a* function
718 -i a* function?
719 -i a* function?
719 ?-i a* function
720 ?-i a* function
720
721
721 Arguments:
722 Arguments:
722
723
723 PATTERN
724 PATTERN
724
725
725 where PATTERN is a string containing * as a wildcard similar to its
726 where PATTERN is a string containing * as a wildcard similar to its
726 use in a shell. The pattern is matched in all namespaces on the
727 use in a shell. The pattern is matched in all namespaces on the
727 search path. By default objects starting with a single _ are not
728 search path. By default objects starting with a single _ are not
728 matched, many IPython generated objects have a single
729 matched, many IPython generated objects have a single
729 underscore. The default is case insensitive matching. Matching is
730 underscore. The default is case insensitive matching. Matching is
730 also done on the attributes of objects and not only on the objects
731 also done on the attributes of objects and not only on the objects
731 in a module.
732 in a module.
732
733
733 [OBJECT TYPE]
734 [OBJECT TYPE]
734
735
735 Is the name of a python type from the types module. The name is
736 Is the name of a python type from the types module. The name is
736 given in lowercase without the ending type, ex. StringType is
737 given in lowercase without the ending type, ex. StringType is
737 written string. By adding a type here only objects matching the
738 written string. By adding a type here only objects matching the
738 given type are matched. Using all here makes the pattern match all
739 given type are matched. Using all here makes the pattern match all
739 types (this is the default).
740 types (this is the default).
740
741
741 Options:
742 Options:
742
743
743 -a: makes the pattern match even objects whose names start with a
744 -a: makes the pattern match even objects whose names start with a
744 single underscore. These names are normally ommitted from the
745 single underscore. These names are normally ommitted from the
745 search.
746 search.
746
747
747 -i/-c: make the pattern case insensitive/sensitive. If neither of
748 -i/-c: make the pattern case insensitive/sensitive. If neither of
748 these options is given, the default is read from your ipythonrc
749 these options is given, the default is read from your ipythonrc
749 file. The option name which sets this value is
750 file. The option name which sets this value is
750 'wildcards_case_sensitive'. If this option is not specified in your
751 'wildcards_case_sensitive'. If this option is not specified in your
751 ipythonrc file, IPython's internal default is to do a case sensitive
752 ipythonrc file, IPython's internal default is to do a case sensitive
752 search.
753 search.
753
754
754 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
755 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
755 specifiy can be searched in any of the following namespaces:
756 specifiy can be searched in any of the following namespaces:
756 'builtin', 'user', 'user_global','internal', 'alias', where
757 'builtin', 'user', 'user_global','internal', 'alias', where
757 'builtin' and 'user' are the search defaults. Note that you should
758 'builtin' and 'user' are the search defaults. Note that you should
758 not use quotes when specifying namespaces.
759 not use quotes when specifying namespaces.
759
760
760 'Builtin' contains the python module builtin, 'user' contains all
761 'Builtin' contains the python module builtin, 'user' contains all
761 user data, 'alias' only contain the shell aliases and no python
762 user data, 'alias' only contain the shell aliases and no python
762 objects, 'internal' contains objects used by IPython. The
763 objects, 'internal' contains objects used by IPython. The
763 'user_global' namespace is only used by embedded IPython instances,
764 'user_global' namespace is only used by embedded IPython instances,
764 and it contains module-level globals. You can add namespaces to the
765 and it contains module-level globals. You can add namespaces to the
765 search with -s or exclude them with -e (these options can be given
766 search with -s or exclude them with -e (these options can be given
766 more than once).
767 more than once).
767
768
768 Examples:
769 Examples:
769
770
770 %psearch a* -> objects beginning with an a
771 %psearch a* -> objects beginning with an a
771 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
772 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
772 %psearch a* function -> all functions beginning with an a
773 %psearch a* function -> all functions beginning with an a
773 %psearch re.e* -> objects beginning with an e in module re
774 %psearch re.e* -> objects beginning with an e in module re
774 %psearch r*.e* -> objects that start with e in modules starting in r
775 %psearch r*.e* -> objects that start with e in modules starting in r
775 %psearch r*.* string -> all strings in modules beginning with r
776 %psearch r*.* string -> all strings in modules beginning with r
776
777
777 Case sensitve search:
778 Case sensitve search:
778
779
779 %psearch -c a* list all object beginning with lower case a
780 %psearch -c a* list all object beginning with lower case a
780
781
781 Show objects beginning with a single _:
782 Show objects beginning with a single _:
782
783
783 %psearch -a _* list objects beginning with a single underscore"""
784 %psearch -a _* list objects beginning with a single underscore"""
784
785
785 # default namespaces to be searched
786 # default namespaces to be searched
786 def_search = ['user','builtin']
787 def_search = ['user','builtin']
787
788
788 # Process options/args
789 # Process options/args
789 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
790 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
790 opt = opts.get
791 opt = opts.get
791 shell = self.shell
792 shell = self.shell
792 psearch = shell.inspector.psearch
793 psearch = shell.inspector.psearch
793
794
794 # select case options
795 # select case options
795 if opts.has_key('i'):
796 if opts.has_key('i'):
796 ignore_case = True
797 ignore_case = True
797 elif opts.has_key('c'):
798 elif opts.has_key('c'):
798 ignore_case = False
799 ignore_case = False
799 else:
800 else:
800 ignore_case = not shell.rc.wildcards_case_sensitive
801 ignore_case = not shell.rc.wildcards_case_sensitive
801
802
802 # Build list of namespaces to search from user options
803 # Build list of namespaces to search from user options
803 def_search.extend(opt('s',[]))
804 def_search.extend(opt('s',[]))
804 ns_exclude = ns_exclude=opt('e',[])
805 ns_exclude = ns_exclude=opt('e',[])
805 ns_search = [nm for nm in def_search if nm not in ns_exclude]
806 ns_search = [nm for nm in def_search if nm not in ns_exclude]
806
807
807 # Call the actual search
808 # Call the actual search
808 try:
809 try:
809 psearch(args,shell.ns_table,ns_search,
810 psearch(args,shell.ns_table,ns_search,
810 show_all=opt('a'),ignore_case=ignore_case)
811 show_all=opt('a'),ignore_case=ignore_case)
811 except:
812 except:
812 shell.showtraceback()
813 shell.showtraceback()
813
814
814 def magic_who_ls(self, parameter_s=''):
815 def magic_who_ls(self, parameter_s=''):
815 """Return a sorted list of all interactive variables.
816 """Return a sorted list of all interactive variables.
816
817
817 If arguments are given, only variables of types matching these
818 If arguments are given, only variables of types matching these
818 arguments are returned."""
819 arguments are returned."""
819
820
820 user_ns = self.shell.user_ns
821 user_ns = self.shell.user_ns
821 internal_ns = self.shell.internal_ns
822 internal_ns = self.shell.internal_ns
822 user_config_ns = self.shell.user_config_ns
823 user_config_ns = self.shell.user_config_ns
823 out = []
824 out = []
824 typelist = parameter_s.split()
825 typelist = parameter_s.split()
825
826
826 for i in user_ns:
827 for i in user_ns:
827 if not (i.startswith('_') or i.startswith('_i')) \
828 if not (i.startswith('_') or i.startswith('_i')) \
828 and not (i in internal_ns or i in user_config_ns):
829 and not (i in internal_ns or i in user_config_ns):
829 if typelist:
830 if typelist:
830 if type(user_ns[i]).__name__ in typelist:
831 if type(user_ns[i]).__name__ in typelist:
831 out.append(i)
832 out.append(i)
832 else:
833 else:
833 out.append(i)
834 out.append(i)
834 out.sort()
835 out.sort()
835 return out
836 return out
836
837
837 def magic_who(self, parameter_s=''):
838 def magic_who(self, parameter_s=''):
838 """Print all interactive variables, with some minimal formatting.
839 """Print all interactive variables, with some minimal formatting.
839
840
840 If any arguments are given, only variables whose type matches one of
841 If any arguments are given, only variables whose type matches one of
841 these are printed. For example:
842 these are printed. For example:
842
843
843 %who function str
844 %who function str
844
845
845 will only list functions and strings, excluding all other types of
846 will only list functions and strings, excluding all other types of
846 variables. To find the proper type names, simply use type(var) at a
847 variables. To find the proper type names, simply use type(var) at a
847 command line to see how python prints type names. For example:
848 command line to see how python prints type names. For example:
848
849
849 In [1]: type('hello')\\
850 In [1]: type('hello')\\
850 Out[1]: <type 'str'>
851 Out[1]: <type 'str'>
851
852
852 indicates that the type name for strings is 'str'.
853 indicates that the type name for strings is 'str'.
853
854
854 %who always excludes executed names loaded through your configuration
855 %who always excludes executed names loaded through your configuration
855 file and things which are internal to IPython.
856 file and things which are internal to IPython.
856
857
857 This is deliberate, as typically you may load many modules and the
858 This is deliberate, as typically you may load many modules and the
858 purpose of %who is to show you only what you've manually defined."""
859 purpose of %who is to show you only what you've manually defined."""
859
860
860 varlist = self.magic_who_ls(parameter_s)
861 varlist = self.magic_who_ls(parameter_s)
861 if not varlist:
862 if not varlist:
862 print 'Interactive namespace is empty.'
863 print 'Interactive namespace is empty.'
863 return
864 return
864
865
865 # if we have variables, move on...
866 # if we have variables, move on...
866
867
867 # stupid flushing problem: when prompts have no separators, stdout is
868 # stupid flushing problem: when prompts have no separators, stdout is
868 # getting lost. I'm starting to think this is a python bug. I'm having
869 # getting lost. I'm starting to think this is a python bug. I'm having
869 # to force a flush with a print because even a sys.stdout.flush
870 # to force a flush with a print because even a sys.stdout.flush
870 # doesn't seem to do anything!
871 # doesn't seem to do anything!
871
872
872 count = 0
873 count = 0
873 for i in varlist:
874 for i in varlist:
874 print i+'\t',
875 print i+'\t',
875 count += 1
876 count += 1
876 if count > 8:
877 if count > 8:
877 count = 0
878 count = 0
878 print
879 print
879 sys.stdout.flush() # FIXME. Why the hell isn't this flushing???
880 sys.stdout.flush() # FIXME. Why the hell isn't this flushing???
880
881
881 print # well, this does force a flush at the expense of an extra \n
882 print # well, this does force a flush at the expense of an extra \n
882
883
883 def magic_whos(self, parameter_s=''):
884 def magic_whos(self, parameter_s=''):
884 """Like %who, but gives some extra information about each variable.
885 """Like %who, but gives some extra information about each variable.
885
886
886 The same type filtering of %who can be applied here.
887 The same type filtering of %who can be applied here.
887
888
888 For all variables, the type is printed. Additionally it prints:
889 For all variables, the type is printed. Additionally it prints:
889
890
890 - For {},[],(): their length.
891 - For {},[],(): their length.
891
892
892 - For Numeric arrays, a summary with shape, number of elements,
893 - For Numeric arrays, a summary with shape, number of elements,
893 typecode and size in memory.
894 typecode and size in memory.
894
895
895 - Everything else: a string representation, snipping their middle if
896 - Everything else: a string representation, snipping their middle if
896 too long."""
897 too long."""
897
898
898 varnames = self.magic_who_ls(parameter_s)
899 varnames = self.magic_who_ls(parameter_s)
899 if not varnames:
900 if not varnames:
900 print 'Interactive namespace is empty.'
901 print 'Interactive namespace is empty.'
901 return
902 return
902
903
903 # if we have variables, move on...
904 # if we have variables, move on...
904
905
905 # for these types, show len() instead of data:
906 # for these types, show len() instead of data:
906 seq_types = [types.DictType,types.ListType,types.TupleType]
907 seq_types = [types.DictType,types.ListType,types.TupleType]
907
908
908 # for Numeric arrays, display summary info
909 # for Numeric arrays, display summary info
909 try:
910 try:
910 import Numeric
911 import Numeric
911 except ImportError:
912 except ImportError:
912 array_type = None
913 array_type = None
913 else:
914 else:
914 array_type = Numeric.ArrayType.__name__
915 array_type = Numeric.ArrayType.__name__
915
916
916 # Find all variable names and types so we can figure out column sizes
917 # Find all variable names and types so we can figure out column sizes
917 get_vars = lambda i: self.shell.user_ns[i]
918 get_vars = lambda i: self.shell.user_ns[i]
918 type_name = lambda v: type(v).__name__
919 type_name = lambda v: type(v).__name__
919 varlist = map(get_vars,varnames)
920 varlist = map(get_vars,varnames)
920
921
921 typelist = []
922 typelist = []
922 for vv in varlist:
923 for vv in varlist:
923 tt = type_name(vv)
924 tt = type_name(vv)
924 if tt=='instance':
925 if tt=='instance':
925 typelist.append(str(vv.__class__))
926 typelist.append(str(vv.__class__))
926 else:
927 else:
927 typelist.append(tt)
928 typelist.append(tt)
928
929
929 # column labels and # of spaces as separator
930 # column labels and # of spaces as separator
930 varlabel = 'Variable'
931 varlabel = 'Variable'
931 typelabel = 'Type'
932 typelabel = 'Type'
932 datalabel = 'Data/Info'
933 datalabel = 'Data/Info'
933 colsep = 3
934 colsep = 3
934 # variable format strings
935 # variable format strings
935 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
936 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
936 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
937 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
937 aformat = "%s: %s elems, type `%s`, %s bytes"
938 aformat = "%s: %s elems, type `%s`, %s bytes"
938 # find the size of the columns to format the output nicely
939 # find the size of the columns to format the output nicely
939 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
940 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
940 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
941 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
941 # table header
942 # table header
942 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
943 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
943 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
944 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
944 # and the table itself
945 # and the table itself
945 kb = 1024
946 kb = 1024
946 Mb = 1048576 # kb**2
947 Mb = 1048576 # kb**2
947 for vname,var,vtype in zip(varnames,varlist,typelist):
948 for vname,var,vtype in zip(varnames,varlist,typelist):
948 print itpl(vformat),
949 print itpl(vformat),
949 if vtype in seq_types:
950 if vtype in seq_types:
950 print len(var)
951 print len(var)
951 elif vtype==array_type:
952 elif vtype==array_type:
952 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
953 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
953 vsize = Numeric.size(var)
954 vsize = Numeric.size(var)
954 vbytes = vsize*var.itemsize()
955 vbytes = vsize*var.itemsize()
955 if vbytes < 100000:
956 if vbytes < 100000:
956 print aformat % (vshape,vsize,var.typecode(),vbytes)
957 print aformat % (vshape,vsize,var.typecode(),vbytes)
957 else:
958 else:
958 print aformat % (vshape,vsize,var.typecode(),vbytes),
959 print aformat % (vshape,vsize,var.typecode(),vbytes),
959 if vbytes < Mb:
960 if vbytes < Mb:
960 print '(%s kb)' % (vbytes/kb,)
961 print '(%s kb)' % (vbytes/kb,)
961 else:
962 else:
962 print '(%s Mb)' % (vbytes/Mb,)
963 print '(%s Mb)' % (vbytes/Mb,)
963 else:
964 else:
964 vstr = str(var).replace('\n','\\n')
965 vstr = str(var).replace('\n','\\n')
965 if len(vstr) < 50:
966 if len(vstr) < 50:
966 print vstr
967 print vstr
967 else:
968 else:
968 printpl(vfmt_short)
969 printpl(vfmt_short)
969
970
970 def magic_reset(self, parameter_s=''):
971 def magic_reset(self, parameter_s=''):
971 """Resets the namespace by removing all names defined by the user.
972 """Resets the namespace by removing all names defined by the user.
972
973
973 Input/Output history are left around in case you need them."""
974 Input/Output history are left around in case you need them."""
974
975
975 ans = raw_input(
976 ans = raw_input(
976 "Once deleted, variables cannot be recovered. Proceed (y/n)? ")
977 "Once deleted, variables cannot be recovered. Proceed (y/n)? ")
977 if not ans.lower() == 'y':
978 if not ans.lower() == 'y':
978 print 'Nothing done.'
979 print 'Nothing done.'
979 return
980 return
980 user_ns = self.shell.user_ns
981 user_ns = self.shell.user_ns
981 for i in self.magic_who_ls():
982 for i in self.magic_who_ls():
982 del(user_ns[i])
983 del(user_ns[i])
983
984
984 def magic_config(self,parameter_s=''):
985 def magic_config(self,parameter_s=''):
985 """Show IPython's internal configuration."""
986 """Show IPython's internal configuration."""
986
987
987 page('Current configuration structure:\n'+
988 page('Current configuration structure:\n'+
988 pformat(self.shell.rc.dict()))
989 pformat(self.shell.rc.dict()))
989
990
990 def magic_logstart(self,parameter_s=''):
991 def magic_logstart(self,parameter_s=''):
991 """Start logging anywhere in a session.
992 """Start logging anywhere in a session.
992
993
993 %logstart [-o|-t] [log_name [log_mode]]
994 %logstart [-o|-t] [log_name [log_mode]]
994
995
995 If no name is given, it defaults to a file named 'ipython_log.py' in your
996 If no name is given, it defaults to a file named 'ipython_log.py' in your
996 current directory, in 'rotate' mode (see below).
997 current directory, in 'rotate' mode (see below).
997
998
998 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
999 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
999 history up to that point and then continues logging.
1000 history up to that point and then continues logging.
1000
1001
1001 %logstart takes a second optional parameter: logging mode. This can be one
1002 %logstart takes a second optional parameter: logging mode. This can be one
1002 of (note that the modes are given unquoted):\\
1003 of (note that the modes are given unquoted):\\
1003 append: well, that says it.\\
1004 append: well, that says it.\\
1004 backup: rename (if exists) to name~ and start name.\\
1005 backup: rename (if exists) to name~ and start name.\\
1005 global: single logfile in your home dir, appended to.\\
1006 global: single logfile in your home dir, appended to.\\
1006 over : overwrite existing log.\\
1007 over : overwrite existing log.\\
1007 rotate: create rotating logs name.1~, name.2~, etc.
1008 rotate: create rotating logs name.1~, name.2~, etc.
1008
1009
1009 Options:
1010 Options:
1010
1011
1011 -o: log also IPython's output. In this mode, all commands which
1012 -o: log also IPython's output. In this mode, all commands which
1012 generate an Out[NN] prompt are recorded to the logfile, right after
1013 generate an Out[NN] prompt are recorded to the logfile, right after
1013 their corresponding input line. The output lines are always
1014 their corresponding input line. The output lines are always
1014 prepended with a '#[Out]# ' marker, so that the log remains valid
1015 prepended with a '#[Out]# ' marker, so that the log remains valid
1015 Python code.
1016 Python code.
1016
1017
1017 Since this marker is always the same, filtering only the output from
1018 Since this marker is always the same, filtering only the output from
1018 a log is very easy, using for example a simple awk call:
1019 a log is very easy, using for example a simple awk call:
1019
1020
1020 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1021 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1021
1022
1022 -t: put timestamps before each input line logged (these are put in
1023 -t: put timestamps before each input line logged (these are put in
1023 comments)."""
1024 comments)."""
1024
1025
1025 opts,par = self.parse_options(parameter_s,'ot')
1026 opts,par = self.parse_options(parameter_s,'ot')
1026 log_output = 'o' in opts
1027 log_output = 'o' in opts
1027 timestamp = 't' in opts
1028 timestamp = 't' in opts
1028
1029
1029 rc = self.shell.rc
1030 rc = self.shell.rc
1030 logger = self.shell.logger
1031 logger = self.shell.logger
1031
1032
1032 # if no args are given, the defaults set in the logger constructor by
1033 # if no args are given, the defaults set in the logger constructor by
1033 # ipytohn remain valid
1034 # ipytohn remain valid
1034 if par:
1035 if par:
1035 try:
1036 try:
1036 logfname,logmode = par.split()
1037 logfname,logmode = par.split()
1037 except:
1038 except:
1038 logfname = par
1039 logfname = par
1039 logmode = 'backup'
1040 logmode = 'backup'
1040 else:
1041 else:
1041 logfname = logger.logfname
1042 logfname = logger.logfname
1042 logmode = logger.logmode
1043 logmode = logger.logmode
1043 # put logfname into rc struct as if it had been called on the command
1044 # put logfname into rc struct as if it had been called on the command
1044 # line, so it ends up saved in the log header Save it in case we need
1045 # line, so it ends up saved in the log header Save it in case we need
1045 # to restore it...
1046 # to restore it...
1046 old_logfile = rc.opts.get('logfile','')
1047 old_logfile = rc.opts.get('logfile','')
1047 if logfname:
1048 if logfname:
1048 logfname = os.path.expanduser(logfname)
1049 logfname = os.path.expanduser(logfname)
1049 rc.opts.logfile = logfname
1050 rc.opts.logfile = logfname
1050 loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
1051 loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
1051 try:
1052 try:
1052 started = logger.logstart(logfname,loghead,logmode,
1053 started = logger.logstart(logfname,loghead,logmode,
1053 log_output,timestamp)
1054 log_output,timestamp)
1054 except:
1055 except:
1055 rc.opts.logfile = old_logfile
1056 rc.opts.logfile = old_logfile
1056 warn("Couldn't start log: %s" % sys.exc_info()[1])
1057 warn("Couldn't start log: %s" % sys.exc_info()[1])
1057 else:
1058 else:
1058 # log input history up to this point, optionally interleaving
1059 # log input history up to this point, optionally interleaving
1059 # output if requested
1060 # output if requested
1060
1061
1061 if timestamp:
1062 if timestamp:
1062 # disable timestamping for the previous history, since we've
1063 # disable timestamping for the previous history, since we've
1063 # lost those already (no time machine here).
1064 # lost those already (no time machine here).
1064 logger.timestamp = False
1065 logger.timestamp = False
1065 if log_output:
1066 if log_output:
1066 log_write = logger.log_write
1067 log_write = logger.log_write
1067 input_hist = self.shell.input_hist
1068 input_hist = self.shell.input_hist
1068 output_hist = self.shell.output_hist
1069 output_hist = self.shell.output_hist
1069 for n in range(1,len(input_hist)-1):
1070 for n in range(1,len(input_hist)-1):
1070 log_write(input_hist[n].rstrip())
1071 log_write(input_hist[n].rstrip())
1071 if n in output_hist:
1072 if n in output_hist:
1072 log_write(repr(output_hist[n]),'output')
1073 log_write(repr(output_hist[n]),'output')
1073 else:
1074 else:
1074 logger.log_write(self.shell.input_hist[1:])
1075 logger.log_write(self.shell.input_hist[1:])
1075 if timestamp:
1076 if timestamp:
1076 # re-enable timestamping
1077 # re-enable timestamping
1077 logger.timestamp = True
1078 logger.timestamp = True
1078
1079
1079 print ('Activating auto-logging. '
1080 print ('Activating auto-logging. '
1080 'Current session state plus future input saved.')
1081 'Current session state plus future input saved.')
1081 logger.logstate()
1082 logger.logstate()
1082
1083
1083 def magic_logoff(self,parameter_s=''):
1084 def magic_logoff(self,parameter_s=''):
1084 """Temporarily stop logging.
1085 """Temporarily stop logging.
1085
1086
1086 You must have previously started logging."""
1087 You must have previously started logging."""
1087 self.shell.logger.switch_log(0)
1088 self.shell.logger.switch_log(0)
1088
1089
1089 def magic_logon(self,parameter_s=''):
1090 def magic_logon(self,parameter_s=''):
1090 """Restart logging.
1091 """Restart logging.
1091
1092
1092 This function is for restarting logging which you've temporarily
1093 This function is for restarting logging which you've temporarily
1093 stopped with %logoff. For starting logging for the first time, you
1094 stopped with %logoff. For starting logging for the first time, you
1094 must use the %logstart function, which allows you to specify an
1095 must use the %logstart function, which allows you to specify an
1095 optional log filename."""
1096 optional log filename."""
1096
1097
1097 self.shell.logger.switch_log(1)
1098 self.shell.logger.switch_log(1)
1098
1099
1099 def magic_logstate(self,parameter_s=''):
1100 def magic_logstate(self,parameter_s=''):
1100 """Print the status of the logging system."""
1101 """Print the status of the logging system."""
1101
1102
1102 self.shell.logger.logstate()
1103 self.shell.logger.logstate()
1103
1104
1104 def magic_pdb(self, parameter_s=''):
1105 def magic_pdb(self, parameter_s=''):
1105 """Control the calling of the pdb interactive debugger.
1106 """Control the calling of the pdb interactive debugger.
1106
1107
1107 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1108 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1108 argument it works as a toggle.
1109 argument it works as a toggle.
1109
1110
1110 When an exception is triggered, IPython can optionally call the
1111 When an exception is triggered, IPython can optionally call the
1111 interactive pdb debugger after the traceback printout. %pdb toggles
1112 interactive pdb debugger after the traceback printout. %pdb toggles
1112 this feature on and off."""
1113 this feature on and off."""
1113
1114
1114 par = parameter_s.strip().lower()
1115 par = parameter_s.strip().lower()
1115
1116
1116 if par:
1117 if par:
1117 try:
1118 try:
1118 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1119 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1119 except KeyError:
1120 except KeyError:
1120 print ('Incorrect argument. Use on/1, off/0, '
1121 print ('Incorrect argument. Use on/1, off/0, '
1121 'or nothing for a toggle.')
1122 'or nothing for a toggle.')
1122 return
1123 return
1123 else:
1124 else:
1124 # toggle
1125 # toggle
1125 new_pdb = not self.shell.InteractiveTB.call_pdb
1126 new_pdb = not self.shell.InteractiveTB.call_pdb
1126
1127
1127 # set on the shell
1128 # set on the shell
1128 self.shell.call_pdb = new_pdb
1129 self.shell.call_pdb = new_pdb
1129 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1130 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1130
1131
1131 def magic_prun(self, parameter_s ='',user_mode=1,
1132 def magic_prun(self, parameter_s ='',user_mode=1,
1132 opts=None,arg_lst=None,prog_ns=None):
1133 opts=None,arg_lst=None,prog_ns=None):
1133
1134
1134 """Run a statement through the python code profiler.
1135 """Run a statement through the python code profiler.
1135
1136
1136 Usage:\\
1137 Usage:\\
1137 %prun [options] statement
1138 %prun [options] statement
1138
1139
1139 The given statement (which doesn't require quote marks) is run via the
1140 The given statement (which doesn't require quote marks) is run via the
1140 python profiler in a manner similar to the profile.run() function.
1141 python profiler in a manner similar to the profile.run() function.
1141 Namespaces are internally managed to work correctly; profile.run
1142 Namespaces are internally managed to work correctly; profile.run
1142 cannot be used in IPython because it makes certain assumptions about
1143 cannot be used in IPython because it makes certain assumptions about
1143 namespaces which do not hold under IPython.
1144 namespaces which do not hold under IPython.
1144
1145
1145 Options:
1146 Options:
1146
1147
1147 -l <limit>: you can place restrictions on what or how much of the
1148 -l <limit>: you can place restrictions on what or how much of the
1148 profile gets printed. The limit value can be:
1149 profile gets printed. The limit value can be:
1149
1150
1150 * A string: only information for function names containing this string
1151 * A string: only information for function names containing this string
1151 is printed.
1152 is printed.
1152
1153
1153 * An integer: only these many lines are printed.
1154 * An integer: only these many lines are printed.
1154
1155
1155 * A float (between 0 and 1): this fraction of the report is printed
1156 * A float (between 0 and 1): this fraction of the report is printed
1156 (for example, use a limit of 0.4 to see the topmost 40% only).
1157 (for example, use a limit of 0.4 to see the topmost 40% only).
1157
1158
1158 You can combine several limits with repeated use of the option. For
1159 You can combine several limits with repeated use of the option. For
1159 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1160 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1160 information about class constructors.
1161 information about class constructors.
1161
1162
1162 -r: return the pstats.Stats object generated by the profiling. This
1163 -r: return the pstats.Stats object generated by the profiling. This
1163 object has all the information about the profile in it, and you can
1164 object has all the information about the profile in it, and you can
1164 later use it for further analysis or in other functions.
1165 later use it for further analysis or in other functions.
1165
1166
1166 Since magic functions have a particular form of calling which prevents
1167 Since magic functions have a particular form of calling which prevents
1167 you from writing something like:\\
1168 you from writing something like:\\
1168 In [1]: p = %prun -r print 4 # invalid!\\
1169 In [1]: p = %prun -r print 4 # invalid!\\
1169 you must instead use IPython's automatic variables to assign this:\\
1170 you must instead use IPython's automatic variables to assign this:\\
1170 In [1]: %prun -r print 4 \\
1171 In [1]: %prun -r print 4 \\
1171 Out[1]: <pstats.Stats instance at 0x8222cec>\\
1172 Out[1]: <pstats.Stats instance at 0x8222cec>\\
1172 In [2]: stats = _
1173 In [2]: stats = _
1173
1174
1174 If you really need to assign this value via an explicit function call,
1175 If you really need to assign this value via an explicit function call,
1175 you can always tap directly into the true name of the magic function
1176 you can always tap directly into the true name of the magic function
1176 by using the _ip.magic function:\\
1177 by using the _ip.magic function:\\
1177 In [3]: stats = _ip.magic('prun','-r print 4')
1178 In [3]: stats = _ip.magic('prun','-r print 4')
1178
1179
1179 You can type _ip.magic? for more details.
1180 You can type _ip.magic? for more details.
1180
1181
1181 -s <key>: sort profile by given key. You can provide more than one key
1182 -s <key>: sort profile by given key. You can provide more than one key
1182 by using the option several times: '-s key1 -s key2 -s key3...'. The
1183 by using the option several times: '-s key1 -s key2 -s key3...'. The
1183 default sorting key is 'time'.
1184 default sorting key is 'time'.
1184
1185
1185 The following is copied verbatim from the profile documentation
1186 The following is copied verbatim from the profile documentation
1186 referenced below:
1187 referenced below:
1187
1188
1188 When more than one key is provided, additional keys are used as
1189 When more than one key is provided, additional keys are used as
1189 secondary criteria when the there is equality in all keys selected
1190 secondary criteria when the there is equality in all keys selected
1190 before them.
1191 before them.
1191
1192
1192 Abbreviations can be used for any key names, as long as the
1193 Abbreviations can be used for any key names, as long as the
1193 abbreviation is unambiguous. The following are the keys currently
1194 abbreviation is unambiguous. The following are the keys currently
1194 defined:
1195 defined:
1195
1196
1196 Valid Arg Meaning\\
1197 Valid Arg Meaning\\
1197 "calls" call count\\
1198 "calls" call count\\
1198 "cumulative" cumulative time\\
1199 "cumulative" cumulative time\\
1199 "file" file name\\
1200 "file" file name\\
1200 "module" file name\\
1201 "module" file name\\
1201 "pcalls" primitive call count\\
1202 "pcalls" primitive call count\\
1202 "line" line number\\
1203 "line" line number\\
1203 "name" function name\\
1204 "name" function name\\
1204 "nfl" name/file/line\\
1205 "nfl" name/file/line\\
1205 "stdname" standard name\\
1206 "stdname" standard name\\
1206 "time" internal time
1207 "time" internal time
1207
1208
1208 Note that all sorts on statistics are in descending order (placing
1209 Note that all sorts on statistics are in descending order (placing
1209 most time consuming items first), where as name, file, and line number
1210 most time consuming items first), where as name, file, and line number
1210 searches are in ascending order (i.e., alphabetical). The subtle
1211 searches are in ascending order (i.e., alphabetical). The subtle
1211 distinction between "nfl" and "stdname" is that the standard name is a
1212 distinction between "nfl" and "stdname" is that the standard name is a
1212 sort of the name as printed, which means that the embedded line
1213 sort of the name as printed, which means that the embedded line
1213 numbers get compared in an odd way. For example, lines 3, 20, and 40
1214 numbers get compared in an odd way. For example, lines 3, 20, and 40
1214 would (if the file names were the same) appear in the string order
1215 would (if the file names were the same) appear in the string order
1215 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1216 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1216 line numbers. In fact, sort_stats("nfl") is the same as
1217 line numbers. In fact, sort_stats("nfl") is the same as
1217 sort_stats("name", "file", "line").
1218 sort_stats("name", "file", "line").
1218
1219
1219 -T <filename>: save profile results as shown on screen to a text
1220 -T <filename>: save profile results as shown on screen to a text
1220 file. The profile is still shown on screen.
1221 file. The profile is still shown on screen.
1221
1222
1222 -D <filename>: save (via dump_stats) profile statistics to given
1223 -D <filename>: save (via dump_stats) profile statistics to given
1223 filename. This data is in a format understod by the pstats module, and
1224 filename. This data is in a format understod by the pstats module, and
1224 is generated by a call to the dump_stats() method of profile
1225 is generated by a call to the dump_stats() method of profile
1225 objects. The profile is still shown on screen.
1226 objects. The profile is still shown on screen.
1226
1227
1227 If you want to run complete programs under the profiler's control, use
1228 If you want to run complete programs under the profiler's control, use
1228 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1229 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1229 contains profiler specific options as described here.
1230 contains profiler specific options as described here.
1230
1231
1231 You can read the complete documentation for the profile module with:\\
1232 You can read the complete documentation for the profile module with:\\
1232 In [1]: import profile; profile.help() """
1233 In [1]: import profile; profile.help() """
1233
1234
1234 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1235 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1235 # protect user quote marks
1236 # protect user quote marks
1236 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1237 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1237
1238
1238 if user_mode: # regular user call
1239 if user_mode: # regular user call
1239 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1240 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1240 list_all=1)
1241 list_all=1)
1241 namespace = self.shell.user_ns
1242 namespace = self.shell.user_ns
1242 else: # called to run a program by %run -p
1243 else: # called to run a program by %run -p
1243 try:
1244 try:
1244 filename = get_py_filename(arg_lst[0])
1245 filename = get_py_filename(arg_lst[0])
1245 except IOError,msg:
1246 except IOError,msg:
1246 error(msg)
1247 error(msg)
1247 return
1248 return
1248
1249
1249 arg_str = 'execfile(filename,prog_ns)'
1250 arg_str = 'execfile(filename,prog_ns)'
1250 namespace = locals()
1251 namespace = locals()
1251
1252
1252 opts.merge(opts_def)
1253 opts.merge(opts_def)
1253
1254
1254 prof = profile.Profile()
1255 prof = profile.Profile()
1255 try:
1256 try:
1256 prof = prof.runctx(arg_str,namespace,namespace)
1257 prof = prof.runctx(arg_str,namespace,namespace)
1257 sys_exit = ''
1258 sys_exit = ''
1258 except SystemExit:
1259 except SystemExit:
1259 sys_exit = """*** SystemExit exception caught in code being profiled."""
1260 sys_exit = """*** SystemExit exception caught in code being profiled."""
1260
1261
1261 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1262 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1262
1263
1263 lims = opts.l
1264 lims = opts.l
1264 if lims:
1265 if lims:
1265 lims = [] # rebuild lims with ints/floats/strings
1266 lims = [] # rebuild lims with ints/floats/strings
1266 for lim in opts.l:
1267 for lim in opts.l:
1267 try:
1268 try:
1268 lims.append(int(lim))
1269 lims.append(int(lim))
1269 except ValueError:
1270 except ValueError:
1270 try:
1271 try:
1271 lims.append(float(lim))
1272 lims.append(float(lim))
1272 except ValueError:
1273 except ValueError:
1273 lims.append(lim)
1274 lims.append(lim)
1274
1275
1275 # trap output
1276 # trap output
1276 sys_stdout = sys.stdout
1277 sys_stdout = sys.stdout
1277 stdout_trap = StringIO()
1278 stdout_trap = StringIO()
1278 try:
1279 try:
1279 sys.stdout = stdout_trap
1280 sys.stdout = stdout_trap
1280 stats.print_stats(*lims)
1281 stats.print_stats(*lims)
1281 finally:
1282 finally:
1282 sys.stdout = sys_stdout
1283 sys.stdout = sys_stdout
1283 output = stdout_trap.getvalue()
1284 output = stdout_trap.getvalue()
1284 output = output.rstrip()
1285 output = output.rstrip()
1285
1286
1286 page(output,screen_lines=self.shell.rc.screen_length)
1287 page(output,screen_lines=self.shell.rc.screen_length)
1287 print sys_exit,
1288 print sys_exit,
1288
1289
1289 dump_file = opts.D[0]
1290 dump_file = opts.D[0]
1290 text_file = opts.T[0]
1291 text_file = opts.T[0]
1291 if dump_file:
1292 if dump_file:
1292 prof.dump_stats(dump_file)
1293 prof.dump_stats(dump_file)
1293 print '\n*** Profile stats marshalled to file',\
1294 print '\n*** Profile stats marshalled to file',\
1294 `dump_file`+'.',sys_exit
1295 `dump_file`+'.',sys_exit
1295 if text_file:
1296 if text_file:
1296 file(text_file,'w').write(output)
1297 file(text_file,'w').write(output)
1297 print '\n*** Profile printout saved to text file',\
1298 print '\n*** Profile printout saved to text file',\
1298 `text_file`+'.',sys_exit
1299 `text_file`+'.',sys_exit
1299
1300
1300 if opts.has_key('r'):
1301 if opts.has_key('r'):
1301 return stats
1302 return stats
1302 else:
1303 else:
1303 return None
1304 return None
1304
1305
1305 def magic_run(self, parameter_s ='',runner=None):
1306 def magic_run(self, parameter_s ='',runner=None):
1306 """Run the named file inside IPython as a program.
1307 """Run the named file inside IPython as a program.
1307
1308
1308 Usage:\\
1309 Usage:\\
1309 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1310 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1310
1311
1311 Parameters after the filename are passed as command-line arguments to
1312 Parameters after the filename are passed as command-line arguments to
1312 the program (put in sys.argv). Then, control returns to IPython's
1313 the program (put in sys.argv). Then, control returns to IPython's
1313 prompt.
1314 prompt.
1314
1315
1315 This is similar to running at a system prompt:\\
1316 This is similar to running at a system prompt:\\
1316 $ python file args\\
1317 $ python file args\\
1317 but with the advantage of giving you IPython's tracebacks, and of
1318 but with the advantage of giving you IPython's tracebacks, and of
1318 loading all variables into your interactive namespace for further use
1319 loading all variables into your interactive namespace for further use
1319 (unless -p is used, see below).
1320 (unless -p is used, see below).
1320
1321
1321 The file is executed in a namespace initially consisting only of
1322 The file is executed in a namespace initially consisting only of
1322 __name__=='__main__' and sys.argv constructed as indicated. It thus
1323 __name__=='__main__' and sys.argv constructed as indicated. It thus
1323 sees its environment as if it were being run as a stand-alone
1324 sees its environment as if it were being run as a stand-alone
1324 program. But after execution, the IPython interactive namespace gets
1325 program. But after execution, the IPython interactive namespace gets
1325 updated with all variables defined in the program (except for __name__
1326 updated with all variables defined in the program (except for __name__
1326 and sys.argv). This allows for very convenient loading of code for
1327 and sys.argv). This allows for very convenient loading of code for
1327 interactive work, while giving each program a 'clean sheet' to run in.
1328 interactive work, while giving each program a 'clean sheet' to run in.
1328
1329
1329 Options:
1330 Options:
1330
1331
1331 -n: __name__ is NOT set to '__main__', but to the running file's name
1332 -n: __name__ is NOT set to '__main__', but to the running file's name
1332 without extension (as python does under import). This allows running
1333 without extension (as python does under import). This allows running
1333 scripts and reloading the definitions in them without calling code
1334 scripts and reloading the definitions in them without calling code
1334 protected by an ' if __name__ == "__main__" ' clause.
1335 protected by an ' if __name__ == "__main__" ' clause.
1335
1336
1336 -i: run the file in IPython's namespace instead of an empty one. This
1337 -i: run the file in IPython's namespace instead of an empty one. This
1337 is useful if you are experimenting with code written in a text editor
1338 is useful if you are experimenting with code written in a text editor
1338 which depends on variables defined interactively.
1339 which depends on variables defined interactively.
1339
1340
1340 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1341 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1341 being run. This is particularly useful if IPython is being used to
1342 being run. This is particularly useful if IPython is being used to
1342 run unittests, which always exit with a sys.exit() call. In such
1343 run unittests, which always exit with a sys.exit() call. In such
1343 cases you are interested in the output of the test results, not in
1344 cases you are interested in the output of the test results, not in
1344 seeing a traceback of the unittest module.
1345 seeing a traceback of the unittest module.
1345
1346
1346 -t: print timing information at the end of the run. IPython will give
1347 -t: print timing information at the end of the run. IPython will give
1347 you an estimated CPU time consumption for your script, which under
1348 you an estimated CPU time consumption for your script, which under
1348 Unix uses the resource module to avoid the wraparound problems of
1349 Unix uses the resource module to avoid the wraparound problems of
1349 time.clock(). Under Unix, an estimate of time spent on system tasks
1350 time.clock(). Under Unix, an estimate of time spent on system tasks
1350 is also given (for Windows platforms this is reported as 0.0).
1351 is also given (for Windows platforms this is reported as 0.0).
1351
1352
1352 If -t is given, an additional -N<N> option can be given, where <N>
1353 If -t is given, an additional -N<N> option can be given, where <N>
1353 must be an integer indicating how many times you want the script to
1354 must be an integer indicating how many times you want the script to
1354 run. The final timing report will include total and per run results.
1355 run. The final timing report will include total and per run results.
1355
1356
1356 For example (testing the script uniq_stable.py):
1357 For example (testing the script uniq_stable.py):
1357
1358
1358 In [1]: run -t uniq_stable
1359 In [1]: run -t uniq_stable
1359
1360
1360 IPython CPU timings (estimated):\\
1361 IPython CPU timings (estimated):\\
1361 User : 0.19597 s.\\
1362 User : 0.19597 s.\\
1362 System: 0.0 s.\\
1363 System: 0.0 s.\\
1363
1364
1364 In [2]: run -t -N5 uniq_stable
1365 In [2]: run -t -N5 uniq_stable
1365
1366
1366 IPython CPU timings (estimated):\\
1367 IPython CPU timings (estimated):\\
1367 Total runs performed: 5\\
1368 Total runs performed: 5\\
1368 Times : Total Per run\\
1369 Times : Total Per run\\
1369 User : 0.910862 s, 0.1821724 s.\\
1370 User : 0.910862 s, 0.1821724 s.\\
1370 System: 0.0 s, 0.0 s.
1371 System: 0.0 s, 0.0 s.
1371
1372
1372 -d: run your program under the control of pdb, the Python debugger.
1373 -d: run your program under the control of pdb, the Python debugger.
1373 This allows you to execute your program step by step, watch variables,
1374 This allows you to execute your program step by step, watch variables,
1374 etc. Internally, what IPython does is similar to calling:
1375 etc. Internally, what IPython does is similar to calling:
1375
1376
1376 pdb.run('execfile("YOURFILENAME")')
1377 pdb.run('execfile("YOURFILENAME")')
1377
1378
1378 with a breakpoint set on line 1 of your file. You can change the line
1379 with a breakpoint set on line 1 of your file. You can change the line
1379 number for this automatic breakpoint to be <N> by using the -bN option
1380 number for this automatic breakpoint to be <N> by using the -bN option
1380 (where N must be an integer). For example:
1381 (where N must be an integer). For example:
1381
1382
1382 %run -d -b40 myscript
1383 %run -d -b40 myscript
1383
1384
1384 will set the first breakpoint at line 40 in myscript.py. Note that
1385 will set the first breakpoint at line 40 in myscript.py. Note that
1385 the first breakpoint must be set on a line which actually does
1386 the first breakpoint must be set on a line which actually does
1386 something (not a comment or docstring) for it to stop execution.
1387 something (not a comment or docstring) for it to stop execution.
1387
1388
1388 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1389 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1389 first enter 'c' (without qoutes) to start execution up to the first
1390 first enter 'c' (without qoutes) to start execution up to the first
1390 breakpoint.
1391 breakpoint.
1391
1392
1392 Entering 'help' gives information about the use of the debugger. You
1393 Entering 'help' gives information about the use of the debugger. You
1393 can easily see pdb's full documentation with "import pdb;pdb.help()"
1394 can easily see pdb's full documentation with "import pdb;pdb.help()"
1394 at a prompt.
1395 at a prompt.
1395
1396
1396 -p: run program under the control of the Python profiler module (which
1397 -p: run program under the control of the Python profiler module (which
1397 prints a detailed report of execution times, function calls, etc).
1398 prints a detailed report of execution times, function calls, etc).
1398
1399
1399 You can pass other options after -p which affect the behavior of the
1400 You can pass other options after -p which affect the behavior of the
1400 profiler itself. See the docs for %prun for details.
1401 profiler itself. See the docs for %prun for details.
1401
1402
1402 In this mode, the program's variables do NOT propagate back to the
1403 In this mode, the program's variables do NOT propagate back to the
1403 IPython interactive namespace (because they remain in the namespace
1404 IPython interactive namespace (because they remain in the namespace
1404 where the profiler executes them).
1405 where the profiler executes them).
1405
1406
1406 Internally this triggers a call to %prun, see its documentation for
1407 Internally this triggers a call to %prun, see its documentation for
1407 details on the options available specifically for profiling."""
1408 details on the options available specifically for profiling."""
1408
1409
1409 # get arguments and set sys.argv for program to be run.
1410 # get arguments and set sys.argv for program to be run.
1410 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1411 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1411 mode='list',list_all=1)
1412 mode='list',list_all=1)
1412
1413
1413 try:
1414 try:
1414 filename = get_py_filename(arg_lst[0])
1415 filename = get_py_filename(arg_lst[0])
1415 except IndexError:
1416 except IndexError:
1416 warn('you must provide at least a filename.')
1417 warn('you must provide at least a filename.')
1417 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1418 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1418 return
1419 return
1419 except IOError,msg:
1420 except IOError,msg:
1420 error(msg)
1421 error(msg)
1421 return
1422 return
1422
1423
1423 # Control the response to exit() calls made by the script being run
1424 # Control the response to exit() calls made by the script being run
1424 exit_ignore = opts.has_key('e')
1425 exit_ignore = opts.has_key('e')
1425
1426
1426 # Make sure that the running script gets a proper sys.argv as if it
1427 # Make sure that the running script gets a proper sys.argv as if it
1427 # were run from a system shell.
1428 # were run from a system shell.
1428 save_argv = sys.argv # save it for later restoring
1429 save_argv = sys.argv # save it for later restoring
1429 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1430 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1430
1431
1431 if opts.has_key('i'):
1432 if opts.has_key('i'):
1432 prog_ns = self.shell.user_ns
1433 prog_ns = self.shell.user_ns
1433 __name__save = self.shell.user_ns['__name__']
1434 __name__save = self.shell.user_ns['__name__']
1434 prog_ns['__name__'] = '__main__'
1435 prog_ns['__name__'] = '__main__'
1435 else:
1436 else:
1436 if opts.has_key('n'):
1437 if opts.has_key('n'):
1437 name = os.path.splitext(os.path.basename(filename))[0]
1438 name = os.path.splitext(os.path.basename(filename))[0]
1438 else:
1439 else:
1439 name = '__main__'
1440 name = '__main__'
1440 prog_ns = {'__name__':name}
1441 prog_ns = {'__name__':name}
1441
1442
1442 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1443 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1443 # set the __file__ global in the script's namespace
1444 # set the __file__ global in the script's namespace
1444 prog_ns['__file__'] = filename
1445 prog_ns['__file__'] = filename
1445
1446
1446 # pickle fix. See iplib for an explanation. But we need to make sure
1447 # pickle fix. See iplib for an explanation. But we need to make sure
1447 # that, if we overwrite __main__, we replace it at the end
1448 # that, if we overwrite __main__, we replace it at the end
1448 if prog_ns['__name__'] == '__main__':
1449 if prog_ns['__name__'] == '__main__':
1449 restore_main = sys.modules['__main__']
1450 restore_main = sys.modules['__main__']
1450 else:
1451 else:
1451 restore_main = False
1452 restore_main = False
1452
1453
1453 sys.modules[prog_ns['__name__']] = FakeModule(prog_ns)
1454 sys.modules[prog_ns['__name__']] = FakeModule(prog_ns)
1454
1455
1455 stats = None
1456 stats = None
1456 try:
1457 try:
1457 if opts.has_key('p'):
1458 if opts.has_key('p'):
1458 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1459 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1459 else:
1460 else:
1460 if opts.has_key('d'):
1461 if opts.has_key('d'):
1461 deb = Debugger.Pdb(self.shell.rc.colors)
1462 deb = Debugger.Pdb(self.shell.rc.colors)
1462 # reset Breakpoint state, which is moronically kept
1463 # reset Breakpoint state, which is moronically kept
1463 # in a class
1464 # in a class
1464 bdb.Breakpoint.next = 1
1465 bdb.Breakpoint.next = 1
1465 bdb.Breakpoint.bplist = {}
1466 bdb.Breakpoint.bplist = {}
1466 bdb.Breakpoint.bpbynumber = [None]
1467 bdb.Breakpoint.bpbynumber = [None]
1467 # Set an initial breakpoint to stop execution
1468 # Set an initial breakpoint to stop execution
1468 maxtries = 10
1469 maxtries = 10
1469 bp = int(opts.get('b',[1])[0])
1470 bp = int(opts.get('b',[1])[0])
1470 checkline = deb.checkline(filename,bp)
1471 checkline = deb.checkline(filename,bp)
1471 if not checkline:
1472 if not checkline:
1472 for bp in range(bp+1,bp+maxtries+1):
1473 for bp in range(bp+1,bp+maxtries+1):
1473 if deb.checkline(filename,bp):
1474 if deb.checkline(filename,bp):
1474 break
1475 break
1475 else:
1476 else:
1476 msg = ("\nI failed to find a valid line to set "
1477 msg = ("\nI failed to find a valid line to set "
1477 "a breakpoint\n"
1478 "a breakpoint\n"
1478 "after trying up to line: %s.\n"
1479 "after trying up to line: %s.\n"
1479 "Please set a valid breakpoint manually "
1480 "Please set a valid breakpoint manually "
1480 "with the -b option." % bp)
1481 "with the -b option." % bp)
1481 error(msg)
1482 error(msg)
1482 return
1483 return
1483 # if we find a good linenumber, set the breakpoint
1484 # if we find a good linenumber, set the breakpoint
1484 deb.do_break('%s:%s' % (filename,bp))
1485 deb.do_break('%s:%s' % (filename,bp))
1485 # Start file run
1486 # Start file run
1486 print "NOTE: Enter 'c' at the",
1487 print "NOTE: Enter 'c' at the",
1487 print "ipdb> prompt to start your script."
1488 print "ipdb> prompt to start your script."
1488 try:
1489 try:
1489 deb.run('execfile("%s")' % filename,prog_ns)
1490 deb.run('execfile("%s")' % filename,prog_ns)
1490 except:
1491 except:
1491 etype, value, tb = sys.exc_info()
1492 etype, value, tb = sys.exc_info()
1492 # Skip three frames in the traceback: the %run one,
1493 # Skip three frames in the traceback: the %run one,
1493 # one inside bdb.py, and the command-line typed by the
1494 # one inside bdb.py, and the command-line typed by the
1494 # user (run by exec in pdb itself).
1495 # user (run by exec in pdb itself).
1495 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1496 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1496 else:
1497 else:
1497 if runner is None:
1498 if runner is None:
1498 runner = self.shell.safe_execfile
1499 runner = self.shell.safe_execfile
1499 if opts.has_key('t'):
1500 if opts.has_key('t'):
1500 try:
1501 try:
1501 nruns = int(opts['N'][0])
1502 nruns = int(opts['N'][0])
1502 if nruns < 1:
1503 if nruns < 1:
1503 error('Number of runs must be >=1')
1504 error('Number of runs must be >=1')
1504 return
1505 return
1505 except (KeyError):
1506 except (KeyError):
1506 nruns = 1
1507 nruns = 1
1507 if nruns == 1:
1508 if nruns == 1:
1508 t0 = clock2()
1509 t0 = clock2()
1509 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1510 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1510 t1 = clock2()
1511 t1 = clock2()
1511 t_usr = t1[0]-t0[0]
1512 t_usr = t1[0]-t0[0]
1512 t_sys = t1[1]-t1[1]
1513 t_sys = t1[1]-t1[1]
1513 print "\nIPython CPU timings (estimated):"
1514 print "\nIPython CPU timings (estimated):"
1514 print " User : %10s s." % t_usr
1515 print " User : %10s s." % t_usr
1515 print " System: %10s s." % t_sys
1516 print " System: %10s s." % t_sys
1516 else:
1517 else:
1517 runs = range(nruns)
1518 runs = range(nruns)
1518 t0 = clock2()
1519 t0 = clock2()
1519 for nr in runs:
1520 for nr in runs:
1520 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1521 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1521 t1 = clock2()
1522 t1 = clock2()
1522 t_usr = t1[0]-t0[0]
1523 t_usr = t1[0]-t0[0]
1523 t_sys = t1[1]-t1[1]
1524 t_sys = t1[1]-t1[1]
1524 print "\nIPython CPU timings (estimated):"
1525 print "\nIPython CPU timings (estimated):"
1525 print "Total runs performed:",nruns
1526 print "Total runs performed:",nruns
1526 print " Times : %10s %10s" % ('Total','Per run')
1527 print " Times : %10s %10s" % ('Total','Per run')
1527 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1528 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1528 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1529 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1529
1530
1530 else:
1531 else:
1531 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1532 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1532 if opts.has_key('i'):
1533 if opts.has_key('i'):
1533 self.shell.user_ns['__name__'] = __name__save
1534 self.shell.user_ns['__name__'] = __name__save
1534 else:
1535 else:
1535 # update IPython interactive namespace
1536 # update IPython interactive namespace
1536 del prog_ns['__name__']
1537 del prog_ns['__name__']
1537 self.shell.user_ns.update(prog_ns)
1538 self.shell.user_ns.update(prog_ns)
1538 finally:
1539 finally:
1539 sys.argv = save_argv
1540 sys.argv = save_argv
1540 if restore_main:
1541 if restore_main:
1541 sys.modules['__main__'] = restore_main
1542 sys.modules['__main__'] = restore_main
1542 return stats
1543 return stats
1543
1544
1544 def magic_runlog(self, parameter_s =''):
1545 def magic_runlog(self, parameter_s =''):
1545 """Run files as logs.
1546 """Run files as logs.
1546
1547
1547 Usage:\\
1548 Usage:\\
1548 %runlog file1 file2 ...
1549 %runlog file1 file2 ...
1549
1550
1550 Run the named files (treating them as log files) in sequence inside
1551 Run the named files (treating them as log files) in sequence inside
1551 the interpreter, and return to the prompt. This is much slower than
1552 the interpreter, and return to the prompt. This is much slower than
1552 %run because each line is executed in a try/except block, but it
1553 %run because each line is executed in a try/except block, but it
1553 allows running files with syntax errors in them.
1554 allows running files with syntax errors in them.
1554
1555
1555 Normally IPython will guess when a file is one of its own logfiles, so
1556 Normally IPython will guess when a file is one of its own logfiles, so
1556 you can typically use %run even for logs. This shorthand allows you to
1557 you can typically use %run even for logs. This shorthand allows you to
1557 force any file to be treated as a log file."""
1558 force any file to be treated as a log file."""
1558
1559
1559 for f in parameter_s.split():
1560 for f in parameter_s.split():
1560 self.shell.safe_execfile(f,self.shell.user_ns,
1561 self.shell.safe_execfile(f,self.shell.user_ns,
1561 self.shell.user_ns,islog=1)
1562 self.shell.user_ns,islog=1)
1562
1563
1563 def magic_timeit(self, parameter_s =''):
1564 def magic_timeit(self, parameter_s =''):
1564 """Time execution of a Python statement or expression
1565 """Time execution of a Python statement or expression
1565
1566
1566 Usage:\\
1567 Usage:\\
1567 %timeit [-n<N> -r<R> [-t|-c]] statement
1568 %timeit [-n<N> -r<R> [-t|-c]] statement
1568
1569
1569 Time execution of a Python statement or expression using the timeit
1570 Time execution of a Python statement or expression using the timeit
1570 module.
1571 module.
1571
1572
1572 Options:
1573 Options:
1573 -n<N>: execute the given statement <N> times in a loop. If this value
1574 -n<N>: execute the given statement <N> times in a loop. If this value
1574 is not given, a fitting value is chosen.
1575 is not given, a fitting value is chosen.
1575
1576
1576 -r<R>: repeat the loop iteration <R> times and take the best result.
1577 -r<R>: repeat the loop iteration <R> times and take the best result.
1577 Default: 3
1578 Default: 3
1578
1579
1579 -t: use time.time to measure the time, which is the default on Unix.
1580 -t: use time.time to measure the time, which is the default on Unix.
1580 This function measures wall time.
1581 This function measures wall time.
1581
1582
1582 -c: use time.clock to measure the time, which is the default on
1583 -c: use time.clock to measure the time, which is the default on
1583 Windows and measures wall time. On Unix, resource.getrusage is used
1584 Windows and measures wall time. On Unix, resource.getrusage is used
1584 instead and returns the CPU user time.
1585 instead and returns the CPU user time.
1585
1586
1586 -p<P>: use a precision of <P> digits to display the timing result.
1587 -p<P>: use a precision of <P> digits to display the timing result.
1587 Default: 3
1588 Default: 3
1588
1589
1589
1590
1590 Examples:\\
1591 Examples:\\
1591 In [1]: %timeit pass
1592 In [1]: %timeit pass
1592 10000000 loops, best of 3: 53.3 ns per loop
1593 10000000 loops, best of 3: 53.3 ns per loop
1593
1594
1594 In [2]: u = None
1595 In [2]: u = None
1595
1596
1596 In [3]: %timeit u is None
1597 In [3]: %timeit u is None
1597 10000000 loops, best of 3: 184 ns per loop
1598 10000000 loops, best of 3: 184 ns per loop
1598
1599
1599 In [4]: %timeit -r 4 u == None
1600 In [4]: %timeit -r 4 u == None
1600 1000000 loops, best of 4: 242 ns per loop
1601 1000000 loops, best of 4: 242 ns per loop
1601
1602
1602 In [5]: import time
1603 In [5]: import time
1603
1604
1604 In [6]: %timeit -n1 time.sleep(2)
1605 In [6]: %timeit -n1 time.sleep(2)
1605 1 loops, best of 3: 2 s per loop
1606 1 loops, best of 3: 2 s per loop
1606
1607
1607
1608
1608 The times reported by %timeit will be slightly higher than those reported
1609 The times reported by %timeit will be slightly higher than those reported
1609 by the timeit.py script when variables are accessed. This is due to the
1610 by the timeit.py script when variables are accessed. This is due to the
1610 fact that %timeit executes the statement in the namespace of the shell,
1611 fact that %timeit executes the statement in the namespace of the shell,
1611 compared with timeit.py, which uses a single setup statement to import
1612 compared with timeit.py, which uses a single setup statement to import
1612 function or create variables. Generally, the bias does not matter as long
1613 function or create variables. Generally, the bias does not matter as long
1613 as results from timeit.py are not mixed with those from %timeit."""
1614 as results from timeit.py are not mixed with those from %timeit."""
1614 import timeit
1615 import timeit
1615 import math
1616 import math
1616
1617
1617 units = ["s", "ms", "\xc2\xb5s", "ns"]
1618 units = ["s", "ms", "\xc2\xb5s", "ns"]
1618 scaling = [1, 1e3, 1e6, 1e9]
1619 scaling = [1, 1e3, 1e6, 1e9]
1619
1620
1620 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:')
1621 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:')
1621 if stmt == "":
1622 if stmt == "":
1622 return
1623 return
1623 timefunc = timeit.default_timer
1624 timefunc = timeit.default_timer
1624 number = int(getattr(opts, "n", 0))
1625 number = int(getattr(opts, "n", 0))
1625 repeat = int(getattr(opts, "r", timeit.default_repeat))
1626 repeat = int(getattr(opts, "r", timeit.default_repeat))
1626 precision = int(getattr(opts, "p", 3))
1627 precision = int(getattr(opts, "p", 3))
1627 if hasattr(opts, "t"):
1628 if hasattr(opts, "t"):
1628 timefunc = time.time
1629 timefunc = time.time
1629 if hasattr(opts, "c"):
1630 if hasattr(opts, "c"):
1630 timefunc = clock
1631 timefunc = clock
1631
1632
1632 timer = timeit.Timer(timer=timefunc)
1633 timer = timeit.Timer(timer=timefunc)
1633 # this code has tight coupling to the inner workings of timeit.Timer,
1634 # this code has tight coupling to the inner workings of timeit.Timer,
1634 # but is there a better way to achieve that the code stmt has access
1635 # but is there a better way to achieve that the code stmt has access
1635 # to the shell namespace?
1636 # to the shell namespace?
1636
1637
1637 src = timeit.template % {'stmt': timeit.reindent(stmt, 8), 'setup': "pass"}
1638 src = timeit.template % {'stmt': timeit.reindent(stmt, 8), 'setup': "pass"}
1638 code = compile(src, "<magic-timeit>", "exec")
1639 code = compile(src, "<magic-timeit>", "exec")
1639 ns = {}
1640 ns = {}
1640 exec code in self.shell.user_ns, ns
1641 exec code in self.shell.user_ns, ns
1641 timer.inner = ns["inner"]
1642 timer.inner = ns["inner"]
1642
1643
1643 if number == 0:
1644 if number == 0:
1644 # determine number so that 0.2 <= total time < 2.0
1645 # determine number so that 0.2 <= total time < 2.0
1645 number = 1
1646 number = 1
1646 for i in range(1, 10):
1647 for i in range(1, 10):
1647 number *= 10
1648 number *= 10
1648 if timer.timeit(number) >= 0.2:
1649 if timer.timeit(number) >= 0.2:
1649 break
1650 break
1650
1651
1651 best = min(timer.repeat(repeat, number)) / number
1652 best = min(timer.repeat(repeat, number)) / number
1652
1653
1653 if best > 0.0:
1654 if best > 0.0:
1654 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1655 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1655 else:
1656 else:
1656 order = 3
1657 order = 3
1657 print "%d loops, best of %d: %.*g %s per loop" % (number, repeat, precision,
1658 print "%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1659 precision,
1658 best * scaling[order],
1660 best * scaling[order],
1659 units[order])
1661 units[order])
1660
1661
1662
1663
1662
1664 def magic_time(self,parameter_s = ''):
1663 def magic_time(self,parameter_s = ''):
1665 """Time execution of a Python statement or expression.
1664 """Time execution of a Python statement or expression.
1666
1665
1667 The CPU and wall clock times are printed, and the value of the
1666 The CPU and wall clock times are printed, and the value of the
1668 expression (if any) is returned. Note that under Win32, system time
1667 expression (if any) is returned. Note that under Win32, system time
1669 is always reported as 0, since it can not be measured.
1668 is always reported as 0, since it can not be measured.
1670
1669
1671 This function provides very basic timing functionality. In Python
1670 This function provides very basic timing functionality. In Python
1672 2.3, the timeit module offers more control and sophistication, but for
1671 2.3, the timeit module offers more control and sophistication, so this
1673 now IPython supports Python 2.2, so we can not rely on timeit being
1672 could be rewritten to use it (patches welcome).
1674 present.
1675
1673
1676 Some examples:
1674 Some examples:
1677
1675
1678 In [1]: time 2**128
1676 In [1]: time 2**128
1679 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1677 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1680 Wall time: 0.00
1678 Wall time: 0.00
1681 Out[1]: 340282366920938463463374607431768211456L
1679 Out[1]: 340282366920938463463374607431768211456L
1682
1680
1683 In [2]: n = 1000000
1681 In [2]: n = 1000000
1684
1682
1685 In [3]: time sum(range(n))
1683 In [3]: time sum(range(n))
1686 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1684 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1687 Wall time: 1.37
1685 Wall time: 1.37
1688 Out[3]: 499999500000L
1686 Out[3]: 499999500000L
1689
1687
1690 In [4]: time print 'hello world'
1688 In [4]: time print 'hello world'
1691 hello world
1689 hello world
1692 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1690 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1693 Wall time: 0.00
1691 Wall time: 0.00
1694 """
1692 """
1695
1693
1696 # fail immediately if the given expression can't be compiled
1694 # fail immediately if the given expression can't be compiled
1697 try:
1695 try:
1698 mode = 'eval'
1696 mode = 'eval'
1699 code = compile(parameter_s,'<timed eval>',mode)
1697 code = compile(parameter_s,'<timed eval>',mode)
1700 except SyntaxError:
1698 except SyntaxError:
1701 mode = 'exec'
1699 mode = 'exec'
1702 code = compile(parameter_s,'<timed exec>',mode)
1700 code = compile(parameter_s,'<timed exec>',mode)
1703 # skew measurement as little as possible
1701 # skew measurement as little as possible
1704 glob = self.shell.user_ns
1702 glob = self.shell.user_ns
1705 clk = clock2
1703 clk = clock2
1706 wtime = time.time
1704 wtime = time.time
1707 # time execution
1705 # time execution
1708 wall_st = wtime()
1706 wall_st = wtime()
1709 if mode=='eval':
1707 if mode=='eval':
1710 st = clk()
1708 st = clk()
1711 out = eval(code,glob)
1709 out = eval(code,glob)
1712 end = clk()
1710 end = clk()
1713 else:
1711 else:
1714 st = clk()
1712 st = clk()
1715 exec code in glob
1713 exec code in glob
1716 end = clk()
1714 end = clk()
1717 out = None
1715 out = None
1718 wall_end = wtime()
1716 wall_end = wtime()
1719 # Compute actual times and report
1717 # Compute actual times and report
1720 wall_time = wall_end-wall_st
1718 wall_time = wall_end-wall_st
1721 cpu_user = end[0]-st[0]
1719 cpu_user = end[0]-st[0]
1722 cpu_sys = end[1]-st[1]
1720 cpu_sys = end[1]-st[1]
1723 cpu_tot = cpu_user+cpu_sys
1721 cpu_tot = cpu_user+cpu_sys
1724 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1722 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1725 (cpu_user,cpu_sys,cpu_tot)
1723 (cpu_user,cpu_sys,cpu_tot)
1726 print "Wall time: %.2f" % wall_time
1724 print "Wall time: %.2f" % wall_time
1727 return out
1725 return out
1728
1726
1729 def magic_macro(self,parameter_s = ''):
1727 def magic_macro(self,parameter_s = ''):
1730 """Define a set of input lines as a macro for future re-execution.
1728 """Define a set of input lines as a macro for future re-execution.
1731
1729
1732 Usage:\\
1730 Usage:\\
1733 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1731 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1734
1732
1735 Options:
1733 Options:
1736
1734
1737 -r: use 'raw' input. By default, the 'processed' history is used,
1735 -r: use 'raw' input. By default, the 'processed' history is used,
1738 so that magics are loaded in their transformed version to valid
1736 so that magics are loaded in their transformed version to valid
1739 Python. If this option is given, the raw input as typed as the
1737 Python. If this option is given, the raw input as typed as the
1740 command line is used instead.
1738 command line is used instead.
1741
1739
1742 This will define a global variable called `name` which is a string
1740 This will define a global variable called `name` which is a string
1743 made of joining the slices and lines you specify (n1,n2,... numbers
1741 made of joining the slices and lines you specify (n1,n2,... numbers
1744 above) from your input history into a single string. This variable
1742 above) from your input history into a single string. This variable
1745 acts like an automatic function which re-executes those lines as if
1743 acts like an automatic function which re-executes those lines as if
1746 you had typed them. You just type 'name' at the prompt and the code
1744 you had typed them. You just type 'name' at the prompt and the code
1747 executes.
1745 executes.
1748
1746
1749 The notation for indicating number ranges is: n1-n2 means 'use line
1747 The notation for indicating number ranges is: n1-n2 means 'use line
1750 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
1748 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
1751 using the lines numbered 5,6 and 7.
1749 using the lines numbered 5,6 and 7.
1752
1750
1753 Note: as a 'hidden' feature, you can also use traditional python slice
1751 Note: as a 'hidden' feature, you can also use traditional python slice
1754 notation, where N:M means numbers N through M-1.
1752 notation, where N:M means numbers N through M-1.
1755
1753
1756 For example, if your history contains (%hist prints it):
1754 For example, if your history contains (%hist prints it):
1757
1755
1758 44: x=1\\
1756 44: x=1\\
1759 45: y=3\\
1757 45: y=3\\
1760 46: z=x+y\\
1758 46: z=x+y\\
1761 47: print x\\
1759 47: print x\\
1762 48: a=5\\
1760 48: a=5\\
1763 49: print 'x',x,'y',y\\
1761 49: print 'x',x,'y',y\\
1764
1762
1765 you can create a macro with lines 44 through 47 (included) and line 49
1763 you can create a macro with lines 44 through 47 (included) and line 49
1766 called my_macro with:
1764 called my_macro with:
1767
1765
1768 In [51]: %macro my_macro 44-47 49
1766 In [51]: %macro my_macro 44-47 49
1769
1767
1770 Now, typing `my_macro` (without quotes) will re-execute all this code
1768 Now, typing `my_macro` (without quotes) will re-execute all this code
1771 in one pass.
1769 in one pass.
1772
1770
1773 You don't need to give the line-numbers in order, and any given line
1771 You don't need to give the line-numbers in order, and any given line
1774 number can appear multiple times. You can assemble macros with any
1772 number can appear multiple times. You can assemble macros with any
1775 lines from your input history in any order.
1773 lines from your input history in any order.
1776
1774
1777 The macro is a simple object which holds its value in an attribute,
1775 The macro is a simple object which holds its value in an attribute,
1778 but IPython's display system checks for macros and executes them as
1776 but IPython's display system checks for macros and executes them as
1779 code instead of printing them when you type their name.
1777 code instead of printing them when you type their name.
1780
1778
1781 You can view a macro's contents by explicitly printing it with:
1779 You can view a macro's contents by explicitly printing it with:
1782
1780
1783 'print macro_name'.
1781 'print macro_name'.
1784
1782
1785 For one-off cases which DON'T contain magic function calls in them you
1783 For one-off cases which DON'T contain magic function calls in them you
1786 can obtain similar results by explicitly executing slices from your
1784 can obtain similar results by explicitly executing slices from your
1787 input history with:
1785 input history with:
1788
1786
1789 In [60]: exec In[44:48]+In[49]"""
1787 In [60]: exec In[44:48]+In[49]"""
1790
1788
1791 opts,args = self.parse_options(parameter_s,'r',mode='list')
1789 opts,args = self.parse_options(parameter_s,'r',mode='list')
1792 name,ranges = args[0], args[1:]
1790 name,ranges = args[0], args[1:]
1793 #print 'rng',ranges # dbg
1791 #print 'rng',ranges # dbg
1794 lines = self.extract_input_slices(ranges,opts.has_key('r'))
1792 lines = self.extract_input_slices(ranges,opts.has_key('r'))
1795 macro = Macro(lines)
1793 macro = Macro(lines)
1796 self.shell.user_ns.update({name:macro})
1794 self.shell.user_ns.update({name:macro})
1797 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1795 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1798 print 'Macro contents:'
1796 print 'Macro contents:'
1799 print macro,
1797 print macro,
1800
1798
1801 def magic_save(self,parameter_s = ''):
1799 def magic_save(self,parameter_s = ''):
1802 """Save a set of lines to a given filename.
1800 """Save a set of lines to a given filename.
1803
1801
1804 Usage:\\
1802 Usage:\\
1805 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
1803 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
1806
1804
1807 Options:
1805 Options:
1808
1806
1809 -r: use 'raw' input. By default, the 'processed' history is used,
1807 -r: use 'raw' input. By default, the 'processed' history is used,
1810 so that magics are loaded in their transformed version to valid
1808 so that magics are loaded in their transformed version to valid
1811 Python. If this option is given, the raw input as typed as the
1809 Python. If this option is given, the raw input as typed as the
1812 command line is used instead.
1810 command line is used instead.
1813
1811
1814 This function uses the same syntax as %macro for line extraction, but
1812 This function uses the same syntax as %macro for line extraction, but
1815 instead of creating a macro it saves the resulting string to the
1813 instead of creating a macro it saves the resulting string to the
1816 filename you specify.
1814 filename you specify.
1817
1815
1818 It adds a '.py' extension to the file if you don't do so yourself, and
1816 It adds a '.py' extension to the file if you don't do so yourself, and
1819 it asks for confirmation before overwriting existing files."""
1817 it asks for confirmation before overwriting existing files."""
1820
1818
1821 opts,args = self.parse_options(parameter_s,'r',mode='list')
1819 opts,args = self.parse_options(parameter_s,'r',mode='list')
1822 fname,ranges = args[0], args[1:]
1820 fname,ranges = args[0], args[1:]
1823 if not fname.endswith('.py'):
1821 if not fname.endswith('.py'):
1824 fname += '.py'
1822 fname += '.py'
1825 if os.path.isfile(fname):
1823 if os.path.isfile(fname):
1826 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
1824 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
1827 if ans.lower() not in ['y','yes']:
1825 if ans.lower() not in ['y','yes']:
1828 print 'Operation cancelled.'
1826 print 'Operation cancelled.'
1829 return
1827 return
1830 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
1828 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
1831 f = file(fname,'w')
1829 f = file(fname,'w')
1832 f.write(cmds)
1830 f.write(cmds)
1833 f.close()
1831 f.close()
1834 print 'The following commands were written to file `%s`:' % fname
1832 print 'The following commands were written to file `%s`:' % fname
1835 print cmds
1833 print cmds
1836
1834
1837 def _edit_macro(self,mname,macro):
1835 def _edit_macro(self,mname,macro):
1838 """open an editor with the macro data in a file"""
1836 """open an editor with the macro data in a file"""
1839 filename = self.shell.mktempfile(macro.value)
1837 filename = self.shell.mktempfile(macro.value)
1840 self.shell.hooks.editor(filename)
1838 self.shell.hooks.editor(filename)
1841
1839
1842 # and make a new macro object, to replace the old one
1840 # and make a new macro object, to replace the old one
1843 mfile = open(filename)
1841 mfile = open(filename)
1844 mvalue = mfile.read()
1842 mvalue = mfile.read()
1845 mfile.close()
1843 mfile.close()
1846 self.shell.user_ns[mname] = Macro(mvalue)
1844 self.shell.user_ns[mname] = Macro(mvalue)
1847
1845
1848 def magic_ed(self,parameter_s=''):
1846 def magic_ed(self,parameter_s=''):
1849 """Alias to %edit."""
1847 """Alias to %edit."""
1850 return self.magic_edit(parameter_s)
1848 return self.magic_edit(parameter_s)
1851
1849
1852 def magic_edit(self,parameter_s='',last_call=['','']):
1850 def magic_edit(self,parameter_s='',last_call=['','']):
1853 """Bring up an editor and execute the resulting code.
1851 """Bring up an editor and execute the resulting code.
1854
1852
1855 Usage:
1853 Usage:
1856 %edit [options] [args]
1854 %edit [options] [args]
1857
1855
1858 %edit runs IPython's editor hook. The default version of this hook is
1856 %edit runs IPython's editor hook. The default version of this hook is
1859 set to call the __IPYTHON__.rc.editor command. This is read from your
1857 set to call the __IPYTHON__.rc.editor command. This is read from your
1860 environment variable $EDITOR. If this isn't found, it will default to
1858 environment variable $EDITOR. If this isn't found, it will default to
1861 vi under Linux/Unix and to notepad under Windows. See the end of this
1859 vi under Linux/Unix and to notepad under Windows. See the end of this
1862 docstring for how to change the editor hook.
1860 docstring for how to change the editor hook.
1863
1861
1864 You can also set the value of this editor via the command line option
1862 You can also set the value of this editor via the command line option
1865 '-editor' or in your ipythonrc file. This is useful if you wish to use
1863 '-editor' or in your ipythonrc file. This is useful if you wish to use
1866 specifically for IPython an editor different from your typical default
1864 specifically for IPython an editor different from your typical default
1867 (and for Windows users who typically don't set environment variables).
1865 (and for Windows users who typically don't set environment variables).
1868
1866
1869 This command allows you to conveniently edit multi-line code right in
1867 This command allows you to conveniently edit multi-line code right in
1870 your IPython session.
1868 your IPython session.
1871
1869
1872 If called without arguments, %edit opens up an empty editor with a
1870 If called without arguments, %edit opens up an empty editor with a
1873 temporary file and will execute the contents of this file when you
1871 temporary file and will execute the contents of this file when you
1874 close it (don't forget to save it!).
1872 close it (don't forget to save it!).
1875
1873
1876
1874
1877 Options:
1875 Options:
1878
1876
1879 -p: this will call the editor with the same data as the previous time
1877 -p: this will call the editor with the same data as the previous time
1880 it was used, regardless of how long ago (in your current session) it
1878 it was used, regardless of how long ago (in your current session) it
1881 was.
1879 was.
1882
1880
1883 -r: use 'raw' input. This option only applies to input taken from the
1881 -r: use 'raw' input. This option only applies to input taken from the
1884 user's history. By default, the 'processed' history is used, so that
1882 user's history. By default, the 'processed' history is used, so that
1885 magics are loaded in their transformed version to valid Python. If
1883 magics are loaded in their transformed version to valid Python. If
1886 this option is given, the raw input as typed as the command line is
1884 this option is given, the raw input as typed as the command line is
1887 used instead. When you exit the editor, it will be executed by
1885 used instead. When you exit the editor, it will be executed by
1888 IPython's own processor.
1886 IPython's own processor.
1889
1887
1890 -x: do not execute the edited code immediately upon exit. This is
1888 -x: do not execute the edited code immediately upon exit. This is
1891 mainly useful if you are editing programs which need to be called with
1889 mainly useful if you are editing programs which need to be called with
1892 command line arguments, which you can then do using %run.
1890 command line arguments, which you can then do using %run.
1893
1891
1894
1892
1895 Arguments:
1893 Arguments:
1896
1894
1897 If arguments are given, the following possibilites exist:
1895 If arguments are given, the following possibilites exist:
1898
1896
1899 - The arguments are numbers or pairs of colon-separated numbers (like
1897 - The arguments are numbers or pairs of colon-separated numbers (like
1900 1 4:8 9). These are interpreted as lines of previous input to be
1898 1 4:8 9). These are interpreted as lines of previous input to be
1901 loaded into the editor. The syntax is the same of the %macro command.
1899 loaded into the editor. The syntax is the same of the %macro command.
1902
1900
1903 - If the argument doesn't start with a number, it is evaluated as a
1901 - If the argument doesn't start with a number, it is evaluated as a
1904 variable and its contents loaded into the editor. You can thus edit
1902 variable and its contents loaded into the editor. You can thus edit
1905 any string which contains python code (including the result of
1903 any string which contains python code (including the result of
1906 previous edits).
1904 previous edits).
1907
1905
1908 - If the argument is the name of an object (other than a string),
1906 - If the argument is the name of an object (other than a string),
1909 IPython will try to locate the file where it was defined and open the
1907 IPython will try to locate the file where it was defined and open the
1910 editor at the point where it is defined. You can use `%edit function`
1908 editor at the point where it is defined. You can use `%edit function`
1911 to load an editor exactly at the point where 'function' is defined,
1909 to load an editor exactly at the point where 'function' is defined,
1912 edit it and have the file be executed automatically.
1910 edit it and have the file be executed automatically.
1913
1911
1914 If the object is a macro (see %macro for details), this opens up your
1912 If the object is a macro (see %macro for details), this opens up your
1915 specified editor with a temporary file containing the macro's data.
1913 specified editor with a temporary file containing the macro's data.
1916 Upon exit, the macro is reloaded with the contents of the file.
1914 Upon exit, the macro is reloaded with the contents of the file.
1917
1915
1918 Note: opening at an exact line is only supported under Unix, and some
1916 Note: opening at an exact line is only supported under Unix, and some
1919 editors (like kedit and gedit up to Gnome 2.8) do not understand the
1917 editors (like kedit and gedit up to Gnome 2.8) do not understand the
1920 '+NUMBER' parameter necessary for this feature. Good editors like
1918 '+NUMBER' parameter necessary for this feature. Good editors like
1921 (X)Emacs, vi, jed, pico and joe all do.
1919 (X)Emacs, vi, jed, pico and joe all do.
1922
1920
1923 - If the argument is not found as a variable, IPython will look for a
1921 - If the argument is not found as a variable, IPython will look for a
1924 file with that name (adding .py if necessary) and load it into the
1922 file with that name (adding .py if necessary) and load it into the
1925 editor. It will execute its contents with execfile() when you exit,
1923 editor. It will execute its contents with execfile() when you exit,
1926 loading any code in the file into your interactive namespace.
1924 loading any code in the file into your interactive namespace.
1927
1925
1928 After executing your code, %edit will return as output the code you
1926 After executing your code, %edit will return as output the code you
1929 typed in the editor (except when it was an existing file). This way
1927 typed in the editor (except when it was an existing file). This way
1930 you can reload the code in further invocations of %edit as a variable,
1928 you can reload the code in further invocations of %edit as a variable,
1931 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
1929 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
1932 the output.
1930 the output.
1933
1931
1934 Note that %edit is also available through the alias %ed.
1932 Note that %edit is also available through the alias %ed.
1935
1933
1936 This is an example of creating a simple function inside the editor and
1934 This is an example of creating a simple function inside the editor and
1937 then modifying it. First, start up the editor:
1935 then modifying it. First, start up the editor:
1938
1936
1939 In [1]: ed\\
1937 In [1]: ed\\
1940 Editing... done. Executing edited code...\\
1938 Editing... done. Executing edited code...\\
1941 Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
1939 Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
1942
1940
1943 We can then call the function foo():
1941 We can then call the function foo():
1944
1942
1945 In [2]: foo()\\
1943 In [2]: foo()\\
1946 foo() was defined in an editing session
1944 foo() was defined in an editing session
1947
1945
1948 Now we edit foo. IPython automatically loads the editor with the
1946 Now we edit foo. IPython automatically loads the editor with the
1949 (temporary) file where foo() was previously defined:
1947 (temporary) file where foo() was previously defined:
1950
1948
1951 In [3]: ed foo\\
1949 In [3]: ed foo\\
1952 Editing... done. Executing edited code...
1950 Editing... done. Executing edited code...
1953
1951
1954 And if we call foo() again we get the modified version:
1952 And if we call foo() again we get the modified version:
1955
1953
1956 In [4]: foo()\\
1954 In [4]: foo()\\
1957 foo() has now been changed!
1955 foo() has now been changed!
1958
1956
1959 Here is an example of how to edit a code snippet successive
1957 Here is an example of how to edit a code snippet successive
1960 times. First we call the editor:
1958 times. First we call the editor:
1961
1959
1962 In [8]: ed\\
1960 In [8]: ed\\
1963 Editing... done. Executing edited code...\\
1961 Editing... done. Executing edited code...\\
1964 hello\\
1962 hello\\
1965 Out[8]: "print 'hello'\\n"
1963 Out[8]: "print 'hello'\\n"
1966
1964
1967 Now we call it again with the previous output (stored in _):
1965 Now we call it again with the previous output (stored in _):
1968
1966
1969 In [9]: ed _\\
1967 In [9]: ed _\\
1970 Editing... done. Executing edited code...\\
1968 Editing... done. Executing edited code...\\
1971 hello world\\
1969 hello world\\
1972 Out[9]: "print 'hello world'\\n"
1970 Out[9]: "print 'hello world'\\n"
1973
1971
1974 Now we call it with the output #8 (stored in _8, also as Out[8]):
1972 Now we call it with the output #8 (stored in _8, also as Out[8]):
1975
1973
1976 In [10]: ed _8\\
1974 In [10]: ed _8\\
1977 Editing... done. Executing edited code...\\
1975 Editing... done. Executing edited code...\\
1978 hello again\\
1976 hello again\\
1979 Out[10]: "print 'hello again'\\n"
1977 Out[10]: "print 'hello again'\\n"
1980
1978
1981
1979
1982 Changing the default editor hook:
1980 Changing the default editor hook:
1983
1981
1984 If you wish to write your own editor hook, you can put it in a
1982 If you wish to write your own editor hook, you can put it in a
1985 configuration file which you load at startup time. The default hook
1983 configuration file which you load at startup time. The default hook
1986 is defined in the IPython.hooks module, and you can use that as a
1984 is defined in the IPython.hooks module, and you can use that as a
1987 starting example for further modifications. That file also has
1985 starting example for further modifications. That file also has
1988 general instructions on how to set a new hook for use once you've
1986 general instructions on how to set a new hook for use once you've
1989 defined it."""
1987 defined it."""
1990
1988
1991 # FIXME: This function has become a convoluted mess. It needs a
1989 # FIXME: This function has become a convoluted mess. It needs a
1992 # ground-up rewrite with clean, simple logic.
1990 # ground-up rewrite with clean, simple logic.
1993
1991
1994 def make_filename(arg):
1992 def make_filename(arg):
1995 "Make a filename from the given args"
1993 "Make a filename from the given args"
1996 try:
1994 try:
1997 filename = get_py_filename(arg)
1995 filename = get_py_filename(arg)
1998 except IOError:
1996 except IOError:
1999 if args.endswith('.py'):
1997 if args.endswith('.py'):
2000 filename = arg
1998 filename = arg
2001 else:
1999 else:
2002 filename = None
2000 filename = None
2003 return filename
2001 return filename
2004
2002
2005 # custom exceptions
2003 # custom exceptions
2006 class DataIsObject(Exception): pass
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 # Set a few locals from the options for convenience:
2010 # Set a few locals from the options for convenience:
2010 opts_p = opts.has_key('p')
2011 opts_p = opts.has_key('p')
2011 opts_r = opts.has_key('r')
2012 opts_r = opts.has_key('r')
2012
2013
2013 # Default line number value
2014 # Default line number value
2014 lineno = None
2015 lineno = None
2015 if opts_p:
2016 if opts_p:
2016 args = '_%s' % last_call[0]
2017 args = '_%s' % last_call[0]
2017 if not self.shell.user_ns.has_key(args):
2018 if not self.shell.user_ns.has_key(args):
2018 args = last_call[1]
2019 args = last_call[1]
2019
2020
2020 # use last_call to remember the state of the previous call, but don't
2021 # use last_call to remember the state of the previous call, but don't
2021 # let it be clobbered by successive '-p' calls.
2022 # let it be clobbered by successive '-p' calls.
2022 try:
2023 try:
2023 last_call[0] = self.shell.outputcache.prompt_count
2024 last_call[0] = self.shell.outputcache.prompt_count
2024 if not opts_p:
2025 if not opts_p:
2025 last_call[1] = parameter_s
2026 last_call[1] = parameter_s
2026 except:
2027 except:
2027 pass
2028 pass
2028
2029
2029 # by default this is done with temp files, except when the given
2030 # by default this is done with temp files, except when the given
2030 # arg is a filename
2031 # arg is a filename
2031 use_temp = 1
2032 use_temp = 1
2032
2033
2033 if re.match(r'\d',args):
2034 if re.match(r'\d',args):
2034 # Mode where user specifies ranges of lines, like in %macro.
2035 # Mode where user specifies ranges of lines, like in %macro.
2035 # This means that you can't edit files whose names begin with
2036 # This means that you can't edit files whose names begin with
2036 # numbers this way. Tough.
2037 # numbers this way. Tough.
2037 ranges = args.split()
2038 ranges = args.split()
2038 data = ''.join(self.extract_input_slices(ranges,opts_r))
2039 data = ''.join(self.extract_input_slices(ranges,opts_r))
2039 elif args.endswith('.py'):
2040 elif args.endswith('.py'):
2040 filename = make_filename(args)
2041 filename = make_filename(args)
2041 data = ''
2042 data = ''
2042 use_temp = 0
2043 use_temp = 0
2043 elif args:
2044 elif args:
2044 try:
2045 try:
2045 # Load the parameter given as a variable. If not a string,
2046 # Load the parameter given as a variable. If not a string,
2046 # process it as an object instead (below)
2047 # process it as an object instead (below)
2047
2048
2048 #print '*** args',args,'type',type(args) # dbg
2049 #print '*** args',args,'type',type(args) # dbg
2049 data = eval(args,self.shell.user_ns)
2050 data = eval(args,self.shell.user_ns)
2050 if not type(data) in StringTypes:
2051 if not type(data) in StringTypes:
2051 raise DataIsObject
2052 raise DataIsObject
2052
2053
2053 except (NameError,SyntaxError):
2054 except (NameError,SyntaxError):
2054 # given argument is not a variable, try as a filename
2055 # given argument is not a variable, try as a filename
2055 filename = make_filename(args)
2056 filename = make_filename(args)
2056 if filename is None:
2057 if filename is None:
2057 warn("Argument given (%s) can't be found as a variable "
2058 warn("Argument given (%s) can't be found as a variable "
2058 "or as a filename." % args)
2059 "or as a filename." % args)
2059 return
2060 return
2060
2061
2061 data = ''
2062 data = ''
2062 use_temp = 0
2063 use_temp = 0
2063 except DataIsObject:
2064 except DataIsObject:
2064
2065
2065 # macros have a special edit function
2066 # macros have a special edit function
2066 if isinstance(data,Macro):
2067 if isinstance(data,Macro):
2067 self._edit_macro(args,data)
2068 self._edit_macro(args,data)
2068 return
2069 return
2069
2070
2070 # For objects, try to edit the file where they are defined
2071 # For objects, try to edit the file where they are defined
2071 try:
2072 try:
2072 filename = inspect.getabsfile(data)
2073 filename = inspect.getabsfile(data)
2073 datafile = 1
2074 datafile = 1
2074 except TypeError:
2075 except TypeError:
2075 filename = make_filename(args)
2076 filename = make_filename(args)
2076 datafile = 1
2077 datafile = 1
2077 warn('Could not find file where `%s` is defined.\n'
2078 warn('Could not find file where `%s` is defined.\n'
2078 'Opening a file named `%s`' % (args,filename))
2079 'Opening a file named `%s`' % (args,filename))
2079 # Now, make sure we can actually read the source (if it was in
2080 # Now, make sure we can actually read the source (if it was in
2080 # a temp file it's gone by now).
2081 # a temp file it's gone by now).
2081 if datafile:
2082 if datafile:
2082 try:
2083 try:
2083 lineno = inspect.getsourcelines(data)[1]
2084 lineno = inspect.getsourcelines(data)[1]
2084 except IOError:
2085 except IOError:
2085 filename = make_filename(args)
2086 filename = make_filename(args)
2086 if filename is None:
2087 if filename is None:
2087 warn('The file `%s` where `%s` was defined cannot '
2088 warn('The file `%s` where `%s` was defined cannot '
2088 'be read.' % (filename,data))
2089 'be read.' % (filename,data))
2089 return
2090 return
2090 use_temp = 0
2091 use_temp = 0
2091 else:
2092 else:
2092 data = ''
2093 data = ''
2093
2094
2094 if use_temp:
2095 if use_temp:
2095 filename = self.shell.mktempfile(data)
2096 filename = self.shell.mktempfile(data)
2096 print 'IPython will make a temporary file named:',filename
2097 print 'IPython will make a temporary file named:',filename
2097
2098
2098 # do actual editing here
2099 # do actual editing here
2099 print 'Editing...',
2100 print 'Editing...',
2100 sys.stdout.flush()
2101 sys.stdout.flush()
2101 self.shell.hooks.editor(filename,lineno)
2102 self.shell.hooks.editor(filename,lineno)
2102 if opts.has_key('x'): # -x prevents actual execution
2103 if opts.has_key('x'): # -x prevents actual execution
2103 print
2104 print
2104 else:
2105 else:
2105 print 'done. Executing edited code...'
2106 print 'done. Executing edited code...'
2106 if opts_r:
2107 if opts_r:
2107 self.shell.runlines(file_read(filename))
2108 self.shell.runlines(file_read(filename))
2108 else:
2109 else:
2109 self.shell.safe_execfile(filename,self.shell.user_ns)
2110 self.shell.safe_execfile(filename,self.shell.user_ns)
2110 if use_temp:
2111 if use_temp:
2111 try:
2112 try:
2112 return open(filename).read()
2113 return open(filename).read()
2113 except IOError,msg:
2114 except IOError,msg:
2114 if msg.filename == filename:
2115 if msg.filename == filename:
2115 warn('File not found. Did you forget to save?')
2116 warn('File not found. Did you forget to save?')
2116 return
2117 return
2117 else:
2118 else:
2118 self.shell.showtraceback()
2119 self.shell.showtraceback()
2119
2120
2120 def magic_xmode(self,parameter_s = ''):
2121 def magic_xmode(self,parameter_s = ''):
2121 """Switch modes for the exception handlers.
2122 """Switch modes for the exception handlers.
2122
2123
2123 Valid modes: Plain, Context and Verbose.
2124 Valid modes: Plain, Context and Verbose.
2124
2125
2125 If called without arguments, acts as a toggle."""
2126 If called without arguments, acts as a toggle."""
2126
2127
2127 def xmode_switch_err(name):
2128 def xmode_switch_err(name):
2128 warn('Error changing %s exception modes.\n%s' %
2129 warn('Error changing %s exception modes.\n%s' %
2129 (name,sys.exc_info()[1]))
2130 (name,sys.exc_info()[1]))
2130
2131
2131 shell = self.shell
2132 shell = self.shell
2132 new_mode = parameter_s.strip().capitalize()
2133 new_mode = parameter_s.strip().capitalize()
2133 try:
2134 try:
2134 shell.InteractiveTB.set_mode(mode=new_mode)
2135 shell.InteractiveTB.set_mode(mode=new_mode)
2135 print 'Exception reporting mode:',shell.InteractiveTB.mode
2136 print 'Exception reporting mode:',shell.InteractiveTB.mode
2136 except:
2137 except:
2137 xmode_switch_err('user')
2138 xmode_switch_err('user')
2138
2139
2139 # threaded shells use a special handler in sys.excepthook
2140 # threaded shells use a special handler in sys.excepthook
2140 if shell.isthreaded:
2141 if shell.isthreaded:
2141 try:
2142 try:
2142 shell.sys_excepthook.set_mode(mode=new_mode)
2143 shell.sys_excepthook.set_mode(mode=new_mode)
2143 except:
2144 except:
2144 xmode_switch_err('threaded')
2145 xmode_switch_err('threaded')
2145
2146
2146 def magic_colors(self,parameter_s = ''):
2147 def magic_colors(self,parameter_s = ''):
2147 """Switch color scheme for prompts, info system and exception handlers.
2148 """Switch color scheme for prompts, info system and exception handlers.
2148
2149
2149 Currently implemented schemes: NoColor, Linux, LightBG.
2150 Currently implemented schemes: NoColor, Linux, LightBG.
2150
2151
2151 Color scheme names are not case-sensitive."""
2152 Color scheme names are not case-sensitive."""
2152
2153
2153 def color_switch_err(name):
2154 def color_switch_err(name):
2154 warn('Error changing %s color schemes.\n%s' %
2155 warn('Error changing %s color schemes.\n%s' %
2155 (name,sys.exc_info()[1]))
2156 (name,sys.exc_info()[1]))
2156
2157
2157
2158
2158 new_scheme = parameter_s.strip()
2159 new_scheme = parameter_s.strip()
2159 if not new_scheme:
2160 if not new_scheme:
2160 print 'You must specify a color scheme.'
2161 print 'You must specify a color scheme.'
2161 return
2162 return
2162 import IPython.rlineimpl as readline
2163 import IPython.rlineimpl as readline
2163 if not readline.have_readline:
2164 if not readline.have_readline:
2164 msg = """\
2165 msg = """\
2165 Proper color support under MS Windows requires Gary Bishop's readline library.
2166 Proper color support under MS Windows requires Gary Bishop's readline library.
2166 You can find it at:
2167 You can find it at:
2167 http://sourceforge.net/projects/uncpythontools
2168 http://sourceforge.net/projects/uncpythontools
2168 Gary's readline needs the ctypes module, from:
2169 Gary's readline needs the ctypes module, from:
2169 http://starship.python.net/crew/theller/ctypes
2170 http://starship.python.net/crew/theller/ctypes
2170
2171
2171 Defaulting color scheme to 'NoColor'"""
2172 Defaulting color scheme to 'NoColor'"""
2172 new_scheme = 'NoColor'
2173 new_scheme = 'NoColor'
2173 warn(msg)
2174 warn(msg)
2174 # local shortcut
2175 # local shortcut
2175 shell = self.shell
2176 shell = self.shell
2176
2177
2177 # Set prompt colors
2178 # Set prompt colors
2178 try:
2179 try:
2179 shell.outputcache.set_colors(new_scheme)
2180 shell.outputcache.set_colors(new_scheme)
2180 except:
2181 except:
2181 color_switch_err('prompt')
2182 color_switch_err('prompt')
2182 else:
2183 else:
2183 shell.rc.colors = \
2184 shell.rc.colors = \
2184 shell.outputcache.color_table.active_scheme_name
2185 shell.outputcache.color_table.active_scheme_name
2185 # Set exception colors
2186 # Set exception colors
2186 try:
2187 try:
2187 shell.InteractiveTB.set_colors(scheme = new_scheme)
2188 shell.InteractiveTB.set_colors(scheme = new_scheme)
2188 shell.SyntaxTB.set_colors(scheme = new_scheme)
2189 shell.SyntaxTB.set_colors(scheme = new_scheme)
2189 except:
2190 except:
2190 color_switch_err('exception')
2191 color_switch_err('exception')
2191
2192
2192 # threaded shells use a verbose traceback in sys.excepthook
2193 # threaded shells use a verbose traceback in sys.excepthook
2193 if shell.isthreaded:
2194 if shell.isthreaded:
2194 try:
2195 try:
2195 shell.sys_excepthook.set_colors(scheme=new_scheme)
2196 shell.sys_excepthook.set_colors(scheme=new_scheme)
2196 except:
2197 except:
2197 color_switch_err('system exception handler')
2198 color_switch_err('system exception handler')
2198
2199
2199 # Set info (for 'object?') colors
2200 # Set info (for 'object?') colors
2200 if shell.rc.color_info:
2201 if shell.rc.color_info:
2201 try:
2202 try:
2202 shell.inspector.set_active_scheme(new_scheme)
2203 shell.inspector.set_active_scheme(new_scheme)
2203 except:
2204 except:
2204 color_switch_err('object inspector')
2205 color_switch_err('object inspector')
2205 else:
2206 else:
2206 shell.inspector.set_active_scheme('NoColor')
2207 shell.inspector.set_active_scheme('NoColor')
2207
2208
2208 def magic_color_info(self,parameter_s = ''):
2209 def magic_color_info(self,parameter_s = ''):
2209 """Toggle color_info.
2210 """Toggle color_info.
2210
2211
2211 The color_info configuration parameter controls whether colors are
2212 The color_info configuration parameter controls whether colors are
2212 used for displaying object details (by things like %psource, %pfile or
2213 used for displaying object details (by things like %psource, %pfile or
2213 the '?' system). This function toggles this value with each call.
2214 the '?' system). This function toggles this value with each call.
2214
2215
2215 Note that unless you have a fairly recent pager (less works better
2216 Note that unless you have a fairly recent pager (less works better
2216 than more) in your system, using colored object information displays
2217 than more) in your system, using colored object information displays
2217 will not work properly. Test it and see."""
2218 will not work properly. Test it and see."""
2218
2219
2219 self.shell.rc.color_info = 1 - self.shell.rc.color_info
2220 self.shell.rc.color_info = 1 - self.shell.rc.color_info
2220 self.magic_colors(self.shell.rc.colors)
2221 self.magic_colors(self.shell.rc.colors)
2221 print 'Object introspection functions have now coloring:',
2222 print 'Object introspection functions have now coloring:',
2222 print ['OFF','ON'][self.shell.rc.color_info]
2223 print ['OFF','ON'][self.shell.rc.color_info]
2223
2224
2224 def magic_Pprint(self, parameter_s=''):
2225 def magic_Pprint(self, parameter_s=''):
2225 """Toggle pretty printing on/off."""
2226 """Toggle pretty printing on/off."""
2226
2227
2227 self.shell.rc.pprint = 1 - self.shell.rc.pprint
2228 self.shell.rc.pprint = 1 - self.shell.rc.pprint
2228 print 'Pretty printing has been turned', \
2229 print 'Pretty printing has been turned', \
2229 ['OFF','ON'][self.shell.rc.pprint]
2230 ['OFF','ON'][self.shell.rc.pprint]
2230
2231
2231 def magic_exit(self, parameter_s=''):
2232 def magic_exit(self, parameter_s=''):
2232 """Exit IPython, confirming if configured to do so.
2233 """Exit IPython, confirming if configured to do so.
2233
2234
2234 You can configure whether IPython asks for confirmation upon exit by
2235 You can configure whether IPython asks for confirmation upon exit by
2235 setting the confirm_exit flag in the ipythonrc file."""
2236 setting the confirm_exit flag in the ipythonrc file."""
2236
2237
2237 self.shell.exit()
2238 self.shell.exit()
2238
2239
2239 def magic_quit(self, parameter_s=''):
2240 def magic_quit(self, parameter_s=''):
2240 """Exit IPython, confirming if configured to do so (like %exit)"""
2241 """Exit IPython, confirming if configured to do so (like %exit)"""
2241
2242
2242 self.shell.exit()
2243 self.shell.exit()
2243
2244
2244 def magic_Exit(self, parameter_s=''):
2245 def magic_Exit(self, parameter_s=''):
2245 """Exit IPython without confirmation."""
2246 """Exit IPython without confirmation."""
2246
2247
2247 self.shell.exit_now = True
2248 self.shell.exit_now = True
2248
2249
2249 def magic_Quit(self, parameter_s=''):
2250 def magic_Quit(self, parameter_s=''):
2250 """Exit IPython without confirmation (like %Exit)."""
2251 """Exit IPython without confirmation (like %Exit)."""
2251
2252
2252 self.shell.exit_now = True
2253 self.shell.exit_now = True
2253
2254
2254 #......................................................................
2255 #......................................................................
2255 # Functions to implement unix shell-type things
2256 # Functions to implement unix shell-type things
2256
2257
2257 def magic_alias(self, parameter_s = ''):
2258 def magic_alias(self, parameter_s = ''):
2258 """Define an alias for a system command.
2259 """Define an alias for a system command.
2259
2260
2260 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2261 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2261
2262
2262 Then, typing 'alias_name params' will execute the system command 'cmd
2263 Then, typing 'alias_name params' will execute the system command 'cmd
2263 params' (from your underlying operating system).
2264 params' (from your underlying operating system).
2264
2265
2265 Aliases have lower precedence than magic functions and Python normal
2266 Aliases have lower precedence than magic functions and Python normal
2266 variables, so if 'foo' is both a Python variable and an alias, the
2267 variables, so if 'foo' is both a Python variable and an alias, the
2267 alias can not be executed until 'del foo' removes the Python variable.
2268 alias can not be executed until 'del foo' removes the Python variable.
2268
2269
2269 You can use the %l specifier in an alias definition to represent the
2270 You can use the %l specifier in an alias definition to represent the
2270 whole line when the alias is called. For example:
2271 whole line when the alias is called. For example:
2271
2272
2272 In [2]: alias all echo "Input in brackets: <%l>"\\
2273 In [2]: alias all echo "Input in brackets: <%l>"\\
2273 In [3]: all hello world\\
2274 In [3]: all hello world\\
2274 Input in brackets: <hello world>
2275 Input in brackets: <hello world>
2275
2276
2276 You can also define aliases with parameters using %s specifiers (one
2277 You can also define aliases with parameters using %s specifiers (one
2277 per parameter):
2278 per parameter):
2278
2279
2279 In [1]: alias parts echo first %s second %s\\
2280 In [1]: alias parts echo first %s second %s\\
2280 In [2]: %parts A B\\
2281 In [2]: %parts A B\\
2281 first A second B\\
2282 first A second B\\
2282 In [3]: %parts A\\
2283 In [3]: %parts A\\
2283 Incorrect number of arguments: 2 expected.\\
2284 Incorrect number of arguments: 2 expected.\\
2284 parts is an alias to: 'echo first %s second %s'
2285 parts is an alias to: 'echo first %s second %s'
2285
2286
2286 Note that %l and %s are mutually exclusive. You can only use one or
2287 Note that %l and %s are mutually exclusive. You can only use one or
2287 the other in your aliases.
2288 the other in your aliases.
2288
2289
2289 Aliases expand Python variables just like system calls using ! or !!
2290 Aliases expand Python variables just like system calls using ! or !!
2290 do: all expressions prefixed with '$' get expanded. For details of
2291 do: all expressions prefixed with '$' get expanded. For details of
2291 the semantic rules, see PEP-215:
2292 the semantic rules, see PEP-215:
2292 http://www.python.org/peps/pep-0215.html. This is the library used by
2293 http://www.python.org/peps/pep-0215.html. This is the library used by
2293 IPython for variable expansion. If you want to access a true shell
2294 IPython for variable expansion. If you want to access a true shell
2294 variable, an extra $ is necessary to prevent its expansion by IPython:
2295 variable, an extra $ is necessary to prevent its expansion by IPython:
2295
2296
2296 In [6]: alias show echo\\
2297 In [6]: alias show echo\\
2297 In [7]: PATH='A Python string'\\
2298 In [7]: PATH='A Python string'\\
2298 In [8]: show $PATH\\
2299 In [8]: show $PATH\\
2299 A Python string\\
2300 A Python string\\
2300 In [9]: show $$PATH\\
2301 In [9]: show $$PATH\\
2301 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2302 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2302
2303
2303 You can use the alias facility to acess all of $PATH. See the %rehash
2304 You can use the alias facility to acess all of $PATH. See the %rehash
2304 and %rehashx functions, which automatically create aliases for the
2305 and %rehashx functions, which automatically create aliases for the
2305 contents of your $PATH.
2306 contents of your $PATH.
2306
2307
2307 If called with no parameters, %alias prints the current alias table."""
2308 If called with no parameters, %alias prints the current alias table."""
2308
2309
2309 par = parameter_s.strip()
2310 par = parameter_s.strip()
2310 if not par:
2311 if not par:
2311 if self.shell.rc.automagic:
2312 if self.shell.rc.automagic:
2312 prechar = ''
2313 prechar = ''
2313 else:
2314 else:
2314 prechar = self.shell.ESC_MAGIC
2315 prechar = self.shell.ESC_MAGIC
2315 #print 'Alias\t\tSystem Command\n'+'-'*30
2316 #print 'Alias\t\tSystem Command\n'+'-'*30
2316 atab = self.shell.alias_table
2317 atab = self.shell.alias_table
2317 aliases = atab.keys()
2318 aliases = atab.keys()
2318 aliases.sort()
2319 aliases.sort()
2319 res = []
2320 res = []
2320 for alias in aliases:
2321 for alias in aliases:
2321 res.append((alias, atab[alias][1]))
2322 res.append((alias, atab[alias][1]))
2322 print "Total number of aliases:",len(aliases)
2323 print "Total number of aliases:",len(aliases)
2323 return res
2324 return res
2324 try:
2325 try:
2325 alias,cmd = par.split(None,1)
2326 alias,cmd = par.split(None,1)
2326 except:
2327 except:
2327 print OInspect.getdoc(self.magic_alias)
2328 print OInspect.getdoc(self.magic_alias)
2328 else:
2329 else:
2329 nargs = cmd.count('%s')
2330 nargs = cmd.count('%s')
2330 if nargs>0 and cmd.find('%l')>=0:
2331 if nargs>0 and cmd.find('%l')>=0:
2331 error('The %s and %l specifiers are mutually exclusive '
2332 error('The %s and %l specifiers are mutually exclusive '
2332 'in alias definitions.')
2333 'in alias definitions.')
2333 else: # all looks OK
2334 else: # all looks OK
2334 self.shell.alias_table[alias] = (nargs,cmd)
2335 self.shell.alias_table[alias] = (nargs,cmd)
2335 self.shell.alias_table_validate(verbose=0)
2336 self.shell.alias_table_validate(verbose=0)
2336 # end magic_alias
2337 # end magic_alias
2337
2338
2338 def magic_unalias(self, parameter_s = ''):
2339 def magic_unalias(self, parameter_s = ''):
2339 """Remove an alias"""
2340 """Remove an alias"""
2340
2341
2341 aname = parameter_s.strip()
2342 aname = parameter_s.strip()
2342 if aname in self.shell.alias_table:
2343 if aname in self.shell.alias_table:
2343 del self.shell.alias_table[aname]
2344 del self.shell.alias_table[aname]
2344
2345
2345 def magic_rehash(self, parameter_s = ''):
2346 def magic_rehash(self, parameter_s = ''):
2346 """Update the alias table with all entries in $PATH.
2347 """Update the alias table with all entries in $PATH.
2347
2348
2348 This version does no checks on execute permissions or whether the
2349 This version does no checks on execute permissions or whether the
2349 contents of $PATH are truly files (instead of directories or something
2350 contents of $PATH are truly files (instead of directories or something
2350 else). For such a safer (but slower) version, use %rehashx."""
2351 else). For such a safer (but slower) version, use %rehashx."""
2351
2352
2352 # This function (and rehashx) manipulate the alias_table directly
2353 # This function (and rehashx) manipulate the alias_table directly
2353 # rather than calling magic_alias, for speed reasons. A rehash on a
2354 # rather than calling magic_alias, for speed reasons. A rehash on a
2354 # typical Linux box involves several thousand entries, so efficiency
2355 # typical Linux box involves several thousand entries, so efficiency
2355 # here is a top concern.
2356 # here is a top concern.
2356
2357
2357 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2358 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2358 alias_table = self.shell.alias_table
2359 alias_table = self.shell.alias_table
2359 for pdir in path:
2360 for pdir in path:
2360 for ff in os.listdir(pdir):
2361 for ff in os.listdir(pdir):
2361 # each entry in the alias table must be (N,name), where
2362 # each entry in the alias table must be (N,name), where
2362 # N is the number of positional arguments of the alias.
2363 # N is the number of positional arguments of the alias.
2363 alias_table[ff] = (0,ff)
2364 alias_table[ff] = (0,ff)
2364 # Make sure the alias table doesn't contain keywords or builtins
2365 # Make sure the alias table doesn't contain keywords or builtins
2365 self.shell.alias_table_validate()
2366 self.shell.alias_table_validate()
2366 # Call again init_auto_alias() so we get 'rm -i' and other modified
2367 # Call again init_auto_alias() so we get 'rm -i' and other modified
2367 # aliases since %rehash will probably clobber them
2368 # aliases since %rehash will probably clobber them
2368 self.shell.init_auto_alias()
2369 self.shell.init_auto_alias()
2369
2370
2370 def magic_rehashx(self, parameter_s = ''):
2371 def magic_rehashx(self, parameter_s = ''):
2371 """Update the alias table with all executable files in $PATH.
2372 """Update the alias table with all executable files in $PATH.
2372
2373
2373 This version explicitly checks that every entry in $PATH is a file
2374 This version explicitly checks that every entry in $PATH is a file
2374 with execute access (os.X_OK), so it is much slower than %rehash.
2375 with execute access (os.X_OK), so it is much slower than %rehash.
2375
2376
2376 Under Windows, it checks executability as a match agains a
2377 Under Windows, it checks executability as a match agains a
2377 '|'-separated string of extensions, stored in the IPython config
2378 '|'-separated string of extensions, stored in the IPython config
2378 variable win_exec_ext. This defaults to 'exe|com|bat'. """
2379 variable win_exec_ext. This defaults to 'exe|com|bat'. """
2379
2380
2380 path = [os.path.abspath(os.path.expanduser(p)) for p in
2381 path = [os.path.abspath(os.path.expanduser(p)) for p in
2381 os.environ['PATH'].split(os.pathsep)]
2382 os.environ['PATH'].split(os.pathsep)]
2382 path = filter(os.path.isdir,path)
2383 path = filter(os.path.isdir,path)
2383
2384
2384 alias_table = self.shell.alias_table
2385 alias_table = self.shell.alias_table
2385 syscmdlist = []
2386 syscmdlist = []
2386 if os.name == 'posix':
2387 if os.name == 'posix':
2387 isexec = lambda fname:os.path.isfile(fname) and \
2388 isexec = lambda fname:os.path.isfile(fname) and \
2388 os.access(fname,os.X_OK)
2389 os.access(fname,os.X_OK)
2389 else:
2390 else:
2390
2391
2391 try:
2392 try:
2392 winext = os.environ['pathext'].replace(';','|').replace('.','')
2393 winext = os.environ['pathext'].replace(';','|').replace('.','')
2393 except KeyError:
2394 except KeyError:
2394 winext = 'exe|com|bat'
2395 winext = 'exe|com|bat'
2395
2396
2396 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2397 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2397 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2398 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2398 savedir = os.getcwd()
2399 savedir = os.getcwd()
2399 try:
2400 try:
2400 # write the whole loop for posix/Windows so we don't have an if in
2401 # write the whole loop for posix/Windows so we don't have an if in
2401 # the innermost part
2402 # the innermost part
2402 if os.name == 'posix':
2403 if os.name == 'posix':
2403 for pdir in path:
2404 for pdir in path:
2404 os.chdir(pdir)
2405 os.chdir(pdir)
2405 for ff in os.listdir(pdir):
2406 for ff in os.listdir(pdir):
2406 if isexec(ff):
2407 if isexec(ff):
2407 # each entry in the alias table must be (N,name),
2408 # each entry in the alias table must be (N,name),
2408 # where N is the number of positional arguments of the
2409 # where N is the number of positional arguments of the
2409 # alias.
2410 # alias.
2410 alias_table[ff] = (0,ff)
2411 alias_table[ff] = (0,ff)
2411 syscmdlist.append(ff)
2412 syscmdlist.append(ff)
2412 else:
2413 else:
2413 for pdir in path:
2414 for pdir in path:
2414 os.chdir(pdir)
2415 os.chdir(pdir)
2415 for ff in os.listdir(pdir):
2416 for ff in os.listdir(pdir):
2416 if isexec(ff):
2417 if isexec(ff):
2417 alias_table[execre.sub(r'\1',ff)] = (0,ff)
2418 alias_table[execre.sub(r'\1',ff)] = (0,ff)
2418 syscmdlist.append(ff)
2419 syscmdlist.append(ff)
2419 # Make sure the alias table doesn't contain keywords or builtins
2420 # Make sure the alias table doesn't contain keywords or builtins
2420 self.shell.alias_table_validate()
2421 self.shell.alias_table_validate()
2421 # Call again init_auto_alias() so we get 'rm -i' and other
2422 # Call again init_auto_alias() so we get 'rm -i' and other
2422 # modified aliases since %rehashx will probably clobber them
2423 # modified aliases since %rehashx will probably clobber them
2423 self.shell.init_auto_alias()
2424 self.shell.init_auto_alias()
2424 db = self.getapi().getdb()
2425 db = self.getapi().db
2425 db['syscmdlist'] = syscmdlist
2426 db['syscmdlist'] = syscmdlist
2426 finally:
2427 finally:
2427 os.chdir(savedir)
2428 os.chdir(savedir)
2428
2429
2429 def magic_pwd(self, parameter_s = ''):
2430 def magic_pwd(self, parameter_s = ''):
2430 """Return the current working directory path."""
2431 """Return the current working directory path."""
2431 return os.getcwd()
2432 return os.getcwd()
2432
2433
2433 def magic_cd(self, parameter_s=''):
2434 def magic_cd(self, parameter_s=''):
2434 """Change the current working directory.
2435 """Change the current working directory.
2435
2436
2436 This command automatically maintains an internal list of directories
2437 This command automatically maintains an internal list of directories
2437 you visit during your IPython session, in the variable _dh. The
2438 you visit during your IPython session, in the variable _dh. The
2438 command %dhist shows this history nicely formatted.
2439 command %dhist shows this history nicely formatted.
2439
2440
2440 Usage:
2441 Usage:
2441
2442
2442 cd 'dir': changes to directory 'dir'.
2443 cd 'dir': changes to directory 'dir'.
2443
2444
2444 cd -: changes to the last visited directory.
2445 cd -: changes to the last visited directory.
2445
2446
2446 cd -<n>: changes to the n-th directory in the directory history.
2447 cd -<n>: changes to the n-th directory in the directory history.
2447
2448
2448 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2449 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2449 (note: cd <bookmark_name> is enough if there is no
2450 (note: cd <bookmark_name> is enough if there is no
2450 directory <bookmark_name>, but a bookmark with the name exists.)
2451 directory <bookmark_name>, but a bookmark with the name exists.)
2451
2452
2452 Options:
2453 Options:
2453
2454
2454 -q: quiet. Do not print the working directory after the cd command is
2455 -q: quiet. Do not print the working directory after the cd command is
2455 executed. By default IPython's cd command does print this directory,
2456 executed. By default IPython's cd command does print this directory,
2456 since the default prompts do not display path information.
2457 since the default prompts do not display path information.
2457
2458
2458 Note that !cd doesn't work for this purpose because the shell where
2459 Note that !cd doesn't work for this purpose because the shell where
2459 !command runs is immediately discarded after executing 'command'."""
2460 !command runs is immediately discarded after executing 'command'."""
2460
2461
2461 parameter_s = parameter_s.strip()
2462 parameter_s = parameter_s.strip()
2462 #bkms = self.shell.persist.get("bookmarks",{})
2463 #bkms = self.shell.persist.get("bookmarks",{})
2463
2464
2464 numcd = re.match(r'(-)(\d+)$',parameter_s)
2465 numcd = re.match(r'(-)(\d+)$',parameter_s)
2465 # jump in directory history by number
2466 # jump in directory history by number
2466 if numcd:
2467 if numcd:
2467 nn = int(numcd.group(2))
2468 nn = int(numcd.group(2))
2468 try:
2469 try:
2469 ps = self.shell.user_ns['_dh'][nn]
2470 ps = self.shell.user_ns['_dh'][nn]
2470 except IndexError:
2471 except IndexError:
2471 print 'The requested directory does not exist in history.'
2472 print 'The requested directory does not exist in history.'
2472 return
2473 return
2473 else:
2474 else:
2474 opts = {}
2475 opts = {}
2475 else:
2476 else:
2476 #turn all non-space-escaping backslashes to slashes,
2477 #turn all non-space-escaping backslashes to slashes,
2477 # for c:\windows\directory\names\
2478 # for c:\windows\directory\names\
2478 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2479 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2479 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2480 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2480 # jump to previous
2481 # jump to previous
2481 if ps == '-':
2482 if ps == '-':
2482 try:
2483 try:
2483 ps = self.shell.user_ns['_dh'][-2]
2484 ps = self.shell.user_ns['_dh'][-2]
2484 except IndexError:
2485 except IndexError:
2485 print 'No previous directory to change to.'
2486 print 'No previous directory to change to.'
2486 return
2487 return
2487 # jump to bookmark if needed
2488 # jump to bookmark if needed
2488 else:
2489 else:
2489 if not os.path.isdir(ps) or opts.has_key('b'):
2490 if not os.path.isdir(ps) or opts.has_key('b'):
2490 bkms = self.db.get('bookmarks', {})
2491 bkms = self.db.get('bookmarks', {})
2491
2492
2492 if bkms.has_key(ps):
2493 if bkms.has_key(ps):
2493 target = bkms[ps]
2494 target = bkms[ps]
2494 print '(bookmark:%s) -> %s' % (ps,target)
2495 print '(bookmark:%s) -> %s' % (ps,target)
2495 ps = target
2496 ps = target
2496 else:
2497 else:
2497 if opts.has_key('b'):
2498 if opts.has_key('b'):
2498 error("Bookmark '%s' not found. "
2499 error("Bookmark '%s' not found. "
2499 "Use '%%bookmark -l' to see your bookmarks." % ps)
2500 "Use '%%bookmark -l' to see your bookmarks." % ps)
2500 return
2501 return
2501
2502
2502 # at this point ps should point to the target dir
2503 # at this point ps should point to the target dir
2503 if ps:
2504 if ps:
2504 try:
2505 try:
2505 os.chdir(os.path.expanduser(ps))
2506 os.chdir(os.path.expanduser(ps))
2506 ttitle = ("IPy:" + (
2507 ttitle = ("IPy:" + (
2507 os.getcwd() == '/' and '/' or os.path.basename(os.getcwd())))
2508 os.getcwd() == '/' and '/' or os.path.basename(os.getcwd())))
2508 platutils.set_term_title(ttitle)
2509 platutils.set_term_title(ttitle)
2509 except OSError:
2510 except OSError:
2510 print sys.exc_info()[1]
2511 print sys.exc_info()[1]
2511 else:
2512 else:
2512 self.shell.user_ns['_dh'].append(os.getcwd())
2513 self.shell.user_ns['_dh'].append(os.getcwd())
2513 else:
2514 else:
2514 os.chdir(self.shell.home_dir)
2515 os.chdir(self.shell.home_dir)
2515 platutils.set_term_title("IPy:~")
2516 platutils.set_term_title("IPy:~")
2516 self.shell.user_ns['_dh'].append(os.getcwd())
2517 self.shell.user_ns['_dh'].append(os.getcwd())
2517 if not 'q' in opts:
2518 if not 'q' in opts:
2518 print self.shell.user_ns['_dh'][-1]
2519 print self.shell.user_ns['_dh'][-1]
2519
2520
2520 def magic_dhist(self, parameter_s=''):
2521 def magic_dhist(self, parameter_s=''):
2521 """Print your history of visited directories.
2522 """Print your history of visited directories.
2522
2523
2523 %dhist -> print full history\\
2524 %dhist -> print full history\\
2524 %dhist n -> print last n entries only\\
2525 %dhist n -> print last n entries only\\
2525 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2526 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2526
2527
2527 This history is automatically maintained by the %cd command, and
2528 This history is automatically maintained by the %cd command, and
2528 always available as the global list variable _dh. You can use %cd -<n>
2529 always available as the global list variable _dh. You can use %cd -<n>
2529 to go to directory number <n>."""
2530 to go to directory number <n>."""
2530
2531
2531 dh = self.shell.user_ns['_dh']
2532 dh = self.shell.user_ns['_dh']
2532 if parameter_s:
2533 if parameter_s:
2533 try:
2534 try:
2534 args = map(int,parameter_s.split())
2535 args = map(int,parameter_s.split())
2535 except:
2536 except:
2536 self.arg_err(Magic.magic_dhist)
2537 self.arg_err(Magic.magic_dhist)
2537 return
2538 return
2538 if len(args) == 1:
2539 if len(args) == 1:
2539 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2540 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2540 elif len(args) == 2:
2541 elif len(args) == 2:
2541 ini,fin = args
2542 ini,fin = args
2542 else:
2543 else:
2543 self.arg_err(Magic.magic_dhist)
2544 self.arg_err(Magic.magic_dhist)
2544 return
2545 return
2545 else:
2546 else:
2546 ini,fin = 0,len(dh)
2547 ini,fin = 0,len(dh)
2547 nlprint(dh,
2548 nlprint(dh,
2548 header = 'Directory history (kept in _dh)',
2549 header = 'Directory history (kept in _dh)',
2549 start=ini,stop=fin)
2550 start=ini,stop=fin)
2550
2551
2551 def magic_env(self, parameter_s=''):
2552 def magic_env(self, parameter_s=''):
2552 """List environment variables."""
2553 """List environment variables."""
2553
2554
2554 return os.environ.data
2555 return os.environ.data
2555
2556
2556 def magic_pushd(self, parameter_s=''):
2557 def magic_pushd(self, parameter_s=''):
2557 """Place the current dir on stack and change directory.
2558 """Place the current dir on stack and change directory.
2558
2559
2559 Usage:\\
2560 Usage:\\
2560 %pushd ['dirname']
2561 %pushd ['dirname']
2561
2562
2562 %pushd with no arguments does a %pushd to your home directory.
2563 %pushd with no arguments does a %pushd to your home directory.
2563 """
2564 """
2564 if parameter_s == '': parameter_s = '~'
2565 if parameter_s == '': parameter_s = '~'
2565 dir_s = self.shell.dir_stack
2566 dir_s = self.shell.dir_stack
2566 if len(dir_s)>0 and os.path.expanduser(parameter_s) != \
2567 if len(dir_s)>0 and os.path.expanduser(parameter_s) != \
2567 os.path.expanduser(self.shell.dir_stack[0]):
2568 os.path.expanduser(self.shell.dir_stack[0]):
2568 try:
2569 try:
2569 self.magic_cd(parameter_s)
2570 self.magic_cd(parameter_s)
2570 dir_s.insert(0,os.getcwd().replace(self.home_dir,'~'))
2571 dir_s.insert(0,os.getcwd().replace(self.home_dir,'~'))
2571 self.magic_dirs()
2572 self.magic_dirs()
2572 except:
2573 except:
2573 print 'Invalid directory'
2574 print 'Invalid directory'
2574 else:
2575 else:
2575 print 'You are already there!'
2576 print 'You are already there!'
2576
2577
2577 def magic_popd(self, parameter_s=''):
2578 def magic_popd(self, parameter_s=''):
2578 """Change to directory popped off the top of the stack.
2579 """Change to directory popped off the top of the stack.
2579 """
2580 """
2580 if len (self.shell.dir_stack) > 1:
2581 if len (self.shell.dir_stack) > 1:
2581 self.shell.dir_stack.pop(0)
2582 self.shell.dir_stack.pop(0)
2582 self.magic_cd(self.shell.dir_stack[0])
2583 self.magic_cd(self.shell.dir_stack[0])
2583 print self.shell.dir_stack[0]
2584 print self.shell.dir_stack[0]
2584 else:
2585 else:
2585 print "You can't remove the starting directory from the stack:",\
2586 print "You can't remove the starting directory from the stack:",\
2586 self.shell.dir_stack
2587 self.shell.dir_stack
2587
2588
2588 def magic_dirs(self, parameter_s=''):
2589 def magic_dirs(self, parameter_s=''):
2589 """Return the current directory stack."""
2590 """Return the current directory stack."""
2590
2591
2591 return self.shell.dir_stack[:]
2592 return self.shell.dir_stack[:]
2592
2593
2593 def magic_sc(self, parameter_s=''):
2594 def magic_sc(self, parameter_s=''):
2594 """Shell capture - execute a shell command and capture its output.
2595 """Shell capture - execute a shell command and capture its output.
2595
2596
2596 DEPRECATED. Suboptimal, retained for backwards compatibility.
2597 DEPRECATED. Suboptimal, retained for backwards compatibility.
2597
2598
2598 You should use the form 'var = !command' instead. Example:
2599 You should use the form 'var = !command' instead. Example:
2599
2600
2600 "%sc -l myfiles = ls ~" should now be written as
2601 "%sc -l myfiles = ls ~" should now be written as
2601
2602
2602 "myfiles = !ls ~"
2603 "myfiles = !ls ~"
2603
2604
2604 myfiles.s, myfiles.l and myfiles.n still apply as documented
2605 myfiles.s, myfiles.l and myfiles.n still apply as documented
2605 below.
2606 below.
2606
2607
2607 --
2608 --
2608 %sc [options] varname=command
2609 %sc [options] varname=command
2609
2610
2610 IPython will run the given command using commands.getoutput(), and
2611 IPython will run the given command using commands.getoutput(), and
2611 will then update the user's interactive namespace with a variable
2612 will then update the user's interactive namespace with a variable
2612 called varname, containing the value of the call. Your command can
2613 called varname, containing the value of the call. Your command can
2613 contain shell wildcards, pipes, etc.
2614 contain shell wildcards, pipes, etc.
2614
2615
2615 The '=' sign in the syntax is mandatory, and the variable name you
2616 The '=' sign in the syntax is mandatory, and the variable name you
2616 supply must follow Python's standard conventions for valid names.
2617 supply must follow Python's standard conventions for valid names.
2617
2618
2618 (A special format without variable name exists for internal use)
2619 (A special format without variable name exists for internal use)
2619
2620
2620 Options:
2621 Options:
2621
2622
2622 -l: list output. Split the output on newlines into a list before
2623 -l: list output. Split the output on newlines into a list before
2623 assigning it to the given variable. By default the output is stored
2624 assigning it to the given variable. By default the output is stored
2624 as a single string.
2625 as a single string.
2625
2626
2626 -v: verbose. Print the contents of the variable.
2627 -v: verbose. Print the contents of the variable.
2627
2628
2628 In most cases you should not need to split as a list, because the
2629 In most cases you should not need to split as a list, because the
2629 returned value is a special type of string which can automatically
2630 returned value is a special type of string which can automatically
2630 provide its contents either as a list (split on newlines) or as a
2631 provide its contents either as a list (split on newlines) or as a
2631 space-separated string. These are convenient, respectively, either
2632 space-separated string. These are convenient, respectively, either
2632 for sequential processing or to be passed to a shell command.
2633 for sequential processing or to be passed to a shell command.
2633
2634
2634 For example:
2635 For example:
2635
2636
2636 # Capture into variable a
2637 # Capture into variable a
2637 In [9]: sc a=ls *py
2638 In [9]: sc a=ls *py
2638
2639
2639 # a is a string with embedded newlines
2640 # a is a string with embedded newlines
2640 In [10]: a
2641 In [10]: a
2641 Out[10]: 'setup.py\nwin32_manual_post_install.py'
2642 Out[10]: 'setup.py\nwin32_manual_post_install.py'
2642
2643
2643 # which can be seen as a list:
2644 # which can be seen as a list:
2644 In [11]: a.l
2645 In [11]: a.l
2645 Out[11]: ['setup.py', 'win32_manual_post_install.py']
2646 Out[11]: ['setup.py', 'win32_manual_post_install.py']
2646
2647
2647 # or as a whitespace-separated string:
2648 # or as a whitespace-separated string:
2648 In [12]: a.s
2649 In [12]: a.s
2649 Out[12]: 'setup.py win32_manual_post_install.py'
2650 Out[12]: 'setup.py win32_manual_post_install.py'
2650
2651
2651 # a.s is useful to pass as a single command line:
2652 # a.s is useful to pass as a single command line:
2652 In [13]: !wc -l $a.s
2653 In [13]: !wc -l $a.s
2653 146 setup.py
2654 146 setup.py
2654 130 win32_manual_post_install.py
2655 130 win32_manual_post_install.py
2655 276 total
2656 276 total
2656
2657
2657 # while the list form is useful to loop over:
2658 # while the list form is useful to loop over:
2658 In [14]: for f in a.l:
2659 In [14]: for f in a.l:
2659 ....: !wc -l $f
2660 ....: !wc -l $f
2660 ....:
2661 ....:
2661 146 setup.py
2662 146 setup.py
2662 130 win32_manual_post_install.py
2663 130 win32_manual_post_install.py
2663
2664
2664 Similiarly, the lists returned by the -l option are also special, in
2665 Similiarly, the lists returned by the -l option are also special, in
2665 the sense that you can equally invoke the .s attribute on them to
2666 the sense that you can equally invoke the .s attribute on them to
2666 automatically get a whitespace-separated string from their contents:
2667 automatically get a whitespace-separated string from their contents:
2667
2668
2668 In [1]: sc -l b=ls *py
2669 In [1]: sc -l b=ls *py
2669
2670
2670 In [2]: b
2671 In [2]: b
2671 Out[2]: ['setup.py', 'win32_manual_post_install.py']
2672 Out[2]: ['setup.py', 'win32_manual_post_install.py']
2672
2673
2673 In [3]: b.s
2674 In [3]: b.s
2674 Out[3]: 'setup.py win32_manual_post_install.py'
2675 Out[3]: 'setup.py win32_manual_post_install.py'
2675
2676
2676 In summary, both the lists and strings used for ouptut capture have
2677 In summary, both the lists and strings used for ouptut capture have
2677 the following special attributes:
2678 the following special attributes:
2678
2679
2679 .l (or .list) : value as list.
2680 .l (or .list) : value as list.
2680 .n (or .nlstr): value as newline-separated string.
2681 .n (or .nlstr): value as newline-separated string.
2681 .s (or .spstr): value as space-separated string.
2682 .s (or .spstr): value as space-separated string.
2682 """
2683 """
2683
2684
2684 opts,args = self.parse_options(parameter_s,'lv')
2685 opts,args = self.parse_options(parameter_s,'lv')
2685 # Try to get a variable name and command to run
2686 # Try to get a variable name and command to run
2686 try:
2687 try:
2687 # the variable name must be obtained from the parse_options
2688 # the variable name must be obtained from the parse_options
2688 # output, which uses shlex.split to strip options out.
2689 # output, which uses shlex.split to strip options out.
2689 var,_ = args.split('=',1)
2690 var,_ = args.split('=',1)
2690 var = var.strip()
2691 var = var.strip()
2691 # But the the command has to be extracted from the original input
2692 # But the the command has to be extracted from the original input
2692 # parameter_s, not on what parse_options returns, to avoid the
2693 # parameter_s, not on what parse_options returns, to avoid the
2693 # quote stripping which shlex.split performs on it.
2694 # quote stripping which shlex.split performs on it.
2694 _,cmd = parameter_s.split('=',1)
2695 _,cmd = parameter_s.split('=',1)
2695 except ValueError:
2696 except ValueError:
2696 var,cmd = '',''
2697 var,cmd = '',''
2697 # If all looks ok, proceed
2698 # If all looks ok, proceed
2698 out,err = self.shell.getoutputerror(cmd)
2699 out,err = self.shell.getoutputerror(cmd)
2699 if err:
2700 if err:
2700 print >> Term.cerr,err
2701 print >> Term.cerr,err
2701 if opts.has_key('l'):
2702 if opts.has_key('l'):
2702 out = SList(out.split('\n'))
2703 out = SList(out.split('\n'))
2703 else:
2704 else:
2704 out = LSString(out)
2705 out = LSString(out)
2705 if opts.has_key('v'):
2706 if opts.has_key('v'):
2706 print '%s ==\n%s' % (var,pformat(out))
2707 print '%s ==\n%s' % (var,pformat(out))
2707 if var:
2708 if var:
2708 self.shell.user_ns.update({var:out})
2709 self.shell.user_ns.update({var:out})
2709 else:
2710 else:
2710 return out
2711 return out
2711
2712
2712 def magic_sx(self, parameter_s=''):
2713 def magic_sx(self, parameter_s=''):
2713 """Shell execute - run a shell command and capture its output.
2714 """Shell execute - run a shell command and capture its output.
2714
2715
2715 %sx command
2716 %sx command
2716
2717
2717 IPython will run the given command using commands.getoutput(), and
2718 IPython will run the given command using commands.getoutput(), and
2718 return the result formatted as a list (split on '\\n'). Since the
2719 return the result formatted as a list (split on '\\n'). Since the
2719 output is _returned_, it will be stored in ipython's regular output
2720 output is _returned_, it will be stored in ipython's regular output
2720 cache Out[N] and in the '_N' automatic variables.
2721 cache Out[N] and in the '_N' automatic variables.
2721
2722
2722 Notes:
2723 Notes:
2723
2724
2724 1) If an input line begins with '!!', then %sx is automatically
2725 1) If an input line begins with '!!', then %sx is automatically
2725 invoked. That is, while:
2726 invoked. That is, while:
2726 !ls
2727 !ls
2727 causes ipython to simply issue system('ls'), typing
2728 causes ipython to simply issue system('ls'), typing
2728 !!ls
2729 !!ls
2729 is a shorthand equivalent to:
2730 is a shorthand equivalent to:
2730 %sx ls
2731 %sx ls
2731
2732
2732 2) %sx differs from %sc in that %sx automatically splits into a list,
2733 2) %sx differs from %sc in that %sx automatically splits into a list,
2733 like '%sc -l'. The reason for this is to make it as easy as possible
2734 like '%sc -l'. The reason for this is to make it as easy as possible
2734 to process line-oriented shell output via further python commands.
2735 to process line-oriented shell output via further python commands.
2735 %sc is meant to provide much finer control, but requires more
2736 %sc is meant to provide much finer control, but requires more
2736 typing.
2737 typing.
2737
2738
2738 3) Just like %sc -l, this is a list with special attributes:
2739 3) Just like %sc -l, this is a list with special attributes:
2739
2740
2740 .l (or .list) : value as list.
2741 .l (or .list) : value as list.
2741 .n (or .nlstr): value as newline-separated string.
2742 .n (or .nlstr): value as newline-separated string.
2742 .s (or .spstr): value as whitespace-separated string.
2743 .s (or .spstr): value as whitespace-separated string.
2743
2744
2744 This is very useful when trying to use such lists as arguments to
2745 This is very useful when trying to use such lists as arguments to
2745 system commands."""
2746 system commands."""
2746
2747
2747 if parameter_s:
2748 if parameter_s:
2748 out,err = self.shell.getoutputerror(parameter_s)
2749 out,err = self.shell.getoutputerror(parameter_s)
2749 if err:
2750 if err:
2750 print >> Term.cerr,err
2751 print >> Term.cerr,err
2751 return SList(out.split('\n'))
2752 return SList(out.split('\n'))
2752
2753
2753 def magic_bg(self, parameter_s=''):
2754 def magic_bg(self, parameter_s=''):
2754 """Run a job in the background, in a separate thread.
2755 """Run a job in the background, in a separate thread.
2755
2756
2756 For example,
2757 For example,
2757
2758
2758 %bg myfunc(x,y,z=1)
2759 %bg myfunc(x,y,z=1)
2759
2760
2760 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
2761 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
2761 execution starts, a message will be printed indicating the job
2762 execution starts, a message will be printed indicating the job
2762 number. If your job number is 5, you can use
2763 number. If your job number is 5, you can use
2763
2764
2764 myvar = jobs.result(5) or myvar = jobs[5].result
2765 myvar = jobs.result(5) or myvar = jobs[5].result
2765
2766
2766 to assign this result to variable 'myvar'.
2767 to assign this result to variable 'myvar'.
2767
2768
2768 IPython has a job manager, accessible via the 'jobs' object. You can
2769 IPython has a job manager, accessible via the 'jobs' object. You can
2769 type jobs? to get more information about it, and use jobs.<TAB> to see
2770 type jobs? to get more information about it, and use jobs.<TAB> to see
2770 its attributes. All attributes not starting with an underscore are
2771 its attributes. All attributes not starting with an underscore are
2771 meant for public use.
2772 meant for public use.
2772
2773
2773 In particular, look at the jobs.new() method, which is used to create
2774 In particular, look at the jobs.new() method, which is used to create
2774 new jobs. This magic %bg function is just a convenience wrapper
2775 new jobs. This magic %bg function is just a convenience wrapper
2775 around jobs.new(), for expression-based jobs. If you want to create a
2776 around jobs.new(), for expression-based jobs. If you want to create a
2776 new job with an explicit function object and arguments, you must call
2777 new job with an explicit function object and arguments, you must call
2777 jobs.new() directly.
2778 jobs.new() directly.
2778
2779
2779 The jobs.new docstring also describes in detail several important
2780 The jobs.new docstring also describes in detail several important
2780 caveats associated with a thread-based model for background job
2781 caveats associated with a thread-based model for background job
2781 execution. Type jobs.new? for details.
2782 execution. Type jobs.new? for details.
2782
2783
2783 You can check the status of all jobs with jobs.status().
2784 You can check the status of all jobs with jobs.status().
2784
2785
2785 The jobs variable is set by IPython into the Python builtin namespace.
2786 The jobs variable is set by IPython into the Python builtin namespace.
2786 If you ever declare a variable named 'jobs', you will shadow this
2787 If you ever declare a variable named 'jobs', you will shadow this
2787 name. You can either delete your global jobs variable to regain
2788 name. You can either delete your global jobs variable to regain
2788 access to the job manager, or make a new name and assign it manually
2789 access to the job manager, or make a new name and assign it manually
2789 to the manager (stored in IPython's namespace). For example, to
2790 to the manager (stored in IPython's namespace). For example, to
2790 assign the job manager to the Jobs name, use:
2791 assign the job manager to the Jobs name, use:
2791
2792
2792 Jobs = __builtins__.jobs"""
2793 Jobs = __builtins__.jobs"""
2793
2794
2794 self.shell.jobs.new(parameter_s,self.shell.user_ns)
2795 self.shell.jobs.new(parameter_s,self.shell.user_ns)
2795
2796
2796
2797
2797 def magic_bookmark(self, parameter_s=''):
2798 def magic_bookmark(self, parameter_s=''):
2798 """Manage IPython's bookmark system.
2799 """Manage IPython's bookmark system.
2799
2800
2800 %bookmark <name> - set bookmark to current dir
2801 %bookmark <name> - set bookmark to current dir
2801 %bookmark <name> <dir> - set bookmark to <dir>
2802 %bookmark <name> <dir> - set bookmark to <dir>
2802 %bookmark -l - list all bookmarks
2803 %bookmark -l - list all bookmarks
2803 %bookmark -d <name> - remove bookmark
2804 %bookmark -d <name> - remove bookmark
2804 %bookmark -r - remove all bookmarks
2805 %bookmark -r - remove all bookmarks
2805
2806
2806 You can later on access a bookmarked folder with:
2807 You can later on access a bookmarked folder with:
2807 %cd -b <name>
2808 %cd -b <name>
2808 or simply '%cd <name>' if there is no directory called <name> AND
2809 or simply '%cd <name>' if there is no directory called <name> AND
2809 there is such a bookmark defined.
2810 there is such a bookmark defined.
2810
2811
2811 Your bookmarks persist through IPython sessions, but they are
2812 Your bookmarks persist through IPython sessions, but they are
2812 associated with each profile."""
2813 associated with each profile."""
2813
2814
2814 opts,args = self.parse_options(parameter_s,'drl',mode='list')
2815 opts,args = self.parse_options(parameter_s,'drl',mode='list')
2815 if len(args) > 2:
2816 if len(args) > 2:
2816 error('You can only give at most two arguments')
2817 error('You can only give at most two arguments')
2817 return
2818 return
2818
2819
2819 bkms = self.db.get('bookmarks',{})
2820 bkms = self.db.get('bookmarks',{})
2820
2821
2821 if opts.has_key('d'):
2822 if opts.has_key('d'):
2822 try:
2823 try:
2823 todel = args[0]
2824 todel = args[0]
2824 except IndexError:
2825 except IndexError:
2825 error('You must provide a bookmark to delete')
2826 error('You must provide a bookmark to delete')
2826 else:
2827 else:
2827 try:
2828 try:
2828 del bkms[todel]
2829 del bkms[todel]
2829 except:
2830 except:
2830 error("Can't delete bookmark '%s'" % todel)
2831 error("Can't delete bookmark '%s'" % todel)
2831 elif opts.has_key('r'):
2832 elif opts.has_key('r'):
2832 bkms = {}
2833 bkms = {}
2833 elif opts.has_key('l'):
2834 elif opts.has_key('l'):
2834 bks = bkms.keys()
2835 bks = bkms.keys()
2835 bks.sort()
2836 bks.sort()
2836 if bks:
2837 if bks:
2837 size = max(map(len,bks))
2838 size = max(map(len,bks))
2838 else:
2839 else:
2839 size = 0
2840 size = 0
2840 fmt = '%-'+str(size)+'s -> %s'
2841 fmt = '%-'+str(size)+'s -> %s'
2841 print 'Current bookmarks:'
2842 print 'Current bookmarks:'
2842 for bk in bks:
2843 for bk in bks:
2843 print fmt % (bk,bkms[bk])
2844 print fmt % (bk,bkms[bk])
2844 else:
2845 else:
2845 if not args:
2846 if not args:
2846 error("You must specify the bookmark name")
2847 error("You must specify the bookmark name")
2847 elif len(args)==1:
2848 elif len(args)==1:
2848 bkms[args[0]] = os.getcwd()
2849 bkms[args[0]] = os.getcwd()
2849 elif len(args)==2:
2850 elif len(args)==2:
2850 bkms[args[0]] = args[1]
2851 bkms[args[0]] = args[1]
2851 self.db['bookmarks'] = bkms
2852 self.db['bookmarks'] = bkms
2852
2853
2853 def magic_pycat(self, parameter_s=''):
2854 def magic_pycat(self, parameter_s=''):
2854 """Show a syntax-highlighted file through a pager.
2855 """Show a syntax-highlighted file through a pager.
2855
2856
2856 This magic is similar to the cat utility, but it will assume the file
2857 This magic is similar to the cat utility, but it will assume the file
2857 to be Python source and will show it with syntax highlighting. """
2858 to be Python source and will show it with syntax highlighting. """
2858
2859
2859 try:
2860 try:
2860 filename = get_py_filename(parameter_s)
2861 filename = get_py_filename(parameter_s)
2861 cont = file_read(filename)
2862 cont = file_read(filename)
2862 except IOError:
2863 except IOError:
2863 try:
2864 try:
2864 cont = eval(parameter_s,self.user_ns)
2865 cont = eval(parameter_s,self.user_ns)
2865 except NameError:
2866 except NameError:
2866 cont = None
2867 cont = None
2867 if cont is None:
2868 if cont is None:
2868 print "Error: no such file or variable"
2869 print "Error: no such file or variable"
2869 return
2870 return
2870
2871
2871 page(self.shell.pycolorize(cont),
2872 page(self.shell.pycolorize(cont),
2872 screen_lines=self.shell.rc.screen_length)
2873 screen_lines=self.shell.rc.screen_length)
2873
2874
2874 def magic_cpaste(self, parameter_s=''):
2875 def magic_cpaste(self, parameter_s=''):
2875 """Allows you to paste & execute a pre-formatted code block from clipboard
2876 """Allows you to paste & execute a pre-formatted code block from clipboard
2876
2877
2877 You must terminate the block with '--' (two minus-signs) alone on the
2878 You must terminate the block with '--' (two minus-signs) alone on the
2878 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
2879 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
2879 is the new sentinel for this operation)
2880 is the new sentinel for this operation)
2880
2881
2881 The block is dedented prior to execution to enable execution of
2882 The block is dedented prior to execution to enable execution of
2882 method definitions. The executed block is also assigned to variable
2883 method definitions. The executed block is also assigned to variable
2883 named 'pasted_block' for later editing with '%edit pasted_block'.
2884 named 'pasted_block' for later editing with '%edit pasted_block'.
2884
2885
2885 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
2886 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
2886 This assigns the pasted block to variable 'foo' as string, without
2887 This assigns the pasted block to variable 'foo' as string, without
2887 dedenting or executing it.
2888 dedenting or executing it.
2888
2889
2889 Do not be alarmed by garbled output on Windows (it's a readline bug).
2890 Do not be alarmed by garbled output on Windows (it's a readline bug).
2890 Just press enter and type -- (and press enter again) and the block
2891 Just press enter and type -- (and press enter again) and the block
2891 will be what was just pasted.
2892 will be what was just pasted.
2892
2893
2893 IPython statements (magics, shell escapes) are not supported (yet).
2894 IPython statements (magics, shell escapes) are not supported (yet).
2894 """
2895 """
2895 opts,args = self.parse_options(parameter_s,'s:',mode='string')
2896 opts,args = self.parse_options(parameter_s,'s:',mode='string')
2896 par = args.strip()
2897 par = args.strip()
2897 sentinel = opts.get('s','--')
2898 sentinel = opts.get('s','--')
2898
2899
2899 from IPython import iplib
2900 from IPython import iplib
2900 lines = []
2901 lines = []
2901 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
2902 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
2902 while 1:
2903 while 1:
2903 l = iplib.raw_input_original(':')
2904 l = iplib.raw_input_original(':')
2904 if l ==sentinel:
2905 if l ==sentinel:
2905 break
2906 break
2906 lines.append(l)
2907 lines.append(l)
2907 block = "\n".join(lines) + '\n'
2908 block = "\n".join(lines) + '\n'
2908 #print "block:\n",block
2909 #print "block:\n",block
2909 if not par:
2910 if not par:
2910 b = textwrap.dedent(block)
2911 b = textwrap.dedent(block)
2911 exec b in self.user_ns
2912 exec b in self.user_ns
2912 self.user_ns['pasted_block'] = b
2913 self.user_ns['pasted_block'] = b
2913 else:
2914 else:
2914 self.user_ns[par] = block
2915 self.user_ns[par] = block
2915 print "Block assigned to '%s'" % par
2916 print "Block assigned to '%s'" % par
2916
2917
2917 def magic_quickref(self,arg):
2918 def magic_quickref(self,arg):
2918 """ Show a quick reference sheet """
2919 """ Show a quick reference sheet """
2919 import IPython.usage
2920 import IPython.usage
2920 qr = IPython.usage.quick_reference + self.magic_magic('-brief')
2921 qr = IPython.usage.quick_reference + self.magic_magic('-brief')
2921
2922
2922 page(qr)
2923 page(qr)
2923
2924
2924 def magic_upgrade(self,arg):
2925 def magic_upgrade(self,arg):
2925 """ Upgrade your IPython installation
2926 """ Upgrade your IPython installation
2926
2927
2927 This will copy the config files that don't yet exist in your
2928 This will copy the config files that don't yet exist in your
2928 ipython dir from the system config dir. Use this after upgrading
2929 ipython dir from the system config dir. Use this after upgrading
2929 IPython if you don't wish to delete your .ipython dir.
2930 IPython if you don't wish to delete your .ipython dir.
2930
2931
2931 Call with -nolegacy to get rid of ipythonrc* files (recommended for
2932 Call with -nolegacy to get rid of ipythonrc* files (recommended for
2932 new users)
2933 new users)
2933
2934
2934 """
2935 """
2935 ip = self.getapi()
2936 ip = self.getapi()
2936 ipinstallation = path(IPython.__file__).dirname()
2937 ipinstallation = path(IPython.__file__).dirname()
2937 upgrade_script = sys.executable + " " + ipinstallation / 'upgrade_dir.py'
2938 upgrade_script = sys.executable + " " + ipinstallation / 'upgrade_dir.py'
2938 src_config = ipinstallation / 'UserConfig'
2939 src_config = ipinstallation / 'UserConfig'
2939 userdir = path(ip.options().ipythondir)
2940 userdir = path(ip.options.ipythondir)
2940 cmd = upgrade_script + " " + src_config + " " + userdir
2941 cmd = upgrade_script + " " + src_config + " " + userdir
2941 print ">",cmd
2942 print ">",cmd
2942 shell(cmd)
2943 shell(cmd)
2943 if arg == '-nolegacy':
2944 if arg == '-nolegacy':
2944 legacy = userdir.files('ipythonrc*')
2945 legacy = userdir.files('ipythonrc*')
2945 print "Nuking legacy files:",legacy
2946 print "Nuking legacy files:",legacy
2946
2947
2947 [p.remove() for p in legacy]
2948 [p.remove() for p in legacy]
2948 suffix = (sys.platform == 'win32' and '.ini' or '')
2949 suffix = (sys.platform == 'win32' and '.ini' or '')
2949 (userdir / ('ipythonrc' + suffix)).write_text('# Empty, see ipy_user_conf.py\n')
2950 (userdir / ('ipythonrc' + suffix)).write_text('# Empty, see ipy_user_conf.py\n')
2950
2951
2951
2952
2952 # end Magic
2953 # end Magic
@@ -1,85 +1,82 b''
1 """ Shell mode for ipython
1 """Shell mode for IPython.
2
2
3 Start ipython in shell mode by invoking "ipython -p sh"
3 Start ipython in shell mode by invoking "ipython -p sh"
4
4
5 (the old version, "ipython -p pysh" still works but this is the more "modern"
5 (the old version, "ipython -p pysh" still works but this is the more "modern"
6 shell mode and is recommended for users who don't care about pysh-mode
6 shell mode and is recommended for users who don't care about pysh-mode
7 compatibility)
7 compatibility)
8
9
10 """
8 """
11
9
12
13 from IPython import ipapi
10 from IPython import ipapi
14 import os,textwrap
11 import os,textwrap
15
12
16 # The import below effectively obsoletes your old-style ipythonrc[.ini],
13 # The import below effectively obsoletes your old-style ipythonrc[.ini],
17 # so consider yourself warned!
14 # so consider yourself warned!
18
15
19 import ipy_sane_defaults
16 import ipy_defaults
20
17
21 def main():
18 def main():
22 ip = ipapi.get()
19 ip = ipapi.get()
23 o = ip.options()
20 o = ip.options
24 # autocall to "full" mode (smart mode is default, I like full mode)
21 # autocall to "full" mode (smart mode is default, I like full mode)
25
22
26 o.autocall = 2
23 o.autocall = 2
27
24
28 # Jason Orendorff's path class is handy to have in user namespace
25 # Jason Orendorff's path class is handy to have in user namespace
29 # if you are doing shell-like stuff
26 # if you are doing shell-like stuff
30 try:
27 try:
31 ip.ex("from path import path" )
28 ip.ex("from path import path" )
32 except ImportError:
29 except ImportError:
33 pass
30 pass
34
31
35 ip.ex('import os')
32 ip.ex('import os')
36 ip.ex("def up(): os.chdir('..')")
33 ip.ex("def up(): os.chdir('..')")
37
34
38 # Get pysh-like prompt for all profiles.
35 # Get pysh-like prompt for all profiles.
39
36
40 o.prompt_in1= '\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> '
37 o.prompt_in1= '\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> '
41 o.prompt_in2= '\C_Green|\C_LightGreen\D\C_Green> '
38 o.prompt_in2= '\C_Green|\C_LightGreen\D\C_Green> '
42 o.prompt_out= '<\#> '
39 o.prompt_out= '<\#> '
43
40
44 from IPython import Release
41 from IPython import Release
45
42
46 import sys
43 import sys
47 # I like my banner minimal.
44 # I like my banner minimal.
48 o.banner = "Py %s IPy %s\n" % (sys.version.split('\n')[0],Release.version)
45 o.banner = "Py %s IPy %s\n" % (sys.version.split('\n')[0],Release.version)
49
46
50 # make 'd' an alias for ls -F
47 # make 'd' an alias for ls -F
51
48
52 ip.magic('alias d ls -F --color=auto')
49 ip.magic('alias d ls -F --color=auto')
53
50
54 # Make available all system commands through "rehashing" immediately.
51 # Make available all system commands through "rehashing" immediately.
55 # You can comment these lines out to speed up startup on very slow
52 # You can comment these lines out to speed up startup on very slow
56 # machines, and to conserve a bit of memory. Note that pysh profile does this
53 # machines, and to conserve a bit of memory. Note that pysh profile does this
57 # automatically
54 # automatically
58 ip.IP.default_option('cd','-q')
55 ip.IP.default_option('cd','-q')
59
56
60
57
61 o.prompts_pad_left="1"
58 o.prompts_pad_left="1"
62 # Remove all blank lines in between prompts, like a normal shell.
59 # Remove all blank lines in between prompts, like a normal shell.
63 o.separate_in="0"
60 o.separate_in="0"
64 o.separate_out="0"
61 o.separate_out="0"
65 o.separate_out2="0"
62 o.separate_out2="0"
66
63
67 # now alias all syscommands
64 # now alias all syscommands
68
65
69 db = ip.getdb()
66 db = ip.db
70
67
71 syscmds = db.get("syscmdlist",[] )
68 syscmds = db.get("syscmdlist",[] )
72 if not syscmds:
69 if not syscmds:
73 print textwrap.dedent("""
70 print textwrap.dedent("""
74 System command list not initialized, probably the first run...
71 System command list not initialized, probably the first run...
75 running %rehashx to refresh the command list. Run %rehashx
72 running %rehashx to refresh the command list. Run %rehashx
76 again to refresh command list (after installing new software etc.)
73 again to refresh command list (after installing new software etc.)
77 """)
74 """)
78 ip.magic('rehashx')
75 ip.magic('rehashx')
79 syscmds = db.get("syscmdlist")
76 syscmds = db.get("syscmdlist")
80 for cmd in syscmds:
77 for cmd in syscmds:
81 #print "al",cmd
78 #print "al",cmd
82 noext, ext = os.path.splitext(cmd)
79 noext, ext = os.path.splitext(cmd)
83 ip.IP.alias_table[noext] = (0,cmd)
80 ip.IP.alias_table[noext] = (0,cmd)
84
81
85 main()
82 main()
@@ -1,31 +1,31 b''
1 """ User configuration file for IPython
1 """ User configuration file for IPython
2
2
3 This is a more flexible and safe way to configure ipython than *rc files
3 This is a more flexible and safe way to configure ipython than *rc files
4 (ipythonrc, ipythonrc-pysh etc.)
4 (ipythonrc, ipythonrc-pysh etc.)
5
5
6 This file is always imported on ipython startup. You can import the
6 This file is always imported on ipython startup. You can import the
7 ipython extensions you need here (see IPython/Extensions directory).
7 ipython extensions you need here (see IPython/Extensions directory).
8
8
9 Feel free to edit this file to customize your ipython experience.
9 Feel free to edit this file to customize your ipython experience.
10
10
11 Note that as such this file does nothing, for backwards compatibility.
11 Note that as such this file does nothing, for backwards compatibility.
12 Consult e.g. file 'ipy_profile_sh.py' for an example of the things
12 Consult e.g. file 'ipy_profile_sh.py' for an example of the things
13 you can do here.
13 you can do here.
14
14
15 """
15 """
16
16
17 # Most of your config files and extensions will probably start with this import
17 # Most of your config files and extensions will probably start with this import
18
18
19 import IPython.ipapi
19 import IPython.ipapi
20 ip = IPython.ipapi.get()
20 ip = IPython.ipapi.get()
21
21
22 # You probably want to uncomment this if you did %upgrade -nolegacy
22 # You probably want to uncomment this if you did %upgrade -nolegacy
23 # import ipy_sane_defaults
23 # import ipy_defaults
24
24
25 def main():
25 def main():
26 o = ip.options()
26 o = ip.options
27 # An example on how to set options
27 # An example on how to set options
28 #o.autocall = 1
28 #o.autocall = 1
29
29
30 main()
30 main()
31
31
@@ -1,73 +1,73 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 IPython -- An enhanced Interactive Python
3 IPython -- An enhanced Interactive Python
4
4
5 One of Python's nicest features is its interactive interpreter. This allows
5 One of Python's nicest features is its interactive interpreter. This allows
6 very fast testing of ideas without the overhead of creating test files as is
6 very fast testing of ideas without the overhead of creating test files as is
7 typical in most programming languages. However, the interpreter supplied with
7 typical in most programming languages. However, the interpreter supplied with
8 the standard Python distribution is fairly primitive (and IDLE isn't really
8 the standard Python distribution is fairly primitive (and IDLE isn't really
9 much better).
9 much better).
10
10
11 IPython tries to:
11 IPython tries to:
12
12
13 i - provide an efficient environment for interactive work in Python
13 i - provide an efficient environment for interactive work in Python
14 programming. It tries to address what we see as shortcomings of the standard
14 programming. It tries to address what we see as shortcomings of the standard
15 Python prompt, and adds many features to make interactive work much more
15 Python prompt, and adds many features to make interactive work much more
16 efficient.
16 efficient.
17
17
18 ii - offer a flexible framework so that it can be used as the base
18 ii - offer a flexible framework so that it can be used as the base
19 environment for other projects and problems where Python can be the
19 environment for other projects and problems where Python can be the
20 underlying language. Specifically scientific environments like Mathematica,
20 underlying language. Specifically scientific environments like Mathematica,
21 IDL and Mathcad inspired its design, but similar ideas can be useful in many
21 IDL and Mathcad inspired its design, but similar ideas can be useful in many
22 fields. Python is a fabulous language for implementing this kind of system
22 fields. Python is a fabulous language for implementing this kind of system
23 (due to its dynamic and introspective features), and with suitable libraries
23 (due to its dynamic and introspective features), and with suitable libraries
24 entire systems could be built leveraging Python's power.
24 entire systems could be built leveraging Python's power.
25
25
26 iii - serve as an embeddable, ready to go interpreter for your own programs.
26 iii - serve as an embeddable, ready to go interpreter for your own programs.
27
27
28 IPython requires Python 2.2 or newer.
28 IPython requires Python 2.3 or newer.
29
29
30 $Id: __init__.py 1110 2006-01-30 20:43:30Z vivainio $"""
30 $Id: __init__.py 1314 2006-05-19 18:24:14Z fperez $"""
31
31
32 #*****************************************************************************
32 #*****************************************************************************
33 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
33 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
34 #
34 #
35 # Distributed under the terms of the BSD License. The full license is in
35 # Distributed under the terms of the BSD License. The full license is in
36 # the file COPYING, distributed as part of this software.
36 # the file COPYING, distributed as part of this software.
37 #*****************************************************************************
37 #*****************************************************************************
38
38
39 # Enforce proper version requirements
39 # Enforce proper version requirements
40 import sys
40 import sys
41
41
42 if sys.version[0:3] < '2.3':
42 if sys.version[0:3] < '2.3':
43 raise ImportError, 'Python Version 2.3 or above is required.'
43 raise ImportError, 'Python Version 2.3 or above is required.'
44
44
45 # Make it easy to import extensions - they are always directly on pythonpath.
45 # Make it easy to import extensions - they are always directly on pythonpath.
46 # Therefore, non-IPython modules can be added to Extensions directory
46 # Therefore, non-IPython modules can be added to Extensions directory
47
47
48 import os
48 import os
49 sys.path.append(os.path.dirname(__file__) + "/Extensions")
49 sys.path.append(os.path.dirname(__file__) + "/Extensions")
50
50
51 # Define what gets imported with a 'from IPython import *'
51 # Define what gets imported with a 'from IPython import *'
52 __all__ = ['deep_reload','genutils','ipstruct','ultraTB','DPyGetOpt',
52 __all__ = ['deep_reload','genutils','ipstruct','ultraTB','DPyGetOpt',
53 'Itpl','hooks','ConfigLoader','OutputTrap','Release','Shell',
53 'Itpl','hooks','ConfigLoader','OutputTrap','Release','Shell',
54 'platutils','platutils_win32','platutils_posix','platutils_dummy',
54 'platutils','platutils_win32','platutils_posix','platutils_dummy',
55 'ipapi','rlineimpl']
55 'ipapi','rlineimpl']
56
56
57 # Load __all__ in IPython namespace so that a simple 'import IPython' gives
57 # Load __all__ in IPython namespace so that a simple 'import IPython' gives
58 # access to them via IPython.<name>
58 # access to them via IPython.<name>
59 glob,loc = globals(),locals()
59 glob,loc = globals(),locals()
60 for name in __all__:
60 for name in __all__:
61 __import__(name,glob,loc,[])
61 __import__(name,glob,loc,[])
62
62
63 # Release data
63 # Release data
64 from IPython import Release # do it explicitly so pydoc can see it - pydoc bug
64 from IPython import Release # do it explicitly so pydoc can see it - pydoc bug
65 __author__ = '%s <%s>\n%s <%s>\n%s <%s>' % \
65 __author__ = '%s <%s>\n%s <%s>\n%s <%s>' % \
66 ( Release.authors['Fernando'] + Release.authors['Janko'] + \
66 ( Release.authors['Fernando'] + Release.authors['Janko'] + \
67 Release.authors['Nathan'] )
67 Release.authors['Nathan'] )
68 __license__ = Release.license
68 __license__ = Release.license
69 __version__ = Release.version
69 __version__ = Release.version
70 __revision__ = Release.revision
70 __revision__ = Release.revision
71
71
72 # Namespace cleanup
72 # Namespace cleanup
73 del name,glob,loc
73 del name,glob,loc
@@ -1,570 +1,570 b''
1 """Word completion for IPython.
1 """Word completion for IPython.
2
2
3 This module is a fork of the rlcompleter module in the Python standard
3 This module is a fork of the rlcompleter module in the Python standard
4 library. The original enhancements made to rlcompleter have been sent
4 library. The original enhancements made to rlcompleter have been sent
5 upstream and were accepted as of Python 2.3, but we need a lot more
5 upstream and were accepted as of Python 2.3, but we need a lot more
6 functionality specific to IPython, so this module will continue to live as an
6 functionality specific to IPython, so this module will continue to live as an
7 IPython-specific utility.
7 IPython-specific utility.
8
8
9 ---------------------------------------------------------------------------
9 ---------------------------------------------------------------------------
10 Original rlcompleter documentation:
10 Original rlcompleter documentation:
11
11
12 This requires the latest extension to the readline module (the
12 This requires the latest extension to the readline module (the
13 completes keywords, built-ins and globals in __main__; when completing
13 completes keywords, built-ins and globals in __main__; when completing
14 NAME.NAME..., it evaluates (!) the expression up to the last dot and
14 NAME.NAME..., it evaluates (!) the expression up to the last dot and
15 completes its attributes.
15 completes its attributes.
16
16
17 It's very cool to do "import string" type "string.", hit the
17 It's very cool to do "import string" type "string.", hit the
18 completion key (twice), and see the list of names defined by the
18 completion key (twice), and see the list of names defined by the
19 string module!
19 string module!
20
20
21 Tip: to use the tab key as the completion key, call
21 Tip: to use the tab key as the completion key, call
22
22
23 readline.parse_and_bind("tab: complete")
23 readline.parse_and_bind("tab: complete")
24
24
25 Notes:
25 Notes:
26
26
27 - Exceptions raised by the completer function are *ignored* (and
27 - Exceptions raised by the completer function are *ignored* (and
28 generally cause the completion to fail). This is a feature -- since
28 generally cause the completion to fail). This is a feature -- since
29 readline sets the tty device in raw (or cbreak) mode, printing a
29 readline sets the tty device in raw (or cbreak) mode, printing a
30 traceback wouldn't work well without some complicated hoopla to save,
30 traceback wouldn't work well without some complicated hoopla to save,
31 reset and restore the tty state.
31 reset and restore the tty state.
32
32
33 - The evaluation of the NAME.NAME... form may cause arbitrary
33 - The evaluation of the NAME.NAME... form may cause arbitrary
34 application defined code to be executed if an object with a
34 application defined code to be executed if an object with a
35 __getattr__ hook is found. Since it is the responsibility of the
35 __getattr__ hook is found. Since it is the responsibility of the
36 application (or the user) to enable this feature, I consider this an
36 application (or the user) to enable this feature, I consider this an
37 acceptable risk. More complicated expressions (e.g. function calls or
37 acceptable risk. More complicated expressions (e.g. function calls or
38 indexing operations) are *not* evaluated.
38 indexing operations) are *not* evaluated.
39
39
40 - GNU readline is also used by the built-in functions input() and
40 - GNU readline is also used by the built-in functions input() and
41 raw_input(), and thus these also benefit/suffer from the completer
41 raw_input(), and thus these also benefit/suffer from the completer
42 features. Clearly an interactive application can benefit by
42 features. Clearly an interactive application can benefit by
43 specifying its own completer function and using raw_input() for all
43 specifying its own completer function and using raw_input() for all
44 its input.
44 its input.
45
45
46 - When the original stdin is not a tty device, GNU readline is never
46 - When the original stdin is not a tty device, GNU readline is never
47 used, and this module (and the readline module) are silently inactive.
47 used, and this module (and the readline module) are silently inactive.
48
48
49 """
49 """
50
50
51 #*****************************************************************************
51 #*****************************************************************************
52 #
52 #
53 # Since this file is essentially a minimally modified copy of the rlcompleter
53 # Since this file is essentially a minimally modified copy of the rlcompleter
54 # module which is part of the standard Python distribution, I assume that the
54 # module which is part of the standard Python distribution, I assume that the
55 # proper procedure is to maintain its copyright as belonging to the Python
55 # proper procedure is to maintain its copyright as belonging to the Python
56 # Software Foundation (in addition to my own, for all new code).
56 # Software Foundation (in addition to my own, for all new code).
57 #
57 #
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
59 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
59 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
60 #
60 #
61 # Distributed under the terms of the BSD License. The full license is in
61 # Distributed under the terms of the BSD License. The full license is in
62 # the file COPYING, distributed as part of this software.
62 # the file COPYING, distributed as part of this software.
63 #
63 #
64 #*****************************************************************************
64 #*****************************************************************************
65
65
66 import __builtin__
66 import __builtin__
67 import __main__
67 import __main__
68 import glob
68 import glob
69 import keyword
69 import keyword
70 import os
70 import os
71 import re
71 import re
72 import shlex
72 import sys
73 import sys
73 import IPython.rlineimpl as readline
74 import IPython.rlineimpl as readline
74
75
75 import types
76 import types
76
77
77 # Python 2.4 offers sets as a builtin
78 # Python 2.4 offers sets as a builtin
78 try:
79 try:
79 set([1,2])
80 set([1,2])
80 except NameError:
81 except NameError:
81 from sets import Set as set
82 from sets import Set as set
82
83
83
84 from IPython.genutils import debugx
84 from IPython.genutils import shlex_split,debugx
85
85
86 __all__ = ['Completer','IPCompleter']
86 __all__ = ['Completer','IPCompleter']
87
87
88 def get_class_members(cls):
88 def get_class_members(cls):
89 ret = dir(cls)
89 ret = dir(cls)
90 if hasattr(cls,'__bases__'):
90 if hasattr(cls,'__bases__'):
91 for base in cls.__bases__:
91 for base in cls.__bases__:
92 ret.extend(get_class_members(base))
92 ret.extend(get_class_members(base))
93 return ret
93 return ret
94
94
95 class Completer:
95 class Completer:
96 def __init__(self,namespace=None,global_namespace=None):
96 def __init__(self,namespace=None,global_namespace=None):
97 """Create a new completer for the command line.
97 """Create a new completer for the command line.
98
98
99 Completer([namespace,global_namespace]) -> completer instance.
99 Completer([namespace,global_namespace]) -> completer instance.
100
100
101 If unspecified, the default namespace where completions are performed
101 If unspecified, the default namespace where completions are performed
102 is __main__ (technically, __main__.__dict__). Namespaces should be
102 is __main__ (technically, __main__.__dict__). Namespaces should be
103 given as dictionaries.
103 given as dictionaries.
104
104
105 An optional second namespace can be given. This allows the completer
105 An optional second namespace can be given. This allows the completer
106 to handle cases where both the local and global scopes need to be
106 to handle cases where both the local and global scopes need to be
107 distinguished.
107 distinguished.
108
108
109 Completer instances should be used as the completion mechanism of
109 Completer instances should be used as the completion mechanism of
110 readline via the set_completer() call:
110 readline via the set_completer() call:
111
111
112 readline.set_completer(Completer(my_namespace).complete)
112 readline.set_completer(Completer(my_namespace).complete)
113 """
113 """
114
114
115 # some minimal strict typechecks. For some core data structures, I
115 # some minimal strict typechecks. For some core data structures, I
116 # want actual basic python types, not just anything that looks like
116 # want actual basic python types, not just anything that looks like
117 # one. This is especially true for namespaces.
117 # one. This is especially true for namespaces.
118 for ns in (namespace,global_namespace):
118 for ns in (namespace,global_namespace):
119 if ns is not None and type(ns) != types.DictType:
119 if ns is not None and type(ns) != types.DictType:
120 raise TypeError,'namespace must be a dictionary'
120 raise TypeError,'namespace must be a dictionary'
121
121
122 # Don't bind to namespace quite yet, but flag whether the user wants a
122 # Don't bind to namespace quite yet, but flag whether the user wants a
123 # specific namespace or to use __main__.__dict__. This will allow us
123 # specific namespace or to use __main__.__dict__. This will allow us
124 # to bind to __main__.__dict__ at completion time, not now.
124 # to bind to __main__.__dict__ at completion time, not now.
125 if namespace is None:
125 if namespace is None:
126 self.use_main_ns = 1
126 self.use_main_ns = 1
127 else:
127 else:
128 self.use_main_ns = 0
128 self.use_main_ns = 0
129 self.namespace = namespace
129 self.namespace = namespace
130
130
131 # The global namespace, if given, can be bound directly
131 # The global namespace, if given, can be bound directly
132 if global_namespace is None:
132 if global_namespace is None:
133 self.global_namespace = {}
133 self.global_namespace = {}
134 else:
134 else:
135 self.global_namespace = global_namespace
135 self.global_namespace = global_namespace
136
136
137 def complete(self, text, state):
137 def complete(self, text, state):
138 """Return the next possible completion for 'text'.
138 """Return the next possible completion for 'text'.
139
139
140 This is called successively with state == 0, 1, 2, ... until it
140 This is called successively with state == 0, 1, 2, ... until it
141 returns None. The completion should begin with 'text'.
141 returns None. The completion should begin with 'text'.
142
142
143 """
143 """
144 if self.use_main_ns:
144 if self.use_main_ns:
145 self.namespace = __main__.__dict__
145 self.namespace = __main__.__dict__
146
146
147 if state == 0:
147 if state == 0:
148 if "." in text:
148 if "." in text:
149 self.matches = self.attr_matches(text)
149 self.matches = self.attr_matches(text)
150 else:
150 else:
151 self.matches = self.global_matches(text)
151 self.matches = self.global_matches(text)
152 try:
152 try:
153 return self.matches[state]
153 return self.matches[state]
154 except IndexError:
154 except IndexError:
155 return None
155 return None
156
156
157 def global_matches(self, text):
157 def global_matches(self, text):
158 """Compute matches when text is a simple name.
158 """Compute matches when text is a simple name.
159
159
160 Return a list of all keywords, built-in functions and names currently
160 Return a list of all keywords, built-in functions and names currently
161 defined in self.namespace or self.global_namespace that match.
161 defined in self.namespace or self.global_namespace that match.
162
162
163 """
163 """
164 matches = []
164 matches = []
165 match_append = matches.append
165 match_append = matches.append
166 n = len(text)
166 n = len(text)
167 for lst in [keyword.kwlist,
167 for lst in [keyword.kwlist,
168 __builtin__.__dict__.keys(),
168 __builtin__.__dict__.keys(),
169 self.namespace.keys(),
169 self.namespace.keys(),
170 self.global_namespace.keys()]:
170 self.global_namespace.keys()]:
171 for word in lst:
171 for word in lst:
172 if word[:n] == text and word != "__builtins__":
172 if word[:n] == text and word != "__builtins__":
173 match_append(word)
173 match_append(word)
174 return matches
174 return matches
175
175
176 def attr_matches(self, text):
176 def attr_matches(self, text):
177 """Compute matches when text contains a dot.
177 """Compute matches when text contains a dot.
178
178
179 Assuming the text is of the form NAME.NAME....[NAME], and is
179 Assuming the text is of the form NAME.NAME....[NAME], and is
180 evaluatable in self.namespace or self.global_namespace, it will be
180 evaluatable in self.namespace or self.global_namespace, it will be
181 evaluated and its attributes (as revealed by dir()) are used as
181 evaluated and its attributes (as revealed by dir()) are used as
182 possible completions. (For class instances, class members are are
182 possible completions. (For class instances, class members are are
183 also considered.)
183 also considered.)
184
184
185 WARNING: this can still invoke arbitrary C code, if an object
185 WARNING: this can still invoke arbitrary C code, if an object
186 with a __getattr__ hook is evaluated.
186 with a __getattr__ hook is evaluated.
187
187
188 """
188 """
189 import re
189 import re
190
190
191 # Another option, seems to work great. Catches things like ''.<tab>
191 # Another option, seems to work great. Catches things like ''.<tab>
192 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
192 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
193
193
194 if not m:
194 if not m:
195 return []
195 return []
196
196
197 expr, attr = m.group(1, 3)
197 expr, attr = m.group(1, 3)
198 try:
198 try:
199 object = eval(expr, self.namespace)
199 object = eval(expr, self.namespace)
200 except:
200 except:
201 object = eval(expr, self.global_namespace)
201 object = eval(expr, self.global_namespace)
202
202
203 # Start building the attribute list via dir(), and then complete it
203 # Start building the attribute list via dir(), and then complete it
204 # with a few extra special-purpose calls.
204 # with a few extra special-purpose calls.
205 words = dir(object)
205 words = dir(object)
206
206
207 if hasattr(object,'__class__'):
207 if hasattr(object,'__class__'):
208 words.append('__class__')
208 words.append('__class__')
209 words.extend(get_class_members(object.__class__))
209 words.extend(get_class_members(object.__class__))
210
210
211 # this is the 'dir' function for objects with Enthought's traits
211 # this is the 'dir' function for objects with Enthought's traits
212 if hasattr(object, 'trait_names'):
212 if hasattr(object, 'trait_names'):
213 try:
213 try:
214 words.extend(object.trait_names())
214 words.extend(object.trait_names())
215 # eliminate possible duplicates, as some traits may also
215 # eliminate possible duplicates, as some traits may also
216 # appear as normal attributes in the dir() call.
216 # appear as normal attributes in the dir() call.
217 words = set(words)
217 words = set(words)
218 except TypeError:
218 except TypeError:
219 # This will happen if `object` is a class and not an instance.
219 # This will happen if `object` is a class and not an instance.
220 pass
220 pass
221
221
222 # filter out non-string attributes which may be stuffed by dir() calls
222 # filter out non-string attributes which may be stuffed by dir() calls
223 # and poor coding in third-party modules
223 # and poor coding in third-party modules
224 words = [w for w in words
224 words = [w for w in words
225 if isinstance(w, basestring) and w != "__builtins__"]
225 if isinstance(w, basestring) and w != "__builtins__"]
226 # Build match list to return
226 # Build match list to return
227 n = len(attr)
227 n = len(attr)
228 return ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
228 return ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
229
229
230 class IPCompleter(Completer):
230 class IPCompleter(Completer):
231 """Extension of the completer class with IPython-specific features"""
231 """Extension of the completer class with IPython-specific features"""
232
232
233 def __init__(self,shell,namespace=None,global_namespace=None,
233 def __init__(self,shell,namespace=None,global_namespace=None,
234 omit__names=0,alias_table=None):
234 omit__names=0,alias_table=None):
235 """IPCompleter() -> completer
235 """IPCompleter() -> completer
236
236
237 Return a completer object suitable for use by the readline library
237 Return a completer object suitable for use by the readline library
238 via readline.set_completer().
238 via readline.set_completer().
239
239
240 Inputs:
240 Inputs:
241
241
242 - shell: a pointer to the ipython shell itself. This is needed
242 - shell: a pointer to the ipython shell itself. This is needed
243 because this completer knows about magic functions, and those can
243 because this completer knows about magic functions, and those can
244 only be accessed via the ipython instance.
244 only be accessed via the ipython instance.
245
245
246 - namespace: an optional dict where completions are performed.
246 - namespace: an optional dict where completions are performed.
247
247
248 - global_namespace: secondary optional dict for completions, to
248 - global_namespace: secondary optional dict for completions, to
249 handle cases (such as IPython embedded inside functions) where
249 handle cases (such as IPython embedded inside functions) where
250 both Python scopes are visible.
250 both Python scopes are visible.
251
251
252 - The optional omit__names parameter sets the completer to omit the
252 - The optional omit__names parameter sets the completer to omit the
253 'magic' names (__magicname__) for python objects unless the text
253 'magic' names (__magicname__) for python objects unless the text
254 to be completed explicitly starts with one or more underscores.
254 to be completed explicitly starts with one or more underscores.
255
255
256 - If alias_table is supplied, it should be a dictionary of aliases
256 - If alias_table is supplied, it should be a dictionary of aliases
257 to complete. """
257 to complete. """
258
258
259 Completer.__init__(self,namespace,global_namespace)
259 Completer.__init__(self,namespace,global_namespace)
260 self.magic_prefix = shell.name+'.magic_'
260 self.magic_prefix = shell.name+'.magic_'
261 self.magic_escape = shell.ESC_MAGIC
261 self.magic_escape = shell.ESC_MAGIC
262 self.readline = readline
262 self.readline = readline
263 delims = self.readline.get_completer_delims()
263 delims = self.readline.get_completer_delims()
264 delims = delims.replace(self.magic_escape,'')
264 delims = delims.replace(self.magic_escape,'')
265 self.readline.set_completer_delims(delims)
265 self.readline.set_completer_delims(delims)
266 self.get_line_buffer = self.readline.get_line_buffer
266 self.get_line_buffer = self.readline.get_line_buffer
267 self.omit__names = omit__names
267 self.omit__names = omit__names
268 self.merge_completions = shell.rc.readline_merge_completions
268 self.merge_completions = shell.rc.readline_merge_completions
269
269
270 if alias_table is None:
270 if alias_table is None:
271 alias_table = {}
271 alias_table = {}
272 self.alias_table = alias_table
272 self.alias_table = alias_table
273 # Regexp to split filenames with spaces in them
273 # Regexp to split filenames with spaces in them
274 self.space_name_re = re.compile(r'([^\\] )')
274 self.space_name_re = re.compile(r'([^\\] )')
275 # Hold a local ref. to glob.glob for speed
275 # Hold a local ref. to glob.glob for speed
276 self.glob = glob.glob
276 self.glob = glob.glob
277
277
278 # Determine if we are running on 'dumb' terminals, like (X)Emacs
278 # Determine if we are running on 'dumb' terminals, like (X)Emacs
279 # buffers, to avoid completion problems.
279 # buffers, to avoid completion problems.
280 term = os.environ.get('TERM','xterm')
280 term = os.environ.get('TERM','xterm')
281 self.dumb_terminal = term in ['dumb','emacs']
281 self.dumb_terminal = term in ['dumb','emacs']
282
282
283 # Special handling of backslashes needed in win32 platforms
283 # Special handling of backslashes needed in win32 platforms
284 if sys.platform == "win32":
284 if sys.platform == "win32":
285 self.clean_glob = self._clean_glob_win32
285 self.clean_glob = self._clean_glob_win32
286 else:
286 else:
287 self.clean_glob = self._clean_glob
287 self.clean_glob = self._clean_glob
288 self.matchers = [self.python_matches,
288 self.matchers = [self.python_matches,
289 self.file_matches,
289 self.file_matches,
290 self.alias_matches,
290 self.alias_matches,
291 self.python_func_kw_matches]
291 self.python_func_kw_matches]
292
292
293 # Code contributed by Alex Schmolck, for ipython/emacs integration
293 # Code contributed by Alex Schmolck, for ipython/emacs integration
294 def all_completions(self, text):
294 def all_completions(self, text):
295 """Return all possible completions for the benefit of emacs."""
295 """Return all possible completions for the benefit of emacs."""
296
296
297 completions = []
297 completions = []
298 comp_append = completions.append
298 comp_append = completions.append
299 try:
299 try:
300 for i in xrange(sys.maxint):
300 for i in xrange(sys.maxint):
301 res = self.complete(text, i)
301 res = self.complete(text, i)
302
302
303 if not res: break
303 if not res: break
304
304
305 comp_append(res)
305 comp_append(res)
306 #XXX workaround for ``notDefined.<tab>``
306 #XXX workaround for ``notDefined.<tab>``
307 except NameError:
307 except NameError:
308 pass
308 pass
309 return completions
309 return completions
310 # /end Alex Schmolck code.
310 # /end Alex Schmolck code.
311
311
312 def _clean_glob(self,text):
312 def _clean_glob(self,text):
313 return self.glob("%s*" % text)
313 return self.glob("%s*" % text)
314
314
315 def _clean_glob_win32(self,text):
315 def _clean_glob_win32(self,text):
316 return [f.replace("\\","/")
316 return [f.replace("\\","/")
317 for f in self.glob("%s*" % text)]
317 for f in self.glob("%s*" % text)]
318
318
319 def file_matches(self, text):
319 def file_matches(self, text):
320 """Match filneames, expanding ~USER type strings.
320 """Match filneames, expanding ~USER type strings.
321
321
322 Most of the seemingly convoluted logic in this completer is an
322 Most of the seemingly convoluted logic in this completer is an
323 attempt to handle filenames with spaces in them. And yet it's not
323 attempt to handle filenames with spaces in them. And yet it's not
324 quite perfect, because Python's readline doesn't expose all of the
324 quite perfect, because Python's readline doesn't expose all of the
325 GNU readline details needed for this to be done correctly.
325 GNU readline details needed for this to be done correctly.
326
326
327 For a filename with a space in it, the printed completions will be
327 For a filename with a space in it, the printed completions will be
328 only the parts after what's already been typed (instead of the
328 only the parts after what's already been typed (instead of the
329 full completions, as is normally done). I don't think with the
329 full completions, as is normally done). I don't think with the
330 current (as of Python 2.3) Python readline it's possible to do
330 current (as of Python 2.3) Python readline it's possible to do
331 better."""
331 better."""
332
332
333 #print 'Completer->file_matches: <%s>' % text # dbg
333 #print 'Completer->file_matches: <%s>' % text # dbg
334
334
335 # chars that require escaping with backslash - i.e. chars
335 # chars that require escaping with backslash - i.e. chars
336 # that readline treats incorrectly as delimiters, but we
336 # that readline treats incorrectly as delimiters, but we
337 # don't want to treat as delimiters in filename matching
337 # don't want to treat as delimiters in filename matching
338 # when escaped with backslash
338 # when escaped with backslash
339
339
340 protectables = ' ()[]{}'
340 protectables = ' ()[]{}'
341
341
342 def protect_filename(s):
342 def protect_filename(s):
343 return "".join([(ch in protectables and '\\' + ch or ch)
343 return "".join([(ch in protectables and '\\' + ch or ch)
344 for ch in s])
344 for ch in s])
345
345
346 lbuf = self.lbuf
346 lbuf = self.lbuf
347 open_quotes = 0 # track strings with open quotes
347 open_quotes = 0 # track strings with open quotes
348 try:
348 try:
349 lsplit = shlex_split(lbuf)[-1]
349 lsplit = shlex.split(lbuf)[-1]
350 except ValueError:
350 except ValueError:
351 # typically an unmatched ", or backslash without escaped char.
351 # typically an unmatched ", or backslash without escaped char.
352 if lbuf.count('"')==1:
352 if lbuf.count('"')==1:
353 open_quotes = 1
353 open_quotes = 1
354 lsplit = lbuf.split('"')[-1]
354 lsplit = lbuf.split('"')[-1]
355 elif lbuf.count("'")==1:
355 elif lbuf.count("'")==1:
356 open_quotes = 1
356 open_quotes = 1
357 lsplit = lbuf.split("'")[-1]
357 lsplit = lbuf.split("'")[-1]
358 else:
358 else:
359 return None
359 return None
360 except IndexError:
360 except IndexError:
361 # tab pressed on empty line
361 # tab pressed on empty line
362 lsplit = ""
362 lsplit = ""
363
363
364 if lsplit != protect_filename(lsplit):
364 if lsplit != protect_filename(lsplit):
365 # if protectables are found, do matching on the whole escaped
365 # if protectables are found, do matching on the whole escaped
366 # name
366 # name
367 has_protectables = 1
367 has_protectables = 1
368 text0,text = text,lsplit
368 text0,text = text,lsplit
369 else:
369 else:
370 has_protectables = 0
370 has_protectables = 0
371 text = os.path.expanduser(text)
371 text = os.path.expanduser(text)
372
372
373 if text == "":
373 if text == "":
374 return [protect_filename(f) for f in self.glob("*")]
374 return [protect_filename(f) for f in self.glob("*")]
375
375
376 m0 = self.clean_glob(text.replace('\\',''))
376 m0 = self.clean_glob(text.replace('\\',''))
377 if has_protectables:
377 if has_protectables:
378 # If we had protectables, we need to revert our changes to the
378 # If we had protectables, we need to revert our changes to the
379 # beginning of filename so that we don't double-write the part
379 # beginning of filename so that we don't double-write the part
380 # of the filename we have so far
380 # of the filename we have so far
381 len_lsplit = len(lsplit)
381 len_lsplit = len(lsplit)
382 matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0]
382 matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0]
383 else:
383 else:
384 if open_quotes:
384 if open_quotes:
385 # if we have a string with an open quote, we don't need to
385 # if we have a string with an open quote, we don't need to
386 # protect the names at all (and we _shouldn't_, as it
386 # protect the names at all (and we _shouldn't_, as it
387 # would cause bugs when the filesystem call is made).
387 # would cause bugs when the filesystem call is made).
388 matches = m0
388 matches = m0
389 else:
389 else:
390 matches = [protect_filename(f) for f in m0]
390 matches = [protect_filename(f) for f in m0]
391 if len(matches) == 1 and os.path.isdir(matches[0]):
391 if len(matches) == 1 and os.path.isdir(matches[0]):
392 # Takes care of links to directories also. Use '/'
392 # Takes care of links to directories also. Use '/'
393 # explicitly, even under Windows, so that name completions
393 # explicitly, even under Windows, so that name completions
394 # don't end up escaped.
394 # don't end up escaped.
395 matches[0] += '/'
395 matches[0] += '/'
396 return matches
396 return matches
397
397
398 def alias_matches(self, text):
398 def alias_matches(self, text):
399 """Match internal system aliases"""
399 """Match internal system aliases"""
400 #print 'Completer->alias_matches:',text,'lb',self.lbuf # dbg
400 #print 'Completer->alias_matches:',text,'lb',self.lbuf # dbg
401
401
402 # if we are not in the first 'item', alias matching
402 # if we are not in the first 'item', alias matching
403 # doesn't make sense
403 # doesn't make sense
404 if ' ' in self.lbuf:
404 if ' ' in self.lbuf:
405 return []
405 return []
406 text = os.path.expanduser(text)
406 text = os.path.expanduser(text)
407 aliases = self.alias_table.keys()
407 aliases = self.alias_table.keys()
408 if text == "":
408 if text == "":
409 return aliases
409 return aliases
410 else:
410 else:
411 return [alias for alias in aliases if alias.startswith(text)]
411 return [alias for alias in aliases if alias.startswith(text)]
412
412
413 def python_matches(self,text):
413 def python_matches(self,text):
414 """Match attributes or global python names"""
414 """Match attributes or global python names"""
415
415
416 #print 'Completer->python_matches, txt=<%s>' % text # dbg
416 #print 'Completer->python_matches, txt=<%s>' % text # dbg
417 if "." in text:
417 if "." in text:
418 try:
418 try:
419 matches = self.attr_matches(text)
419 matches = self.attr_matches(text)
420 if text.endswith('.') and self.omit__names:
420 if text.endswith('.') and self.omit__names:
421 if self.omit__names == 1:
421 if self.omit__names == 1:
422 # true if txt is _not_ a __ name, false otherwise:
422 # true if txt is _not_ a __ name, false otherwise:
423 no__name = (lambda txt:
423 no__name = (lambda txt:
424 re.match(r'.*\.__.*?__',txt) is None)
424 re.match(r'.*\.__.*?__',txt) is None)
425 else:
425 else:
426 # true if txt is _not_ a _ name, false otherwise:
426 # true if txt is _not_ a _ name, false otherwise:
427 no__name = (lambda txt:
427 no__name = (lambda txt:
428 re.match(r'.*\._.*?',txt) is None)
428 re.match(r'.*\._.*?',txt) is None)
429 matches = filter(no__name, matches)
429 matches = filter(no__name, matches)
430 except NameError:
430 except NameError:
431 # catches <undefined attributes>.<tab>
431 # catches <undefined attributes>.<tab>
432 matches = []
432 matches = []
433 else:
433 else:
434 matches = self.global_matches(text)
434 matches = self.global_matches(text)
435 # this is so completion finds magics when automagic is on:
435 # this is so completion finds magics when automagic is on:
436 if (matches == [] and
436 if (matches == [] and
437 not text.startswith(os.sep) and
437 not text.startswith(os.sep) and
438 not ' ' in self.lbuf):
438 not ' ' in self.lbuf):
439 matches = self.attr_matches(self.magic_prefix+text)
439 matches = self.attr_matches(self.magic_prefix+text)
440 return matches
440 return matches
441
441
442 def _default_arguments(self, obj):
442 def _default_arguments(self, obj):
443 """Return the list of default arguments of obj if it is callable,
443 """Return the list of default arguments of obj if it is callable,
444 or empty list otherwise."""
444 or empty list otherwise."""
445
445
446 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
446 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
447 # for classes, check for __init__,__new__
447 # for classes, check for __init__,__new__
448 if inspect.isclass(obj):
448 if inspect.isclass(obj):
449 obj = (getattr(obj,'__init__',None) or
449 obj = (getattr(obj,'__init__',None) or
450 getattr(obj,'__new__',None))
450 getattr(obj,'__new__',None))
451 # for all others, check if they are __call__able
451 # for all others, check if they are __call__able
452 elif hasattr(obj, '__call__'):
452 elif hasattr(obj, '__call__'):
453 obj = obj.__call__
453 obj = obj.__call__
454 # XXX: is there a way to handle the builtins ?
454 # XXX: is there a way to handle the builtins ?
455 try:
455 try:
456 args,_,_1,defaults = inspect.getargspec(obj)
456 args,_,_1,defaults = inspect.getargspec(obj)
457 if defaults:
457 if defaults:
458 return args[-len(defaults):]
458 return args[-len(defaults):]
459 except TypeError: pass
459 except TypeError: pass
460 return []
460 return []
461
461
462 def python_func_kw_matches(self,text):
462 def python_func_kw_matches(self,text):
463 """Match named parameters (kwargs) of the last open function"""
463 """Match named parameters (kwargs) of the last open function"""
464
464
465 if "." in text: # a parameter cannot be dotted
465 if "." in text: # a parameter cannot be dotted
466 return []
466 return []
467 try: regexp = self.__funcParamsRegex
467 try: regexp = self.__funcParamsRegex
468 except AttributeError:
468 except AttributeError:
469 regexp = self.__funcParamsRegex = re.compile(r'''
469 regexp = self.__funcParamsRegex = re.compile(r'''
470 '.*?' | # single quoted strings or
470 '.*?' | # single quoted strings or
471 ".*?" | # double quoted strings or
471 ".*?" | # double quoted strings or
472 \w+ | # identifier
472 \w+ | # identifier
473 \S # other characters
473 \S # other characters
474 ''', re.VERBOSE | re.DOTALL)
474 ''', re.VERBOSE | re.DOTALL)
475 # 1. find the nearest identifier that comes before an unclosed
475 # 1. find the nearest identifier that comes before an unclosed
476 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
476 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
477 tokens = regexp.findall(self.get_line_buffer())
477 tokens = regexp.findall(self.get_line_buffer())
478 tokens.reverse()
478 tokens.reverse()
479 iterTokens = iter(tokens); openPar = 0
479 iterTokens = iter(tokens); openPar = 0
480 for token in iterTokens:
480 for token in iterTokens:
481 if token == ')':
481 if token == ')':
482 openPar -= 1
482 openPar -= 1
483 elif token == '(':
483 elif token == '(':
484 openPar += 1
484 openPar += 1
485 if openPar > 0:
485 if openPar > 0:
486 # found the last unclosed parenthesis
486 # found the last unclosed parenthesis
487 break
487 break
488 else:
488 else:
489 return []
489 return []
490 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
490 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
491 ids = []
491 ids = []
492 isId = re.compile(r'\w+$').match
492 isId = re.compile(r'\w+$').match
493 while True:
493 while True:
494 try:
494 try:
495 ids.append(iterTokens.next())
495 ids.append(iterTokens.next())
496 if not isId(ids[-1]):
496 if not isId(ids[-1]):
497 ids.pop(); break
497 ids.pop(); break
498 if not iterTokens.next() == '.':
498 if not iterTokens.next() == '.':
499 break
499 break
500 except StopIteration:
500 except StopIteration:
501 break
501 break
502 # lookup the candidate callable matches either using global_matches
502 # lookup the candidate callable matches either using global_matches
503 # or attr_matches for dotted names
503 # or attr_matches for dotted names
504 if len(ids) == 1:
504 if len(ids) == 1:
505 callableMatches = self.global_matches(ids[0])
505 callableMatches = self.global_matches(ids[0])
506 else:
506 else:
507 callableMatches = self.attr_matches('.'.join(ids[::-1]))
507 callableMatches = self.attr_matches('.'.join(ids[::-1]))
508 argMatches = []
508 argMatches = []
509 for callableMatch in callableMatches:
509 for callableMatch in callableMatches:
510 try: namedArgs = self._default_arguments(eval(callableMatch,
510 try: namedArgs = self._default_arguments(eval(callableMatch,
511 self.namespace))
511 self.namespace))
512 except: continue
512 except: continue
513 for namedArg in namedArgs:
513 for namedArg in namedArgs:
514 if namedArg.startswith(text):
514 if namedArg.startswith(text):
515 argMatches.append("%s=" %namedArg)
515 argMatches.append("%s=" %namedArg)
516 return argMatches
516 return argMatches
517
517
518 def complete(self, text, state):
518 def complete(self, text, state):
519 """Return the next possible completion for 'text'.
519 """Return the next possible completion for 'text'.
520
520
521 This is called successively with state == 0, 1, 2, ... until it
521 This is called successively with state == 0, 1, 2, ... until it
522 returns None. The completion should begin with 'text'. """
522 returns None. The completion should begin with 'text'. """
523
523
524 #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg
524 #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg
525
525
526 # if there is only a tab on a line with only whitespace, instead
526 # if there is only a tab on a line with only whitespace, instead
527 # of the mostly useless 'do you want to see all million
527 # of the mostly useless 'do you want to see all million
528 # completions' message, just do the right thing and give the user
528 # completions' message, just do the right thing and give the user
529 # his tab! Incidentally, this enables pasting of tabbed text from
529 # his tab! Incidentally, this enables pasting of tabbed text from
530 # an editor (as long as autoindent is off).
530 # an editor (as long as autoindent is off).
531
531
532 # don't apply this on 'dumb' terminals, such as emacs buffers, so we
532 # don't apply this on 'dumb' terminals, such as emacs buffers, so we
533 # don't interfere with their own tab-completion mechanism.
533 # don't interfere with their own tab-completion mechanism.
534 self.lbuf = self.get_line_buffer()[:self.readline.get_endidx()]
534 self.lbuf = self.get_line_buffer()[:self.readline.get_endidx()]
535 if not (self.dumb_terminal or self.get_line_buffer().strip()):
535 if not (self.dumb_terminal or self.get_line_buffer().strip()):
536 self.readline.insert_text('\t')
536 self.readline.insert_text('\t')
537 return None
537 return None
538
538
539 magic_escape = self.magic_escape
539 magic_escape = self.magic_escape
540 magic_prefix = self.magic_prefix
540 magic_prefix = self.magic_prefix
541
541
542 try:
542 try:
543 if text.startswith(magic_escape):
543 if text.startswith(magic_escape):
544 text = text.replace(magic_escape,magic_prefix)
544 text = text.replace(magic_escape,magic_prefix)
545 elif text.startswith('~'):
545 elif text.startswith('~'):
546 text = os.path.expanduser(text)
546 text = os.path.expanduser(text)
547 if state == 0:
547 if state == 0:
548 # Extend the list of completions with the results of each
548 # Extend the list of completions with the results of each
549 # matcher, so we return results to the user from all
549 # matcher, so we return results to the user from all
550 # namespaces.
550 # namespaces.
551 if self.merge_completions:
551 if self.merge_completions:
552 self.matches = []
552 self.matches = []
553 for matcher in self.matchers:
553 for matcher in self.matchers:
554 self.matches.extend(matcher(text))
554 self.matches.extend(matcher(text))
555 else:
555 else:
556 for matcher in self.matchers:
556 for matcher in self.matchers:
557 self.matches = matcher(text)
557 self.matches = matcher(text)
558 if self.matches:
558 if self.matches:
559 break
559 break
560
560
561 try:
561 try:
562 return self.matches[state].replace(magic_prefix,magic_escape)
562 return self.matches[state].replace(magic_prefix,magic_escape)
563 except IndexError:
563 except IndexError:
564 return None
564 return None
565 except:
565 except:
566 #from IPython.ultraTB import AutoFormattedTB; # dbg
566 #from IPython.ultraTB import AutoFormattedTB; # dbg
567 #tb=AutoFormattedTB('Verbose');tb() #dbg
567 #tb=AutoFormattedTB('Verbose');tb() #dbg
568
568
569 # If completion fails, don't annoy the user.
569 # If completion fails, don't annoy the user.
570 return None
570 return None
@@ -1,418 +1,419 b''
1 """Module for interactive demos using IPython.
1 """Module for interactive demos using IPython.
2
2
3 This module implements a few classes for running Python scripts interactively
3 This module implements a few classes for running Python scripts interactively
4 in IPython for demonstrations. With very simple markup (a few tags in
4 in IPython for demonstrations. With very simple markup (a few tags in
5 comments), you can control points where the script stops executing and returns
5 comments), you can control points where the script stops executing and returns
6 control to IPython.
6 control to IPython.
7
7
8 The classes are (see their docstrings for further details):
8 The classes are (see their docstrings for further details):
9
9
10 - Demo: pure python demos
10 - Demo: pure python demos
11
11
12 - IPythonDemo: demos with input to be processed by IPython as if it had been
12 - IPythonDemo: demos with input to be processed by IPython as if it had been
13 typed interactively (so magics work, as well as any other special syntax you
13 typed interactively (so magics work, as well as any other special syntax you
14 may have added via input prefilters).
14 may have added via input prefilters).
15
15
16 - LineDemo: single-line version of the Demo class. These demos are executed
16 - LineDemo: single-line version of the Demo class. These demos are executed
17 one line at a time, and require no markup.
17 one line at a time, and require no markup.
18
18
19 - IPythonLineDemo: IPython version of the LineDemo class (the demo is
19 - IPythonLineDemo: IPython version of the LineDemo class (the demo is
20 executed a line at a time, but processed via IPython).
20 executed a line at a time, but processed via IPython).
21
21
22
22
23 The file is run in its own empty namespace (though you can pass it a string of
23 The file is run in its own empty namespace (though you can pass it a string of
24 arguments as if in a command line environment, and it will see those as
24 arguments as if in a command line environment, and it will see those as
25 sys.argv). But at each stop, the global IPython namespace is updated with the
25 sys.argv). But at each stop, the global IPython namespace is updated with the
26 current internal demo namespace, so you can work interactively with the data
26 current internal demo namespace, so you can work interactively with the data
27 accumulated so far.
27 accumulated so far.
28
28
29 By default, each block of code is printed (with syntax highlighting) before
29 By default, each block of code is printed (with syntax highlighting) before
30 executing it and you have to confirm execution. This is intended to show the
30 executing it and you have to confirm execution. This is intended to show the
31 code to an audience first so you can discuss it, and only proceed with
31 code to an audience first so you can discuss it, and only proceed with
32 execution once you agree. There are a few tags which allow you to modify this
32 execution once you agree. There are a few tags which allow you to modify this
33 behavior.
33 behavior.
34
34
35 The supported tags are:
35 The supported tags are:
36
36
37 # <demo> --- stop ---
37 # <demo> --- stop ---
38
38
39 Defines block boundaries, the points where IPython stops execution of the
39 Defines block boundaries, the points where IPython stops execution of the
40 file and returns to the interactive prompt.
40 file and returns to the interactive prompt.
41
41
42 # <demo> silent
42 # <demo> silent
43
43
44 Make a block execute silently (and hence automatically). Typically used in
44 Make a block execute silently (and hence automatically). Typically used in
45 cases where you have some boilerplate or initialization code which you need
45 cases where you have some boilerplate or initialization code which you need
46 executed but do not want to be seen in the demo.
46 executed but do not want to be seen in the demo.
47
47
48 # <demo> auto
48 # <demo> auto
49
49
50 Make a block execute automatically, but still being printed. Useful for
50 Make a block execute automatically, but still being printed. Useful for
51 simple code which does not warrant discussion, since it avoids the extra
51 simple code which does not warrant discussion, since it avoids the extra
52 manual confirmation.
52 manual confirmation.
53
53
54 # <demo> auto_all
54 # <demo> auto_all
55
55
56 This tag can _only_ be in the first block, and if given it overrides the
56 This tag can _only_ be in the first block, and if given it overrides the
57 individual auto tags to make the whole demo fully automatic (no block asks
57 individual auto tags to make the whole demo fully automatic (no block asks
58 for confirmation). It can also be given at creation time (or the attribute
58 for confirmation). It can also be given at creation time (or the attribute
59 set later) to override what's in the file.
59 set later) to override what's in the file.
60
60
61 While _any_ python file can be run as a Demo instance, if there are no stop
61 While _any_ python file can be run as a Demo instance, if there are no stop
62 tags the whole file will run in a single block (no different that calling
62 tags the whole file will run in a single block (no different that calling
63 first %pycat and then %run). The minimal markup to make this useful is to
63 first %pycat and then %run). The minimal markup to make this useful is to
64 place a set of stop tags; the other tags are only there to let you fine-tune
64 place a set of stop tags; the other tags are only there to let you fine-tune
65 the execution.
65 the execution.
66
66
67 This is probably best explained with the simple example file below. You can
67 This is probably best explained with the simple example file below. You can
68 copy this into a file named ex_demo.py, and try running it via:
68 copy this into a file named ex_demo.py, and try running it via:
69
69
70 from IPython.demo import Demo
70 from IPython.demo import Demo
71 d = Demo('ex_demo.py')
71 d = Demo('ex_demo.py')
72 d() <--- Call the d object (omit the parens if you have autocall set to 2).
72 d() <--- Call the d object (omit the parens if you have autocall set to 2).
73
73
74 Each time you call the demo object, it runs the next block. The demo object
74 Each time you call the demo object, it runs the next block. The demo object
75 has a few useful methods for navigation, like again(), edit(), jump(), seek()
75 has a few useful methods for navigation, like again(), edit(), jump(), seek()
76 and back(). It can be reset for a new run via reset() or reloaded from disk
76 and back(). It can be reset for a new run via reset() or reloaded from disk
77 (in case you've edited the source) via reload(). See their docstrings below.
77 (in case you've edited the source) via reload(). See their docstrings below.
78
78
79 #################### EXAMPLE DEMO <ex_demo.py> ###############################
79 #################### EXAMPLE DEMO <ex_demo.py> ###############################
80 '''A simple interactive demo to illustrate the use of IPython's Demo class.'''
80 '''A simple interactive demo to illustrate the use of IPython's Demo class.'''
81
81
82 print 'Hello, welcome to an interactive IPython demo.'
82 print 'Hello, welcome to an interactive IPython demo.'
83
83
84 # The mark below defines a block boundary, which is a point where IPython will
84 # The mark below defines a block boundary, which is a point where IPython will
85 # stop execution and return to the interactive prompt.
85 # stop execution and return to the interactive prompt.
86 # Note that in actual interactive execution,
86 # Note that in actual interactive execution,
87 # <demo> --- stop ---
87 # <demo> --- stop ---
88
88
89 x = 1
89 x = 1
90 y = 2
90 y = 2
91
91
92 # <demo> --- stop ---
92 # <demo> --- stop ---
93
93
94 # the mark below makes this block as silent
94 # the mark below makes this block as silent
95 # <demo> silent
95 # <demo> silent
96
96
97 print 'This is a silent block, which gets executed but not printed.'
97 print 'This is a silent block, which gets executed but not printed.'
98
98
99 # <demo> --- stop ---
99 # <demo> --- stop ---
100 # <demo> auto
100 # <demo> auto
101 print 'This is an automatic block.'
101 print 'This is an automatic block.'
102 print 'It is executed without asking for confirmation, but printed.'
102 print 'It is executed without asking for confirmation, but printed.'
103 z = x+y
103 z = x+y
104
104
105 print 'z=',x
105 print 'z=',x
106
106
107 # <demo> --- stop ---
107 # <demo> --- stop ---
108 # This is just another normal block.
108 # This is just another normal block.
109 print 'z is now:', z
109 print 'z is now:', z
110
110
111 print 'bye!'
111 print 'bye!'
112 ################### END EXAMPLE DEMO <ex_demo.py> ############################
112 ################### END EXAMPLE DEMO <ex_demo.py> ############################
113 """
113 """
114 #*****************************************************************************
114 #*****************************************************************************
115 # Copyright (C) 2005-2006 Fernando Perez. <Fernando.Perez@colorado.edu>
115 # Copyright (C) 2005-2006 Fernando Perez. <Fernando.Perez@colorado.edu>
116 #
116 #
117 # Distributed under the terms of the BSD License. The full license is in
117 # Distributed under the terms of the BSD License. The full license is in
118 # the file COPYING, distributed as part of this software.
118 # the file COPYING, distributed as part of this software.
119 #
119 #
120 #*****************************************************************************
120 #*****************************************************************************
121
121
122 import exceptions
122 import exceptions
123 import os
123 import os
124 import re
124 import re
125 import shlex
125 import sys
126 import sys
126
127
127 from IPython.PyColorize import Parser
128 from IPython.PyColorize import Parser
128 from IPython.genutils import marquee, shlex_split, file_read, file_readlines
129 from IPython.genutils import marquee, file_read, file_readlines
129
130
130 __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError']
131 __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError']
131
132
132 class DemoError(exceptions.Exception): pass
133 class DemoError(exceptions.Exception): pass
133
134
134 def re_mark(mark):
135 def re_mark(mark):
135 return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE)
136 return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE)
136
137
137 class Demo:
138 class Demo:
138
139
139 re_stop = re_mark('---\s?stop\s?---')
140 re_stop = re_mark('---\s?stop\s?---')
140 re_silent = re_mark('silent')
141 re_silent = re_mark('silent')
141 re_auto = re_mark('auto')
142 re_auto = re_mark('auto')
142 re_auto_all = re_mark('auto_all')
143 re_auto_all = re_mark('auto_all')
143
144
144 def __init__(self,fname,arg_str='',auto_all=None):
145 def __init__(self,fname,arg_str='',auto_all=None):
145 """Make a new demo object. To run the demo, simply call the object.
146 """Make a new demo object. To run the demo, simply call the object.
146
147
147 See the module docstring for full details and an example (you can use
148 See the module docstring for full details and an example (you can use
148 IPython.Demo? in IPython to see it).
149 IPython.Demo? in IPython to see it).
149
150
150 Inputs:
151 Inputs:
151
152
152 - fname = filename.
153 - fname = filename.
153
154
154 Optional inputs:
155 Optional inputs:
155
156
156 - arg_str(''): a string of arguments, internally converted to a list
157 - arg_str(''): a string of arguments, internally converted to a list
157 just like sys.argv, so the demo script can see a similar
158 just like sys.argv, so the demo script can see a similar
158 environment.
159 environment.
159
160
160 - auto_all(None): global flag to run all blocks automatically without
161 - auto_all(None): global flag to run all blocks automatically without
161 confirmation. This attribute overrides the block-level tags and
162 confirmation. This attribute overrides the block-level tags and
162 applies to the whole demo. It is an attribute of the object, and
163 applies to the whole demo. It is an attribute of the object, and
163 can be changed at runtime simply by reassigning it to a boolean
164 can be changed at runtime simply by reassigning it to a boolean
164 value.
165 value.
165 """
166 """
166
167
167 self.fname = fname
168 self.fname = fname
168 self.sys_argv = [fname] + shlex_split(arg_str)
169 self.sys_argv = [fname] + shlex.split(arg_str)
169 self.auto_all = auto_all
170 self.auto_all = auto_all
170
171
171 # get a few things from ipython. While it's a bit ugly design-wise,
172 # get a few things from ipython. While it's a bit ugly design-wise,
172 # it ensures that things like color scheme and the like are always in
173 # it ensures that things like color scheme and the like are always in
173 # sync with the ipython mode being used. This class is only meant to
174 # sync with the ipython mode being used. This class is only meant to
174 # be used inside ipython anyways, so it's OK.
175 # be used inside ipython anyways, so it's OK.
175 self.ip_ns = __IPYTHON__.user_ns
176 self.ip_ns = __IPYTHON__.user_ns
176 self.ip_colorize = __IPYTHON__.pycolorize
177 self.ip_colorize = __IPYTHON__.pycolorize
177 self.ip_showtb = __IPYTHON__.showtraceback
178 self.ip_showtb = __IPYTHON__.showtraceback
178 self.ip_runlines = __IPYTHON__.runlines
179 self.ip_runlines = __IPYTHON__.runlines
179 self.shell = __IPYTHON__
180 self.shell = __IPYTHON__
180
181
181 # load user data and initialize data structures
182 # load user data and initialize data structures
182 self.reload()
183 self.reload()
183
184
184 def reload(self):
185 def reload(self):
185 """Reload source from disk and initialize state."""
186 """Reload source from disk and initialize state."""
186 # read data and parse into blocks
187 # read data and parse into blocks
187 self.src = file_read(self.fname)
188 self.src = file_read(self.fname)
188 src_b = [b.strip() for b in self.re_stop.split(self.src) if b]
189 src_b = [b.strip() for b in self.re_stop.split(self.src) if b]
189 self._silent = [bool(self.re_silent.findall(b)) for b in src_b]
190 self._silent = [bool(self.re_silent.findall(b)) for b in src_b]
190 self._auto = [bool(self.re_auto.findall(b)) for b in src_b]
191 self._auto = [bool(self.re_auto.findall(b)) for b in src_b]
191
192
192 # if auto_all is not given (def. None), we read it from the file
193 # if auto_all is not given (def. None), we read it from the file
193 if self.auto_all is None:
194 if self.auto_all is None:
194 self.auto_all = bool(self.re_auto_all.findall(src_b[0]))
195 self.auto_all = bool(self.re_auto_all.findall(src_b[0]))
195 else:
196 else:
196 self.auto_all = bool(self.auto_all)
197 self.auto_all = bool(self.auto_all)
197
198
198 # Clean the sources from all markup so it doesn't get displayed when
199 # Clean the sources from all markup so it doesn't get displayed when
199 # running the demo
200 # running the demo
200 src_blocks = []
201 src_blocks = []
201 auto_strip = lambda s: self.re_auto.sub('',s)
202 auto_strip = lambda s: self.re_auto.sub('',s)
202 for i,b in enumerate(src_b):
203 for i,b in enumerate(src_b):
203 if self._auto[i]:
204 if self._auto[i]:
204 src_blocks.append(auto_strip(b))
205 src_blocks.append(auto_strip(b))
205 else:
206 else:
206 src_blocks.append(b)
207 src_blocks.append(b)
207 # remove the auto_all marker
208 # remove the auto_all marker
208 src_blocks[0] = self.re_auto_all.sub('',src_blocks[0])
209 src_blocks[0] = self.re_auto_all.sub('',src_blocks[0])
209
210
210 self.nblocks = len(src_blocks)
211 self.nblocks = len(src_blocks)
211 self.src_blocks = src_blocks
212 self.src_blocks = src_blocks
212
213
213 # also build syntax-highlighted source
214 # also build syntax-highlighted source
214 self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
215 self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
215
216
216 # ensure clean namespace and seek offset
217 # ensure clean namespace and seek offset
217 self.reset()
218 self.reset()
218
219
219 def reset(self):
220 def reset(self):
220 """Reset the namespace and seek pointer to restart the demo"""
221 """Reset the namespace and seek pointer to restart the demo"""
221 self.user_ns = {}
222 self.user_ns = {}
222 self.finished = False
223 self.finished = False
223 self.block_index = 0
224 self.block_index = 0
224
225
225 def _validate_index(self,index):
226 def _validate_index(self,index):
226 if index<0 or index>=self.nblocks:
227 if index<0 or index>=self.nblocks:
227 raise ValueError('invalid block index %s' % index)
228 raise ValueError('invalid block index %s' % index)
228
229
229 def _get_index(self,index):
230 def _get_index(self,index):
230 """Get the current block index, validating and checking status.
231 """Get the current block index, validating and checking status.
231
232
232 Returns None if the demo is finished"""
233 Returns None if the demo is finished"""
233
234
234 if index is None:
235 if index is None:
235 if self.finished:
236 if self.finished:
236 print 'Demo finished. Use reset() if you want to rerun it.'
237 print 'Demo finished. Use reset() if you want to rerun it.'
237 return None
238 return None
238 index = self.block_index
239 index = self.block_index
239 else:
240 else:
240 self._validate_index(index)
241 self._validate_index(index)
241 return index
242 return index
242
243
243 def seek(self,index):
244 def seek(self,index):
244 """Move the current seek pointer to the given block"""
245 """Move the current seek pointer to the given block"""
245 self._validate_index(index)
246 self._validate_index(index)
246 self.block_index = index
247 self.block_index = index
247 self.finished = False
248 self.finished = False
248
249
249 def back(self,num=1):
250 def back(self,num=1):
250 """Move the seek pointer back num blocks (default is 1)."""
251 """Move the seek pointer back num blocks (default is 1)."""
251 self.seek(self.block_index-num)
252 self.seek(self.block_index-num)
252
253
253 def jump(self,num):
254 def jump(self,num):
254 """Jump a given number of blocks relative to the current one."""
255 """Jump a given number of blocks relative to the current one."""
255 self.seek(self.block_index+num)
256 self.seek(self.block_index+num)
256
257
257 def again(self):
258 def again(self):
258 """Move the seek pointer back one block and re-execute."""
259 """Move the seek pointer back one block and re-execute."""
259 self.back(1)
260 self.back(1)
260 self()
261 self()
261
262
262 def edit(self,index=None):
263 def edit(self,index=None):
263 """Edit a block.
264 """Edit a block.
264
265
265 If no number is given, use the last block executed.
266 If no number is given, use the last block executed.
266
267
267 This edits the in-memory copy of the demo, it does NOT modify the
268 This edits the in-memory copy of the demo, it does NOT modify the
268 original source file. If you want to do that, simply open the file in
269 original source file. If you want to do that, simply open the file in
269 an editor and use reload() when you make changes to the file. This
270 an editor and use reload() when you make changes to the file. This
270 method is meant to let you change a block during a demonstration for
271 method is meant to let you change a block during a demonstration for
271 explanatory purposes, without damaging your original script."""
272 explanatory purposes, without damaging your original script."""
272
273
273 index = self._get_index(index)
274 index = self._get_index(index)
274 if index is None:
275 if index is None:
275 return
276 return
276 # decrease the index by one (unless we're at the very beginning), so
277 # decrease the index by one (unless we're at the very beginning), so
277 # that the default demo.edit() call opens up the sblock we've last run
278 # that the default demo.edit() call opens up the sblock we've last run
278 if index>0:
279 if index>0:
279 index -= 1
280 index -= 1
280
281
281 filename = self.shell.mktempfile(self.src_blocks[index])
282 filename = self.shell.mktempfile(self.src_blocks[index])
282 self.shell.hooks.editor(filename,1)
283 self.shell.hooks.editor(filename,1)
283 new_block = file_read(filename)
284 new_block = file_read(filename)
284 # update the source and colored block
285 # update the source and colored block
285 self.src_blocks[index] = new_block
286 self.src_blocks[index] = new_block
286 self.src_blocks_colored[index] = self.ip_colorize(new_block)
287 self.src_blocks_colored[index] = self.ip_colorize(new_block)
287 self.block_index = index
288 self.block_index = index
288 # call to run with the newly edited index
289 # call to run with the newly edited index
289 self()
290 self()
290
291
291 def show(self,index=None):
292 def show(self,index=None):
292 """Show a single block on screen"""
293 """Show a single block on screen"""
293
294
294 index = self._get_index(index)
295 index = self._get_index(index)
295 if index is None:
296 if index is None:
296 return
297 return
297
298
298 print marquee('<%s> block # %s (%s remaining)' %
299 print marquee('<%s> block # %s (%s remaining)' %
299 (self.fname,index,self.nblocks-index-1))
300 (self.fname,index,self.nblocks-index-1))
300 print self.src_blocks_colored[index],
301 print self.src_blocks_colored[index],
301 sys.stdout.flush()
302 sys.stdout.flush()
302
303
303 def show_all(self):
304 def show_all(self):
304 """Show entire demo on screen, block by block"""
305 """Show entire demo on screen, block by block"""
305
306
306 fname = self.fname
307 fname = self.fname
307 nblocks = self.nblocks
308 nblocks = self.nblocks
308 silent = self._silent
309 silent = self._silent
309 for index,block in enumerate(self.src_blocks_colored):
310 for index,block in enumerate(self.src_blocks_colored):
310 if silent[index]:
311 if silent[index]:
311 print marquee('<%s> SILENT block # %s (%s remaining)' %
312 print marquee('<%s> SILENT block # %s (%s remaining)' %
312 (fname,index,nblocks-index-1))
313 (fname,index,nblocks-index-1))
313 else:
314 else:
314 print marquee('<%s> block # %s (%s remaining)' %
315 print marquee('<%s> block # %s (%s remaining)' %
315 (fname,index,nblocks-index-1))
316 (fname,index,nblocks-index-1))
316 print block,
317 print block,
317 sys.stdout.flush()
318 sys.stdout.flush()
318
319
319 def runlines(self,source):
320 def runlines(self,source):
320 """Execute a string with one or more lines of code"""
321 """Execute a string with one or more lines of code"""
321
322
322 exec source in self.user_ns
323 exec source in self.user_ns
323
324
324 def __call__(self,index=None):
325 def __call__(self,index=None):
325 """run a block of the demo.
326 """run a block of the demo.
326
327
327 If index is given, it should be an integer >=1 and <= nblocks. This
328 If index is given, it should be an integer >=1 and <= nblocks. This
328 means that the calling convention is one off from typical Python
329 means that the calling convention is one off from typical Python
329 lists. The reason for the inconsistency is that the demo always
330 lists. The reason for the inconsistency is that the demo always
330 prints 'Block n/N, and N is the total, so it would be very odd to use
331 prints 'Block n/N, and N is the total, so it would be very odd to use
331 zero-indexing here."""
332 zero-indexing here."""
332
333
333 index = self._get_index(index)
334 index = self._get_index(index)
334 if index is None:
335 if index is None:
335 return
336 return
336 try:
337 try:
337 next_block = self.src_blocks[index]
338 next_block = self.src_blocks[index]
338 self.block_index += 1
339 self.block_index += 1
339 if self._silent[index]:
340 if self._silent[index]:
340 print marquee('Executing silent block # %s (%s remaining)' %
341 print marquee('Executing silent block # %s (%s remaining)' %
341 (index,self.nblocks-index-1))
342 (index,self.nblocks-index-1))
342 else:
343 else:
343 self.show(index)
344 self.show(index)
344 if self.auto_all or self._auto[index]:
345 if self.auto_all or self._auto[index]:
345 print marquee('output')
346 print marquee('output')
346 else:
347 else:
347 print marquee('Press <q> to quit, <Enter> to execute...'),
348 print marquee('Press <q> to quit, <Enter> to execute...'),
348 ans = raw_input().strip()
349 ans = raw_input().strip()
349 if ans:
350 if ans:
350 print marquee('Block NOT executed')
351 print marquee('Block NOT executed')
351 return
352 return
352 try:
353 try:
353 save_argv = sys.argv
354 save_argv = sys.argv
354 sys.argv = self.sys_argv
355 sys.argv = self.sys_argv
355 self.runlines(next_block)
356 self.runlines(next_block)
356 finally:
357 finally:
357 sys.argv = save_argv
358 sys.argv = save_argv
358
359
359 except:
360 except:
360 self.ip_showtb(filename=self.fname)
361 self.ip_showtb(filename=self.fname)
361 else:
362 else:
362 self.ip_ns.update(self.user_ns)
363 self.ip_ns.update(self.user_ns)
363
364
364 if self.block_index == self.nblocks:
365 if self.block_index == self.nblocks:
365 print
366 print
366 print marquee(' END OF DEMO ')
367 print marquee(' END OF DEMO ')
367 print marquee('Use reset() if you want to rerun it.')
368 print marquee('Use reset() if you want to rerun it.')
368 self.finished = True
369 self.finished = True
369
370
370 class IPythonDemo(Demo):
371 class IPythonDemo(Demo):
371 """Class for interactive demos with IPython's input processing applied.
372 """Class for interactive demos with IPython's input processing applied.
372
373
373 This subclasses Demo, but instead of executing each block by the Python
374 This subclasses Demo, but instead of executing each block by the Python
374 interpreter (via exec), it actually calls IPython on it, so that any input
375 interpreter (via exec), it actually calls IPython on it, so that any input
375 filters which may be in place are applied to the input block.
376 filters which may be in place are applied to the input block.
376
377
377 If you have an interactive environment which exposes special input
378 If you have an interactive environment which exposes special input
378 processing, you can use this class instead to write demo scripts which
379 processing, you can use this class instead to write demo scripts which
379 operate exactly as if you had typed them interactively. The default Demo
380 operate exactly as if you had typed them interactively. The default Demo
380 class requires the input to be valid, pure Python code.
381 class requires the input to be valid, pure Python code.
381 """
382 """
382
383
383 def runlines(self,source):
384 def runlines(self,source):
384 """Execute a string with one or more lines of code"""
385 """Execute a string with one or more lines of code"""
385
386
386 self.runlines(source)
387 self.runlines(source)
387
388
388 class LineDemo(Demo):
389 class LineDemo(Demo):
389 """Demo where each line is executed as a separate block.
390 """Demo where each line is executed as a separate block.
390
391
391 The input script should be valid Python code.
392 The input script should be valid Python code.
392
393
393 This class doesn't require any markup at all, and it's meant for simple
394 This class doesn't require any markup at all, and it's meant for simple
394 scripts (with no nesting or any kind of indentation) which consist of
395 scripts (with no nesting or any kind of indentation) which consist of
395 multiple lines of input to be executed, one at a time, as if they had been
396 multiple lines of input to be executed, one at a time, as if they had been
396 typed in the interactive prompt."""
397 typed in the interactive prompt."""
397
398
398 def reload(self):
399 def reload(self):
399 """Reload source from disk and initialize state."""
400 """Reload source from disk and initialize state."""
400 # read data and parse into blocks
401 # read data and parse into blocks
401 src_b = [l for l in file_readlines(self.fname) if l.strip()]
402 src_b = [l for l in file_readlines(self.fname) if l.strip()]
402 nblocks = len(src_b)
403 nblocks = len(src_b)
403 self.src = os.linesep.join(file_readlines(self.fname))
404 self.src = os.linesep.join(file_readlines(self.fname))
404 self._silent = [False]*nblocks
405 self._silent = [False]*nblocks
405 self._auto = [True]*nblocks
406 self._auto = [True]*nblocks
406 self.auto_all = True
407 self.auto_all = True
407 self.nblocks = nblocks
408 self.nblocks = nblocks
408 self.src_blocks = src_b
409 self.src_blocks = src_b
409
410
410 # also build syntax-highlighted source
411 # also build syntax-highlighted source
411 self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
412 self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
412
413
413 # ensure clean namespace and seek offset
414 # ensure clean namespace and seek offset
414 self.reset()
415 self.reset()
415
416
416 class IPythonLineDemo(IPythonDemo,LineDemo):
417 class IPythonLineDemo(IPythonDemo,LineDemo):
417 """Variant of the LineDemo class whose input is processed by IPython."""
418 """Variant of the LineDemo class whose input is processed by IPython."""
418 pass
419 pass
@@ -1,1776 +1,1703 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 General purpose utilities.
3 General purpose utilities.
4
4
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 these things are also convenient when working at the command line.
6 these things are also convenient when working at the command line.
7
7
8 $Id: genutils.py 1217 2006-03-16 21:49:01Z fperez $"""
8 $Id: genutils.py 1314 2006-05-19 18:24:14Z fperez $"""
9
9
10 #*****************************************************************************
10 #*****************************************************************************
11 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
11 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
12 #
12 #
13 # Distributed under the terms of the BSD License. The full license is in
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
14 # the file COPYING, distributed as part of this software.
15 #*****************************************************************************
15 #*****************************************************************************
16
16
17 from __future__ import generators # 2.2 compatibility
18
19 from IPython import Release
17 from IPython import Release
20 __author__ = '%s <%s>' % Release.authors['Fernando']
18 __author__ = '%s <%s>' % Release.authors['Fernando']
21 __license__ = Release.license
19 __license__ = Release.license
22
20
23 #****************************************************************************
21 #****************************************************************************
24 # required modules from the Python standard library
22 # required modules from the Python standard library
25 import __main__
23 import __main__
26 import commands
24 import commands
27 import os
25 import os
28 import re
26 import re
29 import shlex
30 import shutil
27 import shutil
31 import sys
28 import sys
32 import tempfile
29 import tempfile
33 import time
30 import time
34 import types
31 import types
35
32
36 # Other IPython utilities
33 # Other IPython utilities
37 from IPython.Itpl import Itpl,itpl,printpl
34 from IPython.Itpl import Itpl,itpl,printpl
38 from IPython import DPyGetOpt
35 from IPython import DPyGetOpt
39 from path import path
36 from path import path
40 if os.name == "nt":
37 if os.name == "nt":
41 from IPython.winconsole import get_console_size
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 # Exceptions
41 # Exceptions
105 class Error(Exception):
42 class Error(Exception):
106 """Base class for exceptions in this module."""
43 """Base class for exceptions in this module."""
107 pass
44 pass
108
45
109 #----------------------------------------------------------------------------
46 #----------------------------------------------------------------------------
110 class IOStream:
47 class IOStream:
111 def __init__(self,stream,fallback):
48 def __init__(self,stream,fallback):
112 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
49 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
113 stream = fallback
50 stream = fallback
114 self.stream = stream
51 self.stream = stream
115 self._swrite = stream.write
52 self._swrite = stream.write
116 self.flush = stream.flush
53 self.flush = stream.flush
117
54
118 def write(self,data):
55 def write(self,data):
119 try:
56 try:
120 self._swrite(data)
57 self._swrite(data)
121 except:
58 except:
122 try:
59 try:
123 # print handles some unicode issues which may trip a plain
60 # print handles some unicode issues which may trip a plain
124 # write() call. Attempt to emulate write() by using a
61 # write() call. Attempt to emulate write() by using a
125 # trailing comma
62 # trailing comma
126 print >> self.stream, data,
63 print >> self.stream, data,
127 except:
64 except:
128 # if we get here, something is seriously broken.
65 # if we get here, something is seriously broken.
129 print >> sys.stderr, \
66 print >> sys.stderr, \
130 'ERROR - failed to write data to stream:', self.stream
67 'ERROR - failed to write data to stream:', self.stream
131
68
132 class IOTerm:
69 class IOTerm:
133 """ Term holds the file or file-like objects for handling I/O operations.
70 """ Term holds the file or file-like objects for handling I/O operations.
134
71
135 These are normally just sys.stdin, sys.stdout and sys.stderr but for
72 These are normally just sys.stdin, sys.stdout and sys.stderr but for
136 Windows they can can replaced to allow editing the strings before they are
73 Windows they can can replaced to allow editing the strings before they are
137 displayed."""
74 displayed."""
138
75
139 # In the future, having IPython channel all its I/O operations through
76 # In the future, having IPython channel all its I/O operations through
140 # this class will make it easier to embed it into other environments which
77 # this class will make it easier to embed it into other environments which
141 # are not a normal terminal (such as a GUI-based shell)
78 # are not a normal terminal (such as a GUI-based shell)
142 def __init__(self,cin=None,cout=None,cerr=None):
79 def __init__(self,cin=None,cout=None,cerr=None):
143 self.cin = IOStream(cin,sys.stdin)
80 self.cin = IOStream(cin,sys.stdin)
144 self.cout = IOStream(cout,sys.stdout)
81 self.cout = IOStream(cout,sys.stdout)
145 self.cerr = IOStream(cerr,sys.stderr)
82 self.cerr = IOStream(cerr,sys.stderr)
146
83
147 # Global variable to be used for all I/O
84 # Global variable to be used for all I/O
148 Term = IOTerm()
85 Term = IOTerm()
149
86
150 import IPython.rlineimpl as readline
87 import IPython.rlineimpl as readline
151 # Remake Term to use the readline i/o facilities
88 # Remake Term to use the readline i/o facilities
152 if sys.platform == 'win32' and readline.have_readline:
89 if sys.platform == 'win32' and readline.have_readline:
153
90
154 Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile)
91 Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile)
155
92
156
93
157 #****************************************************************************
94 #****************************************************************************
158 # Generic warning/error printer, used by everything else
95 # Generic warning/error printer, used by everything else
159 def warn(msg,level=2,exit_val=1):
96 def warn(msg,level=2,exit_val=1):
160 """Standard warning printer. Gives formatting consistency.
97 """Standard warning printer. Gives formatting consistency.
161
98
162 Output is sent to Term.cerr (sys.stderr by default).
99 Output is sent to Term.cerr (sys.stderr by default).
163
100
164 Options:
101 Options:
165
102
166 -level(2): allows finer control:
103 -level(2): allows finer control:
167 0 -> Do nothing, dummy function.
104 0 -> Do nothing, dummy function.
168 1 -> Print message.
105 1 -> Print message.
169 2 -> Print 'WARNING:' + message. (Default level).
106 2 -> Print 'WARNING:' + message. (Default level).
170 3 -> Print 'ERROR:' + message.
107 3 -> Print 'ERROR:' + message.
171 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
108 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
172
109
173 -exit_val (1): exit value returned by sys.exit() for a level 4
110 -exit_val (1): exit value returned by sys.exit() for a level 4
174 warning. Ignored for all other levels."""
111 warning. Ignored for all other levels."""
175
112
176 if level>0:
113 if level>0:
177 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
114 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
178 print >> Term.cerr, '%s%s' % (header[level],msg)
115 print >> Term.cerr, '%s%s' % (header[level],msg)
179 if level == 4:
116 if level == 4:
180 print >> Term.cerr,'Exiting.\n'
117 print >> Term.cerr,'Exiting.\n'
181 sys.exit(exit_val)
118 sys.exit(exit_val)
182
119
183 def info(msg):
120 def info(msg):
184 """Equivalent to warn(msg,level=1)."""
121 """Equivalent to warn(msg,level=1)."""
185
122
186 warn(msg,level=1)
123 warn(msg,level=1)
187
124
188 def error(msg):
125 def error(msg):
189 """Equivalent to warn(msg,level=3)."""
126 """Equivalent to warn(msg,level=3)."""
190
127
191 warn(msg,level=3)
128 warn(msg,level=3)
192
129
193 def fatal(msg,exit_val=1):
130 def fatal(msg,exit_val=1):
194 """Equivalent to warn(msg,exit_val=exit_val,level=4)."""
131 """Equivalent to warn(msg,exit_val=exit_val,level=4)."""
195
132
196 warn(msg,exit_val=exit_val,level=4)
133 warn(msg,exit_val=exit_val,level=4)
197
134
198 #---------------------------------------------------------------------------
135 #---------------------------------------------------------------------------
199 # Debugging routines
136 # Debugging routines
200 #
137 #
201 def debugx(expr,pre_msg=''):
138 def debugx(expr,pre_msg=''):
202 """Print the value of an expression from the caller's frame.
139 """Print the value of an expression from the caller's frame.
203
140
204 Takes an expression, evaluates it in the caller's frame and prints both
141 Takes an expression, evaluates it in the caller's frame and prints both
205 the given expression and the resulting value (as well as a debug mark
142 the given expression and the resulting value (as well as a debug mark
206 indicating the name of the calling function. The input must be of a form
143 indicating the name of the calling function. The input must be of a form
207 suitable for eval().
144 suitable for eval().
208
145
209 An optional message can be passed, which will be prepended to the printed
146 An optional message can be passed, which will be prepended to the printed
210 expr->value pair."""
147 expr->value pair."""
211
148
212 cf = sys._getframe(1)
149 cf = sys._getframe(1)
213 print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr,
150 print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr,
214 eval(expr,cf.f_globals,cf.f_locals))
151 eval(expr,cf.f_globals,cf.f_locals))
215
152
216 # deactivate it by uncommenting the following line, which makes it a no-op
153 # deactivate it by uncommenting the following line, which makes it a no-op
217 #def debugx(expr,pre_msg=''): pass
154 #def debugx(expr,pre_msg=''): pass
218
155
219 #----------------------------------------------------------------------------
156 #----------------------------------------------------------------------------
220 StringTypes = types.StringTypes
157 StringTypes = types.StringTypes
221
158
222 # Basic timing functionality
159 # Basic timing functionality
223
160
224 # If possible (Unix), use the resource module instead of time.clock()
161 # If possible (Unix), use the resource module instead of time.clock()
225 try:
162 try:
226 import resource
163 import resource
227 def clock():
164 def clock():
228 """clock() -> floating point number
165 """clock() -> floating point number
229
166
230 Return the CPU time in seconds (user time only, system time is
167 Return the CPU time in seconds (user time only, system time is
231 ignored) since the start of the process. This is done via a call to
168 ignored) since the start of the process. This is done via a call to
232 resource.getrusage, so it avoids the wraparound problems in
169 resource.getrusage, so it avoids the wraparound problems in
233 time.clock()."""
170 time.clock()."""
234
171
235 return resource.getrusage(resource.RUSAGE_SELF)[0]
172 return resource.getrusage(resource.RUSAGE_SELF)[0]
236
173
237 def clock2():
174 def clock2():
238 """clock2() -> (t_user,t_system)
175 """clock2() -> (t_user,t_system)
239
176
240 Similar to clock(), but return a tuple of user/system times."""
177 Similar to clock(), but return a tuple of user/system times."""
241 return resource.getrusage(resource.RUSAGE_SELF)[:2]
178 return resource.getrusage(resource.RUSAGE_SELF)[:2]
242
179
243 except ImportError:
180 except ImportError:
244 clock = time.clock
181 clock = time.clock
245 def clock2():
182 def clock2():
246 """Under windows, system CPU time can't be measured.
183 """Under windows, system CPU time can't be measured.
247
184
248 This just returns clock() and zero."""
185 This just returns clock() and zero."""
249 return time.clock(),0.0
186 return time.clock(),0.0
250
187
251 def timings_out(reps,func,*args,**kw):
188 def timings_out(reps,func,*args,**kw):
252 """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
189 """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
253
190
254 Execute a function reps times, return a tuple with the elapsed total
191 Execute a function reps times, return a tuple with the elapsed total
255 CPU time in seconds, the time per call and the function's output.
192 CPU time in seconds, the time per call and the function's output.
256
193
257 Under Unix, the return value is the sum of user+system time consumed by
194 Under Unix, the return value is the sum of user+system time consumed by
258 the process, computed via the resource module. This prevents problems
195 the process, computed via the resource module. This prevents problems
259 related to the wraparound effect which the time.clock() function has.
196 related to the wraparound effect which the time.clock() function has.
260
197
261 Under Windows the return value is in wall clock seconds. See the
198 Under Windows the return value is in wall clock seconds. See the
262 documentation for the time module for more details."""
199 documentation for the time module for more details."""
263
200
264 reps = int(reps)
201 reps = int(reps)
265 assert reps >=1, 'reps must be >= 1'
202 assert reps >=1, 'reps must be >= 1'
266 if reps==1:
203 if reps==1:
267 start = clock()
204 start = clock()
268 out = func(*args,**kw)
205 out = func(*args,**kw)
269 tot_time = clock()-start
206 tot_time = clock()-start
270 else:
207 else:
271 rng = xrange(reps-1) # the last time is executed separately to store output
208 rng = xrange(reps-1) # the last time is executed separately to store output
272 start = clock()
209 start = clock()
273 for dummy in rng: func(*args,**kw)
210 for dummy in rng: func(*args,**kw)
274 out = func(*args,**kw) # one last time
211 out = func(*args,**kw) # one last time
275 tot_time = clock()-start
212 tot_time = clock()-start
276 av_time = tot_time / reps
213 av_time = tot_time / reps
277 return tot_time,av_time,out
214 return tot_time,av_time,out
278
215
279 def timings(reps,func,*args,**kw):
216 def timings(reps,func,*args,**kw):
280 """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
217 """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
281
218
282 Execute a function reps times, return a tuple with the elapsed total CPU
219 Execute a function reps times, return a tuple with the elapsed total CPU
283 time in seconds and the time per call. These are just the first two values
220 time in seconds and the time per call. These are just the first two values
284 in timings_out()."""
221 in timings_out()."""
285
222
286 return timings_out(reps,func,*args,**kw)[0:2]
223 return timings_out(reps,func,*args,**kw)[0:2]
287
224
288 def timing(func,*args,**kw):
225 def timing(func,*args,**kw):
289 """timing(func,*args,**kw) -> t_total
226 """timing(func,*args,**kw) -> t_total
290
227
291 Execute a function once, return the elapsed total CPU time in
228 Execute a function once, return the elapsed total CPU time in
292 seconds. This is just the first value in timings_out()."""
229 seconds. This is just the first value in timings_out()."""
293
230
294 return timings_out(1,func,*args,**kw)[0]
231 return timings_out(1,func,*args,**kw)[0]
295
232
296 #****************************************************************************
233 #****************************************************************************
297 # file and system
234 # file and system
298
235
299 def system(cmd,verbose=0,debug=0,header=''):
236 def system(cmd,verbose=0,debug=0,header=''):
300 """Execute a system command, return its exit status.
237 """Execute a system command, return its exit status.
301
238
302 Options:
239 Options:
303
240
304 - verbose (0): print the command to be executed.
241 - verbose (0): print the command to be executed.
305
242
306 - debug (0): only print, do not actually execute.
243 - debug (0): only print, do not actually execute.
307
244
308 - header (''): Header to print on screen prior to the executed command (it
245 - header (''): Header to print on screen prior to the executed command (it
309 is only prepended to the command, no newlines are added).
246 is only prepended to the command, no newlines are added).
310
247
311 Note: a stateful version of this function is available through the
248 Note: a stateful version of this function is available through the
312 SystemExec class."""
249 SystemExec class."""
313
250
314 stat = 0
251 stat = 0
315 if verbose or debug: print header+cmd
252 if verbose or debug: print header+cmd
316 sys.stdout.flush()
253 sys.stdout.flush()
317 if not debug: stat = os.system(cmd)
254 if not debug: stat = os.system(cmd)
318 return stat
255 return stat
319
256
320 # This function is used by ipython in a lot of places to make system calls.
257 # This function is used by ipython in a lot of places to make system calls.
321 # We need it to be slightly different under win32, due to the vagaries of
258 # We need it to be slightly different under win32, due to the vagaries of
322 # 'network shares'. A win32 override is below.
259 # 'network shares'. A win32 override is below.
323
260
324 def shell(cmd,verbose=0,debug=0,header=''):
261 def shell(cmd,verbose=0,debug=0,header=''):
325 """Execute a command in the system shell, always return None.
262 """Execute a command in the system shell, always return None.
326
263
327 Options:
264 Options:
328
265
329 - verbose (0): print the command to be executed.
266 - verbose (0): print the command to be executed.
330
267
331 - debug (0): only print, do not actually execute.
268 - debug (0): only print, do not actually execute.
332
269
333 - header (''): Header to print on screen prior to the executed command (it
270 - header (''): Header to print on screen prior to the executed command (it
334 is only prepended to the command, no newlines are added).
271 is only prepended to the command, no newlines are added).
335
272
336 Note: this is similar to genutils.system(), but it returns None so it can
273 Note: this is similar to genutils.system(), but it returns None so it can
337 be conveniently used in interactive loops without getting the return value
274 be conveniently used in interactive loops without getting the return value
338 (typically 0) printed many times."""
275 (typically 0) printed many times."""
339
276
340 stat = 0
277 stat = 0
341 if verbose or debug: print header+cmd
278 if verbose or debug: print header+cmd
342 # flush stdout so we don't mangle python's buffering
279 # flush stdout so we don't mangle python's buffering
343 sys.stdout.flush()
280 sys.stdout.flush()
344 if not debug:
281 if not debug:
345 os.system(cmd)
282 os.system(cmd)
346
283
347 # override shell() for win32 to deal with network shares
284 # override shell() for win32 to deal with network shares
348 if os.name in ('nt','dos'):
285 if os.name in ('nt','dos'):
349
286
350 shell_ori = shell
287 shell_ori = shell
351
288
352 def shell(cmd,verbose=0,debug=0,header=''):
289 def shell(cmd,verbose=0,debug=0,header=''):
353 if os.getcwd().startswith(r"\\"):
290 if os.getcwd().startswith(r"\\"):
354 path = os.getcwd()
291 path = os.getcwd()
355 # change to c drive (cannot be on UNC-share when issuing os.system,
292 # change to c drive (cannot be on UNC-share when issuing os.system,
356 # as cmd.exe cannot handle UNC addresses)
293 # as cmd.exe cannot handle UNC addresses)
357 os.chdir("c:")
294 os.chdir("c:")
358 # issue pushd to the UNC-share and then run the command
295 # issue pushd to the UNC-share and then run the command
359 try:
296 try:
360 shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header)
297 shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header)
361 finally:
298 finally:
362 os.chdir(path)
299 os.chdir(path)
363 else:
300 else:
364 shell_ori(cmd,verbose,debug,header)
301 shell_ori(cmd,verbose,debug,header)
365
302
366 shell.__doc__ = shell_ori.__doc__
303 shell.__doc__ = shell_ori.__doc__
367
304
368 def getoutput(cmd,verbose=0,debug=0,header='',split=0):
305 def getoutput(cmd,verbose=0,debug=0,header='',split=0):
369 """Dummy substitute for perl's backquotes.
306 """Dummy substitute for perl's backquotes.
370
307
371 Executes a command and returns the output.
308 Executes a command and returns the output.
372
309
373 Accepts the same arguments as system(), plus:
310 Accepts the same arguments as system(), plus:
374
311
375 - split(0): if true, the output is returned as a list split on newlines.
312 - split(0): if true, the output is returned as a list split on newlines.
376
313
377 Note: a stateful version of this function is available through the
314 Note: a stateful version of this function is available through the
378 SystemExec class.
315 SystemExec class.
379
316
380 This is pretty much deprecated and rarely used,
317 This is pretty much deprecated and rarely used,
381 genutils.getoutputerror may be what you need.
318 genutils.getoutputerror may be what you need.
382
319
383 """
320 """
384
321
385 if verbose or debug: print header+cmd
322 if verbose or debug: print header+cmd
386 if not debug:
323 if not debug:
387 output = os.popen(cmd).read()
324 output = os.popen(cmd).read()
388 # stipping last \n is here for backwards compat.
325 # stipping last \n is here for backwards compat.
389 if output.endswith('\n'):
326 if output.endswith('\n'):
390 output = output[:-1]
327 output = output[:-1]
391 if split:
328 if split:
392 return output.split('\n')
329 return output.split('\n')
393 else:
330 else:
394 return output
331 return output
395
332
396 def getoutputerror(cmd,verbose=0,debug=0,header='',split=0):
333 def getoutputerror(cmd,verbose=0,debug=0,header='',split=0):
397 """Return (standard output,standard error) of executing cmd in a shell.
334 """Return (standard output,standard error) of executing cmd in a shell.
398
335
399 Accepts the same arguments as system(), plus:
336 Accepts the same arguments as system(), plus:
400
337
401 - split(0): if true, each of stdout/err is returned as a list split on
338 - split(0): if true, each of stdout/err is returned as a list split on
402 newlines.
339 newlines.
403
340
404 Note: a stateful version of this function is available through the
341 Note: a stateful version of this function is available through the
405 SystemExec class."""
342 SystemExec class."""
406
343
407 if verbose or debug: print header+cmd
344 if verbose or debug: print header+cmd
408 if not cmd:
345 if not cmd:
409 if split:
346 if split:
410 return [],[]
347 return [],[]
411 else:
348 else:
412 return '',''
349 return '',''
413 if not debug:
350 if not debug:
414 pin,pout,perr = os.popen3(cmd)
351 pin,pout,perr = os.popen3(cmd)
415 tout = pout.read().rstrip()
352 tout = pout.read().rstrip()
416 terr = perr.read().rstrip()
353 terr = perr.read().rstrip()
417 pin.close()
354 pin.close()
418 pout.close()
355 pout.close()
419 perr.close()
356 perr.close()
420 if split:
357 if split:
421 return tout.split('\n'),terr.split('\n')
358 return tout.split('\n'),terr.split('\n')
422 else:
359 else:
423 return tout,terr
360 return tout,terr
424
361
425 # for compatibility with older naming conventions
362 # for compatibility with older naming conventions
426 xsys = system
363 xsys = system
427 bq = getoutput
364 bq = getoutput
428
365
429 class SystemExec:
366 class SystemExec:
430 """Access the system and getoutput functions through a stateful interface.
367 """Access the system and getoutput functions through a stateful interface.
431
368
432 Note: here we refer to the system and getoutput functions from this
369 Note: here we refer to the system and getoutput functions from this
433 library, not the ones from the standard python library.
370 library, not the ones from the standard python library.
434
371
435 This class offers the system and getoutput functions as methods, but the
372 This class offers the system and getoutput functions as methods, but the
436 verbose, debug and header parameters can be set for the instance (at
373 verbose, debug and header parameters can be set for the instance (at
437 creation time or later) so that they don't need to be specified on each
374 creation time or later) so that they don't need to be specified on each
438 call.
375 call.
439
376
440 For efficiency reasons, there's no way to override the parameters on a
377 For efficiency reasons, there's no way to override the parameters on a
441 per-call basis other than by setting instance attributes. If you need
378 per-call basis other than by setting instance attributes. If you need
442 local overrides, it's best to directly call system() or getoutput().
379 local overrides, it's best to directly call system() or getoutput().
443
380
444 The following names are provided as alternate options:
381 The following names are provided as alternate options:
445 - xsys: alias to system
382 - xsys: alias to system
446 - bq: alias to getoutput
383 - bq: alias to getoutput
447
384
448 An instance can then be created as:
385 An instance can then be created as:
449 >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ')
386 >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ')
450
387
451 And used as:
388 And used as:
452 >>> sysexec.xsys('pwd')
389 >>> sysexec.xsys('pwd')
453 >>> dirlist = sysexec.bq('ls -l')
390 >>> dirlist = sysexec.bq('ls -l')
454 """
391 """
455
392
456 def __init__(self,verbose=0,debug=0,header='',split=0):
393 def __init__(self,verbose=0,debug=0,header='',split=0):
457 """Specify the instance's values for verbose, debug and header."""
394 """Specify the instance's values for verbose, debug and header."""
458 setattr_list(self,'verbose debug header split')
395 setattr_list(self,'verbose debug header split')
459
396
460 def system(self,cmd):
397 def system(self,cmd):
461 """Stateful interface to system(), with the same keyword parameters."""
398 """Stateful interface to system(), with the same keyword parameters."""
462
399
463 system(cmd,self.verbose,self.debug,self.header)
400 system(cmd,self.verbose,self.debug,self.header)
464
401
465 def shell(self,cmd):
402 def shell(self,cmd):
466 """Stateful interface to shell(), with the same keyword parameters."""
403 """Stateful interface to shell(), with the same keyword parameters."""
467
404
468 shell(cmd,self.verbose,self.debug,self.header)
405 shell(cmd,self.verbose,self.debug,self.header)
469
406
470 xsys = system # alias
407 xsys = system # alias
471
408
472 def getoutput(self,cmd):
409 def getoutput(self,cmd):
473 """Stateful interface to getoutput()."""
410 """Stateful interface to getoutput()."""
474
411
475 return getoutput(cmd,self.verbose,self.debug,self.header,self.split)
412 return getoutput(cmd,self.verbose,self.debug,self.header,self.split)
476
413
477 def getoutputerror(self,cmd):
414 def getoutputerror(self,cmd):
478 """Stateful interface to getoutputerror()."""
415 """Stateful interface to getoutputerror()."""
479
416
480 return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split)
417 return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split)
481
418
482 bq = getoutput # alias
419 bq = getoutput # alias
483
420
484 #-----------------------------------------------------------------------------
421 #-----------------------------------------------------------------------------
485 def mutex_opts(dict,ex_op):
422 def mutex_opts(dict,ex_op):
486 """Check for presence of mutually exclusive keys in a dict.
423 """Check for presence of mutually exclusive keys in a dict.
487
424
488 Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]"""
425 Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]"""
489 for op1,op2 in ex_op:
426 for op1,op2 in ex_op:
490 if op1 in dict and op2 in dict:
427 if op1 in dict and op2 in dict:
491 raise ValueError,'\n*** ERROR in Arguments *** '\
428 raise ValueError,'\n*** ERROR in Arguments *** '\
492 'Options '+op1+' and '+op2+' are mutually exclusive.'
429 'Options '+op1+' and '+op2+' are mutually exclusive.'
493
430
494 #-----------------------------------------------------------------------------
431 #-----------------------------------------------------------------------------
495 def get_py_filename(name):
432 def get_py_filename(name):
496 """Return a valid python filename in the current directory.
433 """Return a valid python filename in the current directory.
497
434
498 If the given name is not a file, it adds '.py' and searches again.
435 If the given name is not a file, it adds '.py' and searches again.
499 Raises IOError with an informative message if the file isn't found."""
436 Raises IOError with an informative message if the file isn't found."""
500
437
501 name = os.path.expanduser(name)
438 name = os.path.expanduser(name)
502 if not os.path.isfile(name) and not name.endswith('.py'):
439 if not os.path.isfile(name) and not name.endswith('.py'):
503 name += '.py'
440 name += '.py'
504 if os.path.isfile(name):
441 if os.path.isfile(name):
505 return name
442 return name
506 else:
443 else:
507 raise IOError,'File `%s` not found.' % name
444 raise IOError,'File `%s` not found.' % name
508
445
509 #-----------------------------------------------------------------------------
446 #-----------------------------------------------------------------------------
510 def filefind(fname,alt_dirs = None):
447 def filefind(fname,alt_dirs = None):
511 """Return the given filename either in the current directory, if it
448 """Return the given filename either in the current directory, if it
512 exists, or in a specified list of directories.
449 exists, or in a specified list of directories.
513
450
514 ~ expansion is done on all file and directory names.
451 ~ expansion is done on all file and directory names.
515
452
516 Upon an unsuccessful search, raise an IOError exception."""
453 Upon an unsuccessful search, raise an IOError exception."""
517
454
518 if alt_dirs is None:
455 if alt_dirs is None:
519 try:
456 try:
520 alt_dirs = get_home_dir()
457 alt_dirs = get_home_dir()
521 except HomeDirError:
458 except HomeDirError:
522 alt_dirs = os.getcwd()
459 alt_dirs = os.getcwd()
523 search = [fname] + list_strings(alt_dirs)
460 search = [fname] + list_strings(alt_dirs)
524 search = map(os.path.expanduser,search)
461 search = map(os.path.expanduser,search)
525 #print 'search list for',fname,'list:',search # dbg
462 #print 'search list for',fname,'list:',search # dbg
526 fname = search[0]
463 fname = search[0]
527 if os.path.isfile(fname):
464 if os.path.isfile(fname):
528 return fname
465 return fname
529 for direc in search[1:]:
466 for direc in search[1:]:
530 testname = os.path.join(direc,fname)
467 testname = os.path.join(direc,fname)
531 #print 'testname',testname # dbg
468 #print 'testname',testname # dbg
532 if os.path.isfile(testname):
469 if os.path.isfile(testname):
533 return testname
470 return testname
534 raise IOError,'File' + `fname` + \
471 raise IOError,'File' + `fname` + \
535 ' not found in current or supplied directories:' + `alt_dirs`
472 ' not found in current or supplied directories:' + `alt_dirs`
536
473
537 #----------------------------------------------------------------------------
474 #----------------------------------------------------------------------------
538 def file_read(filename):
475 def file_read(filename):
539 """Read a file and close it. Returns the file source."""
476 """Read a file and close it. Returns the file source."""
540 fobj = open(filename,'r');
477 fobj = open(filename,'r');
541 source = fobj.read();
478 source = fobj.read();
542 fobj.close()
479 fobj.close()
543 return source
480 return source
544
481
545 def file_readlines(filename):
482 def file_readlines(filename):
546 """Read a file and close it. Returns the file source using readlines()."""
483 """Read a file and close it. Returns the file source using readlines()."""
547 fobj = open(filename,'r');
484 fobj = open(filename,'r');
548 lines = fobj.readlines();
485 lines = fobj.readlines();
549 fobj.close()
486 fobj.close()
550 return lines
487 return lines
551
488
552 #----------------------------------------------------------------------------
489 #----------------------------------------------------------------------------
553 def target_outdated(target,deps):
490 def target_outdated(target,deps):
554 """Determine whether a target is out of date.
491 """Determine whether a target is out of date.
555
492
556 target_outdated(target,deps) -> 1/0
493 target_outdated(target,deps) -> 1/0
557
494
558 deps: list of filenames which MUST exist.
495 deps: list of filenames which MUST exist.
559 target: single filename which may or may not exist.
496 target: single filename which may or may not exist.
560
497
561 If target doesn't exist or is older than any file listed in deps, return
498 If target doesn't exist or is older than any file listed in deps, return
562 true, otherwise return false.
499 true, otherwise return false.
563 """
500 """
564 try:
501 try:
565 target_time = os.path.getmtime(target)
502 target_time = os.path.getmtime(target)
566 except os.error:
503 except os.error:
567 return 1
504 return 1
568 for dep in deps:
505 for dep in deps:
569 dep_time = os.path.getmtime(dep)
506 dep_time = os.path.getmtime(dep)
570 if dep_time > target_time:
507 if dep_time > target_time:
571 #print "For target",target,"Dep failed:",dep # dbg
508 #print "For target",target,"Dep failed:",dep # dbg
572 #print "times (dep,tar):",dep_time,target_time # dbg
509 #print "times (dep,tar):",dep_time,target_time # dbg
573 return 1
510 return 1
574 return 0
511 return 0
575
512
576 #-----------------------------------------------------------------------------
513 #-----------------------------------------------------------------------------
577 def target_update(target,deps,cmd):
514 def target_update(target,deps,cmd):
578 """Update a target with a given command given a list of dependencies.
515 """Update a target with a given command given a list of dependencies.
579
516
580 target_update(target,deps,cmd) -> runs cmd if target is outdated.
517 target_update(target,deps,cmd) -> runs cmd if target is outdated.
581
518
582 This is just a wrapper around target_outdated() which calls the given
519 This is just a wrapper around target_outdated() which calls the given
583 command if target is outdated."""
520 command if target is outdated."""
584
521
585 if target_outdated(target,deps):
522 if target_outdated(target,deps):
586 xsys(cmd)
523 xsys(cmd)
587
524
588 #----------------------------------------------------------------------------
525 #----------------------------------------------------------------------------
589 def unquote_ends(istr):
526 def unquote_ends(istr):
590 """Remove a single pair of quotes from the endpoints of a string."""
527 """Remove a single pair of quotes from the endpoints of a string."""
591
528
592 if not istr:
529 if not istr:
593 return istr
530 return istr
594 if (istr[0]=="'" and istr[-1]=="'") or \
531 if (istr[0]=="'" and istr[-1]=="'") or \
595 (istr[0]=='"' and istr[-1]=='"'):
532 (istr[0]=='"' and istr[-1]=='"'):
596 return istr[1:-1]
533 return istr[1:-1]
597 else:
534 else:
598 return istr
535 return istr
599
536
600 #----------------------------------------------------------------------------
537 #----------------------------------------------------------------------------
601 def process_cmdline(argv,names=[],defaults={},usage=''):
538 def process_cmdline(argv,names=[],defaults={},usage=''):
602 """ Process command-line options and arguments.
539 """ Process command-line options and arguments.
603
540
604 Arguments:
541 Arguments:
605
542
606 - argv: list of arguments, typically sys.argv.
543 - argv: list of arguments, typically sys.argv.
607
544
608 - names: list of option names. See DPyGetOpt docs for details on options
545 - names: list of option names. See DPyGetOpt docs for details on options
609 syntax.
546 syntax.
610
547
611 - defaults: dict of default values.
548 - defaults: dict of default values.
612
549
613 - usage: optional usage notice to print if a wrong argument is passed.
550 - usage: optional usage notice to print if a wrong argument is passed.
614
551
615 Return a dict of options and a list of free arguments."""
552 Return a dict of options and a list of free arguments."""
616
553
617 getopt = DPyGetOpt.DPyGetOpt()
554 getopt = DPyGetOpt.DPyGetOpt()
618 getopt.setIgnoreCase(0)
555 getopt.setIgnoreCase(0)
619 getopt.parseConfiguration(names)
556 getopt.parseConfiguration(names)
620
557
621 try:
558 try:
622 getopt.processArguments(argv)
559 getopt.processArguments(argv)
623 except:
560 except:
624 print usage
561 print usage
625 warn(`sys.exc_value`,level=4)
562 warn(`sys.exc_value`,level=4)
626
563
627 defaults.update(getopt.optionValues)
564 defaults.update(getopt.optionValues)
628 args = getopt.freeValues
565 args = getopt.freeValues
629
566
630 return defaults,args
567 return defaults,args
631
568
632 #----------------------------------------------------------------------------
569 #----------------------------------------------------------------------------
633 def optstr2types(ostr):
570 def optstr2types(ostr):
634 """Convert a string of option names to a dict of type mappings.
571 """Convert a string of option names to a dict of type mappings.
635
572
636 optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'}
573 optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'}
637
574
638 This is used to get the types of all the options in a string formatted
575 This is used to get the types of all the options in a string formatted
639 with the conventions of DPyGetOpt. The 'type' None is used for options
576 with the conventions of DPyGetOpt. The 'type' None is used for options
640 which are strings (they need no further conversion). This function's main
577 which are strings (they need no further conversion). This function's main
641 use is to get a typemap for use with read_dict().
578 use is to get a typemap for use with read_dict().
642 """
579 """
643
580
644 typeconv = {None:'',int:'',float:''}
581 typeconv = {None:'',int:'',float:''}
645 typemap = {'s':None,'i':int,'f':float}
582 typemap = {'s':None,'i':int,'f':float}
646 opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)')
583 opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)')
647
584
648 for w in ostr.split():
585 for w in ostr.split():
649 oname,alias,otype = opt_re.match(w).groups()
586 oname,alias,otype = opt_re.match(w).groups()
650 if otype == '' or alias == '!': # simple switches are integers too
587 if otype == '' or alias == '!': # simple switches are integers too
651 otype = 'i'
588 otype = 'i'
652 typeconv[typemap[otype]] += oname + ' '
589 typeconv[typemap[otype]] += oname + ' '
653 return typeconv
590 return typeconv
654
591
655 #----------------------------------------------------------------------------
592 #----------------------------------------------------------------------------
656 def read_dict(filename,type_conv=None,**opt):
593 def read_dict(filename,type_conv=None,**opt):
657
594
658 """Read a dictionary of key=value pairs from an input file, optionally
595 """Read a dictionary of key=value pairs from an input file, optionally
659 performing conversions on the resulting values.
596 performing conversions on the resulting values.
660
597
661 read_dict(filename,type_conv,**opt) -> dict
598 read_dict(filename,type_conv,**opt) -> dict
662
599
663 Only one value per line is accepted, the format should be
600 Only one value per line is accepted, the format should be
664 # optional comments are ignored
601 # optional comments are ignored
665 key value\n
602 key value\n
666
603
667 Args:
604 Args:
668
605
669 - type_conv: A dictionary specifying which keys need to be converted to
606 - type_conv: A dictionary specifying which keys need to be converted to
670 which types. By default all keys are read as strings. This dictionary
607 which types. By default all keys are read as strings. This dictionary
671 should have as its keys valid conversion functions for strings
608 should have as its keys valid conversion functions for strings
672 (int,long,float,complex, or your own). The value for each key
609 (int,long,float,complex, or your own). The value for each key
673 (converter) should be a whitespace separated string containing the names
610 (converter) should be a whitespace separated string containing the names
674 of all the entries in the file to be converted using that function. For
611 of all the entries in the file to be converted using that function. For
675 keys to be left alone, use None as the conversion function (only needed
612 keys to be left alone, use None as the conversion function (only needed
676 with purge=1, see below).
613 with purge=1, see below).
677
614
678 - opt: dictionary with extra options as below (default in parens)
615 - opt: dictionary with extra options as below (default in parens)
679
616
680 purge(0): if set to 1, all keys *not* listed in type_conv are purged out
617 purge(0): if set to 1, all keys *not* listed in type_conv are purged out
681 of the dictionary to be returned. If purge is going to be used, the
618 of the dictionary to be returned. If purge is going to be used, the
682 set of keys to be left as strings also has to be explicitly specified
619 set of keys to be left as strings also has to be explicitly specified
683 using the (non-existent) conversion function None.
620 using the (non-existent) conversion function None.
684
621
685 fs(None): field separator. This is the key/value separator to be used
622 fs(None): field separator. This is the key/value separator to be used
686 when parsing the file. The None default means any whitespace [behavior
623 when parsing the file. The None default means any whitespace [behavior
687 of string.split()].
624 of string.split()].
688
625
689 strip(0): if 1, strip string values of leading/trailinig whitespace.
626 strip(0): if 1, strip string values of leading/trailinig whitespace.
690
627
691 warn(1): warning level if requested keys are not found in file.
628 warn(1): warning level if requested keys are not found in file.
692 - 0: silently ignore.
629 - 0: silently ignore.
693 - 1: inform but proceed.
630 - 1: inform but proceed.
694 - 2: raise KeyError exception.
631 - 2: raise KeyError exception.
695
632
696 no_empty(0): if 1, remove keys with whitespace strings as a value.
633 no_empty(0): if 1, remove keys with whitespace strings as a value.
697
634
698 unique([]): list of keys (or space separated string) which can't be
635 unique([]): list of keys (or space separated string) which can't be
699 repeated. If one such key is found in the file, each new instance
636 repeated. If one such key is found in the file, each new instance
700 overwrites the previous one. For keys not listed here, the behavior is
637 overwrites the previous one. For keys not listed here, the behavior is
701 to make a list of all appearances.
638 to make a list of all appearances.
702
639
703 Example:
640 Example:
704 If the input file test.ini has:
641 If the input file test.ini has:
705 i 3
642 i 3
706 x 4.5
643 x 4.5
707 y 5.5
644 y 5.5
708 s hi ho
645 s hi ho
709 Then:
646 Then:
710
647
711 >>> type_conv={int:'i',float:'x',None:'s'}
648 >>> type_conv={int:'i',float:'x',None:'s'}
712 >>> read_dict('test.ini')
649 >>> read_dict('test.ini')
713 {'i': '3', 's': 'hi ho', 'x': '4.5', 'y': '5.5'}
650 {'i': '3', 's': 'hi ho', 'x': '4.5', 'y': '5.5'}
714 >>> read_dict('test.ini',type_conv)
651 >>> read_dict('test.ini',type_conv)
715 {'i': 3, 's': 'hi ho', 'x': 4.5, 'y': '5.5'}
652 {'i': 3, 's': 'hi ho', 'x': 4.5, 'y': '5.5'}
716 >>> read_dict('test.ini',type_conv,purge=1)
653 >>> read_dict('test.ini',type_conv,purge=1)
717 {'i': 3, 's': 'hi ho', 'x': 4.5}
654 {'i': 3, 's': 'hi ho', 'x': 4.5}
718 """
655 """
719
656
720 # starting config
657 # starting config
721 opt.setdefault('purge',0)
658 opt.setdefault('purge',0)
722 opt.setdefault('fs',None) # field sep defaults to any whitespace
659 opt.setdefault('fs',None) # field sep defaults to any whitespace
723 opt.setdefault('strip',0)
660 opt.setdefault('strip',0)
724 opt.setdefault('warn',1)
661 opt.setdefault('warn',1)
725 opt.setdefault('no_empty',0)
662 opt.setdefault('no_empty',0)
726 opt.setdefault('unique','')
663 opt.setdefault('unique','')
727 if type(opt['unique']) in StringTypes:
664 if type(opt['unique']) in StringTypes:
728 unique_keys = qw(opt['unique'])
665 unique_keys = qw(opt['unique'])
729 elif type(opt['unique']) in (types.TupleType,types.ListType):
666 elif type(opt['unique']) in (types.TupleType,types.ListType):
730 unique_keys = opt['unique']
667 unique_keys = opt['unique']
731 else:
668 else:
732 raise ValueError, 'Unique keys must be given as a string, List or Tuple'
669 raise ValueError, 'Unique keys must be given as a string, List or Tuple'
733
670
734 dict = {}
671 dict = {}
735 # first read in table of values as strings
672 # first read in table of values as strings
736 file = open(filename,'r')
673 file = open(filename,'r')
737 for line in file.readlines():
674 for line in file.readlines():
738 line = line.strip()
675 line = line.strip()
739 if len(line) and line[0]=='#': continue
676 if len(line) and line[0]=='#': continue
740 if len(line)>0:
677 if len(line)>0:
741 lsplit = line.split(opt['fs'],1)
678 lsplit = line.split(opt['fs'],1)
742 try:
679 try:
743 key,val = lsplit
680 key,val = lsplit
744 except ValueError:
681 except ValueError:
745 key,val = lsplit[0],''
682 key,val = lsplit[0],''
746 key = key.strip()
683 key = key.strip()
747 if opt['strip']: val = val.strip()
684 if opt['strip']: val = val.strip()
748 if val == "''" or val == '""': val = ''
685 if val == "''" or val == '""': val = ''
749 if opt['no_empty'] and (val=='' or val.isspace()):
686 if opt['no_empty'] and (val=='' or val.isspace()):
750 continue
687 continue
751 # if a key is found more than once in the file, build a list
688 # if a key is found more than once in the file, build a list
752 # unless it's in the 'unique' list. In that case, last found in file
689 # unless it's in the 'unique' list. In that case, last found in file
753 # takes precedence. User beware.
690 # takes precedence. User beware.
754 try:
691 try:
755 if dict[key] and key in unique_keys:
692 if dict[key] and key in unique_keys:
756 dict[key] = val
693 dict[key] = val
757 elif type(dict[key]) is types.ListType:
694 elif type(dict[key]) is types.ListType:
758 dict[key].append(val)
695 dict[key].append(val)
759 else:
696 else:
760 dict[key] = [dict[key],val]
697 dict[key] = [dict[key],val]
761 except KeyError:
698 except KeyError:
762 dict[key] = val
699 dict[key] = val
763 # purge if requested
700 # purge if requested
764 if opt['purge']:
701 if opt['purge']:
765 accepted_keys = qwflat(type_conv.values())
702 accepted_keys = qwflat(type_conv.values())
766 for key in dict.keys():
703 for key in dict.keys():
767 if key in accepted_keys: continue
704 if key in accepted_keys: continue
768 del(dict[key])
705 del(dict[key])
769 # now convert if requested
706 # now convert if requested
770 if type_conv==None: return dict
707 if type_conv==None: return dict
771 conversions = type_conv.keys()
708 conversions = type_conv.keys()
772 try: conversions.remove(None)
709 try: conversions.remove(None)
773 except: pass
710 except: pass
774 for convert in conversions:
711 for convert in conversions:
775 for val in qw(type_conv[convert]):
712 for val in qw(type_conv[convert]):
776 try:
713 try:
777 dict[val] = convert(dict[val])
714 dict[val] = convert(dict[val])
778 except KeyError,e:
715 except KeyError,e:
779 if opt['warn'] == 0:
716 if opt['warn'] == 0:
780 pass
717 pass
781 elif opt['warn'] == 1:
718 elif opt['warn'] == 1:
782 print >>sys.stderr, 'Warning: key',val,\
719 print >>sys.stderr, 'Warning: key',val,\
783 'not found in file',filename
720 'not found in file',filename
784 elif opt['warn'] == 2:
721 elif opt['warn'] == 2:
785 raise KeyError,e
722 raise KeyError,e
786 else:
723 else:
787 raise ValueError,'Warning level must be 0,1 or 2'
724 raise ValueError,'Warning level must be 0,1 or 2'
788
725
789 return dict
726 return dict
790
727
791 #----------------------------------------------------------------------------
728 #----------------------------------------------------------------------------
792 def flag_calls(func):
729 def flag_calls(func):
793 """Wrap a function to detect and flag when it gets called.
730 """Wrap a function to detect and flag when it gets called.
794
731
795 This is a decorator which takes a function and wraps it in a function with
732 This is a decorator which takes a function and wraps it in a function with
796 a 'called' attribute. wrapper.called is initialized to False.
733 a 'called' attribute. wrapper.called is initialized to False.
797
734
798 The wrapper.called attribute is set to False right before each call to the
735 The wrapper.called attribute is set to False right before each call to the
799 wrapped function, so if the call fails it remains False. After the call
736 wrapped function, so if the call fails it remains False. After the call
800 completes, wrapper.called is set to True and the output is returned.
737 completes, wrapper.called is set to True and the output is returned.
801
738
802 Testing for truth in wrapper.called allows you to determine if a call to
739 Testing for truth in wrapper.called allows you to determine if a call to
803 func() was attempted and succeeded."""
740 func() was attempted and succeeded."""
804
741
805 def wrapper(*args,**kw):
742 def wrapper(*args,**kw):
806 wrapper.called = False
743 wrapper.called = False
807 out = func(*args,**kw)
744 out = func(*args,**kw)
808 wrapper.called = True
745 wrapper.called = True
809 return out
746 return out
810
747
811 wrapper.called = False
748 wrapper.called = False
812 wrapper.__doc__ = func.__doc__
749 wrapper.__doc__ = func.__doc__
813 return wrapper
750 return wrapper
814
751
815 #----------------------------------------------------------------------------
752 #----------------------------------------------------------------------------
816 class HomeDirError(Error):
753 class HomeDirError(Error):
817 pass
754 pass
818
755
819 def get_home_dir():
756 def get_home_dir():
820 """Return the closest possible equivalent to a 'home' directory.
757 """Return the closest possible equivalent to a 'home' directory.
821
758
822 We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH.
759 We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH.
823
760
824 Currently only Posix and NT are implemented, a HomeDirError exception is
761 Currently only Posix and NT are implemented, a HomeDirError exception is
825 raised for all other OSes. """
762 raised for all other OSes. """
826
763
827 isdir = os.path.isdir
764 isdir = os.path.isdir
828 env = os.environ
765 env = os.environ
829 try:
766 try:
830 homedir = env['HOME']
767 homedir = env['HOME']
831 if not isdir(homedir):
768 if not isdir(homedir):
832 # in case a user stuck some string which does NOT resolve to a
769 # in case a user stuck some string which does NOT resolve to a
833 # valid path, it's as good as if we hadn't foud it
770 # valid path, it's as good as if we hadn't foud it
834 raise KeyError
771 raise KeyError
835 return homedir
772 return homedir
836 except KeyError:
773 except KeyError:
837 if os.name == 'posix':
774 if os.name == 'posix':
838 raise HomeDirError,'undefined $HOME, IPython can not proceed.'
775 raise HomeDirError,'undefined $HOME, IPython can not proceed.'
839 elif os.name == 'nt':
776 elif os.name == 'nt':
840 # For some strange reason, win9x returns 'nt' for os.name.
777 # For some strange reason, win9x returns 'nt' for os.name.
841 try:
778 try:
842 homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
779 homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
843 if not isdir(homedir):
780 if not isdir(homedir):
844 homedir = os.path.join(env['USERPROFILE'])
781 homedir = os.path.join(env['USERPROFILE'])
845 if not isdir(homedir):
782 if not isdir(homedir):
846 raise HomeDirError
783 raise HomeDirError
847 return homedir
784 return homedir
848 except:
785 except:
849 try:
786 try:
850 # Use the registry to get the 'My Documents' folder.
787 # Use the registry to get the 'My Documents' folder.
851 import _winreg as wreg
788 import _winreg as wreg
852 key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
789 key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
853 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
790 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
854 homedir = wreg.QueryValueEx(key,'Personal')[0]
791 homedir = wreg.QueryValueEx(key,'Personal')[0]
855 key.Close()
792 key.Close()
856 if not isdir(homedir):
793 if not isdir(homedir):
857 e = ('Invalid "Personal" folder registry key '
794 e = ('Invalid "Personal" folder registry key '
858 'typically "My Documents".\n'
795 'typically "My Documents".\n'
859 'Value: %s\n'
796 'Value: %s\n'
860 'This is not a valid directory on your system.' %
797 'This is not a valid directory on your system.' %
861 homedir)
798 homedir)
862 raise HomeDirError(e)
799 raise HomeDirError(e)
863 return homedir
800 return homedir
864 except HomeDirError:
801 except HomeDirError:
865 raise
802 raise
866 except:
803 except:
867 return 'C:\\'
804 return 'C:\\'
868 elif os.name == 'dos':
805 elif os.name == 'dos':
869 # Desperate, may do absurd things in classic MacOS. May work under DOS.
806 # Desperate, may do absurd things in classic MacOS. May work under DOS.
870 return 'C:\\'
807 return 'C:\\'
871 else:
808 else:
872 raise HomeDirError,'support for your operating system not implemented.'
809 raise HomeDirError,'support for your operating system not implemented.'
873
810
874 #****************************************************************************
811 #****************************************************************************
875 # strings and text
812 # strings and text
876
813
877 class LSString(str):
814 class LSString(str):
878 """String derivative with a special access attributes.
815 """String derivative with a special access attributes.
879
816
880 These are normal strings, but with the special attributes:
817 These are normal strings, but with the special attributes:
881
818
882 .l (or .list) : value as list (split on newlines).
819 .l (or .list) : value as list (split on newlines).
883 .n (or .nlstr): original value (the string itself).
820 .n (or .nlstr): original value (the string itself).
884 .s (or .spstr): value as whitespace-separated string.
821 .s (or .spstr): value as whitespace-separated string.
885
822
886 Any values which require transformations are computed only once and
823 Any values which require transformations are computed only once and
887 cached.
824 cached.
888
825
889 Such strings are very useful to efficiently interact with the shell, which
826 Such strings are very useful to efficiently interact with the shell, which
890 typically only understands whitespace-separated options for commands."""
827 typically only understands whitespace-separated options for commands."""
891
828
892 def get_list(self):
829 def get_list(self):
893 try:
830 try:
894 return self.__list
831 return self.__list
895 except AttributeError:
832 except AttributeError:
896 self.__list = self.split('\n')
833 self.__list = self.split('\n')
897 return self.__list
834 return self.__list
898
835
899 l = list = property(get_list)
836 l = list = property(get_list)
900
837
901 def get_spstr(self):
838 def get_spstr(self):
902 try:
839 try:
903 return self.__spstr
840 return self.__spstr
904 except AttributeError:
841 except AttributeError:
905 self.__spstr = self.replace('\n',' ')
842 self.__spstr = self.replace('\n',' ')
906 return self.__spstr
843 return self.__spstr
907
844
908 s = spstr = property(get_spstr)
845 s = spstr = property(get_spstr)
909
846
910 def get_nlstr(self):
847 def get_nlstr(self):
911 return self
848 return self
912
849
913 n = nlstr = property(get_nlstr)
850 n = nlstr = property(get_nlstr)
914
851
915 def get_paths(self):
852 def get_paths(self):
916 try:
853 try:
917 return self.__paths
854 return self.__paths
918 except AttributeError:
855 except AttributeError:
919 self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)]
856 self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)]
920 return self.__paths
857 return self.__paths
921
858
922 p = paths = property(get_paths)
859 p = paths = property(get_paths)
923
860
924
861
925 #----------------------------------------------------------------------------
862 #----------------------------------------------------------------------------
926 class SList(list):
863 class SList(list):
927 """List derivative with a special access attributes.
864 """List derivative with a special access attributes.
928
865
929 These are normal lists, but with the special attributes:
866 These are normal lists, but with the special attributes:
930
867
931 .l (or .list) : value as list (the list itself).
868 .l (or .list) : value as list (the list itself).
932 .n (or .nlstr): value as a string, joined on newlines.
869 .n (or .nlstr): value as a string, joined on newlines.
933 .s (or .spstr): value as a string, joined on spaces.
870 .s (or .spstr): value as a string, joined on spaces.
934
871
935 Any values which require transformations are computed only once and
872 Any values which require transformations are computed only once and
936 cached."""
873 cached."""
937
874
938 def get_list(self):
875 def get_list(self):
939 return self
876 return self
940
877
941 l = list = property(get_list)
878 l = list = property(get_list)
942
879
943 def get_spstr(self):
880 def get_spstr(self):
944 try:
881 try:
945 return self.__spstr
882 return self.__spstr
946 except AttributeError:
883 except AttributeError:
947 self.__spstr = ' '.join(self)
884 self.__spstr = ' '.join(self)
948 return self.__spstr
885 return self.__spstr
949
886
950 s = spstr = property(get_spstr)
887 s = spstr = property(get_spstr)
951
888
952 def get_nlstr(self):
889 def get_nlstr(self):
953 try:
890 try:
954 return self.__nlstr
891 return self.__nlstr
955 except AttributeError:
892 except AttributeError:
956 self.__nlstr = '\n'.join(self)
893 self.__nlstr = '\n'.join(self)
957 return self.__nlstr
894 return self.__nlstr
958
895
959 n = nlstr = property(get_nlstr)
896 n = nlstr = property(get_nlstr)
960
897
961 def get_paths(self):
898 def get_paths(self):
962 try:
899 try:
963 return self.__paths
900 return self.__paths
964 except AttributeError:
901 except AttributeError:
965 self.__paths = [path(p) for p in self if os.path.exists(p)]
902 self.__paths = [path(p) for p in self if os.path.exists(p)]
966 return self.__paths
903 return self.__paths
967
904
968 p = paths = property(get_paths)
905 p = paths = property(get_paths)
969
906
970 #----------------------------------------------------------------------------
907 #----------------------------------------------------------------------------
971 def esc_quotes(strng):
908 def esc_quotes(strng):
972 """Return the input string with single and double quotes escaped out"""
909 """Return the input string with single and double quotes escaped out"""
973
910
974 return strng.replace('"','\\"').replace("'","\\'")
911 return strng.replace('"','\\"').replace("'","\\'")
975
912
976 #----------------------------------------------------------------------------
913 #----------------------------------------------------------------------------
977 def make_quoted_expr(s):
914 def make_quoted_expr(s):
978 """Return string s in appropriate quotes, using raw string if possible.
915 """Return string s in appropriate quotes, using raw string if possible.
979
916
980 Effectively this turns string: cd \ao\ao\
917 Effectively this turns string: cd \ao\ao\
981 to: r"cd \ao\ao\_"[:-1]
918 to: r"cd \ao\ao\_"[:-1]
982
919
983 Note the use of raw string and padding at the end to allow trailing backslash.
920 Note the use of raw string and padding at the end to allow trailing backslash.
984
921
985 """
922 """
986
923
987 tail = ''
924 tail = ''
988 tailpadding = ''
925 tailpadding = ''
989 raw = ''
926 raw = ''
990 if "\\" in s:
927 if "\\" in s:
991 raw = 'r'
928 raw = 'r'
992 if s.endswith('\\'):
929 if s.endswith('\\'):
993 tail = '[:-1]'
930 tail = '[:-1]'
994 tailpadding = '_'
931 tailpadding = '_'
995 if '"' not in s:
932 if '"' not in s:
996 quote = '"'
933 quote = '"'
997 elif "'" not in s:
934 elif "'" not in s:
998 quote = "'"
935 quote = "'"
999 elif '"""' not in s and not s.endswith('"'):
936 elif '"""' not in s and not s.endswith('"'):
1000 quote = '"""'
937 quote = '"""'
1001 elif "'''" not in s and not s.endswith("'"):
938 elif "'''" not in s and not s.endswith("'"):
1002 quote = "'''"
939 quote = "'''"
1003 else:
940 else:
1004 # give up, backslash-escaped string will do
941 # give up, backslash-escaped string will do
1005 return '"%s"' % esc_quotes(s)
942 return '"%s"' % esc_quotes(s)
1006 res = itpl("$raw$quote$s$tailpadding$quote$tail")
943 res = itpl("$raw$quote$s$tailpadding$quote$tail")
1007 return res
944 return res
1008
945
1009
946
1010 #----------------------------------------------------------------------------
947 #----------------------------------------------------------------------------
1011 def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'):
948 def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'):
1012 """Take multiple lines of input.
949 """Take multiple lines of input.
1013
950
1014 A list with each line of input as a separate element is returned when a
951 A list with each line of input as a separate element is returned when a
1015 termination string is entered (defaults to a single '.'). Input can also
952 termination string is entered (defaults to a single '.'). Input can also
1016 terminate via EOF (^D in Unix, ^Z-RET in Windows).
953 terminate via EOF (^D in Unix, ^Z-RET in Windows).
1017
954
1018 Lines of input which end in \\ are joined into single entries (and a
955 Lines of input which end in \\ are joined into single entries (and a
1019 secondary continuation prompt is issued as long as the user terminates
956 secondary continuation prompt is issued as long as the user terminates
1020 lines with \\). This allows entering very long strings which are still
957 lines with \\). This allows entering very long strings which are still
1021 meant to be treated as single entities.
958 meant to be treated as single entities.
1022 """
959 """
1023
960
1024 try:
961 try:
1025 if header:
962 if header:
1026 header += '\n'
963 header += '\n'
1027 lines = [raw_input(header + ps1)]
964 lines = [raw_input(header + ps1)]
1028 except EOFError:
965 except EOFError:
1029 return []
966 return []
1030 terminate = [terminate_str]
967 terminate = [terminate_str]
1031 try:
968 try:
1032 while lines[-1:] != terminate:
969 while lines[-1:] != terminate:
1033 new_line = raw_input(ps1)
970 new_line = raw_input(ps1)
1034 while new_line.endswith('\\'):
971 while new_line.endswith('\\'):
1035 new_line = new_line[:-1] + raw_input(ps2)
972 new_line = new_line[:-1] + raw_input(ps2)
1036 lines.append(new_line)
973 lines.append(new_line)
1037
974
1038 return lines[:-1] # don't return the termination command
975 return lines[:-1] # don't return the termination command
1039 except EOFError:
976 except EOFError:
1040 print
977 print
1041 return lines
978 return lines
1042
979
1043 #----------------------------------------------------------------------------
980 #----------------------------------------------------------------------------
1044 def raw_input_ext(prompt='', ps2='... '):
981 def raw_input_ext(prompt='', ps2='... '):
1045 """Similar to raw_input(), but accepts extended lines if input ends with \\."""
982 """Similar to raw_input(), but accepts extended lines if input ends with \\."""
1046
983
1047 line = raw_input(prompt)
984 line = raw_input(prompt)
1048 while line.endswith('\\'):
985 while line.endswith('\\'):
1049 line = line[:-1] + raw_input(ps2)
986 line = line[:-1] + raw_input(ps2)
1050 return line
987 return line
1051
988
1052 #----------------------------------------------------------------------------
989 #----------------------------------------------------------------------------
1053 def ask_yes_no(prompt,default=None):
990 def ask_yes_no(prompt,default=None):
1054 """Asks a question and returns an integer 1/0 (y/n) answer.
991 """Asks a question and returns an integer 1/0 (y/n) answer.
1055
992
1056 If default is given (one of 'y','n'), it is used if the user input is
993 If default is given (one of 'y','n'), it is used if the user input is
1057 empty. Otherwise the question is repeated until an answer is given.
994 empty. Otherwise the question is repeated until an answer is given.
1058 If EOF occurs 20 times consecutively, the default answer is assumed,
995 If EOF occurs 20 times consecutively, the default answer is assumed,
1059 or if there is no default, an exception is raised to prevent infinite
996 or if there is no default, an exception is raised to prevent infinite
1060 loops.
997 loops.
1061
998
1062 Valid answers are: y/yes/n/no (match is not case sensitive)."""
999 Valid answers are: y/yes/n/no (match is not case sensitive)."""
1063
1000
1064 answers = {'y':True,'n':False,'yes':True,'no':False}
1001 answers = {'y':True,'n':False,'yes':True,'no':False}
1065 ans = None
1002 ans = None
1066 eofs, max_eofs = 0, 20
1003 eofs, max_eofs = 0, 20
1067 while ans not in answers.keys():
1004 while ans not in answers.keys():
1068 try:
1005 try:
1069 ans = raw_input(prompt+' ').lower()
1006 ans = raw_input(prompt+' ').lower()
1070 if not ans: # response was an empty string
1007 if not ans: # response was an empty string
1071 ans = default
1008 ans = default
1072 eofs = 0
1009 eofs = 0
1073 except (EOFError,KeyboardInterrupt):
1010 except (EOFError,KeyboardInterrupt):
1074 eofs = eofs + 1
1011 eofs = eofs + 1
1075 if eofs >= max_eofs:
1012 if eofs >= max_eofs:
1076 if default in answers.keys():
1013 if default in answers.keys():
1077 ans = default
1014 ans = default
1078 else:
1015 else:
1079 raise
1016 raise
1080
1017
1081 return answers[ans]
1018 return answers[ans]
1082
1019
1083 #----------------------------------------------------------------------------
1020 #----------------------------------------------------------------------------
1084 def marquee(txt='',width=78,mark='*'):
1021 def marquee(txt='',width=78,mark='*'):
1085 """Return the input string centered in a 'marquee'."""
1022 """Return the input string centered in a 'marquee'."""
1086 if not txt:
1023 if not txt:
1087 return (mark*width)[:width]
1024 return (mark*width)[:width]
1088 nmark = (width-len(txt)-2)/len(mark)/2
1025 nmark = (width-len(txt)-2)/len(mark)/2
1089 if nmark < 0: nmark =0
1026 if nmark < 0: nmark =0
1090 marks = mark*nmark
1027 marks = mark*nmark
1091 return '%s %s %s' % (marks,txt,marks)
1028 return '%s %s %s' % (marks,txt,marks)
1092
1029
1093 #----------------------------------------------------------------------------
1030 #----------------------------------------------------------------------------
1094 class EvalDict:
1031 class EvalDict:
1095 """
1032 """
1096 Emulate a dict which evaluates its contents in the caller's frame.
1033 Emulate a dict which evaluates its contents in the caller's frame.
1097
1034
1098 Usage:
1035 Usage:
1099 >>>number = 19
1036 >>>number = 19
1100 >>>text = "python"
1037 >>>text = "python"
1101 >>>print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict()
1038 >>>print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict()
1102 """
1039 """
1103
1040
1104 # This version is due to sismex01@hebmex.com on c.l.py, and is basically a
1041 # This version is due to sismex01@hebmex.com on c.l.py, and is basically a
1105 # modified (shorter) version of:
1042 # modified (shorter) version of:
1106 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by
1043 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by
1107 # Skip Montanaro (skip@pobox.com).
1044 # Skip Montanaro (skip@pobox.com).
1108
1045
1109 def __getitem__(self, name):
1046 def __getitem__(self, name):
1110 frame = sys._getframe(1)
1047 frame = sys._getframe(1)
1111 return eval(name, frame.f_globals, frame.f_locals)
1048 return eval(name, frame.f_globals, frame.f_locals)
1112
1049
1113 EvalString = EvalDict # for backwards compatibility
1050 EvalString = EvalDict # for backwards compatibility
1114 #----------------------------------------------------------------------------
1051 #----------------------------------------------------------------------------
1115 def qw(words,flat=0,sep=None,maxsplit=-1):
1052 def qw(words,flat=0,sep=None,maxsplit=-1):
1116 """Similar to Perl's qw() operator, but with some more options.
1053 """Similar to Perl's qw() operator, but with some more options.
1117
1054
1118 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
1055 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
1119
1056
1120 words can also be a list itself, and with flat=1, the output will be
1057 words can also be a list itself, and with flat=1, the output will be
1121 recursively flattened. Examples:
1058 recursively flattened. Examples:
1122
1059
1123 >>> qw('1 2')
1060 >>> qw('1 2')
1124 ['1', '2']
1061 ['1', '2']
1125 >>> qw(['a b','1 2',['m n','p q']])
1062 >>> qw(['a b','1 2',['m n','p q']])
1126 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
1063 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
1127 >>> qw(['a b','1 2',['m n','p q']],flat=1)
1064 >>> qw(['a b','1 2',['m n','p q']],flat=1)
1128 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] """
1065 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] """
1129
1066
1130 if type(words) in StringTypes:
1067 if type(words) in StringTypes:
1131 return [word.strip() for word in words.split(sep,maxsplit)
1068 return [word.strip() for word in words.split(sep,maxsplit)
1132 if word and not word.isspace() ]
1069 if word and not word.isspace() ]
1133 if flat:
1070 if flat:
1134 return flatten(map(qw,words,[1]*len(words)))
1071 return flatten(map(qw,words,[1]*len(words)))
1135 return map(qw,words)
1072 return map(qw,words)
1136
1073
1137 #----------------------------------------------------------------------------
1074 #----------------------------------------------------------------------------
1138 def qwflat(words,sep=None,maxsplit=-1):
1075 def qwflat(words,sep=None,maxsplit=-1):
1139 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
1076 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
1140 return qw(words,1,sep,maxsplit)
1077 return qw(words,1,sep,maxsplit)
1141
1078
1142 #----------------------------------------------------------------------------
1079 #----------------------------------------------------------------------------
1143 def qw_lol(indata):
1080 def qw_lol(indata):
1144 """qw_lol('a b') -> [['a','b']],
1081 """qw_lol('a b') -> [['a','b']],
1145 otherwise it's just a call to qw().
1082 otherwise it's just a call to qw().
1146
1083
1147 We need this to make sure the modules_some keys *always* end up as a
1084 We need this to make sure the modules_some keys *always* end up as a
1148 list of lists."""
1085 list of lists."""
1149
1086
1150 if type(indata) in StringTypes:
1087 if type(indata) in StringTypes:
1151 return [qw(indata)]
1088 return [qw(indata)]
1152 else:
1089 else:
1153 return qw(indata)
1090 return qw(indata)
1154
1091
1155 #-----------------------------------------------------------------------------
1092 #-----------------------------------------------------------------------------
1156 def list_strings(arg):
1093 def list_strings(arg):
1157 """Always return a list of strings, given a string or list of strings
1094 """Always return a list of strings, given a string or list of strings
1158 as input."""
1095 as input."""
1159
1096
1160 if type(arg) in StringTypes: return [arg]
1097 if type(arg) in StringTypes: return [arg]
1161 else: return arg
1098 else: return arg
1162
1099
1163 #----------------------------------------------------------------------------
1100 #----------------------------------------------------------------------------
1164 def grep(pat,list,case=1):
1101 def grep(pat,list,case=1):
1165 """Simple minded grep-like function.
1102 """Simple minded grep-like function.
1166 grep(pat,list) returns occurrences of pat in list, None on failure.
1103 grep(pat,list) returns occurrences of pat in list, None on failure.
1167
1104
1168 It only does simple string matching, with no support for regexps. Use the
1105 It only does simple string matching, with no support for regexps. Use the
1169 option case=0 for case-insensitive matching."""
1106 option case=0 for case-insensitive matching."""
1170
1107
1171 # This is pretty crude. At least it should implement copying only references
1108 # This is pretty crude. At least it should implement copying only references
1172 # to the original data in case it's big. Now it copies the data for output.
1109 # to the original data in case it's big. Now it copies the data for output.
1173 out=[]
1110 out=[]
1174 if case:
1111 if case:
1175 for term in list:
1112 for term in list:
1176 if term.find(pat)>-1: out.append(term)
1113 if term.find(pat)>-1: out.append(term)
1177 else:
1114 else:
1178 lpat=pat.lower()
1115 lpat=pat.lower()
1179 for term in list:
1116 for term in list:
1180 if term.lower().find(lpat)>-1: out.append(term)
1117 if term.lower().find(lpat)>-1: out.append(term)
1181
1118
1182 if len(out): return out
1119 if len(out): return out
1183 else: return None
1120 else: return None
1184
1121
1185 #----------------------------------------------------------------------------
1122 #----------------------------------------------------------------------------
1186 def dgrep(pat,*opts):
1123 def dgrep(pat,*opts):
1187 """Return grep() on dir()+dir(__builtins__).
1124 """Return grep() on dir()+dir(__builtins__).
1188
1125
1189 A very common use of grep() when working interactively."""
1126 A very common use of grep() when working interactively."""
1190
1127
1191 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
1128 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
1192
1129
1193 #----------------------------------------------------------------------------
1130 #----------------------------------------------------------------------------
1194 def idgrep(pat):
1131 def idgrep(pat):
1195 """Case-insensitive dgrep()"""
1132 """Case-insensitive dgrep()"""
1196
1133
1197 return dgrep(pat,0)
1134 return dgrep(pat,0)
1198
1135
1199 #----------------------------------------------------------------------------
1136 #----------------------------------------------------------------------------
1200 def igrep(pat,list):
1137 def igrep(pat,list):
1201 """Synonym for case-insensitive grep."""
1138 """Synonym for case-insensitive grep."""
1202
1139
1203 return grep(pat,list,case=0)
1140 return grep(pat,list,case=0)
1204
1141
1205 #----------------------------------------------------------------------------
1142 #----------------------------------------------------------------------------
1206 def indent(str,nspaces=4,ntabs=0):
1143 def indent(str,nspaces=4,ntabs=0):
1207 """Indent a string a given number of spaces or tabstops.
1144 """Indent a string a given number of spaces or tabstops.
1208
1145
1209 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
1146 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
1210 """
1147 """
1211 if str is None:
1148 if str is None:
1212 return
1149 return
1213 ind = '\t'*ntabs+' '*nspaces
1150 ind = '\t'*ntabs+' '*nspaces
1214 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
1151 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
1215 if outstr.endswith(os.linesep+ind):
1152 if outstr.endswith(os.linesep+ind):
1216 return outstr[:-len(ind)]
1153 return outstr[:-len(ind)]
1217 else:
1154 else:
1218 return outstr
1155 return outstr
1219
1156
1220 #-----------------------------------------------------------------------------
1157 #-----------------------------------------------------------------------------
1221 def native_line_ends(filename,backup=1):
1158 def native_line_ends(filename,backup=1):
1222 """Convert (in-place) a file to line-ends native to the current OS.
1159 """Convert (in-place) a file to line-ends native to the current OS.
1223
1160
1224 If the optional backup argument is given as false, no backup of the
1161 If the optional backup argument is given as false, no backup of the
1225 original file is left. """
1162 original file is left. """
1226
1163
1227 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
1164 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
1228
1165
1229 bak_filename = filename + backup_suffixes[os.name]
1166 bak_filename = filename + backup_suffixes[os.name]
1230
1167
1231 original = open(filename).read()
1168 original = open(filename).read()
1232 shutil.copy2(filename,bak_filename)
1169 shutil.copy2(filename,bak_filename)
1233 try:
1170 try:
1234 new = open(filename,'wb')
1171 new = open(filename,'wb')
1235 new.write(os.linesep.join(original.splitlines()))
1172 new.write(os.linesep.join(original.splitlines()))
1236 new.write(os.linesep) # ALWAYS put an eol at the end of the file
1173 new.write(os.linesep) # ALWAYS put an eol at the end of the file
1237 new.close()
1174 new.close()
1238 except:
1175 except:
1239 os.rename(bak_filename,filename)
1176 os.rename(bak_filename,filename)
1240 if not backup:
1177 if not backup:
1241 try:
1178 try:
1242 os.remove(bak_filename)
1179 os.remove(bak_filename)
1243 except:
1180 except:
1244 pass
1181 pass
1245
1182
1246 #----------------------------------------------------------------------------
1183 #----------------------------------------------------------------------------
1247 def get_pager_cmd(pager_cmd = None):
1184 def get_pager_cmd(pager_cmd = None):
1248 """Return a pager command.
1185 """Return a pager command.
1249
1186
1250 Makes some attempts at finding an OS-correct one."""
1187 Makes some attempts at finding an OS-correct one."""
1251
1188
1252 if os.name == 'posix':
1189 if os.name == 'posix':
1253 default_pager_cmd = 'less -r' # -r for color control sequences
1190 default_pager_cmd = 'less -r' # -r for color control sequences
1254 elif os.name in ['nt','dos']:
1191 elif os.name in ['nt','dos']:
1255 default_pager_cmd = 'type'
1192 default_pager_cmd = 'type'
1256
1193
1257 if pager_cmd is None:
1194 if pager_cmd is None:
1258 try:
1195 try:
1259 pager_cmd = os.environ['PAGER']
1196 pager_cmd = os.environ['PAGER']
1260 except:
1197 except:
1261 pager_cmd = default_pager_cmd
1198 pager_cmd = default_pager_cmd
1262 return pager_cmd
1199 return pager_cmd
1263
1200
1264 #-----------------------------------------------------------------------------
1201 #-----------------------------------------------------------------------------
1265 def get_pager_start(pager,start):
1202 def get_pager_start(pager,start):
1266 """Return the string for paging files with an offset.
1203 """Return the string for paging files with an offset.
1267
1204
1268 This is the '+N' argument which less and more (under Unix) accept.
1205 This is the '+N' argument which less and more (under Unix) accept.
1269 """
1206 """
1270
1207
1271 if pager in ['less','more']:
1208 if pager in ['less','more']:
1272 if start:
1209 if start:
1273 start_string = '+' + str(start)
1210 start_string = '+' + str(start)
1274 else:
1211 else:
1275 start_string = ''
1212 start_string = ''
1276 else:
1213 else:
1277 start_string = ''
1214 start_string = ''
1278 return start_string
1215 return start_string
1279
1216
1280 #----------------------------------------------------------------------------
1217 #----------------------------------------------------------------------------
1281 if os.name == "nt":
1218 if os.name == "nt":
1282 import msvcrt
1219 import msvcrt
1283 def page_more():
1220 def page_more():
1284 """ Smart pausing between pages
1221 """ Smart pausing between pages
1285
1222
1286 @return: True if need print more lines, False if quit
1223 @return: True if need print more lines, False if quit
1287 """
1224 """
1288 Term.cout.write('---Return to continue, q to quit--- ')
1225 Term.cout.write('---Return to continue, q to quit--- ')
1289 ans = msvcrt.getch()
1226 ans = msvcrt.getch()
1290 if ans in ("q", "Q"):
1227 if ans in ("q", "Q"):
1291 result = False
1228 result = False
1292 else:
1229 else:
1293 result = True
1230 result = True
1294 Term.cout.write("\b"*37 + " "*37 + "\b"*37)
1231 Term.cout.write("\b"*37 + " "*37 + "\b"*37)
1295 return result
1232 return result
1296 else:
1233 else:
1297 def page_more():
1234 def page_more():
1298 ans = raw_input('---Return to continue, q to quit--- ')
1235 ans = raw_input('---Return to continue, q to quit--- ')
1299 if ans.lower().startswith('q'):
1236 if ans.lower().startswith('q'):
1300 return False
1237 return False
1301 else:
1238 else:
1302 return True
1239 return True
1303
1240
1304 esc_re = re.compile(r"(\x1b[^m]+m)")
1241 esc_re = re.compile(r"(\x1b[^m]+m)")
1305
1242
1306 def page_dumb(strng,start=0,screen_lines=25):
1243 def page_dumb(strng,start=0,screen_lines=25):
1307 """Very dumb 'pager' in Python, for when nothing else works.
1244 """Very dumb 'pager' in Python, for when nothing else works.
1308
1245
1309 Only moves forward, same interface as page(), except for pager_cmd and
1246 Only moves forward, same interface as page(), except for pager_cmd and
1310 mode."""
1247 mode."""
1311
1248
1312 out_ln = strng.splitlines()[start:]
1249 out_ln = strng.splitlines()[start:]
1313 screens = chop(out_ln,screen_lines-1)
1250 screens = chop(out_ln,screen_lines-1)
1314 if len(screens) == 1:
1251 if len(screens) == 1:
1315 print >>Term.cout, os.linesep.join(screens[0])
1252 print >>Term.cout, os.linesep.join(screens[0])
1316 else:
1253 else:
1317 last_escape = ""
1254 last_escape = ""
1318 for scr in screens[0:-1]:
1255 for scr in screens[0:-1]:
1319 hunk = os.linesep.join(scr)
1256 hunk = os.linesep.join(scr)
1320 print >>Term.cout, last_escape + hunk
1257 print >>Term.cout, last_escape + hunk
1321 if not page_more():
1258 if not page_more():
1322 return
1259 return
1323 esc_list = esc_re.findall(hunk)
1260 esc_list = esc_re.findall(hunk)
1324 if len(esc_list) > 0:
1261 if len(esc_list) > 0:
1325 last_escape = esc_list[-1]
1262 last_escape = esc_list[-1]
1326 print >>Term.cout, last_escape + os.linesep.join(screens[-1])
1263 print >>Term.cout, last_escape + os.linesep.join(screens[-1])
1327
1264
1328 #----------------------------------------------------------------------------
1265 #----------------------------------------------------------------------------
1329 def page(strng,start=0,screen_lines=0,pager_cmd = None):
1266 def page(strng,start=0,screen_lines=0,pager_cmd = None):
1330 """Print a string, piping through a pager after a certain length.
1267 """Print a string, piping through a pager after a certain length.
1331
1268
1332 The screen_lines parameter specifies the number of *usable* lines of your
1269 The screen_lines parameter specifies the number of *usable* lines of your
1333 terminal screen (total lines minus lines you need to reserve to show other
1270 terminal screen (total lines minus lines you need to reserve to show other
1334 information).
1271 information).
1335
1272
1336 If you set screen_lines to a number <=0, page() will try to auto-determine
1273 If you set screen_lines to a number <=0, page() will try to auto-determine
1337 your screen size and will only use up to (screen_size+screen_lines) for
1274 your screen size and will only use up to (screen_size+screen_lines) for
1338 printing, paging after that. That is, if you want auto-detection but need
1275 printing, paging after that. That is, if you want auto-detection but need
1339 to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
1276 to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
1340 auto-detection without any lines reserved simply use screen_lines = 0.
1277 auto-detection without any lines reserved simply use screen_lines = 0.
1341
1278
1342 If a string won't fit in the allowed lines, it is sent through the
1279 If a string won't fit in the allowed lines, it is sent through the
1343 specified pager command. If none given, look for PAGER in the environment,
1280 specified pager command. If none given, look for PAGER in the environment,
1344 and ultimately default to less.
1281 and ultimately default to less.
1345
1282
1346 If no system pager works, the string is sent through a 'dumb pager'
1283 If no system pager works, the string is sent through a 'dumb pager'
1347 written in python, very simplistic.
1284 written in python, very simplistic.
1348 """
1285 """
1349
1286
1350 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
1287 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
1351 TERM = os.environ.get('TERM','dumb')
1288 TERM = os.environ.get('TERM','dumb')
1352 if TERM in ['dumb','emacs'] and os.name != 'nt':
1289 if TERM in ['dumb','emacs'] and os.name != 'nt':
1353 print strng
1290 print strng
1354 return
1291 return
1355 # chop off the topmost part of the string we don't want to see
1292 # chop off the topmost part of the string we don't want to see
1356 str_lines = strng.split(os.linesep)[start:]
1293 str_lines = strng.split(os.linesep)[start:]
1357 str_toprint = os.linesep.join(str_lines)
1294 str_toprint = os.linesep.join(str_lines)
1358 num_newlines = len(str_lines)
1295 num_newlines = len(str_lines)
1359 len_str = len(str_toprint)
1296 len_str = len(str_toprint)
1360
1297
1361 # Dumb heuristics to guesstimate number of on-screen lines the string
1298 # Dumb heuristics to guesstimate number of on-screen lines the string
1362 # takes. Very basic, but good enough for docstrings in reasonable
1299 # takes. Very basic, but good enough for docstrings in reasonable
1363 # terminals. If someone later feels like refining it, it's not hard.
1300 # terminals. If someone later feels like refining it, it's not hard.
1364 numlines = max(num_newlines,int(len_str/80)+1)
1301 numlines = max(num_newlines,int(len_str/80)+1)
1365
1302
1366 if os.name == "nt":
1303 if os.name == "nt":
1367 screen_lines_def = get_console_size(defaulty=25)[1]
1304 screen_lines_def = get_console_size(defaulty=25)[1]
1368 else:
1305 else:
1369 screen_lines_def = 25 # default value if we can't auto-determine
1306 screen_lines_def = 25 # default value if we can't auto-determine
1370
1307
1371 # auto-determine screen size
1308 # auto-determine screen size
1372 if screen_lines <= 0:
1309 if screen_lines <= 0:
1373 if TERM=='xterm':
1310 if TERM=='xterm':
1374 try:
1311 try:
1375 import curses
1312 import curses
1376 if hasattr(curses,'initscr'):
1313 if hasattr(curses,'initscr'):
1377 use_curses = 1
1314 use_curses = 1
1378 else:
1315 else:
1379 use_curses = 0
1316 use_curses = 0
1380 except ImportError:
1317 except ImportError:
1381 use_curses = 0
1318 use_curses = 0
1382 else:
1319 else:
1383 # curses causes problems on many terminals other than xterm.
1320 # curses causes problems on many terminals other than xterm.
1384 use_curses = 0
1321 use_curses = 0
1385 if use_curses:
1322 if use_curses:
1386 scr = curses.initscr()
1323 scr = curses.initscr()
1387 screen_lines_real,screen_cols = scr.getmaxyx()
1324 screen_lines_real,screen_cols = scr.getmaxyx()
1388 curses.endwin()
1325 curses.endwin()
1389 screen_lines += screen_lines_real
1326 screen_lines += screen_lines_real
1390 #print '***Screen size:',screen_lines_real,'lines x',\
1327 #print '***Screen size:',screen_lines_real,'lines x',\
1391 #screen_cols,'columns.' # dbg
1328 #screen_cols,'columns.' # dbg
1392 else:
1329 else:
1393 screen_lines += screen_lines_def
1330 screen_lines += screen_lines_def
1394
1331
1395 #print 'numlines',numlines,'screenlines',screen_lines # dbg
1332 #print 'numlines',numlines,'screenlines',screen_lines # dbg
1396 if numlines <= screen_lines :
1333 if numlines <= screen_lines :
1397 #print '*** normal print' # dbg
1334 #print '*** normal print' # dbg
1398 print >>Term.cout, str_toprint
1335 print >>Term.cout, str_toprint
1399 else:
1336 else:
1400 # Try to open pager and default to internal one if that fails.
1337 # Try to open pager and default to internal one if that fails.
1401 # All failure modes are tagged as 'retval=1', to match the return
1338 # All failure modes are tagged as 'retval=1', to match the return
1402 # value of a failed system command. If any intermediate attempt
1339 # value of a failed system command. If any intermediate attempt
1403 # sets retval to 1, at the end we resort to our own page_dumb() pager.
1340 # sets retval to 1, at the end we resort to our own page_dumb() pager.
1404 pager_cmd = get_pager_cmd(pager_cmd)
1341 pager_cmd = get_pager_cmd(pager_cmd)
1405 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1342 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1406 if os.name == 'nt':
1343 if os.name == 'nt':
1407 if pager_cmd.startswith('type'):
1344 if pager_cmd.startswith('type'):
1408 # The default WinXP 'type' command is failing on complex strings.
1345 # The default WinXP 'type' command is failing on complex strings.
1409 retval = 1
1346 retval = 1
1410 else:
1347 else:
1411 tmpname = tempfile.mktemp('.txt')
1348 tmpname = tempfile.mktemp('.txt')
1412 tmpfile = file(tmpname,'wt')
1349 tmpfile = file(tmpname,'wt')
1413 tmpfile.write(strng)
1350 tmpfile.write(strng)
1414 tmpfile.close()
1351 tmpfile.close()
1415 cmd = "%s < %s" % (pager_cmd,tmpname)
1352 cmd = "%s < %s" % (pager_cmd,tmpname)
1416 if os.system(cmd):
1353 if os.system(cmd):
1417 retval = 1
1354 retval = 1
1418 else:
1355 else:
1419 retval = None
1356 retval = None
1420 os.remove(tmpname)
1357 os.remove(tmpname)
1421 else:
1358 else:
1422 try:
1359 try:
1423 retval = None
1360 retval = None
1424 # if I use popen4, things hang. No idea why.
1361 # if I use popen4, things hang. No idea why.
1425 #pager,shell_out = os.popen4(pager_cmd)
1362 #pager,shell_out = os.popen4(pager_cmd)
1426 pager = os.popen(pager_cmd,'w')
1363 pager = os.popen(pager_cmd,'w')
1427 pager.write(strng)
1364 pager.write(strng)
1428 pager.close()
1365 pager.close()
1429 retval = pager.close() # success returns None
1366 retval = pager.close() # success returns None
1430 except IOError,msg: # broken pipe when user quits
1367 except IOError,msg: # broken pipe when user quits
1431 if msg.args == (32,'Broken pipe'):
1368 if msg.args == (32,'Broken pipe'):
1432 retval = None
1369 retval = None
1433 else:
1370 else:
1434 retval = 1
1371 retval = 1
1435 except OSError:
1372 except OSError:
1436 # Other strange problems, sometimes seen in Win2k/cygwin
1373 # Other strange problems, sometimes seen in Win2k/cygwin
1437 retval = 1
1374 retval = 1
1438 if retval is not None:
1375 if retval is not None:
1439 page_dumb(strng,screen_lines=screen_lines)
1376 page_dumb(strng,screen_lines=screen_lines)
1440
1377
1441 #----------------------------------------------------------------------------
1378 #----------------------------------------------------------------------------
1442 def page_file(fname,start = 0, pager_cmd = None):
1379 def page_file(fname,start = 0, pager_cmd = None):
1443 """Page a file, using an optional pager command and starting line.
1380 """Page a file, using an optional pager command and starting line.
1444 """
1381 """
1445
1382
1446 pager_cmd = get_pager_cmd(pager_cmd)
1383 pager_cmd = get_pager_cmd(pager_cmd)
1447 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1384 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1448
1385
1449 try:
1386 try:
1450 if os.environ['TERM'] in ['emacs','dumb']:
1387 if os.environ['TERM'] in ['emacs','dumb']:
1451 raise EnvironmentError
1388 raise EnvironmentError
1452 xsys(pager_cmd + ' ' + fname)
1389 xsys(pager_cmd + ' ' + fname)
1453 except:
1390 except:
1454 try:
1391 try:
1455 if start > 0:
1392 if start > 0:
1456 start -= 1
1393 start -= 1
1457 page(open(fname).read(),start)
1394 page(open(fname).read(),start)
1458 except:
1395 except:
1459 print 'Unable to show file',`fname`
1396 print 'Unable to show file',`fname`
1460
1397
1461 #----------------------------------------------------------------------------
1398 #----------------------------------------------------------------------------
1462 def snip_print(str,width = 75,print_full = 0,header = ''):
1399 def snip_print(str,width = 75,print_full = 0,header = ''):
1463 """Print a string snipping the midsection to fit in width.
1400 """Print a string snipping the midsection to fit in width.
1464
1401
1465 print_full: mode control:
1402 print_full: mode control:
1466 - 0: only snip long strings
1403 - 0: only snip long strings
1467 - 1: send to page() directly.
1404 - 1: send to page() directly.
1468 - 2: snip long strings and ask for full length viewing with page()
1405 - 2: snip long strings and ask for full length viewing with page()
1469 Return 1 if snipping was necessary, 0 otherwise."""
1406 Return 1 if snipping was necessary, 0 otherwise."""
1470
1407
1471 if print_full == 1:
1408 if print_full == 1:
1472 page(header+str)
1409 page(header+str)
1473 return 0
1410 return 0
1474
1411
1475 print header,
1412 print header,
1476 if len(str) < width:
1413 if len(str) < width:
1477 print str
1414 print str
1478 snip = 0
1415 snip = 0
1479 else:
1416 else:
1480 whalf = int((width -5)/2)
1417 whalf = int((width -5)/2)
1481 print str[:whalf] + ' <...> ' + str[-whalf:]
1418 print str[:whalf] + ' <...> ' + str[-whalf:]
1482 snip = 1
1419 snip = 1
1483 if snip and print_full == 2:
1420 if snip and print_full == 2:
1484 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
1421 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
1485 page(str)
1422 page(str)
1486 return snip
1423 return snip
1487
1424
1488 #****************************************************************************
1425 #****************************************************************************
1489 # lists, dicts and structures
1426 # lists, dicts and structures
1490
1427
1491 def belong(candidates,checklist):
1428 def belong(candidates,checklist):
1492 """Check whether a list of items appear in a given list of options.
1429 """Check whether a list of items appear in a given list of options.
1493
1430
1494 Returns a list of 1 and 0, one for each candidate given."""
1431 Returns a list of 1 and 0, one for each candidate given."""
1495
1432
1496 return [x in checklist for x in candidates]
1433 return [x in checklist for x in candidates]
1497
1434
1498 #----------------------------------------------------------------------------
1435 #----------------------------------------------------------------------------
1499 def uniq_stable(elems):
1436 def uniq_stable(elems):
1500 """uniq_stable(elems) -> list
1437 """uniq_stable(elems) -> list
1501
1438
1502 Return from an iterable, a list of all the unique elements in the input,
1439 Return from an iterable, a list of all the unique elements in the input,
1503 but maintaining the order in which they first appear.
1440 but maintaining the order in which they first appear.
1504
1441
1505 A naive solution to this problem which just makes a dictionary with the
1442 A naive solution to this problem which just makes a dictionary with the
1506 elements as keys fails to respect the stability condition, since
1443 elements as keys fails to respect the stability condition, since
1507 dictionaries are unsorted by nature.
1444 dictionaries are unsorted by nature.
1508
1445
1509 Note: All elements in the input must be valid dictionary keys for this
1446 Note: All elements in the input must be valid dictionary keys for this
1510 routine to work, as it internally uses a dictionary for efficiency
1447 routine to work, as it internally uses a dictionary for efficiency
1511 reasons."""
1448 reasons."""
1512
1449
1513 unique = []
1450 unique = []
1514 unique_dict = {}
1451 unique_dict = {}
1515 for nn in elems:
1452 for nn in elems:
1516 if nn not in unique_dict:
1453 if nn not in unique_dict:
1517 unique.append(nn)
1454 unique.append(nn)
1518 unique_dict[nn] = None
1455 unique_dict[nn] = None
1519 return unique
1456 return unique
1520
1457
1521 #----------------------------------------------------------------------------
1458 #----------------------------------------------------------------------------
1522 class NLprinter:
1459 class NLprinter:
1523 """Print an arbitrarily nested list, indicating index numbers.
1460 """Print an arbitrarily nested list, indicating index numbers.
1524
1461
1525 An instance of this class called nlprint is available and callable as a
1462 An instance of this class called nlprint is available and callable as a
1526 function.
1463 function.
1527
1464
1528 nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent'
1465 nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent'
1529 and using 'sep' to separate the index from the value. """
1466 and using 'sep' to separate the index from the value. """
1530
1467
1531 def __init__(self):
1468 def __init__(self):
1532 self.depth = 0
1469 self.depth = 0
1533
1470
1534 def __call__(self,lst,pos='',**kw):
1471 def __call__(self,lst,pos='',**kw):
1535 """Prints the nested list numbering levels."""
1472 """Prints the nested list numbering levels."""
1536 kw.setdefault('indent',' ')
1473 kw.setdefault('indent',' ')
1537 kw.setdefault('sep',': ')
1474 kw.setdefault('sep',': ')
1538 kw.setdefault('start',0)
1475 kw.setdefault('start',0)
1539 kw.setdefault('stop',len(lst))
1476 kw.setdefault('stop',len(lst))
1540 # we need to remove start and stop from kw so they don't propagate
1477 # we need to remove start and stop from kw so they don't propagate
1541 # into a recursive call for a nested list.
1478 # into a recursive call for a nested list.
1542 start = kw['start']; del kw['start']
1479 start = kw['start']; del kw['start']
1543 stop = kw['stop']; del kw['stop']
1480 stop = kw['stop']; del kw['stop']
1544 if self.depth == 0 and 'header' in kw.keys():
1481 if self.depth == 0 and 'header' in kw.keys():
1545 print kw['header']
1482 print kw['header']
1546
1483
1547 for idx in range(start,stop):
1484 for idx in range(start,stop):
1548 elem = lst[idx]
1485 elem = lst[idx]
1549 if type(elem)==type([]):
1486 if type(elem)==type([]):
1550 self.depth += 1
1487 self.depth += 1
1551 self.__call__(elem,itpl('$pos$idx,'),**kw)
1488 self.__call__(elem,itpl('$pos$idx,'),**kw)
1552 self.depth -= 1
1489 self.depth -= 1
1553 else:
1490 else:
1554 printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem')
1491 printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem')
1555
1492
1556 nlprint = NLprinter()
1493 nlprint = NLprinter()
1557 #----------------------------------------------------------------------------
1494 #----------------------------------------------------------------------------
1558 def all_belong(candidates,checklist):
1495 def all_belong(candidates,checklist):
1559 """Check whether a list of items ALL appear in a given list of options.
1496 """Check whether a list of items ALL appear in a given list of options.
1560
1497
1561 Returns a single 1 or 0 value."""
1498 Returns a single 1 or 0 value."""
1562
1499
1563 return 1-(0 in [x in checklist for x in candidates])
1500 return 1-(0 in [x in checklist for x in candidates])
1564
1501
1565 #----------------------------------------------------------------------------
1502 #----------------------------------------------------------------------------
1566 def sort_compare(lst1,lst2,inplace = 1):
1503 def sort_compare(lst1,lst2,inplace = 1):
1567 """Sort and compare two lists.
1504 """Sort and compare two lists.
1568
1505
1569 By default it does it in place, thus modifying the lists. Use inplace = 0
1506 By default it does it in place, thus modifying the lists. Use inplace = 0
1570 to avoid that (at the cost of temporary copy creation)."""
1507 to avoid that (at the cost of temporary copy creation)."""
1571 if not inplace:
1508 if not inplace:
1572 lst1 = lst1[:]
1509 lst1 = lst1[:]
1573 lst2 = lst2[:]
1510 lst2 = lst2[:]
1574 lst1.sort(); lst2.sort()
1511 lst1.sort(); lst2.sort()
1575 return lst1 == lst2
1512 return lst1 == lst2
1576
1513
1577 #----------------------------------------------------------------------------
1514 #----------------------------------------------------------------------------
1578 def mkdict(**kwargs):
1515 def mkdict(**kwargs):
1579 """Return a dict from a keyword list.
1516 """Return a dict from a keyword list.
1580
1517
1581 It's just syntactic sugar for making ditcionary creation more convenient:
1518 It's just syntactic sugar for making ditcionary creation more convenient:
1582 # the standard way
1519 # the standard way
1583 >>>data = { 'red' : 1, 'green' : 2, 'blue' : 3 }
1520 >>>data = { 'red' : 1, 'green' : 2, 'blue' : 3 }
1584 # a cleaner way
1521 # a cleaner way
1585 >>>data = dict(red=1, green=2, blue=3)
1522 >>>data = dict(red=1, green=2, blue=3)
1586
1523
1587 If you need more than this, look at the Struct() class."""
1524 If you need more than this, look at the Struct() class."""
1588
1525
1589 return kwargs
1526 return kwargs
1590
1527
1591 #----------------------------------------------------------------------------
1528 #----------------------------------------------------------------------------
1592 def list2dict(lst):
1529 def list2dict(lst):
1593 """Takes a list of (key,value) pairs and turns it into a dict."""
1530 """Takes a list of (key,value) pairs and turns it into a dict."""
1594
1531
1595 dic = {}
1532 dic = {}
1596 for k,v in lst: dic[k] = v
1533 for k,v in lst: dic[k] = v
1597 return dic
1534 return dic
1598
1535
1599 #----------------------------------------------------------------------------
1536 #----------------------------------------------------------------------------
1600 def list2dict2(lst,default=''):
1537 def list2dict2(lst,default=''):
1601 """Takes a list and turns it into a dict.
1538 """Takes a list and turns it into a dict.
1602 Much slower than list2dict, but more versatile. This version can take
1539 Much slower than list2dict, but more versatile. This version can take
1603 lists with sublists of arbitrary length (including sclars)."""
1540 lists with sublists of arbitrary length (including sclars)."""
1604
1541
1605 dic = {}
1542 dic = {}
1606 for elem in lst:
1543 for elem in lst:
1607 if type(elem) in (types.ListType,types.TupleType):
1544 if type(elem) in (types.ListType,types.TupleType):
1608 size = len(elem)
1545 size = len(elem)
1609 if size == 0:
1546 if size == 0:
1610 pass
1547 pass
1611 elif size == 1:
1548 elif size == 1:
1612 dic[elem] = default
1549 dic[elem] = default
1613 else:
1550 else:
1614 k,v = elem[0], elem[1:]
1551 k,v = elem[0], elem[1:]
1615 if len(v) == 1: v = v[0]
1552 if len(v) == 1: v = v[0]
1616 dic[k] = v
1553 dic[k] = v
1617 else:
1554 else:
1618 dic[elem] = default
1555 dic[elem] = default
1619 return dic
1556 return dic
1620
1557
1621 #----------------------------------------------------------------------------
1558 #----------------------------------------------------------------------------
1622 def flatten(seq):
1559 def flatten(seq):
1623 """Flatten a list of lists (NOT recursive, only works for 2d lists)."""
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 return [x for subseq in seq for x in subseq]
1562 return [x for subseq in seq for x in subseq]
1636
1563
1637 #----------------------------------------------------------------------------
1564 #----------------------------------------------------------------------------
1638 def get_slice(seq,start=0,stop=None,step=1):
1565 def get_slice(seq,start=0,stop=None,step=1):
1639 """Get a slice of a sequence with variable step. Specify start,stop,step."""
1566 """Get a slice of a sequence with variable step. Specify start,stop,step."""
1640 if stop == None:
1567 if stop == None:
1641 stop = len(seq)
1568 stop = len(seq)
1642 item = lambda i: seq[i]
1569 item = lambda i: seq[i]
1643 return map(item,xrange(start,stop,step))
1570 return map(item,xrange(start,stop,step))
1644
1571
1645 #----------------------------------------------------------------------------
1572 #----------------------------------------------------------------------------
1646 def chop(seq,size):
1573 def chop(seq,size):
1647 """Chop a sequence into chunks of the given size."""
1574 """Chop a sequence into chunks of the given size."""
1648 chunk = lambda i: seq[i:i+size]
1575 chunk = lambda i: seq[i:i+size]
1649 return map(chunk,xrange(0,len(seq),size))
1576 return map(chunk,xrange(0,len(seq),size))
1650
1577
1651 #----------------------------------------------------------------------------
1578 #----------------------------------------------------------------------------
1652 def with(object, **args):
1579 def with(object, **args):
1653 """Set multiple attributes for an object, similar to Pascal's with.
1580 """Set multiple attributes for an object, similar to Pascal's with.
1654
1581
1655 Example:
1582 Example:
1656 with(jim,
1583 with(jim,
1657 born = 1960,
1584 born = 1960,
1658 haircolour = 'Brown',
1585 haircolour = 'Brown',
1659 eyecolour = 'Green')
1586 eyecolour = 'Green')
1660
1587
1661 Credit: Greg Ewing, in
1588 Credit: Greg Ewing, in
1662 http://mail.python.org/pipermail/python-list/2001-May/040703.html"""
1589 http://mail.python.org/pipermail/python-list/2001-May/040703.html"""
1663
1590
1664 object.__dict__.update(args)
1591 object.__dict__.update(args)
1665
1592
1666 #----------------------------------------------------------------------------
1593 #----------------------------------------------------------------------------
1667 def setattr_list(obj,alist,nspace = None):
1594 def setattr_list(obj,alist,nspace = None):
1668 """Set a list of attributes for an object taken from a namespace.
1595 """Set a list of attributes for an object taken from a namespace.
1669
1596
1670 setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in
1597 setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in
1671 alist with their values taken from nspace, which must be a dict (something
1598 alist with their values taken from nspace, which must be a dict (something
1672 like locals() will often do) If nspace isn't given, locals() of the
1599 like locals() will often do) If nspace isn't given, locals() of the
1673 *caller* is used, so in most cases you can omit it.
1600 *caller* is used, so in most cases you can omit it.
1674
1601
1675 Note that alist can be given as a string, which will be automatically
1602 Note that alist can be given as a string, which will be automatically
1676 split into a list on whitespace. If given as a list, it must be a list of
1603 split into a list on whitespace. If given as a list, it must be a list of
1677 *strings* (the variable names themselves), not of variables."""
1604 *strings* (the variable names themselves), not of variables."""
1678
1605
1679 # this grabs the local variables from the *previous* call frame -- that is
1606 # this grabs the local variables from the *previous* call frame -- that is
1680 # the locals from the function that called setattr_list().
1607 # the locals from the function that called setattr_list().
1681 # - snipped from weave.inline()
1608 # - snipped from weave.inline()
1682 if nspace is None:
1609 if nspace is None:
1683 call_frame = sys._getframe().f_back
1610 call_frame = sys._getframe().f_back
1684 nspace = call_frame.f_locals
1611 nspace = call_frame.f_locals
1685
1612
1686 if type(alist) in StringTypes:
1613 if type(alist) in StringTypes:
1687 alist = alist.split()
1614 alist = alist.split()
1688 for attr in alist:
1615 for attr in alist:
1689 val = eval(attr,nspace)
1616 val = eval(attr,nspace)
1690 setattr(obj,attr,val)
1617 setattr(obj,attr,val)
1691
1618
1692 #----------------------------------------------------------------------------
1619 #----------------------------------------------------------------------------
1693 def getattr_list(obj,alist,*args):
1620 def getattr_list(obj,alist,*args):
1694 """getattr_list(obj,alist[, default]) -> attribute list.
1621 """getattr_list(obj,alist[, default]) -> attribute list.
1695
1622
1696 Get a list of named attributes for an object. When a default argument is
1623 Get a list of named attributes for an object. When a default argument is
1697 given, it is returned when the attribute doesn't exist; without it, an
1624 given, it is returned when the attribute doesn't exist; without it, an
1698 exception is raised in that case.
1625 exception is raised in that case.
1699
1626
1700 Note that alist can be given as a string, which will be automatically
1627 Note that alist can be given as a string, which will be automatically
1701 split into a list on whitespace. If given as a list, it must be a list of
1628 split into a list on whitespace. If given as a list, it must be a list of
1702 *strings* (the variable names themselves), not of variables."""
1629 *strings* (the variable names themselves), not of variables."""
1703
1630
1704 if type(alist) in StringTypes:
1631 if type(alist) in StringTypes:
1705 alist = alist.split()
1632 alist = alist.split()
1706 if args:
1633 if args:
1707 if len(args)==1:
1634 if len(args)==1:
1708 default = args[0]
1635 default = args[0]
1709 return map(lambda attr: getattr(obj,attr,default),alist)
1636 return map(lambda attr: getattr(obj,attr,default),alist)
1710 else:
1637 else:
1711 raise ValueError,'getattr_list() takes only one optional argument'
1638 raise ValueError,'getattr_list() takes only one optional argument'
1712 else:
1639 else:
1713 return map(lambda attr: getattr(obj,attr),alist)
1640 return map(lambda attr: getattr(obj,attr),alist)
1714
1641
1715 #----------------------------------------------------------------------------
1642 #----------------------------------------------------------------------------
1716 def map_method(method,object_list,*argseq,**kw):
1643 def map_method(method,object_list,*argseq,**kw):
1717 """map_method(method,object_list,*args,**kw) -> list
1644 """map_method(method,object_list,*args,**kw) -> list
1718
1645
1719 Return a list of the results of applying the methods to the items of the
1646 Return a list of the results of applying the methods to the items of the
1720 argument sequence(s). If more than one sequence is given, the method is
1647 argument sequence(s). If more than one sequence is given, the method is
1721 called with an argument list consisting of the corresponding item of each
1648 called with an argument list consisting of the corresponding item of each
1722 sequence. All sequences must be of the same length.
1649 sequence. All sequences must be of the same length.
1723
1650
1724 Keyword arguments are passed verbatim to all objects called.
1651 Keyword arguments are passed verbatim to all objects called.
1725
1652
1726 This is Python code, so it's not nearly as fast as the builtin map()."""
1653 This is Python code, so it's not nearly as fast as the builtin map()."""
1727
1654
1728 out_list = []
1655 out_list = []
1729 idx = 0
1656 idx = 0
1730 for object in object_list:
1657 for object in object_list:
1731 try:
1658 try:
1732 handler = getattr(object, method)
1659 handler = getattr(object, method)
1733 except AttributeError:
1660 except AttributeError:
1734 out_list.append(None)
1661 out_list.append(None)
1735 else:
1662 else:
1736 if argseq:
1663 if argseq:
1737 args = map(lambda lst:lst[idx],argseq)
1664 args = map(lambda lst:lst[idx],argseq)
1738 #print 'ob',object,'hand',handler,'ar',args # dbg
1665 #print 'ob',object,'hand',handler,'ar',args # dbg
1739 out_list.append(handler(args,**kw))
1666 out_list.append(handler(args,**kw))
1740 else:
1667 else:
1741 out_list.append(handler(**kw))
1668 out_list.append(handler(**kw))
1742 idx += 1
1669 idx += 1
1743 return out_list
1670 return out_list
1744
1671
1745 #----------------------------------------------------------------------------
1672 #----------------------------------------------------------------------------
1746 def import_fail_info(mod_name,fns=None):
1673 def import_fail_info(mod_name,fns=None):
1747 """Inform load failure for a module."""
1674 """Inform load failure for a module."""
1748
1675
1749 if fns == None:
1676 if fns == None:
1750 warn("Loading of %s failed.\n" % (mod_name,))
1677 warn("Loading of %s failed.\n" % (mod_name,))
1751 else:
1678 else:
1752 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
1679 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
1753
1680
1754 #----------------------------------------------------------------------------
1681 #----------------------------------------------------------------------------
1755 # Proposed popitem() extension, written as a method
1682 # Proposed popitem() extension, written as a method
1756
1683
1757 class NotGiven: pass
1684 class NotGiven: pass
1758
1685
1759 def popkey(dct,key,default=NotGiven):
1686 def popkey(dct,key,default=NotGiven):
1760 """Return dct[key] and delete dct[key].
1687 """Return dct[key] and delete dct[key].
1761
1688
1762 If default is given, return it if dct[key] doesn't exist, otherwise raise
1689 If default is given, return it if dct[key] doesn't exist, otherwise raise
1763 KeyError. """
1690 KeyError. """
1764
1691
1765 try:
1692 try:
1766 val = dct[key]
1693 val = dct[key]
1767 except KeyError:
1694 except KeyError:
1768 if default is NotGiven:
1695 if default is NotGiven:
1769 raise
1696 raise
1770 else:
1697 else:
1771 return default
1698 return default
1772 else:
1699 else:
1773 del dct[key]
1700 del dct[key]
1774 return val
1701 return val
1775 #*************************** end of file <genutils.py> **********************
1702 #*************************** end of file <genutils.py> **********************
1776
1703
@@ -1,203 +1,321 b''
1 ''' IPython customization API
1 ''' IPython customization API
2
2
3 Your one-stop module for configuring & extending ipython
3 Your one-stop module for configuring & extending ipython
4
4
5 The API will probably break when ipython 1.0 is released, but so
5 The API will probably break when ipython 1.0 is released, but so
6 will the other configuration method (rc files).
6 will the other configuration method (rc files).
7
7
8 All names prefixed by underscores are for internal use, not part
8 All names prefixed by underscores are for internal use, not part
9 of the public api.
9 of the public api.
10
10
11 Below is an example that you can just put to a module and import from ipython.
11 Below is an example that you can just put to a module and import from ipython.
12
12
13 A good practice is to install the config script below as e.g.
13 A good practice is to install the config script below as e.g.
14
14
15 ~/.ipython/my_private_conf.py
15 ~/.ipython/my_private_conf.py
16
16
17 And do
17 And do
18
18
19 import_mod my_private_conf
19 import_mod my_private_conf
20
20
21 in ~/.ipython/ipythonrc
21 in ~/.ipython/ipythonrc
22
22
23 That way the module is imported at startup and you can have all your
23 That way the module is imported at startup and you can have all your
24 personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME
24 personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME
25 stuff) in there.
25 stuff) in there.
26
26
27 -----------------------------------------------
27 -----------------------------------------------
28 import IPython.ipapi as ip
28 import IPython.ipapi
29 ip = IPython.ipapi.get()
29
30
30 def ankka_f(self, arg):
31 def ankka_f(self, arg):
31 print "Ankka",self,"says uppercase:",arg.upper()
32 print "Ankka",self,"says uppercase:",arg.upper()
32
33
33 ip.expose_magic("ankka",ankka_f)
34 ip.expose_magic("ankka",ankka_f)
34
35
35 ip.magic('alias sayhi echo "Testing, hi ok"')
36 ip.magic('alias sayhi echo "Testing, hi ok"')
36 ip.magic('alias helloworld echo "Hello world"')
37 ip.magic('alias helloworld echo "Hello world"')
37 ip.system('pwd')
38 ip.system('pwd')
38
39
39 ip.ex('import re')
40 ip.ex('import re')
40 ip.ex("""
41 ip.ex("""
41 def funcci(a,b):
42 def funcci(a,b):
42 print a+b
43 print a+b
43 print funcci(3,4)
44 print funcci(3,4)
44 """)
45 """)
45 ip.ex("funcci(348,9)")
46 ip.ex("funcci(348,9)")
46
47
47 def jed_editor(self,filename, linenum=None):
48 def jed_editor(self,filename, linenum=None):
48 print "Calling my own editor, jed ... via hook!"
49 print "Calling my own editor, jed ... via hook!"
49 import os
50 import os
50 if linenum is None: linenum = 0
51 if linenum is None: linenum = 0
51 os.system('jed +%d %s' % (linenum, filename))
52 os.system('jed +%d %s' % (linenum, filename))
52 print "exiting jed"
53 print "exiting jed"
53
54
54 ip.set_hook('editor',jed_editor)
55 ip.set_hook('editor',jed_editor)
55
56
56 o = ip.options()
57 o = ip.options
57 o.autocall = 2 # FULL autocall mode
58 o.autocall = 2 # FULL autocall mode
58
59
59 print "done!"
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 class TryNext(Exception):
69 class TryNext(Exception):
65 """ Try next hook exception.
70 """Try next hook exception.
66
71
67 Raise this in your hook function to indicate that the next
72 Raise this in your hook function to indicate that the next hook handler
68 hook handler should be used to handle the operation.
73 should be used to handle the operation. If you pass arguments to the
69 If you pass arguments to the constructor those arguments will be
74 constructor those arguments will be used by the next hook instead of the
70 used by the next hook instead of the original ones.
75 original ones.
71 """
76 """
72
77
73 def __init__(self, *args, **kwargs):
78 def __init__(self, *args, **kwargs):
74 self.args = args
79 self.args = args
75 self.kwargs = kwargs
80 self.kwargs = kwargs
76
81
77
78 # contains the most recently instantiated IPApi
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 def get():
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
111 Returns an instance of IPythonNotRunning if not running under IPython.
85 extensions that can be imported as normal modules. You can then
86 direct all the configuration operations against the returned
87 object.
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 return _recent
118 return _recent
92
119
93
94
95 class IPApi:
120 class IPApi:
96 """ The actual API class for configuring IPython
121 """ The actual API class for configuring IPython
97
122
98 You should do all of the IPython configuration by getting
123 You should do all of the IPython configuration by getting an IPApi object
99 an IPApi object with IPython.ipapi.get() and using the provided
124 with IPython.ipapi.get() and using the attributes and methods of the
100 methods.
125 returned object."""
101
126
102 """
103 def __init__(self,ip):
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 self.magic = ip.ipmagic
133 self.magic = ip.ipmagic
106
134
107 self.system = ip.ipsystem
135 self.system = ip.ipsystem
108
136
109 self.set_hook = ip.set_hook
137 self.set_hook = ip.set_hook
110
138
111 self.set_custom_exc = ip.set_custom_exc
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 self.IP = ip
148 self.IP = ip
149
114 global _recent
150 global _recent
115 _recent = self
151 _recent = self
116
152
117
153 # Use a property for some things which are added to the instance very
118
154 # late. I don't have time right now to disentangle the initialization
119 def options(self):
155 # order issues, so a property lets us delay item extraction while
120 """ All configurable variables """
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
160
161 db = property(get_db,None,None,get_db.__doc__)
162
163 def get_options(self):
164 """All configurable variables."""
121 return self.IP.rc
165 return self.IP.rc
122
166
123 def user_ns(self):
167 options = property(get_options,None,None,get_options.__doc__)
124 return self.IP.user_ns
125
168
126 def expose_magic(self,magicname, func):
169 def expose_magic(self,magicname, func):
127 ''' Expose own function as magic function for ipython
170 ''' Expose own function as magic function for ipython
128
171
129 def foo_impl(self,parameter_s=''):
172 def foo_impl(self,parameter_s=''):
130 """My very own magic!. (Use docstrings, IPython reads them)."""
173 """My very own magic!. (Use docstrings, IPython reads them)."""
131 print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>'
174 print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>'
132 print 'The self object is:',self
175 print 'The self object is:',self
133
176
134 ipapi.expose_magic("foo",foo_impl)
177 ipapi.expose_magic("foo",foo_impl)
135 '''
178 '''
136
179
137 import new
180 import new
138 im = new.instancemethod(func,self.IP, self.IP.__class__)
181 im = new.instancemethod(func,self.IP, self.IP.__class__)
139 setattr(self.IP, "magic_" + magicname, im)
182 setattr(self.IP, "magic_" + magicname, im)
140
183
141
142 def ex(self,cmd):
184 def ex(self,cmd):
143 """ Execute a normal python statement in user namespace """
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 def ev(self,expr):
188 def ev(self,expr):
147 """ Evaluate python expression expr in user namespace
189 """ Evaluate python expression expr in user namespace
148
190
149 Returns the result of evaluation"""
191 Returns the result of evaluation"""
150 return eval(expr,self.user_ns())
192 return eval(expr,self.user_ns)
151
193
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
162
163 Return a PickleShareDB object.
164 """
165 return self.IP.db
166 def runlines(self,lines):
194 def runlines(self,lines):
167 """ Run the specified lines in interpreter, honoring ipython directives.
195 """ Run the specified lines in interpreter, honoring ipython directives.
168
196
169 This allows %magic and !shell escape notations.
197 This allows %magic and !shell escape notations.
170
198
171 Takes either all lines in one string or list of lines.
199 Takes either all lines in one string or list of lines.
172 """
200 """
173 if isinstance(lines,basestring):
201 if isinstance(lines,basestring):
174 self.IP.runlines(lines)
202 self.IP.runlines(lines)
175 else:
203 else:
176 self.IP.runlines('\n'.join(lines))
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 def launch_new_instance(user_ns = None):
297 def launch_new_instance(user_ns = None):
180 """ Create and start a new ipython instance.
298 """ Create and start a new ipython instance.
181
299
182 This can be called even without having an already initialized
300 This can be called even without having an already initialized
183 ipython session running.
301 ipython session running.
184
302
185 This is also used as the egg entry point for the 'ipython' script.
303 This is also used as the egg entry point for the 'ipython' script.
186
304
187 """
305 """
188 ses = create_session(user_ns)
306 ses = create_session(user_ns)
189 ses.mainloop()
307 ses.mainloop()
190
308
191
309
192 def create_session(user_ns = None):
310 def create_session(user_ns = None):
193 """ Creates, but does not launch an IPython session.
311 """ Creates, but does not launch an IPython session.
194
312
195 Later on you can call obj.mainloop() on the returned object.
313 Later on you can call obj.mainloop() on the returned object.
196
314
197 This should *not* be run when a session exists already.
315 This should *not* be run when a session exists already.
198
316
199 """
317 """
200 if user_ns is not None:
318 if user_ns is not None:
201 user_ns["__name__"] = user_ns.get("__name__",'ipy_session')
319 user_ns["__name__"] = user_ns.get("__name__",'ipy_session')
202 import IPython
320 import IPython
203 return IPython.Shell.start(user_ns = user_ns) No newline at end of file
321 return IPython.Shell.start(user_ns = user_ns)
@@ -1,2287 +1,2284 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 IPython -- An enhanced Interactive Python
3 IPython -- An enhanced Interactive Python
4
4
5 Requires Python 2.3 or newer.
5 Requires Python 2.3 or newer.
6
6
7 This file contains all the classes and helper functions specific to IPython.
7 This file contains all the classes and helper functions specific to IPython.
8
8
9 $Id: iplib.py 1277 2006-05-02 10:31:55Z walter.doerwald $
9 $Id: iplib.py 1314 2006-05-19 18:24:14Z fperez $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
13 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
13 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
14 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
14 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
15 #
15 #
16 # Distributed under the terms of the BSD License. The full license is in
16 # Distributed under the terms of the BSD License. The full license is in
17 # the file COPYING, distributed as part of this software.
17 # the file COPYING, distributed as part of this software.
18 #
18 #
19 # Note: this code originally subclassed code.InteractiveConsole from the
19 # Note: this code originally subclassed code.InteractiveConsole from the
20 # Python standard library. Over time, all of that class has been copied
20 # Python standard library. Over time, all of that class has been copied
21 # verbatim here for modifications which could not be accomplished by
21 # verbatim here for modifications which could not be accomplished by
22 # subclassing. At this point, there are no dependencies at all on the code
22 # subclassing. At this point, there are no dependencies at all on the code
23 # module anymore (it is not even imported). The Python License (sec. 2)
23 # module anymore (it is not even imported). The Python License (sec. 2)
24 # allows for this, but it's always nice to acknowledge credit where credit is
24 # allows for this, but it's always nice to acknowledge credit where credit is
25 # due.
25 # due.
26 #*****************************************************************************
26 #*****************************************************************************
27
27
28 #****************************************************************************
28 #****************************************************************************
29 # Modules and globals
29 # Modules and globals
30
30
31 from __future__ import generators # for 2.2 backwards-compatibility
32
33 from IPython import Release
31 from IPython import Release
34 __author__ = '%s <%s>\n%s <%s>' % \
32 __author__ = '%s <%s>\n%s <%s>' % \
35 ( Release.authors['Janko'] + Release.authors['Fernando'] )
33 ( Release.authors['Janko'] + Release.authors['Fernando'] )
36 __license__ = Release.license
34 __license__ = Release.license
37 __version__ = Release.version
35 __version__ = Release.version
38
36
39 # Python standard modules
37 # Python standard modules
40 import __main__
38 import __main__
41 import __builtin__
39 import __builtin__
42 import StringIO
40 import StringIO
43 import bdb
41 import bdb
44 import cPickle as pickle
42 import cPickle as pickle
45 import codeop
43 import codeop
46 import exceptions
44 import exceptions
47 import glob
45 import glob
48 import inspect
46 import inspect
49 import keyword
47 import keyword
50 import new
48 import new
51 import os
49 import os
52 import pdb
50 import pdb
53 import pydoc
51 import pydoc
54 import re
52 import re
55 import shutil
53 import shutil
56 import string
54 import string
57 import sys
55 import sys
58 import tempfile
56 import tempfile
59 import traceback
57 import traceback
60 import types
58 import types
61 import pickleshare
59 import pickleshare
62
60
63 from pprint import pprint, pformat
61 from pprint import pprint, pformat
64
62
65 # IPython's own modules
63 # IPython's own modules
66 import IPython
64 import IPython
67 from IPython import OInspect,PyColorize,ultraTB
65 from IPython import OInspect,PyColorize,ultraTB
68 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
66 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
69 from IPython.FakeModule import FakeModule
67 from IPython.FakeModule import FakeModule
70 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
68 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
71 from IPython.Logger import Logger
69 from IPython.Logger import Logger
72 from IPython.Magic import Magic
70 from IPython.Magic import Magic
73 from IPython.Prompts import CachedOutput
71 from IPython.Prompts import CachedOutput
74 from IPython.ipstruct import Struct
72 from IPython.ipstruct import Struct
75 from IPython.background_jobs import BackgroundJobManager
73 from IPython.background_jobs import BackgroundJobManager
76 from IPython.usage import cmd_line_usage,interactive_usage
74 from IPython.usage import cmd_line_usage,interactive_usage
77 from IPython.genutils import *
75 from IPython.genutils import *
78 import IPython.ipapi
76 import IPython.ipapi
79
77
80 # Globals
78 # Globals
81
79
82 # store the builtin raw_input globally, and use this always, in case user code
80 # store the builtin raw_input globally, and use this always, in case user code
83 # overwrites it (like wx.py.PyShell does)
81 # overwrites it (like wx.py.PyShell does)
84 raw_input_original = raw_input
82 raw_input_original = raw_input
85
83
86 # compiled regexps for autoindent management
84 # compiled regexps for autoindent management
87 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
85 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
88
86
89
87
90 #****************************************************************************
88 #****************************************************************************
91 # Some utility function definitions
89 # Some utility function definitions
92
90
93 ini_spaces_re = re.compile(r'^(\s+)')
91 ini_spaces_re = re.compile(r'^(\s+)')
94
92
95 def num_ini_spaces(strng):
93 def num_ini_spaces(strng):
96 """Return the number of initial spaces in a string"""
94 """Return the number of initial spaces in a string"""
97
95
98 ini_spaces = ini_spaces_re.match(strng)
96 ini_spaces = ini_spaces_re.match(strng)
99 if ini_spaces:
97 if ini_spaces:
100 return ini_spaces.end()
98 return ini_spaces.end()
101 else:
99 else:
102 return 0
100 return 0
103
101
104 def softspace(file, newvalue):
102 def softspace(file, newvalue):
105 """Copied from code.py, to remove the dependency"""
103 """Copied from code.py, to remove the dependency"""
106
104
107 oldvalue = 0
105 oldvalue = 0
108 try:
106 try:
109 oldvalue = file.softspace
107 oldvalue = file.softspace
110 except AttributeError:
108 except AttributeError:
111 pass
109 pass
112 try:
110 try:
113 file.softspace = newvalue
111 file.softspace = newvalue
114 except (AttributeError, TypeError):
112 except (AttributeError, TypeError):
115 # "attribute-less object" or "read-only attributes"
113 # "attribute-less object" or "read-only attributes"
116 pass
114 pass
117 return oldvalue
115 return oldvalue
118
116
119
117
120 #****************************************************************************
118 #****************************************************************************
121 # Local use exceptions
119 # Local use exceptions
122 class SpaceInInput(exceptions.Exception): pass
120 class SpaceInInput(exceptions.Exception): pass
123
121
124
122
125 #****************************************************************************
123 #****************************************************************************
126 # Local use classes
124 # Local use classes
127 class Bunch: pass
125 class Bunch: pass
128
126
129 class Undefined: pass
127 class Undefined: pass
130
128
131 class InputList(list):
129 class InputList(list):
132 """Class to store user input.
130 """Class to store user input.
133
131
134 It's basically a list, but slices return a string instead of a list, thus
132 It's basically a list, but slices return a string instead of a list, thus
135 allowing things like (assuming 'In' is an instance):
133 allowing things like (assuming 'In' is an instance):
136
134
137 exec In[4:7]
135 exec In[4:7]
138
136
139 or
137 or
140
138
141 exec In[5:9] + In[14] + In[21:25]"""
139 exec In[5:9] + In[14] + In[21:25]"""
142
140
143 def __getslice__(self,i,j):
141 def __getslice__(self,i,j):
144 return ''.join(list.__getslice__(self,i,j))
142 return ''.join(list.__getslice__(self,i,j))
145
143
146 class SyntaxTB(ultraTB.ListTB):
144 class SyntaxTB(ultraTB.ListTB):
147 """Extension which holds some state: the last exception value"""
145 """Extension which holds some state: the last exception value"""
148
146
149 def __init__(self,color_scheme = 'NoColor'):
147 def __init__(self,color_scheme = 'NoColor'):
150 ultraTB.ListTB.__init__(self,color_scheme)
148 ultraTB.ListTB.__init__(self,color_scheme)
151 self.last_syntax_error = None
149 self.last_syntax_error = None
152
150
153 def __call__(self, etype, value, elist):
151 def __call__(self, etype, value, elist):
154 self.last_syntax_error = value
152 self.last_syntax_error = value
155 ultraTB.ListTB.__call__(self,etype,value,elist)
153 ultraTB.ListTB.__call__(self,etype,value,elist)
156
154
157 def clear_err_state(self):
155 def clear_err_state(self):
158 """Return the current error state and clear it"""
156 """Return the current error state and clear it"""
159 e = self.last_syntax_error
157 e = self.last_syntax_error
160 self.last_syntax_error = None
158 self.last_syntax_error = None
161 return e
159 return e
162
160
163 #****************************************************************************
161 #****************************************************************************
164 # Main IPython class
162 # Main IPython class
165
163
166 # FIXME: the Magic class is a mixin for now, and will unfortunately remain so
164 # FIXME: the Magic class is a mixin for now, and will unfortunately remain so
167 # until a full rewrite is made. I've cleaned all cross-class uses of
165 # until a full rewrite is made. I've cleaned all cross-class uses of
168 # attributes and methods, but too much user code out there relies on the
166 # attributes and methods, but too much user code out there relies on the
169 # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
167 # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
170 #
168 #
171 # But at least now, all the pieces have been separated and we could, in
169 # But at least now, all the pieces have been separated and we could, in
172 # principle, stop using the mixin. This will ease the transition to the
170 # principle, stop using the mixin. This will ease the transition to the
173 # chainsaw branch.
171 # chainsaw branch.
174
172
175 # For reference, the following is the list of 'self.foo' uses in the Magic
173 # For reference, the following is the list of 'self.foo' uses in the Magic
176 # class as of 2005-12-28. These are names we CAN'T use in the main ipython
174 # class as of 2005-12-28. These are names we CAN'T use in the main ipython
177 # class, to prevent clashes.
175 # class, to prevent clashes.
178
176
179 # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
177 # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
180 # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
178 # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
181 # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
179 # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
182 # 'self.value']
180 # 'self.value']
183
181
184 class InteractiveShell(object,Magic):
182 class InteractiveShell(object,Magic):
185 """An enhanced console for Python."""
183 """An enhanced console for Python."""
186
184
187 # class attribute to indicate whether the class supports threads or not.
185 # class attribute to indicate whether the class supports threads or not.
188 # Subclasses with thread support should override this as needed.
186 # Subclasses with thread support should override this as needed.
189 isthreaded = False
187 isthreaded = False
190
188
191 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
189 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
192 user_ns = None,user_global_ns=None,banner2='',
190 user_ns = None,user_global_ns=None,banner2='',
193 custom_exceptions=((),None),embedded=False):
191 custom_exceptions=((),None),embedded=False):
194
192
195
193
196 # log system
194 # log system
197 self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate')
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 # some minimal strict typechecks. For some core data structures, I
197 # some minimal strict typechecks. For some core data structures, I
204 # want actual basic python types, not just anything that looks like
198 # want actual basic python types, not just anything that looks like
205 # one. This is especially true for namespaces.
199 # one. This is especially true for namespaces.
206 for ns in (user_ns,user_global_ns):
200 for ns in (user_ns,user_global_ns):
207 if ns is not None and type(ns) != types.DictType:
201 if ns is not None and type(ns) != types.DictType:
208 raise TypeError,'namespace must be a dictionary'
202 raise TypeError,'namespace must be a dictionary'
209
203
210 # Job manager (for jobs run as background threads)
204 # Job manager (for jobs run as background threads)
211 self.jobs = BackgroundJobManager()
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 # Do the intuitively correct thing for quit/exit: we remove the
207 # Do the intuitively correct thing for quit/exit: we remove the
220 # builtins if they exist, and our own magics will deal with this
208 # builtins if they exist, and our own magics will deal with this
221 try:
209 try:
222 del __builtin__.exit, __builtin__.quit
210 del __builtin__.exit, __builtin__.quit
223 except AttributeError:
211 except AttributeError:
224 pass
212 pass
225
213
226 # Store the actual shell's name
214 # Store the actual shell's name
227 self.name = name
215 self.name = name
228
216
229 # We need to know whether the instance is meant for embedding, since
217 # We need to know whether the instance is meant for embedding, since
230 # global/local namespaces need to be handled differently in that case
218 # global/local namespaces need to be handled differently in that case
231 self.embedded = embedded
219 self.embedded = embedded
232
220
233 # command compiler
221 # command compiler
234 self.compile = codeop.CommandCompiler()
222 self.compile = codeop.CommandCompiler()
235
223
236 # User input buffer
224 # User input buffer
237 self.buffer = []
225 self.buffer = []
238
226
239 # Default name given in compilation of code
227 # Default name given in compilation of code
240 self.filename = '<ipython console>'
228 self.filename = '<ipython console>'
241
229
242 # Make an empty namespace, which extension writers can rely on both
230 # Make an empty namespace, which extension writers can rely on both
243 # existing and NEVER being used by ipython itself. This gives them a
231 # existing and NEVER being used by ipython itself. This gives them a
244 # convenient location for storing additional information and state
232 # convenient location for storing additional information and state
245 # their extensions may require, without fear of collisions with other
233 # their extensions may require, without fear of collisions with other
246 # ipython names that may develop later.
234 # ipython names that may develop later.
247 self.meta = Struct()
235 self.meta = Struct()
248
236
249 # Create the namespace where the user will operate. user_ns is
237 # Create the namespace where the user will operate. user_ns is
250 # normally the only one used, and it is passed to the exec calls as
238 # normally the only one used, and it is passed to the exec calls as
251 # the locals argument. But we do carry a user_global_ns namespace
239 # the locals argument. But we do carry a user_global_ns namespace
252 # given as the exec 'globals' argument, This is useful in embedding
240 # given as the exec 'globals' argument, This is useful in embedding
253 # situations where the ipython shell opens in a context where the
241 # situations where the ipython shell opens in a context where the
254 # distinction between locals and globals is meaningful.
242 # distinction between locals and globals is meaningful.
255
243
256 # FIXME. For some strange reason, __builtins__ is showing up at user
244 # FIXME. For some strange reason, __builtins__ is showing up at user
257 # level as a dict instead of a module. This is a manual fix, but I
245 # level as a dict instead of a module. This is a manual fix, but I
258 # should really track down where the problem is coming from. Alex
246 # should really track down where the problem is coming from. Alex
259 # Schmolck reported this problem first.
247 # Schmolck reported this problem first.
260
248
261 # A useful post by Alex Martelli on this topic:
249 # A useful post by Alex Martelli on this topic:
262 # Re: inconsistent value from __builtins__
250 # Re: inconsistent value from __builtins__
263 # Von: Alex Martelli <aleaxit@yahoo.com>
251 # Von: Alex Martelli <aleaxit@yahoo.com>
264 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
252 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
265 # Gruppen: comp.lang.python
253 # Gruppen: comp.lang.python
266
254
267 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
255 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
268 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
256 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
269 # > <type 'dict'>
257 # > <type 'dict'>
270 # > >>> print type(__builtins__)
258 # > >>> print type(__builtins__)
271 # > <type 'module'>
259 # > <type 'module'>
272 # > Is this difference in return value intentional?
260 # > Is this difference in return value intentional?
273
261
274 # Well, it's documented that '__builtins__' can be either a dictionary
262 # Well, it's documented that '__builtins__' can be either a dictionary
275 # or a module, and it's been that way for a long time. Whether it's
263 # or a module, and it's been that way for a long time. Whether it's
276 # intentional (or sensible), I don't know. In any case, the idea is
264 # intentional (or sensible), I don't know. In any case, the idea is
277 # that if you need to access the built-in namespace directly, you
265 # that if you need to access the built-in namespace directly, you
278 # should start with "import __builtin__" (note, no 's') which will
266 # should start with "import __builtin__" (note, no 's') which will
279 # definitely give you a module. Yeah, it's somewhat confusing:-(.
267 # definitely give you a module. Yeah, it's somewhat confusing:-(.
280
268
281 if user_ns is None:
269 if user_ns is None:
282 # Set __name__ to __main__ to better match the behavior of the
270 # Set __name__ to __main__ to better match the behavior of the
283 # normal interpreter.
271 # normal interpreter.
284 user_ns = {'__name__' :'__main__',
272 user_ns = {'__name__' :'__main__',
285 '__builtins__' : __builtin__,
273 '__builtins__' : __builtin__,
286 }
274 }
287
275
288 if user_global_ns is None:
276 if user_global_ns is None:
289 user_global_ns = {}
277 user_global_ns = {}
290
278
291 # Assign namespaces
279 # Assign namespaces
292 # This is the namespace where all normal user variables live
280 # This is the namespace where all normal user variables live
293 self.user_ns = user_ns
281 self.user_ns = user_ns
294 # Embedded instances require a separate namespace for globals.
282 # Embedded instances require a separate namespace for globals.
295 # Normally this one is unused by non-embedded instances.
283 # Normally this one is unused by non-embedded instances.
296 self.user_global_ns = user_global_ns
284 self.user_global_ns = user_global_ns
297 # A namespace to keep track of internal data structures to prevent
285 # A namespace to keep track of internal data structures to prevent
298 # them from cluttering user-visible stuff. Will be updated later
286 # them from cluttering user-visible stuff. Will be updated later
299 self.internal_ns = {}
287 self.internal_ns = {}
300
288
301 # Namespace of system aliases. Each entry in the alias
289 # Namespace of system aliases. Each entry in the alias
302 # table must be a 2-tuple of the form (N,name), where N is the number
290 # table must be a 2-tuple of the form (N,name), where N is the number
303 # of positional arguments of the alias.
291 # of positional arguments of the alias.
304 self.alias_table = {}
292 self.alias_table = {}
305
293
306 # A table holding all the namespaces IPython deals with, so that
294 # A table holding all the namespaces IPython deals with, so that
307 # introspection facilities can search easily.
295 # introspection facilities can search easily.
308 self.ns_table = {'user':user_ns,
296 self.ns_table = {'user':user_ns,
309 'user_global':user_global_ns,
297 'user_global':user_global_ns,
310 'alias':self.alias_table,
298 'alias':self.alias_table,
311 'internal':self.internal_ns,
299 'internal':self.internal_ns,
312 'builtin':__builtin__.__dict__
300 'builtin':__builtin__.__dict__
313 }
301 }
314
302
315 # The user namespace MUST have a pointer to the shell itself.
303 # The user namespace MUST have a pointer to the shell itself.
316 self.user_ns[name] = self
304 self.user_ns[name] = self
317
305
318 # We need to insert into sys.modules something that looks like a
306 # We need to insert into sys.modules something that looks like a
319 # module but which accesses the IPython namespace, for shelve and
307 # module but which accesses the IPython namespace, for shelve and
320 # pickle to work interactively. Normally they rely on getting
308 # pickle to work interactively. Normally they rely on getting
321 # everything out of __main__, but for embedding purposes each IPython
309 # everything out of __main__, but for embedding purposes each IPython
322 # instance has its own private namespace, so we can't go shoving
310 # instance has its own private namespace, so we can't go shoving
323 # everything into __main__.
311 # everything into __main__.
324
312
325 # note, however, that we should only do this for non-embedded
313 # note, however, that we should only do this for non-embedded
326 # ipythons, which really mimic the __main__.__dict__ with their own
314 # ipythons, which really mimic the __main__.__dict__ with their own
327 # namespace. Embedded instances, on the other hand, should not do
315 # namespace. Embedded instances, on the other hand, should not do
328 # this because they need to manage the user local/global namespaces
316 # this because they need to manage the user local/global namespaces
329 # only, but they live within a 'normal' __main__ (meaning, they
317 # only, but they live within a 'normal' __main__ (meaning, they
330 # shouldn't overtake the execution environment of the script they're
318 # shouldn't overtake the execution environment of the script they're
331 # embedded in).
319 # embedded in).
332
320
333 if not embedded:
321 if not embedded:
334 try:
322 try:
335 main_name = self.user_ns['__name__']
323 main_name = self.user_ns['__name__']
336 except KeyError:
324 except KeyError:
337 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
325 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
338 else:
326 else:
339 #print "pickle hack in place" # dbg
327 #print "pickle hack in place" # dbg
340 #print 'main_name:',main_name # dbg
328 #print 'main_name:',main_name # dbg
341 sys.modules[main_name] = FakeModule(self.user_ns)
329 sys.modules[main_name] = FakeModule(self.user_ns)
342
330
343 # List of input with multi-line handling.
331 # List of input with multi-line handling.
344 # Fill its zero entry, user counter starts at 1
332 # Fill its zero entry, user counter starts at 1
345 self.input_hist = InputList(['\n'])
333 self.input_hist = InputList(['\n'])
346 # This one will hold the 'raw' input history, without any
334 # This one will hold the 'raw' input history, without any
347 # pre-processing. This will allow users to retrieve the input just as
335 # pre-processing. This will allow users to retrieve the input just as
348 # it was exactly typed in by the user, with %hist -r.
336 # it was exactly typed in by the user, with %hist -r.
349 self.input_hist_raw = InputList(['\n'])
337 self.input_hist_raw = InputList(['\n'])
350
338
351 # list of visited directories
339 # list of visited directories
352 try:
340 try:
353 self.dir_hist = [os.getcwd()]
341 self.dir_hist = [os.getcwd()]
354 except IOError, e:
342 except IOError, e:
355 self.dir_hist = []
343 self.dir_hist = []
356
344
357 # dict of output history
345 # dict of output history
358 self.output_hist = {}
346 self.output_hist = {}
359
347
360 # dict of things NOT to alias (keywords, builtins and some magics)
348 # dict of things NOT to alias (keywords, builtins and some magics)
361 no_alias = {}
349 no_alias = {}
362 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
350 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
363 for key in keyword.kwlist + no_alias_magics:
351 for key in keyword.kwlist + no_alias_magics:
364 no_alias[key] = 1
352 no_alias[key] = 1
365 no_alias.update(__builtin__.__dict__)
353 no_alias.update(__builtin__.__dict__)
366 self.no_alias = no_alias
354 self.no_alias = no_alias
367
355
368 # make global variables for user access to these
356 # make global variables for user access to these
369 self.user_ns['_ih'] = self.input_hist
357 self.user_ns['_ih'] = self.input_hist
370 self.user_ns['_oh'] = self.output_hist
358 self.user_ns['_oh'] = self.output_hist
371 self.user_ns['_dh'] = self.dir_hist
359 self.user_ns['_dh'] = self.dir_hist
372
360
373 # user aliases to input and output histories
361 # user aliases to input and output histories
374 self.user_ns['In'] = self.input_hist
362 self.user_ns['In'] = self.input_hist
375 self.user_ns['Out'] = self.output_hist
363 self.user_ns['Out'] = self.output_hist
376
364
377 # Object variable to store code object waiting execution. This is
365 # Object variable to store code object waiting execution. This is
378 # used mainly by the multithreaded shells, but it can come in handy in
366 # used mainly by the multithreaded shells, but it can come in handy in
379 # other situations. No need to use a Queue here, since it's a single
367 # other situations. No need to use a Queue here, since it's a single
380 # item which gets cleared once run.
368 # item which gets cleared once run.
381 self.code_to_run = None
369 self.code_to_run = None
382
370
383 # escapes for automatic behavior on the command line
371 # escapes for automatic behavior on the command line
384 self.ESC_SHELL = '!'
372 self.ESC_SHELL = '!'
385 self.ESC_HELP = '?'
373 self.ESC_HELP = '?'
386 self.ESC_MAGIC = '%'
374 self.ESC_MAGIC = '%'
387 self.ESC_QUOTE = ','
375 self.ESC_QUOTE = ','
388 self.ESC_QUOTE2 = ';'
376 self.ESC_QUOTE2 = ';'
389 self.ESC_PAREN = '/'
377 self.ESC_PAREN = '/'
390
378
391 # And their associated handlers
379 # And their associated handlers
392 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
380 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
393 self.ESC_QUOTE : self.handle_auto,
381 self.ESC_QUOTE : self.handle_auto,
394 self.ESC_QUOTE2 : self.handle_auto,
382 self.ESC_QUOTE2 : self.handle_auto,
395 self.ESC_MAGIC : self.handle_magic,
383 self.ESC_MAGIC : self.handle_magic,
396 self.ESC_HELP : self.handle_help,
384 self.ESC_HELP : self.handle_help,
397 self.ESC_SHELL : self.handle_shell_escape,
385 self.ESC_SHELL : self.handle_shell_escape,
398 }
386 }
399
387
400 # class initializations
388 # class initializations
401 Magic.__init__(self,self)
389 Magic.__init__(self,self)
402
390
403 # Python source parser/formatter for syntax highlighting
391 # Python source parser/formatter for syntax highlighting
404 pyformat = PyColorize.Parser().format
392 pyformat = PyColorize.Parser().format
405 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
393 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
406
394
407 # hooks holds pointers used for user-side customizations
395 # hooks holds pointers used for user-side customizations
408 self.hooks = Struct()
396 self.hooks = Struct()
409
397
410 # Set all default hooks, defined in the IPython.hooks module.
398 # Set all default hooks, defined in the IPython.hooks module.
411 hooks = IPython.hooks
399 hooks = IPython.hooks
412 for hook_name in hooks.__all__:
400 for hook_name in hooks.__all__:
413 # default hooks have priority 100, i.e. low; user hooks should have 0-100 priority
401 # default hooks have priority 100, i.e. low; user hooks should have 0-100 priority
414 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
402 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
415 #print "bound hook",hook_name
403 #print "bound hook",hook_name
416
404
417 # Flag to mark unconditional exit
405 # Flag to mark unconditional exit
418 self.exit_now = False
406 self.exit_now = False
419
407
420 self.usage_min = """\
408 self.usage_min = """\
421 An enhanced console for Python.
409 An enhanced console for Python.
422 Some of its features are:
410 Some of its features are:
423 - Readline support if the readline library is present.
411 - Readline support if the readline library is present.
424 - Tab completion in the local namespace.
412 - Tab completion in the local namespace.
425 - Logging of input, see command-line options.
413 - Logging of input, see command-line options.
426 - System shell escape via ! , eg !ls.
414 - System shell escape via ! , eg !ls.
427 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
415 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
428 - Keeps track of locally defined variables via %who, %whos.
416 - Keeps track of locally defined variables via %who, %whos.
429 - Show object information with a ? eg ?x or x? (use ?? for more info).
417 - Show object information with a ? eg ?x or x? (use ?? for more info).
430 """
418 """
431 if usage: self.usage = usage
419 if usage: self.usage = usage
432 else: self.usage = self.usage_min
420 else: self.usage = self.usage_min
433
421
434 # Storage
422 # Storage
435 self.rc = rc # This will hold all configuration information
423 self.rc = rc # This will hold all configuration information
436 self.pager = 'less'
424 self.pager = 'less'
437 # temporary files used for various purposes. Deleted at exit.
425 # temporary files used for various purposes. Deleted at exit.
438 self.tempfiles = []
426 self.tempfiles = []
439
427
440 # Keep track of readline usage (later set by init_readline)
428 # Keep track of readline usage (later set by init_readline)
441 self.has_readline = False
429 self.has_readline = False
442
430
443 # template for logfile headers. It gets resolved at runtime by the
431 # template for logfile headers. It gets resolved at runtime by the
444 # logstart method.
432 # logstart method.
445 self.loghead_tpl = \
433 self.loghead_tpl = \
446 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
434 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
447 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
435 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
448 #log# opts = %s
436 #log# opts = %s
449 #log# args = %s
437 #log# args = %s
450 #log# It is safe to make manual edits below here.
438 #log# It is safe to make manual edits below here.
451 #log#-----------------------------------------------------------------------
439 #log#-----------------------------------------------------------------------
452 """
440 """
453 # for pushd/popd management
441 # for pushd/popd management
454 try:
442 try:
455 self.home_dir = get_home_dir()
443 self.home_dir = get_home_dir()
456 except HomeDirError,msg:
444 except HomeDirError,msg:
457 fatal(msg)
445 fatal(msg)
458
446
459 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
447 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
460
448
461 # Functions to call the underlying shell.
449 # Functions to call the underlying shell.
462
450
463 # utility to expand user variables via Itpl
451 # utility to expand user variables via Itpl
464 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
452 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
465 self.user_ns))
453 self.user_ns))
466 # The first is similar to os.system, but it doesn't return a value,
454 # The first is similar to os.system, but it doesn't return a value,
467 # and it allows interpolation of variables in the user's namespace.
455 # and it allows interpolation of variables in the user's namespace.
468 self.system = lambda cmd: shell(self.var_expand(cmd),
456 self.system = lambda cmd: shell(self.var_expand(cmd),
469 header='IPython system call: ',
457 header='IPython system call: ',
470 verbose=self.rc.system_verbose)
458 verbose=self.rc.system_verbose)
471 # These are for getoutput and getoutputerror:
459 # These are for getoutput and getoutputerror:
472 self.getoutput = lambda cmd: \
460 self.getoutput = lambda cmd: \
473 getoutput(self.var_expand(cmd),
461 getoutput(self.var_expand(cmd),
474 header='IPython system call: ',
462 header='IPython system call: ',
475 verbose=self.rc.system_verbose)
463 verbose=self.rc.system_verbose)
476 self.getoutputerror = lambda cmd: \
464 self.getoutputerror = lambda cmd: \
477 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
465 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
478 self.user_ns)),
466 self.user_ns)),
479 header='IPython system call: ',
467 header='IPython system call: ',
480 verbose=self.rc.system_verbose)
468 verbose=self.rc.system_verbose)
481
469
482 # RegExp for splitting line contents into pre-char//first
470 # RegExp for splitting line contents into pre-char//first
483 # word-method//rest. For clarity, each group in on one line.
471 # word-method//rest. For clarity, each group in on one line.
484
472
485 # WARNING: update the regexp if the above escapes are changed, as they
473 # WARNING: update the regexp if the above escapes are changed, as they
486 # are hardwired in.
474 # are hardwired in.
487
475
488 # Don't get carried away with trying to make the autocalling catch too
476 # Don't get carried away with trying to make the autocalling catch too
489 # much: it's better to be conservative rather than to trigger hidden
477 # much: it's better to be conservative rather than to trigger hidden
490 # evals() somewhere and end up causing side effects.
478 # evals() somewhere and end up causing side effects.
491
479
492 self.line_split = re.compile(r'^([\s*,;/])'
480 self.line_split = re.compile(r'^([\s*,;/])'
493 r'([\?\w\.]+\w*\s*)'
481 r'([\?\w\.]+\w*\s*)'
494 r'(\(?.*$)')
482 r'(\(?.*$)')
495
483
496 # Original re, keep around for a while in case changes break something
484 # Original re, keep around for a while in case changes break something
497 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
485 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
498 # r'(\s*[\?\w\.]+\w*\s*)'
486 # r'(\s*[\?\w\.]+\w*\s*)'
499 # r'(\(?.*$)')
487 # r'(\(?.*$)')
500
488
501 # RegExp to identify potential function names
489 # RegExp to identify potential function names
502 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
490 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
503
491
504 # RegExp to exclude strings with this start from autocalling. In
492 # RegExp to exclude strings with this start from autocalling. In
505 # particular, all binary operators should be excluded, so that if foo
493 # particular, all binary operators should be excluded, so that if foo
506 # is callable, foo OP bar doesn't become foo(OP bar), which is
494 # is callable, foo OP bar doesn't become foo(OP bar), which is
507 # invalid. The characters '!=()' don't need to be checked for, as the
495 # invalid. The characters '!=()' don't need to be checked for, as the
508 # _prefilter routine explicitely does so, to catch direct calls and
496 # _prefilter routine explicitely does so, to catch direct calls and
509 # rebindings of existing names.
497 # rebindings of existing names.
510
498
511 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
499 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
512 # it affects the rest of the group in square brackets.
500 # it affects the rest of the group in square brackets.
513 self.re_exclude_auto = re.compile(r'^[<>,&^\|\*/\+-]'
501 self.re_exclude_auto = re.compile(r'^[<>,&^\|\*/\+-]'
514 '|^is |^not |^in |^and |^or ')
502 '|^is |^not |^in |^and |^or ')
515
503
516 # try to catch also methods for stuff in lists/tuples/dicts: off
504 # try to catch also methods for stuff in lists/tuples/dicts: off
517 # (experimental). For this to work, the line_split regexp would need
505 # (experimental). For this to work, the line_split regexp would need
518 # to be modified so it wouldn't break things at '['. That line is
506 # to be modified so it wouldn't break things at '['. That line is
519 # nasty enough that I shouldn't change it until I can test it _well_.
507 # nasty enough that I shouldn't change it until I can test it _well_.
520 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
508 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
521
509
522 # keep track of where we started running (mainly for crash post-mortem)
510 # keep track of where we started running (mainly for crash post-mortem)
523 self.starting_dir = os.getcwd()
511 self.starting_dir = os.getcwd()
524
512
525 # Various switches which can be set
513 # Various switches which can be set
526 self.CACHELENGTH = 5000 # this is cheap, it's just text
514 self.CACHELENGTH = 5000 # this is cheap, it's just text
527 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
515 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
528 self.banner2 = banner2
516 self.banner2 = banner2
529
517
530 # TraceBack handlers:
518 # TraceBack handlers:
531
519
532 # Syntax error handler.
520 # Syntax error handler.
533 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
521 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
534
522
535 # The interactive one is initialized with an offset, meaning we always
523 # The interactive one is initialized with an offset, meaning we always
536 # want to remove the topmost item in the traceback, which is our own
524 # want to remove the topmost item in the traceback, which is our own
537 # internal code. Valid modes: ['Plain','Context','Verbose']
525 # internal code. Valid modes: ['Plain','Context','Verbose']
538 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
526 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
539 color_scheme='NoColor',
527 color_scheme='NoColor',
540 tb_offset = 1)
528 tb_offset = 1)
541
529
542 # IPython itself shouldn't crash. This will produce a detailed
530 # IPython itself shouldn't crash. This will produce a detailed
543 # post-mortem if it does. But we only install the crash handler for
531 # post-mortem if it does. But we only install the crash handler for
544 # non-threaded shells, the threaded ones use a normal verbose reporter
532 # non-threaded shells, the threaded ones use a normal verbose reporter
545 # and lose the crash handler. This is because exceptions in the main
533 # and lose the crash handler. This is because exceptions in the main
546 # thread (such as in GUI code) propagate directly to sys.excepthook,
534 # thread (such as in GUI code) propagate directly to sys.excepthook,
547 # and there's no point in printing crash dumps for every user exception.
535 # and there's no point in printing crash dumps for every user exception.
548 if self.isthreaded:
536 if self.isthreaded:
549 sys.excepthook = ultraTB.FormattedTB()
537 sys.excepthook = ultraTB.FormattedTB()
550 else:
538 else:
551 from IPython import CrashHandler
539 from IPython import CrashHandler
552 sys.excepthook = CrashHandler.CrashHandler(self)
540 sys.excepthook = CrashHandler.CrashHandler(self)
553
541
554 # The instance will store a pointer to this, so that runtime code
542 # The instance will store a pointer to this, so that runtime code
555 # (such as magics) can access it. This is because during the
543 # (such as magics) can access it. This is because during the
556 # read-eval loop, it gets temporarily overwritten (to deal with GUI
544 # read-eval loop, it gets temporarily overwritten (to deal with GUI
557 # frameworks).
545 # frameworks).
558 self.sys_excepthook = sys.excepthook
546 self.sys_excepthook = sys.excepthook
559
547
560 # and add any custom exception handlers the user may have specified
548 # and add any custom exception handlers the user may have specified
561 self.set_custom_exc(*custom_exceptions)
549 self.set_custom_exc(*custom_exceptions)
562
550
563 # Object inspector
551 # Object inspector
564 self.inspector = OInspect.Inspector(OInspect.InspectColors,
552 self.inspector = OInspect.Inspector(OInspect.InspectColors,
565 PyColorize.ANSICodeColors,
553 PyColorize.ANSICodeColors,
566 'NoColor')
554 'NoColor')
567 # indentation management
555 # indentation management
568 self.autoindent = False
556 self.autoindent = False
569 self.indent_current_nsp = 0
557 self.indent_current_nsp = 0
570
558
571 # Make some aliases automatically
559 # Make some aliases automatically
572 # Prepare list of shell aliases to auto-define
560 # Prepare list of shell aliases to auto-define
573 if os.name == 'posix':
561 if os.name == 'posix':
574 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
562 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
575 'mv mv -i','rm rm -i','cp cp -i',
563 'mv mv -i','rm rm -i','cp cp -i',
576 'cat cat','less less','clear clear',
564 'cat cat','less less','clear clear',
577 # a better ls
565 # a better ls
578 'ls ls -F',
566 'ls ls -F',
579 # long ls
567 # long ls
580 'll ls -lF',
568 'll ls -lF',
581 # color ls
569 # color ls
582 'lc ls -F -o --color',
570 'lc ls -F -o --color',
583 # ls normal files only
571 # ls normal files only
584 'lf ls -F -o --color %l | grep ^-',
572 'lf ls -F -o --color %l | grep ^-',
585 # ls symbolic links
573 # ls symbolic links
586 'lk ls -F -o --color %l | grep ^l',
574 'lk ls -F -o --color %l | grep ^l',
587 # directories or links to directories,
575 # directories or links to directories,
588 'ldir ls -F -o --color %l | grep /$',
576 'ldir ls -F -o --color %l | grep /$',
589 # things which are executable
577 # things which are executable
590 'lx ls -F -o --color %l | grep ^-..x',
578 'lx ls -F -o --color %l | grep ^-..x',
591 )
579 )
592 elif os.name in ['nt','dos']:
580 elif os.name in ['nt','dos']:
593 auto_alias = ('dir dir /on', 'ls dir /on',
581 auto_alias = ('dir dir /on', 'ls dir /on',
594 'ddir dir /ad /on', 'ldir dir /ad /on',
582 'ddir dir /ad /on', 'ldir dir /ad /on',
595 'mkdir mkdir','rmdir rmdir','echo echo',
583 'mkdir mkdir','rmdir rmdir','echo echo',
596 'ren ren','cls cls','copy copy')
584 'ren ren','cls cls','copy copy')
597 else:
585 else:
598 auto_alias = ()
586 auto_alias = ()
599 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
587 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
600 # Call the actual (public) initializer
588 # Call the actual (public) initializer
601 self.init_auto_alias()
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 # end __init__
600 # end __init__
603
601
604 def pre_config_initialization(self):
602 def pre_config_initialization(self):
605 """Pre-configuration init method
603 """Pre-configuration init method
606
604
607 This is called before the configuration files are processed to
605 This is called before the configuration files are processed to
608 prepare the services the config files might need.
606 prepare the services the config files might need.
609
607
610 self.rc already has reasonable default values at this point.
608 self.rc already has reasonable default values at this point.
611 """
609 """
612 rc = self.rc
610 rc = self.rc
613
611
614 self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db")
612 self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db")
615
616
613
617 def post_config_initialization(self):
614 def post_config_initialization(self):
618 """Post configuration init method
615 """Post configuration init method
619
616
620 This is called after the configuration files have been processed to
617 This is called after the configuration files have been processed to
621 'finalize' the initialization."""
618 'finalize' the initialization."""
622
619
623 rc = self.rc
620 rc = self.rc
624
621
625 # Load readline proper
622 # Load readline proper
626 if rc.readline:
623 if rc.readline:
627 self.init_readline()
624 self.init_readline()
628
625
629 # local shortcut, this is used a LOT
626 # local shortcut, this is used a LOT
630 self.log = self.logger.log
627 self.log = self.logger.log
631
628
632 # Initialize cache, set in/out prompts and printing system
629 # Initialize cache, set in/out prompts and printing system
633 self.outputcache = CachedOutput(self,
630 self.outputcache = CachedOutput(self,
634 rc.cache_size,
631 rc.cache_size,
635 rc.pprint,
632 rc.pprint,
636 input_sep = rc.separate_in,
633 input_sep = rc.separate_in,
637 output_sep = rc.separate_out,
634 output_sep = rc.separate_out,
638 output_sep2 = rc.separate_out2,
635 output_sep2 = rc.separate_out2,
639 ps1 = rc.prompt_in1,
636 ps1 = rc.prompt_in1,
640 ps2 = rc.prompt_in2,
637 ps2 = rc.prompt_in2,
641 ps_out = rc.prompt_out,
638 ps_out = rc.prompt_out,
642 pad_left = rc.prompts_pad_left)
639 pad_left = rc.prompts_pad_left)
643
640
644 # user may have over-ridden the default print hook:
641 # user may have over-ridden the default print hook:
645 try:
642 try:
646 self.outputcache.__class__.display = self.hooks.display
643 self.outputcache.__class__.display = self.hooks.display
647 except AttributeError:
644 except AttributeError:
648 pass
645 pass
649
646
650 # I don't like assigning globally to sys, because it means when embedding
647 # I don't like assigning globally to sys, because it means when embedding
651 # instances, each embedded instance overrides the previous choice. But
648 # instances, each embedded instance overrides the previous choice. But
652 # sys.displayhook seems to be called internally by exec, so I don't see a
649 # sys.displayhook seems to be called internally by exec, so I don't see a
653 # way around it.
650 # way around it.
654 sys.displayhook = self.outputcache
651 sys.displayhook = self.outputcache
655
652
656 # Set user colors (don't do it in the constructor above so that it
653 # Set user colors (don't do it in the constructor above so that it
657 # doesn't crash if colors option is invalid)
654 # doesn't crash if colors option is invalid)
658 self.magic_colors(rc.colors)
655 self.magic_colors(rc.colors)
659
656
660 # Set calling of pdb on exceptions
657 # Set calling of pdb on exceptions
661 self.call_pdb = rc.pdb
658 self.call_pdb = rc.pdb
662
659
663 # Load user aliases
660 # Load user aliases
664 for alias in rc.alias:
661 for alias in rc.alias:
665 self.magic_alias(alias)
662 self.magic_alias(alias)
666 self.hooks.late_startup_hook()
663 self.hooks.late_startup_hook()
667
664
668 for batchfile in [path(arg) for arg in self.rc.args
665 for batchfile in [path(arg) for arg in self.rc.args
669 if arg.lower().endswith('.ipy')]:
666 if arg.lower().endswith('.ipy')]:
670 if not batchfile.isfile():
667 if not batchfile.isfile():
671 print "No such batch file:", batchfile
668 print "No such batch file:", batchfile
672 continue
669 continue
673 self.api.runlines(batchfile.text())
670 self.api.runlines(batchfile.text())
674
671
675 def add_builtins(self):
672 def add_builtins(self):
676 """Store ipython references into the builtin namespace.
673 """Store ipython references into the builtin namespace.
677
674
678 Some parts of ipython operate via builtins injected here, which hold a
675 Some parts of ipython operate via builtins injected here, which hold a
679 reference to IPython itself."""
676 reference to IPython itself."""
680
677
681 # TODO: deprecate all except _ip; 'jobs' should be installed
678 # TODO: deprecate all except _ip; 'jobs' should be installed
682 # by an extension and the rest are under _ip, ipalias is redundant
679 # by an extension and the rest are under _ip, ipalias is redundant
683 builtins_new = dict(__IPYTHON__ = self,
680 builtins_new = dict(__IPYTHON__ = self,
684 ip_set_hook = self.set_hook,
681 ip_set_hook = self.set_hook,
685 jobs = self.jobs,
682 jobs = self.jobs,
686 ipmagic = self.ipmagic,
683 ipmagic = self.ipmagic,
687 ipalias = self.ipalias,
684 ipalias = self.ipalias,
688 ipsystem = self.ipsystem,
685 ipsystem = self.ipsystem,
689 _ip = self.api
686 _ip = self.api
690 )
687 )
691 for biname,bival in builtins_new.items():
688 for biname,bival in builtins_new.items():
692 try:
689 try:
693 # store the orignal value so we can restore it
690 # store the orignal value so we can restore it
694 self.builtins_added[biname] = __builtin__.__dict__[biname]
691 self.builtins_added[biname] = __builtin__.__dict__[biname]
695 except KeyError:
692 except KeyError:
696 # or mark that it wasn't defined, and we'll just delete it at
693 # or mark that it wasn't defined, and we'll just delete it at
697 # cleanup
694 # cleanup
698 self.builtins_added[biname] = Undefined
695 self.builtins_added[biname] = Undefined
699 __builtin__.__dict__[biname] = bival
696 __builtin__.__dict__[biname] = bival
700
697
701 # Keep in the builtins a flag for when IPython is active. We set it
698 # Keep in the builtins a flag for when IPython is active. We set it
702 # with setdefault so that multiple nested IPythons don't clobber one
699 # with setdefault so that multiple nested IPythons don't clobber one
703 # another. Each will increase its value by one upon being activated,
700 # another. Each will increase its value by one upon being activated,
704 # which also gives us a way to determine the nesting level.
701 # which also gives us a way to determine the nesting level.
705 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
702 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
706
703
707 def clean_builtins(self):
704 def clean_builtins(self):
708 """Remove any builtins which might have been added by add_builtins, or
705 """Remove any builtins which might have been added by add_builtins, or
709 restore overwritten ones to their previous values."""
706 restore overwritten ones to their previous values."""
710 for biname,bival in self.builtins_added.items():
707 for biname,bival in self.builtins_added.items():
711 if bival is Undefined:
708 if bival is Undefined:
712 del __builtin__.__dict__[biname]
709 del __builtin__.__dict__[biname]
713 else:
710 else:
714 __builtin__.__dict__[biname] = bival
711 __builtin__.__dict__[biname] = bival
715 self.builtins_added.clear()
712 self.builtins_added.clear()
716
713
717 def set_hook(self,name,hook, priority = 50):
714 def set_hook(self,name,hook, priority = 50):
718 """set_hook(name,hook) -> sets an internal IPython hook.
715 """set_hook(name,hook) -> sets an internal IPython hook.
719
716
720 IPython exposes some of its internal API as user-modifiable hooks. By
717 IPython exposes some of its internal API as user-modifiable hooks. By
721 adding your function to one of these hooks, you can modify IPython's
718 adding your function to one of these hooks, you can modify IPython's
722 behavior to call at runtime your own routines."""
719 behavior to call at runtime your own routines."""
723
720
724 # At some point in the future, this should validate the hook before it
721 # At some point in the future, this should validate the hook before it
725 # accepts it. Probably at least check that the hook takes the number
722 # accepts it. Probably at least check that the hook takes the number
726 # of args it's supposed to.
723 # of args it's supposed to.
727 dp = getattr(self.hooks, name, None)
724 dp = getattr(self.hooks, name, None)
728 if name not in IPython.hooks.__all__:
725 if name not in IPython.hooks.__all__:
729 print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ )
726 print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ )
730 if not dp:
727 if not dp:
731 dp = IPython.hooks.CommandChainDispatcher()
728 dp = IPython.hooks.CommandChainDispatcher()
732
729
733 f = new.instancemethod(hook,self,self.__class__)
730 f = new.instancemethod(hook,self,self.__class__)
734 try:
731 try:
735 dp.add(f,priority)
732 dp.add(f,priority)
736 except AttributeError:
733 except AttributeError:
737 # it was not commandchain, plain old func - replace
734 # it was not commandchain, plain old func - replace
738 dp = f
735 dp = f
739
736
740 setattr(self.hooks,name, dp)
737 setattr(self.hooks,name, dp)
741
738
742
739
743 #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
740 #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
744
741
745 def set_custom_exc(self,exc_tuple,handler):
742 def set_custom_exc(self,exc_tuple,handler):
746 """set_custom_exc(exc_tuple,handler)
743 """set_custom_exc(exc_tuple,handler)
747
744
748 Set a custom exception handler, which will be called if any of the
745 Set a custom exception handler, which will be called if any of the
749 exceptions in exc_tuple occur in the mainloop (specifically, in the
746 exceptions in exc_tuple occur in the mainloop (specifically, in the
750 runcode() method.
747 runcode() method.
751
748
752 Inputs:
749 Inputs:
753
750
754 - exc_tuple: a *tuple* of valid exceptions to call the defined
751 - exc_tuple: a *tuple* of valid exceptions to call the defined
755 handler for. It is very important that you use a tuple, and NOT A
752 handler for. It is very important that you use a tuple, and NOT A
756 LIST here, because of the way Python's except statement works. If
753 LIST here, because of the way Python's except statement works. If
757 you only want to trap a single exception, use a singleton tuple:
754 you only want to trap a single exception, use a singleton tuple:
758
755
759 exc_tuple == (MyCustomException,)
756 exc_tuple == (MyCustomException,)
760
757
761 - handler: this must be defined as a function with the following
758 - handler: this must be defined as a function with the following
762 basic interface: def my_handler(self,etype,value,tb).
759 basic interface: def my_handler(self,etype,value,tb).
763
760
764 This will be made into an instance method (via new.instancemethod)
761 This will be made into an instance method (via new.instancemethod)
765 of IPython itself, and it will be called if any of the exceptions
762 of IPython itself, and it will be called if any of the exceptions
766 listed in the exc_tuple are caught. If the handler is None, an
763 listed in the exc_tuple are caught. If the handler is None, an
767 internal basic one is used, which just prints basic info.
764 internal basic one is used, which just prints basic info.
768
765
769 WARNING: by putting in your own exception handler into IPython's main
766 WARNING: by putting in your own exception handler into IPython's main
770 execution loop, you run a very good chance of nasty crashes. This
767 execution loop, you run a very good chance of nasty crashes. This
771 facility should only be used if you really know what you are doing."""
768 facility should only be used if you really know what you are doing."""
772
769
773 assert type(exc_tuple)==type(()) , \
770 assert type(exc_tuple)==type(()) , \
774 "The custom exceptions must be given AS A TUPLE."
771 "The custom exceptions must be given AS A TUPLE."
775
772
776 def dummy_handler(self,etype,value,tb):
773 def dummy_handler(self,etype,value,tb):
777 print '*** Simple custom exception handler ***'
774 print '*** Simple custom exception handler ***'
778 print 'Exception type :',etype
775 print 'Exception type :',etype
779 print 'Exception value:',value
776 print 'Exception value:',value
780 print 'Traceback :',tb
777 print 'Traceback :',tb
781 print 'Source code :','\n'.join(self.buffer)
778 print 'Source code :','\n'.join(self.buffer)
782
779
783 if handler is None: handler = dummy_handler
780 if handler is None: handler = dummy_handler
784
781
785 self.CustomTB = new.instancemethod(handler,self,self.__class__)
782 self.CustomTB = new.instancemethod(handler,self,self.__class__)
786 self.custom_exceptions = exc_tuple
783 self.custom_exceptions = exc_tuple
787
784
788 def set_custom_completer(self,completer,pos=0):
785 def set_custom_completer(self,completer,pos=0):
789 """set_custom_completer(completer,pos=0)
786 """set_custom_completer(completer,pos=0)
790
787
791 Adds a new custom completer function.
788 Adds a new custom completer function.
792
789
793 The position argument (defaults to 0) is the index in the completers
790 The position argument (defaults to 0) is the index in the completers
794 list where you want the completer to be inserted."""
791 list where you want the completer to be inserted."""
795
792
796 newcomp = new.instancemethod(completer,self.Completer,
793 newcomp = new.instancemethod(completer,self.Completer,
797 self.Completer.__class__)
794 self.Completer.__class__)
798 self.Completer.matchers.insert(pos,newcomp)
795 self.Completer.matchers.insert(pos,newcomp)
799
796
800 def _get_call_pdb(self):
797 def _get_call_pdb(self):
801 return self._call_pdb
798 return self._call_pdb
802
799
803 def _set_call_pdb(self,val):
800 def _set_call_pdb(self,val):
804
801
805 if val not in (0,1,False,True):
802 if val not in (0,1,False,True):
806 raise ValueError,'new call_pdb value must be boolean'
803 raise ValueError,'new call_pdb value must be boolean'
807
804
808 # store value in instance
805 # store value in instance
809 self._call_pdb = val
806 self._call_pdb = val
810
807
811 # notify the actual exception handlers
808 # notify the actual exception handlers
812 self.InteractiveTB.call_pdb = val
809 self.InteractiveTB.call_pdb = val
813 if self.isthreaded:
810 if self.isthreaded:
814 try:
811 try:
815 self.sys_excepthook.call_pdb = val
812 self.sys_excepthook.call_pdb = val
816 except:
813 except:
817 warn('Failed to activate pdb for threaded exception handler')
814 warn('Failed to activate pdb for threaded exception handler')
818
815
819 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
816 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
820 'Control auto-activation of pdb at exceptions')
817 'Control auto-activation of pdb at exceptions')
821
818
822
819
823 # These special functions get installed in the builtin namespace, to
820 # These special functions get installed in the builtin namespace, to
824 # provide programmatic (pure python) access to magics, aliases and system
821 # provide programmatic (pure python) access to magics, aliases and system
825 # calls. This is important for logging, user scripting, and more.
822 # calls. This is important for logging, user scripting, and more.
826
823
827 # We are basically exposing, via normal python functions, the three
824 # We are basically exposing, via normal python functions, the three
828 # mechanisms in which ipython offers special call modes (magics for
825 # mechanisms in which ipython offers special call modes (magics for
829 # internal control, aliases for direct system access via pre-selected
826 # internal control, aliases for direct system access via pre-selected
830 # names, and !cmd for calling arbitrary system commands).
827 # names, and !cmd for calling arbitrary system commands).
831
828
832 def ipmagic(self,arg_s):
829 def ipmagic(self,arg_s):
833 """Call a magic function by name.
830 """Call a magic function by name.
834
831
835 Input: a string containing the name of the magic function to call and any
832 Input: a string containing the name of the magic function to call and any
836 additional arguments to be passed to the magic.
833 additional arguments to be passed to the magic.
837
834
838 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
835 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
839 prompt:
836 prompt:
840
837
841 In[1]: %name -opt foo bar
838 In[1]: %name -opt foo bar
842
839
843 To call a magic without arguments, simply use ipmagic('name').
840 To call a magic without arguments, simply use ipmagic('name').
844
841
845 This provides a proper Python function to call IPython's magics in any
842 This provides a proper Python function to call IPython's magics in any
846 valid Python code you can type at the interpreter, including loops and
843 valid Python code you can type at the interpreter, including loops and
847 compound statements. It is added by IPython to the Python builtin
844 compound statements. It is added by IPython to the Python builtin
848 namespace upon initialization."""
845 namespace upon initialization."""
849
846
850 args = arg_s.split(' ',1)
847 args = arg_s.split(' ',1)
851 magic_name = args[0]
848 magic_name = args[0]
852 magic_name = magic_name.lstrip(self.ESC_MAGIC)
849 magic_name = magic_name.lstrip(self.ESC_MAGIC)
853
850
854 try:
851 try:
855 magic_args = args[1]
852 magic_args = args[1]
856 except IndexError:
853 except IndexError:
857 magic_args = ''
854 magic_args = ''
858 fn = getattr(self,'magic_'+magic_name,None)
855 fn = getattr(self,'magic_'+magic_name,None)
859 if fn is None:
856 if fn is None:
860 error("Magic function `%s` not found." % magic_name)
857 error("Magic function `%s` not found." % magic_name)
861 else:
858 else:
862 magic_args = self.var_expand(magic_args)
859 magic_args = self.var_expand(magic_args)
863 return fn(magic_args)
860 return fn(magic_args)
864
861
865 def ipalias(self,arg_s):
862 def ipalias(self,arg_s):
866 """Call an alias by name.
863 """Call an alias by name.
867
864
868 Input: a string containing the name of the alias to call and any
865 Input: a string containing the name of the alias to call and any
869 additional arguments to be passed to the magic.
866 additional arguments to be passed to the magic.
870
867
871 ipalias('name -opt foo bar') is equivalent to typing at the ipython
868 ipalias('name -opt foo bar') is equivalent to typing at the ipython
872 prompt:
869 prompt:
873
870
874 In[1]: name -opt foo bar
871 In[1]: name -opt foo bar
875
872
876 To call an alias without arguments, simply use ipalias('name').
873 To call an alias without arguments, simply use ipalias('name').
877
874
878 This provides a proper Python function to call IPython's aliases in any
875 This provides a proper Python function to call IPython's aliases in any
879 valid Python code you can type at the interpreter, including loops and
876 valid Python code you can type at the interpreter, including loops and
880 compound statements. It is added by IPython to the Python builtin
877 compound statements. It is added by IPython to the Python builtin
881 namespace upon initialization."""
878 namespace upon initialization."""
882
879
883 args = arg_s.split(' ',1)
880 args = arg_s.split(' ',1)
884 alias_name = args[0]
881 alias_name = args[0]
885 try:
882 try:
886 alias_args = args[1]
883 alias_args = args[1]
887 except IndexError:
884 except IndexError:
888 alias_args = ''
885 alias_args = ''
889 if alias_name in self.alias_table:
886 if alias_name in self.alias_table:
890 self.call_alias(alias_name,alias_args)
887 self.call_alias(alias_name,alias_args)
891 else:
888 else:
892 error("Alias `%s` not found." % alias_name)
889 error("Alias `%s` not found." % alias_name)
893
890
894 def ipsystem(self,arg_s):
891 def ipsystem(self,arg_s):
895 """Make a system call, using IPython."""
892 """Make a system call, using IPython."""
896
893
897 self.system(arg_s)
894 self.system(arg_s)
898
895
899 def complete(self,text):
896 def complete(self,text):
900 """Return a sorted list of all possible completions on text.
897 """Return a sorted list of all possible completions on text.
901
898
902 Inputs:
899 Inputs:
903
900
904 - text: a string of text to be completed on.
901 - text: a string of text to be completed on.
905
902
906 This is a wrapper around the completion mechanism, similar to what
903 This is a wrapper around the completion mechanism, similar to what
907 readline does at the command line when the TAB key is hit. By
904 readline does at the command line when the TAB key is hit. By
908 exposing it as a method, it can be used by other non-readline
905 exposing it as a method, it can be used by other non-readline
909 environments (such as GUIs) for text completion.
906 environments (such as GUIs) for text completion.
910
907
911 Simple usage example:
908 Simple usage example:
912
909
913 In [1]: x = 'hello'
910 In [1]: x = 'hello'
914
911
915 In [2]: __IP.complete('x.l')
912 In [2]: __IP.complete('x.l')
916 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
913 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
917
914
918 complete = self.Completer.complete
915 complete = self.Completer.complete
919 state = 0
916 state = 0
920 # use a dict so we get unique keys, since ipyhton's multiple
917 # use a dict so we get unique keys, since ipyhton's multiple
921 # completers can return duplicates.
918 # completers can return duplicates.
922 comps = {}
919 comps = {}
923 while True:
920 while True:
924 newcomp = complete(text,state)
921 newcomp = complete(text,state)
925 if newcomp is None:
922 if newcomp is None:
926 break
923 break
927 comps[newcomp] = 1
924 comps[newcomp] = 1
928 state += 1
925 state += 1
929 outcomps = comps.keys()
926 outcomps = comps.keys()
930 outcomps.sort()
927 outcomps.sort()
931 return outcomps
928 return outcomps
932
929
933 def set_completer_frame(self, frame=None):
930 def set_completer_frame(self, frame=None):
934 if frame:
931 if frame:
935 self.Completer.namespace = frame.f_locals
932 self.Completer.namespace = frame.f_locals
936 self.Completer.global_namespace = frame.f_globals
933 self.Completer.global_namespace = frame.f_globals
937 else:
934 else:
938 self.Completer.namespace = self.user_ns
935 self.Completer.namespace = self.user_ns
939 self.Completer.global_namespace = self.user_global_ns
936 self.Completer.global_namespace = self.user_global_ns
940
937
941 def init_auto_alias(self):
938 def init_auto_alias(self):
942 """Define some aliases automatically.
939 """Define some aliases automatically.
943
940
944 These are ALL parameter-less aliases"""
941 These are ALL parameter-less aliases"""
945
942
946 for alias,cmd in self.auto_alias:
943 for alias,cmd in self.auto_alias:
947 self.alias_table[alias] = (0,cmd)
944 self.alias_table[alias] = (0,cmd)
948
945
949 def alias_table_validate(self,verbose=0):
946 def alias_table_validate(self,verbose=0):
950 """Update information about the alias table.
947 """Update information about the alias table.
951
948
952 In particular, make sure no Python keywords/builtins are in it."""
949 In particular, make sure no Python keywords/builtins are in it."""
953
950
954 no_alias = self.no_alias
951 no_alias = self.no_alias
955 for k in self.alias_table.keys():
952 for k in self.alias_table.keys():
956 if k in no_alias:
953 if k in no_alias:
957 del self.alias_table[k]
954 del self.alias_table[k]
958 if verbose:
955 if verbose:
959 print ("Deleting alias <%s>, it's a Python "
956 print ("Deleting alias <%s>, it's a Python "
960 "keyword or builtin." % k)
957 "keyword or builtin." % k)
961
958
962 def set_autoindent(self,value=None):
959 def set_autoindent(self,value=None):
963 """Set the autoindent flag, checking for readline support.
960 """Set the autoindent flag, checking for readline support.
964
961
965 If called with no arguments, it acts as a toggle."""
962 If called with no arguments, it acts as a toggle."""
966
963
967 if not self.has_readline:
964 if not self.has_readline:
968 if os.name == 'posix':
965 if os.name == 'posix':
969 warn("The auto-indent feature requires the readline library")
966 warn("The auto-indent feature requires the readline library")
970 self.autoindent = 0
967 self.autoindent = 0
971 return
968 return
972 if value is None:
969 if value is None:
973 self.autoindent = not self.autoindent
970 self.autoindent = not self.autoindent
974 else:
971 else:
975 self.autoindent = value
972 self.autoindent = value
976
973
977 def rc_set_toggle(self,rc_field,value=None):
974 def rc_set_toggle(self,rc_field,value=None):
978 """Set or toggle a field in IPython's rc config. structure.
975 """Set or toggle a field in IPython's rc config. structure.
979
976
980 If called with no arguments, it acts as a toggle.
977 If called with no arguments, it acts as a toggle.
981
978
982 If called with a non-existent field, the resulting AttributeError
979 If called with a non-existent field, the resulting AttributeError
983 exception will propagate out."""
980 exception will propagate out."""
984
981
985 rc_val = getattr(self.rc,rc_field)
982 rc_val = getattr(self.rc,rc_field)
986 if value is None:
983 if value is None:
987 value = not rc_val
984 value = not rc_val
988 setattr(self.rc,rc_field,value)
985 setattr(self.rc,rc_field,value)
989
986
990 def user_setup(self,ipythondir,rc_suffix,mode='install'):
987 def user_setup(self,ipythondir,rc_suffix,mode='install'):
991 """Install the user configuration directory.
988 """Install the user configuration directory.
992
989
993 Can be called when running for the first time or to upgrade the user's
990 Can be called when running for the first time or to upgrade the user's
994 .ipython/ directory with the mode parameter. Valid modes are 'install'
991 .ipython/ directory with the mode parameter. Valid modes are 'install'
995 and 'upgrade'."""
992 and 'upgrade'."""
996
993
997 def wait():
994 def wait():
998 try:
995 try:
999 raw_input("Please press <RETURN> to start IPython.")
996 raw_input("Please press <RETURN> to start IPython.")
1000 except EOFError:
997 except EOFError:
1001 print >> Term.cout
998 print >> Term.cout
1002 print '*'*70
999 print '*'*70
1003
1000
1004 cwd = os.getcwd() # remember where we started
1001 cwd = os.getcwd() # remember where we started
1005 glb = glob.glob
1002 glb = glob.glob
1006 print '*'*70
1003 print '*'*70
1007 if mode == 'install':
1004 if mode == 'install':
1008 print \
1005 print \
1009 """Welcome to IPython. I will try to create a personal configuration directory
1006 """Welcome to IPython. I will try to create a personal configuration directory
1010 where you can customize many aspects of IPython's functionality in:\n"""
1007 where you can customize many aspects of IPython's functionality in:\n"""
1011 else:
1008 else:
1012 print 'I am going to upgrade your configuration in:'
1009 print 'I am going to upgrade your configuration in:'
1013
1010
1014 print ipythondir
1011 print ipythondir
1015
1012
1016 rcdirend = os.path.join('IPython','UserConfig')
1013 rcdirend = os.path.join('IPython','UserConfig')
1017 cfg = lambda d: os.path.join(d,rcdirend)
1014 cfg = lambda d: os.path.join(d,rcdirend)
1018 try:
1015 try:
1019 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
1016 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
1020 except IOError:
1017 except IOError:
1021 warning = """
1018 warning = """
1022 Installation error. IPython's directory was not found.
1019 Installation error. IPython's directory was not found.
1023
1020
1024 Check the following:
1021 Check the following:
1025
1022
1026 The ipython/IPython directory should be in a directory belonging to your
1023 The ipython/IPython directory should be in a directory belonging to your
1027 PYTHONPATH environment variable (that is, it should be in a directory
1024 PYTHONPATH environment variable (that is, it should be in a directory
1028 belonging to sys.path). You can copy it explicitly there or just link to it.
1025 belonging to sys.path). You can copy it explicitly there or just link to it.
1029
1026
1030 IPython will proceed with builtin defaults.
1027 IPython will proceed with builtin defaults.
1031 """
1028 """
1032 warn(warning)
1029 warn(warning)
1033 wait()
1030 wait()
1034 return
1031 return
1035
1032
1036 if mode == 'install':
1033 if mode == 'install':
1037 try:
1034 try:
1038 shutil.copytree(rcdir,ipythondir)
1035 shutil.copytree(rcdir,ipythondir)
1039 os.chdir(ipythondir)
1036 os.chdir(ipythondir)
1040 rc_files = glb("ipythonrc*")
1037 rc_files = glb("ipythonrc*")
1041 for rc_file in rc_files:
1038 for rc_file in rc_files:
1042 os.rename(rc_file,rc_file+rc_suffix)
1039 os.rename(rc_file,rc_file+rc_suffix)
1043 except:
1040 except:
1044 warning = """
1041 warning = """
1045
1042
1046 There was a problem with the installation:
1043 There was a problem with the installation:
1047 %s
1044 %s
1048 Try to correct it or contact the developers if you think it's a bug.
1045 Try to correct it or contact the developers if you think it's a bug.
1049 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1046 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1050 warn(warning)
1047 warn(warning)
1051 wait()
1048 wait()
1052 return
1049 return
1053
1050
1054 elif mode == 'upgrade':
1051 elif mode == 'upgrade':
1055 try:
1052 try:
1056 os.chdir(ipythondir)
1053 os.chdir(ipythondir)
1057 except:
1054 except:
1058 print """
1055 print """
1059 Can not upgrade: changing to directory %s failed. Details:
1056 Can not upgrade: changing to directory %s failed. Details:
1060 %s
1057 %s
1061 """ % (ipythondir,sys.exc_info()[1])
1058 """ % (ipythondir,sys.exc_info()[1])
1062 wait()
1059 wait()
1063 return
1060 return
1064 else:
1061 else:
1065 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1062 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1066 for new_full_path in sources:
1063 for new_full_path in sources:
1067 new_filename = os.path.basename(new_full_path)
1064 new_filename = os.path.basename(new_full_path)
1068 if new_filename.startswith('ipythonrc'):
1065 if new_filename.startswith('ipythonrc'):
1069 new_filename = new_filename + rc_suffix
1066 new_filename = new_filename + rc_suffix
1070 # The config directory should only contain files, skip any
1067 # The config directory should only contain files, skip any
1071 # directories which may be there (like CVS)
1068 # directories which may be there (like CVS)
1072 if os.path.isdir(new_full_path):
1069 if os.path.isdir(new_full_path):
1073 continue
1070 continue
1074 if os.path.exists(new_filename):
1071 if os.path.exists(new_filename):
1075 old_file = new_filename+'.old'
1072 old_file = new_filename+'.old'
1076 if os.path.exists(old_file):
1073 if os.path.exists(old_file):
1077 os.remove(old_file)
1074 os.remove(old_file)
1078 os.rename(new_filename,old_file)
1075 os.rename(new_filename,old_file)
1079 shutil.copy(new_full_path,new_filename)
1076 shutil.copy(new_full_path,new_filename)
1080 else:
1077 else:
1081 raise ValueError,'unrecognized mode for install:',`mode`
1078 raise ValueError,'unrecognized mode for install:',`mode`
1082
1079
1083 # Fix line-endings to those native to each platform in the config
1080 # Fix line-endings to those native to each platform in the config
1084 # directory.
1081 # directory.
1085 try:
1082 try:
1086 os.chdir(ipythondir)
1083 os.chdir(ipythondir)
1087 except:
1084 except:
1088 print """
1085 print """
1089 Problem: changing to directory %s failed.
1086 Problem: changing to directory %s failed.
1090 Details:
1087 Details:
1091 %s
1088 %s
1092
1089
1093 Some configuration files may have incorrect line endings. This should not
1090 Some configuration files may have incorrect line endings. This should not
1094 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1091 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1095 wait()
1092 wait()
1096 else:
1093 else:
1097 for fname in glb('ipythonrc*'):
1094 for fname in glb('ipythonrc*'):
1098 try:
1095 try:
1099 native_line_ends(fname,backup=0)
1096 native_line_ends(fname,backup=0)
1100 except IOError:
1097 except IOError:
1101 pass
1098 pass
1102
1099
1103 if mode == 'install':
1100 if mode == 'install':
1104 print """
1101 print """
1105 Successful installation!
1102 Successful installation!
1106
1103
1107 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1104 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1108 IPython manual (there are both HTML and PDF versions supplied with the
1105 IPython manual (there are both HTML and PDF versions supplied with the
1109 distribution) to make sure that your system environment is properly configured
1106 distribution) to make sure that your system environment is properly configured
1110 to take advantage of IPython's features.
1107 to take advantage of IPython's features.
1111
1108
1112 Important note: the configuration system has changed! The old system is
1109 Important note: the configuration system has changed! The old system is
1113 still in place, but its setting may be partly overridden by the settings in
1110 still in place, but its setting may be partly overridden by the settings in
1114 "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file
1111 "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file
1115 if some of the new settings bother you.
1112 if some of the new settings bother you.
1116
1113
1117 """
1114 """
1118 else:
1115 else:
1119 print """
1116 print """
1120 Successful upgrade!
1117 Successful upgrade!
1121
1118
1122 All files in your directory:
1119 All files in your directory:
1123 %(ipythondir)s
1120 %(ipythondir)s
1124 which would have been overwritten by the upgrade were backed up with a .old
1121 which would have been overwritten by the upgrade were backed up with a .old
1125 extension. If you had made particular customizations in those files you may
1122 extension. If you had made particular customizations in those files you may
1126 want to merge them back into the new files.""" % locals()
1123 want to merge them back into the new files.""" % locals()
1127 wait()
1124 wait()
1128 os.chdir(cwd)
1125 os.chdir(cwd)
1129 # end user_setup()
1126 # end user_setup()
1130
1127
1131 def atexit_operations(self):
1128 def atexit_operations(self):
1132 """This will be executed at the time of exit.
1129 """This will be executed at the time of exit.
1133
1130
1134 Saving of persistent data should be performed here. """
1131 Saving of persistent data should be performed here. """
1135
1132
1136 #print '*** IPython exit cleanup ***' # dbg
1133 #print '*** IPython exit cleanup ***' # dbg
1137 # input history
1134 # input history
1138 self.savehist()
1135 self.savehist()
1139
1136
1140 # Cleanup all tempfiles left around
1137 # Cleanup all tempfiles left around
1141 for tfile in self.tempfiles:
1138 for tfile in self.tempfiles:
1142 try:
1139 try:
1143 os.unlink(tfile)
1140 os.unlink(tfile)
1144 except OSError:
1141 except OSError:
1145 pass
1142 pass
1146
1143
1147 # save the "persistent data" catch-all dictionary
1144 # save the "persistent data" catch-all dictionary
1148 self.hooks.shutdown_hook()
1145 self.hooks.shutdown_hook()
1149
1146
1150 def savehist(self):
1147 def savehist(self):
1151 """Save input history to a file (via readline library)."""
1148 """Save input history to a file (via readline library)."""
1152 try:
1149 try:
1153 self.readline.write_history_file(self.histfile)
1150 self.readline.write_history_file(self.histfile)
1154 except:
1151 except:
1155 print 'Unable to save IPython command history to file: ' + \
1152 print 'Unable to save IPython command history to file: ' + \
1156 `self.histfile`
1153 `self.histfile`
1157
1154
1158 def pre_readline(self):
1155 def pre_readline(self):
1159 """readline hook to be used at the start of each line.
1156 """readline hook to be used at the start of each line.
1160
1157
1161 Currently it handles auto-indent only."""
1158 Currently it handles auto-indent only."""
1162
1159
1163 #debugx('self.indent_current_nsp','pre_readline:')
1160 #debugx('self.indent_current_nsp','pre_readline:')
1164 self.readline.insert_text(self.indent_current_str())
1161 self.readline.insert_text(self.indent_current_str())
1165
1162
1166 def init_readline(self):
1163 def init_readline(self):
1167 """Command history completion/saving/reloading."""
1164 """Command history completion/saving/reloading."""
1168
1165
1169 import IPython.rlineimpl as readline
1166 import IPython.rlineimpl as readline
1170 if not readline.have_readline:
1167 if not readline.have_readline:
1171 self.has_readline = 0
1168 self.has_readline = 0
1172 self.readline = None
1169 self.readline = None
1173 # no point in bugging windows users with this every time:
1170 # no point in bugging windows users with this every time:
1174 warn('Readline services not available on this platform.')
1171 warn('Readline services not available on this platform.')
1175 else:
1172 else:
1176 sys.modules['readline'] = readline
1173 sys.modules['readline'] = readline
1177 import atexit
1174 import atexit
1178 from IPython.completer import IPCompleter
1175 from IPython.completer import IPCompleter
1179 self.Completer = IPCompleter(self,
1176 self.Completer = IPCompleter(self,
1180 self.user_ns,
1177 self.user_ns,
1181 self.user_global_ns,
1178 self.user_global_ns,
1182 self.rc.readline_omit__names,
1179 self.rc.readline_omit__names,
1183 self.alias_table)
1180 self.alias_table)
1184
1181
1185 # Platform-specific configuration
1182 # Platform-specific configuration
1186 if os.name == 'nt':
1183 if os.name == 'nt':
1187 self.readline_startup_hook = readline.set_pre_input_hook
1184 self.readline_startup_hook = readline.set_pre_input_hook
1188 else:
1185 else:
1189 self.readline_startup_hook = readline.set_startup_hook
1186 self.readline_startup_hook = readline.set_startup_hook
1190
1187
1191 # Load user's initrc file (readline config)
1188 # Load user's initrc file (readline config)
1192 inputrc_name = os.environ.get('INPUTRC')
1189 inputrc_name = os.environ.get('INPUTRC')
1193 if inputrc_name is None:
1190 if inputrc_name is None:
1194 home_dir = get_home_dir()
1191 home_dir = get_home_dir()
1195 if home_dir is not None:
1192 if home_dir is not None:
1196 inputrc_name = os.path.join(home_dir,'.inputrc')
1193 inputrc_name = os.path.join(home_dir,'.inputrc')
1197 if os.path.isfile(inputrc_name):
1194 if os.path.isfile(inputrc_name):
1198 try:
1195 try:
1199 readline.read_init_file(inputrc_name)
1196 readline.read_init_file(inputrc_name)
1200 except:
1197 except:
1201 warn('Problems reading readline initialization file <%s>'
1198 warn('Problems reading readline initialization file <%s>'
1202 % inputrc_name)
1199 % inputrc_name)
1203
1200
1204 self.has_readline = 1
1201 self.has_readline = 1
1205 self.readline = readline
1202 self.readline = readline
1206 # save this in sys so embedded copies can restore it properly
1203 # save this in sys so embedded copies can restore it properly
1207 sys.ipcompleter = self.Completer.complete
1204 sys.ipcompleter = self.Completer.complete
1208 readline.set_completer(self.Completer.complete)
1205 readline.set_completer(self.Completer.complete)
1209
1206
1210 # Configure readline according to user's prefs
1207 # Configure readline according to user's prefs
1211 for rlcommand in self.rc.readline_parse_and_bind:
1208 for rlcommand in self.rc.readline_parse_and_bind:
1212 readline.parse_and_bind(rlcommand)
1209 readline.parse_and_bind(rlcommand)
1213
1210
1214 # remove some chars from the delimiters list
1211 # remove some chars from the delimiters list
1215 delims = readline.get_completer_delims()
1212 delims = readline.get_completer_delims()
1216 delims = delims.translate(string._idmap,
1213 delims = delims.translate(string._idmap,
1217 self.rc.readline_remove_delims)
1214 self.rc.readline_remove_delims)
1218 readline.set_completer_delims(delims)
1215 readline.set_completer_delims(delims)
1219 # otherwise we end up with a monster history after a while:
1216 # otherwise we end up with a monster history after a while:
1220 readline.set_history_length(1000)
1217 readline.set_history_length(1000)
1221 try:
1218 try:
1222 #print '*** Reading readline history' # dbg
1219 #print '*** Reading readline history' # dbg
1223 readline.read_history_file(self.histfile)
1220 readline.read_history_file(self.histfile)
1224 except IOError:
1221 except IOError:
1225 pass # It doesn't exist yet.
1222 pass # It doesn't exist yet.
1226
1223
1227 atexit.register(self.atexit_operations)
1224 atexit.register(self.atexit_operations)
1228 del atexit
1225 del atexit
1229
1226
1230 # Configure auto-indent for all platforms
1227 # Configure auto-indent for all platforms
1231 self.set_autoindent(self.rc.autoindent)
1228 self.set_autoindent(self.rc.autoindent)
1232
1229
1233 def _should_recompile(self,e):
1230 def _should_recompile(self,e):
1234 """Utility routine for edit_syntax_error"""
1231 """Utility routine for edit_syntax_error"""
1235
1232
1236 if e.filename in ('<ipython console>','<input>','<string>',
1233 if e.filename in ('<ipython console>','<input>','<string>',
1237 '<console>',None):
1234 '<console>',None):
1238
1235
1239 return False
1236 return False
1240 try:
1237 try:
1241 if (self.rc.autoedit_syntax and
1238 if (self.rc.autoedit_syntax and
1242 not ask_yes_no('Return to editor to correct syntax error? '
1239 not ask_yes_no('Return to editor to correct syntax error? '
1243 '[Y/n] ','y')):
1240 '[Y/n] ','y')):
1244 return False
1241 return False
1245 except EOFError:
1242 except EOFError:
1246 return False
1243 return False
1247
1244
1248 def int0(x):
1245 def int0(x):
1249 try:
1246 try:
1250 return int(x)
1247 return int(x)
1251 except TypeError:
1248 except TypeError:
1252 return 0
1249 return 0
1253 # always pass integer line and offset values to editor hook
1250 # always pass integer line and offset values to editor hook
1254 self.hooks.fix_error_editor(e.filename,
1251 self.hooks.fix_error_editor(e.filename,
1255 int0(e.lineno),int0(e.offset),e.msg)
1252 int0(e.lineno),int0(e.offset),e.msg)
1256 return True
1253 return True
1257
1254
1258 def edit_syntax_error(self):
1255 def edit_syntax_error(self):
1259 """The bottom half of the syntax error handler called in the main loop.
1256 """The bottom half of the syntax error handler called in the main loop.
1260
1257
1261 Loop until syntax error is fixed or user cancels.
1258 Loop until syntax error is fixed or user cancels.
1262 """
1259 """
1263
1260
1264 while self.SyntaxTB.last_syntax_error:
1261 while self.SyntaxTB.last_syntax_error:
1265 # copy and clear last_syntax_error
1262 # copy and clear last_syntax_error
1266 err = self.SyntaxTB.clear_err_state()
1263 err = self.SyntaxTB.clear_err_state()
1267 if not self._should_recompile(err):
1264 if not self._should_recompile(err):
1268 return
1265 return
1269 try:
1266 try:
1270 # may set last_syntax_error again if a SyntaxError is raised
1267 # may set last_syntax_error again if a SyntaxError is raised
1271 self.safe_execfile(err.filename,self.shell.user_ns)
1268 self.safe_execfile(err.filename,self.shell.user_ns)
1272 except:
1269 except:
1273 self.showtraceback()
1270 self.showtraceback()
1274 else:
1271 else:
1275 f = file(err.filename)
1272 f = file(err.filename)
1276 try:
1273 try:
1277 sys.displayhook(f.read())
1274 sys.displayhook(f.read())
1278 finally:
1275 finally:
1279 f.close()
1276 f.close()
1280
1277
1281 def showsyntaxerror(self, filename=None):
1278 def showsyntaxerror(self, filename=None):
1282 """Display the syntax error that just occurred.
1279 """Display the syntax error that just occurred.
1283
1280
1284 This doesn't display a stack trace because there isn't one.
1281 This doesn't display a stack trace because there isn't one.
1285
1282
1286 If a filename is given, it is stuffed in the exception instead
1283 If a filename is given, it is stuffed in the exception instead
1287 of what was there before (because Python's parser always uses
1284 of what was there before (because Python's parser always uses
1288 "<string>" when reading from a string).
1285 "<string>" when reading from a string).
1289 """
1286 """
1290 etype, value, last_traceback = sys.exc_info()
1287 etype, value, last_traceback = sys.exc_info()
1291
1288
1292 # See note about these variables in showtraceback() below
1289 # See note about these variables in showtraceback() below
1293 sys.last_type = etype
1290 sys.last_type = etype
1294 sys.last_value = value
1291 sys.last_value = value
1295 sys.last_traceback = last_traceback
1292 sys.last_traceback = last_traceback
1296
1293
1297 if filename and etype is SyntaxError:
1294 if filename and etype is SyntaxError:
1298 # Work hard to stuff the correct filename in the exception
1295 # Work hard to stuff the correct filename in the exception
1299 try:
1296 try:
1300 msg, (dummy_filename, lineno, offset, line) = value
1297 msg, (dummy_filename, lineno, offset, line) = value
1301 except:
1298 except:
1302 # Not the format we expect; leave it alone
1299 # Not the format we expect; leave it alone
1303 pass
1300 pass
1304 else:
1301 else:
1305 # Stuff in the right filename
1302 # Stuff in the right filename
1306 try:
1303 try:
1307 # Assume SyntaxError is a class exception
1304 # Assume SyntaxError is a class exception
1308 value = SyntaxError(msg, (filename, lineno, offset, line))
1305 value = SyntaxError(msg, (filename, lineno, offset, line))
1309 except:
1306 except:
1310 # If that failed, assume SyntaxError is a string
1307 # If that failed, assume SyntaxError is a string
1311 value = msg, (filename, lineno, offset, line)
1308 value = msg, (filename, lineno, offset, line)
1312 self.SyntaxTB(etype,value,[])
1309 self.SyntaxTB(etype,value,[])
1313
1310
1314 def debugger(self):
1311 def debugger(self):
1315 """Call the pdb debugger."""
1312 """Call the pdb debugger."""
1316
1313
1317 if not self.rc.pdb:
1314 if not self.rc.pdb:
1318 return
1315 return
1319 pdb.pm()
1316 pdb.pm()
1320
1317
1321 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1318 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1322 """Display the exception that just occurred."""
1319 """Display the exception that just occurred."""
1323
1320
1324 # Though this won't be called by syntax errors in the input line,
1321 # Though this won't be called by syntax errors in the input line,
1325 # there may be SyntaxError cases whith imported code.
1322 # there may be SyntaxError cases whith imported code.
1326 if exc_tuple is None:
1323 if exc_tuple is None:
1327 etype, value, tb = sys.exc_info()
1324 etype, value, tb = sys.exc_info()
1328 else:
1325 else:
1329 etype, value, tb = exc_tuple
1326 etype, value, tb = exc_tuple
1330 if etype is SyntaxError:
1327 if etype is SyntaxError:
1331 self.showsyntaxerror(filename)
1328 self.showsyntaxerror(filename)
1332 else:
1329 else:
1333 # WARNING: these variables are somewhat deprecated and not
1330 # WARNING: these variables are somewhat deprecated and not
1334 # necessarily safe to use in a threaded environment, but tools
1331 # necessarily safe to use in a threaded environment, but tools
1335 # like pdb depend on their existence, so let's set them. If we
1332 # like pdb depend on their existence, so let's set them. If we
1336 # find problems in the field, we'll need to revisit their use.
1333 # find problems in the field, we'll need to revisit their use.
1337 sys.last_type = etype
1334 sys.last_type = etype
1338 sys.last_value = value
1335 sys.last_value = value
1339 sys.last_traceback = tb
1336 sys.last_traceback = tb
1340
1337
1341 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1338 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1342 if self.InteractiveTB.call_pdb and self.has_readline:
1339 if self.InteractiveTB.call_pdb and self.has_readline:
1343 # pdb mucks up readline, fix it back
1340 # pdb mucks up readline, fix it back
1344 self.readline.set_completer(self.Completer.complete)
1341 self.readline.set_completer(self.Completer.complete)
1345
1342
1346 def mainloop(self,banner=None):
1343 def mainloop(self,banner=None):
1347 """Creates the local namespace and starts the mainloop.
1344 """Creates the local namespace and starts the mainloop.
1348
1345
1349 If an optional banner argument is given, it will override the
1346 If an optional banner argument is given, it will override the
1350 internally created default banner."""
1347 internally created default banner."""
1351
1348
1352 if self.rc.c: # Emulate Python's -c option
1349 if self.rc.c: # Emulate Python's -c option
1353 self.exec_init_cmd()
1350 self.exec_init_cmd()
1354 if banner is None:
1351 if banner is None:
1355 if not self.rc.banner:
1352 if not self.rc.banner:
1356 banner = ''
1353 banner = ''
1357 # banner is string? Use it directly!
1354 # banner is string? Use it directly!
1358 elif isinstance(self.rc.banner,basestring):
1355 elif isinstance(self.rc.banner,basestring):
1359 banner = self.rc.banner
1356 banner = self.rc.banner
1360 else:
1357 else:
1361 banner = self.BANNER+self.banner2
1358 banner = self.BANNER+self.banner2
1362
1359
1363 self.interact(banner)
1360 self.interact(banner)
1364
1361
1365 def exec_init_cmd(self):
1362 def exec_init_cmd(self):
1366 """Execute a command given at the command line.
1363 """Execute a command given at the command line.
1367
1364
1368 This emulates Python's -c option."""
1365 This emulates Python's -c option."""
1369
1366
1370 #sys.argv = ['-c']
1367 #sys.argv = ['-c']
1371 self.push(self.rc.c)
1368 self.push(self.rc.c)
1372
1369
1373 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1370 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1374 """Embeds IPython into a running python program.
1371 """Embeds IPython into a running python program.
1375
1372
1376 Input:
1373 Input:
1377
1374
1378 - header: An optional header message can be specified.
1375 - header: An optional header message can be specified.
1379
1376
1380 - local_ns, global_ns: working namespaces. If given as None, the
1377 - local_ns, global_ns: working namespaces. If given as None, the
1381 IPython-initialized one is updated with __main__.__dict__, so that
1378 IPython-initialized one is updated with __main__.__dict__, so that
1382 program variables become visible but user-specific configuration
1379 program variables become visible but user-specific configuration
1383 remains possible.
1380 remains possible.
1384
1381
1385 - stack_depth: specifies how many levels in the stack to go to
1382 - stack_depth: specifies how many levels in the stack to go to
1386 looking for namespaces (when local_ns and global_ns are None). This
1383 looking for namespaces (when local_ns and global_ns are None). This
1387 allows an intermediate caller to make sure that this function gets
1384 allows an intermediate caller to make sure that this function gets
1388 the namespace from the intended level in the stack. By default (0)
1385 the namespace from the intended level in the stack. By default (0)
1389 it will get its locals and globals from the immediate caller.
1386 it will get its locals and globals from the immediate caller.
1390
1387
1391 Warning: it's possible to use this in a program which is being run by
1388 Warning: it's possible to use this in a program which is being run by
1392 IPython itself (via %run), but some funny things will happen (a few
1389 IPython itself (via %run), but some funny things will happen (a few
1393 globals get overwritten). In the future this will be cleaned up, as
1390 globals get overwritten). In the future this will be cleaned up, as
1394 there is no fundamental reason why it can't work perfectly."""
1391 there is no fundamental reason why it can't work perfectly."""
1395
1392
1396 # Get locals and globals from caller
1393 # Get locals and globals from caller
1397 if local_ns is None or global_ns is None:
1394 if local_ns is None or global_ns is None:
1398 call_frame = sys._getframe(stack_depth).f_back
1395 call_frame = sys._getframe(stack_depth).f_back
1399
1396
1400 if local_ns is None:
1397 if local_ns is None:
1401 local_ns = call_frame.f_locals
1398 local_ns = call_frame.f_locals
1402 if global_ns is None:
1399 if global_ns is None:
1403 global_ns = call_frame.f_globals
1400 global_ns = call_frame.f_globals
1404
1401
1405 # Update namespaces and fire up interpreter
1402 # Update namespaces and fire up interpreter
1406
1403
1407 # The global one is easy, we can just throw it in
1404 # The global one is easy, we can just throw it in
1408 self.user_global_ns = global_ns
1405 self.user_global_ns = global_ns
1409
1406
1410 # but the user/local one is tricky: ipython needs it to store internal
1407 # but the user/local one is tricky: ipython needs it to store internal
1411 # data, but we also need the locals. We'll copy locals in the user
1408 # data, but we also need the locals. We'll copy locals in the user
1412 # one, but will track what got copied so we can delete them at exit.
1409 # one, but will track what got copied so we can delete them at exit.
1413 # This is so that a later embedded call doesn't see locals from a
1410 # This is so that a later embedded call doesn't see locals from a
1414 # previous call (which most likely existed in a separate scope).
1411 # previous call (which most likely existed in a separate scope).
1415 local_varnames = local_ns.keys()
1412 local_varnames = local_ns.keys()
1416 self.user_ns.update(local_ns)
1413 self.user_ns.update(local_ns)
1417
1414
1418 # Patch for global embedding to make sure that things don't overwrite
1415 # Patch for global embedding to make sure that things don't overwrite
1419 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1416 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1420 # FIXME. Test this a bit more carefully (the if.. is new)
1417 # FIXME. Test this a bit more carefully (the if.. is new)
1421 if local_ns is None and global_ns is None:
1418 if local_ns is None and global_ns is None:
1422 self.user_global_ns.update(__main__.__dict__)
1419 self.user_global_ns.update(__main__.__dict__)
1423
1420
1424 # make sure the tab-completer has the correct frame information, so it
1421 # make sure the tab-completer has the correct frame information, so it
1425 # actually completes using the frame's locals/globals
1422 # actually completes using the frame's locals/globals
1426 self.set_completer_frame()
1423 self.set_completer_frame()
1427
1424
1428 # before activating the interactive mode, we need to make sure that
1425 # before activating the interactive mode, we need to make sure that
1429 # all names in the builtin namespace needed by ipython point to
1426 # all names in the builtin namespace needed by ipython point to
1430 # ourselves, and not to other instances.
1427 # ourselves, and not to other instances.
1431 self.add_builtins()
1428 self.add_builtins()
1432
1429
1433 self.interact(header)
1430 self.interact(header)
1434
1431
1435 # now, purge out the user namespace from anything we might have added
1432 # now, purge out the user namespace from anything we might have added
1436 # from the caller's local namespace
1433 # from the caller's local namespace
1437 delvar = self.user_ns.pop
1434 delvar = self.user_ns.pop
1438 for var in local_varnames:
1435 for var in local_varnames:
1439 delvar(var,None)
1436 delvar(var,None)
1440 # and clean builtins we may have overridden
1437 # and clean builtins we may have overridden
1441 self.clean_builtins()
1438 self.clean_builtins()
1442
1439
1443 def interact(self, banner=None):
1440 def interact(self, banner=None):
1444 """Closely emulate the interactive Python console.
1441 """Closely emulate the interactive Python console.
1445
1442
1446 The optional banner argument specify the banner to print
1443 The optional banner argument specify the banner to print
1447 before the first interaction; by default it prints a banner
1444 before the first interaction; by default it prints a banner
1448 similar to the one printed by the real Python interpreter,
1445 similar to the one printed by the real Python interpreter,
1449 followed by the current class name in parentheses (so as not
1446 followed by the current class name in parentheses (so as not
1450 to confuse this with the real interpreter -- since it's so
1447 to confuse this with the real interpreter -- since it's so
1451 close!).
1448 close!).
1452
1449
1453 """
1450 """
1454 cprt = 'Type "copyright", "credits" or "license" for more information.'
1451 cprt = 'Type "copyright", "credits" or "license" for more information.'
1455 if banner is None:
1452 if banner is None:
1456 self.write("Python %s on %s\n%s\n(%s)\n" %
1453 self.write("Python %s on %s\n%s\n(%s)\n" %
1457 (sys.version, sys.platform, cprt,
1454 (sys.version, sys.platform, cprt,
1458 self.__class__.__name__))
1455 self.__class__.__name__))
1459 else:
1456 else:
1460 self.write(banner)
1457 self.write(banner)
1461
1458
1462 more = 0
1459 more = 0
1463
1460
1464 # Mark activity in the builtins
1461 # Mark activity in the builtins
1465 __builtin__.__dict__['__IPYTHON__active'] += 1
1462 __builtin__.__dict__['__IPYTHON__active'] += 1
1466
1463
1467 # exit_now is set by a call to %Exit or %Quit
1464 # exit_now is set by a call to %Exit or %Quit
1468 self.exit_now = False
1465 self.exit_now = False
1469 while not self.exit_now:
1466 while not self.exit_now:
1470 if more:
1467 if more:
1471 prompt = self.outputcache.prompt2
1468 prompt = self.outputcache.prompt2
1472 if self.autoindent:
1469 if self.autoindent:
1473 self.readline_startup_hook(self.pre_readline)
1470 self.readline_startup_hook(self.pre_readline)
1474 else:
1471 else:
1475 prompt = self.outputcache.prompt1
1472 prompt = self.outputcache.prompt1
1476 try:
1473 try:
1477 line = self.raw_input(prompt,more)
1474 line = self.raw_input(prompt,more)
1478 if self.autoindent:
1475 if self.autoindent:
1479 self.readline_startup_hook(None)
1476 self.readline_startup_hook(None)
1480 except KeyboardInterrupt:
1477 except KeyboardInterrupt:
1481 self.write('\nKeyboardInterrupt\n')
1478 self.write('\nKeyboardInterrupt\n')
1482 self.resetbuffer()
1479 self.resetbuffer()
1483 # keep cache in sync with the prompt counter:
1480 # keep cache in sync with the prompt counter:
1484 self.outputcache.prompt_count -= 1
1481 self.outputcache.prompt_count -= 1
1485
1482
1486 if self.autoindent:
1483 if self.autoindent:
1487 self.indent_current_nsp = 0
1484 self.indent_current_nsp = 0
1488 more = 0
1485 more = 0
1489 except EOFError:
1486 except EOFError:
1490 if self.autoindent:
1487 if self.autoindent:
1491 self.readline_startup_hook(None)
1488 self.readline_startup_hook(None)
1492 self.write('\n')
1489 self.write('\n')
1493 self.exit()
1490 self.exit()
1494 except bdb.BdbQuit:
1491 except bdb.BdbQuit:
1495 warn('The Python debugger has exited with a BdbQuit exception.\n'
1492 warn('The Python debugger has exited with a BdbQuit exception.\n'
1496 'Because of how pdb handles the stack, it is impossible\n'
1493 'Because of how pdb handles the stack, it is impossible\n'
1497 'for IPython to properly format this particular exception.\n'
1494 'for IPython to properly format this particular exception.\n'
1498 'IPython will resume normal operation.')
1495 'IPython will resume normal operation.')
1499 except:
1496 except:
1500 # exceptions here are VERY RARE, but they can be triggered
1497 # exceptions here are VERY RARE, but they can be triggered
1501 # asynchronously by signal handlers, for example.
1498 # asynchronously by signal handlers, for example.
1502 self.showtraceback()
1499 self.showtraceback()
1503 else:
1500 else:
1504 more = self.push(line)
1501 more = self.push(line)
1505 if (self.SyntaxTB.last_syntax_error and
1502 if (self.SyntaxTB.last_syntax_error and
1506 self.rc.autoedit_syntax):
1503 self.rc.autoedit_syntax):
1507 self.edit_syntax_error()
1504 self.edit_syntax_error()
1508
1505
1509 # We are off again...
1506 # We are off again...
1510 __builtin__.__dict__['__IPYTHON__active'] -= 1
1507 __builtin__.__dict__['__IPYTHON__active'] -= 1
1511
1508
1512 def excepthook(self, etype, value, tb):
1509 def excepthook(self, etype, value, tb):
1513 """One more defense for GUI apps that call sys.excepthook.
1510 """One more defense for GUI apps that call sys.excepthook.
1514
1511
1515 GUI frameworks like wxPython trap exceptions and call
1512 GUI frameworks like wxPython trap exceptions and call
1516 sys.excepthook themselves. I guess this is a feature that
1513 sys.excepthook themselves. I guess this is a feature that
1517 enables them to keep running after exceptions that would
1514 enables them to keep running after exceptions that would
1518 otherwise kill their mainloop. This is a bother for IPython
1515 otherwise kill their mainloop. This is a bother for IPython
1519 which excepts to catch all of the program exceptions with a try:
1516 which excepts to catch all of the program exceptions with a try:
1520 except: statement.
1517 except: statement.
1521
1518
1522 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1519 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1523 any app directly invokes sys.excepthook, it will look to the user like
1520 any app directly invokes sys.excepthook, it will look to the user like
1524 IPython crashed. In order to work around this, we can disable the
1521 IPython crashed. In order to work around this, we can disable the
1525 CrashHandler and replace it with this excepthook instead, which prints a
1522 CrashHandler and replace it with this excepthook instead, which prints a
1526 regular traceback using our InteractiveTB. In this fashion, apps which
1523 regular traceback using our InteractiveTB. In this fashion, apps which
1527 call sys.excepthook will generate a regular-looking exception from
1524 call sys.excepthook will generate a regular-looking exception from
1528 IPython, and the CrashHandler will only be triggered by real IPython
1525 IPython, and the CrashHandler will only be triggered by real IPython
1529 crashes.
1526 crashes.
1530
1527
1531 This hook should be used sparingly, only in places which are not likely
1528 This hook should be used sparingly, only in places which are not likely
1532 to be true IPython errors.
1529 to be true IPython errors.
1533 """
1530 """
1534 self.showtraceback((etype,value,tb),tb_offset=0)
1531 self.showtraceback((etype,value,tb),tb_offset=0)
1535
1532
1536 def transform_alias(self, alias,rest=''):
1533 def transform_alias(self, alias,rest=''):
1537 """ Transform alias to system command string
1534 """ Transform alias to system command string
1538
1535
1539 """
1536 """
1540 nargs,cmd = self.alias_table[alias]
1537 nargs,cmd = self.alias_table[alias]
1541 if ' ' in cmd and os.path.isfile(cmd):
1538 if ' ' in cmd and os.path.isfile(cmd):
1542 cmd = '"%s"' % cmd
1539 cmd = '"%s"' % cmd
1543
1540
1544 # Expand the %l special to be the user's input line
1541 # Expand the %l special to be the user's input line
1545 if cmd.find('%l') >= 0:
1542 if cmd.find('%l') >= 0:
1546 cmd = cmd.replace('%l',rest)
1543 cmd = cmd.replace('%l',rest)
1547 rest = ''
1544 rest = ''
1548 if nargs==0:
1545 if nargs==0:
1549 # Simple, argument-less aliases
1546 # Simple, argument-less aliases
1550 cmd = '%s %s' % (cmd,rest)
1547 cmd = '%s %s' % (cmd,rest)
1551 else:
1548 else:
1552 # Handle aliases with positional arguments
1549 # Handle aliases with positional arguments
1553 args = rest.split(None,nargs)
1550 args = rest.split(None,nargs)
1554 if len(args)< nargs:
1551 if len(args)< nargs:
1555 error('Alias <%s> requires %s arguments, %s given.' %
1552 error('Alias <%s> requires %s arguments, %s given.' %
1556 (alias,nargs,len(args)))
1553 (alias,nargs,len(args)))
1557 return None
1554 return None
1558 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1555 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1559 # Now call the macro, evaluating in the user's namespace
1556 # Now call the macro, evaluating in the user's namespace
1560
1557
1561 return cmd
1558 return cmd
1562
1559
1563 def call_alias(self,alias,rest=''):
1560 def call_alias(self,alias,rest=''):
1564 """Call an alias given its name and the rest of the line.
1561 """Call an alias given its name and the rest of the line.
1565
1562
1566 This is only used to provide backwards compatibility for users of
1563 This is only used to provide backwards compatibility for users of
1567 ipalias(), use of which is not recommended for anymore."""
1564 ipalias(), use of which is not recommended for anymore."""
1568
1565
1569 # Now call the macro, evaluating in the user's namespace
1566 # Now call the macro, evaluating in the user's namespace
1570 cmd = self.transform_alias(alias, rest)
1567 cmd = self.transform_alias(alias, rest)
1571 try:
1568 try:
1572 self.system(cmd)
1569 self.system(cmd)
1573 except:
1570 except:
1574 self.showtraceback()
1571 self.showtraceback()
1575
1572
1576 def indent_current_str(self):
1573 def indent_current_str(self):
1577 """return the current level of indentation as a string"""
1574 """return the current level of indentation as a string"""
1578 return self.indent_current_nsp * ' '
1575 return self.indent_current_nsp * ' '
1579
1576
1580 def autoindent_update(self,line):
1577 def autoindent_update(self,line):
1581 """Keep track of the indent level."""
1578 """Keep track of the indent level."""
1582
1579
1583 #debugx('line')
1580 #debugx('line')
1584 #debugx('self.indent_current_nsp')
1581 #debugx('self.indent_current_nsp')
1585 if self.autoindent:
1582 if self.autoindent:
1586 if line:
1583 if line:
1587 inisp = num_ini_spaces(line)
1584 inisp = num_ini_spaces(line)
1588 if inisp < self.indent_current_nsp:
1585 if inisp < self.indent_current_nsp:
1589 self.indent_current_nsp = inisp
1586 self.indent_current_nsp = inisp
1590
1587
1591 if line[-1] == ':':
1588 if line[-1] == ':':
1592 self.indent_current_nsp += 4
1589 self.indent_current_nsp += 4
1593 elif dedent_re.match(line):
1590 elif dedent_re.match(line):
1594 self.indent_current_nsp -= 4
1591 self.indent_current_nsp -= 4
1595 else:
1592 else:
1596 self.indent_current_nsp = 0
1593 self.indent_current_nsp = 0
1597
1594
1598 def runlines(self,lines):
1595 def runlines(self,lines):
1599 """Run a string of one or more lines of source.
1596 """Run a string of one or more lines of source.
1600
1597
1601 This method is capable of running a string containing multiple source
1598 This method is capable of running a string containing multiple source
1602 lines, as if they had been entered at the IPython prompt. Since it
1599 lines, as if they had been entered at the IPython prompt. Since it
1603 exposes IPython's processing machinery, the given strings can contain
1600 exposes IPython's processing machinery, the given strings can contain
1604 magic calls (%magic), special shell access (!cmd), etc."""
1601 magic calls (%magic), special shell access (!cmd), etc."""
1605
1602
1606 # We must start with a clean buffer, in case this is run from an
1603 # We must start with a clean buffer, in case this is run from an
1607 # interactive IPython session (via a magic, for example).
1604 # interactive IPython session (via a magic, for example).
1608 self.resetbuffer()
1605 self.resetbuffer()
1609 lines = lines.split('\n')
1606 lines = lines.split('\n')
1610 more = 0
1607 more = 0
1611 for line in lines:
1608 for line in lines:
1612 # skip blank lines so we don't mess up the prompt counter, but do
1609 # skip blank lines so we don't mess up the prompt counter, but do
1613 # NOT skip even a blank line if we are in a code block (more is
1610 # NOT skip even a blank line if we are in a code block (more is
1614 # true)
1611 # true)
1615 if line or more:
1612 if line or more:
1616 more = self.push(self.prefilter(line,more))
1613 more = self.push(self.prefilter(line,more))
1617 # IPython's runsource returns None if there was an error
1614 # IPython's runsource returns None if there was an error
1618 # compiling the code. This allows us to stop processing right
1615 # compiling the code. This allows us to stop processing right
1619 # away, so the user gets the error message at the right place.
1616 # away, so the user gets the error message at the right place.
1620 if more is None:
1617 if more is None:
1621 break
1618 break
1622 # final newline in case the input didn't have it, so that the code
1619 # final newline in case the input didn't have it, so that the code
1623 # actually does get executed
1620 # actually does get executed
1624 if more:
1621 if more:
1625 self.push('\n')
1622 self.push('\n')
1626
1623
1627 def runsource(self, source, filename='<input>', symbol='single'):
1624 def runsource(self, source, filename='<input>', symbol='single'):
1628 """Compile and run some source in the interpreter.
1625 """Compile and run some source in the interpreter.
1629
1626
1630 Arguments are as for compile_command().
1627 Arguments are as for compile_command().
1631
1628
1632 One several things can happen:
1629 One several things can happen:
1633
1630
1634 1) The input is incorrect; compile_command() raised an
1631 1) The input is incorrect; compile_command() raised an
1635 exception (SyntaxError or OverflowError). A syntax traceback
1632 exception (SyntaxError or OverflowError). A syntax traceback
1636 will be printed by calling the showsyntaxerror() method.
1633 will be printed by calling the showsyntaxerror() method.
1637
1634
1638 2) The input is incomplete, and more input is required;
1635 2) The input is incomplete, and more input is required;
1639 compile_command() returned None. Nothing happens.
1636 compile_command() returned None. Nothing happens.
1640
1637
1641 3) The input is complete; compile_command() returned a code
1638 3) The input is complete; compile_command() returned a code
1642 object. The code is executed by calling self.runcode() (which
1639 object. The code is executed by calling self.runcode() (which
1643 also handles run-time exceptions, except for SystemExit).
1640 also handles run-time exceptions, except for SystemExit).
1644
1641
1645 The return value is:
1642 The return value is:
1646
1643
1647 - True in case 2
1644 - True in case 2
1648
1645
1649 - False in the other cases, unless an exception is raised, where
1646 - False in the other cases, unless an exception is raised, where
1650 None is returned instead. This can be used by external callers to
1647 None is returned instead. This can be used by external callers to
1651 know whether to continue feeding input or not.
1648 know whether to continue feeding input or not.
1652
1649
1653 The return value can be used to decide whether to use sys.ps1 or
1650 The return value can be used to decide whether to use sys.ps1 or
1654 sys.ps2 to prompt the next line."""
1651 sys.ps2 to prompt the next line."""
1655
1652
1656 try:
1653 try:
1657 code = self.compile(source,filename,symbol)
1654 code = self.compile(source,filename,symbol)
1658 except (OverflowError, SyntaxError, ValueError):
1655 except (OverflowError, SyntaxError, ValueError):
1659 # Case 1
1656 # Case 1
1660 self.showsyntaxerror(filename)
1657 self.showsyntaxerror(filename)
1661 return None
1658 return None
1662
1659
1663 if code is None:
1660 if code is None:
1664 # Case 2
1661 # Case 2
1665 return True
1662 return True
1666
1663
1667 # Case 3
1664 # Case 3
1668 # We store the code object so that threaded shells and
1665 # We store the code object so that threaded shells and
1669 # custom exception handlers can access all this info if needed.
1666 # custom exception handlers can access all this info if needed.
1670 # The source corresponding to this can be obtained from the
1667 # The source corresponding to this can be obtained from the
1671 # buffer attribute as '\n'.join(self.buffer).
1668 # buffer attribute as '\n'.join(self.buffer).
1672 self.code_to_run = code
1669 self.code_to_run = code
1673 # now actually execute the code object
1670 # now actually execute the code object
1674 if self.runcode(code) == 0:
1671 if self.runcode(code) == 0:
1675 return False
1672 return False
1676 else:
1673 else:
1677 return None
1674 return None
1678
1675
1679 def runcode(self,code_obj):
1676 def runcode(self,code_obj):
1680 """Execute a code object.
1677 """Execute a code object.
1681
1678
1682 When an exception occurs, self.showtraceback() is called to display a
1679 When an exception occurs, self.showtraceback() is called to display a
1683 traceback.
1680 traceback.
1684
1681
1685 Return value: a flag indicating whether the code to be run completed
1682 Return value: a flag indicating whether the code to be run completed
1686 successfully:
1683 successfully:
1687
1684
1688 - 0: successful execution.
1685 - 0: successful execution.
1689 - 1: an error occurred.
1686 - 1: an error occurred.
1690 """
1687 """
1691
1688
1692 # Set our own excepthook in case the user code tries to call it
1689 # Set our own excepthook in case the user code tries to call it
1693 # directly, so that the IPython crash handler doesn't get triggered
1690 # directly, so that the IPython crash handler doesn't get triggered
1694 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1691 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1695
1692
1696 # we save the original sys.excepthook in the instance, in case config
1693 # we save the original sys.excepthook in the instance, in case config
1697 # code (such as magics) needs access to it.
1694 # code (such as magics) needs access to it.
1698 self.sys_excepthook = old_excepthook
1695 self.sys_excepthook = old_excepthook
1699 outflag = 1 # happens in more places, so it's easier as default
1696 outflag = 1 # happens in more places, so it's easier as default
1700 try:
1697 try:
1701 try:
1698 try:
1702 # Embedded instances require separate global/local namespaces
1699 # Embedded instances require separate global/local namespaces
1703 # so they can see both the surrounding (local) namespace and
1700 # so they can see both the surrounding (local) namespace and
1704 # the module-level globals when called inside another function.
1701 # the module-level globals when called inside another function.
1705 if self.embedded:
1702 if self.embedded:
1706 exec code_obj in self.user_global_ns, self.user_ns
1703 exec code_obj in self.user_global_ns, self.user_ns
1707 # Normal (non-embedded) instances should only have a single
1704 # Normal (non-embedded) instances should only have a single
1708 # namespace for user code execution, otherwise functions won't
1705 # namespace for user code execution, otherwise functions won't
1709 # see interactive top-level globals.
1706 # see interactive top-level globals.
1710 else:
1707 else:
1711 exec code_obj in self.user_ns
1708 exec code_obj in self.user_ns
1712 finally:
1709 finally:
1713 # Reset our crash handler in place
1710 # Reset our crash handler in place
1714 sys.excepthook = old_excepthook
1711 sys.excepthook = old_excepthook
1715 except SystemExit:
1712 except SystemExit:
1716 self.resetbuffer()
1713 self.resetbuffer()
1717 self.showtraceback()
1714 self.showtraceback()
1718 warn("Type exit or quit to exit IPython "
1715 warn("Type exit or quit to exit IPython "
1719 "(%Exit or %Quit do so unconditionally).",level=1)
1716 "(%Exit or %Quit do so unconditionally).",level=1)
1720 except self.custom_exceptions:
1717 except self.custom_exceptions:
1721 etype,value,tb = sys.exc_info()
1718 etype,value,tb = sys.exc_info()
1722 self.CustomTB(etype,value,tb)
1719 self.CustomTB(etype,value,tb)
1723 except:
1720 except:
1724 self.showtraceback()
1721 self.showtraceback()
1725 else:
1722 else:
1726 outflag = 0
1723 outflag = 0
1727 if softspace(sys.stdout, 0):
1724 if softspace(sys.stdout, 0):
1728 print
1725 print
1729 # Flush out code object which has been run (and source)
1726 # Flush out code object which has been run (and source)
1730 self.code_to_run = None
1727 self.code_to_run = None
1731 return outflag
1728 return outflag
1732
1729
1733 def push(self, line):
1730 def push(self, line):
1734 """Push a line to the interpreter.
1731 """Push a line to the interpreter.
1735
1732
1736 The line should not have a trailing newline; it may have
1733 The line should not have a trailing newline; it may have
1737 internal newlines. The line is appended to a buffer and the
1734 internal newlines. The line is appended to a buffer and the
1738 interpreter's runsource() method is called with the
1735 interpreter's runsource() method is called with the
1739 concatenated contents of the buffer as source. If this
1736 concatenated contents of the buffer as source. If this
1740 indicates that the command was executed or invalid, the buffer
1737 indicates that the command was executed or invalid, the buffer
1741 is reset; otherwise, the command is incomplete, and the buffer
1738 is reset; otherwise, the command is incomplete, and the buffer
1742 is left as it was after the line was appended. The return
1739 is left as it was after the line was appended. The return
1743 value is 1 if more input is required, 0 if the line was dealt
1740 value is 1 if more input is required, 0 if the line was dealt
1744 with in some way (this is the same as runsource()).
1741 with in some way (this is the same as runsource()).
1745 """
1742 """
1746
1743
1747 # autoindent management should be done here, and not in the
1744 # autoindent management should be done here, and not in the
1748 # interactive loop, since that one is only seen by keyboard input. We
1745 # interactive loop, since that one is only seen by keyboard input. We
1749 # need this done correctly even for code run via runlines (which uses
1746 # need this done correctly even for code run via runlines (which uses
1750 # push).
1747 # push).
1751
1748
1752 #print 'push line: <%s>' % line # dbg
1749 #print 'push line: <%s>' % line # dbg
1753 self.autoindent_update(line)
1750 self.autoindent_update(line)
1754
1751
1755 self.buffer.append(line)
1752 self.buffer.append(line)
1756 more = self.runsource('\n'.join(self.buffer), self.filename)
1753 more = self.runsource('\n'.join(self.buffer), self.filename)
1757 if not more:
1754 if not more:
1758 self.resetbuffer()
1755 self.resetbuffer()
1759 return more
1756 return more
1760
1757
1761 def resetbuffer(self):
1758 def resetbuffer(self):
1762 """Reset the input buffer."""
1759 """Reset the input buffer."""
1763 self.buffer[:] = []
1760 self.buffer[:] = []
1764
1761
1765 def raw_input(self,prompt='',continue_prompt=False):
1762 def raw_input(self,prompt='',continue_prompt=False):
1766 """Write a prompt and read a line.
1763 """Write a prompt and read a line.
1767
1764
1768 The returned line does not include the trailing newline.
1765 The returned line does not include the trailing newline.
1769 When the user enters the EOF key sequence, EOFError is raised.
1766 When the user enters the EOF key sequence, EOFError is raised.
1770
1767
1771 Optional inputs:
1768 Optional inputs:
1772
1769
1773 - prompt(''): a string to be printed to prompt the user.
1770 - prompt(''): a string to be printed to prompt the user.
1774
1771
1775 - continue_prompt(False): whether this line is the first one or a
1772 - continue_prompt(False): whether this line is the first one or a
1776 continuation in a sequence of inputs.
1773 continuation in a sequence of inputs.
1777 """
1774 """
1778
1775
1779 line = raw_input_original(prompt)
1776 line = raw_input_original(prompt)
1780
1777
1781 # Try to be reasonably smart about not re-indenting pasted input more
1778 # Try to be reasonably smart about not re-indenting pasted input more
1782 # than necessary. We do this by trimming out the auto-indent initial
1779 # than necessary. We do this by trimming out the auto-indent initial
1783 # spaces, if the user's actual input started itself with whitespace.
1780 # spaces, if the user's actual input started itself with whitespace.
1784 #debugx('self.buffer[-1]')
1781 #debugx('self.buffer[-1]')
1785
1782
1786 if self.autoindent:
1783 if self.autoindent:
1787 if num_ini_spaces(line) > self.indent_current_nsp:
1784 if num_ini_spaces(line) > self.indent_current_nsp:
1788 line = line[self.indent_current_nsp:]
1785 line = line[self.indent_current_nsp:]
1789 self.indent_current_nsp = 0
1786 self.indent_current_nsp = 0
1790
1787
1791 # store the unfiltered input before the user has any chance to modify
1788 # store the unfiltered input before the user has any chance to modify
1792 # it.
1789 # it.
1793 if line.strip():
1790 if line.strip():
1794 if continue_prompt:
1791 if continue_prompt:
1795 self.input_hist_raw[-1] += '%s\n' % line
1792 self.input_hist_raw[-1] += '%s\n' % line
1796 else:
1793 else:
1797 self.input_hist_raw.append('%s\n' % line)
1794 self.input_hist_raw.append('%s\n' % line)
1798
1795
1799 lineout = self.prefilter(line,continue_prompt)
1796 lineout = self.prefilter(line,continue_prompt)
1800 return lineout
1797 return lineout
1801
1798
1802 def split_user_input(self,line):
1799 def split_user_input(self,line):
1803 """Split user input into pre-char, function part and rest."""
1800 """Split user input into pre-char, function part and rest."""
1804
1801
1805 lsplit = self.line_split.match(line)
1802 lsplit = self.line_split.match(line)
1806 if lsplit is None: # no regexp match returns None
1803 if lsplit is None: # no regexp match returns None
1807 try:
1804 try:
1808 iFun,theRest = line.split(None,1)
1805 iFun,theRest = line.split(None,1)
1809 except ValueError:
1806 except ValueError:
1810 iFun,theRest = line,''
1807 iFun,theRest = line,''
1811 pre = re.match('^(\s*)(.*)',line).groups()[0]
1808 pre = re.match('^(\s*)(.*)',line).groups()[0]
1812 else:
1809 else:
1813 pre,iFun,theRest = lsplit.groups()
1810 pre,iFun,theRest = lsplit.groups()
1814
1811
1815 #print 'line:<%s>' % line # dbg
1812 #print 'line:<%s>' % line # dbg
1816 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1813 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1817 return pre,iFun.strip(),theRest
1814 return pre,iFun.strip(),theRest
1818
1815
1819 def _prefilter(self, line, continue_prompt):
1816 def _prefilter(self, line, continue_prompt):
1820 """Calls different preprocessors, depending on the form of line."""
1817 """Calls different preprocessors, depending on the form of line."""
1821
1818
1822 # All handlers *must* return a value, even if it's blank ('').
1819 # All handlers *must* return a value, even if it's blank ('').
1823
1820
1824 # Lines are NOT logged here. Handlers should process the line as
1821 # Lines are NOT logged here. Handlers should process the line as
1825 # needed, update the cache AND log it (so that the input cache array
1822 # needed, update the cache AND log it (so that the input cache array
1826 # stays synced).
1823 # stays synced).
1827
1824
1828 # This function is _very_ delicate, and since it's also the one which
1825 # This function is _very_ delicate, and since it's also the one which
1829 # determines IPython's response to user input, it must be as efficient
1826 # determines IPython's response to user input, it must be as efficient
1830 # as possible. For this reason it has _many_ returns in it, trying
1827 # as possible. For this reason it has _many_ returns in it, trying
1831 # always to exit as quickly as it can figure out what it needs to do.
1828 # always to exit as quickly as it can figure out what it needs to do.
1832
1829
1833 # This function is the main responsible for maintaining IPython's
1830 # This function is the main responsible for maintaining IPython's
1834 # behavior respectful of Python's semantics. So be _very_ careful if
1831 # behavior respectful of Python's semantics. So be _very_ careful if
1835 # making changes to anything here.
1832 # making changes to anything here.
1836
1833
1837 #.....................................................................
1834 #.....................................................................
1838 # Code begins
1835 # Code begins
1839
1836
1840 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1837 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1841
1838
1842 # save the line away in case we crash, so the post-mortem handler can
1839 # save the line away in case we crash, so the post-mortem handler can
1843 # record it
1840 # record it
1844 self._last_input_line = line
1841 self._last_input_line = line
1845
1842
1846 #print '***line: <%s>' % line # dbg
1843 #print '***line: <%s>' % line # dbg
1847
1844
1848 # the input history needs to track even empty lines
1845 # the input history needs to track even empty lines
1849 stripped = line.strip()
1846 stripped = line.strip()
1850
1847
1851 if not stripped:
1848 if not stripped:
1852 if not continue_prompt:
1849 if not continue_prompt:
1853 self.outputcache.prompt_count -= 1
1850 self.outputcache.prompt_count -= 1
1854 return self.handle_normal(line,continue_prompt)
1851 return self.handle_normal(line,continue_prompt)
1855 #return self.handle_normal('',continue_prompt)
1852 #return self.handle_normal('',continue_prompt)
1856
1853
1857 # print '***cont',continue_prompt # dbg
1854 # print '***cont',continue_prompt # dbg
1858 # special handlers are only allowed for single line statements
1855 # special handlers are only allowed for single line statements
1859 if continue_prompt and not self.rc.multi_line_specials:
1856 if continue_prompt and not self.rc.multi_line_specials:
1860 return self.handle_normal(line,continue_prompt)
1857 return self.handle_normal(line,continue_prompt)
1861
1858
1862
1859
1863 # For the rest, we need the structure of the input
1860 # For the rest, we need the structure of the input
1864 pre,iFun,theRest = self.split_user_input(line)
1861 pre,iFun,theRest = self.split_user_input(line)
1865
1862
1866 # See whether any pre-existing handler can take care of it
1863 # See whether any pre-existing handler can take care of it
1867
1864
1868 rewritten = self.hooks.input_prefilter(stripped)
1865 rewritten = self.hooks.input_prefilter(stripped)
1869 if rewritten != stripped: # ok, some prefilter did something
1866 if rewritten != stripped: # ok, some prefilter did something
1870 rewritten = pre + rewritten # add indentation
1867 rewritten = pre + rewritten # add indentation
1871 return self.handle_normal(rewritten)
1868 return self.handle_normal(rewritten)
1872
1869
1873
1870
1874
1871
1875
1872
1876 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1873 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1877
1874
1878 # First check for explicit escapes in the last/first character
1875 # First check for explicit escapes in the last/first character
1879 handler = None
1876 handler = None
1880 if line[-1] == self.ESC_HELP:
1877 if line[-1] == self.ESC_HELP:
1881 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1878 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1882 if handler is None:
1879 if handler is None:
1883 # look at the first character of iFun, NOT of line, so we skip
1880 # look at the first character of iFun, NOT of line, so we skip
1884 # leading whitespace in multiline input
1881 # leading whitespace in multiline input
1885 handler = self.esc_handlers.get(iFun[0:1])
1882 handler = self.esc_handlers.get(iFun[0:1])
1886 if handler is not None:
1883 if handler is not None:
1887 return handler(line,continue_prompt,pre,iFun,theRest)
1884 return handler(line,continue_prompt,pre,iFun,theRest)
1888 # Emacs ipython-mode tags certain input lines
1885 # Emacs ipython-mode tags certain input lines
1889 if line.endswith('# PYTHON-MODE'):
1886 if line.endswith('# PYTHON-MODE'):
1890 return self.handle_emacs(line,continue_prompt)
1887 return self.handle_emacs(line,continue_prompt)
1891
1888
1892 # Next, check if we can automatically execute this thing
1889 # Next, check if we can automatically execute this thing
1893
1890
1894 # Allow ! in multi-line statements if multi_line_specials is on:
1891 # Allow ! in multi-line statements if multi_line_specials is on:
1895 if continue_prompt and self.rc.multi_line_specials and \
1892 if continue_prompt and self.rc.multi_line_specials and \
1896 iFun.startswith(self.ESC_SHELL):
1893 iFun.startswith(self.ESC_SHELL):
1897 return self.handle_shell_escape(line,continue_prompt,
1894 return self.handle_shell_escape(line,continue_prompt,
1898 pre=pre,iFun=iFun,
1895 pre=pre,iFun=iFun,
1899 theRest=theRest)
1896 theRest=theRest)
1900
1897
1901 # Let's try to find if the input line is a magic fn
1898 # Let's try to find if the input line is a magic fn
1902 oinfo = None
1899 oinfo = None
1903 if hasattr(self,'magic_'+iFun):
1900 if hasattr(self,'magic_'+iFun):
1904 # WARNING: _ofind uses getattr(), so it can consume generators and
1901 # WARNING: _ofind uses getattr(), so it can consume generators and
1905 # cause other side effects.
1902 # cause other side effects.
1906 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1903 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1907 if oinfo['ismagic']:
1904 if oinfo['ismagic']:
1908 # Be careful not to call magics when a variable assignment is
1905 # Be careful not to call magics when a variable assignment is
1909 # being made (ls='hi', for example)
1906 # being made (ls='hi', for example)
1910 if self.rc.automagic and \
1907 if self.rc.automagic and \
1911 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1908 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1912 (self.rc.multi_line_specials or not continue_prompt):
1909 (self.rc.multi_line_specials or not continue_prompt):
1913 return self.handle_magic(line,continue_prompt,
1910 return self.handle_magic(line,continue_prompt,
1914 pre,iFun,theRest)
1911 pre,iFun,theRest)
1915 else:
1912 else:
1916 return self.handle_normal(line,continue_prompt)
1913 return self.handle_normal(line,continue_prompt)
1917
1914
1918 # If the rest of the line begins with an (in)equality, assginment or
1915 # If the rest of the line begins with an (in)equality, assginment or
1919 # function call, we should not call _ofind but simply execute it.
1916 # function call, we should not call _ofind but simply execute it.
1920 # This avoids spurious geattr() accesses on objects upon assignment.
1917 # This avoids spurious geattr() accesses on objects upon assignment.
1921 #
1918 #
1922 # It also allows users to assign to either alias or magic names true
1919 # It also allows users to assign to either alias or magic names true
1923 # python variables (the magic/alias systems always take second seat to
1920 # python variables (the magic/alias systems always take second seat to
1924 # true python code).
1921 # true python code).
1925 if theRest and theRest[0] in '!=()':
1922 if theRest and theRest[0] in '!=()':
1926 return self.handle_normal(line,continue_prompt)
1923 return self.handle_normal(line,continue_prompt)
1927
1924
1928 if oinfo is None:
1925 if oinfo is None:
1929 # let's try to ensure that _oinfo is ONLY called when autocall is
1926 # let's try to ensure that _oinfo is ONLY called when autocall is
1930 # on. Since it has inevitable potential side effects, at least
1927 # on. Since it has inevitable potential side effects, at least
1931 # having autocall off should be a guarantee to the user that no
1928 # having autocall off should be a guarantee to the user that no
1932 # weird things will happen.
1929 # weird things will happen.
1933
1930
1934 if self.rc.autocall:
1931 if self.rc.autocall:
1935 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1932 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1936 else:
1933 else:
1937 # in this case, all that's left is either an alias or
1934 # in this case, all that's left is either an alias or
1938 # processing the line normally.
1935 # processing the line normally.
1939 if iFun in self.alias_table:
1936 if iFun in self.alias_table:
1940 return self.handle_alias(line,continue_prompt,
1937 return self.handle_alias(line,continue_prompt,
1941 pre,iFun,theRest)
1938 pre,iFun,theRest)
1942
1939
1943 else:
1940 else:
1944 return self.handle_normal(line,continue_prompt)
1941 return self.handle_normal(line,continue_prompt)
1945
1942
1946 if not oinfo['found']:
1943 if not oinfo['found']:
1947 return self.handle_normal(line,continue_prompt)
1944 return self.handle_normal(line,continue_prompt)
1948 else:
1945 else:
1949 #print 'pre<%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1946 #print 'pre<%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1950 if oinfo['isalias']:
1947 if oinfo['isalias']:
1951 return self.handle_alias(line,continue_prompt,
1948 return self.handle_alias(line,continue_prompt,
1952 pre,iFun,theRest)
1949 pre,iFun,theRest)
1953
1950
1954 if (self.rc.autocall
1951 if (self.rc.autocall
1955 and
1952 and
1956 (
1953 (
1957 #only consider exclusion re if not "," or ";" autoquoting
1954 #only consider exclusion re if not "," or ";" autoquoting
1958 (pre == self.ESC_QUOTE or pre == self.ESC_QUOTE2
1955 (pre == self.ESC_QUOTE or pre == self.ESC_QUOTE2
1959 or pre == self.ESC_PAREN) or
1956 or pre == self.ESC_PAREN) or
1960 (not self.re_exclude_auto.match(theRest)))
1957 (not self.re_exclude_auto.match(theRest)))
1961 and
1958 and
1962 self.re_fun_name.match(iFun) and
1959 self.re_fun_name.match(iFun) and
1963 callable(oinfo['obj'])) :
1960 callable(oinfo['obj'])) :
1964 #print 'going auto' # dbg
1961 #print 'going auto' # dbg
1965 return self.handle_auto(line,continue_prompt,
1962 return self.handle_auto(line,continue_prompt,
1966 pre,iFun,theRest,oinfo['obj'])
1963 pre,iFun,theRest,oinfo['obj'])
1967 else:
1964 else:
1968 #print 'was callable?', callable(oinfo['obj']) # dbg
1965 #print 'was callable?', callable(oinfo['obj']) # dbg
1969 return self.handle_normal(line,continue_prompt)
1966 return self.handle_normal(line,continue_prompt)
1970
1967
1971 # If we get here, we have a normal Python line. Log and return.
1968 # If we get here, we have a normal Python line. Log and return.
1972 return self.handle_normal(line,continue_prompt)
1969 return self.handle_normal(line,continue_prompt)
1973
1970
1974 def _prefilter_dumb(self, line, continue_prompt):
1971 def _prefilter_dumb(self, line, continue_prompt):
1975 """simple prefilter function, for debugging"""
1972 """simple prefilter function, for debugging"""
1976 return self.handle_normal(line,continue_prompt)
1973 return self.handle_normal(line,continue_prompt)
1977
1974
1978 # Set the default prefilter() function (this can be user-overridden)
1975 # Set the default prefilter() function (this can be user-overridden)
1979 prefilter = _prefilter
1976 prefilter = _prefilter
1980
1977
1981 def handle_normal(self,line,continue_prompt=None,
1978 def handle_normal(self,line,continue_prompt=None,
1982 pre=None,iFun=None,theRest=None):
1979 pre=None,iFun=None,theRest=None):
1983 """Handle normal input lines. Use as a template for handlers."""
1980 """Handle normal input lines. Use as a template for handlers."""
1984
1981
1985 # With autoindent on, we need some way to exit the input loop, and I
1982 # With autoindent on, we need some way to exit the input loop, and I
1986 # don't want to force the user to have to backspace all the way to
1983 # don't want to force the user to have to backspace all the way to
1987 # clear the line. The rule will be in this case, that either two
1984 # clear the line. The rule will be in this case, that either two
1988 # lines of pure whitespace in a row, or a line of pure whitespace but
1985 # lines of pure whitespace in a row, or a line of pure whitespace but
1989 # of a size different to the indent level, will exit the input loop.
1986 # of a size different to the indent level, will exit the input loop.
1990
1987
1991 if (continue_prompt and self.autoindent and line.isspace() and
1988 if (continue_prompt and self.autoindent and line.isspace() and
1992 (0 < abs(len(line) - self.indent_current_nsp) <= 2 or
1989 (0 < abs(len(line) - self.indent_current_nsp) <= 2 or
1993 (self.buffer[-1]).isspace() )):
1990 (self.buffer[-1]).isspace() )):
1994 line = ''
1991 line = ''
1995
1992
1996 self.log(line,continue_prompt)
1993 self.log(line,continue_prompt)
1997 return line
1994 return line
1998
1995
1999 def handle_alias(self,line,continue_prompt=None,
1996 def handle_alias(self,line,continue_prompt=None,
2000 pre=None,iFun=None,theRest=None):
1997 pre=None,iFun=None,theRest=None):
2001 """Handle alias input lines. """
1998 """Handle alias input lines. """
2002
1999
2003 # pre is needed, because it carries the leading whitespace. Otherwise
2000 # pre is needed, because it carries the leading whitespace. Otherwise
2004 # aliases won't work in indented sections.
2001 # aliases won't work in indented sections.
2005 transformed = self.transform_alias(iFun, theRest)
2002 transformed = self.transform_alias(iFun, theRest)
2006 line_out = '%s_ip.system(%s)' % (pre, make_quoted_expr( transformed ))
2003 line_out = '%s_ip.system(%s)' % (pre, make_quoted_expr( transformed ))
2007 self.log(line_out,continue_prompt)
2004 self.log(line_out,continue_prompt)
2008 return line_out
2005 return line_out
2009
2006
2010 def handle_shell_escape(self, line, continue_prompt=None,
2007 def handle_shell_escape(self, line, continue_prompt=None,
2011 pre=None,iFun=None,theRest=None):
2008 pre=None,iFun=None,theRest=None):
2012 """Execute the line in a shell, empty return value"""
2009 """Execute the line in a shell, empty return value"""
2013
2010
2014 #print 'line in :', `line` # dbg
2011 #print 'line in :', `line` # dbg
2015 # Example of a special handler. Others follow a similar pattern.
2012 # Example of a special handler. Others follow a similar pattern.
2016 if line.lstrip().startswith('!!'):
2013 if line.lstrip().startswith('!!'):
2017 # rewrite iFun/theRest to properly hold the call to %sx and
2014 # rewrite iFun/theRest to properly hold the call to %sx and
2018 # the actual command to be executed, so handle_magic can work
2015 # the actual command to be executed, so handle_magic can work
2019 # correctly
2016 # correctly
2020 theRest = '%s %s' % (iFun[2:],theRest)
2017 theRest = '%s %s' % (iFun[2:],theRest)
2021 iFun = 'sx'
2018 iFun = 'sx'
2022 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,
2019 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,
2023 line.lstrip()[2:]),
2020 line.lstrip()[2:]),
2024 continue_prompt,pre,iFun,theRest)
2021 continue_prompt,pre,iFun,theRest)
2025 else:
2022 else:
2026 cmd=line.lstrip().lstrip('!')
2023 cmd=line.lstrip().lstrip('!')
2027 line_out = '%s_ip.system(%s)' % (pre,make_quoted_expr(cmd))
2024 line_out = '%s_ip.system(%s)' % (pre,make_quoted_expr(cmd))
2028 # update cache/log and return
2025 # update cache/log and return
2029 self.log(line_out,continue_prompt)
2026 self.log(line_out,continue_prompt)
2030 return line_out
2027 return line_out
2031
2028
2032 def handle_magic(self, line, continue_prompt=None,
2029 def handle_magic(self, line, continue_prompt=None,
2033 pre=None,iFun=None,theRest=None):
2030 pre=None,iFun=None,theRest=None):
2034 """Execute magic functions."""
2031 """Execute magic functions."""
2035
2032
2036
2033
2037 cmd = '%s_ip.magic(%s)' % (pre,make_quoted_expr(iFun + " " + theRest))
2034 cmd = '%s_ip.magic(%s)' % (pre,make_quoted_expr(iFun + " " + theRest))
2038 self.log(cmd,continue_prompt)
2035 self.log(cmd,continue_prompt)
2039 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
2036 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
2040 return cmd
2037 return cmd
2041
2038
2042 def handle_auto(self, line, continue_prompt=None,
2039 def handle_auto(self, line, continue_prompt=None,
2043 pre=None,iFun=None,theRest=None,obj=None):
2040 pre=None,iFun=None,theRest=None,obj=None):
2044 """Hande lines which can be auto-executed, quoting if requested."""
2041 """Hande lines which can be auto-executed, quoting if requested."""
2045
2042
2046 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2043 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2047
2044
2048 # This should only be active for single-line input!
2045 # This should only be active for single-line input!
2049 if continue_prompt:
2046 if continue_prompt:
2050 self.log(line,continue_prompt)
2047 self.log(line,continue_prompt)
2051 return line
2048 return line
2052
2049
2053 auto_rewrite = True
2050 auto_rewrite = True
2054
2051
2055 if pre == self.ESC_QUOTE:
2052 if pre == self.ESC_QUOTE:
2056 # Auto-quote splitting on whitespace
2053 # Auto-quote splitting on whitespace
2057 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
2054 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
2058 elif pre == self.ESC_QUOTE2:
2055 elif pre == self.ESC_QUOTE2:
2059 # Auto-quote whole string
2056 # Auto-quote whole string
2060 newcmd = '%s("%s")' % (iFun,theRest)
2057 newcmd = '%s("%s")' % (iFun,theRest)
2061 elif pre == self.ESC_PAREN:
2058 elif pre == self.ESC_PAREN:
2062 newcmd = '%s(%s)' % (iFun,",".join(theRest.split()))
2059 newcmd = '%s(%s)' % (iFun,",".join(theRest.split()))
2063 else:
2060 else:
2064 # Auto-paren.
2061 # Auto-paren.
2065 # We only apply it to argument-less calls if the autocall
2062 # We only apply it to argument-less calls if the autocall
2066 # parameter is set to 2. We only need to check that autocall is <
2063 # parameter is set to 2. We only need to check that autocall is <
2067 # 2, since this function isn't called unless it's at least 1.
2064 # 2, since this function isn't called unless it's at least 1.
2068 if not theRest and (self.rc.autocall < 2):
2065 if not theRest and (self.rc.autocall < 2):
2069 newcmd = '%s %s' % (iFun,theRest)
2066 newcmd = '%s %s' % (iFun,theRest)
2070 auto_rewrite = False
2067 auto_rewrite = False
2071 else:
2068 else:
2072 if theRest.startswith('['):
2069 if theRest.startswith('['):
2073 if hasattr(obj,'__getitem__'):
2070 if hasattr(obj,'__getitem__'):
2074 # Don't autocall in this case: item access for an object
2071 # Don't autocall in this case: item access for an object
2075 # which is BOTH callable and implements __getitem__.
2072 # which is BOTH callable and implements __getitem__.
2076 newcmd = '%s %s' % (iFun,theRest)
2073 newcmd = '%s %s' % (iFun,theRest)
2077 auto_rewrite = False
2074 auto_rewrite = False
2078 else:
2075 else:
2079 # if the object doesn't support [] access, go ahead and
2076 # if the object doesn't support [] access, go ahead and
2080 # autocall
2077 # autocall
2081 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2078 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2082 elif theRest.endswith(';'):
2079 elif theRest.endswith(';'):
2083 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
2080 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
2084 else:
2081 else:
2085 newcmd = '%s(%s)' % (iFun.rstrip(), theRest)
2082 newcmd = '%s(%s)' % (iFun.rstrip(), theRest)
2086
2083
2087 if auto_rewrite:
2084 if auto_rewrite:
2088 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
2085 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
2089 # log what is now valid Python, not the actual user input (without the
2086 # log what is now valid Python, not the actual user input (without the
2090 # final newline)
2087 # final newline)
2091 self.log(newcmd,continue_prompt)
2088 self.log(newcmd,continue_prompt)
2092 return newcmd
2089 return newcmd
2093
2090
2094 def handle_help(self, line, continue_prompt=None,
2091 def handle_help(self, line, continue_prompt=None,
2095 pre=None,iFun=None,theRest=None):
2092 pre=None,iFun=None,theRest=None):
2096 """Try to get some help for the object.
2093 """Try to get some help for the object.
2097
2094
2098 obj? or ?obj -> basic information.
2095 obj? or ?obj -> basic information.
2099 obj?? or ??obj -> more details.
2096 obj?? or ??obj -> more details.
2100 """
2097 """
2101
2098
2102 # We need to make sure that we don't process lines which would be
2099 # We need to make sure that we don't process lines which would be
2103 # otherwise valid python, such as "x=1 # what?"
2100 # otherwise valid python, such as "x=1 # what?"
2104 try:
2101 try:
2105 codeop.compile_command(line)
2102 codeop.compile_command(line)
2106 except SyntaxError:
2103 except SyntaxError:
2107 # We should only handle as help stuff which is NOT valid syntax
2104 # We should only handle as help stuff which is NOT valid syntax
2108 if line[0]==self.ESC_HELP:
2105 if line[0]==self.ESC_HELP:
2109 line = line[1:]
2106 line = line[1:]
2110 elif line[-1]==self.ESC_HELP:
2107 elif line[-1]==self.ESC_HELP:
2111 line = line[:-1]
2108 line = line[:-1]
2112 self.log('#?'+line)
2109 self.log('#?'+line)
2113 if line:
2110 if line:
2114 self.magic_pinfo(line)
2111 self.magic_pinfo(line)
2115 else:
2112 else:
2116 page(self.usage,screen_lines=self.rc.screen_length)
2113 page(self.usage,screen_lines=self.rc.screen_length)
2117 return '' # Empty string is needed here!
2114 return '' # Empty string is needed here!
2118 except:
2115 except:
2119 # Pass any other exceptions through to the normal handler
2116 # Pass any other exceptions through to the normal handler
2120 return self.handle_normal(line,continue_prompt)
2117 return self.handle_normal(line,continue_prompt)
2121 else:
2118 else:
2122 # If the code compiles ok, we should handle it normally
2119 # If the code compiles ok, we should handle it normally
2123 return self.handle_normal(line,continue_prompt)
2120 return self.handle_normal(line,continue_prompt)
2124
2121
2125 def getapi(self):
2122 def getapi(self):
2126 """ Get an IPApi object for this shell instance
2123 """ Get an IPApi object for this shell instance
2127
2124
2128 Getting an IPApi object is always preferable to accessing the shell
2125 Getting an IPApi object is always preferable to accessing the shell
2129 directly, but this holds true especially for extensions.
2126 directly, but this holds true especially for extensions.
2130
2127
2131 It should always be possible to implement an extension with IPApi
2128 It should always be possible to implement an extension with IPApi
2132 alone. If not, contact maintainer to request an addition.
2129 alone. If not, contact maintainer to request an addition.
2133
2130
2134 """
2131 """
2135 return self.api
2132 return self.api
2136
2133
2137 def handle_emacs(self,line,continue_prompt=None,
2134 def handle_emacs(self,line,continue_prompt=None,
2138 pre=None,iFun=None,theRest=None):
2135 pre=None,iFun=None,theRest=None):
2139 """Handle input lines marked by python-mode."""
2136 """Handle input lines marked by python-mode."""
2140
2137
2141 # Currently, nothing is done. Later more functionality can be added
2138 # Currently, nothing is done. Later more functionality can be added
2142 # here if needed.
2139 # here if needed.
2143
2140
2144 # The input cache shouldn't be updated
2141 # The input cache shouldn't be updated
2145
2142
2146 return line
2143 return line
2147
2144
2148 def mktempfile(self,data=None):
2145 def mktempfile(self,data=None):
2149 """Make a new tempfile and return its filename.
2146 """Make a new tempfile and return its filename.
2150
2147
2151 This makes a call to tempfile.mktemp, but it registers the created
2148 This makes a call to tempfile.mktemp, but it registers the created
2152 filename internally so ipython cleans it up at exit time.
2149 filename internally so ipython cleans it up at exit time.
2153
2150
2154 Optional inputs:
2151 Optional inputs:
2155
2152
2156 - data(None): if data is given, it gets written out to the temp file
2153 - data(None): if data is given, it gets written out to the temp file
2157 immediately, and the file is closed again."""
2154 immediately, and the file is closed again."""
2158
2155
2159 filename = tempfile.mktemp('.py','ipython_edit_')
2156 filename = tempfile.mktemp('.py','ipython_edit_')
2160 self.tempfiles.append(filename)
2157 self.tempfiles.append(filename)
2161
2158
2162 if data:
2159 if data:
2163 tmp_file = open(filename,'w')
2160 tmp_file = open(filename,'w')
2164 tmp_file.write(data)
2161 tmp_file.write(data)
2165 tmp_file.close()
2162 tmp_file.close()
2166 return filename
2163 return filename
2167
2164
2168 def write(self,data):
2165 def write(self,data):
2169 """Write a string to the default output"""
2166 """Write a string to the default output"""
2170 Term.cout.write(data)
2167 Term.cout.write(data)
2171
2168
2172 def write_err(self,data):
2169 def write_err(self,data):
2173 """Write a string to the default error output"""
2170 """Write a string to the default error output"""
2174 Term.cerr.write(data)
2171 Term.cerr.write(data)
2175
2172
2176 def exit(self):
2173 def exit(self):
2177 """Handle interactive exit.
2174 """Handle interactive exit.
2178
2175
2179 This method sets the exit_now attribute."""
2176 This method sets the exit_now attribute."""
2180
2177
2181 if self.rc.confirm_exit:
2178 if self.rc.confirm_exit:
2182 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2179 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2183 self.exit_now = True
2180 self.exit_now = True
2184 else:
2181 else:
2185 self.exit_now = True
2182 self.exit_now = True
2186 return self.exit_now
2183 return self.exit_now
2187
2184
2188 def safe_execfile(self,fname,*where,**kw):
2185 def safe_execfile(self,fname,*where,**kw):
2189 fname = os.path.expanduser(fname)
2186 fname = os.path.expanduser(fname)
2190
2187
2191 # find things also in current directory
2188 # find things also in current directory
2192 dname = os.path.dirname(fname)
2189 dname = os.path.dirname(fname)
2193 if not sys.path.count(dname):
2190 if not sys.path.count(dname):
2194 sys.path.append(dname)
2191 sys.path.append(dname)
2195
2192
2196 try:
2193 try:
2197 xfile = open(fname)
2194 xfile = open(fname)
2198 except:
2195 except:
2199 print >> Term.cerr, \
2196 print >> Term.cerr, \
2200 'Could not open file <%s> for safe execution.' % fname
2197 'Could not open file <%s> for safe execution.' % fname
2201 return None
2198 return None
2202
2199
2203 kw.setdefault('islog',0)
2200 kw.setdefault('islog',0)
2204 kw.setdefault('quiet',1)
2201 kw.setdefault('quiet',1)
2205 kw.setdefault('exit_ignore',0)
2202 kw.setdefault('exit_ignore',0)
2206 first = xfile.readline()
2203 first = xfile.readline()
2207 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2204 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2208 xfile.close()
2205 xfile.close()
2209 # line by line execution
2206 # line by line execution
2210 if first.startswith(loghead) or kw['islog']:
2207 if first.startswith(loghead) or kw['islog']:
2211 print 'Loading log file <%s> one line at a time...' % fname
2208 print 'Loading log file <%s> one line at a time...' % fname
2212 if kw['quiet']:
2209 if kw['quiet']:
2213 stdout_save = sys.stdout
2210 stdout_save = sys.stdout
2214 sys.stdout = StringIO.StringIO()
2211 sys.stdout = StringIO.StringIO()
2215 try:
2212 try:
2216 globs,locs = where[0:2]
2213 globs,locs = where[0:2]
2217 except:
2214 except:
2218 try:
2215 try:
2219 globs = locs = where[0]
2216 globs = locs = where[0]
2220 except:
2217 except:
2221 globs = locs = globals()
2218 globs = locs = globals()
2222 badblocks = []
2219 badblocks = []
2223
2220
2224 # we also need to identify indented blocks of code when replaying
2221 # we also need to identify indented blocks of code when replaying
2225 # logs and put them together before passing them to an exec
2222 # logs and put them together before passing them to an exec
2226 # statement. This takes a bit of regexp and look-ahead work in the
2223 # statement. This takes a bit of regexp and look-ahead work in the
2227 # file. It's easiest if we swallow the whole thing in memory
2224 # file. It's easiest if we swallow the whole thing in memory
2228 # first, and manually walk through the lines list moving the
2225 # first, and manually walk through the lines list moving the
2229 # counter ourselves.
2226 # counter ourselves.
2230 indent_re = re.compile('\s+\S')
2227 indent_re = re.compile('\s+\S')
2231 xfile = open(fname)
2228 xfile = open(fname)
2232 filelines = xfile.readlines()
2229 filelines = xfile.readlines()
2233 xfile.close()
2230 xfile.close()
2234 nlines = len(filelines)
2231 nlines = len(filelines)
2235 lnum = 0
2232 lnum = 0
2236 while lnum < nlines:
2233 while lnum < nlines:
2237 line = filelines[lnum]
2234 line = filelines[lnum]
2238 lnum += 1
2235 lnum += 1
2239 # don't re-insert logger status info into cache
2236 # don't re-insert logger status info into cache
2240 if line.startswith('#log#'):
2237 if line.startswith('#log#'):
2241 continue
2238 continue
2242 else:
2239 else:
2243 # build a block of code (maybe a single line) for execution
2240 # build a block of code (maybe a single line) for execution
2244 block = line
2241 block = line
2245 try:
2242 try:
2246 next = filelines[lnum] # lnum has already incremented
2243 next = filelines[lnum] # lnum has already incremented
2247 except:
2244 except:
2248 next = None
2245 next = None
2249 while next and indent_re.match(next):
2246 while next and indent_re.match(next):
2250 block += next
2247 block += next
2251 lnum += 1
2248 lnum += 1
2252 try:
2249 try:
2253 next = filelines[lnum]
2250 next = filelines[lnum]
2254 except:
2251 except:
2255 next = None
2252 next = None
2256 # now execute the block of one or more lines
2253 # now execute the block of one or more lines
2257 try:
2254 try:
2258 exec block in globs,locs
2255 exec block in globs,locs
2259 except SystemExit:
2256 except SystemExit:
2260 pass
2257 pass
2261 except:
2258 except:
2262 badblocks.append(block.rstrip())
2259 badblocks.append(block.rstrip())
2263 if kw['quiet']: # restore stdout
2260 if kw['quiet']: # restore stdout
2264 sys.stdout.close()
2261 sys.stdout.close()
2265 sys.stdout = stdout_save
2262 sys.stdout = stdout_save
2266 print 'Finished replaying log file <%s>' % fname
2263 print 'Finished replaying log file <%s>' % fname
2267 if badblocks:
2264 if badblocks:
2268 print >> sys.stderr, ('\nThe following lines/blocks in file '
2265 print >> sys.stderr, ('\nThe following lines/blocks in file '
2269 '<%s> reported errors:' % fname)
2266 '<%s> reported errors:' % fname)
2270
2267
2271 for badline in badblocks:
2268 for badline in badblocks:
2272 print >> sys.stderr, badline
2269 print >> sys.stderr, badline
2273 else: # regular file execution
2270 else: # regular file execution
2274 try:
2271 try:
2275 execfile(fname,*where)
2272 execfile(fname,*where)
2276 except SyntaxError:
2273 except SyntaxError:
2277 self.showsyntaxerror()
2274 self.showsyntaxerror()
2278 warn('Failure executing file: <%s>' % fname)
2275 warn('Failure executing file: <%s>' % fname)
2279 except SystemExit,status:
2276 except SystemExit,status:
2280 if not kw['exit_ignore']:
2277 if not kw['exit_ignore']:
2281 self.showtraceback()
2278 self.showtraceback()
2282 warn('Failure executing file: <%s>' % fname)
2279 warn('Failure executing file: <%s>' % fname)
2283 except:
2280 except:
2284 self.showtraceback()
2281 self.showtraceback()
2285 warn('Failure executing file: <%s>' % fname)
2282 warn('Failure executing file: <%s>' % fname)
2286
2283
2287 #************************* end of file <iplib.py> *****************************
2284 #************************* end of file <iplib.py> *****************************
General Comments 0
You need to be logged in to leave comments. Login now