Show More
The requested changes are too big and content was truncated. Show full diff
@@ -0,0 +1,47 b'' | |||
|
1 | """ Tab completion support for a couple of linux package managers | |
|
2 | ||
|
3 | This is also an example of how to write custom completer plugins | |
|
4 | or hooks. | |
|
5 | ||
|
6 | Practical use: | |
|
7 | ||
|
8 | [ipython]|1> import ipy_linux_package_managers | |
|
9 | [ipython]|2> apt-get u<<< press tab here >>> | |
|
10 | update upgrade | |
|
11 | [ipython]|2> apt-get up | |
|
12 | ||
|
13 | """ | |
|
14 | import IPython.ipapi | |
|
15 | ||
|
16 | ip = IPython.ipapi.get() | |
|
17 | ||
|
18 | def apt_completers(self, event): | |
|
19 | """ This should return a list of strings with possible completions. | |
|
20 | ||
|
21 | Note that all the included strings that don't start with event.symbol | |
|
22 | are removed, in order to not confuse readline. | |
|
23 | ||
|
24 | """ | |
|
25 | # print event # dbg | |
|
26 | ||
|
27 | # commands are only suggested for the 'command' part of package manager | |
|
28 | # invocation | |
|
29 | ||
|
30 | cmd = (event.line + "<placeholder>").rsplit(None,1)[0] | |
|
31 | # print cmd | |
|
32 | if cmd.endswith('apt-get') or cmd.endswith('yum'): | |
|
33 | return ['update', 'upgrade', 'install', 'remove'] | |
|
34 | ||
|
35 | # later on, add dpkg -l / whatever to get list of possible | |
|
36 | # packages, add switches etc. for the rest of command line | |
|
37 | # filling | |
|
38 | ||
|
39 | raise IPython.ipapi.TryNext | |
|
40 | ||
|
41 | ||
|
42 | # re_key specifies the regexp that triggers the specified completer | |
|
43 | ||
|
44 | ip.set_hook('complete_command', apt_completers, re_key = '.*apt-get') | |
|
45 | ||
|
46 | ip.set_hook('complete_command', apt_completers, re_key = '.*yum') | |
|
47 | No newline at end of file |
@@ -0,0 +1,57 b'' | |||
|
1 | from IPython.hooks import CommandChainDispatcher | |
|
2 | import IPython.hooks | |
|
3 | ||
|
4 | import re | |
|
5 | ||
|
6 | class StrDispatch(object): | |
|
7 | """ Dispatch (lookup) a set of strings / regexps for match """ | |
|
8 | def __init__(self): | |
|
9 | self.strs = {} | |
|
10 | self.regexs = {} | |
|
11 | def add_s(self, s, obj, priority= 0 ): | |
|
12 | """ Adds a target 'string' for dispatching """ | |
|
13 | ||
|
14 | chain = self.strs.get(s, CommandChainDispatcher()) | |
|
15 | chain.add(obj,priority) | |
|
16 | self.strs[s] = chain | |
|
17 | ||
|
18 | def add_re(self, regex, obj, priority= 0 ): | |
|
19 | """ Adds a target regexp for dispatching """ | |
|
20 | ||
|
21 | chain = self.regexs.get(regex, CommandChainDispatcher()) | |
|
22 | chain.add(obj,priority) | |
|
23 | self.regexs[regex] = chain | |
|
24 | ||
|
25 | def dispatch(self, key): | |
|
26 | """ Get a seq of Commandchain objects that match key """ | |
|
27 | if key in self.strs: | |
|
28 | yield self.strs[key] | |
|
29 | ||
|
30 | for r, obj in self.regexs.items(): | |
|
31 | if re.match(r, key): | |
|
32 | yield obj | |
|
33 | else: | |
|
34 | #print "nomatch",key | |
|
35 | pass | |
|
36 | ||
|
37 | ||
|
38 | def __repr__(self): | |
|
39 | return "<Strdispatch %s, %s>" % (self.strs, self.regexs) | |
|
40 | def flat_matches(self, key): | |
|
41 | """ Yield all 'value' targets, without priority """ | |
|
42 | for val in self.dispatch(key): | |
|
43 | for el in val: | |
|
44 | yield el[1] # only value, no priority | |
|
45 | return | |
|
46 | ||
|
47 | ||
|
48 | def test(): | |
|
49 | d = StrDispatch() | |
|
50 | d.add_s('hei',34, priority = 4) | |
|
51 | d.add_s('hei',123, priority = 2) | |
|
52 | print list(d.dispatch('hei')) | |
|
53 | d.add_re('h.i', 686) | |
|
54 | print list(d.flat_matches('hei')) | |
|
55 | ||
|
56 | if __name__ == '__main__': | |
|
57 | test() No newline at end of file |
@@ -1,72 +1,72 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | IPython -- An enhanced Interactive Python |
|
4 | 4 | |
|
5 | 5 | One of Python's nicest features is its interactive interpreter. This allows |
|
6 | 6 | very fast testing of ideas without the overhead of creating test files as is |
|
7 | 7 | typical in most programming languages. However, the interpreter supplied with |
|
8 | 8 | the standard Python distribution is fairly primitive (and IDLE isn't really |
|
9 | 9 | much better). |
|
10 | 10 | |
|
11 | 11 | IPython tries to: |
|
12 | 12 | |
|
13 | 13 | i - provide an efficient environment for interactive work in Python |
|
14 | 14 | programming. It tries to address what we see as shortcomings of the standard |
|
15 | 15 | Python prompt, and adds many features to make interactive work much more |
|
16 | 16 | efficient. |
|
17 | 17 | |
|
18 | 18 | ii - offer a flexible framework so that it can be used as the base |
|
19 | 19 | environment for other projects and problems where Python can be the |
|
20 | 20 | underlying language. Specifically scientific environments like Mathematica, |
|
21 | 21 | IDL and Mathcad inspired its design, but similar ideas can be useful in many |
|
22 | 22 | fields. Python is a fabulous language for implementing this kind of system |
|
23 | 23 | (due to its dynamic and introspective features), and with suitable libraries |
|
24 | 24 | entire systems could be built leveraging Python's power. |
|
25 | 25 | |
|
26 | 26 | iii - serve as an embeddable, ready to go interpreter for your own programs. |
|
27 | 27 | |
|
28 | 28 | IPython requires Python 2.3 or newer. |
|
29 | 29 | |
|
30 |
$Id: __init__.py 1 |
|
|
30 | $Id: __init__.py 1854 2006-10-30 19:54:25Z vivainio $""" | |
|
31 | 31 | |
|
32 | 32 | #***************************************************************************** |
|
33 | 33 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
34 | 34 | # |
|
35 | 35 | # Distributed under the terms of the BSD License. The full license is in |
|
36 | 36 | # the file COPYING, distributed as part of this software. |
|
37 | 37 | #***************************************************************************** |
|
38 | 38 | |
|
39 | 39 | # Enforce proper version requirements |
|
40 | 40 | import sys |
|
41 | 41 | |
|
42 | 42 | if sys.version[0:3] < '2.3': |
|
43 | 43 | raise ImportError('Python Version 2.3 or above is required for IPython.') |
|
44 | 44 | |
|
45 | 45 | # Make it easy to import extensions - they are always directly on pythonpath. |
|
46 | 46 | # Therefore, non-IPython modules can be added to Extensions directory |
|
47 | 47 | import os |
|
48 | 48 | sys.path.append(os.path.dirname(__file__) + "/Extensions") |
|
49 | 49 | |
|
50 | 50 | # Define what gets imported with a 'from IPython import *' |
|
51 | 51 | __all__ = ['deep_reload','genutils','ipstruct','ultraTB','DPyGetOpt', |
|
52 | 52 | 'Itpl','hooks','ConfigLoader','OutputTrap','Release','Shell', |
|
53 | 53 | 'platutils','platutils_win32','platutils_posix','platutils_dummy', |
|
54 | 'ipapi','rlineimpl'] | |
|
54 | 'ipapi','rlineimpl', 'strdispatch'] | |
|
55 | 55 | |
|
56 | 56 | # Load __all__ in IPython namespace so that a simple 'import IPython' gives |
|
57 | 57 | # access to them via IPython.<name> |
|
58 | 58 | glob,loc = globals(),locals() |
|
59 | 59 | for name in __all__: |
|
60 | 60 | __import__(name,glob,loc,[]) |
|
61 | 61 | |
|
62 | 62 | # Release data |
|
63 | 63 | from IPython import Release # do it explicitly so pydoc can see it - pydoc bug |
|
64 | 64 | __author__ = '%s <%s>\n%s <%s>\n%s <%s>' % \ |
|
65 | 65 | ( Release.authors['Fernando'] + Release.authors['Janko'] + \ |
|
66 | 66 | Release.authors['Nathan'] ) |
|
67 | 67 | __license__ = Release.license |
|
68 | 68 | __version__ = Release.version |
|
69 | 69 | __revision__ = Release.revision |
|
70 | 70 | |
|
71 | 71 | # Namespace cleanup |
|
72 | 72 | del name,glob,loc |
@@ -1,581 +1,608 b'' | |||
|
1 | 1 | """Word completion for IPython. |
|
2 | 2 | |
|
3 | 3 | This module is a fork of the rlcompleter module in the Python standard |
|
4 | 4 | library. The original enhancements made to rlcompleter have been sent |
|
5 | 5 | upstream and were accepted as of Python 2.3, but we need a lot more |
|
6 | 6 | functionality specific to IPython, so this module will continue to live as an |
|
7 | 7 | IPython-specific utility. |
|
8 | 8 | |
|
9 | 9 | --------------------------------------------------------------------------- |
|
10 | 10 | Original rlcompleter documentation: |
|
11 | 11 | |
|
12 | 12 | This requires the latest extension to the readline module (the |
|
13 | 13 | completes keywords, built-ins and globals in __main__; when completing |
|
14 | 14 | NAME.NAME..., it evaluates (!) the expression up to the last dot and |
|
15 | 15 | completes its attributes. |
|
16 | 16 | |
|
17 | 17 | It's very cool to do "import string" type "string.", hit the |
|
18 | 18 | completion key (twice), and see the list of names defined by the |
|
19 | 19 | string module! |
|
20 | 20 | |
|
21 | 21 | Tip: to use the tab key as the completion key, call |
|
22 | 22 | |
|
23 | 23 | readline.parse_and_bind("tab: complete") |
|
24 | 24 | |
|
25 | 25 | Notes: |
|
26 | 26 | |
|
27 | 27 | - Exceptions raised by the completer function are *ignored* (and |
|
28 | 28 | generally cause the completion to fail). This is a feature -- since |
|
29 | 29 | readline sets the tty device in raw (or cbreak) mode, printing a |
|
30 | 30 | traceback wouldn't work well without some complicated hoopla to save, |
|
31 | 31 | reset and restore the tty state. |
|
32 | 32 | |
|
33 | 33 | - The evaluation of the NAME.NAME... form may cause arbitrary |
|
34 | 34 | application defined code to be executed if an object with a |
|
35 | 35 | __getattr__ hook is found. Since it is the responsibility of the |
|
36 | 36 | application (or the user) to enable this feature, I consider this an |
|
37 | 37 | acceptable risk. More complicated expressions (e.g. function calls or |
|
38 | 38 | indexing operations) are *not* evaluated. |
|
39 | 39 | |
|
40 | 40 | - GNU readline is also used by the built-in functions input() and |
|
41 | 41 | raw_input(), and thus these also benefit/suffer from the completer |
|
42 | 42 | features. Clearly an interactive application can benefit by |
|
43 | 43 | specifying its own completer function and using raw_input() for all |
|
44 | 44 | its input. |
|
45 | 45 | |
|
46 | 46 | - When the original stdin is not a tty device, GNU readline is never |
|
47 | 47 | used, and this module (and the readline module) are silently inactive. |
|
48 | 48 | |
|
49 | 49 | """ |
|
50 | 50 | |
|
51 | 51 | #***************************************************************************** |
|
52 | 52 | # |
|
53 | 53 | # Since this file is essentially a minimally modified copy of the rlcompleter |
|
54 | 54 | # module which is part of the standard Python distribution, I assume that the |
|
55 | 55 | # proper procedure is to maintain its copyright as belonging to the Python |
|
56 | 56 | # Software Foundation (in addition to my own, for all new code). |
|
57 | 57 | # |
|
58 | 58 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
59 | 59 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
60 | 60 | # |
|
61 | 61 | # Distributed under the terms of the BSD License. The full license is in |
|
62 | 62 | # the file COPYING, distributed as part of this software. |
|
63 | 63 | # |
|
64 | 64 | #***************************************************************************** |
|
65 | 65 | |
|
66 | 66 | import __builtin__ |
|
67 | 67 | import __main__ |
|
68 | 68 | import glob |
|
69 | 69 | import keyword |
|
70 | 70 | import os |
|
71 | 71 | import re |
|
72 | 72 | import shlex |
|
73 | 73 | import sys |
|
74 | 74 | import IPython.rlineimpl as readline |
|
75 | from IPython.ipstruct import Struct | |
|
76 | from IPython import ipapi | |
|
75 | 77 | |
|
76 | 78 | import types |
|
77 | 79 | |
|
78 | 80 | # Python 2.4 offers sets as a builtin |
|
79 | 81 | try: |
|
80 | 82 | set([1,2]) |
|
81 | 83 | except NameError: |
|
82 | 84 | from sets import Set as set |
|
83 | 85 | |
|
84 | 86 | from IPython.genutils import debugx |
|
85 | 87 | |
|
86 | 88 | __all__ = ['Completer','IPCompleter'] |
|
87 | 89 | |
|
88 | 90 | def get_class_members(cls): |
|
89 | 91 | ret = dir(cls) |
|
90 | 92 | if hasattr(cls,'__bases__'): |
|
91 | 93 | for base in cls.__bases__: |
|
92 | 94 | ret.extend(get_class_members(base)) |
|
93 | 95 | return ret |
|
94 | 96 | |
|
95 | 97 | class Completer: |
|
96 | 98 | def __init__(self,namespace=None,global_namespace=None): |
|
97 | 99 | """Create a new completer for the command line. |
|
98 | 100 | |
|
99 | 101 | Completer([namespace,global_namespace]) -> completer instance. |
|
100 | 102 | |
|
101 | 103 | If unspecified, the default namespace where completions are performed |
|
102 | 104 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
103 | 105 | given as dictionaries. |
|
104 | 106 | |
|
105 | 107 | An optional second namespace can be given. This allows the completer |
|
106 | 108 | to handle cases where both the local and global scopes need to be |
|
107 | 109 | distinguished. |
|
108 | 110 | |
|
109 | 111 | Completer instances should be used as the completion mechanism of |
|
110 | 112 | readline via the set_completer() call: |
|
111 | 113 | |
|
112 | 114 | readline.set_completer(Completer(my_namespace).complete) |
|
113 | 115 | """ |
|
114 | 116 | |
|
115 | 117 | # some minimal strict typechecks. For some core data structures, I |
|
116 | 118 | # want actual basic python types, not just anything that looks like |
|
117 | 119 | # one. This is especially true for namespaces. |
|
118 | 120 | for ns in (namespace,global_namespace): |
|
119 | 121 | if ns is not None and type(ns) != types.DictType: |
|
120 | 122 | raise TypeError,'namespace must be a dictionary' |
|
121 | 123 | |
|
122 | 124 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
123 | 125 | # specific namespace or to use __main__.__dict__. This will allow us |
|
124 | 126 | # to bind to __main__.__dict__ at completion time, not now. |
|
125 | 127 | if namespace is None: |
|
126 | 128 | self.use_main_ns = 1 |
|
127 | 129 | else: |
|
128 | 130 | self.use_main_ns = 0 |
|
129 | 131 | self.namespace = namespace |
|
130 | 132 | |
|
131 | 133 | # The global namespace, if given, can be bound directly |
|
132 | 134 | if global_namespace is None: |
|
133 | 135 | self.global_namespace = {} |
|
134 | 136 | else: |
|
135 | 137 | self.global_namespace = global_namespace |
|
136 | 138 | |
|
137 | 139 | def complete(self, text, state): |
|
138 | 140 | """Return the next possible completion for 'text'. |
|
139 | 141 | |
|
140 | 142 | This is called successively with state == 0, 1, 2, ... until it |
|
141 | 143 | returns None. The completion should begin with 'text'. |
|
142 | 144 | |
|
143 | 145 | """ |
|
144 | 146 | if self.use_main_ns: |
|
145 | 147 | self.namespace = __main__.__dict__ |
|
146 | 148 | |
|
147 | 149 | if state == 0: |
|
148 | 150 | if "." in text: |
|
149 | 151 | self.matches = self.attr_matches(text) |
|
150 | 152 | else: |
|
151 | 153 | self.matches = self.global_matches(text) |
|
152 | 154 | try: |
|
153 | 155 | return self.matches[state] |
|
154 | 156 | except IndexError: |
|
155 | 157 | return None |
|
156 | 158 | |
|
157 | 159 | def global_matches(self, text): |
|
158 | 160 | """Compute matches when text is a simple name. |
|
159 | 161 | |
|
160 | 162 | Return a list of all keywords, built-in functions and names currently |
|
161 | 163 | defined in self.namespace or self.global_namespace that match. |
|
162 | 164 | |
|
163 | 165 | """ |
|
164 | 166 | matches = [] |
|
165 | 167 | match_append = matches.append |
|
166 | 168 | n = len(text) |
|
167 | 169 | for lst in [keyword.kwlist, |
|
168 | 170 | __builtin__.__dict__.keys(), |
|
169 | 171 | self.namespace.keys(), |
|
170 | 172 | self.global_namespace.keys()]: |
|
171 | 173 | for word in lst: |
|
172 | 174 | if word[:n] == text and word != "__builtins__": |
|
173 | 175 | match_append(word) |
|
174 | 176 | return matches |
|
175 | 177 | |
|
176 | 178 | def attr_matches(self, text): |
|
177 | 179 | """Compute matches when text contains a dot. |
|
178 | 180 | |
|
179 | 181 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
180 | 182 | evaluatable in self.namespace or self.global_namespace, it will be |
|
181 | 183 | evaluated and its attributes (as revealed by dir()) are used as |
|
182 | 184 | possible completions. (For class instances, class members are are |
|
183 | 185 | also considered.) |
|
184 | 186 | |
|
185 | 187 | WARNING: this can still invoke arbitrary C code, if an object |
|
186 | 188 | with a __getattr__ hook is evaluated. |
|
187 | 189 | |
|
188 | 190 | """ |
|
189 | 191 | import re |
|
190 | 192 | |
|
191 | 193 | # Another option, seems to work great. Catches things like ''.<tab> |
|
192 | 194 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
193 | 195 | |
|
194 | 196 | if not m: |
|
195 | 197 | return [] |
|
196 | 198 | |
|
197 | 199 | expr, attr = m.group(1, 3) |
|
198 | 200 | try: |
|
199 | 201 | object = eval(expr, self.namespace) |
|
200 | 202 | except: |
|
201 | 203 | object = eval(expr, self.global_namespace) |
|
202 | 204 | |
|
203 | 205 | # Start building the attribute list via dir(), and then complete it |
|
204 | 206 | # with a few extra special-purpose calls. |
|
205 | 207 | words = dir(object) |
|
206 | 208 | |
|
207 | 209 | if hasattr(object,'__class__'): |
|
208 | 210 | words.append('__class__') |
|
209 | 211 | words.extend(get_class_members(object.__class__)) |
|
210 | 212 | |
|
211 | 213 | # this is the 'dir' function for objects with Enthought's traits |
|
212 | 214 | if hasattr(object, 'trait_names'): |
|
213 | 215 | try: |
|
214 | 216 | words.extend(object.trait_names()) |
|
215 | 217 | # eliminate possible duplicates, as some traits may also |
|
216 | 218 | # appear as normal attributes in the dir() call. |
|
217 | 219 | words = set(words) |
|
218 | 220 | except TypeError: |
|
219 | 221 | # This will happen if `object` is a class and not an instance. |
|
220 | 222 | pass |
|
221 | 223 | |
|
222 | 224 | # Support for PyCrust-style _getAttributeNames magic method. |
|
223 | 225 | if hasattr(object, '_getAttributeNames'): |
|
224 | 226 | try: |
|
225 | 227 | words.extend(object._getAttributeNames()) |
|
226 | 228 | # Eliminate duplicates. |
|
227 | 229 | words = set(words) |
|
228 | 230 | except TypeError: |
|
229 | 231 | # `object` is a class and not an instance. Ignore |
|
230 | 232 | # this error. |
|
231 | 233 | pass |
|
232 | 234 | |
|
233 | 235 | # filter out non-string attributes which may be stuffed by dir() calls |
|
234 | 236 | # and poor coding in third-party modules |
|
235 | 237 | words = [w for w in words |
|
236 | 238 | if isinstance(w, basestring) and w != "__builtins__"] |
|
237 | 239 | # Build match list to return |
|
238 | 240 | n = len(attr) |
|
239 | 241 | return ["%s.%s" % (expr, w) for w in words if w[:n] == attr ] |
|
240 | 242 | |
|
241 | 243 | class IPCompleter(Completer): |
|
242 | 244 | """Extension of the completer class with IPython-specific features""" |
|
243 | 245 | |
|
244 | 246 | def __init__(self,shell,namespace=None,global_namespace=None, |
|
245 | 247 | omit__names=0,alias_table=None): |
|
246 | 248 | """IPCompleter() -> completer |
|
247 | 249 | |
|
248 | 250 | Return a completer object suitable for use by the readline library |
|
249 | 251 | via readline.set_completer(). |
|
250 | 252 | |
|
251 | 253 | Inputs: |
|
252 | 254 | |
|
253 | 255 | - shell: a pointer to the ipython shell itself. This is needed |
|
254 | 256 | because this completer knows about magic functions, and those can |
|
255 | 257 | only be accessed via the ipython instance. |
|
256 | 258 | |
|
257 | 259 | - namespace: an optional dict where completions are performed. |
|
258 | 260 | |
|
259 | 261 | - global_namespace: secondary optional dict for completions, to |
|
260 | 262 | handle cases (such as IPython embedded inside functions) where |
|
261 | 263 | both Python scopes are visible. |
|
262 | 264 | |
|
263 | 265 | - The optional omit__names parameter sets the completer to omit the |
|
264 | 266 | 'magic' names (__magicname__) for python objects unless the text |
|
265 | 267 | to be completed explicitly starts with one or more underscores. |
|
266 | 268 | |
|
267 | 269 | - If alias_table is supplied, it should be a dictionary of aliases |
|
268 | 270 | to complete. """ |
|
269 | 271 | |
|
270 | 272 | Completer.__init__(self,namespace,global_namespace) |
|
271 | 273 | self.magic_prefix = shell.name+'.magic_' |
|
272 | 274 | self.magic_escape = shell.ESC_MAGIC |
|
273 | 275 | self.readline = readline |
|
274 | 276 | delims = self.readline.get_completer_delims() |
|
275 | 277 | delims = delims.replace(self.magic_escape,'') |
|
276 | 278 | self.readline.set_completer_delims(delims) |
|
277 | 279 | self.get_line_buffer = self.readline.get_line_buffer |
|
278 | 280 | self.omit__names = omit__names |
|
279 | 281 | self.merge_completions = shell.rc.readline_merge_completions |
|
280 | 282 | |
|
281 | 283 | if alias_table is None: |
|
282 | 284 | alias_table = {} |
|
283 | 285 | self.alias_table = alias_table |
|
284 | 286 | # Regexp to split filenames with spaces in them |
|
285 | 287 | self.space_name_re = re.compile(r'([^\\] )') |
|
286 | 288 | # Hold a local ref. to glob.glob for speed |
|
287 | 289 | self.glob = glob.glob |
|
288 | 290 | |
|
289 | 291 | # Determine if we are running on 'dumb' terminals, like (X)Emacs |
|
290 | 292 | # buffers, to avoid completion problems. |
|
291 | 293 | term = os.environ.get('TERM','xterm') |
|
292 | 294 | self.dumb_terminal = term in ['dumb','emacs'] |
|
293 | 295 | |
|
294 | 296 | # Special handling of backslashes needed in win32 platforms |
|
295 | 297 | if sys.platform == "win32": |
|
296 | 298 | self.clean_glob = self._clean_glob_win32 |
|
297 | 299 | else: |
|
298 | 300 | self.clean_glob = self._clean_glob |
|
299 | 301 | self.matchers = [self.python_matches, |
|
300 | 302 | self.file_matches, |
|
301 | 303 | self.alias_matches, |
|
302 | 304 | self.python_func_kw_matches] |
|
303 | 305 | |
|
304 | 306 | # Code contributed by Alex Schmolck, for ipython/emacs integration |
|
305 | 307 | def all_completions(self, text): |
|
306 | 308 | """Return all possible completions for the benefit of emacs.""" |
|
307 | 309 | |
|
308 | 310 | completions = [] |
|
309 | 311 | comp_append = completions.append |
|
310 | 312 | try: |
|
311 | 313 | for i in xrange(sys.maxint): |
|
312 | 314 | res = self.complete(text, i) |
|
313 | 315 | |
|
314 | 316 | if not res: break |
|
315 | 317 | |
|
316 | 318 | comp_append(res) |
|
317 | 319 | #XXX workaround for ``notDefined.<tab>`` |
|
318 | 320 | except NameError: |
|
319 | 321 | pass |
|
320 | 322 | return completions |
|
321 | 323 | # /end Alex Schmolck code. |
|
322 | 324 | |
|
323 | 325 | def _clean_glob(self,text): |
|
324 | 326 | return self.glob("%s*" % text) |
|
325 | 327 | |
|
326 | 328 | def _clean_glob_win32(self,text): |
|
327 | 329 | return [f.replace("\\","/") |
|
328 | 330 | for f in self.glob("%s*" % text)] |
|
329 | 331 | |
|
330 | 332 | def file_matches(self, text): |
|
331 | 333 | """Match filneames, expanding ~USER type strings. |
|
332 | 334 | |
|
333 | 335 | Most of the seemingly convoluted logic in this completer is an |
|
334 | 336 | attempt to handle filenames with spaces in them. And yet it's not |
|
335 | 337 | quite perfect, because Python's readline doesn't expose all of the |
|
336 | 338 | GNU readline details needed for this to be done correctly. |
|
337 | 339 | |
|
338 | 340 | For a filename with a space in it, the printed completions will be |
|
339 | 341 | only the parts after what's already been typed (instead of the |
|
340 | 342 | full completions, as is normally done). I don't think with the |
|
341 | 343 | current (as of Python 2.3) Python readline it's possible to do |
|
342 | 344 | better.""" |
|
343 | 345 | |
|
344 | 346 | #print 'Completer->file_matches: <%s>' % text # dbg |
|
345 | 347 | |
|
346 | 348 | # chars that require escaping with backslash - i.e. chars |
|
347 | 349 | # that readline treats incorrectly as delimiters, but we |
|
348 | 350 | # don't want to treat as delimiters in filename matching |
|
349 | 351 | # when escaped with backslash |
|
350 | 352 | |
|
351 | 353 | protectables = ' ()[]{}' |
|
352 | 354 | |
|
353 | 355 | def protect_filename(s): |
|
354 | 356 | return "".join([(ch in protectables and '\\' + ch or ch) |
|
355 | 357 | for ch in s]) |
|
356 | 358 | |
|
357 | 359 | lbuf = self.lbuf |
|
358 | 360 | open_quotes = 0 # track strings with open quotes |
|
359 | 361 | try: |
|
360 | 362 | lsplit = shlex.split(lbuf)[-1] |
|
361 | 363 | except ValueError: |
|
362 | 364 | # typically an unmatched ", or backslash without escaped char. |
|
363 | 365 | if lbuf.count('"')==1: |
|
364 | 366 | open_quotes = 1 |
|
365 | 367 | lsplit = lbuf.split('"')[-1] |
|
366 | 368 | elif lbuf.count("'")==1: |
|
367 | 369 | open_quotes = 1 |
|
368 | 370 | lsplit = lbuf.split("'")[-1] |
|
369 | 371 | else: |
|
370 | 372 | return None |
|
371 | 373 | except IndexError: |
|
372 | 374 | # tab pressed on empty line |
|
373 | 375 | lsplit = "" |
|
374 | 376 | |
|
375 | 377 | if lsplit != protect_filename(lsplit): |
|
376 | 378 | # if protectables are found, do matching on the whole escaped |
|
377 | 379 | # name |
|
378 | 380 | has_protectables = 1 |
|
379 | 381 | text0,text = text,lsplit |
|
380 | 382 | else: |
|
381 | 383 | has_protectables = 0 |
|
382 | 384 | text = os.path.expanduser(text) |
|
383 | 385 | |
|
384 | 386 | if text == "": |
|
385 | 387 | return [protect_filename(f) for f in self.glob("*")] |
|
386 | 388 | |
|
387 | 389 | m0 = self.clean_glob(text.replace('\\','')) |
|
388 | 390 | if has_protectables: |
|
389 | 391 | # If we had protectables, we need to revert our changes to the |
|
390 | 392 | # beginning of filename so that we don't double-write the part |
|
391 | 393 | # of the filename we have so far |
|
392 | 394 | len_lsplit = len(lsplit) |
|
393 | 395 | matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0] |
|
394 | 396 | else: |
|
395 | 397 | if open_quotes: |
|
396 | 398 | # if we have a string with an open quote, we don't need to |
|
397 | 399 | # protect the names at all (and we _shouldn't_, as it |
|
398 | 400 | # would cause bugs when the filesystem call is made). |
|
399 | 401 | matches = m0 |
|
400 | 402 | else: |
|
401 | 403 | matches = [protect_filename(f) for f in m0] |
|
402 | 404 | if len(matches) == 1 and os.path.isdir(matches[0]): |
|
403 | 405 | # Takes care of links to directories also. Use '/' |
|
404 | 406 | # explicitly, even under Windows, so that name completions |
|
405 | 407 | # don't end up escaped. |
|
406 | 408 | matches[0] += '/' |
|
407 | 409 | return matches |
|
408 | 410 | |
|
409 | 411 | def alias_matches(self, text): |
|
410 | 412 | """Match internal system aliases""" |
|
411 | 413 | #print 'Completer->alias_matches:',text,'lb',self.lbuf # dbg |
|
412 | 414 | |
|
413 | 415 | # if we are not in the first 'item', alias matching |
|
414 | 416 | # doesn't make sense |
|
415 | 417 | if ' ' in self.lbuf: |
|
416 | 418 | return [] |
|
417 | 419 | text = os.path.expanduser(text) |
|
418 | 420 | aliases = self.alias_table.keys() |
|
419 | 421 | if text == "": |
|
420 | 422 | return aliases |
|
421 | 423 | else: |
|
422 | 424 | return [alias for alias in aliases if alias.startswith(text)] |
|
423 | 425 | |
|
424 | 426 | def python_matches(self,text): |
|
425 | 427 | """Match attributes or global python names""" |
|
426 | 428 | |
|
427 | 429 | #print 'Completer->python_matches, txt=<%s>' % text # dbg |
|
428 | 430 | if "." in text: |
|
429 | 431 | try: |
|
430 | 432 | matches = self.attr_matches(text) |
|
431 | 433 | if text.endswith('.') and self.omit__names: |
|
432 | 434 | if self.omit__names == 1: |
|
433 | 435 | # true if txt is _not_ a __ name, false otherwise: |
|
434 | 436 | no__name = (lambda txt: |
|
435 | 437 | re.match(r'.*\.__.*?__',txt) is None) |
|
436 | 438 | else: |
|
437 | 439 | # true if txt is _not_ a _ name, false otherwise: |
|
438 | 440 | no__name = (lambda txt: |
|
439 | 441 | re.match(r'.*\._.*?',txt) is None) |
|
440 | 442 | matches = filter(no__name, matches) |
|
441 | 443 | except NameError: |
|
442 | 444 | # catches <undefined attributes>.<tab> |
|
443 | 445 | matches = [] |
|
444 | 446 | else: |
|
445 | 447 | matches = self.global_matches(text) |
|
446 | 448 | # this is so completion finds magics when automagic is on: |
|
447 | 449 | if (matches == [] and |
|
448 | 450 | not text.startswith(os.sep) and |
|
449 | 451 | not ' ' in self.lbuf): |
|
450 | 452 | matches = self.attr_matches(self.magic_prefix+text) |
|
451 | 453 | return matches |
|
452 | 454 | |
|
453 | 455 | def _default_arguments(self, obj): |
|
454 | 456 | """Return the list of default arguments of obj if it is callable, |
|
455 | 457 | or empty list otherwise.""" |
|
456 | 458 | |
|
457 | 459 | if not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
458 | 460 | # for classes, check for __init__,__new__ |
|
459 | 461 | if inspect.isclass(obj): |
|
460 | 462 | obj = (getattr(obj,'__init__',None) or |
|
461 | 463 | getattr(obj,'__new__',None)) |
|
462 | 464 | # for all others, check if they are __call__able |
|
463 | 465 | elif hasattr(obj, '__call__'): |
|
464 | 466 | obj = obj.__call__ |
|
465 | 467 | # XXX: is there a way to handle the builtins ? |
|
466 | 468 | try: |
|
467 | 469 | args,_,_1,defaults = inspect.getargspec(obj) |
|
468 | 470 | if defaults: |
|
469 | 471 | return args[-len(defaults):] |
|
470 | 472 | except TypeError: pass |
|
471 | 473 | return [] |
|
472 | 474 | |
|
473 | 475 | def python_func_kw_matches(self,text): |
|
474 | 476 | """Match named parameters (kwargs) of the last open function""" |
|
475 | 477 | |
|
476 | 478 | if "." in text: # a parameter cannot be dotted |
|
477 | 479 | return [] |
|
478 | 480 | try: regexp = self.__funcParamsRegex |
|
479 | 481 | except AttributeError: |
|
480 | 482 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
481 | 483 | '.*?' | # single quoted strings or |
|
482 | 484 | ".*?" | # double quoted strings or |
|
483 | 485 | \w+ | # identifier |
|
484 | 486 | \S # other characters |
|
485 | 487 | ''', re.VERBOSE | re.DOTALL) |
|
486 | 488 | # 1. find the nearest identifier that comes before an unclosed |
|
487 | 489 | # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo" |
|
488 | 490 | tokens = regexp.findall(self.get_line_buffer()) |
|
489 | 491 | tokens.reverse() |
|
490 | 492 | iterTokens = iter(tokens); openPar = 0 |
|
491 | 493 | for token in iterTokens: |
|
492 | 494 | if token == ')': |
|
493 | 495 | openPar -= 1 |
|
494 | 496 | elif token == '(': |
|
495 | 497 | openPar += 1 |
|
496 | 498 | if openPar > 0: |
|
497 | 499 | # found the last unclosed parenthesis |
|
498 | 500 | break |
|
499 | 501 | else: |
|
500 | 502 | return [] |
|
501 | 503 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) |
|
502 | 504 | ids = [] |
|
503 | 505 | isId = re.compile(r'\w+$').match |
|
504 | 506 | while True: |
|
505 | 507 | try: |
|
506 | 508 | ids.append(iterTokens.next()) |
|
507 | 509 | if not isId(ids[-1]): |
|
508 | 510 | ids.pop(); break |
|
509 | 511 | if not iterTokens.next() == '.': |
|
510 | 512 | break |
|
511 | 513 | except StopIteration: |
|
512 | 514 | break |
|
513 | 515 | # lookup the candidate callable matches either using global_matches |
|
514 | 516 | # or attr_matches for dotted names |
|
515 | 517 | if len(ids) == 1: |
|
516 | 518 | callableMatches = self.global_matches(ids[0]) |
|
517 | 519 | else: |
|
518 | 520 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
519 | 521 | argMatches = [] |
|
520 | 522 | for callableMatch in callableMatches: |
|
521 | 523 | try: namedArgs = self._default_arguments(eval(callableMatch, |
|
522 | 524 | self.namespace)) |
|
523 | 525 | except: continue |
|
524 | 526 | for namedArg in namedArgs: |
|
525 | 527 | if namedArg.startswith(text): |
|
526 | 528 | argMatches.append("%s=" %namedArg) |
|
527 | 529 | return argMatches |
|
528 | 530 | |
|
531 | def dispatch_custom_completer(self,text): | |
|
532 | # print "Custom! '%s' %s" % (text, self.custom_completers) # dbg | |
|
533 | line = self.lbuf | |
|
534 | event = Struct() | |
|
535 | event.line = line | |
|
536 | event.symbol = text | |
|
537 | event.command = None | |
|
538 | for c in self.custom_completers.flat_matches(self.lbuf): | |
|
539 | # print "try",c # dbg | |
|
540 | try: | |
|
541 | res = c(event) | |
|
542 | return [r for r in res if r.startswith(text)] | |
|
543 | except ipapi.TryNext: | |
|
544 | pass | |
|
545 | ||
|
546 | return None | |
|
547 | ||
|
548 | ||
|
549 | ||
|
529 | 550 | def complete(self, text, state): |
|
530 | 551 | """Return the next possible completion for 'text'. |
|
531 | 552 | |
|
532 | 553 | This is called successively with state == 0, 1, 2, ... until it |
|
533 | 554 | returns None. The completion should begin with 'text'. """ |
|
534 | 555 | |
|
535 | 556 | #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg |
|
536 | 557 | |
|
537 | 558 | # if there is only a tab on a line with only whitespace, instead |
|
538 | 559 | # of the mostly useless 'do you want to see all million |
|
539 | 560 | # completions' message, just do the right thing and give the user |
|
540 | 561 | # his tab! Incidentally, this enables pasting of tabbed text from |
|
541 | 562 | # an editor (as long as autoindent is off). |
|
542 | 563 | |
|
543 | 564 | # don't apply this on 'dumb' terminals, such as emacs buffers, so we |
|
544 | 565 | # don't interfere with their own tab-completion mechanism. |
|
545 | 566 | self.lbuf = self.get_line_buffer()[:self.readline.get_endidx()] |
|
546 | 567 | if not (self.dumb_terminal or self.get_line_buffer().strip()): |
|
547 | 568 | self.readline.insert_text('\t') |
|
548 | 569 | return None |
|
549 | 570 | |
|
571 | ||
|
550 | 572 | magic_escape = self.magic_escape |
|
551 | 573 | magic_prefix = self.magic_prefix |
|
552 | 574 | |
|
553 | 575 | try: |
|
554 | 576 | if text.startswith(magic_escape): |
|
555 | 577 | text = text.replace(magic_escape,magic_prefix) |
|
556 | 578 | elif text.startswith('~'): |
|
557 | 579 | text = os.path.expanduser(text) |
|
558 | 580 | if state == 0: |
|
581 | custom_res = self.dispatch_custom_completer(text) | |
|
582 | if custom_res is not None: | |
|
583 | # did custom completers produce something? | |
|
584 | self.matches = custom_res | |
|
585 | else: | |
|
559 | 586 | # Extend the list of completions with the results of each |
|
560 | 587 | # matcher, so we return results to the user from all |
|
561 | 588 | # namespaces. |
|
562 | 589 | if self.merge_completions: |
|
563 | 590 | self.matches = [] |
|
564 | 591 | for matcher in self.matchers: |
|
565 | 592 | self.matches.extend(matcher(text)) |
|
566 | 593 | else: |
|
567 | 594 | for matcher in self.matchers: |
|
568 | 595 | self.matches = matcher(text) |
|
569 | 596 | if self.matches: |
|
570 | 597 | break |
|
571 | 598 | |
|
572 | 599 | try: |
|
573 | 600 | return self.matches[state].replace(magic_prefix,magic_escape) |
|
574 | 601 | except IndexError: |
|
575 | 602 | return None |
|
576 | 603 | except: |
|
577 |
|
|
|
578 |
|
|
|
604 | from IPython.ultraTB import AutoFormattedTB; # dbg | |
|
605 | tb=AutoFormattedTB('Verbose');tb() #dbg | |
|
579 | 606 | |
|
580 | 607 | # If completion fails, don't annoy the user. |
|
581 | 608 | return None |
@@ -1,210 +1,217 b'' | |||
|
1 | 1 | """hooks for IPython. |
|
2 | 2 | |
|
3 | 3 | In Python, it is possible to overwrite any method of any object if you really |
|
4 | 4 | want to. But IPython exposes a few 'hooks', methods which are _designed_ to |
|
5 | 5 | be overwritten by users for customization purposes. This module defines the |
|
6 | 6 | default versions of all such hooks, which get used by IPython if not |
|
7 | 7 | overridden by the user. |
|
8 | 8 | |
|
9 | 9 | hooks are simple functions, but they should be declared with 'self' as their |
|
10 | 10 | first argument, because when activated they are registered into IPython as |
|
11 | 11 | instance methods. The self argument will be the IPython running instance |
|
12 | 12 | itself, so hooks have full access to the entire IPython object. |
|
13 | 13 | |
|
14 | 14 | If you wish to define a new hook and activate it, you need to put the |
|
15 | 15 | necessary code into a python file which can be either imported or execfile()'d |
|
16 | 16 | from within your ipythonrc configuration. |
|
17 | 17 | |
|
18 | 18 | For example, suppose that you have a module called 'myiphooks' in your |
|
19 | 19 | PYTHONPATH, which contains the following definition: |
|
20 | 20 | |
|
21 | 21 | import os |
|
22 | 22 | import IPython.ipapi |
|
23 | 23 | ip = IPython.ipapi.get() |
|
24 | 24 | |
|
25 | 25 | def calljed(self,filename, linenum): |
|
26 | 26 | "My editor hook calls the jed editor directly." |
|
27 | 27 | print "Calling my own editor, jed ..." |
|
28 | 28 | os.system('jed +%d %s' % (linenum,filename)) |
|
29 | 29 | |
|
30 | 30 | ip.set_hook('editor', calljed) |
|
31 | 31 | |
|
32 | 32 | You can then enable the functionality by doing 'import myiphooks' |
|
33 | 33 | somewhere in your configuration files or ipython command line. |
|
34 | 34 | |
|
35 |
$Id: hooks.py 1 |
|
|
35 | $Id: hooks.py 1854 2006-10-30 19:54:25Z vivainio $""" | |
|
36 | 36 | |
|
37 | 37 | #***************************************************************************** |
|
38 | 38 | # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu> |
|
39 | 39 | # |
|
40 | 40 | # Distributed under the terms of the BSD License. The full license is in |
|
41 | 41 | # the file COPYING, distributed as part of this software. |
|
42 | 42 | #***************************************************************************** |
|
43 | 43 | |
|
44 | 44 | from IPython import Release |
|
45 | 45 | from IPython import ipapi |
|
46 | 46 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
47 | 47 | __license__ = Release.license |
|
48 | 48 | __version__ = Release.version |
|
49 | 49 | |
|
50 | 50 | import os,bisect |
|
51 | 51 | from genutils import Term |
|
52 | 52 | from pprint import PrettyPrinter |
|
53 | 53 | |
|
54 | 54 | # List here all the default hooks. For now it's just the editor functions |
|
55 | 55 | # but over time we'll move here all the public API for user-accessible things. |
|
56 | 56 | __all__ = ['editor', 'fix_error_editor', 'result_display', |
|
57 | 57 | 'input_prefilter', 'shutdown_hook', 'late_startup_hook', |
|
58 | 58 | 'generate_prompt', 'generate_output_prompt' ] |
|
59 | 59 | |
|
60 | 60 | pformat = PrettyPrinter().pformat |
|
61 | 61 | |
|
62 | 62 | def editor(self,filename, linenum=None): |
|
63 | 63 | """Open the default editor at the given filename and linenumber. |
|
64 | 64 | |
|
65 | 65 | This is IPython's default editor hook, you can use it as an example to |
|
66 | 66 | write your own modified one. To set your own editor function as the |
|
67 | 67 | new editor hook, call ip.set_hook('editor',yourfunc).""" |
|
68 | 68 | |
|
69 | 69 | # IPython configures a default editor at startup by reading $EDITOR from |
|
70 | 70 | # the environment, and falling back on vi (unix) or notepad (win32). |
|
71 | 71 | editor = self.rc.editor |
|
72 | 72 | |
|
73 | 73 | # marker for at which line to open the file (for existing objects) |
|
74 | 74 | if linenum is None or editor=='notepad': |
|
75 | 75 | linemark = '' |
|
76 | 76 | else: |
|
77 | 77 | linemark = '+%d' % int(linenum) |
|
78 | 78 | |
|
79 | 79 | # Enclose in quotes if necessary and legal |
|
80 | 80 | if ' ' in editor and os.path.isfile(editor) and editor[0] != '"': |
|
81 | 81 | editor = '"%s"' % editor |
|
82 | 82 | |
|
83 | 83 | # Call the actual editor |
|
84 | 84 | os.system('%s %s %s' % (editor,linemark,filename)) |
|
85 | 85 | |
|
86 | 86 | import tempfile |
|
87 | 87 | def fix_error_editor(self,filename,linenum,column,msg): |
|
88 | 88 | """Open the editor at the given filename, linenumber, column and |
|
89 | 89 | show an error message. This is used for correcting syntax errors. |
|
90 | 90 | The current implementation only has special support for the VIM editor, |
|
91 | 91 | and falls back on the 'editor' hook if VIM is not used. |
|
92 | 92 | |
|
93 | 93 | Call ip.set_hook('fix_error_editor',youfunc) to use your own function, |
|
94 | 94 | """ |
|
95 | 95 | def vim_quickfix_file(): |
|
96 | 96 | t = tempfile.NamedTemporaryFile() |
|
97 | 97 | t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg)) |
|
98 | 98 | t.flush() |
|
99 | 99 | return t |
|
100 | 100 | if os.path.basename(self.rc.editor) != 'vim': |
|
101 | 101 | self.hooks.editor(filename,linenum) |
|
102 | 102 | return |
|
103 | 103 | t = vim_quickfix_file() |
|
104 | 104 | try: |
|
105 | 105 | os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name) |
|
106 | 106 | finally: |
|
107 | 107 | t.close() |
|
108 | 108 | |
|
109 | 109 | |
|
110 | 110 | class CommandChainDispatcher: |
|
111 | 111 | """ Dispatch calls to a chain of commands until some func can handle it |
|
112 | 112 | |
|
113 | 113 | Usage: instantiate, execute "add" to add commands (with optional |
|
114 | 114 | priority), execute normally via f() calling mechanism. |
|
115 | 115 | |
|
116 | 116 | """ |
|
117 | 117 | def __init__(self,commands=None): |
|
118 | 118 | if commands is None: |
|
119 | 119 | self.chain = [] |
|
120 | 120 | else: |
|
121 | 121 | self.chain = commands |
|
122 | 122 | |
|
123 | 123 | |
|
124 | 124 | def __call__(self,*args, **kw): |
|
125 | 125 | """ Command chain is called just like normal func. |
|
126 | 126 | |
|
127 | 127 | This will call all funcs in chain with the same args as were given to this |
|
128 | 128 | function, and return the result of first func that didn't raise |
|
129 | 129 | TryNext """ |
|
130 | 130 | |
|
131 | 131 | for prio,cmd in self.chain: |
|
132 | 132 | #print "prio",prio,"cmd",cmd #dbg |
|
133 | 133 | try: |
|
134 | 134 | ret = cmd(*args, **kw) |
|
135 | 135 | return ret |
|
136 | 136 | except ipapi.TryNext, exc: |
|
137 | 137 | if exc.args or exc.kwargs: |
|
138 | 138 | args = exc.args |
|
139 | 139 | kw = exc.kwargs |
|
140 | 140 | |
|
141 | 141 | def __str__(self): |
|
142 | 142 | return str(self.chain) |
|
143 | 143 | |
|
144 | 144 | def add(self, func, priority=0): |
|
145 | 145 | """ Add a func to the cmd chain with given priority """ |
|
146 | 146 | bisect.insort(self.chain,(priority,func)) |
|
147 | 147 | |
|
148 | def __iter__(self): | |
|
149 | """ Return all objects in chain. | |
|
150 | ||
|
151 | Handy if the objects are not callable. | |
|
152 | """ | |
|
153 | return iter(self.chain) | |
|
154 | ||
|
148 | 155 | def result_display(self,arg): |
|
149 | 156 | """ Default display hook. |
|
150 | 157 | |
|
151 | 158 | Called for displaying the result to the user. |
|
152 | 159 | """ |
|
153 | 160 | |
|
154 | 161 | if self.rc.pprint: |
|
155 | 162 | out = pformat(arg) |
|
156 | 163 | if '\n' in out: |
|
157 | 164 | # So that multi-line strings line up with the left column of |
|
158 | 165 | # the screen, instead of having the output prompt mess up |
|
159 | 166 | # their first line. |
|
160 | 167 | Term.cout.write('\n') |
|
161 | 168 | print >>Term.cout, out |
|
162 | 169 | else: |
|
163 | 170 | # By default, the interactive prompt uses repr() to display results, |
|
164 | 171 | # so we should honor this. Users who'd rather use a different |
|
165 | 172 | # mechanism can easily override this hook. |
|
166 | 173 | print >>Term.cout, repr(arg) |
|
167 | 174 | # the default display hook doesn't manipulate the value to put in history |
|
168 | 175 | return None |
|
169 | 176 | |
|
170 | 177 | def input_prefilter(self,line): |
|
171 | 178 | """ Default input prefilter |
|
172 | 179 | |
|
173 | 180 | This returns the line as unchanged, so that the interpreter |
|
174 | 181 | knows that nothing was done and proceeds with "classic" prefiltering |
|
175 | 182 | (%magics, !shell commands etc.). |
|
176 | 183 | |
|
177 | 184 | Note that leading whitespace is not passed to this hook. Prefilter |
|
178 | 185 | can't alter indentation. |
|
179 | 186 | |
|
180 | 187 | """ |
|
181 | 188 | #print "attempt to rewrite",line #dbg |
|
182 | 189 | return line |
|
183 | 190 | |
|
184 | 191 | def shutdown_hook(self): |
|
185 | 192 | """ default shutdown hook |
|
186 | 193 | |
|
187 | 194 | Typically, shotdown hooks should raise TryNext so all shutdown ops are done |
|
188 | 195 | """ |
|
189 | 196 | |
|
190 | 197 | #print "default shutdown hook ok" # dbg |
|
191 | 198 | return |
|
192 | 199 | |
|
193 | 200 | def late_startup_hook(self): |
|
194 | 201 | """ Executed after ipython has been constructed and configured |
|
195 | 202 | |
|
196 | 203 | """ |
|
197 | 204 | #print "default startup hook ok" # dbg |
|
198 | 205 | |
|
199 | 206 | def generate_prompt(self, is_continuation): |
|
200 | 207 | """ calculate and return a string with the prompt to display """ |
|
201 | 208 | ip = self.api |
|
202 | 209 | if is_continuation: |
|
203 | 210 | return str(ip.IP.outputcache.prompt2) |
|
204 | 211 | return str(ip.IP.outputcache.prompt1) |
|
205 | 212 | |
|
206 | 213 | def generate_output_prompt(self): |
|
207 | 214 | ip = self.api |
|
208 | 215 | return str(ip.IP.outputcache.prompt_out) |
|
209 | 216 | |
|
210 | 217 | No newline at end of file |
@@ -1,2442 +1,2461 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | IPython -- An enhanced Interactive Python |
|
4 | 4 | |
|
5 | 5 | Requires Python 2.3 or newer. |
|
6 | 6 | |
|
7 | 7 | This file contains all the classes and helper functions specific to IPython. |
|
8 | 8 | |
|
9 |
$Id: iplib.py 185 |
|
|
9 | $Id: iplib.py 1854 2006-10-30 19:54:25Z vivainio $ | |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
14 | 14 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
15 | 15 | # |
|
16 | 16 | # Distributed under the terms of the BSD License. The full license is in |
|
17 | 17 | # the file COPYING, distributed as part of this software. |
|
18 | 18 | # |
|
19 | 19 | # Note: this code originally subclassed code.InteractiveConsole from the |
|
20 | 20 | # Python standard library. Over time, all of that class has been copied |
|
21 | 21 | # verbatim here for modifications which could not be accomplished by |
|
22 | 22 | # subclassing. At this point, there are no dependencies at all on the code |
|
23 | 23 | # module anymore (it is not even imported). The Python License (sec. 2) |
|
24 | 24 | # allows for this, but it's always nice to acknowledge credit where credit is |
|
25 | 25 | # due. |
|
26 | 26 | #***************************************************************************** |
|
27 | 27 | |
|
28 | 28 | #**************************************************************************** |
|
29 | 29 | # Modules and globals |
|
30 | 30 | |
|
31 | 31 | from IPython import Release |
|
32 | 32 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
33 | 33 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
34 | 34 | __license__ = Release.license |
|
35 | 35 | __version__ = Release.version |
|
36 | 36 | |
|
37 | 37 | # Python standard modules |
|
38 | 38 | import __main__ |
|
39 | 39 | import __builtin__ |
|
40 | 40 | import StringIO |
|
41 | 41 | import bdb |
|
42 | 42 | import cPickle as pickle |
|
43 | 43 | import codeop |
|
44 | 44 | import exceptions |
|
45 | 45 | import glob |
|
46 | 46 | import inspect |
|
47 | 47 | import keyword |
|
48 | 48 | import new |
|
49 | 49 | import os |
|
50 | 50 | import pydoc |
|
51 | 51 | import re |
|
52 | 52 | import shutil |
|
53 | 53 | import string |
|
54 | 54 | import sys |
|
55 | 55 | import tempfile |
|
56 | 56 | import traceback |
|
57 | 57 | import types |
|
58 | 58 | import pickleshare |
|
59 | 59 | from sets import Set |
|
60 | 60 | from pprint import pprint, pformat |
|
61 | 61 | |
|
62 | 62 | # IPython's own modules |
|
63 | 63 | import IPython |
|
64 | 64 | from IPython import OInspect,PyColorize,ultraTB |
|
65 | 65 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names |
|
66 | 66 | from IPython.FakeModule import FakeModule |
|
67 | 67 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns |
|
68 | 68 | from IPython.Logger import Logger |
|
69 | 69 | from IPython.Magic import Magic |
|
70 | 70 | from IPython.Prompts import CachedOutput |
|
71 | 71 | from IPython.ipstruct import Struct |
|
72 | 72 | from IPython.background_jobs import BackgroundJobManager |
|
73 | 73 | from IPython.usage import cmd_line_usage,interactive_usage |
|
74 | 74 | from IPython.genutils import * |
|
75 | from IPython.strdispatch import StrDispatch | |
|
75 | 76 | import IPython.ipapi |
|
76 | 77 | |
|
77 | 78 | # Globals |
|
78 | 79 | |
|
79 | 80 | # store the builtin raw_input globally, and use this always, in case user code |
|
80 | 81 | # overwrites it (like wx.py.PyShell does) |
|
81 | 82 | raw_input_original = raw_input |
|
82 | 83 | |
|
83 | 84 | # compiled regexps for autoindent management |
|
84 | 85 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
85 | 86 | |
|
86 | 87 | |
|
87 | 88 | #**************************************************************************** |
|
88 | 89 | # Some utility function definitions |
|
89 | 90 | |
|
90 | 91 | ini_spaces_re = re.compile(r'^(\s+)') |
|
91 | 92 | |
|
92 | 93 | def num_ini_spaces(strng): |
|
93 | 94 | """Return the number of initial spaces in a string""" |
|
94 | 95 | |
|
95 | 96 | ini_spaces = ini_spaces_re.match(strng) |
|
96 | 97 | if ini_spaces: |
|
97 | 98 | return ini_spaces.end() |
|
98 | 99 | else: |
|
99 | 100 | return 0 |
|
100 | 101 | |
|
101 | 102 | def softspace(file, newvalue): |
|
102 | 103 | """Copied from code.py, to remove the dependency""" |
|
103 | 104 | |
|
104 | 105 | oldvalue = 0 |
|
105 | 106 | try: |
|
106 | 107 | oldvalue = file.softspace |
|
107 | 108 | except AttributeError: |
|
108 | 109 | pass |
|
109 | 110 | try: |
|
110 | 111 | file.softspace = newvalue |
|
111 | 112 | except (AttributeError, TypeError): |
|
112 | 113 | # "attribute-less object" or "read-only attributes" |
|
113 | 114 | pass |
|
114 | 115 | return oldvalue |
|
115 | 116 | |
|
116 | 117 | |
|
117 | 118 | #**************************************************************************** |
|
118 | 119 | # Local use exceptions |
|
119 | 120 | class SpaceInInput(exceptions.Exception): pass |
|
120 | 121 | |
|
121 | 122 | |
|
122 | 123 | #**************************************************************************** |
|
123 | 124 | # Local use classes |
|
124 | 125 | class Bunch: pass |
|
125 | 126 | |
|
126 | 127 | class Undefined: pass |
|
127 | 128 | |
|
128 | 129 | class Quitter(object): |
|
129 | 130 | """Simple class to handle exit, similar to Python 2.5's. |
|
130 | 131 | |
|
131 | 132 | It handles exiting in an ipython-safe manner, which the one in Python 2.5 |
|
132 | 133 | doesn't do (obviously, since it doesn't know about ipython).""" |
|
133 | 134 | |
|
134 | 135 | def __init__(self,shell,name): |
|
135 | 136 | self.shell = shell |
|
136 | 137 | self.name = name |
|
137 | 138 | |
|
138 | 139 | def __repr__(self): |
|
139 | 140 | return 'Type %s() to exit.' % self.name |
|
140 | 141 | __str__ = __repr__ |
|
141 | 142 | |
|
142 | 143 | def __call__(self): |
|
143 | 144 | self.shell.exit() |
|
144 | 145 | |
|
145 | 146 | class InputList(list): |
|
146 | 147 | """Class to store user input. |
|
147 | 148 | |
|
148 | 149 | It's basically a list, but slices return a string instead of a list, thus |
|
149 | 150 | allowing things like (assuming 'In' is an instance): |
|
150 | 151 | |
|
151 | 152 | exec In[4:7] |
|
152 | 153 | |
|
153 | 154 | or |
|
154 | 155 | |
|
155 | 156 | exec In[5:9] + In[14] + In[21:25]""" |
|
156 | 157 | |
|
157 | 158 | def __getslice__(self,i,j): |
|
158 | 159 | return ''.join(list.__getslice__(self,i,j)) |
|
159 | 160 | |
|
160 | 161 | class SyntaxTB(ultraTB.ListTB): |
|
161 | 162 | """Extension which holds some state: the last exception value""" |
|
162 | 163 | |
|
163 | 164 | def __init__(self,color_scheme = 'NoColor'): |
|
164 | 165 | ultraTB.ListTB.__init__(self,color_scheme) |
|
165 | 166 | self.last_syntax_error = None |
|
166 | 167 | |
|
167 | 168 | def __call__(self, etype, value, elist): |
|
168 | 169 | self.last_syntax_error = value |
|
169 | 170 | ultraTB.ListTB.__call__(self,etype,value,elist) |
|
170 | 171 | |
|
171 | 172 | def clear_err_state(self): |
|
172 | 173 | """Return the current error state and clear it""" |
|
173 | 174 | e = self.last_syntax_error |
|
174 | 175 | self.last_syntax_error = None |
|
175 | 176 | return e |
|
176 | 177 | |
|
177 | 178 | #**************************************************************************** |
|
178 | 179 | # Main IPython class |
|
179 | 180 | |
|
180 | 181 | # FIXME: the Magic class is a mixin for now, and will unfortunately remain so |
|
181 | 182 | # until a full rewrite is made. I've cleaned all cross-class uses of |
|
182 | 183 | # attributes and methods, but too much user code out there relies on the |
|
183 | 184 | # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage. |
|
184 | 185 | # |
|
185 | 186 | # But at least now, all the pieces have been separated and we could, in |
|
186 | 187 | # principle, stop using the mixin. This will ease the transition to the |
|
187 | 188 | # chainsaw branch. |
|
188 | 189 | |
|
189 | 190 | # For reference, the following is the list of 'self.foo' uses in the Magic |
|
190 | 191 | # class as of 2005-12-28. These are names we CAN'T use in the main ipython |
|
191 | 192 | # class, to prevent clashes. |
|
192 | 193 | |
|
193 | 194 | # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind', |
|
194 | 195 | # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic', |
|
195 | 196 | # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell', |
|
196 | 197 | # 'self.value'] |
|
197 | 198 | |
|
198 | 199 | class InteractiveShell(object,Magic): |
|
199 | 200 | """An enhanced console for Python.""" |
|
200 | 201 | |
|
201 | 202 | # class attribute to indicate whether the class supports threads or not. |
|
202 | 203 | # Subclasses with thread support should override this as needed. |
|
203 | 204 | isthreaded = False |
|
204 | 205 | |
|
205 | 206 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
206 | 207 | user_ns = None,user_global_ns=None,banner2='', |
|
207 | 208 | custom_exceptions=((),None),embedded=False): |
|
208 | 209 | |
|
209 | 210 | # log system |
|
210 | 211 | self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate') |
|
211 | 212 | |
|
212 | 213 | # some minimal strict typechecks. For some core data structures, I |
|
213 | 214 | # want actual basic python types, not just anything that looks like |
|
214 | 215 | # one. This is especially true for namespaces. |
|
215 | 216 | for ns in (user_ns,user_global_ns): |
|
216 | 217 | if ns is not None and type(ns) != types.DictType: |
|
217 | 218 | raise TypeError,'namespace must be a dictionary' |
|
218 | 219 | |
|
219 | 220 | # Job manager (for jobs run as background threads) |
|
220 | 221 | self.jobs = BackgroundJobManager() |
|
221 | 222 | |
|
222 | 223 | # Store the actual shell's name |
|
223 | 224 | self.name = name |
|
224 | 225 | |
|
225 | 226 | # We need to know whether the instance is meant for embedding, since |
|
226 | 227 | # global/local namespaces need to be handled differently in that case |
|
227 | 228 | self.embedded = embedded |
|
228 | 229 | |
|
229 | 230 | # command compiler |
|
230 | 231 | self.compile = codeop.CommandCompiler() |
|
231 | 232 | |
|
232 | 233 | # User input buffer |
|
233 | 234 | self.buffer = [] |
|
234 | 235 | |
|
235 | 236 | # Default name given in compilation of code |
|
236 | 237 | self.filename = '<ipython console>' |
|
237 | 238 | |
|
238 | 239 | # Install our own quitter instead of the builtins. For python2.3-2.4, |
|
239 | 240 | # this brings in behavior like 2.5, and for 2.5 it's identical. |
|
240 | 241 | __builtin__.exit = Quitter(self,'exit') |
|
241 | 242 | __builtin__.quit = Quitter(self,'quit') |
|
242 | 243 | |
|
243 | 244 | # Make an empty namespace, which extension writers can rely on both |
|
244 | 245 | # existing and NEVER being used by ipython itself. This gives them a |
|
245 | 246 | # convenient location for storing additional information and state |
|
246 | 247 | # their extensions may require, without fear of collisions with other |
|
247 | 248 | # ipython names that may develop later. |
|
248 | 249 | self.meta = Struct() |
|
249 | 250 | |
|
250 | 251 | # Create the namespace where the user will operate. user_ns is |
|
251 | 252 | # normally the only one used, and it is passed to the exec calls as |
|
252 | 253 | # the locals argument. But we do carry a user_global_ns namespace |
|
253 | 254 | # given as the exec 'globals' argument, This is useful in embedding |
|
254 | 255 | # situations where the ipython shell opens in a context where the |
|
255 | 256 | # distinction between locals and globals is meaningful. |
|
256 | 257 | |
|
257 | 258 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
258 | 259 | # level as a dict instead of a module. This is a manual fix, but I |
|
259 | 260 | # should really track down where the problem is coming from. Alex |
|
260 | 261 | # Schmolck reported this problem first. |
|
261 | 262 | |
|
262 | 263 | # A useful post by Alex Martelli on this topic: |
|
263 | 264 | # Re: inconsistent value from __builtins__ |
|
264 | 265 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
265 | 266 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
266 | 267 | # Gruppen: comp.lang.python |
|
267 | 268 | |
|
268 | 269 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
269 | 270 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
270 | 271 | # > <type 'dict'> |
|
271 | 272 | # > >>> print type(__builtins__) |
|
272 | 273 | # > <type 'module'> |
|
273 | 274 | # > Is this difference in return value intentional? |
|
274 | 275 | |
|
275 | 276 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
276 | 277 | # or a module, and it's been that way for a long time. Whether it's |
|
277 | 278 | # intentional (or sensible), I don't know. In any case, the idea is |
|
278 | 279 | # that if you need to access the built-in namespace directly, you |
|
279 | 280 | # should start with "import __builtin__" (note, no 's') which will |
|
280 | 281 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
281 | 282 | |
|
282 | 283 | # These routines return properly built dicts as needed by the rest of |
|
283 | 284 | # the code, and can also be used by extension writers to generate |
|
284 | 285 | # properly initialized namespaces. |
|
285 | 286 | user_ns = IPython.ipapi.make_user_ns(user_ns) |
|
286 | 287 | user_global_ns = IPython.ipapi.make_user_global_ns(user_global_ns) |
|
287 | 288 | |
|
288 | 289 | # Assign namespaces |
|
289 | 290 | # This is the namespace where all normal user variables live |
|
290 | 291 | self.user_ns = user_ns |
|
291 | 292 | # Embedded instances require a separate namespace for globals. |
|
292 | 293 | # Normally this one is unused by non-embedded instances. |
|
293 | 294 | self.user_global_ns = user_global_ns |
|
294 | 295 | # A namespace to keep track of internal data structures to prevent |
|
295 | 296 | # them from cluttering user-visible stuff. Will be updated later |
|
296 | 297 | self.internal_ns = {} |
|
297 | 298 | |
|
298 | 299 | # Namespace of system aliases. Each entry in the alias |
|
299 | 300 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
300 | 301 | # of positional arguments of the alias. |
|
301 | 302 | self.alias_table = {} |
|
302 | 303 | |
|
303 | 304 | # A table holding all the namespaces IPython deals with, so that |
|
304 | 305 | # introspection facilities can search easily. |
|
305 | 306 | self.ns_table = {'user':user_ns, |
|
306 | 307 | 'user_global':user_global_ns, |
|
307 | 308 | 'alias':self.alias_table, |
|
308 | 309 | 'internal':self.internal_ns, |
|
309 | 310 | 'builtin':__builtin__.__dict__ |
|
310 | 311 | } |
|
311 | 312 | |
|
312 | 313 | # The user namespace MUST have a pointer to the shell itself. |
|
313 | 314 | self.user_ns[name] = self |
|
314 | 315 | |
|
315 | 316 | # We need to insert into sys.modules something that looks like a |
|
316 | 317 | # module but which accesses the IPython namespace, for shelve and |
|
317 | 318 | # pickle to work interactively. Normally they rely on getting |
|
318 | 319 | # everything out of __main__, but for embedding purposes each IPython |
|
319 | 320 | # instance has its own private namespace, so we can't go shoving |
|
320 | 321 | # everything into __main__. |
|
321 | 322 | |
|
322 | 323 | # note, however, that we should only do this for non-embedded |
|
323 | 324 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
324 | 325 | # namespace. Embedded instances, on the other hand, should not do |
|
325 | 326 | # this because they need to manage the user local/global namespaces |
|
326 | 327 | # only, but they live within a 'normal' __main__ (meaning, they |
|
327 | 328 | # shouldn't overtake the execution environment of the script they're |
|
328 | 329 | # embedded in). |
|
329 | 330 | |
|
330 | 331 | if not embedded: |
|
331 | 332 | try: |
|
332 | 333 | main_name = self.user_ns['__name__'] |
|
333 | 334 | except KeyError: |
|
334 | 335 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' |
|
335 | 336 | else: |
|
336 | 337 | #print "pickle hack in place" # dbg |
|
337 | 338 | #print 'main_name:',main_name # dbg |
|
338 | 339 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
339 | 340 | |
|
340 | 341 | # List of input with multi-line handling. |
|
341 | 342 | # Fill its zero entry, user counter starts at 1 |
|
342 | 343 | self.input_hist = InputList(['\n']) |
|
343 | 344 | # This one will hold the 'raw' input history, without any |
|
344 | 345 | # pre-processing. This will allow users to retrieve the input just as |
|
345 | 346 | # it was exactly typed in by the user, with %hist -r. |
|
346 | 347 | self.input_hist_raw = InputList(['\n']) |
|
347 | 348 | |
|
348 | 349 | # list of visited directories |
|
349 | 350 | try: |
|
350 | 351 | self.dir_hist = [os.getcwd()] |
|
351 | 352 | except IOError, e: |
|
352 | 353 | self.dir_hist = [] |
|
353 | 354 | |
|
354 | 355 | # dict of output history |
|
355 | 356 | self.output_hist = {} |
|
356 | 357 | |
|
357 | 358 | # dict of things NOT to alias (keywords, builtins and some magics) |
|
358 | 359 | no_alias = {} |
|
359 | 360 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] |
|
360 | 361 | for key in keyword.kwlist + no_alias_magics: |
|
361 | 362 | no_alias[key] = 1 |
|
362 | 363 | no_alias.update(__builtin__.__dict__) |
|
363 | 364 | self.no_alias = no_alias |
|
364 | 365 | |
|
365 | 366 | # make global variables for user access to these |
|
366 | 367 | self.user_ns['_ih'] = self.input_hist |
|
367 | 368 | self.user_ns['_oh'] = self.output_hist |
|
368 | 369 | self.user_ns['_dh'] = self.dir_hist |
|
369 | 370 | |
|
370 | 371 | # user aliases to input and output histories |
|
371 | 372 | self.user_ns['In'] = self.input_hist |
|
372 | 373 | self.user_ns['Out'] = self.output_hist |
|
373 | 374 | |
|
374 | 375 | # Object variable to store code object waiting execution. This is |
|
375 | 376 | # used mainly by the multithreaded shells, but it can come in handy in |
|
376 | 377 | # other situations. No need to use a Queue here, since it's a single |
|
377 | 378 | # item which gets cleared once run. |
|
378 | 379 | self.code_to_run = None |
|
379 | 380 | |
|
380 | 381 | # escapes for automatic behavior on the command line |
|
381 | 382 | self.ESC_SHELL = '!' |
|
382 | 383 | self.ESC_HELP = '?' |
|
383 | 384 | self.ESC_MAGIC = '%' |
|
384 | 385 | self.ESC_QUOTE = ',' |
|
385 | 386 | self.ESC_QUOTE2 = ';' |
|
386 | 387 | self.ESC_PAREN = '/' |
|
387 | 388 | |
|
388 | 389 | # And their associated handlers |
|
389 | 390 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, |
|
390 | 391 | self.ESC_QUOTE : self.handle_auto, |
|
391 | 392 | self.ESC_QUOTE2 : self.handle_auto, |
|
392 | 393 | self.ESC_MAGIC : self.handle_magic, |
|
393 | 394 | self.ESC_HELP : self.handle_help, |
|
394 | 395 | self.ESC_SHELL : self.handle_shell_escape, |
|
395 | 396 | } |
|
396 | 397 | |
|
397 | 398 | # class initializations |
|
398 | 399 | Magic.__init__(self,self) |
|
399 | 400 | |
|
400 | 401 | # Python source parser/formatter for syntax highlighting |
|
401 | 402 | pyformat = PyColorize.Parser().format |
|
402 | 403 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) |
|
403 | 404 | |
|
404 | 405 | # hooks holds pointers used for user-side customizations |
|
405 | 406 | self.hooks = Struct() |
|
406 | 407 | |
|
408 | self.strdispatchers = {} | |
|
409 | ||
|
407 | 410 | # Set all default hooks, defined in the IPython.hooks module. |
|
408 | 411 | hooks = IPython.hooks |
|
409 | 412 | for hook_name in hooks.__all__: |
|
410 | 413 | # default hooks have priority 100, i.e. low; user hooks should have 0-100 priority |
|
411 | 414 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
412 | 415 | #print "bound hook",hook_name |
|
413 | 416 | |
|
414 | 417 | # Flag to mark unconditional exit |
|
415 | 418 | self.exit_now = False |
|
416 | 419 | |
|
417 | 420 | self.usage_min = """\ |
|
418 | 421 | An enhanced console for Python. |
|
419 | 422 | Some of its features are: |
|
420 | 423 | - Readline support if the readline library is present. |
|
421 | 424 | - Tab completion in the local namespace. |
|
422 | 425 | - Logging of input, see command-line options. |
|
423 | 426 | - System shell escape via ! , eg !ls. |
|
424 | 427 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) |
|
425 | 428 | - Keeps track of locally defined variables via %who, %whos. |
|
426 | 429 | - Show object information with a ? eg ?x or x? (use ?? for more info). |
|
427 | 430 | """ |
|
428 | 431 | if usage: self.usage = usage |
|
429 | 432 | else: self.usage = self.usage_min |
|
430 | 433 | |
|
431 | 434 | # Storage |
|
432 | 435 | self.rc = rc # This will hold all configuration information |
|
433 | 436 | self.pager = 'less' |
|
434 | 437 | # temporary files used for various purposes. Deleted at exit. |
|
435 | 438 | self.tempfiles = [] |
|
436 | 439 | |
|
437 | 440 | # Keep track of readline usage (later set by init_readline) |
|
438 | 441 | self.has_readline = False |
|
439 | 442 | |
|
440 | 443 | # template for logfile headers. It gets resolved at runtime by the |
|
441 | 444 | # logstart method. |
|
442 | 445 | self.loghead_tpl = \ |
|
443 | 446 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
444 | 447 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
445 | 448 | #log# opts = %s |
|
446 | 449 | #log# args = %s |
|
447 | 450 | #log# It is safe to make manual edits below here. |
|
448 | 451 | #log#----------------------------------------------------------------------- |
|
449 | 452 | """ |
|
450 | 453 | # for pushd/popd management |
|
451 | 454 | try: |
|
452 | 455 | self.home_dir = get_home_dir() |
|
453 | 456 | except HomeDirError,msg: |
|
454 | 457 | fatal(msg) |
|
455 | 458 | |
|
456 | 459 | self.dir_stack = [os.getcwd().replace(self.home_dir,'~')] |
|
457 | 460 | |
|
458 | 461 | # Functions to call the underlying shell. |
|
459 | 462 | |
|
460 | 463 | # utility to expand user variables via Itpl |
|
461 | 464 | self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'), |
|
462 | 465 | self.user_ns)) |
|
463 | 466 | # The first is similar to os.system, but it doesn't return a value, |
|
464 | 467 | # and it allows interpolation of variables in the user's namespace. |
|
465 | 468 | self.system = lambda cmd: shell(self.var_expand(cmd), |
|
466 | 469 | header='IPython system call: ', |
|
467 | 470 | verbose=self.rc.system_verbose) |
|
468 | 471 | # These are for getoutput and getoutputerror: |
|
469 | 472 | self.getoutput = lambda cmd: \ |
|
470 | 473 | getoutput(self.var_expand(cmd), |
|
471 | 474 | header='IPython system call: ', |
|
472 | 475 | verbose=self.rc.system_verbose) |
|
473 | 476 | self.getoutputerror = lambda cmd: \ |
|
474 | 477 | getoutputerror(self.var_expand(cmd), |
|
475 | 478 | header='IPython system call: ', |
|
476 | 479 | verbose=self.rc.system_verbose) |
|
477 | 480 | |
|
478 | 481 | # RegExp for splitting line contents into pre-char//first |
|
479 | 482 | # word-method//rest. For clarity, each group in on one line. |
|
480 | 483 | |
|
481 | 484 | # WARNING: update the regexp if the above escapes are changed, as they |
|
482 | 485 | # are hardwired in. |
|
483 | 486 | |
|
484 | 487 | # Don't get carried away with trying to make the autocalling catch too |
|
485 | 488 | # much: it's better to be conservative rather than to trigger hidden |
|
486 | 489 | # evals() somewhere and end up causing side effects. |
|
487 | 490 | |
|
488 | 491 | self.line_split = re.compile(r'^([\s*,;/])' |
|
489 | 492 | r'([\?\w\.]+\w*\s*)' |
|
490 | 493 | r'(\(?.*$)') |
|
491 | 494 | |
|
492 | 495 | # Original re, keep around for a while in case changes break something |
|
493 | 496 | #self.line_split = re.compile(r'(^[\s*!\?%,/]?)' |
|
494 | 497 | # r'(\s*[\?\w\.]+\w*\s*)' |
|
495 | 498 | # r'(\(?.*$)') |
|
496 | 499 | |
|
497 | 500 | # RegExp to identify potential function names |
|
498 | 501 | self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$') |
|
499 | 502 | |
|
500 | 503 | # RegExp to exclude strings with this start from autocalling. In |
|
501 | 504 | # particular, all binary operators should be excluded, so that if foo |
|
502 | 505 | # is callable, foo OP bar doesn't become foo(OP bar), which is |
|
503 | 506 | # invalid. The characters '!=()' don't need to be checked for, as the |
|
504 | 507 | # _prefilter routine explicitely does so, to catch direct calls and |
|
505 | 508 | # rebindings of existing names. |
|
506 | 509 | |
|
507 | 510 | # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise |
|
508 | 511 | # it affects the rest of the group in square brackets. |
|
509 | 512 | self.re_exclude_auto = re.compile(r'^[<>,&^\|\*/\+-]' |
|
510 | 513 | '|^is |^not |^in |^and |^or ') |
|
511 | 514 | |
|
512 | 515 | # try to catch also methods for stuff in lists/tuples/dicts: off |
|
513 | 516 | # (experimental). For this to work, the line_split regexp would need |
|
514 | 517 | # to be modified so it wouldn't break things at '['. That line is |
|
515 | 518 | # nasty enough that I shouldn't change it until I can test it _well_. |
|
516 | 519 | #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$') |
|
517 | 520 | |
|
518 | 521 | # keep track of where we started running (mainly for crash post-mortem) |
|
519 | 522 | self.starting_dir = os.getcwd() |
|
520 | 523 | |
|
521 | 524 | # Various switches which can be set |
|
522 | 525 | self.CACHELENGTH = 5000 # this is cheap, it's just text |
|
523 | 526 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ |
|
524 | 527 | self.banner2 = banner2 |
|
525 | 528 | |
|
526 | 529 | # TraceBack handlers: |
|
527 | 530 | |
|
528 | 531 | # Syntax error handler. |
|
529 | 532 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') |
|
530 | 533 | |
|
531 | 534 | # The interactive one is initialized with an offset, meaning we always |
|
532 | 535 | # want to remove the topmost item in the traceback, which is our own |
|
533 | 536 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
534 | 537 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', |
|
535 | 538 | color_scheme='NoColor', |
|
536 | 539 | tb_offset = 1) |
|
537 | 540 | |
|
538 | 541 | # IPython itself shouldn't crash. This will produce a detailed |
|
539 | 542 | # post-mortem if it does. But we only install the crash handler for |
|
540 | 543 | # non-threaded shells, the threaded ones use a normal verbose reporter |
|
541 | 544 | # and lose the crash handler. This is because exceptions in the main |
|
542 | 545 | # thread (such as in GUI code) propagate directly to sys.excepthook, |
|
543 | 546 | # and there's no point in printing crash dumps for every user exception. |
|
544 | 547 | if self.isthreaded: |
|
545 | 548 | ipCrashHandler = ultraTB.FormattedTB() |
|
546 | 549 | else: |
|
547 | 550 | from IPython import CrashHandler |
|
548 | 551 | ipCrashHandler = CrashHandler.IPythonCrashHandler(self) |
|
549 | 552 | self.set_crash_handler(ipCrashHandler) |
|
550 | 553 | |
|
551 | 554 | # and add any custom exception handlers the user may have specified |
|
552 | 555 | self.set_custom_exc(*custom_exceptions) |
|
553 | 556 | |
|
554 | 557 | # indentation management |
|
555 | 558 | self.autoindent = False |
|
556 | 559 | self.indent_current_nsp = 0 |
|
557 | 560 | |
|
558 | 561 | # Make some aliases automatically |
|
559 | 562 | # Prepare list of shell aliases to auto-define |
|
560 | 563 | if os.name == 'posix': |
|
561 | 564 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', |
|
562 | 565 | 'mv mv -i','rm rm -i','cp cp -i', |
|
563 | 566 | 'cat cat','less less','clear clear', |
|
564 | 567 | # a better ls |
|
565 | 568 | 'ls ls -F', |
|
566 | 569 | # long ls |
|
567 | 570 | 'll ls -lF') |
|
568 | 571 | # Extra ls aliases with color, which need special treatment on BSD |
|
569 | 572 | # variants |
|
570 | 573 | ls_extra = ( # color ls |
|
571 | 574 | 'lc ls -F -o --color', |
|
572 | 575 | # ls normal files only |
|
573 | 576 | 'lf ls -F -o --color %l | grep ^-', |
|
574 | 577 | # ls symbolic links |
|
575 | 578 | 'lk ls -F -o --color %l | grep ^l', |
|
576 | 579 | # directories or links to directories, |
|
577 | 580 | 'ldir ls -F -o --color %l | grep /$', |
|
578 | 581 | # things which are executable |
|
579 | 582 | 'lx ls -F -o --color %l | grep ^-..x', |
|
580 | 583 | ) |
|
581 | 584 | # The BSDs don't ship GNU ls, so they don't understand the |
|
582 | 585 | # --color switch out of the box |
|
583 | 586 | if 'bsd' in sys.platform: |
|
584 | 587 | ls_extra = ( # ls normal files only |
|
585 | 588 | 'lf ls -lF | grep ^-', |
|
586 | 589 | # ls symbolic links |
|
587 | 590 | 'lk ls -lF | grep ^l', |
|
588 | 591 | # directories or links to directories, |
|
589 | 592 | 'ldir ls -lF | grep /$', |
|
590 | 593 | # things which are executable |
|
591 | 594 | 'lx ls -lF | grep ^-..x', |
|
592 | 595 | ) |
|
593 | 596 | auto_alias = auto_alias + ls_extra |
|
594 | 597 | elif os.name in ['nt','dos']: |
|
595 | 598 | auto_alias = ('dir dir /on', 'ls dir /on', |
|
596 | 599 | 'ddir dir /ad /on', 'ldir dir /ad /on', |
|
597 | 600 | 'mkdir mkdir','rmdir rmdir','echo echo', |
|
598 | 601 | 'ren ren','cls cls','copy copy') |
|
599 | 602 | else: |
|
600 | 603 | auto_alias = () |
|
601 | 604 | self.auto_alias = [s.split(None,1) for s in auto_alias] |
|
602 | 605 | # Call the actual (public) initializer |
|
603 | 606 | self.init_auto_alias() |
|
604 | 607 | |
|
605 | 608 | # Produce a public API instance |
|
606 | 609 | self.api = IPython.ipapi.IPApi(self) |
|
607 | 610 | |
|
608 | 611 | # track which builtins we add, so we can clean up later |
|
609 | 612 | self.builtins_added = {} |
|
610 | 613 | # This method will add the necessary builtins for operation, but |
|
611 | 614 | # tracking what it did via the builtins_added dict. |
|
612 | 615 | self.add_builtins() |
|
613 | 616 | |
|
614 | 617 | # end __init__ |
|
615 | 618 | |
|
616 | 619 | def pre_config_initialization(self): |
|
617 | 620 | """Pre-configuration init method |
|
618 | 621 | |
|
619 | 622 | This is called before the configuration files are processed to |
|
620 | 623 | prepare the services the config files might need. |
|
621 | 624 | |
|
622 | 625 | self.rc already has reasonable default values at this point. |
|
623 | 626 | """ |
|
624 | 627 | rc = self.rc |
|
625 | 628 | |
|
626 | 629 | self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db") |
|
627 | 630 | |
|
628 | 631 | def post_config_initialization(self): |
|
629 | 632 | """Post configuration init method |
|
630 | 633 | |
|
631 | 634 | This is called after the configuration files have been processed to |
|
632 | 635 | 'finalize' the initialization.""" |
|
633 | 636 | |
|
634 | 637 | rc = self.rc |
|
635 | 638 | |
|
636 | 639 | # Object inspector |
|
637 | 640 | self.inspector = OInspect.Inspector(OInspect.InspectColors, |
|
638 | 641 | PyColorize.ANSICodeColors, |
|
639 | 642 | 'NoColor', |
|
640 | 643 | rc.object_info_string_level) |
|
641 | 644 | |
|
642 | 645 | # Load readline proper |
|
643 | 646 | if rc.readline: |
|
644 | 647 | self.init_readline() |
|
645 | 648 | |
|
646 | 649 | # local shortcut, this is used a LOT |
|
647 | 650 | self.log = self.logger.log |
|
648 | 651 | |
|
649 | 652 | # Initialize cache, set in/out prompts and printing system |
|
650 | 653 | self.outputcache = CachedOutput(self, |
|
651 | 654 | rc.cache_size, |
|
652 | 655 | rc.pprint, |
|
653 | 656 | input_sep = rc.separate_in, |
|
654 | 657 | output_sep = rc.separate_out, |
|
655 | 658 | output_sep2 = rc.separate_out2, |
|
656 | 659 | ps1 = rc.prompt_in1, |
|
657 | 660 | ps2 = rc.prompt_in2, |
|
658 | 661 | ps_out = rc.prompt_out, |
|
659 | 662 | pad_left = rc.prompts_pad_left) |
|
660 | 663 | |
|
661 | 664 | # user may have over-ridden the default print hook: |
|
662 | 665 | try: |
|
663 | 666 | self.outputcache.__class__.display = self.hooks.display |
|
664 | 667 | except AttributeError: |
|
665 | 668 | pass |
|
666 | 669 | |
|
667 | 670 | # I don't like assigning globally to sys, because it means when |
|
668 | 671 | # embedding instances, each embedded instance overrides the previous |
|
669 | 672 | # choice. But sys.displayhook seems to be called internally by exec, |
|
670 | 673 | # so I don't see a way around it. We first save the original and then |
|
671 | 674 | # overwrite it. |
|
672 | 675 | self.sys_displayhook = sys.displayhook |
|
673 | 676 | sys.displayhook = self.outputcache |
|
674 | 677 | |
|
675 | 678 | # Set user colors (don't do it in the constructor above so that it |
|
676 | 679 | # doesn't crash if colors option is invalid) |
|
677 | 680 | self.magic_colors(rc.colors) |
|
678 | 681 | |
|
679 | 682 | # Set calling of pdb on exceptions |
|
680 | 683 | self.call_pdb = rc.pdb |
|
681 | 684 | |
|
682 | 685 | # Load user aliases |
|
683 | 686 | for alias in rc.alias: |
|
684 | 687 | self.magic_alias(alias) |
|
685 | 688 | self.hooks.late_startup_hook() |
|
686 | 689 | |
|
687 | 690 | batchrun = False |
|
688 | 691 | for batchfile in [path(arg) for arg in self.rc.args |
|
689 | 692 | if arg.lower().endswith('.ipy')]: |
|
690 | 693 | if not batchfile.isfile(): |
|
691 | 694 | print "No such batch file:", batchfile |
|
692 | 695 | continue |
|
693 | 696 | self.api.runlines(batchfile.text()) |
|
694 | 697 | batchrun = True |
|
695 | 698 | if batchrun: |
|
696 | 699 | self.exit_now = True |
|
697 | 700 | |
|
698 | 701 | def add_builtins(self): |
|
699 | 702 | """Store ipython references into the builtin namespace. |
|
700 | 703 | |
|
701 | 704 | Some parts of ipython operate via builtins injected here, which hold a |
|
702 | 705 | reference to IPython itself.""" |
|
703 | 706 | |
|
704 | 707 | # TODO: deprecate all except _ip; 'jobs' should be installed |
|
705 | 708 | # by an extension and the rest are under _ip, ipalias is redundant |
|
706 | 709 | builtins_new = dict(__IPYTHON__ = self, |
|
707 | 710 | ip_set_hook = self.set_hook, |
|
708 | 711 | jobs = self.jobs, |
|
709 | 712 | ipmagic = self.ipmagic, |
|
710 | 713 | ipalias = self.ipalias, |
|
711 | 714 | ipsystem = self.ipsystem, |
|
712 | 715 | _ip = self.api |
|
713 | 716 | ) |
|
714 | 717 | for biname,bival in builtins_new.items(): |
|
715 | 718 | try: |
|
716 | 719 | # store the orignal value so we can restore it |
|
717 | 720 | self.builtins_added[biname] = __builtin__.__dict__[biname] |
|
718 | 721 | except KeyError: |
|
719 | 722 | # or mark that it wasn't defined, and we'll just delete it at |
|
720 | 723 | # cleanup |
|
721 | 724 | self.builtins_added[biname] = Undefined |
|
722 | 725 | __builtin__.__dict__[biname] = bival |
|
723 | 726 | |
|
724 | 727 | # Keep in the builtins a flag for when IPython is active. We set it |
|
725 | 728 | # with setdefault so that multiple nested IPythons don't clobber one |
|
726 | 729 | # another. Each will increase its value by one upon being activated, |
|
727 | 730 | # which also gives us a way to determine the nesting level. |
|
728 | 731 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
729 | 732 | |
|
730 | 733 | def clean_builtins(self): |
|
731 | 734 | """Remove any builtins which might have been added by add_builtins, or |
|
732 | 735 | restore overwritten ones to their previous values.""" |
|
733 | 736 | for biname,bival in self.builtins_added.items(): |
|
734 | 737 | if bival is Undefined: |
|
735 | 738 | del __builtin__.__dict__[biname] |
|
736 | 739 | else: |
|
737 | 740 | __builtin__.__dict__[biname] = bival |
|
738 | 741 | self.builtins_added.clear() |
|
739 | 742 | |
|
740 | def set_hook(self,name,hook, priority = 50): | |
|
743 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): | |
|
741 | 744 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
742 | 745 | |
|
743 | 746 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
744 | 747 | adding your function to one of these hooks, you can modify IPython's |
|
745 | 748 | behavior to call at runtime your own routines.""" |
|
746 | 749 | |
|
747 | 750 | # At some point in the future, this should validate the hook before it |
|
748 | 751 | # accepts it. Probably at least check that the hook takes the number |
|
749 | 752 | # of args it's supposed to. |
|
753 | ||
|
754 | f = new.instancemethod(hook,self,self.__class__) | |
|
755 | ||
|
756 | # check if the hook is for strdispatcher first | |
|
757 | if str_key is not None: | |
|
758 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
|
759 | sdp.add_s(str_key, f, priority ) | |
|
760 | self.strdispatchers[name] = sdp | |
|
761 | return | |
|
762 | if re_key is not None: | |
|
763 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
|
764 | sdp.add_re(re.compile(re_key), f, priority ) | |
|
765 | self.strdispatchers[name] = sdp | |
|
766 | return | |
|
767 | ||
|
750 | 768 | dp = getattr(self.hooks, name, None) |
|
751 | 769 | if name not in IPython.hooks.__all__: |
|
752 | 770 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ ) |
|
753 | 771 | if not dp: |
|
754 | 772 | dp = IPython.hooks.CommandChainDispatcher() |
|
755 | 773 | |
|
756 | f = new.instancemethod(hook,self,self.__class__) | |
|
757 | 774 | try: |
|
758 | 775 | dp.add(f,priority) |
|
759 | 776 | except AttributeError: |
|
760 | 777 | # it was not commandchain, plain old func - replace |
|
761 | 778 | dp = f |
|
762 | 779 | |
|
763 | 780 | setattr(self.hooks,name, dp) |
|
764 | 781 | |
|
765 | 782 | |
|
766 | 783 | #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) |
|
767 | 784 | |
|
768 | 785 | def set_crash_handler(self,crashHandler): |
|
769 | 786 | """Set the IPython crash handler. |
|
770 | 787 | |
|
771 | 788 | This must be a callable with a signature suitable for use as |
|
772 | 789 | sys.excepthook.""" |
|
773 | 790 | |
|
774 | 791 | # Install the given crash handler as the Python exception hook |
|
775 | 792 | sys.excepthook = crashHandler |
|
776 | 793 | |
|
777 | 794 | # The instance will store a pointer to this, so that runtime code |
|
778 | 795 | # (such as magics) can access it. This is because during the |
|
779 | 796 | # read-eval loop, it gets temporarily overwritten (to deal with GUI |
|
780 | 797 | # frameworks). |
|
781 | 798 | self.sys_excepthook = sys.excepthook |
|
782 | 799 | |
|
783 | 800 | |
|
784 | 801 | def set_custom_exc(self,exc_tuple,handler): |
|
785 | 802 | """set_custom_exc(exc_tuple,handler) |
|
786 | 803 | |
|
787 | 804 | Set a custom exception handler, which will be called if any of the |
|
788 | 805 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
789 | 806 | runcode() method. |
|
790 | 807 | |
|
791 | 808 | Inputs: |
|
792 | 809 | |
|
793 | 810 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
794 | 811 | handler for. It is very important that you use a tuple, and NOT A |
|
795 | 812 | LIST here, because of the way Python's except statement works. If |
|
796 | 813 | you only want to trap a single exception, use a singleton tuple: |
|
797 | 814 | |
|
798 | 815 | exc_tuple == (MyCustomException,) |
|
799 | 816 | |
|
800 | 817 | - handler: this must be defined as a function with the following |
|
801 | 818 | basic interface: def my_handler(self,etype,value,tb). |
|
802 | 819 | |
|
803 | 820 | This will be made into an instance method (via new.instancemethod) |
|
804 | 821 | of IPython itself, and it will be called if any of the exceptions |
|
805 | 822 | listed in the exc_tuple are caught. If the handler is None, an |
|
806 | 823 | internal basic one is used, which just prints basic info. |
|
807 | 824 | |
|
808 | 825 | WARNING: by putting in your own exception handler into IPython's main |
|
809 | 826 | execution loop, you run a very good chance of nasty crashes. This |
|
810 | 827 | facility should only be used if you really know what you are doing.""" |
|
811 | 828 | |
|
812 | 829 | assert type(exc_tuple)==type(()) , \ |
|
813 | 830 | "The custom exceptions must be given AS A TUPLE." |
|
814 | 831 | |
|
815 | 832 | def dummy_handler(self,etype,value,tb): |
|
816 | 833 | print '*** Simple custom exception handler ***' |
|
817 | 834 | print 'Exception type :',etype |
|
818 | 835 | print 'Exception value:',value |
|
819 | 836 | print 'Traceback :',tb |
|
820 | 837 | print 'Source code :','\n'.join(self.buffer) |
|
821 | 838 | |
|
822 | 839 | if handler is None: handler = dummy_handler |
|
823 | 840 | |
|
824 | 841 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
825 | 842 | self.custom_exceptions = exc_tuple |
|
826 | 843 | |
|
827 | 844 | def set_custom_completer(self,completer,pos=0): |
|
828 | 845 | """set_custom_completer(completer,pos=0) |
|
829 | 846 | |
|
830 | 847 | Adds a new custom completer function. |
|
831 | 848 | |
|
832 | 849 | The position argument (defaults to 0) is the index in the completers |
|
833 | 850 | list where you want the completer to be inserted.""" |
|
834 | 851 | |
|
835 | 852 | newcomp = new.instancemethod(completer,self.Completer, |
|
836 | 853 | self.Completer.__class__) |
|
837 | 854 | self.Completer.matchers.insert(pos,newcomp) |
|
838 | 855 | |
|
839 | 856 | def _get_call_pdb(self): |
|
840 | 857 | return self._call_pdb |
|
841 | 858 | |
|
842 | 859 | def _set_call_pdb(self,val): |
|
843 | 860 | |
|
844 | 861 | if val not in (0,1,False,True): |
|
845 | 862 | raise ValueError,'new call_pdb value must be boolean' |
|
846 | 863 | |
|
847 | 864 | # store value in instance |
|
848 | 865 | self._call_pdb = val |
|
849 | 866 | |
|
850 | 867 | # notify the actual exception handlers |
|
851 | 868 | self.InteractiveTB.call_pdb = val |
|
852 | 869 | if self.isthreaded: |
|
853 | 870 | try: |
|
854 | 871 | self.sys_excepthook.call_pdb = val |
|
855 | 872 | except: |
|
856 | 873 | warn('Failed to activate pdb for threaded exception handler') |
|
857 | 874 | |
|
858 | 875 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
859 | 876 | 'Control auto-activation of pdb at exceptions') |
|
860 | 877 | |
|
861 | 878 | |
|
862 | 879 | # These special functions get installed in the builtin namespace, to |
|
863 | 880 | # provide programmatic (pure python) access to magics, aliases and system |
|
864 | 881 | # calls. This is important for logging, user scripting, and more. |
|
865 | 882 | |
|
866 | 883 | # We are basically exposing, via normal python functions, the three |
|
867 | 884 | # mechanisms in which ipython offers special call modes (magics for |
|
868 | 885 | # internal control, aliases for direct system access via pre-selected |
|
869 | 886 | # names, and !cmd for calling arbitrary system commands). |
|
870 | 887 | |
|
871 | 888 | def ipmagic(self,arg_s): |
|
872 | 889 | """Call a magic function by name. |
|
873 | 890 | |
|
874 | 891 | Input: a string containing the name of the magic function to call and any |
|
875 | 892 | additional arguments to be passed to the magic. |
|
876 | 893 | |
|
877 | 894 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython |
|
878 | 895 | prompt: |
|
879 | 896 | |
|
880 | 897 | In[1]: %name -opt foo bar |
|
881 | 898 | |
|
882 | 899 | To call a magic without arguments, simply use ipmagic('name'). |
|
883 | 900 | |
|
884 | 901 | This provides a proper Python function to call IPython's magics in any |
|
885 | 902 | valid Python code you can type at the interpreter, including loops and |
|
886 | 903 | compound statements. It is added by IPython to the Python builtin |
|
887 | 904 | namespace upon initialization.""" |
|
888 | 905 | |
|
889 | 906 | args = arg_s.split(' ',1) |
|
890 | 907 | magic_name = args[0] |
|
891 | 908 | magic_name = magic_name.lstrip(self.ESC_MAGIC) |
|
892 | 909 | |
|
893 | 910 | try: |
|
894 | 911 | magic_args = args[1] |
|
895 | 912 | except IndexError: |
|
896 | 913 | magic_args = '' |
|
897 | 914 | fn = getattr(self,'magic_'+magic_name,None) |
|
898 | 915 | if fn is None: |
|
899 | 916 | error("Magic function `%s` not found." % magic_name) |
|
900 | 917 | else: |
|
901 | 918 | magic_args = self.var_expand(magic_args) |
|
902 | 919 | return fn(magic_args) |
|
903 | 920 | |
|
904 | 921 | def ipalias(self,arg_s): |
|
905 | 922 | """Call an alias by name. |
|
906 | 923 | |
|
907 | 924 | Input: a string containing the name of the alias to call and any |
|
908 | 925 | additional arguments to be passed to the magic. |
|
909 | 926 | |
|
910 | 927 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
911 | 928 | prompt: |
|
912 | 929 | |
|
913 | 930 | In[1]: name -opt foo bar |
|
914 | 931 | |
|
915 | 932 | To call an alias without arguments, simply use ipalias('name'). |
|
916 | 933 | |
|
917 | 934 | This provides a proper Python function to call IPython's aliases in any |
|
918 | 935 | valid Python code you can type at the interpreter, including loops and |
|
919 | 936 | compound statements. It is added by IPython to the Python builtin |
|
920 | 937 | namespace upon initialization.""" |
|
921 | 938 | |
|
922 | 939 | args = arg_s.split(' ',1) |
|
923 | 940 | alias_name = args[0] |
|
924 | 941 | try: |
|
925 | 942 | alias_args = args[1] |
|
926 | 943 | except IndexError: |
|
927 | 944 | alias_args = '' |
|
928 | 945 | if alias_name in self.alias_table: |
|
929 | 946 | self.call_alias(alias_name,alias_args) |
|
930 | 947 | else: |
|
931 | 948 | error("Alias `%s` not found." % alias_name) |
|
932 | 949 | |
|
933 | 950 | def ipsystem(self,arg_s): |
|
934 | 951 | """Make a system call, using IPython.""" |
|
935 | 952 | |
|
936 | 953 | self.system(arg_s) |
|
937 | 954 | |
|
938 | 955 | def complete(self,text): |
|
939 | 956 | """Return a sorted list of all possible completions on text. |
|
940 | 957 | |
|
941 | 958 | Inputs: |
|
942 | 959 | |
|
943 | 960 | - text: a string of text to be completed on. |
|
944 | 961 | |
|
945 | 962 | This is a wrapper around the completion mechanism, similar to what |
|
946 | 963 | readline does at the command line when the TAB key is hit. By |
|
947 | 964 | exposing it as a method, it can be used by other non-readline |
|
948 | 965 | environments (such as GUIs) for text completion. |
|
949 | 966 | |
|
950 | 967 | Simple usage example: |
|
951 | 968 | |
|
952 | 969 | In [1]: x = 'hello' |
|
953 | 970 | |
|
954 | 971 | In [2]: __IP.complete('x.l') |
|
955 | 972 | Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']""" |
|
956 | 973 | |
|
957 | 974 | complete = self.Completer.complete |
|
958 | 975 | state = 0 |
|
959 | 976 | # use a dict so we get unique keys, since ipyhton's multiple |
|
960 | 977 | # completers can return duplicates. |
|
961 | 978 | comps = {} |
|
962 | 979 | while True: |
|
963 | 980 | newcomp = complete(text,state) |
|
964 | 981 | if newcomp is None: |
|
965 | 982 | break |
|
966 | 983 | comps[newcomp] = 1 |
|
967 | 984 | state += 1 |
|
968 | 985 | outcomps = comps.keys() |
|
969 | 986 | outcomps.sort() |
|
970 | 987 | return outcomps |
|
971 | 988 | |
|
972 | 989 | def set_completer_frame(self, frame=None): |
|
973 | 990 | if frame: |
|
974 | 991 | self.Completer.namespace = frame.f_locals |
|
975 | 992 | self.Completer.global_namespace = frame.f_globals |
|
976 | 993 | else: |
|
977 | 994 | self.Completer.namespace = self.user_ns |
|
978 | 995 | self.Completer.global_namespace = self.user_global_ns |
|
979 | 996 | |
|
980 | 997 | def init_auto_alias(self): |
|
981 | 998 | """Define some aliases automatically. |
|
982 | 999 | |
|
983 | 1000 | These are ALL parameter-less aliases""" |
|
984 | 1001 | |
|
985 | 1002 | for alias,cmd in self.auto_alias: |
|
986 | 1003 | self.alias_table[alias] = (0,cmd) |
|
987 | 1004 | |
|
988 | 1005 | def alias_table_validate(self,verbose=0): |
|
989 | 1006 | """Update information about the alias table. |
|
990 | 1007 | |
|
991 | 1008 | In particular, make sure no Python keywords/builtins are in it.""" |
|
992 | 1009 | |
|
993 | 1010 | no_alias = self.no_alias |
|
994 | 1011 | for k in self.alias_table.keys(): |
|
995 | 1012 | if k in no_alias: |
|
996 | 1013 | del self.alias_table[k] |
|
997 | 1014 | if verbose: |
|
998 | 1015 | print ("Deleting alias <%s>, it's a Python " |
|
999 | 1016 | "keyword or builtin." % k) |
|
1000 | 1017 | |
|
1001 | 1018 | def set_autoindent(self,value=None): |
|
1002 | 1019 | """Set the autoindent flag, checking for readline support. |
|
1003 | 1020 | |
|
1004 | 1021 | If called with no arguments, it acts as a toggle.""" |
|
1005 | 1022 | |
|
1006 | 1023 | if not self.has_readline: |
|
1007 | 1024 | if os.name == 'posix': |
|
1008 | 1025 | warn("The auto-indent feature requires the readline library") |
|
1009 | 1026 | self.autoindent = 0 |
|
1010 | 1027 | return |
|
1011 | 1028 | if value is None: |
|
1012 | 1029 | self.autoindent = not self.autoindent |
|
1013 | 1030 | else: |
|
1014 | 1031 | self.autoindent = value |
|
1015 | 1032 | |
|
1016 | 1033 | def rc_set_toggle(self,rc_field,value=None): |
|
1017 | 1034 | """Set or toggle a field in IPython's rc config. structure. |
|
1018 | 1035 | |
|
1019 | 1036 | If called with no arguments, it acts as a toggle. |
|
1020 | 1037 | |
|
1021 | 1038 | If called with a non-existent field, the resulting AttributeError |
|
1022 | 1039 | exception will propagate out.""" |
|
1023 | 1040 | |
|
1024 | 1041 | rc_val = getattr(self.rc,rc_field) |
|
1025 | 1042 | if value is None: |
|
1026 | 1043 | value = not rc_val |
|
1027 | 1044 | setattr(self.rc,rc_field,value) |
|
1028 | 1045 | |
|
1029 | 1046 | def user_setup(self,ipythondir,rc_suffix,mode='install'): |
|
1030 | 1047 | """Install the user configuration directory. |
|
1031 | 1048 | |
|
1032 | 1049 | Can be called when running for the first time or to upgrade the user's |
|
1033 | 1050 | .ipython/ directory with the mode parameter. Valid modes are 'install' |
|
1034 | 1051 | and 'upgrade'.""" |
|
1035 | 1052 | |
|
1036 | 1053 | def wait(): |
|
1037 | 1054 | try: |
|
1038 | 1055 | raw_input("Please press <RETURN> to start IPython.") |
|
1039 | 1056 | except EOFError: |
|
1040 | 1057 | print >> Term.cout |
|
1041 | 1058 | print '*'*70 |
|
1042 | 1059 | |
|
1043 | 1060 | cwd = os.getcwd() # remember where we started |
|
1044 | 1061 | glb = glob.glob |
|
1045 | 1062 | print '*'*70 |
|
1046 | 1063 | if mode == 'install': |
|
1047 | 1064 | print \ |
|
1048 | 1065 | """Welcome to IPython. I will try to create a personal configuration directory |
|
1049 | 1066 | where you can customize many aspects of IPython's functionality in:\n""" |
|
1050 | 1067 | else: |
|
1051 | 1068 | print 'I am going to upgrade your configuration in:' |
|
1052 | 1069 | |
|
1053 | 1070 | print ipythondir |
|
1054 | 1071 | |
|
1055 | 1072 | rcdirend = os.path.join('IPython','UserConfig') |
|
1056 | 1073 | cfg = lambda d: os.path.join(d,rcdirend) |
|
1057 | 1074 | try: |
|
1058 | 1075 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] |
|
1059 | 1076 | except IOError: |
|
1060 | 1077 | warning = """ |
|
1061 | 1078 | Installation error. IPython's directory was not found. |
|
1062 | 1079 | |
|
1063 | 1080 | Check the following: |
|
1064 | 1081 | |
|
1065 | 1082 | The ipython/IPython directory should be in a directory belonging to your |
|
1066 | 1083 | PYTHONPATH environment variable (that is, it should be in a directory |
|
1067 | 1084 | belonging to sys.path). You can copy it explicitly there or just link to it. |
|
1068 | 1085 | |
|
1069 | 1086 | IPython will proceed with builtin defaults. |
|
1070 | 1087 | """ |
|
1071 | 1088 | warn(warning) |
|
1072 | 1089 | wait() |
|
1073 | 1090 | return |
|
1074 | 1091 | |
|
1075 | 1092 | if mode == 'install': |
|
1076 | 1093 | try: |
|
1077 | 1094 | shutil.copytree(rcdir,ipythondir) |
|
1078 | 1095 | os.chdir(ipythondir) |
|
1079 | 1096 | rc_files = glb("ipythonrc*") |
|
1080 | 1097 | for rc_file in rc_files: |
|
1081 | 1098 | os.rename(rc_file,rc_file+rc_suffix) |
|
1082 | 1099 | except: |
|
1083 | 1100 | warning = """ |
|
1084 | 1101 | |
|
1085 | 1102 | There was a problem with the installation: |
|
1086 | 1103 | %s |
|
1087 | 1104 | Try to correct it or contact the developers if you think it's a bug. |
|
1088 | 1105 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] |
|
1089 | 1106 | warn(warning) |
|
1090 | 1107 | wait() |
|
1091 | 1108 | return |
|
1092 | 1109 | |
|
1093 | 1110 | elif mode == 'upgrade': |
|
1094 | 1111 | try: |
|
1095 | 1112 | os.chdir(ipythondir) |
|
1096 | 1113 | except: |
|
1097 | 1114 | print """ |
|
1098 | 1115 | Can not upgrade: changing to directory %s failed. Details: |
|
1099 | 1116 | %s |
|
1100 | 1117 | """ % (ipythondir,sys.exc_info()[1]) |
|
1101 | 1118 | wait() |
|
1102 | 1119 | return |
|
1103 | 1120 | else: |
|
1104 | 1121 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) |
|
1105 | 1122 | for new_full_path in sources: |
|
1106 | 1123 | new_filename = os.path.basename(new_full_path) |
|
1107 | 1124 | if new_filename.startswith('ipythonrc'): |
|
1108 | 1125 | new_filename = new_filename + rc_suffix |
|
1109 | 1126 | # The config directory should only contain files, skip any |
|
1110 | 1127 | # directories which may be there (like CVS) |
|
1111 | 1128 | if os.path.isdir(new_full_path): |
|
1112 | 1129 | continue |
|
1113 | 1130 | if os.path.exists(new_filename): |
|
1114 | 1131 | old_file = new_filename+'.old' |
|
1115 | 1132 | if os.path.exists(old_file): |
|
1116 | 1133 | os.remove(old_file) |
|
1117 | 1134 | os.rename(new_filename,old_file) |
|
1118 | 1135 | shutil.copy(new_full_path,new_filename) |
|
1119 | 1136 | else: |
|
1120 | 1137 | raise ValueError,'unrecognized mode for install:',`mode` |
|
1121 | 1138 | |
|
1122 | 1139 | # Fix line-endings to those native to each platform in the config |
|
1123 | 1140 | # directory. |
|
1124 | 1141 | try: |
|
1125 | 1142 | os.chdir(ipythondir) |
|
1126 | 1143 | except: |
|
1127 | 1144 | print """ |
|
1128 | 1145 | Problem: changing to directory %s failed. |
|
1129 | 1146 | Details: |
|
1130 | 1147 | %s |
|
1131 | 1148 | |
|
1132 | 1149 | Some configuration files may have incorrect line endings. This should not |
|
1133 | 1150 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) |
|
1134 | 1151 | wait() |
|
1135 | 1152 | else: |
|
1136 | 1153 | for fname in glb('ipythonrc*'): |
|
1137 | 1154 | try: |
|
1138 | 1155 | native_line_ends(fname,backup=0) |
|
1139 | 1156 | except IOError: |
|
1140 | 1157 | pass |
|
1141 | 1158 | |
|
1142 | 1159 | if mode == 'install': |
|
1143 | 1160 | print """ |
|
1144 | 1161 | Successful installation! |
|
1145 | 1162 | |
|
1146 | 1163 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the |
|
1147 | 1164 | IPython manual (there are both HTML and PDF versions supplied with the |
|
1148 | 1165 | distribution) to make sure that your system environment is properly configured |
|
1149 | 1166 | to take advantage of IPython's features. |
|
1150 | 1167 | |
|
1151 | 1168 | Important note: the configuration system has changed! The old system is |
|
1152 | 1169 | still in place, but its setting may be partly overridden by the settings in |
|
1153 | 1170 | "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file |
|
1154 | 1171 | if some of the new settings bother you. |
|
1155 | 1172 | |
|
1156 | 1173 | """ |
|
1157 | 1174 | else: |
|
1158 | 1175 | print """ |
|
1159 | 1176 | Successful upgrade! |
|
1160 | 1177 | |
|
1161 | 1178 | All files in your directory: |
|
1162 | 1179 | %(ipythondir)s |
|
1163 | 1180 | which would have been overwritten by the upgrade were backed up with a .old |
|
1164 | 1181 | extension. If you had made particular customizations in those files you may |
|
1165 | 1182 | want to merge them back into the new files.""" % locals() |
|
1166 | 1183 | wait() |
|
1167 | 1184 | os.chdir(cwd) |
|
1168 | 1185 | # end user_setup() |
|
1169 | 1186 | |
|
1170 | 1187 | def atexit_operations(self): |
|
1171 | 1188 | """This will be executed at the time of exit. |
|
1172 | 1189 | |
|
1173 | 1190 | Saving of persistent data should be performed here. """ |
|
1174 | 1191 | |
|
1175 | 1192 | #print '*** IPython exit cleanup ***' # dbg |
|
1176 | 1193 | # input history |
|
1177 | 1194 | self.savehist() |
|
1178 | 1195 | |
|
1179 | 1196 | # Cleanup all tempfiles left around |
|
1180 | 1197 | for tfile in self.tempfiles: |
|
1181 | 1198 | try: |
|
1182 | 1199 | os.unlink(tfile) |
|
1183 | 1200 | except OSError: |
|
1184 | 1201 | pass |
|
1185 | 1202 | |
|
1186 | 1203 | # save the "persistent data" catch-all dictionary |
|
1187 | 1204 | self.hooks.shutdown_hook() |
|
1188 | 1205 | |
|
1189 | 1206 | def savehist(self): |
|
1190 | 1207 | """Save input history to a file (via readline library).""" |
|
1191 | 1208 | try: |
|
1192 | 1209 | self.readline.write_history_file(self.histfile) |
|
1193 | 1210 | except: |
|
1194 | 1211 | print 'Unable to save IPython command history to file: ' + \ |
|
1195 | 1212 | `self.histfile` |
|
1196 | 1213 | |
|
1197 | 1214 | def pre_readline(self): |
|
1198 | 1215 | """readline hook to be used at the start of each line. |
|
1199 | 1216 | |
|
1200 | 1217 | Currently it handles auto-indent only.""" |
|
1201 | 1218 | |
|
1202 | 1219 | #debugx('self.indent_current_nsp','pre_readline:') |
|
1203 | 1220 | self.readline.insert_text(self.indent_current_str()) |
|
1204 | 1221 | |
|
1205 | 1222 | def init_readline(self): |
|
1206 | 1223 | """Command history completion/saving/reloading.""" |
|
1207 | 1224 | |
|
1208 | 1225 | import IPython.rlineimpl as readline |
|
1209 | 1226 | if not readline.have_readline: |
|
1210 | 1227 | self.has_readline = 0 |
|
1211 | 1228 | self.readline = None |
|
1212 | 1229 | # no point in bugging windows users with this every time: |
|
1213 | 1230 | warn('Readline services not available on this platform.') |
|
1214 | 1231 | else: |
|
1215 | 1232 | sys.modules['readline'] = readline |
|
1216 | 1233 | import atexit |
|
1217 | 1234 | from IPython.completer import IPCompleter |
|
1218 | 1235 | self.Completer = IPCompleter(self, |
|
1219 | 1236 | self.user_ns, |
|
1220 | 1237 | self.user_global_ns, |
|
1221 | 1238 | self.rc.readline_omit__names, |
|
1222 | 1239 | self.alias_table) |
|
1223 | ||
|
1240 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
|
1241 | self.strdispatchers['complete_command'] = sdisp | |
|
1242 | self.Completer.custom_completers = sdisp | |
|
1224 | 1243 | # Platform-specific configuration |
|
1225 | 1244 | if os.name == 'nt': |
|
1226 | 1245 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1227 | 1246 | else: |
|
1228 | 1247 | self.readline_startup_hook = readline.set_startup_hook |
|
1229 | 1248 | |
|
1230 | 1249 | # Load user's initrc file (readline config) |
|
1231 | 1250 | inputrc_name = os.environ.get('INPUTRC') |
|
1232 | 1251 | if inputrc_name is None: |
|
1233 | 1252 | home_dir = get_home_dir() |
|
1234 | 1253 | if home_dir is not None: |
|
1235 | 1254 | inputrc_name = os.path.join(home_dir,'.inputrc') |
|
1236 | 1255 | if os.path.isfile(inputrc_name): |
|
1237 | 1256 | try: |
|
1238 | 1257 | readline.read_init_file(inputrc_name) |
|
1239 | 1258 | except: |
|
1240 | 1259 | warn('Problems reading readline initialization file <%s>' |
|
1241 | 1260 | % inputrc_name) |
|
1242 | 1261 | |
|
1243 | 1262 | self.has_readline = 1 |
|
1244 | 1263 | self.readline = readline |
|
1245 | 1264 | # save this in sys so embedded copies can restore it properly |
|
1246 | 1265 | sys.ipcompleter = self.Completer.complete |
|
1247 | 1266 | readline.set_completer(self.Completer.complete) |
|
1248 | 1267 | |
|
1249 | 1268 | # Configure readline according to user's prefs |
|
1250 | 1269 | for rlcommand in self.rc.readline_parse_and_bind: |
|
1251 | 1270 | readline.parse_and_bind(rlcommand) |
|
1252 | 1271 | |
|
1253 | 1272 | # remove some chars from the delimiters list |
|
1254 | 1273 | delims = readline.get_completer_delims() |
|
1255 | 1274 | delims = delims.translate(string._idmap, |
|
1256 | 1275 | self.rc.readline_remove_delims) |
|
1257 | 1276 | readline.set_completer_delims(delims) |
|
1258 | 1277 | # otherwise we end up with a monster history after a while: |
|
1259 | 1278 | readline.set_history_length(1000) |
|
1260 | 1279 | try: |
|
1261 | 1280 | #print '*** Reading readline history' # dbg |
|
1262 | 1281 | readline.read_history_file(self.histfile) |
|
1263 | 1282 | except IOError: |
|
1264 | 1283 | pass # It doesn't exist yet. |
|
1265 | 1284 | |
|
1266 | 1285 | atexit.register(self.atexit_operations) |
|
1267 | 1286 | del atexit |
|
1268 | 1287 | |
|
1269 | 1288 | # Configure auto-indent for all platforms |
|
1270 | 1289 | self.set_autoindent(self.rc.autoindent) |
|
1271 | 1290 | |
|
1272 | 1291 | def ask_yes_no(self,prompt,default=True): |
|
1273 | 1292 | if self.rc.quiet: |
|
1274 | 1293 | return True |
|
1275 | 1294 | return ask_yes_no(prompt,default) |
|
1276 | 1295 | |
|
1277 | 1296 | def _should_recompile(self,e): |
|
1278 | 1297 | """Utility routine for edit_syntax_error""" |
|
1279 | 1298 | |
|
1280 | 1299 | if e.filename in ('<ipython console>','<input>','<string>', |
|
1281 | 1300 | '<console>','<BackgroundJob compilation>', |
|
1282 | 1301 | None): |
|
1283 | 1302 | |
|
1284 | 1303 | return False |
|
1285 | 1304 | try: |
|
1286 | 1305 | if (self.rc.autoedit_syntax and |
|
1287 | 1306 | not self.ask_yes_no('Return to editor to correct syntax error? ' |
|
1288 | 1307 | '[Y/n] ','y')): |
|
1289 | 1308 | return False |
|
1290 | 1309 | except EOFError: |
|
1291 | 1310 | return False |
|
1292 | 1311 | |
|
1293 | 1312 | def int0(x): |
|
1294 | 1313 | try: |
|
1295 | 1314 | return int(x) |
|
1296 | 1315 | except TypeError: |
|
1297 | 1316 | return 0 |
|
1298 | 1317 | # always pass integer line and offset values to editor hook |
|
1299 | 1318 | self.hooks.fix_error_editor(e.filename, |
|
1300 | 1319 | int0(e.lineno),int0(e.offset),e.msg) |
|
1301 | 1320 | return True |
|
1302 | 1321 | |
|
1303 | 1322 | def edit_syntax_error(self): |
|
1304 | 1323 | """The bottom half of the syntax error handler called in the main loop. |
|
1305 | 1324 | |
|
1306 | 1325 | Loop until syntax error is fixed or user cancels. |
|
1307 | 1326 | """ |
|
1308 | 1327 | |
|
1309 | 1328 | while self.SyntaxTB.last_syntax_error: |
|
1310 | 1329 | # copy and clear last_syntax_error |
|
1311 | 1330 | err = self.SyntaxTB.clear_err_state() |
|
1312 | 1331 | if not self._should_recompile(err): |
|
1313 | 1332 | return |
|
1314 | 1333 | try: |
|
1315 | 1334 | # may set last_syntax_error again if a SyntaxError is raised |
|
1316 | 1335 | self.safe_execfile(err.filename,self.user_ns) |
|
1317 | 1336 | except: |
|
1318 | 1337 | self.showtraceback() |
|
1319 | 1338 | else: |
|
1320 | 1339 | try: |
|
1321 | 1340 | f = file(err.filename) |
|
1322 | 1341 | try: |
|
1323 | 1342 | sys.displayhook(f.read()) |
|
1324 | 1343 | finally: |
|
1325 | 1344 | f.close() |
|
1326 | 1345 | except: |
|
1327 | 1346 | self.showtraceback() |
|
1328 | 1347 | |
|
1329 | 1348 | def showsyntaxerror(self, filename=None): |
|
1330 | 1349 | """Display the syntax error that just occurred. |
|
1331 | 1350 | |
|
1332 | 1351 | This doesn't display a stack trace because there isn't one. |
|
1333 | 1352 | |
|
1334 | 1353 | If a filename is given, it is stuffed in the exception instead |
|
1335 | 1354 | of what was there before (because Python's parser always uses |
|
1336 | 1355 | "<string>" when reading from a string). |
|
1337 | 1356 | """ |
|
1338 | 1357 | etype, value, last_traceback = sys.exc_info() |
|
1339 | 1358 | |
|
1340 | 1359 | # See note about these variables in showtraceback() below |
|
1341 | 1360 | sys.last_type = etype |
|
1342 | 1361 | sys.last_value = value |
|
1343 | 1362 | sys.last_traceback = last_traceback |
|
1344 | 1363 | |
|
1345 | 1364 | if filename and etype is SyntaxError: |
|
1346 | 1365 | # Work hard to stuff the correct filename in the exception |
|
1347 | 1366 | try: |
|
1348 | 1367 | msg, (dummy_filename, lineno, offset, line) = value |
|
1349 | 1368 | except: |
|
1350 | 1369 | # Not the format we expect; leave it alone |
|
1351 | 1370 | pass |
|
1352 | 1371 | else: |
|
1353 | 1372 | # Stuff in the right filename |
|
1354 | 1373 | try: |
|
1355 | 1374 | # Assume SyntaxError is a class exception |
|
1356 | 1375 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1357 | 1376 | except: |
|
1358 | 1377 | # If that failed, assume SyntaxError is a string |
|
1359 | 1378 | value = msg, (filename, lineno, offset, line) |
|
1360 | 1379 | self.SyntaxTB(etype,value,[]) |
|
1361 | 1380 | |
|
1362 | 1381 | def debugger(self): |
|
1363 | 1382 | """Call the pydb/pdb debugger.""" |
|
1364 | 1383 | |
|
1365 | 1384 | if not self.rc.pdb: |
|
1366 | 1385 | return |
|
1367 | 1386 | have_pydb = False |
|
1368 | 1387 | if sys.version[:3] >= '2.5': |
|
1369 | 1388 | try: |
|
1370 | 1389 | from pydb import pm |
|
1371 | 1390 | have_pydb = True |
|
1372 | 1391 | except ImportError: |
|
1373 | 1392 | pass |
|
1374 | 1393 | if not have_pydb: |
|
1375 | 1394 | from pdb import pm |
|
1376 | 1395 | pm() |
|
1377 | 1396 | |
|
1378 | 1397 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): |
|
1379 | 1398 | """Display the exception that just occurred. |
|
1380 | 1399 | |
|
1381 | 1400 | If nothing is known about the exception, this is the method which |
|
1382 | 1401 | should be used throughout the code for presenting user tracebacks, |
|
1383 | 1402 | rather than directly invoking the InteractiveTB object. |
|
1384 | 1403 | |
|
1385 | 1404 | A specific showsyntaxerror() also exists, but this method can take |
|
1386 | 1405 | care of calling it if needed, so unless you are explicitly catching a |
|
1387 | 1406 | SyntaxError exception, don't try to analyze the stack manually and |
|
1388 | 1407 | simply call this method.""" |
|
1389 | 1408 | |
|
1390 | 1409 | # Though this won't be called by syntax errors in the input line, |
|
1391 | 1410 | # there may be SyntaxError cases whith imported code. |
|
1392 | 1411 | if exc_tuple is None: |
|
1393 | 1412 | etype, value, tb = sys.exc_info() |
|
1394 | 1413 | else: |
|
1395 | 1414 | etype, value, tb = exc_tuple |
|
1396 | 1415 | if etype is SyntaxError: |
|
1397 | 1416 | self.showsyntaxerror(filename) |
|
1398 | 1417 | else: |
|
1399 | 1418 | # WARNING: these variables are somewhat deprecated and not |
|
1400 | 1419 | # necessarily safe to use in a threaded environment, but tools |
|
1401 | 1420 | # like pdb depend on their existence, so let's set them. If we |
|
1402 | 1421 | # find problems in the field, we'll need to revisit their use. |
|
1403 | 1422 | sys.last_type = etype |
|
1404 | 1423 | sys.last_value = value |
|
1405 | 1424 | sys.last_traceback = tb |
|
1406 | 1425 | |
|
1407 | 1426 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) |
|
1408 | 1427 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1409 | 1428 | # pdb mucks up readline, fix it back |
|
1410 | 1429 | self.readline.set_completer(self.Completer.complete) |
|
1411 | 1430 | |
|
1412 | 1431 | def mainloop(self,banner=None): |
|
1413 | 1432 | """Creates the local namespace and starts the mainloop. |
|
1414 | 1433 | |
|
1415 | 1434 | If an optional banner argument is given, it will override the |
|
1416 | 1435 | internally created default banner.""" |
|
1417 | 1436 | |
|
1418 | 1437 | if self.rc.c: # Emulate Python's -c option |
|
1419 | 1438 | self.exec_init_cmd() |
|
1420 | 1439 | if banner is None: |
|
1421 | 1440 | if not self.rc.banner: |
|
1422 | 1441 | banner = '' |
|
1423 | 1442 | # banner is string? Use it directly! |
|
1424 | 1443 | elif isinstance(self.rc.banner,basestring): |
|
1425 | 1444 | banner = self.rc.banner |
|
1426 | 1445 | else: |
|
1427 | 1446 | banner = self.BANNER+self.banner2 |
|
1428 | 1447 | |
|
1429 | 1448 | self.interact(banner) |
|
1430 | 1449 | |
|
1431 | 1450 | def exec_init_cmd(self): |
|
1432 | 1451 | """Execute a command given at the command line. |
|
1433 | 1452 | |
|
1434 | 1453 | This emulates Python's -c option.""" |
|
1435 | 1454 | |
|
1436 | 1455 | #sys.argv = ['-c'] |
|
1437 | 1456 | self.push(self.rc.c) |
|
1438 | 1457 | |
|
1439 | 1458 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): |
|
1440 | 1459 | """Embeds IPython into a running python program. |
|
1441 | 1460 | |
|
1442 | 1461 | Input: |
|
1443 | 1462 | |
|
1444 | 1463 | - header: An optional header message can be specified. |
|
1445 | 1464 | |
|
1446 | 1465 | - local_ns, global_ns: working namespaces. If given as None, the |
|
1447 | 1466 | IPython-initialized one is updated with __main__.__dict__, so that |
|
1448 | 1467 | program variables become visible but user-specific configuration |
|
1449 | 1468 | remains possible. |
|
1450 | 1469 | |
|
1451 | 1470 | - stack_depth: specifies how many levels in the stack to go to |
|
1452 | 1471 | looking for namespaces (when local_ns and global_ns are None). This |
|
1453 | 1472 | allows an intermediate caller to make sure that this function gets |
|
1454 | 1473 | the namespace from the intended level in the stack. By default (0) |
|
1455 | 1474 | it will get its locals and globals from the immediate caller. |
|
1456 | 1475 | |
|
1457 | 1476 | Warning: it's possible to use this in a program which is being run by |
|
1458 | 1477 | IPython itself (via %run), but some funny things will happen (a few |
|
1459 | 1478 | globals get overwritten). In the future this will be cleaned up, as |
|
1460 | 1479 | there is no fundamental reason why it can't work perfectly.""" |
|
1461 | 1480 | |
|
1462 | 1481 | # Get locals and globals from caller |
|
1463 | 1482 | if local_ns is None or global_ns is None: |
|
1464 | 1483 | call_frame = sys._getframe(stack_depth).f_back |
|
1465 | 1484 | |
|
1466 | 1485 | if local_ns is None: |
|
1467 | 1486 | local_ns = call_frame.f_locals |
|
1468 | 1487 | if global_ns is None: |
|
1469 | 1488 | global_ns = call_frame.f_globals |
|
1470 | 1489 | |
|
1471 | 1490 | # Update namespaces and fire up interpreter |
|
1472 | 1491 | |
|
1473 | 1492 | # The global one is easy, we can just throw it in |
|
1474 | 1493 | self.user_global_ns = global_ns |
|
1475 | 1494 | |
|
1476 | 1495 | # but the user/local one is tricky: ipython needs it to store internal |
|
1477 | 1496 | # data, but we also need the locals. We'll copy locals in the user |
|
1478 | 1497 | # one, but will track what got copied so we can delete them at exit. |
|
1479 | 1498 | # This is so that a later embedded call doesn't see locals from a |
|
1480 | 1499 | # previous call (which most likely existed in a separate scope). |
|
1481 | 1500 | local_varnames = local_ns.keys() |
|
1482 | 1501 | self.user_ns.update(local_ns) |
|
1483 | 1502 | |
|
1484 | 1503 | # Patch for global embedding to make sure that things don't overwrite |
|
1485 | 1504 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
1486 | 1505 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
1487 | 1506 | if local_ns is None and global_ns is None: |
|
1488 | 1507 | self.user_global_ns.update(__main__.__dict__) |
|
1489 | 1508 | |
|
1490 | 1509 | # make sure the tab-completer has the correct frame information, so it |
|
1491 | 1510 | # actually completes using the frame's locals/globals |
|
1492 | 1511 | self.set_completer_frame() |
|
1493 | 1512 | |
|
1494 | 1513 | # before activating the interactive mode, we need to make sure that |
|
1495 | 1514 | # all names in the builtin namespace needed by ipython point to |
|
1496 | 1515 | # ourselves, and not to other instances. |
|
1497 | 1516 | self.add_builtins() |
|
1498 | 1517 | |
|
1499 | 1518 | self.interact(header) |
|
1500 | 1519 | |
|
1501 | 1520 | # now, purge out the user namespace from anything we might have added |
|
1502 | 1521 | # from the caller's local namespace |
|
1503 | 1522 | delvar = self.user_ns.pop |
|
1504 | 1523 | for var in local_varnames: |
|
1505 | 1524 | delvar(var,None) |
|
1506 | 1525 | # and clean builtins we may have overridden |
|
1507 | 1526 | self.clean_builtins() |
|
1508 | 1527 | |
|
1509 | 1528 | def interact(self, banner=None): |
|
1510 | 1529 | """Closely emulate the interactive Python console. |
|
1511 | 1530 | |
|
1512 | 1531 | The optional banner argument specify the banner to print |
|
1513 | 1532 | before the first interaction; by default it prints a banner |
|
1514 | 1533 | similar to the one printed by the real Python interpreter, |
|
1515 | 1534 | followed by the current class name in parentheses (so as not |
|
1516 | 1535 | to confuse this with the real interpreter -- since it's so |
|
1517 | 1536 | close!). |
|
1518 | 1537 | |
|
1519 | 1538 | """ |
|
1520 | 1539 | |
|
1521 | 1540 | if self.exit_now: |
|
1522 | 1541 | # batch run -> do not interact |
|
1523 | 1542 | return |
|
1524 | 1543 | cprt = 'Type "copyright", "credits" or "license" for more information.' |
|
1525 | 1544 | if banner is None: |
|
1526 | 1545 | self.write("Python %s on %s\n%s\n(%s)\n" % |
|
1527 | 1546 | (sys.version, sys.platform, cprt, |
|
1528 | 1547 | self.__class__.__name__)) |
|
1529 | 1548 | else: |
|
1530 | 1549 | self.write(banner) |
|
1531 | 1550 | |
|
1532 | 1551 | more = 0 |
|
1533 | 1552 | |
|
1534 | 1553 | # Mark activity in the builtins |
|
1535 | 1554 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1536 | 1555 | |
|
1537 | 1556 | # exit_now is set by a call to %Exit or %Quit |
|
1538 | 1557 | while not self.exit_now: |
|
1539 | 1558 | if more: |
|
1540 | 1559 | prompt = self.hooks.generate_prompt(True) |
|
1541 | 1560 | if self.autoindent: |
|
1542 | 1561 | self.readline_startup_hook(self.pre_readline) |
|
1543 | 1562 | else: |
|
1544 | 1563 | prompt = self.hooks.generate_prompt(False) |
|
1545 | 1564 | try: |
|
1546 | 1565 | line = self.raw_input(prompt,more) |
|
1547 | 1566 | if self.exit_now: |
|
1548 | 1567 | # quick exit on sys.std[in|out] close |
|
1549 | 1568 | break |
|
1550 | 1569 | if self.autoindent: |
|
1551 | 1570 | self.readline_startup_hook(None) |
|
1552 | 1571 | except KeyboardInterrupt: |
|
1553 | 1572 | self.write('\nKeyboardInterrupt\n') |
|
1554 | 1573 | self.resetbuffer() |
|
1555 | 1574 | # keep cache in sync with the prompt counter: |
|
1556 | 1575 | self.outputcache.prompt_count -= 1 |
|
1557 | 1576 | |
|
1558 | 1577 | if self.autoindent: |
|
1559 | 1578 | self.indent_current_nsp = 0 |
|
1560 | 1579 | more = 0 |
|
1561 | 1580 | except EOFError: |
|
1562 | 1581 | if self.autoindent: |
|
1563 | 1582 | self.readline_startup_hook(None) |
|
1564 | 1583 | self.write('\n') |
|
1565 | 1584 | self.exit() |
|
1566 | 1585 | except bdb.BdbQuit: |
|
1567 | 1586 | warn('The Python debugger has exited with a BdbQuit exception.\n' |
|
1568 | 1587 | 'Because of how pdb handles the stack, it is impossible\n' |
|
1569 | 1588 | 'for IPython to properly format this particular exception.\n' |
|
1570 | 1589 | 'IPython will resume normal operation.') |
|
1571 | 1590 | except: |
|
1572 | 1591 | # exceptions here are VERY RARE, but they can be triggered |
|
1573 | 1592 | # asynchronously by signal handlers, for example. |
|
1574 | 1593 | self.showtraceback() |
|
1575 | 1594 | else: |
|
1576 | 1595 | more = self.push(line) |
|
1577 | 1596 | if (self.SyntaxTB.last_syntax_error and |
|
1578 | 1597 | self.rc.autoedit_syntax): |
|
1579 | 1598 | self.edit_syntax_error() |
|
1580 | 1599 | |
|
1581 | 1600 | # We are off again... |
|
1582 | 1601 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
1583 | 1602 | |
|
1584 | 1603 | def excepthook(self, etype, value, tb): |
|
1585 | 1604 | """One more defense for GUI apps that call sys.excepthook. |
|
1586 | 1605 | |
|
1587 | 1606 | GUI frameworks like wxPython trap exceptions and call |
|
1588 | 1607 | sys.excepthook themselves. I guess this is a feature that |
|
1589 | 1608 | enables them to keep running after exceptions that would |
|
1590 | 1609 | otherwise kill their mainloop. This is a bother for IPython |
|
1591 | 1610 | which excepts to catch all of the program exceptions with a try: |
|
1592 | 1611 | except: statement. |
|
1593 | 1612 | |
|
1594 | 1613 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1595 | 1614 | any app directly invokes sys.excepthook, it will look to the user like |
|
1596 | 1615 | IPython crashed. In order to work around this, we can disable the |
|
1597 | 1616 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1598 | 1617 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1599 | 1618 | call sys.excepthook will generate a regular-looking exception from |
|
1600 | 1619 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1601 | 1620 | crashes. |
|
1602 | 1621 | |
|
1603 | 1622 | This hook should be used sparingly, only in places which are not likely |
|
1604 | 1623 | to be true IPython errors. |
|
1605 | 1624 | """ |
|
1606 | 1625 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1607 | 1626 | |
|
1608 | 1627 | def expand_aliases(self,fn,rest): |
|
1609 | 1628 | """ Expand multiple levels of aliases: |
|
1610 | 1629 | |
|
1611 | 1630 | if: |
|
1612 | 1631 | |
|
1613 | 1632 | alias foo bar /tmp |
|
1614 | 1633 | alias baz foo |
|
1615 | 1634 | |
|
1616 | 1635 | then: |
|
1617 | 1636 | |
|
1618 | 1637 | baz huhhahhei -> bar /tmp huhhahhei |
|
1619 | 1638 | |
|
1620 | 1639 | """ |
|
1621 | 1640 | line = fn + " " + rest |
|
1622 | 1641 | |
|
1623 | 1642 | done = Set() |
|
1624 | 1643 | while 1: |
|
1625 | 1644 | pre,fn,rest = self.split_user_input(line) |
|
1626 | 1645 | if fn in self.alias_table: |
|
1627 | 1646 | if fn in done: |
|
1628 | 1647 | warn("Cyclic alias definition, repeated '%s'" % fn) |
|
1629 | 1648 | return "" |
|
1630 | 1649 | done.add(fn) |
|
1631 | 1650 | |
|
1632 | 1651 | l2 = self.transform_alias(fn,rest) |
|
1633 | 1652 | # dir -> dir |
|
1634 | 1653 | if l2 == line: |
|
1635 | 1654 | break |
|
1636 | 1655 | # ls -> ls -F should not recurse forever |
|
1637 | 1656 | if l2.split(None,1)[0] == line.split(None,1)[0]: |
|
1638 | 1657 | line = l2 |
|
1639 | 1658 | break |
|
1640 | 1659 | |
|
1641 | 1660 | line=l2 |
|
1642 | 1661 | |
|
1643 | 1662 | |
|
1644 | 1663 | # print "al expand to",line #dbg |
|
1645 | 1664 | else: |
|
1646 | 1665 | break |
|
1647 | 1666 | |
|
1648 | 1667 | return line |
|
1649 | 1668 | |
|
1650 | 1669 | def transform_alias(self, alias,rest=''): |
|
1651 | 1670 | """ Transform alias to system command string. |
|
1652 | 1671 | """ |
|
1653 | 1672 | nargs,cmd = self.alias_table[alias] |
|
1654 | 1673 | if ' ' in cmd and os.path.isfile(cmd): |
|
1655 | 1674 | cmd = '"%s"' % cmd |
|
1656 | 1675 | |
|
1657 | 1676 | # Expand the %l special to be the user's input line |
|
1658 | 1677 | if cmd.find('%l') >= 0: |
|
1659 | 1678 | cmd = cmd.replace('%l',rest) |
|
1660 | 1679 | rest = '' |
|
1661 | 1680 | if nargs==0: |
|
1662 | 1681 | # Simple, argument-less aliases |
|
1663 | 1682 | cmd = '%s %s' % (cmd,rest) |
|
1664 | 1683 | else: |
|
1665 | 1684 | # Handle aliases with positional arguments |
|
1666 | 1685 | args = rest.split(None,nargs) |
|
1667 | 1686 | if len(args)< nargs: |
|
1668 | 1687 | error('Alias <%s> requires %s arguments, %s given.' % |
|
1669 | 1688 | (alias,nargs,len(args))) |
|
1670 | 1689 | return None |
|
1671 | 1690 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
1672 | 1691 | # Now call the macro, evaluating in the user's namespace |
|
1673 | 1692 | #print 'new command: <%r>' % cmd # dbg |
|
1674 | 1693 | return cmd |
|
1675 | 1694 | |
|
1676 | 1695 | def call_alias(self,alias,rest=''): |
|
1677 | 1696 | """Call an alias given its name and the rest of the line. |
|
1678 | 1697 | |
|
1679 | 1698 | This is only used to provide backwards compatibility for users of |
|
1680 | 1699 | ipalias(), use of which is not recommended for anymore.""" |
|
1681 | 1700 | |
|
1682 | 1701 | # Now call the macro, evaluating in the user's namespace |
|
1683 | 1702 | cmd = self.transform_alias(alias, rest) |
|
1684 | 1703 | try: |
|
1685 | 1704 | self.system(cmd) |
|
1686 | 1705 | except: |
|
1687 | 1706 | self.showtraceback() |
|
1688 | 1707 | |
|
1689 | 1708 | def indent_current_str(self): |
|
1690 | 1709 | """return the current level of indentation as a string""" |
|
1691 | 1710 | return self.indent_current_nsp * ' ' |
|
1692 | 1711 | |
|
1693 | 1712 | def autoindent_update(self,line): |
|
1694 | 1713 | """Keep track of the indent level.""" |
|
1695 | 1714 | |
|
1696 | 1715 | #debugx('line') |
|
1697 | 1716 | #debugx('self.indent_current_nsp') |
|
1698 | 1717 | if self.autoindent: |
|
1699 | 1718 | if line: |
|
1700 | 1719 | inisp = num_ini_spaces(line) |
|
1701 | 1720 | if inisp < self.indent_current_nsp: |
|
1702 | 1721 | self.indent_current_nsp = inisp |
|
1703 | 1722 | |
|
1704 | 1723 | if line[-1] == ':': |
|
1705 | 1724 | self.indent_current_nsp += 4 |
|
1706 | 1725 | elif dedent_re.match(line): |
|
1707 | 1726 | self.indent_current_nsp -= 4 |
|
1708 | 1727 | else: |
|
1709 | 1728 | self.indent_current_nsp = 0 |
|
1710 | 1729 | |
|
1711 | 1730 | def runlines(self,lines): |
|
1712 | 1731 | """Run a string of one or more lines of source. |
|
1713 | 1732 | |
|
1714 | 1733 | This method is capable of running a string containing multiple source |
|
1715 | 1734 | lines, as if they had been entered at the IPython prompt. Since it |
|
1716 | 1735 | exposes IPython's processing machinery, the given strings can contain |
|
1717 | 1736 | magic calls (%magic), special shell access (!cmd), etc.""" |
|
1718 | 1737 | |
|
1719 | 1738 | # We must start with a clean buffer, in case this is run from an |
|
1720 | 1739 | # interactive IPython session (via a magic, for example). |
|
1721 | 1740 | self.resetbuffer() |
|
1722 | 1741 | lines = lines.split('\n') |
|
1723 | 1742 | more = 0 |
|
1724 | 1743 | for line in lines: |
|
1725 | 1744 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1726 | 1745 | # NOT skip even a blank line if we are in a code block (more is |
|
1727 | 1746 | # true) |
|
1728 | 1747 | if line or more: |
|
1729 | 1748 | more = self.push(self.prefilter(line,more)) |
|
1730 | 1749 | # IPython's runsource returns None if there was an error |
|
1731 | 1750 | # compiling the code. This allows us to stop processing right |
|
1732 | 1751 | # away, so the user gets the error message at the right place. |
|
1733 | 1752 | if more is None: |
|
1734 | 1753 | break |
|
1735 | 1754 | # final newline in case the input didn't have it, so that the code |
|
1736 | 1755 | # actually does get executed |
|
1737 | 1756 | if more: |
|
1738 | 1757 | self.push('\n') |
|
1739 | 1758 | |
|
1740 | 1759 | def runsource(self, source, filename='<input>', symbol='single'): |
|
1741 | 1760 | """Compile and run some source in the interpreter. |
|
1742 | 1761 | |
|
1743 | 1762 | Arguments are as for compile_command(). |
|
1744 | 1763 | |
|
1745 | 1764 | One several things can happen: |
|
1746 | 1765 | |
|
1747 | 1766 | 1) The input is incorrect; compile_command() raised an |
|
1748 | 1767 | exception (SyntaxError or OverflowError). A syntax traceback |
|
1749 | 1768 | will be printed by calling the showsyntaxerror() method. |
|
1750 | 1769 | |
|
1751 | 1770 | 2) The input is incomplete, and more input is required; |
|
1752 | 1771 | compile_command() returned None. Nothing happens. |
|
1753 | 1772 | |
|
1754 | 1773 | 3) The input is complete; compile_command() returned a code |
|
1755 | 1774 | object. The code is executed by calling self.runcode() (which |
|
1756 | 1775 | also handles run-time exceptions, except for SystemExit). |
|
1757 | 1776 | |
|
1758 | 1777 | The return value is: |
|
1759 | 1778 | |
|
1760 | 1779 | - True in case 2 |
|
1761 | 1780 | |
|
1762 | 1781 | - False in the other cases, unless an exception is raised, where |
|
1763 | 1782 | None is returned instead. This can be used by external callers to |
|
1764 | 1783 | know whether to continue feeding input or not. |
|
1765 | 1784 | |
|
1766 | 1785 | The return value can be used to decide whether to use sys.ps1 or |
|
1767 | 1786 | sys.ps2 to prompt the next line.""" |
|
1768 | 1787 | |
|
1769 | 1788 | try: |
|
1770 | 1789 | code = self.compile(source,filename,symbol) |
|
1771 | 1790 | except (OverflowError, SyntaxError, ValueError): |
|
1772 | 1791 | # Case 1 |
|
1773 | 1792 | self.showsyntaxerror(filename) |
|
1774 | 1793 | return None |
|
1775 | 1794 | |
|
1776 | 1795 | if code is None: |
|
1777 | 1796 | # Case 2 |
|
1778 | 1797 | return True |
|
1779 | 1798 | |
|
1780 | 1799 | # Case 3 |
|
1781 | 1800 | # We store the code object so that threaded shells and |
|
1782 | 1801 | # custom exception handlers can access all this info if needed. |
|
1783 | 1802 | # The source corresponding to this can be obtained from the |
|
1784 | 1803 | # buffer attribute as '\n'.join(self.buffer). |
|
1785 | 1804 | self.code_to_run = code |
|
1786 | 1805 | # now actually execute the code object |
|
1787 | 1806 | if self.runcode(code) == 0: |
|
1788 | 1807 | return False |
|
1789 | 1808 | else: |
|
1790 | 1809 | return None |
|
1791 | 1810 | |
|
1792 | 1811 | def runcode(self,code_obj): |
|
1793 | 1812 | """Execute a code object. |
|
1794 | 1813 | |
|
1795 | 1814 | When an exception occurs, self.showtraceback() is called to display a |
|
1796 | 1815 | traceback. |
|
1797 | 1816 | |
|
1798 | 1817 | Return value: a flag indicating whether the code to be run completed |
|
1799 | 1818 | successfully: |
|
1800 | 1819 | |
|
1801 | 1820 | - 0: successful execution. |
|
1802 | 1821 | - 1: an error occurred. |
|
1803 | 1822 | """ |
|
1804 | 1823 | |
|
1805 | 1824 | # Set our own excepthook in case the user code tries to call it |
|
1806 | 1825 | # directly, so that the IPython crash handler doesn't get triggered |
|
1807 | 1826 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
1808 | 1827 | |
|
1809 | 1828 | # we save the original sys.excepthook in the instance, in case config |
|
1810 | 1829 | # code (such as magics) needs access to it. |
|
1811 | 1830 | self.sys_excepthook = old_excepthook |
|
1812 | 1831 | outflag = 1 # happens in more places, so it's easier as default |
|
1813 | 1832 | try: |
|
1814 | 1833 | try: |
|
1815 | 1834 | # Embedded instances require separate global/local namespaces |
|
1816 | 1835 | # so they can see both the surrounding (local) namespace and |
|
1817 | 1836 | # the module-level globals when called inside another function. |
|
1818 | 1837 | if self.embedded: |
|
1819 | 1838 | exec code_obj in self.user_global_ns, self.user_ns |
|
1820 | 1839 | # Normal (non-embedded) instances should only have a single |
|
1821 | 1840 | # namespace for user code execution, otherwise functions won't |
|
1822 | 1841 | # see interactive top-level globals. |
|
1823 | 1842 | else: |
|
1824 | 1843 | exec code_obj in self.user_ns |
|
1825 | 1844 | finally: |
|
1826 | 1845 | # Reset our crash handler in place |
|
1827 | 1846 | sys.excepthook = old_excepthook |
|
1828 | 1847 | except SystemExit: |
|
1829 | 1848 | self.resetbuffer() |
|
1830 | 1849 | self.showtraceback() |
|
1831 | 1850 | warn("Type %exit or %quit to exit IPython " |
|
1832 | 1851 | "(%Exit or %Quit do so unconditionally).",level=1) |
|
1833 | 1852 | except self.custom_exceptions: |
|
1834 | 1853 | etype,value,tb = sys.exc_info() |
|
1835 | 1854 | self.CustomTB(etype,value,tb) |
|
1836 | 1855 | except: |
|
1837 | 1856 | self.showtraceback() |
|
1838 | 1857 | else: |
|
1839 | 1858 | outflag = 0 |
|
1840 | 1859 | if softspace(sys.stdout, 0): |
|
1841 | 1860 | |
|
1842 | 1861 | # Flush out code object which has been run (and source) |
|
1843 | 1862 | self.code_to_run = None |
|
1844 | 1863 | return outflag |
|
1845 | 1864 | |
|
1846 | 1865 | def push(self, line): |
|
1847 | 1866 | """Push a line to the interpreter. |
|
1848 | 1867 | |
|
1849 | 1868 | The line should not have a trailing newline; it may have |
|
1850 | 1869 | internal newlines. The line is appended to a buffer and the |
|
1851 | 1870 | interpreter's runsource() method is called with the |
|
1852 | 1871 | concatenated contents of the buffer as source. If this |
|
1853 | 1872 | indicates that the command was executed or invalid, the buffer |
|
1854 | 1873 | is reset; otherwise, the command is incomplete, and the buffer |
|
1855 | 1874 | is left as it was after the line was appended. The return |
|
1856 | 1875 | value is 1 if more input is required, 0 if the line was dealt |
|
1857 | 1876 | with in some way (this is the same as runsource()). |
|
1858 | 1877 | """ |
|
1859 | 1878 | |
|
1860 | 1879 | # autoindent management should be done here, and not in the |
|
1861 | 1880 | # interactive loop, since that one is only seen by keyboard input. We |
|
1862 | 1881 | # need this done correctly even for code run via runlines (which uses |
|
1863 | 1882 | # push). |
|
1864 | 1883 | |
|
1865 | 1884 | #print 'push line: <%s>' % line # dbg |
|
1866 | 1885 | for subline in line.splitlines(): |
|
1867 | 1886 | self.autoindent_update(subline) |
|
1868 | 1887 | self.buffer.append(line) |
|
1869 | 1888 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
1870 | 1889 | if not more: |
|
1871 | 1890 | self.resetbuffer() |
|
1872 | 1891 | return more |
|
1873 | 1892 | |
|
1874 | 1893 | def resetbuffer(self): |
|
1875 | 1894 | """Reset the input buffer.""" |
|
1876 | 1895 | self.buffer[:] = [] |
|
1877 | 1896 | |
|
1878 | 1897 | def raw_input(self,prompt='',continue_prompt=False): |
|
1879 | 1898 | """Write a prompt and read a line. |
|
1880 | 1899 | |
|
1881 | 1900 | The returned line does not include the trailing newline. |
|
1882 | 1901 | When the user enters the EOF key sequence, EOFError is raised. |
|
1883 | 1902 | |
|
1884 | 1903 | Optional inputs: |
|
1885 | 1904 | |
|
1886 | 1905 | - prompt(''): a string to be printed to prompt the user. |
|
1887 | 1906 | |
|
1888 | 1907 | - continue_prompt(False): whether this line is the first one or a |
|
1889 | 1908 | continuation in a sequence of inputs. |
|
1890 | 1909 | """ |
|
1891 | 1910 | |
|
1892 | 1911 | try: |
|
1893 | 1912 | line = raw_input_original(prompt) |
|
1894 | 1913 | except ValueError: |
|
1895 | 1914 | warn("\n********\nYou or a %run:ed script called sys.stdin.close() or sys.stdout.close()!\nExiting IPython!") |
|
1896 | 1915 | self.exit_now = True |
|
1897 | 1916 | return "" |
|
1898 | 1917 | |
|
1899 | 1918 | |
|
1900 | 1919 | # Try to be reasonably smart about not re-indenting pasted input more |
|
1901 | 1920 | # than necessary. We do this by trimming out the auto-indent initial |
|
1902 | 1921 | # spaces, if the user's actual input started itself with whitespace. |
|
1903 | 1922 | #debugx('self.buffer[-1]') |
|
1904 | 1923 | |
|
1905 | 1924 | if self.autoindent: |
|
1906 | 1925 | if num_ini_spaces(line) > self.indent_current_nsp: |
|
1907 | 1926 | line = line[self.indent_current_nsp:] |
|
1908 | 1927 | self.indent_current_nsp = 0 |
|
1909 | 1928 | |
|
1910 | 1929 | # store the unfiltered input before the user has any chance to modify |
|
1911 | 1930 | # it. |
|
1912 | 1931 | if line.strip(): |
|
1913 | 1932 | if continue_prompt: |
|
1914 | 1933 | self.input_hist_raw[-1] += '%s\n' % line |
|
1915 | 1934 | if self.has_readline: # and some config option is set? |
|
1916 | 1935 | try: |
|
1917 | 1936 | histlen = self.readline.get_current_history_length() |
|
1918 | 1937 | newhist = self.input_hist_raw[-1].rstrip() |
|
1919 | 1938 | self.readline.remove_history_item(histlen-1) |
|
1920 | 1939 | self.readline.replace_history_item(histlen-2,newhist) |
|
1921 | 1940 | except AttributeError: |
|
1922 | 1941 | pass # re{move,place}_history_item are new in 2.4. |
|
1923 | 1942 | else: |
|
1924 | 1943 | self.input_hist_raw.append('%s\n' % line) |
|
1925 | 1944 | |
|
1926 | 1945 | try: |
|
1927 | 1946 | lineout = self.prefilter(line,continue_prompt) |
|
1928 | 1947 | except: |
|
1929 | 1948 | # blanket except, in case a user-defined prefilter crashes, so it |
|
1930 | 1949 | # can't take all of ipython with it. |
|
1931 | 1950 | self.showtraceback() |
|
1932 | 1951 | return '' |
|
1933 | 1952 | else: |
|
1934 | 1953 | return lineout |
|
1935 | 1954 | |
|
1936 | 1955 | def split_user_input(self,line): |
|
1937 | 1956 | """Split user input into pre-char, function part and rest.""" |
|
1938 | 1957 | |
|
1939 | 1958 | lsplit = self.line_split.match(line) |
|
1940 | 1959 | if lsplit is None: # no regexp match returns None |
|
1941 | 1960 | try: |
|
1942 | 1961 | iFun,theRest = line.split(None,1) |
|
1943 | 1962 | except ValueError: |
|
1944 | 1963 | iFun,theRest = line,'' |
|
1945 | 1964 | pre = re.match('^(\s*)(.*)',line).groups()[0] |
|
1946 | 1965 | else: |
|
1947 | 1966 | pre,iFun,theRest = lsplit.groups() |
|
1948 | 1967 | |
|
1949 | 1968 | #print 'line:<%s>' % line # dbg |
|
1950 | 1969 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg |
|
1951 | 1970 | return pre,iFun.strip(),theRest |
|
1952 | 1971 | |
|
1953 | 1972 | def _prefilter(self, line, continue_prompt): |
|
1954 | 1973 | """Calls different preprocessors, depending on the form of line.""" |
|
1955 | 1974 | |
|
1956 | 1975 | # All handlers *must* return a value, even if it's blank (''). |
|
1957 | 1976 | |
|
1958 | 1977 | # Lines are NOT logged here. Handlers should process the line as |
|
1959 | 1978 | # needed, update the cache AND log it (so that the input cache array |
|
1960 | 1979 | # stays synced). |
|
1961 | 1980 | |
|
1962 | 1981 | # This function is _very_ delicate, and since it's also the one which |
|
1963 | 1982 | # determines IPython's response to user input, it must be as efficient |
|
1964 | 1983 | # as possible. For this reason it has _many_ returns in it, trying |
|
1965 | 1984 | # always to exit as quickly as it can figure out what it needs to do. |
|
1966 | 1985 | |
|
1967 | 1986 | # This function is the main responsible for maintaining IPython's |
|
1968 | 1987 | # behavior respectful of Python's semantics. So be _very_ careful if |
|
1969 | 1988 | # making changes to anything here. |
|
1970 | 1989 | |
|
1971 | 1990 | #..................................................................... |
|
1972 | 1991 | # Code begins |
|
1973 | 1992 | |
|
1974 | 1993 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg |
|
1975 | 1994 | |
|
1976 | 1995 | # save the line away in case we crash, so the post-mortem handler can |
|
1977 | 1996 | # record it |
|
1978 | 1997 | self._last_input_line = line |
|
1979 | 1998 | |
|
1980 | 1999 | #print '***line: <%s>' % line # dbg |
|
1981 | 2000 | |
|
1982 | 2001 | # the input history needs to track even empty lines |
|
1983 | 2002 | stripped = line.strip() |
|
1984 | 2003 | |
|
1985 | 2004 | if not stripped: |
|
1986 | 2005 | if not continue_prompt: |
|
1987 | 2006 | self.outputcache.prompt_count -= 1 |
|
1988 | 2007 | return self.handle_normal(line,continue_prompt) |
|
1989 | 2008 | #return self.handle_normal('',continue_prompt) |
|
1990 | 2009 | |
|
1991 | 2010 | # print '***cont',continue_prompt # dbg |
|
1992 | 2011 | # special handlers are only allowed for single line statements |
|
1993 | 2012 | if continue_prompt and not self.rc.multi_line_specials: |
|
1994 | 2013 | return self.handle_normal(line,continue_prompt) |
|
1995 | 2014 | |
|
1996 | 2015 | |
|
1997 | 2016 | # For the rest, we need the structure of the input |
|
1998 | 2017 | pre,iFun,theRest = self.split_user_input(line) |
|
1999 | 2018 | |
|
2000 | 2019 | # See whether any pre-existing handler can take care of it |
|
2001 | 2020 | |
|
2002 | 2021 | rewritten = self.hooks.input_prefilter(stripped) |
|
2003 | 2022 | if rewritten != stripped: # ok, some prefilter did something |
|
2004 | 2023 | rewritten = pre + rewritten # add indentation |
|
2005 | 2024 | return self.handle_normal(rewritten) |
|
2006 | 2025 | |
|
2007 | 2026 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2008 | 2027 | |
|
2009 | 2028 | # First check for explicit escapes in the last/first character |
|
2010 | 2029 | handler = None |
|
2011 | 2030 | if line[-1] == self.ESC_HELP: |
|
2012 | 2031 | handler = self.esc_handlers.get(line[-1]) # the ? can be at the end |
|
2013 | 2032 | if handler is None: |
|
2014 | 2033 | # look at the first character of iFun, NOT of line, so we skip |
|
2015 | 2034 | # leading whitespace in multiline input |
|
2016 | 2035 | handler = self.esc_handlers.get(iFun[0:1]) |
|
2017 | 2036 | if handler is not None: |
|
2018 | 2037 | return handler(line,continue_prompt,pre,iFun,theRest) |
|
2019 | 2038 | # Emacs ipython-mode tags certain input lines |
|
2020 | 2039 | if line.endswith('# PYTHON-MODE'): |
|
2021 | 2040 | return self.handle_emacs(line,continue_prompt) |
|
2022 | 2041 | |
|
2023 | 2042 | # Next, check if we can automatically execute this thing |
|
2024 | 2043 | |
|
2025 | 2044 | # Allow ! in multi-line statements if multi_line_specials is on: |
|
2026 | 2045 | if continue_prompt and self.rc.multi_line_specials and \ |
|
2027 | 2046 | iFun.startswith(self.ESC_SHELL): |
|
2028 | 2047 | return self.handle_shell_escape(line,continue_prompt, |
|
2029 | 2048 | pre=pre,iFun=iFun, |
|
2030 | 2049 | theRest=theRest) |
|
2031 | 2050 | |
|
2032 | 2051 | # Let's try to find if the input line is a magic fn |
|
2033 | 2052 | oinfo = None |
|
2034 | 2053 | if hasattr(self,'magic_'+iFun): |
|
2035 | 2054 | # WARNING: _ofind uses getattr(), so it can consume generators and |
|
2036 | 2055 | # cause other side effects. |
|
2037 | 2056 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
2038 | 2057 | if oinfo['ismagic']: |
|
2039 | 2058 | # Be careful not to call magics when a variable assignment is |
|
2040 | 2059 | # being made (ls='hi', for example) |
|
2041 | 2060 | if self.rc.automagic and \ |
|
2042 | 2061 | (len(theRest)==0 or theRest[0] not in '!=()<>,') and \ |
|
2043 | 2062 | (self.rc.multi_line_specials or not continue_prompt): |
|
2044 | 2063 | return self.handle_magic(line,continue_prompt, |
|
2045 | 2064 | pre,iFun,theRest) |
|
2046 | 2065 | else: |
|
2047 | 2066 | return self.handle_normal(line,continue_prompt) |
|
2048 | 2067 | |
|
2049 | 2068 | # If the rest of the line begins with an (in)equality, assginment or |
|
2050 | 2069 | # function call, we should not call _ofind but simply execute it. |
|
2051 | 2070 | # This avoids spurious geattr() accesses on objects upon assignment. |
|
2052 | 2071 | # |
|
2053 | 2072 | # It also allows users to assign to either alias or magic names true |
|
2054 | 2073 | # python variables (the magic/alias systems always take second seat to |
|
2055 | 2074 | # true python code). |
|
2056 | 2075 | if theRest and theRest[0] in '!=()': |
|
2057 | 2076 | return self.handle_normal(line,continue_prompt) |
|
2058 | 2077 | |
|
2059 | 2078 | if oinfo is None: |
|
2060 | 2079 | # let's try to ensure that _oinfo is ONLY called when autocall is |
|
2061 | 2080 | # on. Since it has inevitable potential side effects, at least |
|
2062 | 2081 | # having autocall off should be a guarantee to the user that no |
|
2063 | 2082 | # weird things will happen. |
|
2064 | 2083 | |
|
2065 | 2084 | if self.rc.autocall: |
|
2066 | 2085 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
2067 | 2086 | else: |
|
2068 | 2087 | # in this case, all that's left is either an alias or |
|
2069 | 2088 | # processing the line normally. |
|
2070 | 2089 | if iFun in self.alias_table: |
|
2071 | 2090 | # if autocall is off, by not running _ofind we won't know |
|
2072 | 2091 | # whether the given name may also exist in one of the |
|
2073 | 2092 | # user's namespace. At this point, it's best to do a |
|
2074 | 2093 | # quick check just to be sure that we don't let aliases |
|
2075 | 2094 | # shadow variables. |
|
2076 | 2095 | head = iFun.split('.',1)[0] |
|
2077 | 2096 | if head in self.user_ns or head in self.internal_ns \ |
|
2078 | 2097 | or head in __builtin__.__dict__: |
|
2079 | 2098 | return self.handle_normal(line,continue_prompt) |
|
2080 | 2099 | else: |
|
2081 | 2100 | return self.handle_alias(line,continue_prompt, |
|
2082 | 2101 | pre,iFun,theRest) |
|
2083 | 2102 | |
|
2084 | 2103 | else: |
|
2085 | 2104 | return self.handle_normal(line,continue_prompt) |
|
2086 | 2105 | |
|
2087 | 2106 | if not oinfo['found']: |
|
2088 | 2107 | return self.handle_normal(line,continue_prompt) |
|
2089 | 2108 | else: |
|
2090 | 2109 | #print 'pre<%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2091 | 2110 | if oinfo['isalias']: |
|
2092 | 2111 | return self.handle_alias(line,continue_prompt, |
|
2093 | 2112 | pre,iFun,theRest) |
|
2094 | 2113 | |
|
2095 | 2114 | if (self.rc.autocall |
|
2096 | 2115 | and |
|
2097 | 2116 | ( |
|
2098 | 2117 | #only consider exclusion re if not "," or ";" autoquoting |
|
2099 | 2118 | (pre == self.ESC_QUOTE or pre == self.ESC_QUOTE2 |
|
2100 | 2119 | or pre == self.ESC_PAREN) or |
|
2101 | 2120 | (not self.re_exclude_auto.match(theRest))) |
|
2102 | 2121 | and |
|
2103 | 2122 | self.re_fun_name.match(iFun) and |
|
2104 | 2123 | callable(oinfo['obj'])) : |
|
2105 | 2124 | #print 'going auto' # dbg |
|
2106 | 2125 | return self.handle_auto(line,continue_prompt, |
|
2107 | 2126 | pre,iFun,theRest,oinfo['obj']) |
|
2108 | 2127 | else: |
|
2109 | 2128 | #print 'was callable?', callable(oinfo['obj']) # dbg |
|
2110 | 2129 | return self.handle_normal(line,continue_prompt) |
|
2111 | 2130 | |
|
2112 | 2131 | # If we get here, we have a normal Python line. Log and return. |
|
2113 | 2132 | return self.handle_normal(line,continue_prompt) |
|
2114 | 2133 | |
|
2115 | 2134 | def _prefilter_dumb(self, line, continue_prompt): |
|
2116 | 2135 | """simple prefilter function, for debugging""" |
|
2117 | 2136 | return self.handle_normal(line,continue_prompt) |
|
2118 | 2137 | |
|
2119 | 2138 | |
|
2120 | 2139 | def multiline_prefilter(self, line, continue_prompt): |
|
2121 | 2140 | """ Run _prefilter for each line of input |
|
2122 | 2141 | |
|
2123 | 2142 | Covers cases where there are multiple lines in the user entry, |
|
2124 | 2143 | which is the case when the user goes back to a multiline history |
|
2125 | 2144 | entry and presses enter. |
|
2126 | 2145 | |
|
2127 | 2146 | """ |
|
2128 | 2147 | out = [] |
|
2129 | 2148 | for l in line.rstrip('\n').split('\n'): |
|
2130 | 2149 | out.append(self._prefilter(l, continue_prompt)) |
|
2131 | 2150 | return '\n'.join(out) |
|
2132 | 2151 | |
|
2133 | 2152 | # Set the default prefilter() function (this can be user-overridden) |
|
2134 | 2153 | prefilter = multiline_prefilter |
|
2135 | 2154 | |
|
2136 | 2155 | def handle_normal(self,line,continue_prompt=None, |
|
2137 | 2156 | pre=None,iFun=None,theRest=None): |
|
2138 | 2157 | """Handle normal input lines. Use as a template for handlers.""" |
|
2139 | 2158 | |
|
2140 | 2159 | # With autoindent on, we need some way to exit the input loop, and I |
|
2141 | 2160 | # don't want to force the user to have to backspace all the way to |
|
2142 | 2161 | # clear the line. The rule will be in this case, that either two |
|
2143 | 2162 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
2144 | 2163 | # of a size different to the indent level, will exit the input loop. |
|
2145 | 2164 | |
|
2146 | 2165 | if (continue_prompt and self.autoindent and line.isspace() and |
|
2147 | 2166 | (0 < abs(len(line) - self.indent_current_nsp) <= 2 or |
|
2148 | 2167 | (self.buffer[-1]).isspace() )): |
|
2149 | 2168 | line = '' |
|
2150 | 2169 | |
|
2151 | 2170 | self.log(line,line,continue_prompt) |
|
2152 | 2171 | return line |
|
2153 | 2172 | |
|
2154 | 2173 | def handle_alias(self,line,continue_prompt=None, |
|
2155 | 2174 | pre=None,iFun=None,theRest=None): |
|
2156 | 2175 | """Handle alias input lines. """ |
|
2157 | 2176 | |
|
2158 | 2177 | # pre is needed, because it carries the leading whitespace. Otherwise |
|
2159 | 2178 | # aliases won't work in indented sections. |
|
2160 | 2179 | transformed = self.expand_aliases(iFun, theRest) |
|
2161 | 2180 | line_out = '%s_ip.system(%s)' % (pre, make_quoted_expr( transformed )) |
|
2162 | 2181 | self.log(line,line_out,continue_prompt) |
|
2163 | 2182 | #print 'line out:',line_out # dbg |
|
2164 | 2183 | return line_out |
|
2165 | 2184 | |
|
2166 | 2185 | def handle_shell_escape(self, line, continue_prompt=None, |
|
2167 | 2186 | pre=None,iFun=None,theRest=None): |
|
2168 | 2187 | """Execute the line in a shell, empty return value""" |
|
2169 | 2188 | |
|
2170 | 2189 | #print 'line in :', `line` # dbg |
|
2171 | 2190 | # Example of a special handler. Others follow a similar pattern. |
|
2172 | 2191 | if line.lstrip().startswith('!!'): |
|
2173 | 2192 | # rewrite iFun/theRest to properly hold the call to %sx and |
|
2174 | 2193 | # the actual command to be executed, so handle_magic can work |
|
2175 | 2194 | # correctly |
|
2176 | 2195 | theRest = '%s %s' % (iFun[2:],theRest) |
|
2177 | 2196 | iFun = 'sx' |
|
2178 | 2197 | return self.handle_magic('%ssx %s' % (self.ESC_MAGIC, |
|
2179 | 2198 | line.lstrip()[2:]), |
|
2180 | 2199 | continue_prompt,pre,iFun,theRest) |
|
2181 | 2200 | else: |
|
2182 | 2201 | cmd=line.lstrip().lstrip('!') |
|
2183 | 2202 | line_out = '%s_ip.system(%s)' % (pre,make_quoted_expr(cmd)) |
|
2184 | 2203 | # update cache/log and return |
|
2185 | 2204 | self.log(line,line_out,continue_prompt) |
|
2186 | 2205 | return line_out |
|
2187 | 2206 | |
|
2188 | 2207 | def handle_magic(self, line, continue_prompt=None, |
|
2189 | 2208 | pre=None,iFun=None,theRest=None): |
|
2190 | 2209 | """Execute magic functions.""" |
|
2191 | 2210 | |
|
2192 | 2211 | |
|
2193 | 2212 | cmd = '%s_ip.magic(%s)' % (pre,make_quoted_expr(iFun + " " + theRest)) |
|
2194 | 2213 | self.log(line,cmd,continue_prompt) |
|
2195 | 2214 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg |
|
2196 | 2215 | return cmd |
|
2197 | 2216 | |
|
2198 | 2217 | def handle_auto(self, line, continue_prompt=None, |
|
2199 | 2218 | pre=None,iFun=None,theRest=None,obj=None): |
|
2200 | 2219 | """Hande lines which can be auto-executed, quoting if requested.""" |
|
2201 | 2220 | |
|
2202 | 2221 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2203 | 2222 | |
|
2204 | 2223 | # This should only be active for single-line input! |
|
2205 | 2224 | if continue_prompt: |
|
2206 | 2225 | self.log(line,line,continue_prompt) |
|
2207 | 2226 | return line |
|
2208 | 2227 | |
|
2209 | 2228 | auto_rewrite = True |
|
2210 | 2229 | |
|
2211 | 2230 | if pre == self.ESC_QUOTE: |
|
2212 | 2231 | # Auto-quote splitting on whitespace |
|
2213 | 2232 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) |
|
2214 | 2233 | elif pre == self.ESC_QUOTE2: |
|
2215 | 2234 | # Auto-quote whole string |
|
2216 | 2235 | newcmd = '%s("%s")' % (iFun,theRest) |
|
2217 | 2236 | elif pre == self.ESC_PAREN: |
|
2218 | 2237 | newcmd = '%s(%s)' % (iFun,",".join(theRest.split())) |
|
2219 | 2238 | else: |
|
2220 | 2239 | # Auto-paren. |
|
2221 | 2240 | # We only apply it to argument-less calls if the autocall |
|
2222 | 2241 | # parameter is set to 2. We only need to check that autocall is < |
|
2223 | 2242 | # 2, since this function isn't called unless it's at least 1. |
|
2224 | 2243 | if not theRest and (self.rc.autocall < 2): |
|
2225 | 2244 | newcmd = '%s %s' % (iFun,theRest) |
|
2226 | 2245 | auto_rewrite = False |
|
2227 | 2246 | else: |
|
2228 | 2247 | if theRest.startswith('['): |
|
2229 | 2248 | if hasattr(obj,'__getitem__'): |
|
2230 | 2249 | # Don't autocall in this case: item access for an object |
|
2231 | 2250 | # which is BOTH callable and implements __getitem__. |
|
2232 | 2251 | newcmd = '%s %s' % (iFun,theRest) |
|
2233 | 2252 | auto_rewrite = False |
|
2234 | 2253 | else: |
|
2235 | 2254 | # if the object doesn't support [] access, go ahead and |
|
2236 | 2255 | # autocall |
|
2237 | 2256 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
2238 | 2257 | elif theRest.endswith(';'): |
|
2239 | 2258 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) |
|
2240 | 2259 | else: |
|
2241 | 2260 | newcmd = '%s(%s)' % (iFun.rstrip(), theRest) |
|
2242 | 2261 | |
|
2243 | 2262 | if auto_rewrite: |
|
2244 | 2263 | print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd |
|
2245 | 2264 | # log what is now valid Python, not the actual user input (without the |
|
2246 | 2265 | # final newline) |
|
2247 | 2266 | self.log(line,newcmd,continue_prompt) |
|
2248 | 2267 | return newcmd |
|
2249 | 2268 | |
|
2250 | 2269 | def handle_help(self, line, continue_prompt=None, |
|
2251 | 2270 | pre=None,iFun=None,theRest=None): |
|
2252 | 2271 | """Try to get some help for the object. |
|
2253 | 2272 | |
|
2254 | 2273 | obj? or ?obj -> basic information. |
|
2255 | 2274 | obj?? or ??obj -> more details. |
|
2256 | 2275 | """ |
|
2257 | 2276 | |
|
2258 | 2277 | # We need to make sure that we don't process lines which would be |
|
2259 | 2278 | # otherwise valid python, such as "x=1 # what?" |
|
2260 | 2279 | try: |
|
2261 | 2280 | codeop.compile_command(line) |
|
2262 | 2281 | except SyntaxError: |
|
2263 | 2282 | # We should only handle as help stuff which is NOT valid syntax |
|
2264 | 2283 | if line[0]==self.ESC_HELP: |
|
2265 | 2284 | line = line[1:] |
|
2266 | 2285 | elif line[-1]==self.ESC_HELP: |
|
2267 | 2286 | line = line[:-1] |
|
2268 | 2287 | self.log(line,'#?'+line,continue_prompt) |
|
2269 | 2288 | if line: |
|
2270 | 2289 | self.magic_pinfo(line) |
|
2271 | 2290 | else: |
|
2272 | 2291 | page(self.usage,screen_lines=self.rc.screen_length) |
|
2273 | 2292 | return '' # Empty string is needed here! |
|
2274 | 2293 | except: |
|
2275 | 2294 | # Pass any other exceptions through to the normal handler |
|
2276 | 2295 | return self.handle_normal(line,continue_prompt) |
|
2277 | 2296 | else: |
|
2278 | 2297 | # If the code compiles ok, we should handle it normally |
|
2279 | 2298 | return self.handle_normal(line,continue_prompt) |
|
2280 | 2299 | |
|
2281 | 2300 | def getapi(self): |
|
2282 | 2301 | """ Get an IPApi object for this shell instance |
|
2283 | 2302 | |
|
2284 | 2303 | Getting an IPApi object is always preferable to accessing the shell |
|
2285 | 2304 | directly, but this holds true especially for extensions. |
|
2286 | 2305 | |
|
2287 | 2306 | It should always be possible to implement an extension with IPApi |
|
2288 | 2307 | alone. If not, contact maintainer to request an addition. |
|
2289 | 2308 | |
|
2290 | 2309 | """ |
|
2291 | 2310 | return self.api |
|
2292 | 2311 | |
|
2293 | 2312 | def handle_emacs(self,line,continue_prompt=None, |
|
2294 | 2313 | pre=None,iFun=None,theRest=None): |
|
2295 | 2314 | """Handle input lines marked by python-mode.""" |
|
2296 | 2315 | |
|
2297 | 2316 | # Currently, nothing is done. Later more functionality can be added |
|
2298 | 2317 | # here if needed. |
|
2299 | 2318 | |
|
2300 | 2319 | # The input cache shouldn't be updated |
|
2301 | 2320 | |
|
2302 | 2321 | return line |
|
2303 | 2322 | |
|
2304 | 2323 | def mktempfile(self,data=None): |
|
2305 | 2324 | """Make a new tempfile and return its filename. |
|
2306 | 2325 | |
|
2307 | 2326 | This makes a call to tempfile.mktemp, but it registers the created |
|
2308 | 2327 | filename internally so ipython cleans it up at exit time. |
|
2309 | 2328 | |
|
2310 | 2329 | Optional inputs: |
|
2311 | 2330 | |
|
2312 | 2331 | - data(None): if data is given, it gets written out to the temp file |
|
2313 | 2332 | immediately, and the file is closed again.""" |
|
2314 | 2333 | |
|
2315 | 2334 | filename = tempfile.mktemp('.py','ipython_edit_') |
|
2316 | 2335 | self.tempfiles.append(filename) |
|
2317 | 2336 | |
|
2318 | 2337 | if data: |
|
2319 | 2338 | tmp_file = open(filename,'w') |
|
2320 | 2339 | tmp_file.write(data) |
|
2321 | 2340 | tmp_file.close() |
|
2322 | 2341 | return filename |
|
2323 | 2342 | |
|
2324 | 2343 | def write(self,data): |
|
2325 | 2344 | """Write a string to the default output""" |
|
2326 | 2345 | Term.cout.write(data) |
|
2327 | 2346 | |
|
2328 | 2347 | def write_err(self,data): |
|
2329 | 2348 | """Write a string to the default error output""" |
|
2330 | 2349 | Term.cerr.write(data) |
|
2331 | 2350 | |
|
2332 | 2351 | def exit(self): |
|
2333 | 2352 | """Handle interactive exit. |
|
2334 | 2353 | |
|
2335 | 2354 | This method sets the exit_now attribute.""" |
|
2336 | 2355 | |
|
2337 | 2356 | if self.rc.confirm_exit: |
|
2338 | 2357 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
2339 | 2358 | self.exit_now = True |
|
2340 | 2359 | else: |
|
2341 | 2360 | self.exit_now = True |
|
2342 | 2361 | |
|
2343 | 2362 | def safe_execfile(self,fname,*where,**kw): |
|
2344 | 2363 | fname = os.path.expanduser(fname) |
|
2345 | 2364 | |
|
2346 | 2365 | # find things also in current directory |
|
2347 | 2366 | dname = os.path.dirname(fname) |
|
2348 | 2367 | if not sys.path.count(dname): |
|
2349 | 2368 | sys.path.append(dname) |
|
2350 | 2369 | |
|
2351 | 2370 | try: |
|
2352 | 2371 | xfile = open(fname) |
|
2353 | 2372 | except: |
|
2354 | 2373 | print >> Term.cerr, \ |
|
2355 | 2374 | 'Could not open file <%s> for safe execution.' % fname |
|
2356 | 2375 | return None |
|
2357 | 2376 | |
|
2358 | 2377 | kw.setdefault('islog',0) |
|
2359 | 2378 | kw.setdefault('quiet',1) |
|
2360 | 2379 | kw.setdefault('exit_ignore',0) |
|
2361 | 2380 | first = xfile.readline() |
|
2362 | 2381 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() |
|
2363 | 2382 | xfile.close() |
|
2364 | 2383 | # line by line execution |
|
2365 | 2384 | if first.startswith(loghead) or kw['islog']: |
|
2366 | 2385 | print 'Loading log file <%s> one line at a time...' % fname |
|
2367 | 2386 | if kw['quiet']: |
|
2368 | 2387 | stdout_save = sys.stdout |
|
2369 | 2388 | sys.stdout = StringIO.StringIO() |
|
2370 | 2389 | try: |
|
2371 | 2390 | globs,locs = where[0:2] |
|
2372 | 2391 | except: |
|
2373 | 2392 | try: |
|
2374 | 2393 | globs = locs = where[0] |
|
2375 | 2394 | except: |
|
2376 | 2395 | globs = locs = globals() |
|
2377 | 2396 | badblocks = [] |
|
2378 | 2397 | |
|
2379 | 2398 | # we also need to identify indented blocks of code when replaying |
|
2380 | 2399 | # logs and put them together before passing them to an exec |
|
2381 | 2400 | # statement. This takes a bit of regexp and look-ahead work in the |
|
2382 | 2401 | # file. It's easiest if we swallow the whole thing in memory |
|
2383 | 2402 | # first, and manually walk through the lines list moving the |
|
2384 | 2403 | # counter ourselves. |
|
2385 | 2404 | indent_re = re.compile('\s+\S') |
|
2386 | 2405 | xfile = open(fname) |
|
2387 | 2406 | filelines = xfile.readlines() |
|
2388 | 2407 | xfile.close() |
|
2389 | 2408 | nlines = len(filelines) |
|
2390 | 2409 | lnum = 0 |
|
2391 | 2410 | while lnum < nlines: |
|
2392 | 2411 | line = filelines[lnum] |
|
2393 | 2412 | lnum += 1 |
|
2394 | 2413 | # don't re-insert logger status info into cache |
|
2395 | 2414 | if line.startswith('#log#'): |
|
2396 | 2415 | continue |
|
2397 | 2416 | else: |
|
2398 | 2417 | # build a block of code (maybe a single line) for execution |
|
2399 | 2418 | block = line |
|
2400 | 2419 | try: |
|
2401 | 2420 | next = filelines[lnum] # lnum has already incremented |
|
2402 | 2421 | except: |
|
2403 | 2422 | next = None |
|
2404 | 2423 | while next and indent_re.match(next): |
|
2405 | 2424 | block += next |
|
2406 | 2425 | lnum += 1 |
|
2407 | 2426 | try: |
|
2408 | 2427 | next = filelines[lnum] |
|
2409 | 2428 | except: |
|
2410 | 2429 | next = None |
|
2411 | 2430 | # now execute the block of one or more lines |
|
2412 | 2431 | try: |
|
2413 | 2432 | exec block in globs,locs |
|
2414 | 2433 | except SystemExit: |
|
2415 | 2434 | pass |
|
2416 | 2435 | except: |
|
2417 | 2436 | badblocks.append(block.rstrip()) |
|
2418 | 2437 | if kw['quiet']: # restore stdout |
|
2419 | 2438 | sys.stdout.close() |
|
2420 | 2439 | sys.stdout = stdout_save |
|
2421 | 2440 | print 'Finished replaying log file <%s>' % fname |
|
2422 | 2441 | if badblocks: |
|
2423 | 2442 | print >> sys.stderr, ('\nThe following lines/blocks in file ' |
|
2424 | 2443 | '<%s> reported errors:' % fname) |
|
2425 | 2444 | |
|
2426 | 2445 | for badline in badblocks: |
|
2427 | 2446 | print >> sys.stderr, badline |
|
2428 | 2447 | else: # regular file execution |
|
2429 | 2448 | try: |
|
2430 | 2449 | execfile(fname,*where) |
|
2431 | 2450 | except SyntaxError: |
|
2432 | 2451 | self.showsyntaxerror() |
|
2433 | 2452 | warn('Failure executing file: <%s>' % fname) |
|
2434 | 2453 | except SystemExit,status: |
|
2435 | 2454 | if not kw['exit_ignore']: |
|
2436 | 2455 | self.showtraceback() |
|
2437 | 2456 | warn('Failure executing file: <%s>' % fname) |
|
2438 | 2457 | except: |
|
2439 | 2458 | self.showtraceback() |
|
2440 | 2459 | warn('Failure executing file: <%s>' % fname) |
|
2441 | 2460 | |
|
2442 | 2461 | #************************* end of file <iplib.py> ***************************** |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now