Show More
@@ -1,619 +1,620 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Display formatters. |
|
2 | """Display formatters. | |
3 |
|
3 | |||
4 |
|
4 | |||
5 | Authors: |
|
5 | Authors: | |
6 |
|
6 | |||
7 | * Robert Kern |
|
7 | * Robert Kern | |
8 | * Brian Granger |
|
8 | * Brian Granger | |
9 | """ |
|
9 | """ | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Copyright (c) 2010, IPython Development Team. |
|
11 | # Copyright (c) 2010, IPython Development Team. | |
12 | # |
|
12 | # | |
13 | # Distributed under the terms of the Modified BSD License. |
|
13 | # Distributed under the terms of the Modified BSD License. | |
14 | # |
|
14 | # | |
15 | # The full license is in the file COPYING.txt, distributed with this software. |
|
15 | # The full license is in the file COPYING.txt, distributed with this software. | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Imports |
|
19 | # Imports | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | # Stdlib imports |
|
22 | # Stdlib imports | |
23 | import abc |
|
23 | import abc | |
24 | import sys |
|
24 | import sys | |
25 | # We must use StringIO, as cStringIO doesn't handle unicode properly. |
|
25 | # We must use StringIO, as cStringIO doesn't handle unicode properly. | |
26 | from StringIO import StringIO |
|
26 | from StringIO import StringIO | |
27 |
|
27 | |||
28 | # Our own imports |
|
28 | # Our own imports | |
29 | from IPython.config.configurable import Configurable |
|
29 | from IPython.config.configurable import Configurable | |
30 | from IPython.lib import pretty |
|
30 | from IPython.lib import pretty | |
31 | from IPython.utils.traitlets import Bool, Dict, Int, Unicode, CUnicode, ObjectName |
|
31 | from IPython.utils.traitlets import Bool, Dict, Int, Unicode, CUnicode, ObjectName | |
|
32 | from IPython.utils.py3compat import unicode_to_str | |||
32 |
|
33 | |||
33 |
|
34 | |||
34 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
35 | # The main DisplayFormatter class |
|
36 | # The main DisplayFormatter class | |
36 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
37 |
|
38 | |||
38 |
|
39 | |||
39 | class DisplayFormatter(Configurable): |
|
40 | class DisplayFormatter(Configurable): | |
40 |
|
41 | |||
41 | # When set to true only the default plain text formatter will be used. |
|
42 | # When set to true only the default plain text formatter will be used. | |
42 | plain_text_only = Bool(False, config=True) |
|
43 | plain_text_only = Bool(False, config=True) | |
43 |
|
44 | |||
44 | # A dict of formatter whose keys are format types (MIME types) and whose |
|
45 | # A dict of formatter whose keys are format types (MIME types) and whose | |
45 | # values are subclasses of BaseFormatter. |
|
46 | # values are subclasses of BaseFormatter. | |
46 | formatters = Dict(config=True) |
|
47 | formatters = Dict(config=True) | |
47 | def _formatters_default(self): |
|
48 | def _formatters_default(self): | |
48 | """Activate the default formatters.""" |
|
49 | """Activate the default formatters.""" | |
49 | formatter_classes = [ |
|
50 | formatter_classes = [ | |
50 | PlainTextFormatter, |
|
51 | PlainTextFormatter, | |
51 | HTMLFormatter, |
|
52 | HTMLFormatter, | |
52 | SVGFormatter, |
|
53 | SVGFormatter, | |
53 | PNGFormatter, |
|
54 | PNGFormatter, | |
54 | JPEGFormatter, |
|
55 | JPEGFormatter, | |
55 | LatexFormatter, |
|
56 | LatexFormatter, | |
56 | JSONFormatter, |
|
57 | JSONFormatter, | |
57 | JavascriptFormatter |
|
58 | JavascriptFormatter | |
58 | ] |
|
59 | ] | |
59 | d = {} |
|
60 | d = {} | |
60 | for cls in formatter_classes: |
|
61 | for cls in formatter_classes: | |
61 | f = cls(config=self.config) |
|
62 | f = cls(config=self.config) | |
62 | d[f.format_type] = f |
|
63 | d[f.format_type] = f | |
63 | return d |
|
64 | return d | |
64 |
|
65 | |||
65 | def format(self, obj, include=None, exclude=None): |
|
66 | def format(self, obj, include=None, exclude=None): | |
66 | """Return a format data dict for an object. |
|
67 | """Return a format data dict for an object. | |
67 |
|
68 | |||
68 | By default all format types will be computed. |
|
69 | By default all format types will be computed. | |
69 |
|
70 | |||
70 | The following MIME types are currently implemented: |
|
71 | The following MIME types are currently implemented: | |
71 |
|
72 | |||
72 | * text/plain |
|
73 | * text/plain | |
73 | * text/html |
|
74 | * text/html | |
74 | * text/latex |
|
75 | * text/latex | |
75 | * application/json |
|
76 | * application/json | |
76 | * application/javascript |
|
77 | * application/javascript | |
77 | * image/png |
|
78 | * image/png | |
78 | * image/jpeg |
|
79 | * image/jpeg | |
79 | * image/svg+xml |
|
80 | * image/svg+xml | |
80 |
|
81 | |||
81 | Parameters |
|
82 | Parameters | |
82 | ---------- |
|
83 | ---------- | |
83 | obj : object |
|
84 | obj : object | |
84 | The Python object whose format data will be computed. |
|
85 | The Python object whose format data will be computed. | |
85 | include : list or tuple, optional |
|
86 | include : list or tuple, optional | |
86 | A list of format type strings (MIME types) to include in the |
|
87 | A list of format type strings (MIME types) to include in the | |
87 | format data dict. If this is set *only* the format types included |
|
88 | format data dict. If this is set *only* the format types included | |
88 | in this list will be computed. |
|
89 | in this list will be computed. | |
89 | exclude : list or tuple, optional |
|
90 | exclude : list or tuple, optional | |
90 | A list of format type string (MIME types) to exclue in the format |
|
91 | A list of format type string (MIME types) to exclue in the format | |
91 | data dict. If this is set all format types will be computed, |
|
92 | data dict. If this is set all format types will be computed, | |
92 | except for those included in this argument. |
|
93 | except for those included in this argument. | |
93 |
|
94 | |||
94 | Returns |
|
95 | Returns | |
95 | ------- |
|
96 | ------- | |
96 | format_dict : dict |
|
97 | format_dict : dict | |
97 | A dictionary of key/value pairs, one or each format that was |
|
98 | A dictionary of key/value pairs, one or each format that was | |
98 | generated for the object. The keys are the format types, which |
|
99 | generated for the object. The keys are the format types, which | |
99 | will usually be MIME type strings and the values and JSON'able |
|
100 | will usually be MIME type strings and the values and JSON'able | |
100 | data structure containing the raw data for the representation in |
|
101 | data structure containing the raw data for the representation in | |
101 | that format. |
|
102 | that format. | |
102 | """ |
|
103 | """ | |
103 | format_dict = {} |
|
104 | format_dict = {} | |
104 |
|
105 | |||
105 | # If plain text only is active |
|
106 | # If plain text only is active | |
106 | if self.plain_text_only: |
|
107 | if self.plain_text_only: | |
107 | formatter = self.formatters['text/plain'] |
|
108 | formatter = self.formatters['text/plain'] | |
108 | try: |
|
109 | try: | |
109 | data = formatter(obj) |
|
110 | data = formatter(obj) | |
110 | except: |
|
111 | except: | |
111 | # FIXME: log the exception |
|
112 | # FIXME: log the exception | |
112 | raise |
|
113 | raise | |
113 | if data is not None: |
|
114 | if data is not None: | |
114 | format_dict['text/plain'] = data |
|
115 | format_dict['text/plain'] = data | |
115 | return format_dict |
|
116 | return format_dict | |
116 |
|
117 | |||
117 | for format_type, formatter in self.formatters.items(): |
|
118 | for format_type, formatter in self.formatters.items(): | |
118 | if include is not None: |
|
119 | if include is not None: | |
119 | if format_type not in include: |
|
120 | if format_type not in include: | |
120 | continue |
|
121 | continue | |
121 | if exclude is not None: |
|
122 | if exclude is not None: | |
122 | if format_type in exclude: |
|
123 | if format_type in exclude: | |
123 | continue |
|
124 | continue | |
124 | try: |
|
125 | try: | |
125 | data = formatter(obj) |
|
126 | data = formatter(obj) | |
126 | except: |
|
127 | except: | |
127 | # FIXME: log the exception |
|
128 | # FIXME: log the exception | |
128 | raise |
|
129 | raise | |
129 | if data is not None: |
|
130 | if data is not None: | |
130 | format_dict[format_type] = data |
|
131 | format_dict[format_type] = data | |
131 | return format_dict |
|
132 | return format_dict | |
132 |
|
133 | |||
133 | @property |
|
134 | @property | |
134 | def format_types(self): |
|
135 | def format_types(self): | |
135 | """Return the format types (MIME types) of the active formatters.""" |
|
136 | """Return the format types (MIME types) of the active formatters.""" | |
136 | return self.formatters.keys() |
|
137 | return self.formatters.keys() | |
137 |
|
138 | |||
138 |
|
139 | |||
139 | #----------------------------------------------------------------------------- |
|
140 | #----------------------------------------------------------------------------- | |
140 | # Formatters for specific format types (text, html, svg, etc.) |
|
141 | # Formatters for specific format types (text, html, svg, etc.) | |
141 | #----------------------------------------------------------------------------- |
|
142 | #----------------------------------------------------------------------------- | |
142 |
|
143 | |||
143 |
|
144 | |||
144 | class FormatterABC(object): |
|
145 | class FormatterABC(object): | |
145 | """ Abstract base class for Formatters. |
|
146 | """ Abstract base class for Formatters. | |
146 |
|
147 | |||
147 | A formatter is a callable class that is responsible for computing the |
|
148 | A formatter is a callable class that is responsible for computing the | |
148 | raw format data for a particular format type (MIME type). For example, |
|
149 | raw format data for a particular format type (MIME type). For example, | |
149 | an HTML formatter would have a format type of `text/html` and would return |
|
150 | an HTML formatter would have a format type of `text/html` and would return | |
150 | the HTML representation of the object when called. |
|
151 | the HTML representation of the object when called. | |
151 | """ |
|
152 | """ | |
152 | __metaclass__ = abc.ABCMeta |
|
153 | __metaclass__ = abc.ABCMeta | |
153 |
|
154 | |||
154 | # The format type of the data returned, usually a MIME type. |
|
155 | # The format type of the data returned, usually a MIME type. | |
155 | format_type = 'text/plain' |
|
156 | format_type = 'text/plain' | |
156 |
|
157 | |||
157 | # Is the formatter enabled... |
|
158 | # Is the formatter enabled... | |
158 | enabled = True |
|
159 | enabled = True | |
159 |
|
160 | |||
160 | @abc.abstractmethod |
|
161 | @abc.abstractmethod | |
161 | def __call__(self, obj): |
|
162 | def __call__(self, obj): | |
162 | """Return a JSON'able representation of the object. |
|
163 | """Return a JSON'able representation of the object. | |
163 |
|
164 | |||
164 | If the object cannot be formatted by this formatter, then return None |
|
165 | If the object cannot be formatted by this formatter, then return None | |
165 | """ |
|
166 | """ | |
166 | try: |
|
167 | try: | |
167 | return repr(obj) |
|
168 | return repr(obj) | |
168 | except TypeError: |
|
169 | except TypeError: | |
169 | return None |
|
170 | return None | |
170 |
|
171 | |||
171 |
|
172 | |||
172 | class BaseFormatter(Configurable): |
|
173 | class BaseFormatter(Configurable): | |
173 | """A base formatter class that is configurable. |
|
174 | """A base formatter class that is configurable. | |
174 |
|
175 | |||
175 | This formatter should usually be used as the base class of all formatters. |
|
176 | This formatter should usually be used as the base class of all formatters. | |
176 | It is a traited :class:`Configurable` class and includes an extensible |
|
177 | It is a traited :class:`Configurable` class and includes an extensible | |
177 | API for users to determine how their objects are formatted. The following |
|
178 | API for users to determine how their objects are formatted. The following | |
178 | logic is used to find a function to format an given object. |
|
179 | logic is used to find a function to format an given object. | |
179 |
|
180 | |||
180 | 1. The object is introspected to see if it has a method with the name |
|
181 | 1. The object is introspected to see if it has a method with the name | |
181 | :attr:`print_method`. If is does, that object is passed to that method |
|
182 | :attr:`print_method`. If is does, that object is passed to that method | |
182 | for formatting. |
|
183 | for formatting. | |
183 | 2. If no print method is found, three internal dictionaries are consulted |
|
184 | 2. If no print method is found, three internal dictionaries are consulted | |
184 | to find print method: :attr:`singleton_printers`, :attr:`type_printers` |
|
185 | to find print method: :attr:`singleton_printers`, :attr:`type_printers` | |
185 | and :attr:`deferred_printers`. |
|
186 | and :attr:`deferred_printers`. | |
186 |
|
187 | |||
187 | Users should use these dictionaries to register functions that will be |
|
188 | Users should use these dictionaries to register functions that will be | |
188 | used to compute the format data for their objects (if those objects don't |
|
189 | used to compute the format data for their objects (if those objects don't | |
189 | have the special print methods). The easiest way of using these |
|
190 | have the special print methods). The easiest way of using these | |
190 | dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name` |
|
191 | dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name` | |
191 | methods. |
|
192 | methods. | |
192 |
|
193 | |||
193 | If no function/callable is found to compute the format data, ``None`` is |
|
194 | If no function/callable is found to compute the format data, ``None`` is | |
194 | returned and this format type is not used. |
|
195 | returned and this format type is not used. | |
195 | """ |
|
196 | """ | |
196 |
|
197 | |||
197 | format_type = Unicode('text/plain') |
|
198 | format_type = Unicode('text/plain') | |
198 |
|
199 | |||
199 | enabled = Bool(True, config=True) |
|
200 | enabled = Bool(True, config=True) | |
200 |
|
201 | |||
201 | print_method = ObjectName('__repr__') |
|
202 | print_method = ObjectName('__repr__') | |
202 |
|
203 | |||
203 | # The singleton printers. |
|
204 | # The singleton printers. | |
204 | # Maps the IDs of the builtin singleton objects to the format functions. |
|
205 | # Maps the IDs of the builtin singleton objects to the format functions. | |
205 | singleton_printers = Dict(config=True) |
|
206 | singleton_printers = Dict(config=True) | |
206 | def _singleton_printers_default(self): |
|
207 | def _singleton_printers_default(self): | |
207 | return {} |
|
208 | return {} | |
208 |
|
209 | |||
209 | # The type-specific printers. |
|
210 | # The type-specific printers. | |
210 | # Map type objects to the format functions. |
|
211 | # Map type objects to the format functions. | |
211 | type_printers = Dict(config=True) |
|
212 | type_printers = Dict(config=True) | |
212 | def _type_printers_default(self): |
|
213 | def _type_printers_default(self): | |
213 | return {} |
|
214 | return {} | |
214 |
|
215 | |||
215 | # The deferred-import type-specific printers. |
|
216 | # The deferred-import type-specific printers. | |
216 | # Map (modulename, classname) pairs to the format functions. |
|
217 | # Map (modulename, classname) pairs to the format functions. | |
217 | deferred_printers = Dict(config=True) |
|
218 | deferred_printers = Dict(config=True) | |
218 | def _deferred_printers_default(self): |
|
219 | def _deferred_printers_default(self): | |
219 | return {} |
|
220 | return {} | |
220 |
|
221 | |||
221 | def __call__(self, obj): |
|
222 | def __call__(self, obj): | |
222 | """Compute the format for an object.""" |
|
223 | """Compute the format for an object.""" | |
223 | if self.enabled: |
|
224 | if self.enabled: | |
224 | obj_id = id(obj) |
|
225 | obj_id = id(obj) | |
225 | try: |
|
226 | try: | |
226 | obj_class = getattr(obj, '__class__', None) or type(obj) |
|
227 | obj_class = getattr(obj, '__class__', None) or type(obj) | |
227 | # First try to find registered singleton printers for the type. |
|
228 | # First try to find registered singleton printers for the type. | |
228 | try: |
|
229 | try: | |
229 | printer = self.singleton_printers[obj_id] |
|
230 | printer = self.singleton_printers[obj_id] | |
230 | except (TypeError, KeyError): |
|
231 | except (TypeError, KeyError): | |
231 | pass |
|
232 | pass | |
232 | else: |
|
233 | else: | |
233 | return printer(obj) |
|
234 | return printer(obj) | |
234 | # Next look for type_printers. |
|
235 | # Next look for type_printers. | |
235 | for cls in pretty._get_mro(obj_class): |
|
236 | for cls in pretty._get_mro(obj_class): | |
236 | if cls in self.type_printers: |
|
237 | if cls in self.type_printers: | |
237 | return self.type_printers[cls](obj) |
|
238 | return self.type_printers[cls](obj) | |
238 | else: |
|
239 | else: | |
239 | printer = self._in_deferred_types(cls) |
|
240 | printer = self._in_deferred_types(cls) | |
240 | if printer is not None: |
|
241 | if printer is not None: | |
241 | return printer(obj) |
|
242 | return printer(obj) | |
242 | # Finally look for special method names. |
|
243 | # Finally look for special method names. | |
243 | if hasattr(obj_class, self.print_method): |
|
244 | if hasattr(obj_class, self.print_method): | |
244 | printer = getattr(obj_class, self.print_method) |
|
245 | printer = getattr(obj_class, self.print_method) | |
245 | return printer(obj) |
|
246 | return printer(obj) | |
246 | return None |
|
247 | return None | |
247 | except Exception: |
|
248 | except Exception: | |
248 | pass |
|
249 | pass | |
249 | else: |
|
250 | else: | |
250 | return None |
|
251 | return None | |
251 |
|
252 | |||
252 | def for_type(self, typ, func): |
|
253 | def for_type(self, typ, func): | |
253 | """Add a format function for a given type. |
|
254 | """Add a format function for a given type. | |
254 |
|
255 | |||
255 | Parameters |
|
256 | Parameters | |
256 | ----------- |
|
257 | ----------- | |
257 | typ : class |
|
258 | typ : class | |
258 | The class of the object that will be formatted using `func`. |
|
259 | The class of the object that will be formatted using `func`. | |
259 | func : callable |
|
260 | func : callable | |
260 | The callable that will be called to compute the format data. The |
|
261 | The callable that will be called to compute the format data. The | |
261 | call signature of this function is simple, it must take the |
|
262 | call signature of this function is simple, it must take the | |
262 | object to be formatted and return the raw data for the given |
|
263 | object to be formatted and return the raw data for the given | |
263 | format. Subclasses may use a different call signature for the |
|
264 | format. Subclasses may use a different call signature for the | |
264 | `func` argument. |
|
265 | `func` argument. | |
265 | """ |
|
266 | """ | |
266 | oldfunc = self.type_printers.get(typ, None) |
|
267 | oldfunc = self.type_printers.get(typ, None) | |
267 | if func is not None: |
|
268 | if func is not None: | |
268 | # To support easy restoration of old printers, we need to ignore |
|
269 | # To support easy restoration of old printers, we need to ignore | |
269 | # Nones. |
|
270 | # Nones. | |
270 | self.type_printers[typ] = func |
|
271 | self.type_printers[typ] = func | |
271 | return oldfunc |
|
272 | return oldfunc | |
272 |
|
273 | |||
273 | def for_type_by_name(self, type_module, type_name, func): |
|
274 | def for_type_by_name(self, type_module, type_name, func): | |
274 | """Add a format function for a type specified by the full dotted |
|
275 | """Add a format function for a type specified by the full dotted | |
275 | module and name of the type, rather than the type of the object. |
|
276 | module and name of the type, rather than the type of the object. | |
276 |
|
277 | |||
277 | Parameters |
|
278 | Parameters | |
278 | ---------- |
|
279 | ---------- | |
279 | type_module : str |
|
280 | type_module : str | |
280 | The full dotted name of the module the type is defined in, like |
|
281 | The full dotted name of the module the type is defined in, like | |
281 | ``numpy``. |
|
282 | ``numpy``. | |
282 | type_name : str |
|
283 | type_name : str | |
283 | The name of the type (the class name), like ``dtype`` |
|
284 | The name of the type (the class name), like ``dtype`` | |
284 | func : callable |
|
285 | func : callable | |
285 | The callable that will be called to compute the format data. The |
|
286 | The callable that will be called to compute the format data. The | |
286 | call signature of this function is simple, it must take the |
|
287 | call signature of this function is simple, it must take the | |
287 | object to be formatted and return the raw data for the given |
|
288 | object to be formatted and return the raw data for the given | |
288 | format. Subclasses may use a different call signature for the |
|
289 | format. Subclasses may use a different call signature for the | |
289 | `func` argument. |
|
290 | `func` argument. | |
290 | """ |
|
291 | """ | |
291 | key = (type_module, type_name) |
|
292 | key = (type_module, type_name) | |
292 | oldfunc = self.deferred_printers.get(key, None) |
|
293 | oldfunc = self.deferred_printers.get(key, None) | |
293 | if func is not None: |
|
294 | if func is not None: | |
294 | # To support easy restoration of old printers, we need to ignore |
|
295 | # To support easy restoration of old printers, we need to ignore | |
295 | # Nones. |
|
296 | # Nones. | |
296 | self.deferred_printers[key] = func |
|
297 | self.deferred_printers[key] = func | |
297 | return oldfunc |
|
298 | return oldfunc | |
298 |
|
299 | |||
299 | def _in_deferred_types(self, cls): |
|
300 | def _in_deferred_types(self, cls): | |
300 | """ |
|
301 | """ | |
301 | Check if the given class is specified in the deferred type registry. |
|
302 | Check if the given class is specified in the deferred type registry. | |
302 |
|
303 | |||
303 | Returns the printer from the registry if it exists, and None if the |
|
304 | Returns the printer from the registry if it exists, and None if the | |
304 | class is not in the registry. Successful matches will be moved to the |
|
305 | class is not in the registry. Successful matches will be moved to the | |
305 | regular type registry for future use. |
|
306 | regular type registry for future use. | |
306 | """ |
|
307 | """ | |
307 | mod = getattr(cls, '__module__', None) |
|
308 | mod = getattr(cls, '__module__', None) | |
308 | name = getattr(cls, '__name__', None) |
|
309 | name = getattr(cls, '__name__', None) | |
309 | key = (mod, name) |
|
310 | key = (mod, name) | |
310 | printer = None |
|
311 | printer = None | |
311 | if key in self.deferred_printers: |
|
312 | if key in self.deferred_printers: | |
312 | # Move the printer over to the regular registry. |
|
313 | # Move the printer over to the regular registry. | |
313 | printer = self.deferred_printers.pop(key) |
|
314 | printer = self.deferred_printers.pop(key) | |
314 | self.type_printers[cls] = printer |
|
315 | self.type_printers[cls] = printer | |
315 | return printer |
|
316 | return printer | |
316 |
|
317 | |||
317 |
|
318 | |||
318 | class PlainTextFormatter(BaseFormatter): |
|
319 | class PlainTextFormatter(BaseFormatter): | |
319 | """The default pretty-printer. |
|
320 | """The default pretty-printer. | |
320 |
|
321 | |||
321 | This uses :mod:`IPython.external.pretty` to compute the format data of |
|
322 | This uses :mod:`IPython.external.pretty` to compute the format data of | |
322 | the object. If the object cannot be pretty printed, :func:`repr` is used. |
|
323 | the object. If the object cannot be pretty printed, :func:`repr` is used. | |
323 | See the documentation of :mod:`IPython.external.pretty` for details on |
|
324 | See the documentation of :mod:`IPython.external.pretty` for details on | |
324 | how to write pretty printers. Here is a simple example:: |
|
325 | how to write pretty printers. Here is a simple example:: | |
325 |
|
326 | |||
326 | def dtype_pprinter(obj, p, cycle): |
|
327 | def dtype_pprinter(obj, p, cycle): | |
327 | if cycle: |
|
328 | if cycle: | |
328 | return p.text('dtype(...)') |
|
329 | return p.text('dtype(...)') | |
329 | if hasattr(obj, 'fields'): |
|
330 | if hasattr(obj, 'fields'): | |
330 | if obj.fields is None: |
|
331 | if obj.fields is None: | |
331 | p.text(repr(obj)) |
|
332 | p.text(repr(obj)) | |
332 | else: |
|
333 | else: | |
333 | p.begin_group(7, 'dtype([') |
|
334 | p.begin_group(7, 'dtype([') | |
334 | for i, field in enumerate(obj.descr): |
|
335 | for i, field in enumerate(obj.descr): | |
335 | if i > 0: |
|
336 | if i > 0: | |
336 | p.text(',') |
|
337 | p.text(',') | |
337 | p.breakable() |
|
338 | p.breakable() | |
338 | p.pretty(field) |
|
339 | p.pretty(field) | |
339 | p.end_group(7, '])') |
|
340 | p.end_group(7, '])') | |
340 | """ |
|
341 | """ | |
341 |
|
342 | |||
342 | # The format type of data returned. |
|
343 | # The format type of data returned. | |
343 | format_type = Unicode('text/plain') |
|
344 | format_type = Unicode('text/plain') | |
344 |
|
345 | |||
345 | # This subclass ignores this attribute as it always need to return |
|
346 | # This subclass ignores this attribute as it always need to return | |
346 | # something. |
|
347 | # something. | |
347 | enabled = Bool(True, config=False) |
|
348 | enabled = Bool(True, config=False) | |
348 |
|
349 | |||
349 | # Look for a _repr_pretty_ methods to use for pretty printing. |
|
350 | # Look for a _repr_pretty_ methods to use for pretty printing. | |
350 | print_method = ObjectName('_repr_pretty_') |
|
351 | print_method = ObjectName('_repr_pretty_') | |
351 |
|
352 | |||
352 | # Whether to pretty-print or not. |
|
353 | # Whether to pretty-print or not. | |
353 | pprint = Bool(True, config=True) |
|
354 | pprint = Bool(True, config=True) | |
354 |
|
355 | |||
355 | # Whether to be verbose or not. |
|
356 | # Whether to be verbose or not. | |
356 | verbose = Bool(False, config=True) |
|
357 | verbose = Bool(False, config=True) | |
357 |
|
358 | |||
358 | # The maximum width. |
|
359 | # The maximum width. | |
359 | max_width = Int(79, config=True) |
|
360 | max_width = Int(79, config=True) | |
360 |
|
361 | |||
361 | # The newline character. |
|
362 | # The newline character. | |
362 | newline = Unicode('\n', config=True) |
|
363 | newline = Unicode('\n', config=True) | |
363 |
|
364 | |||
364 | # format-string for pprinting floats |
|
365 | # format-string for pprinting floats | |
365 | float_format = Unicode('%r') |
|
366 | float_format = Unicode('%r') | |
366 | # setter for float precision, either int or direct format-string |
|
367 | # setter for float precision, either int or direct format-string | |
367 | float_precision = CUnicode('', config=True) |
|
368 | float_precision = CUnicode('', config=True) | |
368 |
|
369 | |||
369 | def _float_precision_changed(self, name, old, new): |
|
370 | def _float_precision_changed(self, name, old, new): | |
370 | """float_precision changed, set float_format accordingly. |
|
371 | """float_precision changed, set float_format accordingly. | |
371 |
|
372 | |||
372 | float_precision can be set by int or str. |
|
373 | float_precision can be set by int or str. | |
373 | This will set float_format, after interpreting input. |
|
374 | This will set float_format, after interpreting input. | |
374 | If numpy has been imported, numpy print precision will also be set. |
|
375 | If numpy has been imported, numpy print precision will also be set. | |
375 |
|
376 | |||
376 | integer `n` sets format to '%.nf', otherwise, format set directly. |
|
377 | integer `n` sets format to '%.nf', otherwise, format set directly. | |
377 |
|
378 | |||
378 | An empty string returns to defaults (repr for float, 8 for numpy). |
|
379 | An empty string returns to defaults (repr for float, 8 for numpy). | |
379 |
|
380 | |||
380 | This parameter can be set via the '%precision' magic. |
|
381 | This parameter can be set via the '%precision' magic. | |
381 | """ |
|
382 | """ | |
382 |
|
383 | |||
383 | if '%' in new: |
|
384 | if '%' in new: | |
384 | # got explicit format string |
|
385 | # got explicit format string | |
385 | fmt = new |
|
386 | fmt = new | |
386 | try: |
|
387 | try: | |
387 | fmt%3.14159 |
|
388 | fmt%3.14159 | |
388 | except Exception: |
|
389 | except Exception: | |
389 | raise ValueError("Precision must be int or format string, not %r"%new) |
|
390 | raise ValueError("Precision must be int or format string, not %r"%new) | |
390 | elif new: |
|
391 | elif new: | |
391 | # otherwise, should be an int |
|
392 | # otherwise, should be an int | |
392 | try: |
|
393 | try: | |
393 | i = int(new) |
|
394 | i = int(new) | |
394 | assert i >= 0 |
|
395 | assert i >= 0 | |
395 | except ValueError: |
|
396 | except ValueError: | |
396 | raise ValueError("Precision must be int or format string, not %r"%new) |
|
397 | raise ValueError("Precision must be int or format string, not %r"%new) | |
397 | except AssertionError: |
|
398 | except AssertionError: | |
398 | raise ValueError("int precision must be non-negative, not %r"%i) |
|
399 | raise ValueError("int precision must be non-negative, not %r"%i) | |
399 |
|
400 | |||
400 | fmt = '%%.%if'%i |
|
401 | fmt = '%%.%if'%i | |
401 | if 'numpy' in sys.modules: |
|
402 | if 'numpy' in sys.modules: | |
402 | # set numpy precision if it has been imported |
|
403 | # set numpy precision if it has been imported | |
403 | import numpy |
|
404 | import numpy | |
404 | numpy.set_printoptions(precision=i) |
|
405 | numpy.set_printoptions(precision=i) | |
405 | else: |
|
406 | else: | |
406 | # default back to repr |
|
407 | # default back to repr | |
407 | fmt = '%r' |
|
408 | fmt = '%r' | |
408 | if 'numpy' in sys.modules: |
|
409 | if 'numpy' in sys.modules: | |
409 | import numpy |
|
410 | import numpy | |
410 | # numpy default is 8 |
|
411 | # numpy default is 8 | |
411 | numpy.set_printoptions(precision=8) |
|
412 | numpy.set_printoptions(precision=8) | |
412 | self.float_format = fmt |
|
413 | self.float_format = fmt | |
413 |
|
414 | |||
414 | # Use the default pretty printers from IPython.external.pretty. |
|
415 | # Use the default pretty printers from IPython.external.pretty. | |
415 | def _singleton_printers_default(self): |
|
416 | def _singleton_printers_default(self): | |
416 | return pretty._singleton_pprinters.copy() |
|
417 | return pretty._singleton_pprinters.copy() | |
417 |
|
418 | |||
418 | def _type_printers_default(self): |
|
419 | def _type_printers_default(self): | |
419 | d = pretty._type_pprinters.copy() |
|
420 | d = pretty._type_pprinters.copy() | |
420 | d[float] = lambda obj,p,cycle: p.text(self.float_format%obj) |
|
421 | d[float] = lambda obj,p,cycle: p.text(self.float_format%obj) | |
421 | return d |
|
422 | return d | |
422 |
|
423 | |||
423 | def _deferred_printers_default(self): |
|
424 | def _deferred_printers_default(self): | |
424 | return pretty._deferred_type_pprinters.copy() |
|
425 | return pretty._deferred_type_pprinters.copy() | |
425 |
|
426 | |||
426 | #### FormatterABC interface #### |
|
427 | #### FormatterABC interface #### | |
427 |
|
428 | |||
428 | def __call__(self, obj): |
|
429 | def __call__(self, obj): | |
429 | """Compute the pretty representation of the object.""" |
|
430 | """Compute the pretty representation of the object.""" | |
430 | if not self.pprint: |
|
431 | if not self.pprint: | |
431 | try: |
|
432 | try: | |
432 | return repr(obj) |
|
433 | return repr(obj) | |
433 | except TypeError: |
|
434 | except TypeError: | |
434 | return '' |
|
435 | return '' | |
435 | else: |
|
436 | else: | |
436 | # This uses use StringIO, as cStringIO doesn't handle unicode. |
|
437 | # This uses use StringIO, as cStringIO doesn't handle unicode. | |
437 | stream = StringIO() |
|
438 | stream = StringIO() | |
438 | # self.newline.encode() is a quick fix for issue gh-597. We need to |
|
439 | # self.newline.encode() is a quick fix for issue gh-597. We need to | |
439 | # ensure that stream does not get a mix of unicode and bytestrings, |
|
440 | # ensure that stream does not get a mix of unicode and bytestrings, | |
440 | # or it will cause trouble. |
|
441 | # or it will cause trouble. | |
441 | printer = pretty.RepresentationPrinter(stream, self.verbose, |
|
442 | printer = pretty.RepresentationPrinter(stream, self.verbose, | |
442 |
self.max_width, self.newline |
|
443 | self.max_width, unicode_to_str(self.newline), | |
443 | singleton_pprinters=self.singleton_printers, |
|
444 | singleton_pprinters=self.singleton_printers, | |
444 | type_pprinters=self.type_printers, |
|
445 | type_pprinters=self.type_printers, | |
445 | deferred_pprinters=self.deferred_printers) |
|
446 | deferred_pprinters=self.deferred_printers) | |
446 | printer.pretty(obj) |
|
447 | printer.pretty(obj) | |
447 | printer.flush() |
|
448 | printer.flush() | |
448 | return stream.getvalue() |
|
449 | return stream.getvalue() | |
449 |
|
450 | |||
450 |
|
451 | |||
451 | class HTMLFormatter(BaseFormatter): |
|
452 | class HTMLFormatter(BaseFormatter): | |
452 | """An HTML formatter. |
|
453 | """An HTML formatter. | |
453 |
|
454 | |||
454 | To define the callables that compute the HTML representation of your |
|
455 | To define the callables that compute the HTML representation of your | |
455 | objects, define a :meth:`_repr_html_` method or use the :meth:`for_type` |
|
456 | objects, define a :meth:`_repr_html_` method or use the :meth:`for_type` | |
456 | or :meth:`for_type_by_name` methods to register functions that handle |
|
457 | or :meth:`for_type_by_name` methods to register functions that handle | |
457 | this. |
|
458 | this. | |
458 |
|
459 | |||
459 | The return value of this formatter should be a valid HTML snippet that |
|
460 | The return value of this formatter should be a valid HTML snippet that | |
460 | could be injected into an existing DOM. It should *not* include the |
|
461 | could be injected into an existing DOM. It should *not* include the | |
461 | ```<html>`` or ```<body>`` tags. |
|
462 | ```<html>`` or ```<body>`` tags. | |
462 | """ |
|
463 | """ | |
463 | format_type = Unicode('text/html') |
|
464 | format_type = Unicode('text/html') | |
464 |
|
465 | |||
465 | print_method = ObjectName('_repr_html_') |
|
466 | print_method = ObjectName('_repr_html_') | |
466 |
|
467 | |||
467 |
|
468 | |||
468 | class SVGFormatter(BaseFormatter): |
|
469 | class SVGFormatter(BaseFormatter): | |
469 | """An SVG formatter. |
|
470 | """An SVG formatter. | |
470 |
|
471 | |||
471 | To define the callables that compute the SVG representation of your |
|
472 | To define the callables that compute the SVG representation of your | |
472 | objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type` |
|
473 | objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type` | |
473 | or :meth:`for_type_by_name` methods to register functions that handle |
|
474 | or :meth:`for_type_by_name` methods to register functions that handle | |
474 | this. |
|
475 | this. | |
475 |
|
476 | |||
476 | The return value of this formatter should be valid SVG enclosed in |
|
477 | The return value of this formatter should be valid SVG enclosed in | |
477 | ```<svg>``` tags, that could be injected into an existing DOM. It should |
|
478 | ```<svg>``` tags, that could be injected into an existing DOM. It should | |
478 | *not* include the ```<html>`` or ```<body>`` tags. |
|
479 | *not* include the ```<html>`` or ```<body>`` tags. | |
479 | """ |
|
480 | """ | |
480 | format_type = Unicode('image/svg+xml') |
|
481 | format_type = Unicode('image/svg+xml') | |
481 |
|
482 | |||
482 | print_method = ObjectName('_repr_svg_') |
|
483 | print_method = ObjectName('_repr_svg_') | |
483 |
|
484 | |||
484 |
|
485 | |||
485 | class PNGFormatter(BaseFormatter): |
|
486 | class PNGFormatter(BaseFormatter): | |
486 | """A PNG formatter. |
|
487 | """A PNG formatter. | |
487 |
|
488 | |||
488 | To define the callables that compute the PNG representation of your |
|
489 | To define the callables that compute the PNG representation of your | |
489 | objects, define a :meth:`_repr_png_` method or use the :meth:`for_type` |
|
490 | objects, define a :meth:`_repr_png_` method or use the :meth:`for_type` | |
490 | or :meth:`for_type_by_name` methods to register functions that handle |
|
491 | or :meth:`for_type_by_name` methods to register functions that handle | |
491 | this. |
|
492 | this. | |
492 |
|
493 | |||
493 | The return value of this formatter should be raw PNG data, *not* |
|
494 | The return value of this formatter should be raw PNG data, *not* | |
494 | base64 encoded. |
|
495 | base64 encoded. | |
495 | """ |
|
496 | """ | |
496 | format_type = Unicode('image/png') |
|
497 | format_type = Unicode('image/png') | |
497 |
|
498 | |||
498 | print_method = ObjectName('_repr_png_') |
|
499 | print_method = ObjectName('_repr_png_') | |
499 |
|
500 | |||
500 |
|
501 | |||
501 | class JPEGFormatter(BaseFormatter): |
|
502 | class JPEGFormatter(BaseFormatter): | |
502 | """A JPEG formatter. |
|
503 | """A JPEG formatter. | |
503 |
|
504 | |||
504 | To define the callables that compute the JPEG representation of your |
|
505 | To define the callables that compute the JPEG representation of your | |
505 | objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type` |
|
506 | objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type` | |
506 | or :meth:`for_type_by_name` methods to register functions that handle |
|
507 | or :meth:`for_type_by_name` methods to register functions that handle | |
507 | this. |
|
508 | this. | |
508 |
|
509 | |||
509 | The return value of this formatter should be raw JPEG data, *not* |
|
510 | The return value of this formatter should be raw JPEG data, *not* | |
510 | base64 encoded. |
|
511 | base64 encoded. | |
511 | """ |
|
512 | """ | |
512 | format_type = Unicode('image/jpeg') |
|
513 | format_type = Unicode('image/jpeg') | |
513 |
|
514 | |||
514 | print_method = ObjectName('_repr_jpeg_') |
|
515 | print_method = ObjectName('_repr_jpeg_') | |
515 |
|
516 | |||
516 |
|
517 | |||
517 | class LatexFormatter(BaseFormatter): |
|
518 | class LatexFormatter(BaseFormatter): | |
518 | """A LaTeX formatter. |
|
519 | """A LaTeX formatter. | |
519 |
|
520 | |||
520 | To define the callables that compute the LaTeX representation of your |
|
521 | To define the callables that compute the LaTeX representation of your | |
521 | objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type` |
|
522 | objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type` | |
522 | or :meth:`for_type_by_name` methods to register functions that handle |
|
523 | or :meth:`for_type_by_name` methods to register functions that handle | |
523 | this. |
|
524 | this. | |
524 |
|
525 | |||
525 | The return value of this formatter should be a valid LaTeX equation, |
|
526 | The return value of this formatter should be a valid LaTeX equation, | |
526 | enclosed in either ```$``` or ```$$```. |
|
527 | enclosed in either ```$``` or ```$$```. | |
527 | """ |
|
528 | """ | |
528 | format_type = Unicode('text/latex') |
|
529 | format_type = Unicode('text/latex') | |
529 |
|
530 | |||
530 | print_method = ObjectName('_repr_latex_') |
|
531 | print_method = ObjectName('_repr_latex_') | |
531 |
|
532 | |||
532 |
|
533 | |||
533 | class JSONFormatter(BaseFormatter): |
|
534 | class JSONFormatter(BaseFormatter): | |
534 | """A JSON string formatter. |
|
535 | """A JSON string formatter. | |
535 |
|
536 | |||
536 | To define the callables that compute the JSON string representation of |
|
537 | To define the callables that compute the JSON string representation of | |
537 | your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type` |
|
538 | your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type` | |
538 | or :meth:`for_type_by_name` methods to register functions that handle |
|
539 | or :meth:`for_type_by_name` methods to register functions that handle | |
539 | this. |
|
540 | this. | |
540 |
|
541 | |||
541 | The return value of this formatter should be a valid JSON string. |
|
542 | The return value of this formatter should be a valid JSON string. | |
542 | """ |
|
543 | """ | |
543 | format_type = Unicode('application/json') |
|
544 | format_type = Unicode('application/json') | |
544 |
|
545 | |||
545 | print_method = ObjectName('_repr_json_') |
|
546 | print_method = ObjectName('_repr_json_') | |
546 |
|
547 | |||
547 |
|
548 | |||
548 | class JavascriptFormatter(BaseFormatter): |
|
549 | class JavascriptFormatter(BaseFormatter): | |
549 | """A Javascript formatter. |
|
550 | """A Javascript formatter. | |
550 |
|
551 | |||
551 | To define the callables that compute the Javascript representation of |
|
552 | To define the callables that compute the Javascript representation of | |
552 | your objects, define a :meth:`_repr_javascript_` method or use the |
|
553 | your objects, define a :meth:`_repr_javascript_` method or use the | |
553 | :meth:`for_type` or :meth:`for_type_by_name` methods to register functions |
|
554 | :meth:`for_type` or :meth:`for_type_by_name` methods to register functions | |
554 | that handle this. |
|
555 | that handle this. | |
555 |
|
556 | |||
556 | The return value of this formatter should be valid Javascript code and |
|
557 | The return value of this formatter should be valid Javascript code and | |
557 | should *not* be enclosed in ```<script>``` tags. |
|
558 | should *not* be enclosed in ```<script>``` tags. | |
558 | """ |
|
559 | """ | |
559 | format_type = Unicode('application/javascript') |
|
560 | format_type = Unicode('application/javascript') | |
560 |
|
561 | |||
561 | print_method = ObjectName('_repr_javascript_') |
|
562 | print_method = ObjectName('_repr_javascript_') | |
562 |
|
563 | |||
563 | FormatterABC.register(BaseFormatter) |
|
564 | FormatterABC.register(BaseFormatter) | |
564 | FormatterABC.register(PlainTextFormatter) |
|
565 | FormatterABC.register(PlainTextFormatter) | |
565 | FormatterABC.register(HTMLFormatter) |
|
566 | FormatterABC.register(HTMLFormatter) | |
566 | FormatterABC.register(SVGFormatter) |
|
567 | FormatterABC.register(SVGFormatter) | |
567 | FormatterABC.register(PNGFormatter) |
|
568 | FormatterABC.register(PNGFormatter) | |
568 | FormatterABC.register(JPEGFormatter) |
|
569 | FormatterABC.register(JPEGFormatter) | |
569 | FormatterABC.register(LatexFormatter) |
|
570 | FormatterABC.register(LatexFormatter) | |
570 | FormatterABC.register(JSONFormatter) |
|
571 | FormatterABC.register(JSONFormatter) | |
571 | FormatterABC.register(JavascriptFormatter) |
|
572 | FormatterABC.register(JavascriptFormatter) | |
572 |
|
573 | |||
573 |
|
574 | |||
574 | def format_display_data(obj, include=None, exclude=None): |
|
575 | def format_display_data(obj, include=None, exclude=None): | |
575 | """Return a format data dict for an object. |
|
576 | """Return a format data dict for an object. | |
576 |
|
577 | |||
577 | By default all format types will be computed. |
|
578 | By default all format types will be computed. | |
578 |
|
579 | |||
579 | The following MIME types are currently implemented: |
|
580 | The following MIME types are currently implemented: | |
580 |
|
581 | |||
581 | * text/plain |
|
582 | * text/plain | |
582 | * text/html |
|
583 | * text/html | |
583 | * text/latex |
|
584 | * text/latex | |
584 | * application/json |
|
585 | * application/json | |
585 | * application/javascript |
|
586 | * application/javascript | |
586 | * image/png |
|
587 | * image/png | |
587 | * image/jpeg |
|
588 | * image/jpeg | |
588 | * image/svg+xml |
|
589 | * image/svg+xml | |
589 |
|
590 | |||
590 | Parameters |
|
591 | Parameters | |
591 | ---------- |
|
592 | ---------- | |
592 | obj : object |
|
593 | obj : object | |
593 | The Python object whose format data will be computed. |
|
594 | The Python object whose format data will be computed. | |
594 |
|
595 | |||
595 | Returns |
|
596 | Returns | |
596 | ------- |
|
597 | ------- | |
597 | format_dict : dict |
|
598 | format_dict : dict | |
598 | A dictionary of key/value pairs, one or each format that was |
|
599 | A dictionary of key/value pairs, one or each format that was | |
599 | generated for the object. The keys are the format types, which |
|
600 | generated for the object. The keys are the format types, which | |
600 | will usually be MIME type strings and the values and JSON'able |
|
601 | will usually be MIME type strings and the values and JSON'able | |
601 | data structure containing the raw data for the representation in |
|
602 | data structure containing the raw data for the representation in | |
602 | that format. |
|
603 | that format. | |
603 | include : list or tuple, optional |
|
604 | include : list or tuple, optional | |
604 | A list of format type strings (MIME types) to include in the |
|
605 | A list of format type strings (MIME types) to include in the | |
605 | format data dict. If this is set *only* the format types included |
|
606 | format data dict. If this is set *only* the format types included | |
606 | in this list will be computed. |
|
607 | in this list will be computed. | |
607 | exclude : list or tuple, optional |
|
608 | exclude : list or tuple, optional | |
608 | A list of format type string (MIME types) to exclue in the format |
|
609 | A list of format type string (MIME types) to exclue in the format | |
609 | data dict. If this is set all format types will be computed, |
|
610 | data dict. If this is set all format types will be computed, | |
610 | except for those included in this argument. |
|
611 | except for those included in this argument. | |
611 | """ |
|
612 | """ | |
612 | from IPython.core.interactiveshell import InteractiveShell |
|
613 | from IPython.core.interactiveshell import InteractiveShell | |
613 |
|
614 | |||
614 | InteractiveShell.instance().display_formatter.format( |
|
615 | InteractiveShell.instance().display_formatter.format( | |
615 | obj, |
|
616 | obj, | |
616 | include, |
|
617 | include, | |
617 | exclude |
|
618 | exclude | |
618 | ) |
|
619 | ) | |
619 |
|
620 |
@@ -1,892 +1,892 b'' | |||||
1 | """Analysis of text input into executable blocks. |
|
1 | """Analysis of text input into executable blocks. | |
2 |
|
2 | |||
3 | The main class in this module, :class:`InputSplitter`, is designed to break |
|
3 | The main class in this module, :class:`InputSplitter`, is designed to break | |
4 | input from either interactive, line-by-line environments or block-based ones, |
|
4 | input from either interactive, line-by-line environments or block-based ones, | |
5 | into standalone blocks that can be executed by Python as 'single' statements |
|
5 | into standalone blocks that can be executed by Python as 'single' statements | |
6 | (thus triggering sys.displayhook). |
|
6 | (thus triggering sys.displayhook). | |
7 |
|
7 | |||
8 | A companion, :class:`IPythonInputSplitter`, provides the same functionality but |
|
8 | A companion, :class:`IPythonInputSplitter`, provides the same functionality but | |
9 | with full support for the extended IPython syntax (magics, system calls, etc). |
|
9 | with full support for the extended IPython syntax (magics, system calls, etc). | |
10 |
|
10 | |||
11 | For more details, see the class docstring below. |
|
11 | For more details, see the class docstring below. | |
12 |
|
12 | |||
13 | Syntax Transformations |
|
13 | Syntax Transformations | |
14 | ---------------------- |
|
14 | ---------------------- | |
15 |
|
15 | |||
16 | One of the main jobs of the code in this file is to apply all syntax |
|
16 | One of the main jobs of the code in this file is to apply all syntax | |
17 | transformations that make up 'the IPython language', i.e. magics, shell |
|
17 | transformations that make up 'the IPython language', i.e. magics, shell | |
18 | escapes, etc. All transformations should be implemented as *fully stateless* |
|
18 | escapes, etc. All transformations should be implemented as *fully stateless* | |
19 | entities, that simply take one line as their input and return a line. |
|
19 | entities, that simply take one line as their input and return a line. | |
20 | Internally for implementation purposes they may be a normal function or a |
|
20 | Internally for implementation purposes they may be a normal function or a | |
21 | callable object, but the only input they receive will be a single line and they |
|
21 | callable object, but the only input they receive will be a single line and they | |
22 | should only return a line, without holding any data-dependent state between |
|
22 | should only return a line, without holding any data-dependent state between | |
23 | calls. |
|
23 | calls. | |
24 |
|
24 | |||
25 | As an example, the EscapedTransformer is a class so we can more clearly group |
|
25 | As an example, the EscapedTransformer is a class so we can more clearly group | |
26 | together the functionality of dispatching to individual functions based on the |
|
26 | together the functionality of dispatching to individual functions based on the | |
27 | starting escape character, but the only method for public use is its call |
|
27 | starting escape character, but the only method for public use is its call | |
28 | method. |
|
28 | method. | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | ToDo |
|
31 | ToDo | |
32 | ---- |
|
32 | ---- | |
33 |
|
33 | |||
34 | - Should we make push() actually raise an exception once push_accepts_more() |
|
34 | - Should we make push() actually raise an exception once push_accepts_more() | |
35 | returns False? |
|
35 | returns False? | |
36 |
|
36 | |||
37 | - Naming cleanups. The tr_* names aren't the most elegant, though now they are |
|
37 | - Naming cleanups. The tr_* names aren't the most elegant, though now they are | |
38 | at least just attributes of a class so not really very exposed. |
|
38 | at least just attributes of a class so not really very exposed. | |
39 |
|
39 | |||
40 | - Think about the best way to support dynamic things: automagic, autocall, |
|
40 | - Think about the best way to support dynamic things: automagic, autocall, | |
41 | macros, etc. |
|
41 | macros, etc. | |
42 |
|
42 | |||
43 | - Think of a better heuristic for the application of the transforms in |
|
43 | - Think of a better heuristic for the application of the transforms in | |
44 | IPythonInputSplitter.push() than looking at the buffer ending in ':'. Idea: |
|
44 | IPythonInputSplitter.push() than looking at the buffer ending in ':'. Idea: | |
45 | track indentation change events (indent, dedent, nothing) and apply them only |
|
45 | track indentation change events (indent, dedent, nothing) and apply them only | |
46 | if the indentation went up, but not otherwise. |
|
46 | if the indentation went up, but not otherwise. | |
47 |
|
47 | |||
48 | - Think of the cleanest way for supporting user-specified transformations (the |
|
48 | - Think of the cleanest way for supporting user-specified transformations (the | |
49 | user prefilters we had before). |
|
49 | user prefilters we had before). | |
50 |
|
50 | |||
51 | Authors |
|
51 | Authors | |
52 | ------- |
|
52 | ------- | |
53 |
|
53 | |||
54 | * Fernando Perez |
|
54 | * Fernando Perez | |
55 | * Brian Granger |
|
55 | * Brian Granger | |
56 | """ |
|
56 | """ | |
57 | #----------------------------------------------------------------------------- |
|
57 | #----------------------------------------------------------------------------- | |
58 | # Copyright (C) 2010 The IPython Development Team |
|
58 | # Copyright (C) 2010 The IPython Development Team | |
59 | # |
|
59 | # | |
60 | # Distributed under the terms of the BSD License. The full license is in |
|
60 | # Distributed under the terms of the BSD License. The full license is in | |
61 | # the file COPYING, distributed as part of this software. |
|
61 | # the file COPYING, distributed as part of this software. | |
62 | #----------------------------------------------------------------------------- |
|
62 | #----------------------------------------------------------------------------- | |
63 | from __future__ import print_function |
|
63 | from __future__ import print_function | |
64 |
|
64 | |||
65 | #----------------------------------------------------------------------------- |
|
65 | #----------------------------------------------------------------------------- | |
66 | # Imports |
|
66 | # Imports | |
67 | #----------------------------------------------------------------------------- |
|
67 | #----------------------------------------------------------------------------- | |
68 | # stdlib |
|
68 | # stdlib | |
69 | import ast |
|
69 | import ast | |
70 | import codeop |
|
70 | import codeop | |
71 | import re |
|
71 | import re | |
72 | import sys |
|
72 | import sys | |
73 | import tokenize |
|
73 | import tokenize | |
74 | from StringIO import StringIO |
|
74 | from StringIO import StringIO | |
75 |
|
75 | |||
76 | # IPython modules |
|
76 | # IPython modules | |
77 | from IPython.utils.text import make_quoted_expr |
|
77 | from IPython.utils.text import make_quoted_expr | |
|
78 | from IPython.utils.py3compat import cast_unicode | |||
78 |
|
79 | |||
79 | #----------------------------------------------------------------------------- |
|
80 | #----------------------------------------------------------------------------- | |
80 | # Globals |
|
81 | # Globals | |
81 | #----------------------------------------------------------------------------- |
|
82 | #----------------------------------------------------------------------------- | |
82 |
|
83 | |||
83 | # The escape sequences that define the syntax transformations IPython will |
|
84 | # The escape sequences that define the syntax transformations IPython will | |
84 | # apply to user input. These can NOT be just changed here: many regular |
|
85 | # apply to user input. These can NOT be just changed here: many regular | |
85 | # expressions and other parts of the code may use their hardcoded values, and |
|
86 | # expressions and other parts of the code may use their hardcoded values, and | |
86 | # for all intents and purposes they constitute the 'IPython syntax', so they |
|
87 | # for all intents and purposes they constitute the 'IPython syntax', so they | |
87 | # should be considered fixed. |
|
88 | # should be considered fixed. | |
88 |
|
89 | |||
89 | ESC_SHELL = '!' # Send line to underlying system shell |
|
90 | ESC_SHELL = '!' # Send line to underlying system shell | |
90 | ESC_SH_CAP = '!!' # Send line to system shell and capture output |
|
91 | ESC_SH_CAP = '!!' # Send line to system shell and capture output | |
91 | ESC_HELP = '?' # Find information about object |
|
92 | ESC_HELP = '?' # Find information about object | |
92 | ESC_HELP2 = '??' # Find extra-detailed information about object |
|
93 | ESC_HELP2 = '??' # Find extra-detailed information about object | |
93 | ESC_MAGIC = '%' # Call magic function |
|
94 | ESC_MAGIC = '%' # Call magic function | |
94 | ESC_QUOTE = ',' # Split args on whitespace, quote each as string and call |
|
95 | ESC_QUOTE = ',' # Split args on whitespace, quote each as string and call | |
95 | ESC_QUOTE2 = ';' # Quote all args as a single string, call |
|
96 | ESC_QUOTE2 = ';' # Quote all args as a single string, call | |
96 | ESC_PAREN = '/' # Call first argument with rest of line as arguments |
|
97 | ESC_PAREN = '/' # Call first argument with rest of line as arguments | |
97 |
|
98 | |||
98 | #----------------------------------------------------------------------------- |
|
99 | #----------------------------------------------------------------------------- | |
99 | # Utilities |
|
100 | # Utilities | |
100 | #----------------------------------------------------------------------------- |
|
101 | #----------------------------------------------------------------------------- | |
101 |
|
102 | |||
102 | # FIXME: These are general-purpose utilities that later can be moved to the |
|
103 | # FIXME: These are general-purpose utilities that later can be moved to the | |
103 | # general ward. Kept here for now because we're being very strict about test |
|
104 | # general ward. Kept here for now because we're being very strict about test | |
104 | # coverage with this code, and this lets us ensure that we keep 100% coverage |
|
105 | # coverage with this code, and this lets us ensure that we keep 100% coverage | |
105 | # while developing. |
|
106 | # while developing. | |
106 |
|
107 | |||
107 | # compiled regexps for autoindent management |
|
108 | # compiled regexps for autoindent management | |
108 | dedent_re = re.compile('|'.join([ |
|
109 | dedent_re = re.compile('|'.join([ | |
109 | r'^\s+raise(\s.*)?$', # raise statement (+ space + other stuff, maybe) |
|
110 | r'^\s+raise(\s.*)?$', # raise statement (+ space + other stuff, maybe) | |
110 | r'^\s+raise\([^\)]*\).*$', # wacky raise with immediate open paren |
|
111 | r'^\s+raise\([^\)]*\).*$', # wacky raise with immediate open paren | |
111 | r'^\s+return(\s.*)?$', # normal return (+ space + other stuff, maybe) |
|
112 | r'^\s+return(\s.*)?$', # normal return (+ space + other stuff, maybe) | |
112 | r'^\s+return\([^\)]*\).*$', # wacky return with immediate open paren |
|
113 | r'^\s+return\([^\)]*\).*$', # wacky return with immediate open paren | |
113 | r'^\s+pass\s*$' # pass (optionally followed by trailing spaces) |
|
114 | r'^\s+pass\s*$' # pass (optionally followed by trailing spaces) | |
114 | ])) |
|
115 | ])) | |
115 | ini_spaces_re = re.compile(r'^([ \t\r\f\v]+)') |
|
116 | ini_spaces_re = re.compile(r'^([ \t\r\f\v]+)') | |
116 |
|
117 | |||
117 | # regexp to match pure comment lines so we don't accidentally insert 'if 1:' |
|
118 | # regexp to match pure comment lines so we don't accidentally insert 'if 1:' | |
118 | # before pure comments |
|
119 | # before pure comments | |
119 | comment_line_re = re.compile('^\s*\#') |
|
120 | comment_line_re = re.compile('^\s*\#') | |
120 |
|
121 | |||
121 |
|
122 | |||
122 | def num_ini_spaces(s): |
|
123 | def num_ini_spaces(s): | |
123 | """Return the number of initial spaces in a string. |
|
124 | """Return the number of initial spaces in a string. | |
124 |
|
125 | |||
125 | Note that tabs are counted as a single space. For now, we do *not* support |
|
126 | Note that tabs are counted as a single space. For now, we do *not* support | |
126 | mixing of tabs and spaces in the user's input. |
|
127 | mixing of tabs and spaces in the user's input. | |
127 |
|
128 | |||
128 | Parameters |
|
129 | Parameters | |
129 | ---------- |
|
130 | ---------- | |
130 | s : string |
|
131 | s : string | |
131 |
|
132 | |||
132 | Returns |
|
133 | Returns | |
133 | ------- |
|
134 | ------- | |
134 | n : int |
|
135 | n : int | |
135 | """ |
|
136 | """ | |
136 |
|
137 | |||
137 | ini_spaces = ini_spaces_re.match(s) |
|
138 | ini_spaces = ini_spaces_re.match(s) | |
138 | if ini_spaces: |
|
139 | if ini_spaces: | |
139 | return ini_spaces.end() |
|
140 | return ini_spaces.end() | |
140 | else: |
|
141 | else: | |
141 | return 0 |
|
142 | return 0 | |
142 |
|
143 | |||
143 |
|
144 | |||
144 | def remove_comments(src): |
|
145 | def remove_comments(src): | |
145 | """Remove all comments from input source. |
|
146 | """Remove all comments from input source. | |
146 |
|
147 | |||
147 | Note: comments are NOT recognized inside of strings! |
|
148 | Note: comments are NOT recognized inside of strings! | |
148 |
|
149 | |||
149 | Parameters |
|
150 | Parameters | |
150 | ---------- |
|
151 | ---------- | |
151 | src : string |
|
152 | src : string | |
152 | A single or multiline input string. |
|
153 | A single or multiline input string. | |
153 |
|
154 | |||
154 | Returns |
|
155 | Returns | |
155 | ------- |
|
156 | ------- | |
156 | String with all Python comments removed. |
|
157 | String with all Python comments removed. | |
157 | """ |
|
158 | """ | |
158 |
|
159 | |||
159 | return re.sub('#.*', '', src) |
|
160 | return re.sub('#.*', '', src) | |
160 |
|
161 | |||
161 | def has_comment(src): |
|
162 | def has_comment(src): | |
162 | """Indicate whether an input line has (i.e. ends in, or is) a comment. |
|
163 | """Indicate whether an input line has (i.e. ends in, or is) a comment. | |
163 |
|
164 | |||
164 | This uses tokenize, so it can distinguish comments from # inside strings. |
|
165 | This uses tokenize, so it can distinguish comments from # inside strings. | |
165 |
|
166 | |||
166 | Parameters |
|
167 | Parameters | |
167 | ---------- |
|
168 | ---------- | |
168 | src : string |
|
169 | src : string | |
169 | A single line input string. |
|
170 | A single line input string. | |
170 |
|
171 | |||
171 | Returns |
|
172 | Returns | |
172 | ------- |
|
173 | ------- | |
173 | Boolean: True if source has a comment. |
|
174 | Boolean: True if source has a comment. | |
174 | """ |
|
175 | """ | |
175 | readline = StringIO(src).readline |
|
176 | readline = StringIO(src).readline | |
176 | toktypes = set() |
|
177 | toktypes = set() | |
177 | try: |
|
178 | try: | |
178 | for t in tokenize.generate_tokens(readline): |
|
179 | for t in tokenize.generate_tokens(readline): | |
179 | toktypes.add(t[0]) |
|
180 | toktypes.add(t[0]) | |
180 | except tokenize.TokenError: |
|
181 | except tokenize.TokenError: | |
181 | pass |
|
182 | pass | |
182 | return(tokenize.COMMENT in toktypes) |
|
183 | return(tokenize.COMMENT in toktypes) | |
183 |
|
184 | |||
184 |
|
185 | |||
185 | def get_input_encoding(): |
|
186 | def get_input_encoding(): | |
186 | """Return the default standard input encoding. |
|
187 | """Return the default standard input encoding. | |
187 |
|
188 | |||
188 | If sys.stdin has no encoding, 'ascii' is returned.""" |
|
189 | If sys.stdin has no encoding, 'ascii' is returned.""" | |
189 | # There are strange environments for which sys.stdin.encoding is None. We |
|
190 | # There are strange environments for which sys.stdin.encoding is None. We | |
190 | # ensure that a valid encoding is returned. |
|
191 | # ensure that a valid encoding is returned. | |
191 | encoding = getattr(sys.stdin, 'encoding', None) |
|
192 | encoding = getattr(sys.stdin, 'encoding', None) | |
192 | if encoding is None: |
|
193 | if encoding is None: | |
193 | encoding = 'ascii' |
|
194 | encoding = 'ascii' | |
194 | return encoding |
|
195 | return encoding | |
195 |
|
196 | |||
196 | #----------------------------------------------------------------------------- |
|
197 | #----------------------------------------------------------------------------- | |
197 | # Classes and functions for normal Python syntax handling |
|
198 | # Classes and functions for normal Python syntax handling | |
198 | #----------------------------------------------------------------------------- |
|
199 | #----------------------------------------------------------------------------- | |
199 |
|
200 | |||
200 | class InputSplitter(object): |
|
201 | class InputSplitter(object): | |
201 | """An object that can accumulate lines of Python source before execution. |
|
202 | """An object that can accumulate lines of Python source before execution. | |
202 |
|
203 | |||
203 | This object is designed to be fed python source line-by-line, using |
|
204 | This object is designed to be fed python source line-by-line, using | |
204 | :meth:`push`. It will return on each push whether the currently pushed |
|
205 | :meth:`push`. It will return on each push whether the currently pushed | |
205 | code could be executed already. In addition, it provides a method called |
|
206 | code could be executed already. In addition, it provides a method called | |
206 | :meth:`push_accepts_more` that can be used to query whether more input |
|
207 | :meth:`push_accepts_more` that can be used to query whether more input | |
207 | can be pushed into a single interactive block. |
|
208 | can be pushed into a single interactive block. | |
208 |
|
209 | |||
209 | This is a simple example of how an interactive terminal-based client can use |
|
210 | This is a simple example of how an interactive terminal-based client can use | |
210 | this tool:: |
|
211 | this tool:: | |
211 |
|
212 | |||
212 | isp = InputSplitter() |
|
213 | isp = InputSplitter() | |
213 | while isp.push_accepts_more(): |
|
214 | while isp.push_accepts_more(): | |
214 | indent = ' '*isp.indent_spaces |
|
215 | indent = ' '*isp.indent_spaces | |
215 | prompt = '>>> ' + indent |
|
216 | prompt = '>>> ' + indent | |
216 | line = indent + raw_input(prompt) |
|
217 | line = indent + raw_input(prompt) | |
217 | isp.push(line) |
|
218 | isp.push(line) | |
218 | print 'Input source was:\n', isp.source_reset(), |
|
219 | print 'Input source was:\n', isp.source_reset(), | |
219 | """ |
|
220 | """ | |
220 | # Number of spaces of indentation computed from input that has been pushed |
|
221 | # Number of spaces of indentation computed from input that has been pushed | |
221 | # so far. This is the attributes callers should query to get the current |
|
222 | # so far. This is the attributes callers should query to get the current | |
222 | # indentation level, in order to provide auto-indent facilities. |
|
223 | # indentation level, in order to provide auto-indent facilities. | |
223 | indent_spaces = 0 |
|
224 | indent_spaces = 0 | |
224 | # String, indicating the default input encoding. It is computed by default |
|
225 | # String, indicating the default input encoding. It is computed by default | |
225 | # at initialization time via get_input_encoding(), but it can be reset by a |
|
226 | # at initialization time via get_input_encoding(), but it can be reset by a | |
226 | # client with specific knowledge of the encoding. |
|
227 | # client with specific knowledge of the encoding. | |
227 | encoding = '' |
|
228 | encoding = '' | |
228 | # String where the current full source input is stored, properly encoded. |
|
229 | # String where the current full source input is stored, properly encoded. | |
229 | # Reading this attribute is the normal way of querying the currently pushed |
|
230 | # Reading this attribute is the normal way of querying the currently pushed | |
230 | # source code, that has been properly encoded. |
|
231 | # source code, that has been properly encoded. | |
231 | source = '' |
|
232 | source = '' | |
232 | # Code object corresponding to the current source. It is automatically |
|
233 | # Code object corresponding to the current source. It is automatically | |
233 | # synced to the source, so it can be queried at any time to obtain the code |
|
234 | # synced to the source, so it can be queried at any time to obtain the code | |
234 | # object; it will be None if the source doesn't compile to valid Python. |
|
235 | # object; it will be None if the source doesn't compile to valid Python. | |
235 | code = None |
|
236 | code = None | |
236 | # Input mode |
|
237 | # Input mode | |
237 | input_mode = 'line' |
|
238 | input_mode = 'line' | |
238 |
|
239 | |||
239 | # Private attributes |
|
240 | # Private attributes | |
240 |
|
241 | |||
241 | # List with lines of input accumulated so far |
|
242 | # List with lines of input accumulated so far | |
242 | _buffer = None |
|
243 | _buffer = None | |
243 | # Command compiler |
|
244 | # Command compiler | |
244 | _compile = None |
|
245 | _compile = None | |
245 | # Mark when input has changed indentation all the way back to flush-left |
|
246 | # Mark when input has changed indentation all the way back to flush-left | |
246 | _full_dedent = False |
|
247 | _full_dedent = False | |
247 | # Boolean indicating whether the current block is complete |
|
248 | # Boolean indicating whether the current block is complete | |
248 | _is_complete = None |
|
249 | _is_complete = None | |
249 |
|
250 | |||
250 | def __init__(self, input_mode=None): |
|
251 | def __init__(self, input_mode=None): | |
251 | """Create a new InputSplitter instance. |
|
252 | """Create a new InputSplitter instance. | |
252 |
|
253 | |||
253 | Parameters |
|
254 | Parameters | |
254 | ---------- |
|
255 | ---------- | |
255 | input_mode : str |
|
256 | input_mode : str | |
256 |
|
257 | |||
257 | One of ['line', 'cell']; default is 'line'. |
|
258 | One of ['line', 'cell']; default is 'line'. | |
258 |
|
259 | |||
259 | The input_mode parameter controls how new inputs are used when fed via |
|
260 | The input_mode parameter controls how new inputs are used when fed via | |
260 | the :meth:`push` method: |
|
261 | the :meth:`push` method: | |
261 |
|
262 | |||
262 | - 'line': meant for line-oriented clients, inputs are appended one at a |
|
263 | - 'line': meant for line-oriented clients, inputs are appended one at a | |
263 | time to the internal buffer and the whole buffer is compiled. |
|
264 | time to the internal buffer and the whole buffer is compiled. | |
264 |
|
265 | |||
265 | - 'cell': meant for clients that can edit multi-line 'cells' of text at |
|
266 | - 'cell': meant for clients that can edit multi-line 'cells' of text at | |
266 | a time. A cell can contain one or more blocks that can be compile in |
|
267 | a time. A cell can contain one or more blocks that can be compile in | |
267 | 'single' mode by Python. In this mode, each new input new input |
|
268 | 'single' mode by Python. In this mode, each new input new input | |
268 | completely replaces all prior inputs. Cell mode is thus equivalent |
|
269 | completely replaces all prior inputs. Cell mode is thus equivalent | |
269 | to prepending a full reset() to every push() call. |
|
270 | to prepending a full reset() to every push() call. | |
270 | """ |
|
271 | """ | |
271 | self._buffer = [] |
|
272 | self._buffer = [] | |
272 | self._compile = codeop.CommandCompiler() |
|
273 | self._compile = codeop.CommandCompiler() | |
273 | self.encoding = get_input_encoding() |
|
274 | self.encoding = get_input_encoding() | |
274 | self.input_mode = InputSplitter.input_mode if input_mode is None \ |
|
275 | self.input_mode = InputSplitter.input_mode if input_mode is None \ | |
275 | else input_mode |
|
276 | else input_mode | |
276 |
|
277 | |||
277 | def reset(self): |
|
278 | def reset(self): | |
278 | """Reset the input buffer and associated state.""" |
|
279 | """Reset the input buffer and associated state.""" | |
279 | self.indent_spaces = 0 |
|
280 | self.indent_spaces = 0 | |
280 | self._buffer[:] = [] |
|
281 | self._buffer[:] = [] | |
281 | self.source = '' |
|
282 | self.source = '' | |
282 | self.code = None |
|
283 | self.code = None | |
283 | self._is_complete = False |
|
284 | self._is_complete = False | |
284 | self._full_dedent = False |
|
285 | self._full_dedent = False | |
285 |
|
286 | |||
286 | def source_reset(self): |
|
287 | def source_reset(self): | |
287 | """Return the input source and perform a full reset. |
|
288 | """Return the input source and perform a full reset. | |
288 | """ |
|
289 | """ | |
289 | out = self.source |
|
290 | out = self.source | |
290 | self.reset() |
|
291 | self.reset() | |
291 | return out |
|
292 | return out | |
292 |
|
293 | |||
293 | def push(self, lines): |
|
294 | def push(self, lines): | |
294 | """Push one or more lines of input. |
|
295 | """Push one or more lines of input. | |
295 |
|
296 | |||
296 | This stores the given lines and returns a status code indicating |
|
297 | This stores the given lines and returns a status code indicating | |
297 | whether the code forms a complete Python block or not. |
|
298 | whether the code forms a complete Python block or not. | |
298 |
|
299 | |||
299 | Any exceptions generated in compilation are swallowed, but if an |
|
300 | Any exceptions generated in compilation are swallowed, but if an | |
300 | exception was produced, the method returns True. |
|
301 | exception was produced, the method returns True. | |
301 |
|
302 | |||
302 | Parameters |
|
303 | Parameters | |
303 | ---------- |
|
304 | ---------- | |
304 | lines : string |
|
305 | lines : string | |
305 | One or more lines of Python input. |
|
306 | One or more lines of Python input. | |
306 |
|
307 | |||
307 | Returns |
|
308 | Returns | |
308 | ------- |
|
309 | ------- | |
309 | is_complete : boolean |
|
310 | is_complete : boolean | |
310 | True if the current input source (the result of the current input |
|
311 | True if the current input source (the result of the current input | |
311 | plus prior inputs) forms a complete Python execution block. Note that |
|
312 | plus prior inputs) forms a complete Python execution block. Note that | |
312 | this value is also stored as a private attribute (_is_complete), so it |
|
313 | this value is also stored as a private attribute (_is_complete), so it | |
313 | can be queried at any time. |
|
314 | can be queried at any time. | |
314 | """ |
|
315 | """ | |
315 | if self.input_mode == 'cell': |
|
316 | if self.input_mode == 'cell': | |
316 | self.reset() |
|
317 | self.reset() | |
317 |
|
318 | |||
318 | self._store(lines) |
|
319 | self._store(lines) | |
319 | source = self.source |
|
320 | source = self.source | |
320 |
|
321 | |||
321 | # Before calling _compile(), reset the code object to None so that if an |
|
322 | # Before calling _compile(), reset the code object to None so that if an | |
322 | # exception is raised in compilation, we don't mislead by having |
|
323 | # exception is raised in compilation, we don't mislead by having | |
323 | # inconsistent code/source attributes. |
|
324 | # inconsistent code/source attributes. | |
324 | self.code, self._is_complete = None, None |
|
325 | self.code, self._is_complete = None, None | |
325 |
|
326 | |||
326 | # Honor termination lines properly |
|
327 | # Honor termination lines properly | |
327 | if source.rstrip().endswith('\\'): |
|
328 | if source.rstrip().endswith('\\'): | |
328 | return False |
|
329 | return False | |
329 |
|
330 | |||
330 | self._update_indent(lines) |
|
331 | self._update_indent(lines) | |
331 | try: |
|
332 | try: | |
332 | self.code = self._compile(source, symbol="exec") |
|
333 | self.code = self._compile(source, symbol="exec") | |
333 | # Invalid syntax can produce any of a number of different errors from |
|
334 | # Invalid syntax can produce any of a number of different errors from | |
334 | # inside the compiler, so we have to catch them all. Syntax errors |
|
335 | # inside the compiler, so we have to catch them all. Syntax errors | |
335 | # immediately produce a 'ready' block, so the invalid Python can be |
|
336 | # immediately produce a 'ready' block, so the invalid Python can be | |
336 | # sent to the kernel for evaluation with possible ipython |
|
337 | # sent to the kernel for evaluation with possible ipython | |
337 | # special-syntax conversion. |
|
338 | # special-syntax conversion. | |
338 | except (SyntaxError, OverflowError, ValueError, TypeError, |
|
339 | except (SyntaxError, OverflowError, ValueError, TypeError, | |
339 | MemoryError): |
|
340 | MemoryError): | |
340 | self._is_complete = True |
|
341 | self._is_complete = True | |
341 | else: |
|
342 | else: | |
342 | # Compilation didn't produce any exceptions (though it may not have |
|
343 | # Compilation didn't produce any exceptions (though it may not have | |
343 | # given a complete code object) |
|
344 | # given a complete code object) | |
344 | self._is_complete = self.code is not None |
|
345 | self._is_complete = self.code is not None | |
345 |
|
346 | |||
346 | return self._is_complete |
|
347 | return self._is_complete | |
347 |
|
348 | |||
348 | def push_accepts_more(self): |
|
349 | def push_accepts_more(self): | |
349 | """Return whether a block of interactive input can accept more input. |
|
350 | """Return whether a block of interactive input can accept more input. | |
350 |
|
351 | |||
351 | This method is meant to be used by line-oriented frontends, who need to |
|
352 | This method is meant to be used by line-oriented frontends, who need to | |
352 | guess whether a block is complete or not based solely on prior and |
|
353 | guess whether a block is complete or not based solely on prior and | |
353 | current input lines. The InputSplitter considers it has a complete |
|
354 | current input lines. The InputSplitter considers it has a complete | |
354 | interactive block and will not accept more input only when either a |
|
355 | interactive block and will not accept more input only when either a | |
355 | SyntaxError is raised, or *all* of the following are true: |
|
356 | SyntaxError is raised, or *all* of the following are true: | |
356 |
|
357 | |||
357 | 1. The input compiles to a complete statement. |
|
358 | 1. The input compiles to a complete statement. | |
358 |
|
359 | |||
359 | 2. The indentation level is flush-left (because if we are indented, |
|
360 | 2. The indentation level is flush-left (because if we are indented, | |
360 | like inside a function definition or for loop, we need to keep |
|
361 | like inside a function definition or for loop, we need to keep | |
361 | reading new input). |
|
362 | reading new input). | |
362 |
|
363 | |||
363 | 3. There is one extra line consisting only of whitespace. |
|
364 | 3. There is one extra line consisting only of whitespace. | |
364 |
|
365 | |||
365 | Because of condition #3, this method should be used only by |
|
366 | Because of condition #3, this method should be used only by | |
366 | *line-oriented* frontends, since it means that intermediate blank lines |
|
367 | *line-oriented* frontends, since it means that intermediate blank lines | |
367 | are not allowed in function definitions (or any other indented block). |
|
368 | are not allowed in function definitions (or any other indented block). | |
368 |
|
369 | |||
369 | If the current input produces a syntax error, this method immediately |
|
370 | If the current input produces a syntax error, this method immediately | |
370 | returns False but does *not* raise the syntax error exception, as |
|
371 | returns False but does *not* raise the syntax error exception, as | |
371 | typically clients will want to send invalid syntax to an execution |
|
372 | typically clients will want to send invalid syntax to an execution | |
372 | backend which might convert the invalid syntax into valid Python via |
|
373 | backend which might convert the invalid syntax into valid Python via | |
373 | one of the dynamic IPython mechanisms. |
|
374 | one of the dynamic IPython mechanisms. | |
374 | """ |
|
375 | """ | |
375 |
|
376 | |||
376 | # With incomplete input, unconditionally accept more |
|
377 | # With incomplete input, unconditionally accept more | |
377 | if not self._is_complete: |
|
378 | if not self._is_complete: | |
378 | return True |
|
379 | return True | |
379 |
|
380 | |||
380 | # If we already have complete input and we're flush left, the answer |
|
381 | # If we already have complete input and we're flush left, the answer | |
381 | # depends. In line mode, if there hasn't been any indentation, |
|
382 | # depends. In line mode, if there hasn't been any indentation, | |
382 | # that's it. If we've come back from some indentation, we need |
|
383 | # that's it. If we've come back from some indentation, we need | |
383 | # the blank final line to finish. |
|
384 | # the blank final line to finish. | |
384 | # In cell mode, we need to check how many blocks the input so far |
|
385 | # In cell mode, we need to check how many blocks the input so far | |
385 | # compiles into, because if there's already more than one full |
|
386 | # compiles into, because if there's already more than one full | |
386 | # independent block of input, then the client has entered full |
|
387 | # independent block of input, then the client has entered full | |
387 | # 'cell' mode and is feeding lines that each is complete. In this |
|
388 | # 'cell' mode and is feeding lines that each is complete. In this | |
388 | # case we should then keep accepting. The Qt terminal-like console |
|
389 | # case we should then keep accepting. The Qt terminal-like console | |
389 | # does precisely this, to provide the convenience of terminal-like |
|
390 | # does precisely this, to provide the convenience of terminal-like | |
390 | # input of single expressions, but allowing the user (with a |
|
391 | # input of single expressions, but allowing the user (with a | |
391 | # separate keystroke) to switch to 'cell' mode and type multiple |
|
392 | # separate keystroke) to switch to 'cell' mode and type multiple | |
392 | # expressions in one shot. |
|
393 | # expressions in one shot. | |
393 | if self.indent_spaces==0: |
|
394 | if self.indent_spaces==0: | |
394 | if self.input_mode=='line': |
|
395 | if self.input_mode=='line': | |
395 | if not self._full_dedent: |
|
396 | if not self._full_dedent: | |
396 | return False |
|
397 | return False | |
397 | else: |
|
398 | else: | |
398 | try: |
|
399 | try: | |
399 | code_ast = ast.parse(u''.join(self._buffer)) |
|
400 | code_ast = ast.parse(u''.join(self._buffer)) | |
400 | except Exception: |
|
401 | except Exception: | |
401 | return False |
|
402 | return False | |
402 | else: |
|
403 | else: | |
403 | if len(code_ast.body) == 1: |
|
404 | if len(code_ast.body) == 1: | |
404 | return False |
|
405 | return False | |
405 |
|
406 | |||
406 | # When input is complete, then termination is marked by an extra blank |
|
407 | # When input is complete, then termination is marked by an extra blank | |
407 | # line at the end. |
|
408 | # line at the end. | |
408 | last_line = self.source.splitlines()[-1] |
|
409 | last_line = self.source.splitlines()[-1] | |
409 | return bool(last_line and not last_line.isspace()) |
|
410 | return bool(last_line and not last_line.isspace()) | |
410 |
|
411 | |||
411 | #------------------------------------------------------------------------ |
|
412 | #------------------------------------------------------------------------ | |
412 | # Private interface |
|
413 | # Private interface | |
413 | #------------------------------------------------------------------------ |
|
414 | #------------------------------------------------------------------------ | |
414 |
|
415 | |||
415 | def _find_indent(self, line): |
|
416 | def _find_indent(self, line): | |
416 | """Compute the new indentation level for a single line. |
|
417 | """Compute the new indentation level for a single line. | |
417 |
|
418 | |||
418 | Parameters |
|
419 | Parameters | |
419 | ---------- |
|
420 | ---------- | |
420 | line : str |
|
421 | line : str | |
421 | A single new line of non-whitespace, non-comment Python input. |
|
422 | A single new line of non-whitespace, non-comment Python input. | |
422 |
|
423 | |||
423 | Returns |
|
424 | Returns | |
424 | ------- |
|
425 | ------- | |
425 | indent_spaces : int |
|
426 | indent_spaces : int | |
426 | New value for the indent level (it may be equal to self.indent_spaces |
|
427 | New value for the indent level (it may be equal to self.indent_spaces | |
427 | if indentation doesn't change. |
|
428 | if indentation doesn't change. | |
428 |
|
429 | |||
429 | full_dedent : boolean |
|
430 | full_dedent : boolean | |
430 | Whether the new line causes a full flush-left dedent. |
|
431 | Whether the new line causes a full flush-left dedent. | |
431 | """ |
|
432 | """ | |
432 | indent_spaces = self.indent_spaces |
|
433 | indent_spaces = self.indent_spaces | |
433 | full_dedent = self._full_dedent |
|
434 | full_dedent = self._full_dedent | |
434 |
|
435 | |||
435 | inisp = num_ini_spaces(line) |
|
436 | inisp = num_ini_spaces(line) | |
436 | if inisp < indent_spaces: |
|
437 | if inisp < indent_spaces: | |
437 | indent_spaces = inisp |
|
438 | indent_spaces = inisp | |
438 | if indent_spaces <= 0: |
|
439 | if indent_spaces <= 0: | |
439 | #print 'Full dedent in text',self.source # dbg |
|
440 | #print 'Full dedent in text',self.source # dbg | |
440 | full_dedent = True |
|
441 | full_dedent = True | |
441 |
|
442 | |||
442 | if line.rstrip()[-1] == ':': |
|
443 | if line.rstrip()[-1] == ':': | |
443 | indent_spaces += 4 |
|
444 | indent_spaces += 4 | |
444 | elif dedent_re.match(line): |
|
445 | elif dedent_re.match(line): | |
445 | indent_spaces -= 4 |
|
446 | indent_spaces -= 4 | |
446 | if indent_spaces <= 0: |
|
447 | if indent_spaces <= 0: | |
447 | full_dedent = True |
|
448 | full_dedent = True | |
448 |
|
449 | |||
449 | # Safety |
|
450 | # Safety | |
450 | if indent_spaces < 0: |
|
451 | if indent_spaces < 0: | |
451 | indent_spaces = 0 |
|
452 | indent_spaces = 0 | |
452 | #print 'safety' # dbg |
|
453 | #print 'safety' # dbg | |
453 |
|
454 | |||
454 | return indent_spaces, full_dedent |
|
455 | return indent_spaces, full_dedent | |
455 |
|
456 | |||
456 | def _update_indent(self, lines): |
|
457 | def _update_indent(self, lines): | |
457 | for line in remove_comments(lines).splitlines(): |
|
458 | for line in remove_comments(lines).splitlines(): | |
458 | if line and not line.isspace(): |
|
459 | if line and not line.isspace(): | |
459 | self.indent_spaces, self._full_dedent = self._find_indent(line) |
|
460 | self.indent_spaces, self._full_dedent = self._find_indent(line) | |
460 |
|
461 | |||
461 | def _store(self, lines, buffer=None, store='source'): |
|
462 | def _store(self, lines, buffer=None, store='source'): | |
462 | """Store one or more lines of input. |
|
463 | """Store one or more lines of input. | |
463 |
|
464 | |||
464 | If input lines are not newline-terminated, a newline is automatically |
|
465 | If input lines are not newline-terminated, a newline is automatically | |
465 | appended.""" |
|
466 | appended.""" | |
466 |
|
467 | |||
467 | if buffer is None: |
|
468 | if buffer is None: | |
468 | buffer = self._buffer |
|
469 | buffer = self._buffer | |
469 |
|
470 | |||
470 | if lines.endswith('\n'): |
|
471 | if lines.endswith('\n'): | |
471 | buffer.append(lines) |
|
472 | buffer.append(lines) | |
472 | else: |
|
473 | else: | |
473 | buffer.append(lines+'\n') |
|
474 | buffer.append(lines+'\n') | |
474 | setattr(self, store, self._set_source(buffer)) |
|
475 | setattr(self, store, self._set_source(buffer)) | |
475 |
|
476 | |||
476 | def _set_source(self, buffer): |
|
477 | def _set_source(self, buffer): | |
477 | return u''.join(buffer) |
|
478 | return u''.join(buffer) | |
478 |
|
479 | |||
479 |
|
480 | |||
480 | #----------------------------------------------------------------------------- |
|
481 | #----------------------------------------------------------------------------- | |
481 | # Functions and classes for IPython-specific syntactic support |
|
482 | # Functions and classes for IPython-specific syntactic support | |
482 | #----------------------------------------------------------------------------- |
|
483 | #----------------------------------------------------------------------------- | |
483 |
|
484 | |||
484 | # RegExp for splitting line contents into pre-char//first word-method//rest. |
|
485 | # RegExp for splitting line contents into pre-char//first word-method//rest. | |
485 | # For clarity, each group in on one line. |
|
486 | # For clarity, each group in on one line. | |
486 |
|
487 | |||
487 | line_split = re.compile(""" |
|
488 | line_split = re.compile(""" | |
488 | ^(\s*) # any leading space |
|
489 | ^(\s*) # any leading space | |
489 | ([,;/%]|!!?|\?\??) # escape character or characters |
|
490 | ([,;/%]|!!?|\?\??) # escape character or characters | |
490 | \s*(%?[\w\.\*]*) # function/method, possibly with leading % |
|
491 | \s*(%?[\w\.\*]*) # function/method, possibly with leading % | |
491 | # to correctly treat things like '?%magic' |
|
492 | # to correctly treat things like '?%magic' | |
492 | (\s+.*$|$) # rest of line |
|
493 | (\s+.*$|$) # rest of line | |
493 | """, re.VERBOSE) |
|
494 | """, re.VERBOSE) | |
494 |
|
495 | |||
495 |
|
496 | |||
496 | def split_user_input(line): |
|
497 | def split_user_input(line): | |
497 | """Split user input into early whitespace, esc-char, function part and rest. |
|
498 | """Split user input into early whitespace, esc-char, function part and rest. | |
498 |
|
499 | |||
499 | This is currently handles lines with '=' in them in a very inconsistent |
|
500 | This is currently handles lines with '=' in them in a very inconsistent | |
500 | manner. |
|
501 | manner. | |
501 |
|
502 | |||
502 | Examples |
|
503 | Examples | |
503 | ======== |
|
504 | ======== | |
504 | >>> split_user_input('x=1') |
|
505 | >>> split_user_input('x=1') | |
505 | ('', '', 'x=1', '') |
|
506 | ('', '', 'x=1', '') | |
506 | >>> split_user_input('?') |
|
507 | >>> split_user_input('?') | |
507 | ('', '?', '', '') |
|
508 | ('', '?', '', '') | |
508 | >>> split_user_input('??') |
|
509 | >>> split_user_input('??') | |
509 | ('', '??', '', '') |
|
510 | ('', '??', '', '') | |
510 | >>> split_user_input(' ?') |
|
511 | >>> split_user_input(' ?') | |
511 | (' ', '?', '', '') |
|
512 | (' ', '?', '', '') | |
512 | >>> split_user_input(' ??') |
|
513 | >>> split_user_input(' ??') | |
513 | (' ', '??', '', '') |
|
514 | (' ', '??', '', '') | |
514 | >>> split_user_input('??x') |
|
515 | >>> split_user_input('??x') | |
515 | ('', '??', 'x', '') |
|
516 | ('', '??', 'x', '') | |
516 | >>> split_user_input('?x=1') |
|
517 | >>> split_user_input('?x=1') | |
517 | ('', '', '?x=1', '') |
|
518 | ('', '', '?x=1', '') | |
518 | >>> split_user_input('!ls') |
|
519 | >>> split_user_input('!ls') | |
519 | ('', '!', 'ls', '') |
|
520 | ('', '!', 'ls', '') | |
520 | >>> split_user_input(' !ls') |
|
521 | >>> split_user_input(' !ls') | |
521 | (' ', '!', 'ls', '') |
|
522 | (' ', '!', 'ls', '') | |
522 | >>> split_user_input('!!ls') |
|
523 | >>> split_user_input('!!ls') | |
523 | ('', '!!', 'ls', '') |
|
524 | ('', '!!', 'ls', '') | |
524 | >>> split_user_input(' !!ls') |
|
525 | >>> split_user_input(' !!ls') | |
525 | (' ', '!!', 'ls', '') |
|
526 | (' ', '!!', 'ls', '') | |
526 | >>> split_user_input(',ls') |
|
527 | >>> split_user_input(',ls') | |
527 | ('', ',', 'ls', '') |
|
528 | ('', ',', 'ls', '') | |
528 | >>> split_user_input(';ls') |
|
529 | >>> split_user_input(';ls') | |
529 | ('', ';', 'ls', '') |
|
530 | ('', ';', 'ls', '') | |
530 | >>> split_user_input(' ;ls') |
|
531 | >>> split_user_input(' ;ls') | |
531 | (' ', ';', 'ls', '') |
|
532 | (' ', ';', 'ls', '') | |
532 | >>> split_user_input('f.g(x)') |
|
533 | >>> split_user_input('f.g(x)') | |
533 | ('', '', 'f.g(x)', '') |
|
534 | ('', '', 'f.g(x)', '') | |
534 | >>> split_user_input('f.g (x)') |
|
535 | >>> split_user_input('f.g (x)') | |
535 | ('', '', 'f.g', '(x)') |
|
536 | ('', '', 'f.g', '(x)') | |
536 | >>> split_user_input('?%hist') |
|
537 | >>> split_user_input('?%hist') | |
537 | ('', '?', '%hist', '') |
|
538 | ('', '?', '%hist', '') | |
538 | >>> split_user_input('?x*') |
|
539 | >>> split_user_input('?x*') | |
539 | ('', '?', 'x*', '') |
|
540 | ('', '?', 'x*', '') | |
540 | """ |
|
541 | """ | |
541 | match = line_split.match(line) |
|
542 | match = line_split.match(line) | |
542 | if match: |
|
543 | if match: | |
543 | lspace, esc, fpart, rest = match.groups() |
|
544 | lspace, esc, fpart, rest = match.groups() | |
544 | else: |
|
545 | else: | |
545 | # print "match failed for line '%s'" % line |
|
546 | # print "match failed for line '%s'" % line | |
546 | try: |
|
547 | try: | |
547 | fpart, rest = line.split(None, 1) |
|
548 | fpart, rest = line.split(None, 1) | |
548 | except ValueError: |
|
549 | except ValueError: | |
549 | # print "split failed for line '%s'" % line |
|
550 | # print "split failed for line '%s'" % line | |
550 | fpart, rest = line,'' |
|
551 | fpart, rest = line,'' | |
551 | lspace = re.match('^(\s*)(.*)', line).groups()[0] |
|
552 | lspace = re.match('^(\s*)(.*)', line).groups()[0] | |
552 | esc = '' |
|
553 | esc = '' | |
553 |
|
554 | |||
554 | # fpart has to be a valid python identifier, so it better be only pure |
|
555 | # fpart has to be a valid python identifier, so it better be only pure | |
555 | # ascii, no unicode: |
|
556 | # ascii, no unicode: | |
556 | try: |
|
557 | try: | |
557 | fpart = fpart.encode('ascii') |
|
558 | fpart = fpart.encode('ascii') | |
558 | except UnicodeEncodeError: |
|
559 | except UnicodeEncodeError: | |
559 | lspace = unicode(lspace) |
|
560 | lspace = unicode(lspace) | |
560 | rest = fpart + u' ' + rest |
|
561 | rest = fpart + u' ' + rest | |
561 | fpart = u'' |
|
562 | fpart = u'' | |
562 |
|
563 | |||
563 | #print 'line:<%s>' % line # dbg |
|
564 | #print 'line:<%s>' % line # dbg | |
564 | #print 'esc <%s> fpart <%s> rest <%s>' % (esc,fpart.strip(),rest) # dbg |
|
565 | #print 'esc <%s> fpart <%s> rest <%s>' % (esc,fpart.strip(),rest) # dbg | |
565 | return lspace, esc, fpart.strip(), rest.lstrip() |
|
566 | return lspace, esc, fpart.strip(), rest.lstrip() | |
566 |
|
567 | |||
567 |
|
568 | |||
568 | # The escaped translators ALL receive a line where their own escape has been |
|
569 | # The escaped translators ALL receive a line where their own escape has been | |
569 | # stripped. Only '?' is valid at the end of the line, all others can only be |
|
570 | # stripped. Only '?' is valid at the end of the line, all others can only be | |
570 | # placed at the start. |
|
571 | # placed at the start. | |
571 |
|
572 | |||
572 | class LineInfo(object): |
|
573 | class LineInfo(object): | |
573 | """A single line of input and associated info. |
|
574 | """A single line of input and associated info. | |
574 |
|
575 | |||
575 | This is a utility class that mostly wraps the output of |
|
576 | This is a utility class that mostly wraps the output of | |
576 | :func:`split_user_input` into a convenient object to be passed around |
|
577 | :func:`split_user_input` into a convenient object to be passed around | |
577 | during input transformations. |
|
578 | during input transformations. | |
578 |
|
579 | |||
579 | Includes the following as properties: |
|
580 | Includes the following as properties: | |
580 |
|
581 | |||
581 | line |
|
582 | line | |
582 | The original, raw line |
|
583 | The original, raw line | |
583 |
|
584 | |||
584 | lspace |
|
585 | lspace | |
585 | Any early whitespace before actual text starts. |
|
586 | Any early whitespace before actual text starts. | |
586 |
|
587 | |||
587 | esc |
|
588 | esc | |
588 | The initial esc character (or characters, for double-char escapes like |
|
589 | The initial esc character (or characters, for double-char escapes like | |
589 | '??' or '!!'). |
|
590 | '??' or '!!'). | |
590 |
|
591 | |||
591 | fpart |
|
592 | fpart | |
592 | The 'function part', which is basically the maximal initial sequence |
|
593 | The 'function part', which is basically the maximal initial sequence | |
593 | of valid python identifiers and the '.' character. This is what is |
|
594 | of valid python identifiers and the '.' character. This is what is | |
594 | checked for alias and magic transformations, used for auto-calling, |
|
595 | checked for alias and magic transformations, used for auto-calling, | |
595 | etc. |
|
596 | etc. | |
596 |
|
597 | |||
597 | rest |
|
598 | rest | |
598 | Everything else on the line. |
|
599 | Everything else on the line. | |
599 | """ |
|
600 | """ | |
600 | def __init__(self, line): |
|
601 | def __init__(self, line): | |
601 | self.line = line |
|
602 | self.line = line | |
602 | self.lspace, self.esc, self.fpart, self.rest = \ |
|
603 | self.lspace, self.esc, self.fpart, self.rest = \ | |
603 | split_user_input(line) |
|
604 | split_user_input(line) | |
604 |
|
605 | |||
605 | def __str__(self): |
|
606 | def __str__(self): | |
606 | return "LineInfo [%s|%s|%s|%s]" % (self.lspace, self.esc, |
|
607 | return "LineInfo [%s|%s|%s|%s]" % (self.lspace, self.esc, | |
607 | self.fpart, self.rest) |
|
608 | self.fpart, self.rest) | |
608 |
|
609 | |||
609 |
|
610 | |||
610 | # Transformations of the special syntaxes that don't rely on an explicit escape |
|
611 | # Transformations of the special syntaxes that don't rely on an explicit escape | |
611 | # character but instead on patterns on the input line |
|
612 | # character but instead on patterns on the input line | |
612 |
|
613 | |||
613 | # The core transformations are implemented as standalone functions that can be |
|
614 | # The core transformations are implemented as standalone functions that can be | |
614 | # tested and validated in isolation. Each of these uses a regexp, we |
|
615 | # tested and validated in isolation. Each of these uses a regexp, we | |
615 | # pre-compile these and keep them close to each function definition for clarity |
|
616 | # pre-compile these and keep them close to each function definition for clarity | |
616 |
|
617 | |||
617 | _assign_system_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' |
|
618 | _assign_system_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' | |
618 | r'\s*=\s*!\s*(?P<cmd>.*)') |
|
619 | r'\s*=\s*!\s*(?P<cmd>.*)') | |
619 |
|
620 | |||
620 | def transform_assign_system(line): |
|
621 | def transform_assign_system(line): | |
621 | """Handle the `files = !ls` syntax.""" |
|
622 | """Handle the `files = !ls` syntax.""" | |
622 | m = _assign_system_re.match(line) |
|
623 | m = _assign_system_re.match(line) | |
623 | if m is not None: |
|
624 | if m is not None: | |
624 | cmd = m.group('cmd') |
|
625 | cmd = m.group('cmd') | |
625 | lhs = m.group('lhs') |
|
626 | lhs = m.group('lhs') | |
626 | expr = make_quoted_expr(cmd) |
|
627 | expr = make_quoted_expr(cmd) | |
627 | new_line = '%s = get_ipython().getoutput(%s)' % (lhs, expr) |
|
628 | new_line = '%s = get_ipython().getoutput(%s)' % (lhs, expr) | |
628 | return new_line |
|
629 | return new_line | |
629 | return line |
|
630 | return line | |
630 |
|
631 | |||
631 |
|
632 | |||
632 | _assign_magic_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' |
|
633 | _assign_magic_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' | |
633 | r'\s*=\s*%\s*(?P<cmd>.*)') |
|
634 | r'\s*=\s*%\s*(?P<cmd>.*)') | |
634 |
|
635 | |||
635 | def transform_assign_magic(line): |
|
636 | def transform_assign_magic(line): | |
636 | """Handle the `a = %who` syntax.""" |
|
637 | """Handle the `a = %who` syntax.""" | |
637 | m = _assign_magic_re.match(line) |
|
638 | m = _assign_magic_re.match(line) | |
638 | if m is not None: |
|
639 | if m is not None: | |
639 | cmd = m.group('cmd') |
|
640 | cmd = m.group('cmd') | |
640 | lhs = m.group('lhs') |
|
641 | lhs = m.group('lhs') | |
641 | expr = make_quoted_expr(cmd) |
|
642 | expr = make_quoted_expr(cmd) | |
642 | new_line = '%s = get_ipython().magic(%s)' % (lhs, expr) |
|
643 | new_line = '%s = get_ipython().magic(%s)' % (lhs, expr) | |
643 | return new_line |
|
644 | return new_line | |
644 | return line |
|
645 | return line | |
645 |
|
646 | |||
646 |
|
647 | |||
647 | _classic_prompt_re = re.compile(r'^([ \t]*>>> |^[ \t]*\.\.\. )') |
|
648 | _classic_prompt_re = re.compile(r'^([ \t]*>>> |^[ \t]*\.\.\. )') | |
648 |
|
649 | |||
649 | def transform_classic_prompt(line): |
|
650 | def transform_classic_prompt(line): | |
650 | """Handle inputs that start with '>>> ' syntax.""" |
|
651 | """Handle inputs that start with '>>> ' syntax.""" | |
651 |
|
652 | |||
652 | if not line or line.isspace(): |
|
653 | if not line or line.isspace(): | |
653 | return line |
|
654 | return line | |
654 | m = _classic_prompt_re.match(line) |
|
655 | m = _classic_prompt_re.match(line) | |
655 | if m: |
|
656 | if m: | |
656 | return line[len(m.group(0)):] |
|
657 | return line[len(m.group(0)):] | |
657 | else: |
|
658 | else: | |
658 | return line |
|
659 | return line | |
659 |
|
660 | |||
660 |
|
661 | |||
661 | _ipy_prompt_re = re.compile(r'^([ \t]*In \[\d+\]: |^[ \t]*\ \ \ \.\.\.+: )') |
|
662 | _ipy_prompt_re = re.compile(r'^([ \t]*In \[\d+\]: |^[ \t]*\ \ \ \.\.\.+: )') | |
662 |
|
663 | |||
663 | def transform_ipy_prompt(line): |
|
664 | def transform_ipy_prompt(line): | |
664 | """Handle inputs that start classic IPython prompt syntax.""" |
|
665 | """Handle inputs that start classic IPython prompt syntax.""" | |
665 |
|
666 | |||
666 | if not line or line.isspace(): |
|
667 | if not line or line.isspace(): | |
667 | return line |
|
668 | return line | |
668 | #print 'LINE: %r' % line # dbg |
|
669 | #print 'LINE: %r' % line # dbg | |
669 | m = _ipy_prompt_re.match(line) |
|
670 | m = _ipy_prompt_re.match(line) | |
670 | if m: |
|
671 | if m: | |
671 | #print 'MATCH! %r -> %r' % (line, line[len(m.group(0)):]) # dbg |
|
672 | #print 'MATCH! %r -> %r' % (line, line[len(m.group(0)):]) # dbg | |
672 | return line[len(m.group(0)):] |
|
673 | return line[len(m.group(0)):] | |
673 | else: |
|
674 | else: | |
674 | return line |
|
675 | return line | |
675 |
|
676 | |||
676 |
|
677 | |||
677 | def _make_help_call(target, esc, lspace, next_input=None): |
|
678 | def _make_help_call(target, esc, lspace, next_input=None): | |
678 | """Prepares a pinfo(2)/psearch call from a target name and the escape |
|
679 | """Prepares a pinfo(2)/psearch call from a target name and the escape | |
679 | (i.e. ? or ??)""" |
|
680 | (i.e. ? or ??)""" | |
680 | method = 'pinfo2' if esc == '??' \ |
|
681 | method = 'pinfo2' if esc == '??' \ | |
681 | else 'psearch' if '*' in target \ |
|
682 | else 'psearch' if '*' in target \ | |
682 | else 'pinfo' |
|
683 | else 'pinfo' | |
683 |
|
684 | |||
684 | if next_input: |
|
685 | if next_input: | |
685 | tpl = '%sget_ipython().magic(u"%s %s", next_input=%s)' |
|
686 | tpl = '%sget_ipython().magic(u"%s %s", next_input=%s)' | |
686 | return tpl % (lspace, method, target, make_quoted_expr(next_input)) |
|
687 | return tpl % (lspace, method, target, make_quoted_expr(next_input)) | |
687 | else: |
|
688 | else: | |
688 | return '%sget_ipython().magic(u"%s %s")' % (lspace, method, target) |
|
689 | return '%sget_ipython().magic(u"%s %s")' % (lspace, method, target) | |
689 |
|
690 | |||
690 | _initial_space_re = re.compile(r'\s*') |
|
691 | _initial_space_re = re.compile(r'\s*') | |
691 | _help_end_re = re.compile(r"""(%? |
|
692 | _help_end_re = re.compile(r"""(%? | |
692 | [a-zA-Z_*][a-zA-Z0-9_*]* # Variable name |
|
693 | [a-zA-Z_*][a-zA-Z0-9_*]* # Variable name | |
693 | (\.[a-zA-Z_*][a-zA-Z0-9_*]*)* # .etc.etc |
|
694 | (\.[a-zA-Z_*][a-zA-Z0-9_*]*)* # .etc.etc | |
694 | ) |
|
695 | ) | |
695 | (\?\??)$ # ? or ??""", |
|
696 | (\?\??)$ # ? or ??""", | |
696 | re.VERBOSE) |
|
697 | re.VERBOSE) | |
697 | def transform_help_end(line): |
|
698 | def transform_help_end(line): | |
698 | """Translate lines with ?/?? at the end""" |
|
699 | """Translate lines with ?/?? at the end""" | |
699 | m = _help_end_re.search(line) |
|
700 | m = _help_end_re.search(line) | |
700 | if m is None or has_comment(line): |
|
701 | if m is None or has_comment(line): | |
701 | return line |
|
702 | return line | |
702 | target = m.group(1) |
|
703 | target = m.group(1) | |
703 | esc = m.group(3) |
|
704 | esc = m.group(3) | |
704 | lspace = _initial_space_re.match(line).group(0) |
|
705 | lspace = _initial_space_re.match(line).group(0) | |
705 | newline = _make_help_call(target, esc, lspace) |
|
706 | newline = _make_help_call(target, esc, lspace) | |
706 |
|
707 | |||
707 | # If we're mid-command, put it back on the next prompt for the user. |
|
708 | # If we're mid-command, put it back on the next prompt for the user. | |
708 | next_input = line.rstrip('?') if line.strip() != m.group(0) else None |
|
709 | next_input = line.rstrip('?') if line.strip() != m.group(0) else None | |
709 |
|
710 | |||
710 | return _make_help_call(target, esc, lspace, next_input) |
|
711 | return _make_help_call(target, esc, lspace, next_input) | |
711 |
|
712 | |||
712 |
|
713 | |||
713 | class EscapedTransformer(object): |
|
714 | class EscapedTransformer(object): | |
714 | """Class to transform lines that are explicitly escaped out.""" |
|
715 | """Class to transform lines that are explicitly escaped out.""" | |
715 |
|
716 | |||
716 | def __init__(self): |
|
717 | def __init__(self): | |
717 | tr = { ESC_SHELL : self._tr_system, |
|
718 | tr = { ESC_SHELL : self._tr_system, | |
718 | ESC_SH_CAP : self._tr_system2, |
|
719 | ESC_SH_CAP : self._tr_system2, | |
719 | ESC_HELP : self._tr_help, |
|
720 | ESC_HELP : self._tr_help, | |
720 | ESC_HELP2 : self._tr_help, |
|
721 | ESC_HELP2 : self._tr_help, | |
721 | ESC_MAGIC : self._tr_magic, |
|
722 | ESC_MAGIC : self._tr_magic, | |
722 | ESC_QUOTE : self._tr_quote, |
|
723 | ESC_QUOTE : self._tr_quote, | |
723 | ESC_QUOTE2 : self._tr_quote2, |
|
724 | ESC_QUOTE2 : self._tr_quote2, | |
724 | ESC_PAREN : self._tr_paren } |
|
725 | ESC_PAREN : self._tr_paren } | |
725 | self.tr = tr |
|
726 | self.tr = tr | |
726 |
|
727 | |||
727 | # Support for syntax transformations that use explicit escapes typed by the |
|
728 | # Support for syntax transformations that use explicit escapes typed by the | |
728 | # user at the beginning of a line |
|
729 | # user at the beginning of a line | |
729 | @staticmethod |
|
730 | @staticmethod | |
730 | def _tr_system(line_info): |
|
731 | def _tr_system(line_info): | |
731 | "Translate lines escaped with: !" |
|
732 | "Translate lines escaped with: !" | |
732 | cmd = line_info.line.lstrip().lstrip(ESC_SHELL) |
|
733 | cmd = line_info.line.lstrip().lstrip(ESC_SHELL) | |
733 | return '%sget_ipython().system(%s)' % (line_info.lspace, |
|
734 | return '%sget_ipython().system(%s)' % (line_info.lspace, | |
734 | make_quoted_expr(cmd)) |
|
735 | make_quoted_expr(cmd)) | |
735 |
|
736 | |||
736 | @staticmethod |
|
737 | @staticmethod | |
737 | def _tr_system2(line_info): |
|
738 | def _tr_system2(line_info): | |
738 | "Translate lines escaped with: !!" |
|
739 | "Translate lines escaped with: !!" | |
739 | cmd = line_info.line.lstrip()[2:] |
|
740 | cmd = line_info.line.lstrip()[2:] | |
740 | return '%sget_ipython().getoutput(%s)' % (line_info.lspace, |
|
741 | return '%sget_ipython().getoutput(%s)' % (line_info.lspace, | |
741 | make_quoted_expr(cmd)) |
|
742 | make_quoted_expr(cmd)) | |
742 |
|
743 | |||
743 | @staticmethod |
|
744 | @staticmethod | |
744 | def _tr_help(line_info): |
|
745 | def _tr_help(line_info): | |
745 | "Translate lines escaped with: ?/??" |
|
746 | "Translate lines escaped with: ?/??" | |
746 | # A naked help line should just fire the intro help screen |
|
747 | # A naked help line should just fire the intro help screen | |
747 | if not line_info.line[1:]: |
|
748 | if not line_info.line[1:]: | |
748 | return 'get_ipython().show_usage()' |
|
749 | return 'get_ipython().show_usage()' | |
749 |
|
750 | |||
750 | return _make_help_call(line_info.fpart, line_info.esc, line_info.lspace) |
|
751 | return _make_help_call(line_info.fpart, line_info.esc, line_info.lspace) | |
751 |
|
752 | |||
752 | @staticmethod |
|
753 | @staticmethod | |
753 | def _tr_magic(line_info): |
|
754 | def _tr_magic(line_info): | |
754 | "Translate lines escaped with: %" |
|
755 | "Translate lines escaped with: %" | |
755 | tpl = '%sget_ipython().magic(%s)' |
|
756 | tpl = '%sget_ipython().magic(%s)' | |
756 | cmd = make_quoted_expr(' '.join([line_info.fpart, |
|
757 | cmd = make_quoted_expr(' '.join([line_info.fpart, | |
757 | line_info.rest]).strip()) |
|
758 | line_info.rest]).strip()) | |
758 | return tpl % (line_info.lspace, cmd) |
|
759 | return tpl % (line_info.lspace, cmd) | |
759 |
|
760 | |||
760 | @staticmethod |
|
761 | @staticmethod | |
761 | def _tr_quote(line_info): |
|
762 | def _tr_quote(line_info): | |
762 | "Translate lines escaped with: ," |
|
763 | "Translate lines escaped with: ," | |
763 | return '%s%s("%s")' % (line_info.lspace, line_info.fpart, |
|
764 | return '%s%s("%s")' % (line_info.lspace, line_info.fpart, | |
764 | '", "'.join(line_info.rest.split()) ) |
|
765 | '", "'.join(line_info.rest.split()) ) | |
765 |
|
766 | |||
766 | @staticmethod |
|
767 | @staticmethod | |
767 | def _tr_quote2(line_info): |
|
768 | def _tr_quote2(line_info): | |
768 | "Translate lines escaped with: ;" |
|
769 | "Translate lines escaped with: ;" | |
769 | return '%s%s("%s")' % (line_info.lspace, line_info.fpart, |
|
770 | return '%s%s("%s")' % (line_info.lspace, line_info.fpart, | |
770 | line_info.rest) |
|
771 | line_info.rest) | |
771 |
|
772 | |||
772 | @staticmethod |
|
773 | @staticmethod | |
773 | def _tr_paren(line_info): |
|
774 | def _tr_paren(line_info): | |
774 | "Translate lines escaped with: /" |
|
775 | "Translate lines escaped with: /" | |
775 | return '%s%s(%s)' % (line_info.lspace, line_info.fpart, |
|
776 | return '%s%s(%s)' % (line_info.lspace, line_info.fpart, | |
776 | ", ".join(line_info.rest.split())) |
|
777 | ", ".join(line_info.rest.split())) | |
777 |
|
778 | |||
778 | def __call__(self, line): |
|
779 | def __call__(self, line): | |
779 | """Class to transform lines that are explicitly escaped out. |
|
780 | """Class to transform lines that are explicitly escaped out. | |
780 |
|
781 | |||
781 | This calls the above _tr_* static methods for the actual line |
|
782 | This calls the above _tr_* static methods for the actual line | |
782 | translations.""" |
|
783 | translations.""" | |
783 |
|
784 | |||
784 | # Empty lines just get returned unmodified |
|
785 | # Empty lines just get returned unmodified | |
785 | if not line or line.isspace(): |
|
786 | if not line or line.isspace(): | |
786 | return line |
|
787 | return line | |
787 |
|
788 | |||
788 | # Get line endpoints, where the escapes can be |
|
789 | # Get line endpoints, where the escapes can be | |
789 | line_info = LineInfo(line) |
|
790 | line_info = LineInfo(line) | |
790 |
|
791 | |||
791 | if not line_info.esc in self.tr: |
|
792 | if not line_info.esc in self.tr: | |
792 | # If we don't recognize the escape, don't modify the line |
|
793 | # If we don't recognize the escape, don't modify the line | |
793 | return line |
|
794 | return line | |
794 |
|
795 | |||
795 | return self.tr[line_info.esc](line_info) |
|
796 | return self.tr[line_info.esc](line_info) | |
796 |
|
797 | |||
797 |
|
798 | |||
798 | # A function-looking object to be used by the rest of the code. The purpose of |
|
799 | # A function-looking object to be used by the rest of the code. The purpose of | |
799 | # the class in this case is to organize related functionality, more than to |
|
800 | # the class in this case is to organize related functionality, more than to | |
800 | # manage state. |
|
801 | # manage state. | |
801 | transform_escaped = EscapedTransformer() |
|
802 | transform_escaped = EscapedTransformer() | |
802 |
|
803 | |||
803 |
|
804 | |||
804 | class IPythonInputSplitter(InputSplitter): |
|
805 | class IPythonInputSplitter(InputSplitter): | |
805 | """An input splitter that recognizes all of IPython's special syntax.""" |
|
806 | """An input splitter that recognizes all of IPython's special syntax.""" | |
806 |
|
807 | |||
807 | # String with raw, untransformed input. |
|
808 | # String with raw, untransformed input. | |
808 | source_raw = '' |
|
809 | source_raw = '' | |
809 |
|
810 | |||
810 | # Private attributes |
|
811 | # Private attributes | |
811 |
|
812 | |||
812 | # List with lines of raw input accumulated so far. |
|
813 | # List with lines of raw input accumulated so far. | |
813 | _buffer_raw = None |
|
814 | _buffer_raw = None | |
814 |
|
815 | |||
815 | def __init__(self, input_mode=None): |
|
816 | def __init__(self, input_mode=None): | |
816 | InputSplitter.__init__(self, input_mode) |
|
817 | InputSplitter.__init__(self, input_mode) | |
817 | self._buffer_raw = [] |
|
818 | self._buffer_raw = [] | |
818 |
|
819 | |||
819 | def reset(self): |
|
820 | def reset(self): | |
820 | """Reset the input buffer and associated state.""" |
|
821 | """Reset the input buffer and associated state.""" | |
821 | InputSplitter.reset(self) |
|
822 | InputSplitter.reset(self) | |
822 | self._buffer_raw[:] = [] |
|
823 | self._buffer_raw[:] = [] | |
823 | self.source_raw = '' |
|
824 | self.source_raw = '' | |
824 |
|
825 | |||
825 | def source_raw_reset(self): |
|
826 | def source_raw_reset(self): | |
826 | """Return input and raw source and perform a full reset. |
|
827 | """Return input and raw source and perform a full reset. | |
827 | """ |
|
828 | """ | |
828 | out = self.source |
|
829 | out = self.source | |
829 | out_r = self.source_raw |
|
830 | out_r = self.source_raw | |
830 | self.reset() |
|
831 | self.reset() | |
831 | return out, out_r |
|
832 | return out, out_r | |
832 |
|
833 | |||
833 | def push(self, lines): |
|
834 | def push(self, lines): | |
834 | """Push one or more lines of IPython input. |
|
835 | """Push one or more lines of IPython input. | |
835 | """ |
|
836 | """ | |
836 | if not lines: |
|
837 | if not lines: | |
837 | return super(IPythonInputSplitter, self).push(lines) |
|
838 | return super(IPythonInputSplitter, self).push(lines) | |
838 |
|
839 | |||
839 | # We must ensure all input is pure unicode |
|
840 | # We must ensure all input is pure unicode | |
840 | if type(lines)==str: |
|
841 | lines = cast_unicode(lines, self.encoding) | |
841 | lines = lines.decode(self.encoding) |
|
|||
842 |
|
842 | |||
843 | lines_list = lines.splitlines() |
|
843 | lines_list = lines.splitlines() | |
844 |
|
844 | |||
845 | transforms = [transform_ipy_prompt, transform_classic_prompt, |
|
845 | transforms = [transform_ipy_prompt, transform_classic_prompt, | |
846 | transform_escaped, transform_help_end, |
|
846 | transform_escaped, transform_help_end, | |
847 | transform_assign_system, transform_assign_magic] |
|
847 | transform_assign_system, transform_assign_magic] | |
848 |
|
848 | |||
849 | # Transform logic |
|
849 | # Transform logic | |
850 | # |
|
850 | # | |
851 | # We only apply the line transformers to the input if we have either no |
|
851 | # We only apply the line transformers to the input if we have either no | |
852 | # input yet, or complete input, or if the last line of the buffer ends |
|
852 | # input yet, or complete input, or if the last line of the buffer ends | |
853 | # with ':' (opening an indented block). This prevents the accidental |
|
853 | # with ':' (opening an indented block). This prevents the accidental | |
854 | # transformation of escapes inside multiline expressions like |
|
854 | # transformation of escapes inside multiline expressions like | |
855 | # triple-quoted strings or parenthesized expressions. |
|
855 | # triple-quoted strings or parenthesized expressions. | |
856 | # |
|
856 | # | |
857 | # The last heuristic, while ugly, ensures that the first line of an |
|
857 | # The last heuristic, while ugly, ensures that the first line of an | |
858 | # indented block is correctly transformed. |
|
858 | # indented block is correctly transformed. | |
859 | # |
|
859 | # | |
860 | # FIXME: try to find a cleaner approach for this last bit. |
|
860 | # FIXME: try to find a cleaner approach for this last bit. | |
861 |
|
861 | |||
862 | # If we were in 'block' mode, since we're going to pump the parent |
|
862 | # If we were in 'block' mode, since we're going to pump the parent | |
863 | # class by hand line by line, we need to temporarily switch out to |
|
863 | # class by hand line by line, we need to temporarily switch out to | |
864 | # 'line' mode, do a single manual reset and then feed the lines one |
|
864 | # 'line' mode, do a single manual reset and then feed the lines one | |
865 | # by one. Note that this only matters if the input has more than one |
|
865 | # by one. Note that this only matters if the input has more than one | |
866 | # line. |
|
866 | # line. | |
867 | changed_input_mode = False |
|
867 | changed_input_mode = False | |
868 |
|
868 | |||
869 | if self.input_mode == 'cell': |
|
869 | if self.input_mode == 'cell': | |
870 | self.reset() |
|
870 | self.reset() | |
871 | changed_input_mode = True |
|
871 | changed_input_mode = True | |
872 | saved_input_mode = 'cell' |
|
872 | saved_input_mode = 'cell' | |
873 | self.input_mode = 'line' |
|
873 | self.input_mode = 'line' | |
874 |
|
874 | |||
875 | # Store raw source before applying any transformations to it. Note |
|
875 | # Store raw source before applying any transformations to it. Note | |
876 | # that this must be done *after* the reset() call that would otherwise |
|
876 | # that this must be done *after* the reset() call that would otherwise | |
877 | # flush the buffer. |
|
877 | # flush the buffer. | |
878 | self._store(lines, self._buffer_raw, 'source_raw') |
|
878 | self._store(lines, self._buffer_raw, 'source_raw') | |
879 |
|
879 | |||
880 | try: |
|
880 | try: | |
881 | push = super(IPythonInputSplitter, self).push |
|
881 | push = super(IPythonInputSplitter, self).push | |
882 | for line in lines_list: |
|
882 | for line in lines_list: | |
883 | if self._is_complete or not self._buffer or \ |
|
883 | if self._is_complete or not self._buffer or \ | |
884 | (self._buffer and self._buffer[-1].rstrip().endswith(':')): |
|
884 | (self._buffer and self._buffer[-1].rstrip().endswith(':')): | |
885 | for f in transforms: |
|
885 | for f in transforms: | |
886 | line = f(line) |
|
886 | line = f(line) | |
887 |
|
887 | |||
888 | out = push(line) |
|
888 | out = push(line) | |
889 | finally: |
|
889 | finally: | |
890 | if changed_input_mode: |
|
890 | if changed_input_mode: | |
891 | self.input_mode = saved_input_mode |
|
891 | self.input_mode = saved_input_mode | |
892 | return out |
|
892 | return out |
General Comments 0
You need to be logged in to leave comments.
Login now