##// END OF EJS Templates
Lots of work on the display system, focused on pylab stuff....
Brian Granger -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,146 +1,150 b''
1 1 # Get the config being loaded so we can set attributes on it
2 2 c = get_config()
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Global options
6 6 #-----------------------------------------------------------------------------
7 7
8 8 # c.Global.display_banner = True
9 9
10 10 # c.Global.classic = False
11 11
12 12 # c.Global.nosep = True
13 13
14 14 # Set this to determine the detail of what is logged at startup.
15 15 # The default is 30 and possible values are 0,10,20,30,40,50.
16 16 # c.Global.log_level = 20
17 17
18 18 # This should be a list of importable Python modules that have an
19 19 # load_in_ipython(ip) method. This method gets called when the extension
20 20 # is loaded. You can put your extensions anywhere they can be imported
21 21 # but we add the extensions subdir of the ipython directory to sys.path
22 22 # during extension loading, so you can put them there as well.
23 23 # c.Global.extensions = [
24 24 # 'myextension'
25 25 # ]
26 26
27 27 # These lines are run in IPython in the user's namespace after extensions
28 28 # are loaded. They can contain full IPython syntax with magics etc.
29 29 # c.Global.exec_lines = [
30 30 # 'import numpy',
31 31 # 'a = 10; b = 20',
32 32 # '1/0'
33 33 # ]
34 34
35 35 # These files are run in IPython in the user's namespace. Files with a .py
36 36 # extension need to be pure Python. Files with a .ipy extension can have
37 37 # custom IPython syntax (like magics, etc.).
38 38 # These files need to be in the cwd, the ipython_dir or be absolute paths.
39 39 # c.Global.exec_files = [
40 40 # 'mycode.py',
41 41 # 'fancy.ipy'
42 42 # ]
43 43
44 44 #-----------------------------------------------------------------------------
45 45 # InteractiveShell options
46 46 #-----------------------------------------------------------------------------
47 47
48 48 # c.InteractiveShell.autocall = 1
49 49
50 50 # c.TerminalInteractiveShell.autoedit_syntax = False
51 51
52 52 # c.InteractiveShell.autoindent = True
53 53
54 54 # c.InteractiveShell.automagic = False
55 55
56 56 # c.TerminalTerminalInteractiveShell.banner1 = 'This if for overriding the default IPython banner'
57 57
58 58 # c.TerminalTerminalInteractiveShell.banner2 = "This is for extra banner text"
59 59
60 60 # c.InteractiveShell.cache_size = 1000
61 61
62 62 # c.InteractiveShell.colors = 'LightBG'
63 63
64 64 # c.InteractiveShell.color_info = True
65 65
66 66 # c.TerminalInteractiveShell.confirm_exit = True
67 67
68 68 # c.InteractiveShell.deep_reload = False
69 69
70 70 # c.TerminalInteractiveShell.editor = 'nano'
71 71
72 72 # c.InteractiveShell.logstart = True
73 73
74 74 # c.InteractiveShell.logfile = u'ipython_log.py'
75 75
76 76 # c.InteractiveShell.logappend = u'mylog.py'
77 77
78 78 # c.InteractiveShell.object_info_string_level = 0
79 79
80 80 # c.TerminalInteractiveShell.pager = 'less'
81 81
82 82 # c.InteractiveShell.pdb = False
83 83
84 # c.InteractiveShell.pprint = True
85
86 84 # c.InteractiveShell.prompt_in1 = 'In [\#]: '
87 85 # c.InteractiveShell.prompt_in2 = ' .\D.: '
88 86 # c.InteractiveShell.prompt_out = 'Out[\#]: '
89 87 # c.InteractiveShell.prompts_pad_left = True
90 88
91 89 # c.InteractiveShell.quiet = False
92 90
93 91 # c.InteractiveShell.history_length = 10000
94 92
95 93 # Readline
96 94 # c.InteractiveShell.readline_use = True
97 95
98 96 # c.InteractiveShell.readline_parse_and_bind = [
99 97 # 'tab: complete',
100 98 # '"\C-l": possible-completions',
101 99 # 'set show-all-if-ambiguous on',
102 100 # '"\C-o": tab-insert',
103 101 # '"\M-i": " "',
104 102 # '"\M-o": "\d\d\d\d"',
105 103 # '"\M-I": "\d\d\d\d"',
106 104 # '"\C-r": reverse-search-history',
107 105 # '"\C-s": forward-search-history',
108 106 # '"\C-p": history-search-backward',
109 107 # '"\C-n": history-search-forward',
110 108 # '"\e[A": history-search-backward',
111 109 # '"\e[B": history-search-forward',
112 110 # '"\C-k": kill-line',
113 111 # '"\C-u": unix-line-discard',
114 112 # ]
115 113 # c.InteractiveShell.readline_remove_delims = '-/~'
116 114 # c.InteractiveShell.readline_merge_completions = True
117 115 # c.InteractiveShell.readline_omit__names = 0
118 116
119 117 # c.TerminalInteractiveShell.screen_length = 0
120 118
121 119 # c.InteractiveShell.separate_in = '\n'
122 120 # c.InteractiveShell.separate_out = ''
123 121 # c.InteractiveShell.separate_out2 = ''
124 122
125 123 # c.TerminalInteractiveShell.term_title = False
126 124
127 125 # c.InteractiveShell.wildcards_case_sensitive = True
128 126
129 127 # c.InteractiveShell.xmode = 'Context'
130 128
131 129 #-----------------------------------------------------------------------------
130 # Formatter and display options
131 #-----------------------------------------------------------------------------
132
133 # c.PlainTextFormatter.pprint = True
134
135 #-----------------------------------------------------------------------------
132 136 # PrefilterManager options
133 137 #-----------------------------------------------------------------------------
134 138
135 139 # c.PrefilterManager.multi_line_specials = True
136 140
137 141 #-----------------------------------------------------------------------------
138 142 # AliasManager options
139 143 #-----------------------------------------------------------------------------
140 144
141 145 # Do this to disable all defaults
142 146 # c.AliasManager.default_aliases = []
143 147
144 148 # c.AliasManager.user_aliases = [
145 149 # ('foo', 'echo Hi')
146 150 # ]
@@ -1,474 +1,503 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Display formatters.
3 3
4 4
5 5 Authors:
6 6
7 7 * Robert Kern
8 8 * Brian Granger
9 9 """
10 10 #-----------------------------------------------------------------------------
11 11 # Copyright (c) 2010, IPython Development Team.
12 12 #
13 13 # Distributed under the terms of the Modified BSD License.
14 14 #
15 15 # The full license is in the file COPYING.txt, distributed with this software.
16 16 #-----------------------------------------------------------------------------
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Imports
20 20 #-----------------------------------------------------------------------------
21 21
22 22 # Stdlib imports
23 23 import abc
24 24 # We must use StringIO, as cStringIO doesn't handle unicode properly.
25 25 from StringIO import StringIO
26 26
27 27 # Our own imports
28 28 from IPython.config.configurable import Configurable
29 29 from IPython.external import pretty
30 30 from IPython.utils.traitlets import Bool, Dict, Int, Str
31 31
32 32
33 33 #-----------------------------------------------------------------------------
34 34 # The main DisplayFormatter class
35 35 #-----------------------------------------------------------------------------
36 36
37 37
38 38 class DisplayFormatter(Configurable):
39 39
40 # When set to true only the default plain text formatter will be used.
41 plain_text_only = Bool(False, config=True)
42
40 43 # A dict of formatter whose keys are format types (MIME types) and whose
41 44 # values are subclasses of BaseFormatter.
42 45 formatters = Dict(config=True)
43 46 def _formatters_default(self):
44 47 """Activate the default formatters."""
45 48 formatter_classes = [
46 49 PlainTextFormatter,
47 50 HTMLFormatter,
48 51 SVGFormatter,
49 52 PNGFormatter,
50 53 LatexFormatter,
51 54 JSONFormatter
52 55 ]
53 56 d = {}
54 57 for cls in formatter_classes:
55 58 f = cls(config=self.config)
56 59 d[f.format_type] = f
57 60 return d
58 61
59 62 def format(self, obj, include=None, exclude=None):
60 63 """Return a format data dict for an object.
61 64
62 65 By default all format types will be computed.
63 66
64 67 The following MIME types are currently implemented:
65 68
66 69 * text/plain
67 70 * text/html
68 71 * text/latex
69 72 * application/json
70 73 * image/png
71 74 * immage/svg+xml
72 75
73 76 Parameters
74 77 ----------
75 78 obj : object
76 79 The Python object whose format data will be computed.
77 80
78 81 Returns
79 82 -------
80 83 format_dict : dict
81 84 A dictionary of key/value pairs, one or each format that was
82 85 generated for the object. The keys are the format types, which
83 86 will usually be MIME type strings and the values and JSON'able
84 87 data structure containing the raw data for the representation in
85 88 that format.
86 89 include : list or tuple, optional
87 90 A list of format type strings (MIME types) to include in the
88 91 format data dict. If this is set *only* the format types included
89 92 in this list will be computed.
90 93 exclude : list or tuple, optional
91 94 A list of format type string (MIME types) to exclue in the format
92 95 data dict. If this is set all format types will be computed,
93 96 except for those included in this argument.
94 97 """
95 98 format_dict = {}
99
100 # If plain text only is active
101 if self.plain_text_only:
102 formatter = self.formatters['text/plain']
103 try:
104 data = formatter(obj)
105 except:
106 # FIXME: log the exception
107 raise
108 if data is not None:
109 format_dict['text/plain'] = data
110 return format_dict
111
96 112 for format_type, formatter in self.formatters.items():
97 113 if include is not None:
98 114 if format_type not in include:
99 115 continue
100 116 if exclude is not None:
101 117 if format_type in exclude:
102 118 continue
103 119 try:
104 120 data = formatter(obj)
105 121 except:
106 122 # FIXME: log the exception
107 123 raise
108 124 if data is not None:
109 125 format_dict[format_type] = data
110 126 return format_dict
111 127
112 128 @property
113 129 def format_types(self):
114 130 """Return the format types (MIME types) of the active formatters."""
115 131 return self.formatters.keys()
116 132
117 133
118 134 #-----------------------------------------------------------------------------
119 135 # Formatters for specific format types (text, html, svg, etc.)
120 136 #-----------------------------------------------------------------------------
121 137
122 138
123 139 class FormatterABC(object):
124 140 """ Abstract base class for Formatters.
125 141
126 142 A formatter is a callable class that is responsible for computing the
127 143 raw format data for a particular format type (MIME type). For example,
128 144 an HTML formatter would have a format type of `text/html` and would return
129 145 the HTML representation of the object when called.
130 146 """
131 147 __metaclass__ = abc.ABCMeta
132 148
133 149 # The format type of the data returned, usually a MIME type.
134 150 format_type = 'text/plain'
135 151
152 # Is the formatter enabled...
153 enabled = True
154
136 155 @abc.abstractmethod
137 156 def __call__(self, obj):
138 157 """Return a JSON'able representation of the object.
139 158
140 159 If the object cannot be formatted by this formatter, then return None
141 160 """
142 161 try:
143 162 return repr(obj)
144 163 except TypeError:
145 164 return None
146 165
147 166
148 167 class BaseFormatter(Configurable):
149 168 """A base formatter class that is configurable.
150 169
151 170 This formatter should usually be used as the base class of all formatters.
152 171 It is a traited :class:`Configurable` class and includes an extensible
153 172 API for users to determine how their objects are formatted. The following
154 173 logic is used to find a function to format an given object.
155 174
156 175 1. The object is introspected to see if it has a method with the name
157 176 :attr:`print_method`. If is does, that object is passed to that method
158 177 for formatting.
159 178 2. If no print method is found, three internal dictionaries are consulted
160 179 to find print method: :attr:`singleton_printers`, :attr:`type_printers`
161 180 and :attr:`deferred_printers`.
162 181
163 182 Users should use these dictionarie to register functions that will be used
164 183 to compute the format data for their objects (if those objects don't have
165 184 the special print methods). The easiest way of using these dictionaries
166 185 is through the :meth:`for_type` and :meth:`for_type_by_name` methods.
167 186
168 187 If no function/callable is found to compute the format data, ``None`` is
169 188 returned and this format type is not used.
170 189 """
171 190
172 191 format_type = Str('text/plain')
173 192
193 enabled = Bool(True, config=True)
194
174 195 print_method = Str('__repr__')
175 196
176 197 # The singleton printers.
177 198 # Maps the IDs of the builtin singleton objects to the format functions.
178 199 singleton_printers = Dict(config=True)
179 200 def _singleton_printers_default(self):
180 201 return {}
181 202
182 203 # The type-specific printers.
183 204 # Map type objects to the format functions.
184 205 type_printers = Dict(config=True)
185 206 def _type_printers_default(self):
186 207 return {}
187 208
188 209 # The deferred-import type-specific printers.
189 210 # Map (modulename, classname) pairs to the format functions.
190 211 deferred_printers = Dict(config=True)
191 212 def _deferred_printers_default(self):
192 213 return {}
193 214
194 215 def __call__(self, obj):
195 216 """Compute the format for an object."""
217 if self.enabled:
196 218 obj_id = id(obj)
197 219 try:
198 220 obj_class = getattr(obj, '__class__', None) or type(obj)
199 221 if hasattr(obj_class, self.print_method):
200 222 printer = getattr(obj_class, self.print_method)
201 223 return printer(obj)
202 224 try:
203 225 printer = self.singleton_printers[obj_id]
204 226 except (TypeError, KeyError):
205 227 pass
206 228 else:
207 229 return printer(obj)
208 230 for cls in pretty._get_mro(obj_class):
209 231 if cls in self.type_printers:
210 232 return self.type_printers[cls](obj)
211 233 else:
212 234 printer = self._in_deferred_types(cls)
213 235 if printer is not None:
214 236 return printer(obj)
215 237 return None
216 238 except Exception:
217 239 pass
240 else:
241 return None
218 242
219 243 def for_type(self, typ, func):
220 244 """Add a format function for a given type.
221 245
222 246 Parameteres
223 247 -----------
224 248 typ : class
225 249 The class of the object that will be formatted using `func`.
226 250 func : callable
227 251 The callable that will be called to compute the format data. The
228 252 call signature of this function is simple, it must take the
229 253 object to be formatted and return the raw data for the given
230 254 format. Subclasses may use a different call signature for the
231 255 `func` argument.
232 256 """
233 257 oldfunc = self.type_printers.get(typ, None)
234 258 if func is not None:
235 259 # To support easy restoration of old printers, we need to ignore
236 260 # Nones.
237 261 self.type_printers[typ] = func
238 262 return oldfunc
239 263
240 264 def for_type_by_name(self, type_module, type_name, func):
241 265 """Add a format function for a type specified by the full dotted
242 266 module and name of the type, rather than the type of the object.
243 267
244 268 Parameters
245 269 ----------
246 270 type_module : str
247 271 The full dotted name of the module the type is defined in, like
248 272 ``numpy``.
249 273 type_name : str
250 274 The name of the type (the class name), like ``dtype``
251 275 func : callable
252 276 The callable that will be called to compute the format data. The
253 277 call signature of this function is simple, it must take the
254 278 object to be formatted and return the raw data for the given
255 279 format. Subclasses may use a different call signature for the
256 280 `func` argument.
257 281 """
258 282 key = (type_module, type_name)
259 283 oldfunc = self.deferred_printers.get(key, None)
260 284 if func is not None:
261 285 # To support easy restoration of old printers, we need to ignore
262 286 # Nones.
263 287 self.deferred_printers[key] = func
264 288 return oldfunc
265 289
266 290 def _in_deferred_types(self, cls):
267 291 """
268 292 Check if the given class is specified in the deferred type registry.
269 293
270 294 Returns the printer from the registry if it exists, and None if the
271 295 class is not in the registry. Successful matches will be moved to the
272 296 regular type registry for future use.
273 297 """
274 298 mod = getattr(cls, '__module__', None)
275 299 name = getattr(cls, '__name__', None)
276 300 key = (mod, name)
277 301 printer = None
278 302 if key in self.deferred_printers:
279 303 # Move the printer over to the regular registry.
280 304 printer = self.deferred_printers.pop(key)
281 305 self.type_printers[cls] = printer
282 306 return printer
283 307
308
284 309 class PlainTextFormatter(BaseFormatter):
285 310 """The default pretty-printer.
286 311
287 312 This uses :mod:`IPython.external.pretty` to compute the format data of
288 313 the object. If the object cannot be pretty printed, :func:`repr` is used.
289 314 See the documentation of :mod:`IPython.external.pretty` for details on
290 315 how to write pretty printers. Here is a simple example::
291 316
292 317 def dtype_pprinter(obj, p, cycle):
293 318 if cycle:
294 319 return p.text('dtype(...)')
295 320 if hasattr(obj, 'fields'):
296 321 if obj.fields is None:
297 322 p.text(repr(obj))
298 323 else:
299 324 p.begin_group(7, 'dtype([')
300 325 for i, field in enumerate(obj.descr):
301 326 if i > 0:
302 327 p.text(',')
303 328 p.breakable()
304 329 p.pretty(field)
305 330 p.end_group(7, '])')
306 331 """
307 332
308 333 # The format type of data returned.
309 334 format_type = Str('text/plain')
310 335
336 # This subclass ignores this attribute as it always need to return
337 # something.
338 enabled = Bool(True, config=False)
339
311 340 # Look for a __pretty__ methods to use for pretty printing.
312 341 print_method = Str('__pretty__')
313 342
314 343 # Whether to pretty-print or not.
315 344 pprint = Bool(True, config=True)
316 345
317 346 # Whether to be verbose or not.
318 347 verbose = Bool(False, config=True)
319 348
320 349 # The maximum width.
321 350 max_width = Int(79, config=True)
322 351
323 352 # The newline character.
324 353 newline = Str('\n', config=True)
325 354
326 355 # Use the default pretty printers from IPython.external.pretty.
327 356 def _singleton_printers_default(self):
328 357 return pretty._singleton_pprinters.copy()
329 358
330 359 def _type_printers_default(self):
331 360 return pretty._type_pprinters.copy()
332 361
333 362 def _deferred_printers_default(self):
334 363 return pretty._deferred_type_pprinters.copy()
335 364
336 365 #### FormatterABC interface ####
337 366
338 367 def __call__(self, obj):
339 368 """Compute the pretty representation of the object."""
340 369 if not self.pprint:
341 370 try:
342 371 return repr(obj)
343 372 except TypeError:
344 373 return ''
345 374 else:
346 375 # This uses use StringIO, as cStringIO doesn't handle unicode.
347 376 stream = StringIO()
348 377 printer = pretty.RepresentationPrinter(stream, self.verbose,
349 378 self.max_width, self.newline,
350 379 singleton_pprinters=self.singleton_printers,
351 380 type_pprinters=self.type_printers,
352 381 deferred_pprinters=self.deferred_printers)
353 382 printer.pretty(obj)
354 383 printer.flush()
355 384 return stream.getvalue()
356 385
357 386
358 387 class HTMLFormatter(BaseFormatter):
359 388 """An HTML formatter.
360 389
361 390 To define the callables that compute the HTML representation of your
362 391 objects, define a :meth:`__html__` method or use the :meth:`for_type`
363 392 or :meth:`for_type_by_name` methods to register functions that handle
364 393 this.
365 394 """
366 395 format_type = Str('text/html')
367 396
368 397 print_method = Str('__html__')
369 398
370 399
371 400 class SVGFormatter(BaseFormatter):
372 401 """An SVG formatter.
373 402
374 403 To define the callables that compute the SVG representation of your
375 404 objects, define a :meth:`__svg__` method or use the :meth:`for_type`
376 405 or :meth:`for_type_by_name` methods to register functions that handle
377 406 this.
378 407 """
379 408 format_type = Str('image/svg+xml')
380 409
381 410 print_method = Str('__svg__')
382 411
383 412
384 413 class PNGFormatter(BaseFormatter):
385 414 """A PNG formatter.
386 415
387 416 To define the callables that compute the PNG representation of your
388 417 objects, define a :meth:`__svg__` method or use the :meth:`for_type`
389 418 or :meth:`for_type_by_name` methods to register functions that handle
390 419 this. The raw data should be the base64 encoded raw png data.
391 420 """
392 421 format_type = Str('image/png')
393 422
394 423 print_method = Str('__png__')
395 424
396 425
397 426 class LatexFormatter(BaseFormatter):
398 427 """A LaTeX formatter.
399 428
400 429 To define the callables that compute the LaTeX representation of your
401 430 objects, define a :meth:`__latex__` method or use the :meth:`for_type`
402 431 or :meth:`for_type_by_name` methods to register functions that handle
403 432 this.
404 433 """
405 434 format_type = Str('text/latex')
406 435
407 436 print_method = Str('__latex__')
408 437
409 438
410 439 class JSONFormatter(BaseFormatter):
411 440 """A JSON string formatter.
412 441
413 442 To define the callables that compute the JSON string representation of
414 443 your objects, define a :meth:`__json__` method or use the :meth:`for_type`
415 444 or :meth:`for_type_by_name` methods to register functions that handle
416 445 this.
417 446 """
418 447 format_type = Str('application/json')
419 448
420 449 print_method = Str('__json__')
421 450
422 451
423 452 FormatterABC.register(BaseFormatter)
424 453 FormatterABC.register(PlainTextFormatter)
425 454 FormatterABC.register(HTMLFormatter)
426 455 FormatterABC.register(SVGFormatter)
427 456 FormatterABC.register(PNGFormatter)
428 457 FormatterABC.register(LatexFormatter)
429 458 FormatterABC.register(JSONFormatter)
430 459
431 460
432 461 def format_display_data(obj, include=None, exclude=None):
433 462 """Return a format data dict for an object.
434 463
435 464 By default all format types will be computed.
436 465
437 466 The following MIME types are currently implemented:
438 467
439 468 * text/plain
440 469 * text/html
441 470 * text/latex
442 471 * application/json
443 472 * image/png
444 473 * immage/svg+xml
445 474
446 475 Parameters
447 476 ----------
448 477 obj : object
449 478 The Python object whose format data will be computed.
450 479
451 480 Returns
452 481 -------
453 482 format_dict : dict
454 483 A dictionary of key/value pairs, one or each format that was
455 484 generated for the object. The keys are the format types, which
456 485 will usually be MIME type strings and the values and JSON'able
457 486 data structure containing the raw data for the representation in
458 487 that format.
459 488 include : list or tuple, optional
460 489 A list of format type strings (MIME types) to include in the
461 490 format data dict. If this is set *only* the format types included
462 491 in this list will be computed.
463 492 exclude : list or tuple, optional
464 493 A list of format type string (MIME types) to exclue in the format
465 494 data dict. If this is set all format types will be computed,
466 495 except for those included in this argument.
467 496 """
468 497 from IPython.core.interactiveshell import InteractiveShell
469 498
470 499 InteractiveShell.instance().display_formatter.format(
471 500 obj,
472 501 include,
473 502 exclude
474 503 )
@@ -1,263 +1,236 b''
1 1 """hooks for IPython.
2 2
3 3 In Python, it is possible to overwrite any method of any object if you really
4 4 want to. But IPython exposes a few 'hooks', methods which are _designed_ to
5 5 be overwritten by users for customization purposes. This module defines the
6 6 default versions of all such hooks, which get used by IPython if not
7 7 overridden by the user.
8 8
9 9 hooks are simple functions, but they should be declared with 'self' as their
10 10 first argument, because when activated they are registered into IPython as
11 11 instance methods. The self argument will be the IPython running instance
12 12 itself, so hooks have full access to the entire IPython object.
13 13
14 14 If you wish to define a new hook and activate it, you need to put the
15 15 necessary code into a python file which can be either imported or execfile()'d
16 16 from within your ipythonrc configuration.
17 17
18 18 For example, suppose that you have a module called 'myiphooks' in your
19 19 PYTHONPATH, which contains the following definition:
20 20
21 21 import os
22 22 from IPython.core import ipapi
23 23 ip = ipapi.get()
24 24
25 25 def calljed(self,filename, linenum):
26 26 "My editor hook calls the jed editor directly."
27 27 print "Calling my own editor, jed ..."
28 28 if os.system('jed +%d %s' % (linenum,filename)) != 0:
29 29 raise TryNext()
30 30
31 31 ip.set_hook('editor', calljed)
32 32
33 33 You can then enable the functionality by doing 'import myiphooks'
34 34 somewhere in your configuration files or ipython command line.
35 35 """
36 36
37 37 #*****************************************************************************
38 38 # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
39 39 #
40 40 # Distributed under the terms of the BSD License. The full license is in
41 41 # the file COPYING, distributed as part of this software.
42 42 #*****************************************************************************
43 43
44 44 import os, bisect
45 45 import sys
46 46
47 47 from IPython.core.error import TryNext
48 48 import IPython.utils.io
49 49
50 50 # List here all the default hooks. For now it's just the editor functions
51 51 # but over time we'll move here all the public API for user-accessible things.
52 52
53 53 __all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor',
54 54 'input_prefilter', 'shutdown_hook', 'late_startup_hook',
55 55 'generate_prompt', 'show_in_pager','pre_prompt_hook',
56 56 'pre_run_code_hook', 'clipboard_get']
57 57
58 58 def editor(self,filename, linenum=None):
59 59 """Open the default editor at the given filename and linenumber.
60 60
61 61 This is IPython's default editor hook, you can use it as an example to
62 62 write your own modified one. To set your own editor function as the
63 63 new editor hook, call ip.set_hook('editor',yourfunc)."""
64 64
65 65 # IPython configures a default editor at startup by reading $EDITOR from
66 66 # the environment, and falling back on vi (unix) or notepad (win32).
67 67 editor = self.editor
68 68
69 69 # marker for at which line to open the file (for existing objects)
70 70 if linenum is None or editor=='notepad':
71 71 linemark = ''
72 72 else:
73 73 linemark = '+%d' % int(linenum)
74 74
75 75 # Enclose in quotes if necessary and legal
76 76 if ' ' in editor and os.path.isfile(editor) and editor[0] != '"':
77 77 editor = '"%s"' % editor
78 78
79 79 # Call the actual editor
80 80 if os.system('%s %s %s' % (editor,linemark,filename)) != 0:
81 81 raise TryNext()
82 82
83 83 import tempfile
84 84 def fix_error_editor(self,filename,linenum,column,msg):
85 85 """Open the editor at the given filename, linenumber, column and
86 86 show an error message. This is used for correcting syntax errors.
87 87 The current implementation only has special support for the VIM editor,
88 88 and falls back on the 'editor' hook if VIM is not used.
89 89
90 90 Call ip.set_hook('fix_error_editor',youfunc) to use your own function,
91 91 """
92 92 def vim_quickfix_file():
93 93 t = tempfile.NamedTemporaryFile()
94 94 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
95 95 t.flush()
96 96 return t
97 97 if os.path.basename(self.editor) != 'vim':
98 98 self.hooks.editor(filename,linenum)
99 99 return
100 100 t = vim_quickfix_file()
101 101 try:
102 102 if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
103 103 raise TryNext()
104 104 finally:
105 105 t.close()
106 106
107 107
108 108 def synchronize_with_editor(self, filename, linenum, column):
109 109 pass
110 110
111 111
112 112 class CommandChainDispatcher:
113 113 """ Dispatch calls to a chain of commands until some func can handle it
114 114
115 115 Usage: instantiate, execute "add" to add commands (with optional
116 116 priority), execute normally via f() calling mechanism.
117 117
118 118 """
119 119 def __init__(self,commands=None):
120 120 if commands is None:
121 121 self.chain = []
122 122 else:
123 123 self.chain = commands
124 124
125 125
126 126 def __call__(self,*args, **kw):
127 127 """ Command chain is called just like normal func.
128 128
129 129 This will call all funcs in chain with the same args as were given to this
130 130 function, and return the result of first func that didn't raise
131 131 TryNext """
132 132
133 133 for prio,cmd in self.chain:
134 134 #print "prio",prio,"cmd",cmd #dbg
135 135 try:
136 136 return cmd(*args, **kw)
137 137 except TryNext, exc:
138 138 if exc.args or exc.kwargs:
139 139 args = exc.args
140 140 kw = exc.kwargs
141 141 # if no function will accept it, raise TryNext up to the caller
142 142 raise TryNext
143 143
144 144 def __str__(self):
145 145 return str(self.chain)
146 146
147 147 def add(self, func, priority=0):
148 148 """ Add a func to the cmd chain with given priority """
149 149 bisect.insort(self.chain,(priority,func))
150 150
151 151 def __iter__(self):
152 152 """ Return all objects in chain.
153 153
154 154 Handy if the objects are not callable.
155 155 """
156 156 return iter(self.chain)
157 157
158 158
159 def result_display(self,arg):
160 """ Default display hook.
161
162 Called for displaying the result to the user.
163 """
164
165 if self.pprint:
166 try:
167 out = pformat(arg)
168 except:
169 # Work around possible bugs in pformat
170 out = repr(arg)
171 if '\n' in out:
172 # So that multi-line strings line up with the left column of
173 # the screen, instead of having the output prompt mess up
174 # their first line.
175 IPython.utils.io.Term.cout.write('\n')
176 print >>IPython.utils.io.Term.cout, out
177 else:
178 # By default, the interactive prompt uses repr() to display results,
179 # so we should honor this. Users who'd rather use a different
180 # mechanism can easily override this hook.
181 print >>IPython.utils.io.Term.cout, repr(arg)
182 # the default display hook doesn't manipulate the value to put in history
183 return None
184
185
186 159 def input_prefilter(self,line):
187 160 """ Default input prefilter
188 161
189 162 This returns the line as unchanged, so that the interpreter
190 163 knows that nothing was done and proceeds with "classic" prefiltering
191 164 (%magics, !shell commands etc.).
192 165
193 166 Note that leading whitespace is not passed to this hook. Prefilter
194 167 can't alter indentation.
195 168
196 169 """
197 170 #print "attempt to rewrite",line #dbg
198 171 return line
199 172
200 173
201 174 def shutdown_hook(self):
202 175 """ default shutdown hook
203 176
204 177 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
205 178 """
206 179
207 180 #print "default shutdown hook ok" # dbg
208 181 return
209 182
210 183
211 184 def late_startup_hook(self):
212 185 """ Executed after ipython has been constructed and configured
213 186
214 187 """
215 188 #print "default startup hook ok" # dbg
216 189
217 190
218 191 def generate_prompt(self, is_continuation):
219 192 """ calculate and return a string with the prompt to display """
220 193 if is_continuation:
221 194 return str(self.displayhook.prompt2)
222 195 return str(self.displayhook.prompt1)
223 196
224 197
225 198 def show_in_pager(self,s):
226 199 """ Run a string through pager """
227 200 # raising TryNext here will use the default paging functionality
228 201 raise TryNext
229 202
230 203
231 204 def pre_prompt_hook(self):
232 205 """ Run before displaying the next prompt
233 206
234 207 Use this e.g. to display output from asynchronous operations (in order
235 208 to not mess up text entry)
236 209 """
237 210
238 211 return None
239 212
240 213
241 214 def pre_run_code_hook(self):
242 215 """ Executed before running the (prefiltered) code in IPython """
243 216 return None
244 217
245 218
246 219 def clipboard_get(self):
247 220 """ Get text from the clipboard.
248 221 """
249 222 from IPython.lib.clipboard import (
250 223 osx_clipboard_get, tkinter_clipboard_get,
251 224 win32_clipboard_get
252 225 )
253 226 if sys.platform == 'win32':
254 227 chain = [win32_clipboard_get, tkinter_clipboard_get]
255 228 elif sys.platform == 'darwin':
256 229 chain = [osx_clipboard_get, tkinter_clipboard_get]
257 230 else:
258 231 chain = [tkinter_clipboard_get]
259 232 dispatcher = CommandChainDispatcher()
260 233 for func in chain:
261 234 dispatcher.add(func)
262 235 text = dispatcher()
263 236 return text
@@ -1,2556 +1,2555 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Main IPython class."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2010 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 from __future__ import with_statement
18 18 from __future__ import absolute_import
19 19
20 20 import __builtin__
21 21 import __future__
22 22 import abc
23 23 import atexit
24 24 import codeop
25 25 import os
26 26 import re
27 27 import sys
28 28 import tempfile
29 29 import types
30 30 from contextlib import nested
31 31
32 32 from IPython.config.configurable import Configurable
33 33 from IPython.core import debugger, oinspect
34 34 from IPython.core import history as ipcorehist
35 35 from IPython.core import page
36 36 from IPython.core import prefilter
37 37 from IPython.core import shadowns
38 38 from IPython.core import ultratb
39 39 from IPython.core.alias import AliasManager
40 40 from IPython.core.builtin_trap import BuiltinTrap
41 41 from IPython.core.compilerop import CachingCompiler
42 42 from IPython.core.display_trap import DisplayTrap
43 43 from IPython.core.displayhook import DisplayHook
44 44 from IPython.core.displaypub import DisplayPublisher
45 45 from IPython.core.error import TryNext, UsageError
46 46 from IPython.core.extensions import ExtensionManager
47 47 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
48 48 from IPython.core.formatters import DisplayFormatter
49 49 from IPython.core.history import HistoryManager
50 50 from IPython.core.inputsplitter import IPythonInputSplitter
51 51 from IPython.core.logger import Logger
52 52 from IPython.core.magic import Magic
53 53 from IPython.core.payload import PayloadManager
54 54 from IPython.core.plugin import PluginManager
55 55 from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
56 56 from IPython.external.Itpl import ItplNS
57 57 from IPython.utils import PyColorize
58 58 from IPython.utils import io
59 59 from IPython.utils import pickleshare
60 60 from IPython.utils.doctestreload import doctest_reload
61 61 from IPython.utils.io import ask_yes_no, rprint
62 62 from IPython.utils.ipstruct import Struct
63 63 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
64 64 from IPython.utils.process import system, getoutput
65 65 from IPython.utils.strdispatch import StrDispatch
66 66 from IPython.utils.syspathcontext import prepended_to_syspath
67 67 from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList
68 68 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
69 69 List, Unicode, Instance, Type)
70 70 from IPython.utils.warn import warn, error, fatal
71 71 import IPython.core.hooks
72 72
73 73 #-----------------------------------------------------------------------------
74 74 # Globals
75 75 #-----------------------------------------------------------------------------
76 76
77 77 # compiled regexps for autoindent management
78 78 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
79 79
80 80 #-----------------------------------------------------------------------------
81 81 # Utilities
82 82 #-----------------------------------------------------------------------------
83 83
84 84 # store the builtin raw_input globally, and use this always, in case user code
85 85 # overwrites it (like wx.py.PyShell does)
86 86 raw_input_original = raw_input
87 87
88 88 def softspace(file, newvalue):
89 89 """Copied from code.py, to remove the dependency"""
90 90
91 91 oldvalue = 0
92 92 try:
93 93 oldvalue = file.softspace
94 94 except AttributeError:
95 95 pass
96 96 try:
97 97 file.softspace = newvalue
98 98 except (AttributeError, TypeError):
99 99 # "attribute-less object" or "read-only attributes"
100 100 pass
101 101 return oldvalue
102 102
103 103
104 104 def no_op(*a, **kw): pass
105 105
106 106 class SpaceInInput(Exception): pass
107 107
108 108 class Bunch: pass
109 109
110 110
111 111 def get_default_colors():
112 112 if sys.platform=='darwin':
113 113 return "LightBG"
114 114 elif os.name=='nt':
115 115 return 'Linux'
116 116 else:
117 117 return 'Linux'
118 118
119 119
120 120 class SeparateStr(Str):
121 121 """A Str subclass to validate separate_in, separate_out, etc.
122 122
123 123 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
124 124 """
125 125
126 126 def validate(self, obj, value):
127 127 if value == '0': value = ''
128 128 value = value.replace('\\n','\n')
129 129 return super(SeparateStr, self).validate(obj, value)
130 130
131 131 class MultipleInstanceError(Exception):
132 132 pass
133 133
134 134
135 135 #-----------------------------------------------------------------------------
136 136 # Main IPython class
137 137 #-----------------------------------------------------------------------------
138 138
139 139 class InteractiveShell(Configurable, Magic):
140 140 """An enhanced, interactive shell for Python."""
141 141
142 142 _instance = None
143 143 autocall = Enum((0,1,2), default_value=1, config=True)
144 144 # TODO: remove all autoindent logic and put into frontends.
145 145 # We can't do this yet because even runlines uses the autoindent.
146 146 autoindent = CBool(True, config=True)
147 147 automagic = CBool(True, config=True)
148 148 cache_size = Int(1000, config=True)
149 149 color_info = CBool(True, config=True)
150 150 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
151 151 default_value=get_default_colors(), config=True)
152 152 debug = CBool(False, config=True)
153 153 deep_reload = CBool(False, config=True)
154 154 display_formatter = Instance(DisplayFormatter)
155 155 displayhook_class = Type(DisplayHook)
156 156 display_pub_class = Type(DisplayPublisher)
157 157
158 158 exit_now = CBool(False)
159 159 # Monotonically increasing execution counter
160 160 execution_count = Int(1)
161 161 filename = Str("<ipython console>")
162 162 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
163 163
164 164 # Input splitter, to split entire cells of input into either individual
165 165 # interactive statements or whole blocks.
166 166 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
167 167 (), {})
168 168 logstart = CBool(False, config=True)
169 169 logfile = Str('', config=True)
170 170 logappend = Str('', config=True)
171 171 object_info_string_level = Enum((0,1,2), default_value=0,
172 172 config=True)
173 173 pdb = CBool(False, config=True)
174 174
175 pprint = CBool(True, config=True)
176 175 profile = Str('', config=True)
177 176 prompt_in1 = Str('In [\\#]: ', config=True)
178 177 prompt_in2 = Str(' .\\D.: ', config=True)
179 178 prompt_out = Str('Out[\\#]: ', config=True)
180 179 prompts_pad_left = CBool(True, config=True)
181 180 quiet = CBool(False, config=True)
182 181
183 182 history_length = Int(10000, config=True)
184 183
185 184 # The readline stuff will eventually be moved to the terminal subclass
186 185 # but for now, we can't do that as readline is welded in everywhere.
187 186 readline_use = CBool(True, config=True)
188 187 readline_merge_completions = CBool(True, config=True)
189 188 readline_omit__names = Enum((0,1,2), default_value=2, config=True)
190 189 readline_remove_delims = Str('-/~', config=True)
191 190 readline_parse_and_bind = List([
192 191 'tab: complete',
193 192 '"\C-l": clear-screen',
194 193 'set show-all-if-ambiguous on',
195 194 '"\C-o": tab-insert',
196 195 '"\M-i": " "',
197 196 '"\M-o": "\d\d\d\d"',
198 197 '"\M-I": "\d\d\d\d"',
199 198 '"\C-r": reverse-search-history',
200 199 '"\C-s": forward-search-history',
201 200 '"\C-p": history-search-backward',
202 201 '"\C-n": history-search-forward',
203 202 '"\e[A": history-search-backward',
204 203 '"\e[B": history-search-forward',
205 204 '"\C-k": kill-line',
206 205 '"\C-u": unix-line-discard',
207 206 ], allow_none=False, config=True)
208 207
209 208 # TODO: this part of prompt management should be moved to the frontends.
210 209 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
211 210 separate_in = SeparateStr('\n', config=True)
212 211 separate_out = SeparateStr('', config=True)
213 212 separate_out2 = SeparateStr('', config=True)
214 213 wildcards_case_sensitive = CBool(True, config=True)
215 214 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
216 215 default_value='Context', config=True)
217 216
218 217 # Subcomponents of InteractiveShell
219 218 alias_manager = Instance('IPython.core.alias.AliasManager')
220 219 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
221 220 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
222 221 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
223 222 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
224 223 plugin_manager = Instance('IPython.core.plugin.PluginManager')
225 224 payload_manager = Instance('IPython.core.payload.PayloadManager')
226 225 history_manager = Instance('IPython.core.history.HistoryManager')
227 226
228 227 # Private interface
229 228 _post_execute = set()
230 229
231 230 def __init__(self, config=None, ipython_dir=None,
232 231 user_ns=None, user_global_ns=None,
233 232 custom_exceptions=((), None)):
234 233
235 234 # This is where traits with a config_key argument are updated
236 235 # from the values on config.
237 236 super(InteractiveShell, self).__init__(config=config)
238 237
239 238 # These are relatively independent and stateless
240 239 self.init_ipython_dir(ipython_dir)
241 240 self.init_instance_attrs()
242 241 self.init_environment()
243 242
244 243 # Create namespaces (user_ns, user_global_ns, etc.)
245 244 self.init_create_namespaces(user_ns, user_global_ns)
246 245 # This has to be done after init_create_namespaces because it uses
247 246 # something in self.user_ns, but before init_sys_modules, which
248 247 # is the first thing to modify sys.
249 248 # TODO: When we override sys.stdout and sys.stderr before this class
250 249 # is created, we are saving the overridden ones here. Not sure if this
251 250 # is what we want to do.
252 251 self.save_sys_module_state()
253 252 self.init_sys_modules()
254 253
255 254 self.init_history()
256 255 self.init_encoding()
257 256 self.init_prefilter()
258 257
259 258 Magic.__init__(self, self)
260 259
261 260 self.init_syntax_highlighting()
262 261 self.init_hooks()
263 262 self.init_pushd_popd_magic()
264 263 # self.init_traceback_handlers use to be here, but we moved it below
265 264 # because it and init_io have to come after init_readline.
266 265 self.init_user_ns()
267 266 self.init_logger()
268 267 self.init_alias()
269 268 self.init_builtins()
270 269
271 270 # pre_config_initialization
272 271
273 272 # The next section should contain everything that was in ipmaker.
274 273 self.init_logstart()
275 274
276 275 # The following was in post_config_initialization
277 276 self.init_inspector()
278 277 # init_readline() must come before init_io(), because init_io uses
279 278 # readline related things.
280 279 self.init_readline()
281 280 # init_completer must come after init_readline, because it needs to
282 281 # know whether readline is present or not system-wide to configure the
283 282 # completers, since the completion machinery can now operate
284 283 # independently of readline (e.g. over the network)
285 284 self.init_completer()
286 285 # TODO: init_io() needs to happen before init_traceback handlers
287 286 # because the traceback handlers hardcode the stdout/stderr streams.
288 287 # This logic in in debugger.Pdb and should eventually be changed.
289 288 self.init_io()
290 289 self.init_traceback_handlers(custom_exceptions)
291 290 self.init_prompts()
292 291 self.init_display_formatter()
293 292 self.init_display_pub()
294 293 self.init_displayhook()
295 294 self.init_reload_doctest()
296 295 self.init_magics()
297 296 self.init_pdb()
298 297 self.init_extension_manager()
299 298 self.init_plugin_manager()
300 299 self.init_payload()
301 300 self.hooks.late_startup_hook()
302 301 atexit.register(self.atexit_operations)
303 302
304 303 # While we're trying to have each part of the code directly access what it
305 304 # needs without keeping redundant references to objects, we have too much
306 305 # legacy code that expects ip.db to exist, so let's make it a property that
307 306 # retrieves the underlying object from our new history manager.
308 307 @property
309 308 def db(self):
310 309 return self.history_manager.shadow_db
311 310
312 311 @classmethod
313 312 def instance(cls, *args, **kwargs):
314 313 """Returns a global InteractiveShell instance."""
315 314 if cls._instance is None:
316 315 inst = cls(*args, **kwargs)
317 316 # Now make sure that the instance will also be returned by
318 317 # the subclasses instance attribute.
319 318 for subclass in cls.mro():
320 319 if issubclass(cls, subclass) and \
321 320 issubclass(subclass, InteractiveShell):
322 321 subclass._instance = inst
323 322 else:
324 323 break
325 324 if isinstance(cls._instance, cls):
326 325 return cls._instance
327 326 else:
328 327 raise MultipleInstanceError(
329 328 'Multiple incompatible subclass instances of '
330 329 'InteractiveShell are being created.'
331 330 )
332 331
333 332 @classmethod
334 333 def initialized(cls):
335 334 return hasattr(cls, "_instance")
336 335
337 336 def get_ipython(self):
338 337 """Return the currently running IPython instance."""
339 338 return self
340 339
341 340 #-------------------------------------------------------------------------
342 341 # Trait changed handlers
343 342 #-------------------------------------------------------------------------
344 343
345 344 def _ipython_dir_changed(self, name, new):
346 345 if not os.path.isdir(new):
347 346 os.makedirs(new, mode = 0777)
348 347
349 348 def set_autoindent(self,value=None):
350 349 """Set the autoindent flag, checking for readline support.
351 350
352 351 If called with no arguments, it acts as a toggle."""
353 352
354 353 if not self.has_readline:
355 354 if os.name == 'posix':
356 355 warn("The auto-indent feature requires the readline library")
357 356 self.autoindent = 0
358 357 return
359 358 if value is None:
360 359 self.autoindent = not self.autoindent
361 360 else:
362 361 self.autoindent = value
363 362
364 363 #-------------------------------------------------------------------------
365 364 # init_* methods called by __init__
366 365 #-------------------------------------------------------------------------
367 366
368 367 def init_ipython_dir(self, ipython_dir):
369 368 if ipython_dir is not None:
370 369 self.ipython_dir = ipython_dir
371 370 self.config.Global.ipython_dir = self.ipython_dir
372 371 return
373 372
374 373 if hasattr(self.config.Global, 'ipython_dir'):
375 374 self.ipython_dir = self.config.Global.ipython_dir
376 375 else:
377 376 self.ipython_dir = get_ipython_dir()
378 377
379 378 # All children can just read this
380 379 self.config.Global.ipython_dir = self.ipython_dir
381 380
382 381 def init_instance_attrs(self):
383 382 self.more = False
384 383
385 384 # command compiler
386 385 self.compile = CachingCompiler()
387 386
388 387 # User input buffers
389 388 # NOTE: these variables are slated for full removal, once we are 100%
390 389 # sure that the new execution logic is solid. We will delte runlines,
391 390 # push_line and these buffers, as all input will be managed by the
392 391 # frontends via an inputsplitter instance.
393 392 self.buffer = []
394 393 self.buffer_raw = []
395 394
396 395 # Make an empty namespace, which extension writers can rely on both
397 396 # existing and NEVER being used by ipython itself. This gives them a
398 397 # convenient location for storing additional information and state
399 398 # their extensions may require, without fear of collisions with other
400 399 # ipython names that may develop later.
401 400 self.meta = Struct()
402 401
403 402 # Object variable to store code object waiting execution. This is
404 403 # used mainly by the multithreaded shells, but it can come in handy in
405 404 # other situations. No need to use a Queue here, since it's a single
406 405 # item which gets cleared once run.
407 406 self.code_to_run = None
408 407
409 408 # Temporary files used for various purposes. Deleted at exit.
410 409 self.tempfiles = []
411 410
412 411 # Keep track of readline usage (later set by init_readline)
413 412 self.has_readline = False
414 413
415 414 # keep track of where we started running (mainly for crash post-mortem)
416 415 # This is not being used anywhere currently.
417 416 self.starting_dir = os.getcwd()
418 417
419 418 # Indentation management
420 419 self.indent_current_nsp = 0
421 420
422 421 def init_environment(self):
423 422 """Any changes we need to make to the user's environment."""
424 423 pass
425 424
426 425 def init_encoding(self):
427 426 # Get system encoding at startup time. Certain terminals (like Emacs
428 427 # under Win32 have it set to None, and we need to have a known valid
429 428 # encoding to use in the raw_input() method
430 429 try:
431 430 self.stdin_encoding = sys.stdin.encoding or 'ascii'
432 431 except AttributeError:
433 432 self.stdin_encoding = 'ascii'
434 433
435 434 def init_syntax_highlighting(self):
436 435 # Python source parser/formatter for syntax highlighting
437 436 pyformat = PyColorize.Parser().format
438 437 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
439 438
440 439 def init_pushd_popd_magic(self):
441 440 # for pushd/popd management
442 441 try:
443 442 self.home_dir = get_home_dir()
444 443 except HomeDirError, msg:
445 444 fatal(msg)
446 445
447 446 self.dir_stack = []
448 447
449 448 def init_logger(self):
450 449 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
451 450 logmode='rotate')
452 451
453 452 def init_logstart(self):
454 453 """Initialize logging in case it was requested at the command line.
455 454 """
456 455 if self.logappend:
457 456 self.magic_logstart(self.logappend + ' append')
458 457 elif self.logfile:
459 458 self.magic_logstart(self.logfile)
460 459 elif self.logstart:
461 460 self.magic_logstart()
462 461
463 462 def init_builtins(self):
464 463 self.builtin_trap = BuiltinTrap(shell=self)
465 464
466 465 def init_inspector(self):
467 466 # Object inspector
468 467 self.inspector = oinspect.Inspector(oinspect.InspectColors,
469 468 PyColorize.ANSICodeColors,
470 469 'NoColor',
471 470 self.object_info_string_level)
472 471
473 472 def init_io(self):
474 473 # This will just use sys.stdout and sys.stderr. If you want to
475 474 # override sys.stdout and sys.stderr themselves, you need to do that
476 475 # *before* instantiating this class, because Term holds onto
477 476 # references to the underlying streams.
478 477 if sys.platform == 'win32' and self.has_readline:
479 478 Term = io.IOTerm(cout=self.readline._outputfile,
480 479 cerr=self.readline._outputfile)
481 480 else:
482 481 Term = io.IOTerm()
483 482 io.Term = Term
484 483
485 484 def init_prompts(self):
486 485 # TODO: This is a pass for now because the prompts are managed inside
487 486 # the DisplayHook. Once there is a separate prompt manager, this
488 487 # will initialize that object and all prompt related information.
489 488 pass
490 489
491 490 def init_display_formatter(self):
492 491 self.display_formatter = DisplayFormatter(config=self.config)
493 492
494 493 def init_display_pub(self):
495 494 self.display_pub = self.display_pub_class(config=self.config)
496 495
497 496 def init_displayhook(self):
498 497 # Initialize displayhook, set in/out prompts and printing system
499 498 self.displayhook = self.displayhook_class(
500 499 config=self.config,
501 500 shell=self,
502 501 cache_size=self.cache_size,
503 502 input_sep = self.separate_in,
504 503 output_sep = self.separate_out,
505 504 output_sep2 = self.separate_out2,
506 505 ps1 = self.prompt_in1,
507 506 ps2 = self.prompt_in2,
508 507 ps_out = self.prompt_out,
509 508 pad_left = self.prompts_pad_left
510 509 )
511 510 # This is a context manager that installs/revmoes the displayhook at
512 511 # the appropriate time.
513 512 self.display_trap = DisplayTrap(hook=self.displayhook)
514 513
515 514 def init_reload_doctest(self):
516 515 # Do a proper resetting of doctest, including the necessary displayhook
517 516 # monkeypatching
518 517 try:
519 518 doctest_reload()
520 519 except ImportError:
521 520 warn("doctest module does not exist.")
522 521
523 522 #-------------------------------------------------------------------------
524 523 # Things related to injections into the sys module
525 524 #-------------------------------------------------------------------------
526 525
527 526 def save_sys_module_state(self):
528 527 """Save the state of hooks in the sys module.
529 528
530 529 This has to be called after self.user_ns is created.
531 530 """
532 531 self._orig_sys_module_state = {}
533 532 self._orig_sys_module_state['stdin'] = sys.stdin
534 533 self._orig_sys_module_state['stdout'] = sys.stdout
535 534 self._orig_sys_module_state['stderr'] = sys.stderr
536 535 self._orig_sys_module_state['excepthook'] = sys.excepthook
537 536 try:
538 537 self._orig_sys_modules_main_name = self.user_ns['__name__']
539 538 except KeyError:
540 539 pass
541 540
542 541 def restore_sys_module_state(self):
543 542 """Restore the state of the sys module."""
544 543 try:
545 544 for k, v in self._orig_sys_module_state.iteritems():
546 545 setattr(sys, k, v)
547 546 except AttributeError:
548 547 pass
549 548 # Reset what what done in self.init_sys_modules
550 549 try:
551 550 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
552 551 except (AttributeError, KeyError):
553 552 pass
554 553
555 554 #-------------------------------------------------------------------------
556 555 # Things related to hooks
557 556 #-------------------------------------------------------------------------
558 557
559 558 def init_hooks(self):
560 559 # hooks holds pointers used for user-side customizations
561 560 self.hooks = Struct()
562 561
563 562 self.strdispatchers = {}
564 563
565 564 # Set all default hooks, defined in the IPython.hooks module.
566 565 hooks = IPython.core.hooks
567 566 for hook_name in hooks.__all__:
568 567 # default hooks have priority 100, i.e. low; user hooks should have
569 568 # 0-100 priority
570 569 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
571 570
572 571 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
573 572 """set_hook(name,hook) -> sets an internal IPython hook.
574 573
575 574 IPython exposes some of its internal API as user-modifiable hooks. By
576 575 adding your function to one of these hooks, you can modify IPython's
577 576 behavior to call at runtime your own routines."""
578 577
579 578 # At some point in the future, this should validate the hook before it
580 579 # accepts it. Probably at least check that the hook takes the number
581 580 # of args it's supposed to.
582 581
583 582 f = types.MethodType(hook,self)
584 583
585 584 # check if the hook is for strdispatcher first
586 585 if str_key is not None:
587 586 sdp = self.strdispatchers.get(name, StrDispatch())
588 587 sdp.add_s(str_key, f, priority )
589 588 self.strdispatchers[name] = sdp
590 589 return
591 590 if re_key is not None:
592 591 sdp = self.strdispatchers.get(name, StrDispatch())
593 592 sdp.add_re(re.compile(re_key), f, priority )
594 593 self.strdispatchers[name] = sdp
595 594 return
596 595
597 596 dp = getattr(self.hooks, name, None)
598 597 if name not in IPython.core.hooks.__all__:
599 598 print "Warning! Hook '%s' is not one of %s" % \
600 599 (name, IPython.core.hooks.__all__ )
601 600 if not dp:
602 601 dp = IPython.core.hooks.CommandChainDispatcher()
603 602
604 603 try:
605 604 dp.add(f,priority)
606 605 except AttributeError:
607 606 # it was not commandchain, plain old func - replace
608 607 dp = f
609 608
610 609 setattr(self.hooks,name, dp)
611 610
612 611 def register_post_execute(self, func):
613 612 """Register a function for calling after code execution.
614 613 """
615 614 if not callable(func):
616 615 raise ValueError('argument %s must be callable' % func)
617 616 self._post_execute.add(func)
618 617
619 618 #-------------------------------------------------------------------------
620 619 # Things related to the "main" module
621 620 #-------------------------------------------------------------------------
622 621
623 622 def new_main_mod(self,ns=None):
624 623 """Return a new 'main' module object for user code execution.
625 624 """
626 625 main_mod = self._user_main_module
627 626 init_fakemod_dict(main_mod,ns)
628 627 return main_mod
629 628
630 629 def cache_main_mod(self,ns,fname):
631 630 """Cache a main module's namespace.
632 631
633 632 When scripts are executed via %run, we must keep a reference to the
634 633 namespace of their __main__ module (a FakeModule instance) around so
635 634 that Python doesn't clear it, rendering objects defined therein
636 635 useless.
637 636
638 637 This method keeps said reference in a private dict, keyed by the
639 638 absolute path of the module object (which corresponds to the script
640 639 path). This way, for multiple executions of the same script we only
641 640 keep one copy of the namespace (the last one), thus preventing memory
642 641 leaks from old references while allowing the objects from the last
643 642 execution to be accessible.
644 643
645 644 Note: we can not allow the actual FakeModule instances to be deleted,
646 645 because of how Python tears down modules (it hard-sets all their
647 646 references to None without regard for reference counts). This method
648 647 must therefore make a *copy* of the given namespace, to allow the
649 648 original module's __dict__ to be cleared and reused.
650 649
651 650
652 651 Parameters
653 652 ----------
654 653 ns : a namespace (a dict, typically)
655 654
656 655 fname : str
657 656 Filename associated with the namespace.
658 657
659 658 Examples
660 659 --------
661 660
662 661 In [10]: import IPython
663 662
664 663 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
665 664
666 665 In [12]: IPython.__file__ in _ip._main_ns_cache
667 666 Out[12]: True
668 667 """
669 668 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
670 669
671 670 def clear_main_mod_cache(self):
672 671 """Clear the cache of main modules.
673 672
674 673 Mainly for use by utilities like %reset.
675 674
676 675 Examples
677 676 --------
678 677
679 678 In [15]: import IPython
680 679
681 680 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
682 681
683 682 In [17]: len(_ip._main_ns_cache) > 0
684 683 Out[17]: True
685 684
686 685 In [18]: _ip.clear_main_mod_cache()
687 686
688 687 In [19]: len(_ip._main_ns_cache) == 0
689 688 Out[19]: True
690 689 """
691 690 self._main_ns_cache.clear()
692 691
693 692 #-------------------------------------------------------------------------
694 693 # Things related to debugging
695 694 #-------------------------------------------------------------------------
696 695
697 696 def init_pdb(self):
698 697 # Set calling of pdb on exceptions
699 698 # self.call_pdb is a property
700 699 self.call_pdb = self.pdb
701 700
702 701 def _get_call_pdb(self):
703 702 return self._call_pdb
704 703
705 704 def _set_call_pdb(self,val):
706 705
707 706 if val not in (0,1,False,True):
708 707 raise ValueError,'new call_pdb value must be boolean'
709 708
710 709 # store value in instance
711 710 self._call_pdb = val
712 711
713 712 # notify the actual exception handlers
714 713 self.InteractiveTB.call_pdb = val
715 714
716 715 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
717 716 'Control auto-activation of pdb at exceptions')
718 717
719 718 def debugger(self,force=False):
720 719 """Call the pydb/pdb debugger.
721 720
722 721 Keywords:
723 722
724 723 - force(False): by default, this routine checks the instance call_pdb
725 724 flag and does not actually invoke the debugger if the flag is false.
726 725 The 'force' option forces the debugger to activate even if the flag
727 726 is false.
728 727 """
729 728
730 729 if not (force or self.call_pdb):
731 730 return
732 731
733 732 if not hasattr(sys,'last_traceback'):
734 733 error('No traceback has been produced, nothing to debug.')
735 734 return
736 735
737 736 # use pydb if available
738 737 if debugger.has_pydb:
739 738 from pydb import pm
740 739 else:
741 740 # fallback to our internal debugger
742 741 pm = lambda : self.InteractiveTB.debugger(force=True)
743 742 self.history_saving_wrapper(pm)()
744 743
745 744 #-------------------------------------------------------------------------
746 745 # Things related to IPython's various namespaces
747 746 #-------------------------------------------------------------------------
748 747
749 748 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
750 749 # Create the namespace where the user will operate. user_ns is
751 750 # normally the only one used, and it is passed to the exec calls as
752 751 # the locals argument. But we do carry a user_global_ns namespace
753 752 # given as the exec 'globals' argument, This is useful in embedding
754 753 # situations where the ipython shell opens in a context where the
755 754 # distinction between locals and globals is meaningful. For
756 755 # non-embedded contexts, it is just the same object as the user_ns dict.
757 756
758 757 # FIXME. For some strange reason, __builtins__ is showing up at user
759 758 # level as a dict instead of a module. This is a manual fix, but I
760 759 # should really track down where the problem is coming from. Alex
761 760 # Schmolck reported this problem first.
762 761
763 762 # A useful post by Alex Martelli on this topic:
764 763 # Re: inconsistent value from __builtins__
765 764 # Von: Alex Martelli <aleaxit@yahoo.com>
766 765 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
767 766 # Gruppen: comp.lang.python
768 767
769 768 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
770 769 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
771 770 # > <type 'dict'>
772 771 # > >>> print type(__builtins__)
773 772 # > <type 'module'>
774 773 # > Is this difference in return value intentional?
775 774
776 775 # Well, it's documented that '__builtins__' can be either a dictionary
777 776 # or a module, and it's been that way for a long time. Whether it's
778 777 # intentional (or sensible), I don't know. In any case, the idea is
779 778 # that if you need to access the built-in namespace directly, you
780 779 # should start with "import __builtin__" (note, no 's') which will
781 780 # definitely give you a module. Yeah, it's somewhat confusing:-(.
782 781
783 782 # These routines return properly built dicts as needed by the rest of
784 783 # the code, and can also be used by extension writers to generate
785 784 # properly initialized namespaces.
786 785 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
787 786 user_global_ns)
788 787
789 788 # Assign namespaces
790 789 # This is the namespace where all normal user variables live
791 790 self.user_ns = user_ns
792 791 self.user_global_ns = user_global_ns
793 792
794 793 # An auxiliary namespace that checks what parts of the user_ns were
795 794 # loaded at startup, so we can list later only variables defined in
796 795 # actual interactive use. Since it is always a subset of user_ns, it
797 796 # doesn't need to be separately tracked in the ns_table.
798 797 self.user_ns_hidden = {}
799 798
800 799 # A namespace to keep track of internal data structures to prevent
801 800 # them from cluttering user-visible stuff. Will be updated later
802 801 self.internal_ns = {}
803 802
804 803 # Now that FakeModule produces a real module, we've run into a nasty
805 804 # problem: after script execution (via %run), the module where the user
806 805 # code ran is deleted. Now that this object is a true module (needed
807 806 # so docetst and other tools work correctly), the Python module
808 807 # teardown mechanism runs over it, and sets to None every variable
809 808 # present in that module. Top-level references to objects from the
810 809 # script survive, because the user_ns is updated with them. However,
811 810 # calling functions defined in the script that use other things from
812 811 # the script will fail, because the function's closure had references
813 812 # to the original objects, which are now all None. So we must protect
814 813 # these modules from deletion by keeping a cache.
815 814 #
816 815 # To avoid keeping stale modules around (we only need the one from the
817 816 # last run), we use a dict keyed with the full path to the script, so
818 817 # only the last version of the module is held in the cache. Note,
819 818 # however, that we must cache the module *namespace contents* (their
820 819 # __dict__). Because if we try to cache the actual modules, old ones
821 820 # (uncached) could be destroyed while still holding references (such as
822 821 # those held by GUI objects that tend to be long-lived)>
823 822 #
824 823 # The %reset command will flush this cache. See the cache_main_mod()
825 824 # and clear_main_mod_cache() methods for details on use.
826 825
827 826 # This is the cache used for 'main' namespaces
828 827 self._main_ns_cache = {}
829 828 # And this is the single instance of FakeModule whose __dict__ we keep
830 829 # copying and clearing for reuse on each %run
831 830 self._user_main_module = FakeModule()
832 831
833 832 # A table holding all the namespaces IPython deals with, so that
834 833 # introspection facilities can search easily.
835 834 self.ns_table = {'user':user_ns,
836 835 'user_global':user_global_ns,
837 836 'internal':self.internal_ns,
838 837 'builtin':__builtin__.__dict__
839 838 }
840 839
841 840 # Similarly, track all namespaces where references can be held and that
842 841 # we can safely clear (so it can NOT include builtin). This one can be
843 842 # a simple list. Note that the main execution namespaces, user_ns and
844 843 # user_global_ns, can NOT be listed here, as clearing them blindly
845 844 # causes errors in object __del__ methods. Instead, the reset() method
846 845 # clears them manually and carefully.
847 846 self.ns_refs_table = [ self.user_ns_hidden,
848 847 self.internal_ns, self._main_ns_cache ]
849 848
850 849 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
851 850 """Return a valid local and global user interactive namespaces.
852 851
853 852 This builds a dict with the minimal information needed to operate as a
854 853 valid IPython user namespace, which you can pass to the various
855 854 embedding classes in ipython. The default implementation returns the
856 855 same dict for both the locals and the globals to allow functions to
857 856 refer to variables in the namespace. Customized implementations can
858 857 return different dicts. The locals dictionary can actually be anything
859 858 following the basic mapping protocol of a dict, but the globals dict
860 859 must be a true dict, not even a subclass. It is recommended that any
861 860 custom object for the locals namespace synchronize with the globals
862 861 dict somehow.
863 862
864 863 Raises TypeError if the provided globals namespace is not a true dict.
865 864
866 865 Parameters
867 866 ----------
868 867 user_ns : dict-like, optional
869 868 The current user namespace. The items in this namespace should
870 869 be included in the output. If None, an appropriate blank
871 870 namespace should be created.
872 871 user_global_ns : dict, optional
873 872 The current user global namespace. The items in this namespace
874 873 should be included in the output. If None, an appropriate
875 874 blank namespace should be created.
876 875
877 876 Returns
878 877 -------
879 878 A pair of dictionary-like object to be used as the local namespace
880 879 of the interpreter and a dict to be used as the global namespace.
881 880 """
882 881
883 882
884 883 # We must ensure that __builtin__ (without the final 's') is always
885 884 # available and pointing to the __builtin__ *module*. For more details:
886 885 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
887 886
888 887 if user_ns is None:
889 888 # Set __name__ to __main__ to better match the behavior of the
890 889 # normal interpreter.
891 890 user_ns = {'__name__' :'__main__',
892 891 '__builtin__' : __builtin__,
893 892 '__builtins__' : __builtin__,
894 893 }
895 894 else:
896 895 user_ns.setdefault('__name__','__main__')
897 896 user_ns.setdefault('__builtin__',__builtin__)
898 897 user_ns.setdefault('__builtins__',__builtin__)
899 898
900 899 if user_global_ns is None:
901 900 user_global_ns = user_ns
902 901 if type(user_global_ns) is not dict:
903 902 raise TypeError("user_global_ns must be a true dict; got %r"
904 903 % type(user_global_ns))
905 904
906 905 return user_ns, user_global_ns
907 906
908 907 def init_sys_modules(self):
909 908 # We need to insert into sys.modules something that looks like a
910 909 # module but which accesses the IPython namespace, for shelve and
911 910 # pickle to work interactively. Normally they rely on getting
912 911 # everything out of __main__, but for embedding purposes each IPython
913 912 # instance has its own private namespace, so we can't go shoving
914 913 # everything into __main__.
915 914
916 915 # note, however, that we should only do this for non-embedded
917 916 # ipythons, which really mimic the __main__.__dict__ with their own
918 917 # namespace. Embedded instances, on the other hand, should not do
919 918 # this because they need to manage the user local/global namespaces
920 919 # only, but they live within a 'normal' __main__ (meaning, they
921 920 # shouldn't overtake the execution environment of the script they're
922 921 # embedded in).
923 922
924 923 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
925 924
926 925 try:
927 926 main_name = self.user_ns['__name__']
928 927 except KeyError:
929 928 raise KeyError('user_ns dictionary MUST have a "__name__" key')
930 929 else:
931 930 sys.modules[main_name] = FakeModule(self.user_ns)
932 931
933 932 def init_user_ns(self):
934 933 """Initialize all user-visible namespaces to their minimum defaults.
935 934
936 935 Certain history lists are also initialized here, as they effectively
937 936 act as user namespaces.
938 937
939 938 Notes
940 939 -----
941 940 All data structures here are only filled in, they are NOT reset by this
942 941 method. If they were not empty before, data will simply be added to
943 942 therm.
944 943 """
945 944 # This function works in two parts: first we put a few things in
946 945 # user_ns, and we sync that contents into user_ns_hidden so that these
947 946 # initial variables aren't shown by %who. After the sync, we add the
948 947 # rest of what we *do* want the user to see with %who even on a new
949 948 # session (probably nothing, so theye really only see their own stuff)
950 949
951 950 # The user dict must *always* have a __builtin__ reference to the
952 951 # Python standard __builtin__ namespace, which must be imported.
953 952 # This is so that certain operations in prompt evaluation can be
954 953 # reliably executed with builtins. Note that we can NOT use
955 954 # __builtins__ (note the 's'), because that can either be a dict or a
956 955 # module, and can even mutate at runtime, depending on the context
957 956 # (Python makes no guarantees on it). In contrast, __builtin__ is
958 957 # always a module object, though it must be explicitly imported.
959 958
960 959 # For more details:
961 960 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
962 961 ns = dict(__builtin__ = __builtin__)
963 962
964 963 # Put 'help' in the user namespace
965 964 try:
966 965 from site import _Helper
967 966 ns['help'] = _Helper()
968 967 except ImportError:
969 968 warn('help() not available - check site.py')
970 969
971 970 # make global variables for user access to the histories
972 971 ns['_ih'] = self.history_manager.input_hist_parsed
973 972 ns['_oh'] = self.history_manager.output_hist
974 973 ns['_dh'] = self.history_manager.dir_hist
975 974
976 975 ns['_sh'] = shadowns
977 976
978 977 # user aliases to input and output histories. These shouldn't show up
979 978 # in %who, as they can have very large reprs.
980 979 ns['In'] = self.history_manager.input_hist_parsed
981 980 ns['Out'] = self.history_manager.output_hist
982 981
983 982 # Store myself as the public api!!!
984 983 ns['get_ipython'] = self.get_ipython
985 984
986 985 # Sync what we've added so far to user_ns_hidden so these aren't seen
987 986 # by %who
988 987 self.user_ns_hidden.update(ns)
989 988
990 989 # Anything put into ns now would show up in %who. Think twice before
991 990 # putting anything here, as we really want %who to show the user their
992 991 # stuff, not our variables.
993 992
994 993 # Finally, update the real user's namespace
995 994 self.user_ns.update(ns)
996 995
997 996 def reset(self):
998 997 """Clear all internal namespaces.
999 998
1000 999 Note that this is much more aggressive than %reset, since it clears
1001 1000 fully all namespaces, as well as all input/output lists.
1002 1001 """
1003 1002 # Clear histories
1004 1003 self.history_manager.reset()
1005 1004
1006 1005 # Reset counter used to index all histories
1007 1006 self.execution_count = 0
1008 1007
1009 1008 # Restore the user namespaces to minimal usability
1010 1009 for ns in self.ns_refs_table:
1011 1010 ns.clear()
1012 1011
1013 1012 # The main execution namespaces must be cleared very carefully,
1014 1013 # skipping the deletion of the builtin-related keys, because doing so
1015 1014 # would cause errors in many object's __del__ methods.
1016 1015 for ns in [self.user_ns, self.user_global_ns]:
1017 1016 drop_keys = set(ns.keys())
1018 1017 drop_keys.discard('__builtin__')
1019 1018 drop_keys.discard('__builtins__')
1020 1019 for k in drop_keys:
1021 1020 del ns[k]
1022 1021
1023 1022 # Restore the user namespaces to minimal usability
1024 1023 self.init_user_ns()
1025 1024
1026 1025 # Restore the default and user aliases
1027 1026 self.alias_manager.clear_aliases()
1028 1027 self.alias_manager.init_aliases()
1029 1028
1030 1029 def reset_selective(self, regex=None):
1031 1030 """Clear selective variables from internal namespaces based on a
1032 1031 specified regular expression.
1033 1032
1034 1033 Parameters
1035 1034 ----------
1036 1035 regex : string or compiled pattern, optional
1037 1036 A regular expression pattern that will be used in searching
1038 1037 variable names in the users namespaces.
1039 1038 """
1040 1039 if regex is not None:
1041 1040 try:
1042 1041 m = re.compile(regex)
1043 1042 except TypeError:
1044 1043 raise TypeError('regex must be a string or compiled pattern')
1045 1044 # Search for keys in each namespace that match the given regex
1046 1045 # If a match is found, delete the key/value pair.
1047 1046 for ns in self.ns_refs_table:
1048 1047 for var in ns:
1049 1048 if m.search(var):
1050 1049 del ns[var]
1051 1050
1052 1051 def push(self, variables, interactive=True):
1053 1052 """Inject a group of variables into the IPython user namespace.
1054 1053
1055 1054 Parameters
1056 1055 ----------
1057 1056 variables : dict, str or list/tuple of str
1058 1057 The variables to inject into the user's namespace. If a dict, a
1059 1058 simple update is done. If a str, the string is assumed to have
1060 1059 variable names separated by spaces. A list/tuple of str can also
1061 1060 be used to give the variable names. If just the variable names are
1062 1061 give (list/tuple/str) then the variable values looked up in the
1063 1062 callers frame.
1064 1063 interactive : bool
1065 1064 If True (default), the variables will be listed with the ``who``
1066 1065 magic.
1067 1066 """
1068 1067 vdict = None
1069 1068
1070 1069 # We need a dict of name/value pairs to do namespace updates.
1071 1070 if isinstance(variables, dict):
1072 1071 vdict = variables
1073 1072 elif isinstance(variables, (basestring, list, tuple)):
1074 1073 if isinstance(variables, basestring):
1075 1074 vlist = variables.split()
1076 1075 else:
1077 1076 vlist = variables
1078 1077 vdict = {}
1079 1078 cf = sys._getframe(1)
1080 1079 for name in vlist:
1081 1080 try:
1082 1081 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1083 1082 except:
1084 1083 print ('Could not get variable %s from %s' %
1085 1084 (name,cf.f_code.co_name))
1086 1085 else:
1087 1086 raise ValueError('variables must be a dict/str/list/tuple')
1088 1087
1089 1088 # Propagate variables to user namespace
1090 1089 self.user_ns.update(vdict)
1091 1090
1092 1091 # And configure interactive visibility
1093 1092 config_ns = self.user_ns_hidden
1094 1093 if interactive:
1095 1094 for name, val in vdict.iteritems():
1096 1095 config_ns.pop(name, None)
1097 1096 else:
1098 1097 for name,val in vdict.iteritems():
1099 1098 config_ns[name] = val
1100 1099
1101 1100 #-------------------------------------------------------------------------
1102 1101 # Things related to object introspection
1103 1102 #-------------------------------------------------------------------------
1104 1103
1105 1104 def _ofind(self, oname, namespaces=None):
1106 1105 """Find an object in the available namespaces.
1107 1106
1108 1107 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1109 1108
1110 1109 Has special code to detect magic functions.
1111 1110 """
1112 1111 #oname = oname.strip()
1113 1112 #print '1- oname: <%r>' % oname # dbg
1114 1113 try:
1115 1114 oname = oname.strip().encode('ascii')
1116 1115 #print '2- oname: <%r>' % oname # dbg
1117 1116 except UnicodeEncodeError:
1118 1117 print 'Python identifiers can only contain ascii characters.'
1119 1118 return dict(found=False)
1120 1119
1121 1120 alias_ns = None
1122 1121 if namespaces is None:
1123 1122 # Namespaces to search in:
1124 1123 # Put them in a list. The order is important so that we
1125 1124 # find things in the same order that Python finds them.
1126 1125 namespaces = [ ('Interactive', self.user_ns),
1127 1126 ('IPython internal', self.internal_ns),
1128 1127 ('Python builtin', __builtin__.__dict__),
1129 1128 ('Alias', self.alias_manager.alias_table),
1130 1129 ]
1131 1130 alias_ns = self.alias_manager.alias_table
1132 1131
1133 1132 # initialize results to 'null'
1134 1133 found = False; obj = None; ospace = None; ds = None;
1135 1134 ismagic = False; isalias = False; parent = None
1136 1135
1137 1136 # We need to special-case 'print', which as of python2.6 registers as a
1138 1137 # function but should only be treated as one if print_function was
1139 1138 # loaded with a future import. In this case, just bail.
1140 1139 if (oname == 'print' and not (self.compile.compiler_flags &
1141 1140 __future__.CO_FUTURE_PRINT_FUNCTION)):
1142 1141 return {'found':found, 'obj':obj, 'namespace':ospace,
1143 1142 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1144 1143
1145 1144 # Look for the given name by splitting it in parts. If the head is
1146 1145 # found, then we look for all the remaining parts as members, and only
1147 1146 # declare success if we can find them all.
1148 1147 oname_parts = oname.split('.')
1149 1148 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1150 1149 for nsname,ns in namespaces:
1151 1150 try:
1152 1151 obj = ns[oname_head]
1153 1152 except KeyError:
1154 1153 continue
1155 1154 else:
1156 1155 #print 'oname_rest:', oname_rest # dbg
1157 1156 for part in oname_rest:
1158 1157 try:
1159 1158 parent = obj
1160 1159 obj = getattr(obj,part)
1161 1160 except:
1162 1161 # Blanket except b/c some badly implemented objects
1163 1162 # allow __getattr__ to raise exceptions other than
1164 1163 # AttributeError, which then crashes IPython.
1165 1164 break
1166 1165 else:
1167 1166 # If we finish the for loop (no break), we got all members
1168 1167 found = True
1169 1168 ospace = nsname
1170 1169 if ns == alias_ns:
1171 1170 isalias = True
1172 1171 break # namespace loop
1173 1172
1174 1173 # Try to see if it's magic
1175 1174 if not found:
1176 1175 if oname.startswith(ESC_MAGIC):
1177 1176 oname = oname[1:]
1178 1177 obj = getattr(self,'magic_'+oname,None)
1179 1178 if obj is not None:
1180 1179 found = True
1181 1180 ospace = 'IPython internal'
1182 1181 ismagic = True
1183 1182
1184 1183 # Last try: special-case some literals like '', [], {}, etc:
1185 1184 if not found and oname_head in ["''",'""','[]','{}','()']:
1186 1185 obj = eval(oname_head)
1187 1186 found = True
1188 1187 ospace = 'Interactive'
1189 1188
1190 1189 return {'found':found, 'obj':obj, 'namespace':ospace,
1191 1190 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1192 1191
1193 1192 def _ofind_property(self, oname, info):
1194 1193 """Second part of object finding, to look for property details."""
1195 1194 if info.found:
1196 1195 # Get the docstring of the class property if it exists.
1197 1196 path = oname.split('.')
1198 1197 root = '.'.join(path[:-1])
1199 1198 if info.parent is not None:
1200 1199 try:
1201 1200 target = getattr(info.parent, '__class__')
1202 1201 # The object belongs to a class instance.
1203 1202 try:
1204 1203 target = getattr(target, path[-1])
1205 1204 # The class defines the object.
1206 1205 if isinstance(target, property):
1207 1206 oname = root + '.__class__.' + path[-1]
1208 1207 info = Struct(self._ofind(oname))
1209 1208 except AttributeError: pass
1210 1209 except AttributeError: pass
1211 1210
1212 1211 # We return either the new info or the unmodified input if the object
1213 1212 # hadn't been found
1214 1213 return info
1215 1214
1216 1215 def _object_find(self, oname, namespaces=None):
1217 1216 """Find an object and return a struct with info about it."""
1218 1217 inf = Struct(self._ofind(oname, namespaces))
1219 1218 return Struct(self._ofind_property(oname, inf))
1220 1219
1221 1220 def _inspect(self, meth, oname, namespaces=None, **kw):
1222 1221 """Generic interface to the inspector system.
1223 1222
1224 1223 This function is meant to be called by pdef, pdoc & friends."""
1225 1224 info = self._object_find(oname)
1226 1225 if info.found:
1227 1226 pmethod = getattr(self.inspector, meth)
1228 1227 formatter = format_screen if info.ismagic else None
1229 1228 if meth == 'pdoc':
1230 1229 pmethod(info.obj, oname, formatter)
1231 1230 elif meth == 'pinfo':
1232 1231 pmethod(info.obj, oname, formatter, info, **kw)
1233 1232 else:
1234 1233 pmethod(info.obj, oname)
1235 1234 else:
1236 1235 print 'Object `%s` not found.' % oname
1237 1236 return 'not found' # so callers can take other action
1238 1237
1239 1238 def object_inspect(self, oname):
1240 1239 info = self._object_find(oname)
1241 1240 if info.found:
1242 1241 return self.inspector.info(info.obj, oname, info=info)
1243 1242 else:
1244 1243 return oinspect.object_info(name=oname, found=False)
1245 1244
1246 1245 #-------------------------------------------------------------------------
1247 1246 # Things related to history management
1248 1247 #-------------------------------------------------------------------------
1249 1248
1250 1249 def init_history(self):
1251 1250 """Sets up the command history, and starts regular autosaves."""
1252 1251 self.history_manager = HistoryManager(shell=self)
1253 1252
1254 1253 def save_history(self):
1255 1254 """Save input history to a file (via readline library)."""
1256 1255 self.history_manager.save_history()
1257 1256
1258 1257 def reload_history(self):
1259 1258 """Reload the input history from disk file."""
1260 1259 self.history_manager.reload_history()
1261 1260
1262 1261 def history_saving_wrapper(self, func):
1263 1262 """ Wrap func for readline history saving
1264 1263
1265 1264 Convert func into callable that saves & restores
1266 1265 history around the call """
1267 1266
1268 1267 if self.has_readline:
1269 1268 from IPython.utils import rlineimpl as readline
1270 1269 else:
1271 1270 return func
1272 1271
1273 1272 def wrapper():
1274 1273 self.save_history()
1275 1274 try:
1276 1275 func()
1277 1276 finally:
1278 1277 self.reload_history()
1279 1278 return wrapper
1280 1279
1281 1280 def get_history(self, index=None, raw=False, output=True):
1282 1281 return self.history_manager.get_history(index, raw, output)
1283 1282
1284 1283
1285 1284 #-------------------------------------------------------------------------
1286 1285 # Things related to exception handling and tracebacks (not debugging)
1287 1286 #-------------------------------------------------------------------------
1288 1287
1289 1288 def init_traceback_handlers(self, custom_exceptions):
1290 1289 # Syntax error handler.
1291 1290 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1292 1291
1293 1292 # The interactive one is initialized with an offset, meaning we always
1294 1293 # want to remove the topmost item in the traceback, which is our own
1295 1294 # internal code. Valid modes: ['Plain','Context','Verbose']
1296 1295 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1297 1296 color_scheme='NoColor',
1298 1297 tb_offset = 1,
1299 1298 check_cache=self.compile.check_cache)
1300 1299
1301 1300 # The instance will store a pointer to the system-wide exception hook,
1302 1301 # so that runtime code (such as magics) can access it. This is because
1303 1302 # during the read-eval loop, it may get temporarily overwritten.
1304 1303 self.sys_excepthook = sys.excepthook
1305 1304
1306 1305 # and add any custom exception handlers the user may have specified
1307 1306 self.set_custom_exc(*custom_exceptions)
1308 1307
1309 1308 # Set the exception mode
1310 1309 self.InteractiveTB.set_mode(mode=self.xmode)
1311 1310
1312 1311 def set_custom_exc(self, exc_tuple, handler):
1313 1312 """set_custom_exc(exc_tuple,handler)
1314 1313
1315 1314 Set a custom exception handler, which will be called if any of the
1316 1315 exceptions in exc_tuple occur in the mainloop (specifically, in the
1317 1316 run_code() method.
1318 1317
1319 1318 Inputs:
1320 1319
1321 1320 - exc_tuple: a *tuple* of valid exceptions to call the defined
1322 1321 handler for. It is very important that you use a tuple, and NOT A
1323 1322 LIST here, because of the way Python's except statement works. If
1324 1323 you only want to trap a single exception, use a singleton tuple:
1325 1324
1326 1325 exc_tuple == (MyCustomException,)
1327 1326
1328 1327 - handler: this must be defined as a function with the following
1329 1328 basic interface::
1330 1329
1331 1330 def my_handler(self, etype, value, tb, tb_offset=None)
1332 1331 ...
1333 1332 # The return value must be
1334 1333 return structured_traceback
1335 1334
1336 1335 This will be made into an instance method (via types.MethodType)
1337 1336 of IPython itself, and it will be called if any of the exceptions
1338 1337 listed in the exc_tuple are caught. If the handler is None, an
1339 1338 internal basic one is used, which just prints basic info.
1340 1339
1341 1340 WARNING: by putting in your own exception handler into IPython's main
1342 1341 execution loop, you run a very good chance of nasty crashes. This
1343 1342 facility should only be used if you really know what you are doing."""
1344 1343
1345 1344 assert type(exc_tuple)==type(()) , \
1346 1345 "The custom exceptions must be given AS A TUPLE."
1347 1346
1348 1347 def dummy_handler(self,etype,value,tb):
1349 1348 print '*** Simple custom exception handler ***'
1350 1349 print 'Exception type :',etype
1351 1350 print 'Exception value:',value
1352 1351 print 'Traceback :',tb
1353 1352 print 'Source code :','\n'.join(self.buffer)
1354 1353
1355 1354 if handler is None: handler = dummy_handler
1356 1355
1357 1356 self.CustomTB = types.MethodType(handler,self)
1358 1357 self.custom_exceptions = exc_tuple
1359 1358
1360 1359 def excepthook(self, etype, value, tb):
1361 1360 """One more defense for GUI apps that call sys.excepthook.
1362 1361
1363 1362 GUI frameworks like wxPython trap exceptions and call
1364 1363 sys.excepthook themselves. I guess this is a feature that
1365 1364 enables them to keep running after exceptions that would
1366 1365 otherwise kill their mainloop. This is a bother for IPython
1367 1366 which excepts to catch all of the program exceptions with a try:
1368 1367 except: statement.
1369 1368
1370 1369 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1371 1370 any app directly invokes sys.excepthook, it will look to the user like
1372 1371 IPython crashed. In order to work around this, we can disable the
1373 1372 CrashHandler and replace it with this excepthook instead, which prints a
1374 1373 regular traceback using our InteractiveTB. In this fashion, apps which
1375 1374 call sys.excepthook will generate a regular-looking exception from
1376 1375 IPython, and the CrashHandler will only be triggered by real IPython
1377 1376 crashes.
1378 1377
1379 1378 This hook should be used sparingly, only in places which are not likely
1380 1379 to be true IPython errors.
1381 1380 """
1382 1381 self.showtraceback((etype,value,tb),tb_offset=0)
1383 1382
1384 1383 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1385 1384 exception_only=False):
1386 1385 """Display the exception that just occurred.
1387 1386
1388 1387 If nothing is known about the exception, this is the method which
1389 1388 should be used throughout the code for presenting user tracebacks,
1390 1389 rather than directly invoking the InteractiveTB object.
1391 1390
1392 1391 A specific showsyntaxerror() also exists, but this method can take
1393 1392 care of calling it if needed, so unless you are explicitly catching a
1394 1393 SyntaxError exception, don't try to analyze the stack manually and
1395 1394 simply call this method."""
1396 1395
1397 1396 try:
1398 1397 if exc_tuple is None:
1399 1398 etype, value, tb = sys.exc_info()
1400 1399 else:
1401 1400 etype, value, tb = exc_tuple
1402 1401
1403 1402 if etype is None:
1404 1403 if hasattr(sys, 'last_type'):
1405 1404 etype, value, tb = sys.last_type, sys.last_value, \
1406 1405 sys.last_traceback
1407 1406 else:
1408 1407 self.write_err('No traceback available to show.\n')
1409 1408 return
1410 1409
1411 1410 if etype is SyntaxError:
1412 1411 # Though this won't be called by syntax errors in the input
1413 1412 # line, there may be SyntaxError cases whith imported code.
1414 1413 self.showsyntaxerror(filename)
1415 1414 elif etype is UsageError:
1416 1415 print "UsageError:", value
1417 1416 else:
1418 1417 # WARNING: these variables are somewhat deprecated and not
1419 1418 # necessarily safe to use in a threaded environment, but tools
1420 1419 # like pdb depend on their existence, so let's set them. If we
1421 1420 # find problems in the field, we'll need to revisit their use.
1422 1421 sys.last_type = etype
1423 1422 sys.last_value = value
1424 1423 sys.last_traceback = tb
1425 1424
1426 1425 if etype in self.custom_exceptions:
1427 1426 # FIXME: Old custom traceback objects may just return a
1428 1427 # string, in that case we just put it into a list
1429 1428 stb = self.CustomTB(etype, value, tb, tb_offset)
1430 1429 if isinstance(ctb, basestring):
1431 1430 stb = [stb]
1432 1431 else:
1433 1432 if exception_only:
1434 1433 stb = ['An exception has occurred, use %tb to see '
1435 1434 'the full traceback.\n']
1436 1435 stb.extend(self.InteractiveTB.get_exception_only(etype,
1437 1436 value))
1438 1437 else:
1439 1438 stb = self.InteractiveTB.structured_traceback(etype,
1440 1439 value, tb, tb_offset=tb_offset)
1441 1440 # FIXME: the pdb calling should be done by us, not by
1442 1441 # the code computing the traceback.
1443 1442 if self.InteractiveTB.call_pdb:
1444 1443 # pdb mucks up readline, fix it back
1445 1444 self.set_readline_completer()
1446 1445
1447 1446 # Actually show the traceback
1448 1447 self._showtraceback(etype, value, stb)
1449 1448
1450 1449 except KeyboardInterrupt:
1451 1450 self.write_err("\nKeyboardInterrupt\n")
1452 1451
1453 1452 def _showtraceback(self, etype, evalue, stb):
1454 1453 """Actually show a traceback.
1455 1454
1456 1455 Subclasses may override this method to put the traceback on a different
1457 1456 place, like a side channel.
1458 1457 """
1459 1458 print >> io.Term.cout, self.InteractiveTB.stb2text(stb)
1460 1459
1461 1460 def showsyntaxerror(self, filename=None):
1462 1461 """Display the syntax error that just occurred.
1463 1462
1464 1463 This doesn't display a stack trace because there isn't one.
1465 1464
1466 1465 If a filename is given, it is stuffed in the exception instead
1467 1466 of what was there before (because Python's parser always uses
1468 1467 "<string>" when reading from a string).
1469 1468 """
1470 1469 etype, value, last_traceback = sys.exc_info()
1471 1470
1472 1471 # See note about these variables in showtraceback() above
1473 1472 sys.last_type = etype
1474 1473 sys.last_value = value
1475 1474 sys.last_traceback = last_traceback
1476 1475
1477 1476 if filename and etype is SyntaxError:
1478 1477 # Work hard to stuff the correct filename in the exception
1479 1478 try:
1480 1479 msg, (dummy_filename, lineno, offset, line) = value
1481 1480 except:
1482 1481 # Not the format we expect; leave it alone
1483 1482 pass
1484 1483 else:
1485 1484 # Stuff in the right filename
1486 1485 try:
1487 1486 # Assume SyntaxError is a class exception
1488 1487 value = SyntaxError(msg, (filename, lineno, offset, line))
1489 1488 except:
1490 1489 # If that failed, assume SyntaxError is a string
1491 1490 value = msg, (filename, lineno, offset, line)
1492 1491 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1493 1492 self._showtraceback(etype, value, stb)
1494 1493
1495 1494 #-------------------------------------------------------------------------
1496 1495 # Things related to readline
1497 1496 #-------------------------------------------------------------------------
1498 1497
1499 1498 def init_readline(self):
1500 1499 """Command history completion/saving/reloading."""
1501 1500
1502 1501 if self.readline_use:
1503 1502 import IPython.utils.rlineimpl as readline
1504 1503
1505 1504 self.rl_next_input = None
1506 1505 self.rl_do_indent = False
1507 1506
1508 1507 if not self.readline_use or not readline.have_readline:
1509 1508 self.has_readline = False
1510 1509 self.readline = None
1511 1510 # Set a number of methods that depend on readline to be no-op
1512 1511 self.set_readline_completer = no_op
1513 1512 self.set_custom_completer = no_op
1514 1513 self.set_completer_frame = no_op
1515 1514 warn('Readline services not available or not loaded.')
1516 1515 else:
1517 1516 self.has_readline = True
1518 1517 self.readline = readline
1519 1518 sys.modules['readline'] = readline
1520 1519
1521 1520 # Platform-specific configuration
1522 1521 if os.name == 'nt':
1523 1522 # FIXME - check with Frederick to see if we can harmonize
1524 1523 # naming conventions with pyreadline to avoid this
1525 1524 # platform-dependent check
1526 1525 self.readline_startup_hook = readline.set_pre_input_hook
1527 1526 else:
1528 1527 self.readline_startup_hook = readline.set_startup_hook
1529 1528
1530 1529 # Load user's initrc file (readline config)
1531 1530 # Or if libedit is used, load editrc.
1532 1531 inputrc_name = os.environ.get('INPUTRC')
1533 1532 if inputrc_name is None:
1534 1533 home_dir = get_home_dir()
1535 1534 if home_dir is not None:
1536 1535 inputrc_name = '.inputrc'
1537 1536 if readline.uses_libedit:
1538 1537 inputrc_name = '.editrc'
1539 1538 inputrc_name = os.path.join(home_dir, inputrc_name)
1540 1539 if os.path.isfile(inputrc_name):
1541 1540 try:
1542 1541 readline.read_init_file(inputrc_name)
1543 1542 except:
1544 1543 warn('Problems reading readline initialization file <%s>'
1545 1544 % inputrc_name)
1546 1545
1547 1546 # Configure readline according to user's prefs
1548 1547 # This is only done if GNU readline is being used. If libedit
1549 1548 # is being used (as on Leopard) the readline config is
1550 1549 # not run as the syntax for libedit is different.
1551 1550 if not readline.uses_libedit:
1552 1551 for rlcommand in self.readline_parse_and_bind:
1553 1552 #print "loading rl:",rlcommand # dbg
1554 1553 readline.parse_and_bind(rlcommand)
1555 1554
1556 1555 # Remove some chars from the delimiters list. If we encounter
1557 1556 # unicode chars, discard them.
1558 1557 delims = readline.get_completer_delims().encode("ascii", "ignore")
1559 1558 delims = delims.translate(None, self.readline_remove_delims)
1560 1559 delims = delims.replace(ESC_MAGIC, '')
1561 1560 readline.set_completer_delims(delims)
1562 1561 # otherwise we end up with a monster history after a while:
1563 1562 readline.set_history_length(self.history_length)
1564 1563 try:
1565 1564 #print '*** Reading readline history' # dbg
1566 1565 self.reload_history()
1567 1566 except IOError:
1568 1567 pass # It doesn't exist yet.
1569 1568
1570 1569 # Configure auto-indent for all platforms
1571 1570 self.set_autoindent(self.autoindent)
1572 1571
1573 1572 def set_next_input(self, s):
1574 1573 """ Sets the 'default' input string for the next command line.
1575 1574
1576 1575 Requires readline.
1577 1576
1578 1577 Example:
1579 1578
1580 1579 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1581 1580 [D:\ipython]|2> Hello Word_ # cursor is here
1582 1581 """
1583 1582
1584 1583 self.rl_next_input = s
1585 1584
1586 1585 # Maybe move this to the terminal subclass?
1587 1586 def pre_readline(self):
1588 1587 """readline hook to be used at the start of each line.
1589 1588
1590 1589 Currently it handles auto-indent only."""
1591 1590
1592 1591 if self.rl_do_indent:
1593 1592 self.readline.insert_text(self._indent_current_str())
1594 1593 if self.rl_next_input is not None:
1595 1594 self.readline.insert_text(self.rl_next_input)
1596 1595 self.rl_next_input = None
1597 1596
1598 1597 def _indent_current_str(self):
1599 1598 """return the current level of indentation as a string"""
1600 1599 return self.input_splitter.indent_spaces * ' '
1601 1600
1602 1601 #-------------------------------------------------------------------------
1603 1602 # Things related to text completion
1604 1603 #-------------------------------------------------------------------------
1605 1604
1606 1605 def init_completer(self):
1607 1606 """Initialize the completion machinery.
1608 1607
1609 1608 This creates completion machinery that can be used by client code,
1610 1609 either interactively in-process (typically triggered by the readline
1611 1610 library), programatically (such as in test suites) or out-of-prcess
1612 1611 (typically over the network by remote frontends).
1613 1612 """
1614 1613 from IPython.core.completer import IPCompleter
1615 1614 from IPython.core.completerlib import (module_completer,
1616 1615 magic_run_completer, cd_completer)
1617 1616
1618 1617 self.Completer = IPCompleter(self,
1619 1618 self.user_ns,
1620 1619 self.user_global_ns,
1621 1620 self.readline_omit__names,
1622 1621 self.alias_manager.alias_table,
1623 1622 self.has_readline)
1624 1623
1625 1624 # Add custom completers to the basic ones built into IPCompleter
1626 1625 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1627 1626 self.strdispatchers['complete_command'] = sdisp
1628 1627 self.Completer.custom_completers = sdisp
1629 1628
1630 1629 self.set_hook('complete_command', module_completer, str_key = 'import')
1631 1630 self.set_hook('complete_command', module_completer, str_key = 'from')
1632 1631 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1633 1632 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1634 1633
1635 1634 # Only configure readline if we truly are using readline. IPython can
1636 1635 # do tab-completion over the network, in GUIs, etc, where readline
1637 1636 # itself may be absent
1638 1637 if self.has_readline:
1639 1638 self.set_readline_completer()
1640 1639
1641 1640 def complete(self, text, line=None, cursor_pos=None):
1642 1641 """Return the completed text and a list of completions.
1643 1642
1644 1643 Parameters
1645 1644 ----------
1646 1645
1647 1646 text : string
1648 1647 A string of text to be completed on. It can be given as empty and
1649 1648 instead a line/position pair are given. In this case, the
1650 1649 completer itself will split the line like readline does.
1651 1650
1652 1651 line : string, optional
1653 1652 The complete line that text is part of.
1654 1653
1655 1654 cursor_pos : int, optional
1656 1655 The position of the cursor on the input line.
1657 1656
1658 1657 Returns
1659 1658 -------
1660 1659 text : string
1661 1660 The actual text that was completed.
1662 1661
1663 1662 matches : list
1664 1663 A sorted list with all possible completions.
1665 1664
1666 1665 The optional arguments allow the completion to take more context into
1667 1666 account, and are part of the low-level completion API.
1668 1667
1669 1668 This is a wrapper around the completion mechanism, similar to what
1670 1669 readline does at the command line when the TAB key is hit. By
1671 1670 exposing it as a method, it can be used by other non-readline
1672 1671 environments (such as GUIs) for text completion.
1673 1672
1674 1673 Simple usage example:
1675 1674
1676 1675 In [1]: x = 'hello'
1677 1676
1678 1677 In [2]: _ip.complete('x.l')
1679 1678 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1680 1679 """
1681 1680
1682 1681 # Inject names into __builtin__ so we can complete on the added names.
1683 1682 with self.builtin_trap:
1684 1683 return self.Completer.complete(text, line, cursor_pos)
1685 1684
1686 1685 def set_custom_completer(self, completer, pos=0):
1687 1686 """Adds a new custom completer function.
1688 1687
1689 1688 The position argument (defaults to 0) is the index in the completers
1690 1689 list where you want the completer to be inserted."""
1691 1690
1692 1691 newcomp = types.MethodType(completer,self.Completer)
1693 1692 self.Completer.matchers.insert(pos,newcomp)
1694 1693
1695 1694 def set_readline_completer(self):
1696 1695 """Reset readline's completer to be our own."""
1697 1696 self.readline.set_completer(self.Completer.rlcomplete)
1698 1697
1699 1698 def set_completer_frame(self, frame=None):
1700 1699 """Set the frame of the completer."""
1701 1700 if frame:
1702 1701 self.Completer.namespace = frame.f_locals
1703 1702 self.Completer.global_namespace = frame.f_globals
1704 1703 else:
1705 1704 self.Completer.namespace = self.user_ns
1706 1705 self.Completer.global_namespace = self.user_global_ns
1707 1706
1708 1707 #-------------------------------------------------------------------------
1709 1708 # Things related to magics
1710 1709 #-------------------------------------------------------------------------
1711 1710
1712 1711 def init_magics(self):
1713 1712 # FIXME: Move the color initialization to the DisplayHook, which
1714 1713 # should be split into a prompt manager and displayhook. We probably
1715 1714 # even need a centralize colors management object.
1716 1715 self.magic_colors(self.colors)
1717 1716 # History was moved to a separate module
1718 1717 from . import history
1719 1718 history.init_ipython(self)
1720 1719
1721 1720 def magic(self,arg_s):
1722 1721 """Call a magic function by name.
1723 1722
1724 1723 Input: a string containing the name of the magic function to call and
1725 1724 any additional arguments to be passed to the magic.
1726 1725
1727 1726 magic('name -opt foo bar') is equivalent to typing at the ipython
1728 1727 prompt:
1729 1728
1730 1729 In[1]: %name -opt foo bar
1731 1730
1732 1731 To call a magic without arguments, simply use magic('name').
1733 1732
1734 1733 This provides a proper Python function to call IPython's magics in any
1735 1734 valid Python code you can type at the interpreter, including loops and
1736 1735 compound statements.
1737 1736 """
1738 1737 args = arg_s.split(' ',1)
1739 1738 magic_name = args[0]
1740 1739 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1741 1740
1742 1741 try:
1743 1742 magic_args = args[1]
1744 1743 except IndexError:
1745 1744 magic_args = ''
1746 1745 fn = getattr(self,'magic_'+magic_name,None)
1747 1746 if fn is None:
1748 1747 error("Magic function `%s` not found." % magic_name)
1749 1748 else:
1750 1749 magic_args = self.var_expand(magic_args,1)
1751 1750 with nested(self.builtin_trap,):
1752 1751 result = fn(magic_args)
1753 1752 return result
1754 1753
1755 1754 def define_magic(self, magicname, func):
1756 1755 """Expose own function as magic function for ipython
1757 1756
1758 1757 def foo_impl(self,parameter_s=''):
1759 1758 'My very own magic!. (Use docstrings, IPython reads them).'
1760 1759 print 'Magic function. Passed parameter is between < >:'
1761 1760 print '<%s>' % parameter_s
1762 1761 print 'The self object is:',self
1763 1762
1764 1763 self.define_magic('foo',foo_impl)
1765 1764 """
1766 1765
1767 1766 import new
1768 1767 im = types.MethodType(func,self)
1769 1768 old = getattr(self, "magic_" + magicname, None)
1770 1769 setattr(self, "magic_" + magicname, im)
1771 1770 return old
1772 1771
1773 1772 #-------------------------------------------------------------------------
1774 1773 # Things related to macros
1775 1774 #-------------------------------------------------------------------------
1776 1775
1777 1776 def define_macro(self, name, themacro):
1778 1777 """Define a new macro
1779 1778
1780 1779 Parameters
1781 1780 ----------
1782 1781 name : str
1783 1782 The name of the macro.
1784 1783 themacro : str or Macro
1785 1784 The action to do upon invoking the macro. If a string, a new
1786 1785 Macro object is created by passing the string to it.
1787 1786 """
1788 1787
1789 1788 from IPython.core import macro
1790 1789
1791 1790 if isinstance(themacro, basestring):
1792 1791 themacro = macro.Macro(themacro)
1793 1792 if not isinstance(themacro, macro.Macro):
1794 1793 raise ValueError('A macro must be a string or a Macro instance.')
1795 1794 self.user_ns[name] = themacro
1796 1795
1797 1796 #-------------------------------------------------------------------------
1798 1797 # Things related to the running of system commands
1799 1798 #-------------------------------------------------------------------------
1800 1799
1801 1800 def system(self, cmd):
1802 1801 """Call the given cmd in a subprocess.
1803 1802
1804 1803 Parameters
1805 1804 ----------
1806 1805 cmd : str
1807 1806 Command to execute (can not end in '&', as bacground processes are
1808 1807 not supported.
1809 1808 """
1810 1809 # We do not support backgrounding processes because we either use
1811 1810 # pexpect or pipes to read from. Users can always just call
1812 1811 # os.system() if they really want a background process.
1813 1812 if cmd.endswith('&'):
1814 1813 raise OSError("Background processes not supported.")
1815 1814
1816 1815 return system(self.var_expand(cmd, depth=2))
1817 1816
1818 1817 def getoutput(self, cmd, split=True):
1819 1818 """Get output (possibly including stderr) from a subprocess.
1820 1819
1821 1820 Parameters
1822 1821 ----------
1823 1822 cmd : str
1824 1823 Command to execute (can not end in '&', as background processes are
1825 1824 not supported.
1826 1825 split : bool, optional
1827 1826
1828 1827 If True, split the output into an IPython SList. Otherwise, an
1829 1828 IPython LSString is returned. These are objects similar to normal
1830 1829 lists and strings, with a few convenience attributes for easier
1831 1830 manipulation of line-based output. You can use '?' on them for
1832 1831 details.
1833 1832 """
1834 1833 if cmd.endswith('&'):
1835 1834 raise OSError("Background processes not supported.")
1836 1835 out = getoutput(self.var_expand(cmd, depth=2))
1837 1836 if split:
1838 1837 out = SList(out.splitlines())
1839 1838 else:
1840 1839 out = LSString(out)
1841 1840 return out
1842 1841
1843 1842 #-------------------------------------------------------------------------
1844 1843 # Things related to aliases
1845 1844 #-------------------------------------------------------------------------
1846 1845
1847 1846 def init_alias(self):
1848 1847 self.alias_manager = AliasManager(shell=self, config=self.config)
1849 1848 self.ns_table['alias'] = self.alias_manager.alias_table,
1850 1849
1851 1850 #-------------------------------------------------------------------------
1852 1851 # Things related to extensions and plugins
1853 1852 #-------------------------------------------------------------------------
1854 1853
1855 1854 def init_extension_manager(self):
1856 1855 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1857 1856
1858 1857 def init_plugin_manager(self):
1859 1858 self.plugin_manager = PluginManager(config=self.config)
1860 1859
1861 1860 #-------------------------------------------------------------------------
1862 1861 # Things related to payloads
1863 1862 #-------------------------------------------------------------------------
1864 1863
1865 1864 def init_payload(self):
1866 1865 self.payload_manager = PayloadManager(config=self.config)
1867 1866
1868 1867 #-------------------------------------------------------------------------
1869 1868 # Things related to the prefilter
1870 1869 #-------------------------------------------------------------------------
1871 1870
1872 1871 def init_prefilter(self):
1873 1872 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
1874 1873 # Ultimately this will be refactored in the new interpreter code, but
1875 1874 # for now, we should expose the main prefilter method (there's legacy
1876 1875 # code out there that may rely on this).
1877 1876 self.prefilter = self.prefilter_manager.prefilter_lines
1878 1877
1879 1878 def auto_rewrite_input(self, cmd):
1880 1879 """Print to the screen the rewritten form of the user's command.
1881 1880
1882 1881 This shows visual feedback by rewriting input lines that cause
1883 1882 automatic calling to kick in, like::
1884 1883
1885 1884 /f x
1886 1885
1887 1886 into::
1888 1887
1889 1888 ------> f(x)
1890 1889
1891 1890 after the user's input prompt. This helps the user understand that the
1892 1891 input line was transformed automatically by IPython.
1893 1892 """
1894 1893 rw = self.displayhook.prompt1.auto_rewrite() + cmd
1895 1894
1896 1895 try:
1897 1896 # plain ascii works better w/ pyreadline, on some machines, so
1898 1897 # we use it and only print uncolored rewrite if we have unicode
1899 1898 rw = str(rw)
1900 1899 print >> IPython.utils.io.Term.cout, rw
1901 1900 except UnicodeEncodeError:
1902 1901 print "------> " + cmd
1903 1902
1904 1903 #-------------------------------------------------------------------------
1905 1904 # Things related to extracting values/expressions from kernel and user_ns
1906 1905 #-------------------------------------------------------------------------
1907 1906
1908 1907 def _simple_error(self):
1909 1908 etype, value = sys.exc_info()[:2]
1910 1909 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
1911 1910
1912 1911 def user_variables(self, names):
1913 1912 """Get a list of variable names from the user's namespace.
1914 1913
1915 1914 Parameters
1916 1915 ----------
1917 1916 names : list of strings
1918 1917 A list of names of variables to be read from the user namespace.
1919 1918
1920 1919 Returns
1921 1920 -------
1922 1921 A dict, keyed by the input names and with the repr() of each value.
1923 1922 """
1924 1923 out = {}
1925 1924 user_ns = self.user_ns
1926 1925 for varname in names:
1927 1926 try:
1928 1927 value = repr(user_ns[varname])
1929 1928 except:
1930 1929 value = self._simple_error()
1931 1930 out[varname] = value
1932 1931 return out
1933 1932
1934 1933 def user_expressions(self, expressions):
1935 1934 """Evaluate a dict of expressions in the user's namespace.
1936 1935
1937 1936 Parameters
1938 1937 ----------
1939 1938 expressions : dict
1940 1939 A dict with string keys and string values. The expression values
1941 1940 should be valid Python expressions, each of which will be evaluated
1942 1941 in the user namespace.
1943 1942
1944 1943 Returns
1945 1944 -------
1946 1945 A dict, keyed like the input expressions dict, with the repr() of each
1947 1946 value.
1948 1947 """
1949 1948 out = {}
1950 1949 user_ns = self.user_ns
1951 1950 global_ns = self.user_global_ns
1952 1951 for key, expr in expressions.iteritems():
1953 1952 try:
1954 1953 value = repr(eval(expr, global_ns, user_ns))
1955 1954 except:
1956 1955 value = self._simple_error()
1957 1956 out[key] = value
1958 1957 return out
1959 1958
1960 1959 #-------------------------------------------------------------------------
1961 1960 # Things related to the running of code
1962 1961 #-------------------------------------------------------------------------
1963 1962
1964 1963 def ex(self, cmd):
1965 1964 """Execute a normal python statement in user namespace."""
1966 1965 with nested(self.builtin_trap,):
1967 1966 exec cmd in self.user_global_ns, self.user_ns
1968 1967
1969 1968 def ev(self, expr):
1970 1969 """Evaluate python expression expr in user namespace.
1971 1970
1972 1971 Returns the result of evaluation
1973 1972 """
1974 1973 with nested(self.builtin_trap,):
1975 1974 return eval(expr, self.user_global_ns, self.user_ns)
1976 1975
1977 1976 def safe_execfile(self, fname, *where, **kw):
1978 1977 """A safe version of the builtin execfile().
1979 1978
1980 1979 This version will never throw an exception, but instead print
1981 1980 helpful error messages to the screen. This only works on pure
1982 1981 Python files with the .py extension.
1983 1982
1984 1983 Parameters
1985 1984 ----------
1986 1985 fname : string
1987 1986 The name of the file to be executed.
1988 1987 where : tuple
1989 1988 One or two namespaces, passed to execfile() as (globals,locals).
1990 1989 If only one is given, it is passed as both.
1991 1990 exit_ignore : bool (False)
1992 1991 If True, then silence SystemExit for non-zero status (it is always
1993 1992 silenced for zero status, as it is so common).
1994 1993 """
1995 1994 kw.setdefault('exit_ignore', False)
1996 1995
1997 1996 fname = os.path.abspath(os.path.expanduser(fname))
1998 1997
1999 1998 # Make sure we have a .py file
2000 1999 if not fname.endswith('.py'):
2001 2000 warn('File must end with .py to be run using execfile: <%s>' % fname)
2002 2001
2003 2002 # Make sure we can open the file
2004 2003 try:
2005 2004 with open(fname) as thefile:
2006 2005 pass
2007 2006 except:
2008 2007 warn('Could not open file <%s> for safe execution.' % fname)
2009 2008 return
2010 2009
2011 2010 # Find things also in current directory. This is needed to mimic the
2012 2011 # behavior of running a script from the system command line, where
2013 2012 # Python inserts the script's directory into sys.path
2014 2013 dname = os.path.dirname(fname)
2015 2014
2016 2015 with prepended_to_syspath(dname):
2017 2016 try:
2018 2017 execfile(fname,*where)
2019 2018 except SystemExit, status:
2020 2019 # If the call was made with 0 or None exit status (sys.exit(0)
2021 2020 # or sys.exit() ), don't bother showing a traceback, as both of
2022 2021 # these are considered normal by the OS:
2023 2022 # > python -c'import sys;sys.exit(0)'; echo $?
2024 2023 # 0
2025 2024 # > python -c'import sys;sys.exit()'; echo $?
2026 2025 # 0
2027 2026 # For other exit status, we show the exception unless
2028 2027 # explicitly silenced, but only in short form.
2029 2028 if status.code not in (0, None) and not kw['exit_ignore']:
2030 2029 self.showtraceback(exception_only=True)
2031 2030 except:
2032 2031 self.showtraceback()
2033 2032
2034 2033 def safe_execfile_ipy(self, fname):
2035 2034 """Like safe_execfile, but for .ipy files with IPython syntax.
2036 2035
2037 2036 Parameters
2038 2037 ----------
2039 2038 fname : str
2040 2039 The name of the file to execute. The filename must have a
2041 2040 .ipy extension.
2042 2041 """
2043 2042 fname = os.path.abspath(os.path.expanduser(fname))
2044 2043
2045 2044 # Make sure we have a .py file
2046 2045 if not fname.endswith('.ipy'):
2047 2046 warn('File must end with .py to be run using execfile: <%s>' % fname)
2048 2047
2049 2048 # Make sure we can open the file
2050 2049 try:
2051 2050 with open(fname) as thefile:
2052 2051 pass
2053 2052 except:
2054 2053 warn('Could not open file <%s> for safe execution.' % fname)
2055 2054 return
2056 2055
2057 2056 # Find things also in current directory. This is needed to mimic the
2058 2057 # behavior of running a script from the system command line, where
2059 2058 # Python inserts the script's directory into sys.path
2060 2059 dname = os.path.dirname(fname)
2061 2060
2062 2061 with prepended_to_syspath(dname):
2063 2062 try:
2064 2063 with open(fname) as thefile:
2065 2064 # self.run_cell currently captures all exceptions
2066 2065 # raised in user code. It would be nice if there were
2067 2066 # versions of runlines, execfile that did raise, so
2068 2067 # we could catch the errors.
2069 2068 self.run_cell(thefile.read())
2070 2069 except:
2071 2070 self.showtraceback()
2072 2071 warn('Unknown failure executing file: <%s>' % fname)
2073 2072
2074 2073 def run_cell(self, cell):
2075 2074 """Run the contents of an entire multiline 'cell' of code.
2076 2075
2077 2076 The cell is split into separate blocks which can be executed
2078 2077 individually. Then, based on how many blocks there are, they are
2079 2078 executed as follows:
2080 2079
2081 2080 - A single block: 'single' mode.
2082 2081
2083 2082 If there's more than one block, it depends:
2084 2083
2085 2084 - if the last one is no more than two lines long, run all but the last
2086 2085 in 'exec' mode and the very last one in 'single' mode. This makes it
2087 2086 easy to type simple expressions at the end to see computed values. -
2088 2087 otherwise (last one is also multiline), run all in 'exec' mode
2089 2088
2090 2089 When code is executed in 'single' mode, :func:`sys.displayhook` fires,
2091 2090 results are displayed and output prompts are computed. In 'exec' mode,
2092 2091 no results are displayed unless :func:`print` is called explicitly;
2093 2092 this mode is more akin to running a script.
2094 2093
2095 2094 Parameters
2096 2095 ----------
2097 2096 cell : str
2098 2097 A single or multiline string.
2099 2098 """
2100 2099
2101 2100 # We need to break up the input into executable blocks that can be run
2102 2101 # in 'single' mode, to provide comfortable user behavior.
2103 2102 blocks = self.input_splitter.split_blocks(cell)
2104 2103
2105 2104 if not blocks:
2106 2105 return
2107 2106
2108 2107 # Store the 'ipython' version of the cell as well, since that's what
2109 2108 # needs to go into the translated history and get executed (the
2110 2109 # original cell may contain non-python syntax).
2111 2110 ipy_cell = ''.join(blocks)
2112 2111
2113 2112 # Store raw and processed history
2114 2113 self.history_manager.store_inputs(ipy_cell, cell)
2115 2114
2116 2115 self.logger.log(ipy_cell, cell)
2117 2116 # dbg code!!!
2118 2117 if 0:
2119 2118 def myapp(self, val): # dbg
2120 2119 import traceback as tb
2121 2120 stack = ''.join(tb.format_stack())
2122 2121 print 'Value:', val
2123 2122 print 'Stack:\n', stack
2124 2123 list.append(self, val)
2125 2124
2126 2125 import new
2127 2126 self.history_manager.input_hist_parsed.append = types.MethodType(myapp,
2128 2127 self.history_manager.input_hist_parsed)
2129 2128 # End dbg
2130 2129
2131 2130 # All user code execution must happen with our context managers active
2132 2131 with nested(self.builtin_trap, self.display_trap):
2133 2132
2134 2133 # Single-block input should behave like an interactive prompt
2135 2134 if len(blocks) == 1:
2136 2135 # since we return here, we need to update the execution count
2137 2136 out = self.run_one_block(blocks[0])
2138 2137 self.execution_count += 1
2139 2138 return out
2140 2139
2141 2140 # In multi-block input, if the last block is a simple (one-two
2142 2141 # lines) expression, run it in single mode so it produces output.
2143 2142 # Otherwise just feed the whole thing to run_code. This seems like
2144 2143 # a reasonable usability design.
2145 2144 last = blocks[-1]
2146 2145 last_nlines = len(last.splitlines())
2147 2146
2148 2147 # Note: below, whenever we call run_code, we must sync history
2149 2148 # ourselves, because run_code is NOT meant to manage history at all.
2150 2149 if last_nlines < 2:
2151 2150 # Here we consider the cell split between 'body' and 'last',
2152 2151 # store all history and execute 'body', and if successful, then
2153 2152 # proceed to execute 'last'.
2154 2153
2155 2154 # Get the main body to run as a cell
2156 2155 ipy_body = ''.join(blocks[:-1])
2157 2156 retcode = self.run_source(ipy_body, symbol='exec',
2158 2157 post_execute=False)
2159 2158 if retcode==0:
2160 2159 # And the last expression via runlines so it produces output
2161 2160 self.run_one_block(last)
2162 2161 else:
2163 2162 # Run the whole cell as one entity, storing both raw and
2164 2163 # processed input in history
2165 2164 self.run_source(ipy_cell, symbol='exec')
2166 2165
2167 2166 # Each cell is a *single* input, regardless of how many lines it has
2168 2167 self.execution_count += 1
2169 2168
2170 2169 def run_one_block(self, block):
2171 2170 """Run a single interactive block.
2172 2171
2173 2172 If the block is single-line, dynamic transformations are applied to it
2174 2173 (like automagics, autocall and alias recognition).
2175 2174 """
2176 2175 if len(block.splitlines()) <= 1:
2177 2176 out = self.run_single_line(block)
2178 2177 else:
2179 2178 out = self.run_code(block)
2180 2179 return out
2181 2180
2182 2181 def run_single_line(self, line):
2183 2182 """Run a single-line interactive statement.
2184 2183
2185 2184 This assumes the input has been transformed to IPython syntax by
2186 2185 applying all static transformations (those with an explicit prefix like
2187 2186 % or !), but it will further try to apply the dynamic ones.
2188 2187
2189 2188 It does not update history.
2190 2189 """
2191 2190 tline = self.prefilter_manager.prefilter_line(line)
2192 2191 return self.run_source(tline)
2193 2192
2194 2193 # PENDING REMOVAL: this method is slated for deletion, once our new
2195 2194 # input logic has been 100% moved to frontends and is stable.
2196 2195 def runlines(self, lines, clean=False):
2197 2196 """Run a string of one or more lines of source.
2198 2197
2199 2198 This method is capable of running a string containing multiple source
2200 2199 lines, as if they had been entered at the IPython prompt. Since it
2201 2200 exposes IPython's processing machinery, the given strings can contain
2202 2201 magic calls (%magic), special shell access (!cmd), etc.
2203 2202 """
2204 2203
2205 2204 if isinstance(lines, (list, tuple)):
2206 2205 lines = '\n'.join(lines)
2207 2206
2208 2207 if clean:
2209 2208 lines = self._cleanup_ipy_script(lines)
2210 2209
2211 2210 # We must start with a clean buffer, in case this is run from an
2212 2211 # interactive IPython session (via a magic, for example).
2213 2212 self.reset_buffer()
2214 2213 lines = lines.splitlines()
2215 2214
2216 2215 # Since we will prefilter all lines, store the user's raw input too
2217 2216 # before we apply any transformations
2218 2217 self.buffer_raw[:] = [ l+'\n' for l in lines]
2219 2218
2220 2219 more = False
2221 2220 prefilter_lines = self.prefilter_manager.prefilter_lines
2222 2221 with nested(self.builtin_trap, self.display_trap):
2223 2222 for line in lines:
2224 2223 # skip blank lines so we don't mess up the prompt counter, but
2225 2224 # do NOT skip even a blank line if we are in a code block (more
2226 2225 # is true)
2227 2226
2228 2227 if line or more:
2229 2228 more = self.push_line(prefilter_lines(line, more))
2230 2229 # IPython's run_source returns None if there was an error
2231 2230 # compiling the code. This allows us to stop processing
2232 2231 # right away, so the user gets the error message at the
2233 2232 # right place.
2234 2233 if more is None:
2235 2234 break
2236 2235 # final newline in case the input didn't have it, so that the code
2237 2236 # actually does get executed
2238 2237 if more:
2239 2238 self.push_line('\n')
2240 2239
2241 2240 def run_source(self, source, filename=None,
2242 2241 symbol='single', post_execute=True):
2243 2242 """Compile and run some source in the interpreter.
2244 2243
2245 2244 Arguments are as for compile_command().
2246 2245
2247 2246 One several things can happen:
2248 2247
2249 2248 1) The input is incorrect; compile_command() raised an
2250 2249 exception (SyntaxError or OverflowError). A syntax traceback
2251 2250 will be printed by calling the showsyntaxerror() method.
2252 2251
2253 2252 2) The input is incomplete, and more input is required;
2254 2253 compile_command() returned None. Nothing happens.
2255 2254
2256 2255 3) The input is complete; compile_command() returned a code
2257 2256 object. The code is executed by calling self.run_code() (which
2258 2257 also handles run-time exceptions, except for SystemExit).
2259 2258
2260 2259 The return value is:
2261 2260
2262 2261 - True in case 2
2263 2262
2264 2263 - False in the other cases, unless an exception is raised, where
2265 2264 None is returned instead. This can be used by external callers to
2266 2265 know whether to continue feeding input or not.
2267 2266
2268 2267 The return value can be used to decide whether to use sys.ps1 or
2269 2268 sys.ps2 to prompt the next line."""
2270 2269
2271 2270 # We need to ensure that the source is unicode from here on.
2272 2271 if type(source)==str:
2273 2272 usource = source.decode(self.stdin_encoding)
2274 2273 else:
2275 2274 usource = source
2276 2275
2277 2276 if 0: # dbg
2278 2277 print 'Source:', repr(source) # dbg
2279 2278 print 'USource:', repr(usource) # dbg
2280 2279 print 'type:', type(source) # dbg
2281 2280 print 'encoding', self.stdin_encoding # dbg
2282 2281
2283 2282 try:
2284 2283 code = self.compile(usource, symbol, self.execution_count)
2285 2284 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2286 2285 # Case 1
2287 2286 self.showsyntaxerror(filename)
2288 2287 return None
2289 2288
2290 2289 if code is None:
2291 2290 # Case 2
2292 2291 return True
2293 2292
2294 2293 # Case 3
2295 2294 # We store the code object so that threaded shells and
2296 2295 # custom exception handlers can access all this info if needed.
2297 2296 # The source corresponding to this can be obtained from the
2298 2297 # buffer attribute as '\n'.join(self.buffer).
2299 2298 self.code_to_run = code
2300 2299 # now actually execute the code object
2301 2300 if self.run_code(code, post_execute) == 0:
2302 2301 return False
2303 2302 else:
2304 2303 return None
2305 2304
2306 2305 # For backwards compatibility
2307 2306 runsource = run_source
2308 2307
2309 2308 def run_code(self, code_obj, post_execute=True):
2310 2309 """Execute a code object.
2311 2310
2312 2311 When an exception occurs, self.showtraceback() is called to display a
2313 2312 traceback.
2314 2313
2315 2314 Return value: a flag indicating whether the code to be run completed
2316 2315 successfully:
2317 2316
2318 2317 - 0: successful execution.
2319 2318 - 1: an error occurred.
2320 2319 """
2321 2320
2322 2321 # Set our own excepthook in case the user code tries to call it
2323 2322 # directly, so that the IPython crash handler doesn't get triggered
2324 2323 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2325 2324
2326 2325 # we save the original sys.excepthook in the instance, in case config
2327 2326 # code (such as magics) needs access to it.
2328 2327 self.sys_excepthook = old_excepthook
2329 2328 outflag = 1 # happens in more places, so it's easier as default
2330 2329 try:
2331 2330 try:
2332 2331 self.hooks.pre_run_code_hook()
2333 2332 #rprint('Running code') # dbg
2334 2333 exec code_obj in self.user_global_ns, self.user_ns
2335 2334 finally:
2336 2335 # Reset our crash handler in place
2337 2336 sys.excepthook = old_excepthook
2338 2337 except SystemExit:
2339 2338 self.reset_buffer()
2340 2339 self.showtraceback(exception_only=True)
2341 2340 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2342 2341 except self.custom_exceptions:
2343 2342 etype,value,tb = sys.exc_info()
2344 2343 self.CustomTB(etype,value,tb)
2345 2344 except:
2346 2345 self.showtraceback()
2347 2346 else:
2348 2347 outflag = 0
2349 2348 if softspace(sys.stdout, 0):
2350 2349 print
2351 2350
2352 2351 # Execute any registered post-execution functions. Here, any errors
2353 2352 # are reported only minimally and just on the terminal, because the
2354 2353 # main exception channel may be occupied with a user traceback.
2355 2354 # FIXME: we need to think this mechanism a little more carefully.
2356 2355 if post_execute:
2357 2356 for func in self._post_execute:
2358 2357 try:
2359 2358 func()
2360 2359 except:
2361 2360 head = '[ ERROR ] Evaluating post_execute function: %s' % \
2362 2361 func
2363 2362 print >> io.Term.cout, head
2364 2363 print >> io.Term.cout, self._simple_error()
2365 2364 print >> io.Term.cout, 'Removing from post_execute'
2366 2365 self._post_execute.remove(func)
2367 2366
2368 2367 # Flush out code object which has been run (and source)
2369 2368 self.code_to_run = None
2370 2369 return outflag
2371 2370
2372 2371 # For backwards compatibility
2373 2372 runcode = run_code
2374 2373
2375 2374 # PENDING REMOVAL: this method is slated for deletion, once our new
2376 2375 # input logic has been 100% moved to frontends and is stable.
2377 2376 def push_line(self, line):
2378 2377 """Push a line to the interpreter.
2379 2378
2380 2379 The line should not have a trailing newline; it may have
2381 2380 internal newlines. The line is appended to a buffer and the
2382 2381 interpreter's run_source() method is called with the
2383 2382 concatenated contents of the buffer as source. If this
2384 2383 indicates that the command was executed or invalid, the buffer
2385 2384 is reset; otherwise, the command is incomplete, and the buffer
2386 2385 is left as it was after the line was appended. The return
2387 2386 value is 1 if more input is required, 0 if the line was dealt
2388 2387 with in some way (this is the same as run_source()).
2389 2388 """
2390 2389
2391 2390 # autoindent management should be done here, and not in the
2392 2391 # interactive loop, since that one is only seen by keyboard input. We
2393 2392 # need this done correctly even for code run via runlines (which uses
2394 2393 # push).
2395 2394
2396 2395 #print 'push line: <%s>' % line # dbg
2397 2396 self.buffer.append(line)
2398 2397 full_source = '\n'.join(self.buffer)
2399 2398 more = self.run_source(full_source, self.filename)
2400 2399 if not more:
2401 2400 self.history_manager.store_inputs('\n'.join(self.buffer_raw),
2402 2401 full_source)
2403 2402 self.reset_buffer()
2404 2403 self.execution_count += 1
2405 2404 return more
2406 2405
2407 2406 def reset_buffer(self):
2408 2407 """Reset the input buffer."""
2409 2408 self.buffer[:] = []
2410 2409 self.buffer_raw[:] = []
2411 2410 self.input_splitter.reset()
2412 2411
2413 2412 # For backwards compatibility
2414 2413 resetbuffer = reset_buffer
2415 2414
2416 2415 def _is_secondary_block_start(self, s):
2417 2416 if not s.endswith(':'):
2418 2417 return False
2419 2418 if (s.startswith('elif') or
2420 2419 s.startswith('else') or
2421 2420 s.startswith('except') or
2422 2421 s.startswith('finally')):
2423 2422 return True
2424 2423
2425 2424 def _cleanup_ipy_script(self, script):
2426 2425 """Make a script safe for self.runlines()
2427 2426
2428 2427 Currently, IPython is lines based, with blocks being detected by
2429 2428 empty lines. This is a problem for block based scripts that may
2430 2429 not have empty lines after blocks. This script adds those empty
2431 2430 lines to make scripts safe for running in the current line based
2432 2431 IPython.
2433 2432 """
2434 2433 res = []
2435 2434 lines = script.splitlines()
2436 2435 level = 0
2437 2436
2438 2437 for l in lines:
2439 2438 lstripped = l.lstrip()
2440 2439 stripped = l.strip()
2441 2440 if not stripped:
2442 2441 continue
2443 2442 newlevel = len(l) - len(lstripped)
2444 2443 if level > 0 and newlevel == 0 and \
2445 2444 not self._is_secondary_block_start(stripped):
2446 2445 # add empty line
2447 2446 res.append('')
2448 2447 res.append(l)
2449 2448 level = newlevel
2450 2449
2451 2450 return '\n'.join(res) + '\n'
2452 2451
2453 2452 #-------------------------------------------------------------------------
2454 2453 # Things related to GUI support and pylab
2455 2454 #-------------------------------------------------------------------------
2456 2455
2457 2456 def enable_pylab(self, gui=None):
2458 2457 raise NotImplementedError('Implement enable_pylab in a subclass')
2459 2458
2460 2459 #-------------------------------------------------------------------------
2461 2460 # Utilities
2462 2461 #-------------------------------------------------------------------------
2463 2462
2464 2463 def var_expand(self,cmd,depth=0):
2465 2464 """Expand python variables in a string.
2466 2465
2467 2466 The depth argument indicates how many frames above the caller should
2468 2467 be walked to look for the local namespace where to expand variables.
2469 2468
2470 2469 The global namespace for expansion is always the user's interactive
2471 2470 namespace.
2472 2471 """
2473 2472
2474 2473 return str(ItplNS(cmd,
2475 2474 self.user_ns, # globals
2476 2475 # Skip our own frame in searching for locals:
2477 2476 sys._getframe(depth+1).f_locals # locals
2478 2477 ))
2479 2478
2480 2479 def mktempfile(self, data=None, prefix='ipython_edit_'):
2481 2480 """Make a new tempfile and return its filename.
2482 2481
2483 2482 This makes a call to tempfile.mktemp, but it registers the created
2484 2483 filename internally so ipython cleans it up at exit time.
2485 2484
2486 2485 Optional inputs:
2487 2486
2488 2487 - data(None): if data is given, it gets written out to the temp file
2489 2488 immediately, and the file is closed again."""
2490 2489
2491 2490 filename = tempfile.mktemp('.py', prefix)
2492 2491 self.tempfiles.append(filename)
2493 2492
2494 2493 if data:
2495 2494 tmp_file = open(filename,'w')
2496 2495 tmp_file.write(data)
2497 2496 tmp_file.close()
2498 2497 return filename
2499 2498
2500 2499 # TODO: This should be removed when Term is refactored.
2501 2500 def write(self,data):
2502 2501 """Write a string to the default output"""
2503 2502 io.Term.cout.write(data)
2504 2503
2505 2504 # TODO: This should be removed when Term is refactored.
2506 2505 def write_err(self,data):
2507 2506 """Write a string to the default error output"""
2508 2507 io.Term.cerr.write(data)
2509 2508
2510 2509 def ask_yes_no(self,prompt,default=True):
2511 2510 if self.quiet:
2512 2511 return True
2513 2512 return ask_yes_no(prompt,default)
2514 2513
2515 2514 def show_usage(self):
2516 2515 """Show a usage message"""
2517 2516 page.page(IPython.core.usage.interactive_usage)
2518 2517
2519 2518 #-------------------------------------------------------------------------
2520 2519 # Things related to IPython exiting
2521 2520 #-------------------------------------------------------------------------
2522 2521 def atexit_operations(self):
2523 2522 """This will be executed at the time of exit.
2524 2523
2525 2524 Cleanup operations and saving of persistent data that is done
2526 2525 unconditionally by IPython should be performed here.
2527 2526
2528 2527 For things that may depend on startup flags or platform specifics (such
2529 2528 as having readline or not), register a separate atexit function in the
2530 2529 code that has the appropriate information, rather than trying to
2531 2530 clutter
2532 2531 """
2533 2532 # Cleanup all tempfiles left around
2534 2533 for tfile in self.tempfiles:
2535 2534 try:
2536 2535 os.unlink(tfile)
2537 2536 except OSError:
2538 2537 pass
2539 2538
2540 2539 self.save_history()
2541 2540
2542 2541 # Clear all user namespaces to release all references cleanly.
2543 2542 self.reset()
2544 2543
2545 2544 # Run user hooks
2546 2545 self.hooks.shutdown_hook()
2547 2546
2548 2547 def cleanup(self):
2549 2548 self.restore_sys_module_state()
2550 2549
2551 2550
2552 2551 class InteractiveShellABC(object):
2553 2552 """An abstract base class for InteractiveShell."""
2554 2553 __metaclass__ = abc.ABCMeta
2555 2554
2556 2555 InteractiveShellABC.register(InteractiveShell)
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,664 +1,664 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 The :class:`~IPython.core.application.Application` object for the command
5 5 line :command:`ipython` program.
6 6
7 7 Authors
8 8 -------
9 9
10 10 * Brian Granger
11 11 * Fernando Perez
12 12 """
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Copyright (C) 2008-2010 The IPython Development Team
16 16 #
17 17 # Distributed under the terms of the BSD License. The full license is in
18 18 # the file COPYING, distributed as part of this software.
19 19 #-----------------------------------------------------------------------------
20 20
21 21 #-----------------------------------------------------------------------------
22 22 # Imports
23 23 #-----------------------------------------------------------------------------
24 24
25 25 from __future__ import absolute_import
26 26
27 27 import logging
28 28 import os
29 29 import sys
30 30
31 31 from IPython.core import release
32 32 from IPython.core.crashhandler import CrashHandler
33 33 from IPython.core.application import Application, BaseAppConfigLoader
34 34 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
35 35 from IPython.config.loader import (
36 36 Config,
37 37 PyFileConfigLoader
38 38 )
39 39 from IPython.lib import inputhook
40 40 from IPython.utils.path import filefind, get_ipython_dir
41 41 from IPython.core import usage
42 42
43 43 #-----------------------------------------------------------------------------
44 44 # Globals, utilities and helpers
45 45 #-----------------------------------------------------------------------------
46 46
47 47 #: The default config file name for this application.
48 48 default_config_file_name = u'ipython_config.py'
49 49
50 50
51 51 class IPAppConfigLoader(BaseAppConfigLoader):
52 52
53 53 def _add_arguments(self):
54 54 super(IPAppConfigLoader, self)._add_arguments()
55 55 paa = self.parser.add_argument
56 56 paa('-p',
57 57 '--profile', dest='Global.profile', type=unicode,
58 58 help=
59 59 """The string name of the ipython profile to be used. Assume that your
60 60 config file is ipython_config-<name>.py (looks in current dir first,
61 61 then in IPYTHON_DIR). This is a quick way to keep and load multiple
62 62 config files for different tasks, especially if include your basic one
63 63 in your more specialized ones. You can keep a basic
64 64 IPYTHON_DIR/ipython_config.py file and then have other 'profiles' which
65 65 include this one and load extra things for particular tasks.""",
66 66 metavar='Global.profile')
67 67 paa('--config-file',
68 68 dest='Global.config_file', type=unicode,
69 69 help=
70 70 """Set the config file name to override default. Normally IPython
71 71 loads ipython_config.py (from current directory) or
72 72 IPYTHON_DIR/ipython_config.py. If the loading of your config file
73 73 fails, IPython starts with a bare bones configuration (no modules
74 74 loaded at all).""",
75 75 metavar='Global.config_file')
76 76 paa('--autocall',
77 77 dest='InteractiveShell.autocall', type=int,
78 78 help=
79 79 """Make IPython automatically call any callable object even if you
80 80 didn't type explicit parentheses. For example, 'str 43' becomes
81 81 'str(43)' automatically. The value can be '0' to disable the feature,
82 82 '1' for 'smart' autocall, where it is not applied if there are no more
83 83 arguments on the line, and '2' for 'full' autocall, where all callable
84 84 objects are automatically called (even if no arguments are present).
85 85 The default is '1'.""",
86 86 metavar='InteractiveShell.autocall')
87 87 paa('--autoindent',
88 88 action='store_true', dest='InteractiveShell.autoindent',
89 89 help='Turn on autoindenting.')
90 90 paa('--no-autoindent',
91 91 action='store_false', dest='InteractiveShell.autoindent',
92 92 help='Turn off autoindenting.')
93 93 paa('--automagic',
94 94 action='store_true', dest='InteractiveShell.automagic',
95 95 help=
96 96 """Turn on the auto calling of magic commands. Type %%magic at the
97 97 IPython prompt for more information.""")
98 98 paa('--no-automagic',
99 99 action='store_false', dest='InteractiveShell.automagic',
100 100 help='Turn off the auto calling of magic commands.')
101 101 paa('--autoedit-syntax',
102 102 action='store_true', dest='TerminalInteractiveShell.autoedit_syntax',
103 103 help='Turn on auto editing of files with syntax errors.')
104 104 paa('--no-autoedit-syntax',
105 105 action='store_false', dest='TerminalInteractiveShell.autoedit_syntax',
106 106 help='Turn off auto editing of files with syntax errors.')
107 107 paa('--banner',
108 108 action='store_true', dest='Global.display_banner',
109 109 help='Display a banner upon starting IPython.')
110 110 paa('--no-banner',
111 111 action='store_false', dest='Global.display_banner',
112 112 help="Don't display a banner upon starting IPython.")
113 113 paa('--cache-size',
114 114 type=int, dest='InteractiveShell.cache_size',
115 115 help=
116 116 """Set the size of the output cache. The default is 1000, you can
117 117 change it permanently in your config file. Setting it to 0 completely
118 118 disables the caching system, and the minimum value accepted is 20 (if
119 119 you provide a value less than 20, it is reset to 0 and a warning is
120 120 issued). This limit is defined because otherwise you'll spend more
121 121 time re-flushing a too small cache than working""",
122 122 metavar='InteractiveShell.cache_size')
123 123 paa('--classic',
124 124 action='store_true', dest='Global.classic',
125 125 help="Gives IPython a similar feel to the classic Python prompt.")
126 126 paa('--colors',
127 127 type=str, dest='InteractiveShell.colors',
128 128 help="Set the color scheme (NoColor, Linux, and LightBG).",
129 129 metavar='InteractiveShell.colors')
130 130 paa('--color-info',
131 131 action='store_true', dest='InteractiveShell.color_info',
132 132 help=
133 133 """IPython can display information about objects via a set of func-
134 134 tions, and optionally can use colors for this, syntax highlighting
135 135 source code and various other elements. However, because this
136 136 information is passed through a pager (like 'less') and many pagers get
137 137 confused with color codes, this option is off by default. You can test
138 138 it and turn it on permanently in your ipython_config.py file if it
139 139 works for you. Test it and turn it on permanently if it works with
140 140 your system. The magic function %%color_info allows you to toggle this
141 141 inter- actively for testing.""")
142 142 paa('--no-color-info',
143 143 action='store_false', dest='InteractiveShell.color_info',
144 144 help="Disable using colors for info related things.")
145 145 paa('--confirm-exit',
146 146 action='store_true', dest='TerminalInteractiveShell.confirm_exit',
147 147 help=
148 148 """Set to confirm when you try to exit IPython with an EOF (Control-D
149 149 in Unix, Control-Z/Enter in Windows). By typing 'exit', 'quit' or
150 150 '%%Exit', you can force a direct exit without any confirmation.""")
151 151 paa('--no-confirm-exit',
152 152 action='store_false', dest='TerminalInteractiveShell.confirm_exit',
153 153 help="Don't prompt the user when exiting.")
154 154 paa('--deep-reload',
155 155 action='store_true', dest='InteractiveShell.deep_reload',
156 156 help=
157 157 """Enable deep (recursive) reloading by default. IPython can use the
158 158 deep_reload module which reloads changes in modules recursively (it
159 159 replaces the reload() function, so you don't need to change anything to
160 160 use it). deep_reload() forces a full reload of modules whose code may
161 161 have changed, which the default reload() function does not. When
162 162 deep_reload is off, IPython will use the normal reload(), but
163 163 deep_reload will still be available as dreload(). This fea- ture is off
164 164 by default [which means that you have both normal reload() and
165 165 dreload()].""")
166 166 paa('--no-deep-reload',
167 167 action='store_false', dest='InteractiveShell.deep_reload',
168 168 help="Disable deep (recursive) reloading by default.")
169 169 paa('--editor',
170 170 type=str, dest='TerminalInteractiveShell.editor',
171 171 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
172 172 metavar='TerminalInteractiveShell.editor')
173 173 paa('--log','-l',
174 174 action='store_true', dest='InteractiveShell.logstart',
175 175 help="Start logging to the default log file (./ipython_log.py).")
176 176 paa('--logfile','-lf',
177 177 type=unicode, dest='InteractiveShell.logfile',
178 178 help="Start logging to logfile with this name.",
179 179 metavar='InteractiveShell.logfile')
180 180 paa('--log-append','-la',
181 181 type=unicode, dest='InteractiveShell.logappend',
182 182 help="Start logging to the given file in append mode.",
183 183 metavar='InteractiveShell.logfile')
184 184 paa('--pdb',
185 185 action='store_true', dest='InteractiveShell.pdb',
186 186 help="Enable auto calling the pdb debugger after every exception.")
187 187 paa('--no-pdb',
188 188 action='store_false', dest='InteractiveShell.pdb',
189 189 help="Disable auto calling the pdb debugger after every exception.")
190 190 paa('--pprint',
191 action='store_true', dest='InteractiveShell.pprint',
191 action='store_true', dest='PlainTextFormatter.pprint',
192 192 help="Enable auto pretty printing of results.")
193 193 paa('--no-pprint',
194 action='store_false', dest='InteractiveShell.pprint',
194 action='store_false', dest='PlainTextFormatter.pprint',
195 195 help="Disable auto auto pretty printing of results.")
196 196 paa('--prompt-in1','-pi1',
197 197 type=str, dest='InteractiveShell.prompt_in1',
198 198 help=
199 199 """Set the main input prompt ('In [\#]: '). Note that if you are using
200 200 numbered prompts, the number is represented with a '\#' in the string.
201 201 Don't forget to quote strings with spaces embedded in them. Most
202 202 bash-like escapes can be used to customize IPython's prompts, as well
203 203 as a few additional ones which are IPython-spe- cific. All valid
204 204 prompt escapes are described in detail in the Customization section of
205 205 the IPython manual.""",
206 206 metavar='InteractiveShell.prompt_in1')
207 207 paa('--prompt-in2','-pi2',
208 208 type=str, dest='InteractiveShell.prompt_in2',
209 209 help=
210 210 """Set the secondary input prompt (' .\D.: '). Similar to the previous
211 211 option, but used for the continuation prompts. The special sequence
212 212 '\D' is similar to '\#', but with all digits replaced by dots (so you
213 213 can have your continuation prompt aligned with your input prompt).
214 214 Default: ' .\D.: ' (note three spaces at the start for alignment with
215 215 'In [\#]')""",
216 216 metavar='InteractiveShell.prompt_in2')
217 217 paa('--prompt-out','-po',
218 218 type=str, dest='InteractiveShell.prompt_out',
219 219 help="Set the output prompt ('Out[\#]:')",
220 220 metavar='InteractiveShell.prompt_out')
221 221 paa('--quick',
222 222 action='store_true', dest='Global.quick',
223 223 help="Enable quick startup with no config files.")
224 224 paa('--readline',
225 225 action='store_true', dest='InteractiveShell.readline_use',
226 226 help="Enable readline for command line usage.")
227 227 paa('--no-readline',
228 228 action='store_false', dest='InteractiveShell.readline_use',
229 229 help="Disable readline for command line usage.")
230 230 paa('--screen-length','-sl',
231 231 type=int, dest='TerminalInteractiveShell.screen_length',
232 232 help=
233 233 """Number of lines of your screen, used to control printing of very
234 234 long strings. Strings longer than this number of lines will be sent
235 235 through a pager instead of directly printed. The default value for
236 236 this is 0, which means IPython will auto-detect your screen size every
237 237 time it needs to print certain potentially long strings (this doesn't
238 238 change the behavior of the 'print' keyword, it's only triggered
239 239 internally). If for some reason this isn't working well (it needs
240 240 curses support), specify it yourself. Otherwise don't change the
241 241 default.""",
242 242 metavar='TerminalInteractiveShell.screen_length')
243 243 paa('--separate-in','-si',
244 244 type=str, dest='InteractiveShell.separate_in',
245 245 help="Separator before input prompts. Default '\\n'.",
246 246 metavar='InteractiveShell.separate_in')
247 247 paa('--separate-out','-so',
248 248 type=str, dest='InteractiveShell.separate_out',
249 249 help="Separator before output prompts. Default 0 (nothing).",
250 250 metavar='InteractiveShell.separate_out')
251 251 paa('--separate-out2','-so2',
252 252 type=str, dest='InteractiveShell.separate_out2',
253 253 help="Separator after output prompts. Default 0 (nonight).",
254 254 metavar='InteractiveShell.separate_out2')
255 255 paa('--no-sep',
256 256 action='store_true', dest='Global.nosep',
257 257 help="Eliminate all spacing between prompts.")
258 258 paa('--term-title',
259 259 action='store_true', dest='TerminalInteractiveShell.term_title',
260 260 help="Enable auto setting the terminal title.")
261 261 paa('--no-term-title',
262 262 action='store_false', dest='TerminalInteractiveShell.term_title',
263 263 help="Disable auto setting the terminal title.")
264 264 paa('--xmode',
265 265 type=str, dest='InteractiveShell.xmode',
266 266 help=
267 267 """Exception reporting mode ('Plain','Context','Verbose'). Plain:
268 268 similar to python's normal traceback printing. Context: prints 5 lines
269 269 of context source code around each line in the traceback. Verbose:
270 270 similar to Context, but additionally prints the variables currently
271 271 visible where the exception happened (shortening their strings if too
272 272 long). This can potentially be very slow, if you happen to have a huge
273 273 data structure whose string representation is complex to compute.
274 274 Your computer may appear to freeze for a while with cpu usage at 100%%.
275 275 If this occurs, you can cancel the traceback with Ctrl-C (maybe hitting
276 276 it more than once).
277 277 """,
278 278 metavar='InteractiveShell.xmode')
279 279 paa('--ext',
280 280 type=str, dest='Global.extra_extension',
281 281 help="The dotted module name of an IPython extension to load.",
282 282 metavar='Global.extra_extension')
283 283 paa('-c',
284 284 type=str, dest='Global.code_to_run',
285 285 help="Execute the given command string.",
286 286 metavar='Global.code_to_run')
287 287 paa('-i',
288 288 action='store_true', dest='Global.force_interact',
289 289 help=
290 290 "If running code from the command line, become interactive afterwards.")
291 291
292 292 # Options to start with GUI control enabled from the beginning
293 293 paa('--gui',
294 294 type=str, dest='Global.gui',
295 295 help="Enable GUI event loop integration ('qt', 'wx', 'gtk').",
296 296 metavar='gui-mode')
297 297 paa('--pylab','-pylab',
298 298 type=str, dest='Global.pylab',
299 299 nargs='?', const='auto', metavar='gui-mode',
300 300 help="Pre-load matplotlib and numpy for interactive use. "+
301 301 "If no value is given, the gui backend is matplotlib's, else use "+
302 302 "one of: ['tk', 'qt', 'wx', 'gtk'].")
303 303
304 304 # Legacy GUI options. Leave them in for backwards compatibility, but the
305 305 # 'thread' names are really a misnomer now.
306 306 paa('--wthread', '-wthread',
307 307 action='store_true', dest='Global.wthread',
308 308 help=
309 309 """Enable wxPython event loop integration. (DEPRECATED, use --gui wx)""")
310 310 paa('--q4thread', '--qthread', '-q4thread', '-qthread',
311 311 action='store_true', dest='Global.q4thread',
312 312 help=
313 313 """Enable Qt4 event loop integration. Qt3 is no longer supported.
314 314 (DEPRECATED, use --gui qt)""")
315 315 paa('--gthread', '-gthread',
316 316 action='store_true', dest='Global.gthread',
317 317 help=
318 318 """Enable GTK event loop integration. (DEPRECATED, use --gui gtk)""")
319 319
320 320
321 321 #-----------------------------------------------------------------------------
322 322 # Crash handler for this application
323 323 #-----------------------------------------------------------------------------
324 324
325 325 _message_template = """\
326 326 Oops, $self.app_name crashed. We do our best to make it stable, but...
327 327
328 328 A crash report was automatically generated with the following information:
329 329 - A verbatim copy of the crash traceback.
330 330 - A copy of your input history during this session.
331 331 - Data on your current $self.app_name configuration.
332 332
333 333 It was left in the file named:
334 334 \t'$self.crash_report_fname'
335 335 If you can email this file to the developers, the information in it will help
336 336 them in understanding and correcting the problem.
337 337
338 338 You can mail it to: $self.contact_name at $self.contact_email
339 339 with the subject '$self.app_name Crash Report'.
340 340
341 341 If you want to do it now, the following command will work (under Unix):
342 342 mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname
343 343
344 344 To ensure accurate tracking of this issue, please file a report about it at:
345 345 $self.bug_tracker
346 346 """
347 347
348 348 class IPAppCrashHandler(CrashHandler):
349 349 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
350 350
351 351 message_template = _message_template
352 352
353 353 def __init__(self, app):
354 354 contact_name = release.authors['Fernando'][0]
355 355 contact_email = release.authors['Fernando'][1]
356 356 bug_tracker = 'http://github.com/ipython/ipython/issues'
357 357 super(IPAppCrashHandler,self).__init__(
358 358 app, contact_name, contact_email, bug_tracker
359 359 )
360 360
361 361 def make_report(self,traceback):
362 362 """Return a string containing a crash report."""
363 363
364 364 sec_sep = self.section_sep
365 365 # Start with parent report
366 366 report = [super(IPAppCrashHandler, self).make_report(traceback)]
367 367 # Add interactive-specific info we may have
368 368 rpt_add = report.append
369 369 try:
370 370 rpt_add(sec_sep+"History of session input:")
371 371 for line in self.app.shell.user_ns['_ih']:
372 372 rpt_add(line)
373 373 rpt_add('\n*** Last line of input (may not be in above history):\n')
374 374 rpt_add(self.app.shell._last_input_line+'\n')
375 375 except:
376 376 pass
377 377
378 378 return ''.join(report)
379 379
380 380
381 381 #-----------------------------------------------------------------------------
382 382 # Main classes and functions
383 383 #-----------------------------------------------------------------------------
384 384
385 385 class IPythonApp(Application):
386 386 name = u'ipython'
387 387 #: argparse formats better the 'usage' than the 'description' field
388 388 description = None
389 389 usage = usage.cl_usage
390 390 command_line_loader = IPAppConfigLoader
391 391 default_config_file_name = default_config_file_name
392 392 crash_handler_class = IPAppCrashHandler
393 393
394 394 def create_default_config(self):
395 395 super(IPythonApp, self).create_default_config()
396 396 # Eliminate multiple lookups
397 397 Global = self.default_config.Global
398 398
399 399 # Set all default values
400 400 Global.display_banner = True
401 401
402 402 # If the -c flag is given or a file is given to run at the cmd line
403 403 # like "ipython foo.py", normally we exit without starting the main
404 404 # loop. The force_interact config variable allows a user to override
405 405 # this and interact. It is also set by the -i cmd line flag, just
406 406 # like Python.
407 407 Global.force_interact = False
408 408
409 409 # By default always interact by starting the IPython mainloop.
410 410 Global.interact = True
411 411
412 412 # No GUI integration by default
413 413 Global.gui = False
414 414 # Pylab off by default
415 415 Global.pylab = False
416 416
417 417 # Deprecated versions of gui support that used threading, we support
418 418 # them just for bacwards compatibility as an alternate spelling for
419 419 # '--gui X'
420 420 Global.qthread = False
421 421 Global.q4thread = False
422 422 Global.wthread = False
423 423 Global.gthread = False
424 424
425 425 def load_file_config(self):
426 426 if hasattr(self.command_line_config.Global, 'quick'):
427 427 if self.command_line_config.Global.quick:
428 428 self.file_config = Config()
429 429 return
430 430 super(IPythonApp, self).load_file_config()
431 431
432 432 def post_load_file_config(self):
433 433 if hasattr(self.command_line_config.Global, 'extra_extension'):
434 434 if not hasattr(self.file_config.Global, 'extensions'):
435 435 self.file_config.Global.extensions = []
436 436 self.file_config.Global.extensions.append(
437 437 self.command_line_config.Global.extra_extension)
438 438 del self.command_line_config.Global.extra_extension
439 439
440 440 def pre_construct(self):
441 441 config = self.master_config
442 442
443 443 if hasattr(config.Global, 'classic'):
444 444 if config.Global.classic:
445 445 config.InteractiveShell.cache_size = 0
446 config.InteractiveShell.pprint = 0
446 config.PlainTextFormatter.pprint = 0
447 447 config.InteractiveShell.prompt_in1 = '>>> '
448 448 config.InteractiveShell.prompt_in2 = '... '
449 449 config.InteractiveShell.prompt_out = ''
450 450 config.InteractiveShell.separate_in = \
451 451 config.InteractiveShell.separate_out = \
452 452 config.InteractiveShell.separate_out2 = ''
453 453 config.InteractiveShell.colors = 'NoColor'
454 454 config.InteractiveShell.xmode = 'Plain'
455 455
456 456 if hasattr(config.Global, 'nosep'):
457 457 if config.Global.nosep:
458 458 config.InteractiveShell.separate_in = \
459 459 config.InteractiveShell.separate_out = \
460 460 config.InteractiveShell.separate_out2 = ''
461 461
462 462 # if there is code of files to run from the cmd line, don't interact
463 463 # unless the -i flag (Global.force_interact) is true.
464 464 code_to_run = config.Global.get('code_to_run','')
465 465 file_to_run = False
466 466 if self.extra_args and self.extra_args[0]:
467 467 file_to_run = True
468 468 if file_to_run or code_to_run:
469 469 if not config.Global.force_interact:
470 470 config.Global.interact = False
471 471
472 472 def construct(self):
473 473 # I am a little hesitant to put these into InteractiveShell itself.
474 474 # But that might be the place for them
475 475 sys.path.insert(0, '')
476 476
477 477 # Create an InteractiveShell instance.
478 478 self.shell = TerminalInteractiveShell.instance(config=self.master_config)
479 479
480 480 def post_construct(self):
481 481 """Do actions after construct, but before starting the app."""
482 482 config = self.master_config
483 483
484 484 # shell.display_banner should always be False for the terminal
485 485 # based app, because we call shell.show_banner() by hand below
486 486 # so the banner shows *before* all extension loading stuff.
487 487 self.shell.display_banner = False
488 488 if config.Global.display_banner and \
489 489 config.Global.interact:
490 490 self.shell.show_banner()
491 491
492 492 # Make sure there is a space below the banner.
493 493 if self.log_level <= logging.INFO: print
494 494
495 495 # Now a variety of things that happen after the banner is printed.
496 496 self._enable_gui_pylab()
497 497 self._load_extensions()
498 498 self._run_exec_lines()
499 499 self._run_exec_files()
500 500 self._run_cmd_line_code()
501 501
502 502 def _enable_gui_pylab(self):
503 503 """Enable GUI event loop integration, taking pylab into account."""
504 504 Global = self.master_config.Global
505 505
506 506 # Select which gui to use
507 507 if Global.gui:
508 508 gui = Global.gui
509 509 # The following are deprecated, but there's likely to be a lot of use
510 510 # of this form out there, so we might as well support it for now. But
511 511 # the --gui option above takes precedence.
512 512 elif Global.wthread:
513 513 gui = inputhook.GUI_WX
514 514 elif Global.qthread:
515 515 gui = inputhook.GUI_QT
516 516 elif Global.gthread:
517 517 gui = inputhook.GUI_GTK
518 518 else:
519 519 gui = None
520 520
521 521 # Using --pylab will also require gui activation, though which toolkit
522 522 # to use may be chosen automatically based on mpl configuration.
523 523 if Global.pylab:
524 524 activate = self.shell.enable_pylab
525 525 if Global.pylab == 'auto':
526 526 gui = None
527 527 else:
528 528 gui = Global.pylab
529 529 else:
530 530 # Enable only GUI integration, no pylab
531 531 activate = inputhook.enable_gui
532 532
533 533 if gui or Global.pylab:
534 534 try:
535 535 self.log.info("Enabling GUI event loop integration, "
536 536 "toolkit=%s, pylab=%s" % (gui, Global.pylab) )
537 537 activate(gui)
538 538 except:
539 539 self.log.warn("Error in enabling GUI event loop integration:")
540 540 self.shell.showtraceback()
541 541
542 542 def _load_extensions(self):
543 543 """Load all IPython extensions in Global.extensions.
544 544
545 545 This uses the :meth:`ExtensionManager.load_extensions` to load all
546 546 the extensions listed in ``self.master_config.Global.extensions``.
547 547 """
548 548 try:
549 549 if hasattr(self.master_config.Global, 'extensions'):
550 550 self.log.debug("Loading IPython extensions...")
551 551 extensions = self.master_config.Global.extensions
552 552 for ext in extensions:
553 553 try:
554 554 self.log.info("Loading IPython extension: %s" % ext)
555 555 self.shell.extension_manager.load_extension(ext)
556 556 except:
557 557 self.log.warn("Error in loading extension: %s" % ext)
558 558 self.shell.showtraceback()
559 559 except:
560 560 self.log.warn("Unknown error in loading extensions:")
561 561 self.shell.showtraceback()
562 562
563 563 def _run_exec_lines(self):
564 564 """Run lines of code in Global.exec_lines in the user's namespace."""
565 565 try:
566 566 if hasattr(self.master_config.Global, 'exec_lines'):
567 567 self.log.debug("Running code from Global.exec_lines...")
568 568 exec_lines = self.master_config.Global.exec_lines
569 569 for line in exec_lines:
570 570 try:
571 571 self.log.info("Running code in user namespace: %s" %
572 572 line)
573 573 self.shell.run_cell(line)
574 574 except:
575 575 self.log.warn("Error in executing line in user "
576 576 "namespace: %s" % line)
577 577 self.shell.showtraceback()
578 578 except:
579 579 self.log.warn("Unknown error in handling Global.exec_lines:")
580 580 self.shell.showtraceback()
581 581
582 582 def _exec_file(self, fname):
583 583 full_filename = filefind(fname, [u'.', self.ipython_dir])
584 584 if os.path.isfile(full_filename):
585 585 if full_filename.endswith(u'.py'):
586 586 self.log.info("Running file in user namespace: %s" %
587 587 full_filename)
588 588 # Ensure that __file__ is always defined to match Python behavior
589 589 self.shell.user_ns['__file__'] = fname
590 590 try:
591 591 self.shell.safe_execfile(full_filename, self.shell.user_ns)
592 592 finally:
593 593 del self.shell.user_ns['__file__']
594 594 elif full_filename.endswith('.ipy'):
595 595 self.log.info("Running file in user namespace: %s" %
596 596 full_filename)
597 597 self.shell.safe_execfile_ipy(full_filename)
598 598 else:
599 599 self.log.warn("File does not have a .py or .ipy extension: <%s>"
600 600 % full_filename)
601 601 def _run_exec_files(self):
602 602 try:
603 603 if hasattr(self.master_config.Global, 'exec_files'):
604 604 self.log.debug("Running files in Global.exec_files...")
605 605 exec_files = self.master_config.Global.exec_files
606 606 for fname in exec_files:
607 607 self._exec_file(fname)
608 608 except:
609 609 self.log.warn("Unknown error in handling Global.exec_files:")
610 610 self.shell.showtraceback()
611 611
612 612 def _run_cmd_line_code(self):
613 613 if hasattr(self.master_config.Global, 'code_to_run'):
614 614 line = self.master_config.Global.code_to_run
615 615 try:
616 616 self.log.info("Running code given at command line (-c): %s" %
617 617 line)
618 618 self.shell.run_cell(line)
619 619 except:
620 620 self.log.warn("Error in executing line in user namespace: %s" %
621 621 line)
622 622 self.shell.showtraceback()
623 623 return
624 624 # Like Python itself, ignore the second if the first of these is present
625 625 try:
626 626 fname = self.extra_args[0]
627 627 except:
628 628 pass
629 629 else:
630 630 try:
631 631 self._exec_file(fname)
632 632 except:
633 633 self.log.warn("Error in executing file in user namespace: %s" %
634 634 fname)
635 635 self.shell.showtraceback()
636 636
637 637 def start_app(self):
638 638 if self.master_config.Global.interact:
639 639 self.log.debug("Starting IPython's mainloop...")
640 640 self.shell.mainloop()
641 641 else:
642 642 self.log.debug("IPython not interactive, start_app is no-op...")
643 643
644 644
645 645 def load_default_config(ipython_dir=None):
646 646 """Load the default config file from the default ipython_dir.
647 647
648 648 This is useful for embedded shells.
649 649 """
650 650 if ipython_dir is None:
651 651 ipython_dir = get_ipython_dir()
652 652 cl = PyFileConfigLoader(default_config_file_name, ipython_dir)
653 653 config = cl.load_config()
654 654 return config
655 655
656 656
657 657 def launch_new_instance():
658 658 """Create and run a full blown IPython instance"""
659 659 app = IPythonApp()
660 660 app.start()
661 661
662 662
663 663 if __name__ == '__main__':
664 664 launch_new_instance()
@@ -1,208 +1,251 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Pylab (matplotlib) support utilities.
3 3
4 4 Authors
5 5 -------
6 6
7 7 * Fernando Perez.
8 8 * Brian Granger
9 9 """
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Copyright (C) 2009 The IPython Development Team
13 13 #
14 14 # Distributed under the terms of the BSD License. The full license is in
15 15 # the file COPYING, distributed as part of this software.
16 16 #-----------------------------------------------------------------------------
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Imports
20 20 #-----------------------------------------------------------------------------
21 21
22 from cStringIO import StringIO
23
22 24 from IPython.utils.decorators import flag_calls
23 25
24 26 # If user specifies a GUI, that dictates the backend, otherwise we read the
25 27 # user's mpl default from the mpl rc structure
26 28 backends = {'tk': 'TkAgg',
27 29 'gtk': 'GTKAgg',
28 30 'wx': 'WXAgg',
29 31 'qt': 'Qt4Agg', # qt3 not supported
30 32 'qt4': 'Qt4Agg',
31 33 'inline' : 'module://IPython.zmq.pylab.backend_inline'}
32 34
33 35 #-----------------------------------------------------------------------------
34 # Main classes and functions
36 # Matplotlib utilities
37 #-----------------------------------------------------------------------------
38
39 def figsize(sizex, sizey):
40 """Set the default figure size to be [sizex, sizey].
41
42 This is just an easy to remember, convenience wrapper that sets::
43
44 matplotlib.rcParams['figure.figsize'] = [sizex, sizey]
45 """
46 import matplotlib
47 matplotlib.rcParams['figure.figsize'] = [sizex, sizey]
48
49
50 def figure_to_svg(fig):
51 """Convert a figure to svg for inline display."""
52 fc = fig.get_facecolor()
53 ec = fig.get_edgecolor()
54 fig.set_facecolor('white')
55 fig.set_edgecolor('white')
56 try:
57 string_io = StringIO()
58 fig.canvas.print_figure(string_io, format='svg')
59 svg = string_io.getvalue()
60 finally:
61 fig.set_facecolor(fc)
62 fig.set_edgecolor(ec)
63 return svg
64
65
66 # We need a little factory function here to create the closure where
67 # safe_execfile can live.
68 def mpl_runner(safe_execfile):
69 """Factory to return a matplotlib-enabled runner for %run.
70
71 Parameters
72 ----------
73 safe_execfile : function
74 This must be a function with the same interface as the
75 :meth:`safe_execfile` method of IPython.
76
77 Returns
78 -------
79 A function suitable for use as the ``runner`` argument of the %run magic
80 function.
81 """
82
83 def mpl_execfile(fname,*where,**kw):
84 """matplotlib-aware wrapper around safe_execfile.
85
86 Its interface is identical to that of the :func:`execfile` builtin.
87
88 This is ultimately a call to execfile(), but wrapped in safeties to
89 properly handle interactive rendering."""
90
91 import matplotlib
92 import matplotlib.pylab as pylab
93
94 #print '*** Matplotlib runner ***' # dbg
95 # turn off rendering until end of script
96 is_interactive = matplotlib.rcParams['interactive']
97 matplotlib.interactive(False)
98 safe_execfile(fname,*where,**kw)
99 matplotlib.interactive(is_interactive)
100 # make rendering call now, if the user tried to do it
101 if pylab.draw_if_interactive.called:
102 pylab.draw()
103 pylab.draw_if_interactive.called = False
104
105 return mpl_execfile
106
107
108 #-----------------------------------------------------------------------------
109 # Code for initializing matplotlib and importing pylab
35 110 #-----------------------------------------------------------------------------
36 111
37 112
38 113 def find_gui_and_backend(gui=None):
39 114 """Given a gui string return the gui and mpl backend.
40 115
41 116 Parameters
42 117 ----------
43 118 gui : str
44 119 Can be one of ('tk','gtk','wx','qt','qt4','inline').
45 120
46 121 Returns
47 122 -------
48 123 A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg',
49 124 'WXAgg','Qt4Agg','module://IPython.zmq.pylab.backend_inline').
50 125 """
51 126
52 127 import matplotlib
53 128
54 129 if gui:
55 130 # select backend based on requested gui
56 131 backend = backends[gui]
57 132 else:
58 133 backend = matplotlib.rcParams['backend']
59 134 # In this case, we need to find what the appropriate gui selection call
60 135 # should be for IPython, so we can activate inputhook accordingly
61 136 g2b = backends # maps gui names to mpl backend names
62 137 b2g = dict(zip(g2b.values(), g2b.keys())) # reverse dict
63 138 gui = b2g.get(backend, None)
64 139 return gui, backend
65 140
66 141
67 142 def activate_matplotlib(backend):
68 143 """Activate the given backend and set interactive to True."""
69 144
70 145 import matplotlib
71 146 if backend.startswith('module://'):
72 147 # Work around bug in matplotlib: matplotlib.use converts the
73 148 # backend_id to lowercase even if a module name is specified!
74 149 matplotlib.rcParams['backend'] = backend
75 150 else:
76 151 matplotlib.use(backend)
77 152 matplotlib.interactive(True)
78 153
79 154 # This must be imported last in the matplotlib series, after
80 155 # backend/interactivity choices have been made
81 156 import matplotlib.pylab as pylab
82 157
83 158 # XXX For now leave this commented out, but depending on discussions with
84 159 # mpl-dev, we may be able to allow interactive switching...
85 160 #import matplotlib.pyplot
86 161 #matplotlib.pyplot.switch_backend(backend)
87 162
88 163 pylab.show._needmain = False
89 164 # We need to detect at runtime whether show() is called by the user.
90 165 # For this, we wrap it into a decorator which adds a 'called' flag.
91 166 pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive)
92 167
93 168
94 169 def import_pylab(user_ns, backend, import_all=True, shell=None):
95 170 """Import the standard pylab symbols into user_ns."""
96 171
97 172 # Import numpy as np/pyplot as plt are conventions we're trying to
98 173 # somewhat standardize on. Making them available to users by default
99 174 # will greatly help this.
100 175 s = ("import numpy\n"
101 176 "import matplotlib\n"
102 177 "from matplotlib import pylab, mlab, pyplot\n"
103 178 "np = numpy\n"
104 179 "plt = pyplot\n"
105 180 )
106 181 exec s in user_ns
107 182
108 183 if shell is not None:
109 184 exec s in shell.user_ns_hidden
110 185 # If using our svg payload backend, register the post-execution
111 186 # function that will pick up the results for display. This can only be
112 187 # done with access to the real shell object.
113 188 if backend == backends['inline']:
114 from IPython.zmq.pylab.backend_inline import flush_svg, figsize
189 from IPython.zmq.pylab.backend_inline import flush_svg
115 190 from matplotlib import pyplot
116 191 shell.register_post_execute(flush_svg)
117 192 # The typical default figure size is too large for inline use. We
118 193 # might make this a user-configurable parameter later.
119 194 figsize(6.0, 4.0)
120 195 # Add 'figsize' to pyplot and to the user's namespace
121 196 user_ns['figsize'] = pyplot.figsize = figsize
122 197 shell.user_ns_hidden['figsize'] = figsize
123 else:
124 from IPython.zmq.pylab.backend_inline import pastefig
125 from matplotlib import pyplot
126 # Add 'paste' to pyplot and to the user's namespace
127 user_ns['pastefig'] = pyplot.pastefig = pastefig
198
199 # The old pastefig function has been replaced by display
200 # Always add this svg formatter so display works.
201 from IPython.zmq.pylab.backend_inline import figure_to_svg
202 from IPython.core.display import display, display_svg
203 svg_formatter = shell.display_formatter.formatters['image/svg+xml']
204 svg_formatter.for_type_by_name(
205 'matplotlib.figure','Figure',figure_to_svg
206 )
207 # Add display and display_png to the user's namespace
208 user_ns['display'] = display
209 shell.user_ns_hidden['display'] = display
210 user_ns['display_svg'] = display_svg
211 shell.user_ns_hidden['display_svg'] = display_svg
128 212
129 213 if import_all:
130 214 s = ("from matplotlib.pylab import *\n"
131 215 "from numpy import *\n")
132 216 exec s in user_ns
133 217 if shell is not None:
134 218 exec s in shell.user_ns_hidden
135 219
136 220
137 221 def pylab_activate(user_ns, gui=None, import_all=True):
138 222 """Activate pylab mode in the user's namespace.
139 223
140 224 Loads and initializes numpy, matplotlib and friends for interactive use.
141 225
142 226 Parameters
143 227 ----------
144 228 user_ns : dict
145 229 Namespace where the imports will occur.
146 230
147 231 gui : optional, string
148 232 A valid gui name following the conventions of the %gui magic.
149 233
150 234 import_all : optional, boolean
151 235 If true, an 'import *' is done from numpy and pylab.
152 236
153 237 Returns
154 238 -------
155 239 The actual gui used (if not given as input, it was obtained from matplotlib
156 240 itself, and will be needed next to configure IPython's gui integration.
157 241 """
158 242 gui, backend = find_gui_and_backend(gui)
159 243 activate_matplotlib(backend)
160 244 import_pylab(user_ns, backend)
161 245
162 246 print """
163 247 Welcome to pylab, a matplotlib-based Python environment [backend: %s].
164 248 For more information, type 'help(pylab)'.""" % backend
165 249
166 250 return gui
167 251
168 # We need a little factory function here to create the closure where
169 # safe_execfile can live.
170 def mpl_runner(safe_execfile):
171 """Factory to return a matplotlib-enabled runner for %run.
172
173 Parameters
174 ----------
175 safe_execfile : function
176 This must be a function with the same interface as the
177 :meth:`safe_execfile` method of IPython.
178
179 Returns
180 -------
181 A function suitable for use as the ``runner`` argument of the %run magic
182 function.
183 """
184
185 def mpl_execfile(fname,*where,**kw):
186 """matplotlib-aware wrapper around safe_execfile.
187
188 Its interface is identical to that of the :func:`execfile` builtin.
189
190 This is ultimately a call to execfile(), but wrapped in safeties to
191 properly handle interactive rendering."""
192
193 import matplotlib
194 import matplotlib.pylab as pylab
195
196 #print '*** Matplotlib runner ***' # dbg
197 # turn off rendering until end of script
198 is_interactive = matplotlib.rcParams['interactive']
199 matplotlib.interactive(False)
200 safe_execfile(fname,*where,**kw)
201 matplotlib.interactive(is_interactive)
202 # make rendering call now, if the user tried to do it
203 if pylab.draw_if_interactive.called:
204 pylab.draw()
205 pylab.draw_if_interactive.called = False
206
207 return mpl_execfile
208
@@ -1,58 +1,49 b''
1 1 # encoding: utf-8
2 2 """Generic functions for extending IPython.
3 3
4 4 See http://cheeseshop.python.org/pypi/simplegeneric.
5
6 Here is an example from IPython.utils.text::
7
8 def print_lsstring(arg):
9 "Prettier (non-repr-like) and more informative printer for LSString"
10 print "LSString (.p, .n, .l, .s available). Value:"
11 print arg
12
13 print_lsstring = result_display.when_type(LSString)(print_lsstring)
14 5 """
15 6
16 7 #-----------------------------------------------------------------------------
17 8 # Copyright (C) 2008-2009 The IPython Development Team
18 9 #
19 10 # Distributed under the terms of the BSD License. The full license is in
20 11 # the file COPYING, distributed as part of this software.
21 12 #-----------------------------------------------------------------------------
22 13
23 14 #-----------------------------------------------------------------------------
24 15 # Imports
25 16 #-----------------------------------------------------------------------------
26 17
27 18 from IPython.core.error import TryNext
28 19 from IPython.external.simplegeneric import generic
29 20
30 21 #-----------------------------------------------------------------------------
31 22 # Imports
32 23 #-----------------------------------------------------------------------------
33 24
34 25
35 26 @generic
36 27 def inspect_object(obj):
37 28 """Called when you do obj?"""
38 29 raise TryNext
39 30
40 31
41 32 @generic
42 33 def complete_object(obj, prev_completions):
43 34 """Custom completer dispatching for python objects.
44 35
45 36 Parameters
46 37 ----------
47 38 obj : object
48 39 The object to complete.
49 40 prev_completions : list
50 41 List of attributes discovered so far.
51 42
52 43 This should return the list of attributes in obj. If you only wish to
53 44 add to the attributes already discovered normally, return
54 45 own_attrs + prev_completions.
55 46 """
56 47 raise TryNext
57 48
58 49
@@ -1,398 +1,396 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """A dict subclass that supports attribute style access.
4 4
5 5 Authors:
6 6
7 7 * Fernando Perez (original)
8 8 * Brian Granger (refactoring to a dict subclass)
9 9 """
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Copyright (C) 2008-2009 The IPython Development Team
13 13 #
14 14 # Distributed under the terms of the BSD License. The full license is in
15 15 # the file COPYING, distributed as part of this software.
16 16 #-----------------------------------------------------------------------------
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Imports
20 20 #-----------------------------------------------------------------------------
21 21
22 22 from IPython.utils.data import list2dict2
23 23
24 24 __all__ = ['Struct']
25 25
26 26 #-----------------------------------------------------------------------------
27 27 # Code
28 28 #-----------------------------------------------------------------------------
29 29
30 30
31 31 class Struct(dict):
32 32 """A dict subclass with attribute style access.
33 33
34 34 This dict subclass has a a few extra features:
35 35
36 36 * Attribute style access.
37 37 * Protection of class members (like keys, items) when using attribute
38 38 style access.
39 39 * The ability to restrict assignment to only existing keys.
40 40 * Intelligent merging.
41 41 * Overloaded operators.
42 42 """
43 43 _allownew = True
44 44 def __init__(self, *args, **kw):
45 45 """Initialize with a dictionary, another Struct, or data.
46 46
47 47 Parameters
48 48 ----------
49 49 args : dict, Struct
50 50 Initialize with one dict or Struct
51 51 kw : dict
52 52 Initialize with key, value pairs.
53 53
54 54 Examples
55 55 --------
56 56
57 57 >>> s = Struct(a=10,b=30)
58 58 >>> s.a
59 59 10
60 60 >>> s.b
61 61 30
62 62 >>> s2 = Struct(s,c=30)
63 63 >>> s2.keys()
64 64 ['a', 'c', 'b']
65 65 """
66 66 object.__setattr__(self, '_allownew', True)
67 67 dict.__init__(self, *args, **kw)
68 68
69 69 def __setitem__(self, key, value):
70 70 """Set an item with check for allownew.
71 71
72 72 Examples
73 73 --------
74 74
75 75 >>> s = Struct()
76 76 >>> s['a'] = 10
77 77 >>> s.allow_new_attr(False)
78 78 >>> s['a'] = 10
79 79 >>> s['a']
80 80 10
81 81 >>> try:
82 82 ... s['b'] = 20
83 83 ... except KeyError:
84 84 ... print 'this is not allowed'
85 85 ...
86 86 this is not allowed
87 87 """
88 88 if not self._allownew and not self.has_key(key):
89 89 raise KeyError(
90 90 "can't create new attribute %s when allow_new_attr(False)" % key)
91 91 dict.__setitem__(self, key, value)
92 92
93 93 def __setattr__(self, key, value):
94 94 """Set an attr with protection of class members.
95 95
96 96 This calls :meth:`self.__setitem__` but convert :exc:`KeyError` to
97 97 :exc:`AttributeError`.
98 98
99 99 Examples
100 100 --------
101 101
102 102 >>> s = Struct()
103 103 >>> s.a = 10
104 104 >>> s.a
105 105 10
106 106 >>> try:
107 107 ... s.get = 10
108 108 ... except AttributeError:
109 109 ... print "you can't set a class member"
110 110 ...
111 111 you can't set a class member
112 112 """
113 113 # If key is an str it might be a class member or instance var
114 114 if isinstance(key, str):
115 115 # I can't simply call hasattr here because it calls getattr, which
116 116 # calls self.__getattr__, which returns True for keys in
117 117 # self._data. But I only want keys in the class and in
118 118 # self.__dict__
119 119 if key in self.__dict__ or hasattr(Struct, key):
120 120 raise AttributeError(
121 121 'attr %s is a protected member of class Struct.' % key
122 122 )
123 123 try:
124 124 self.__setitem__(key, value)
125 125 except KeyError, e:
126 126 raise AttributeError(e)
127 127
128 128 def __getattr__(self, key):
129 129 """Get an attr by calling :meth:`dict.__getitem__`.
130 130
131 131 Like :meth:`__setattr__`, this method converts :exc:`KeyError` to
132 132 :exc:`AttributeError`.
133 133
134 134 Examples
135 135 --------
136 136
137 137 >>> s = Struct(a=10)
138 138 >>> s.a
139 139 10
140 140 >>> type(s.get)
141 141 <type 'builtin_function_or_method'>
142 142 >>> try:
143 143 ... s.b
144 144 ... except AttributeError:
145 145 ... print "I don't have that key"
146 146 ...
147 147 I don't have that key
148 148 """
149 149 try:
150 150 result = self[key]
151 151 except KeyError:
152 152 raise AttributeError(key)
153 153 else:
154 154 return result
155 155
156 156 def __iadd__(self, other):
157 157 """s += s2 is a shorthand for s.merge(s2).
158 158
159 159 Examples
160 160 --------
161 161
162 162 >>> s = Struct(a=10,b=30)
163 163 >>> s2 = Struct(a=20,c=40)
164 164 >>> s += s2
165 165 >>> s
166 166 {'a': 10, 'c': 40, 'b': 30}
167 167 """
168 168 self.merge(other)
169 169 return self
170 170
171 171 def __add__(self,other):
172 172 """s + s2 -> New Struct made from s.merge(s2).
173 173
174 174 Examples
175 175 --------
176 176
177 177 >>> s1 = Struct(a=10,b=30)
178 178 >>> s2 = Struct(a=20,c=40)
179 179 >>> s = s1 + s2
180 180 >>> s
181 181 {'a': 10, 'c': 40, 'b': 30}
182 182 """
183 183 sout = self.copy()
184 184 sout.merge(other)
185 185 return sout
186 186
187 187 def __sub__(self,other):
188 188 """s1 - s2 -> remove keys in s2 from s1.
189 189
190 190 Examples
191 191 --------
192 192
193 193 >>> s1 = Struct(a=10,b=30)
194 194 >>> s2 = Struct(a=40)
195 195 >>> s = s1 - s2
196 196 >>> s
197 197 {'b': 30}
198 198 """
199 199 sout = self.copy()
200 200 sout -= other
201 201 return sout
202 202
203 203 def __isub__(self,other):
204 204 """Inplace remove keys from self that are in other.
205 205
206 206 Examples
207 207 --------
208 208
209 209 >>> s1 = Struct(a=10,b=30)
210 210 >>> s2 = Struct(a=40)
211 211 >>> s1 -= s2
212 212 >>> s1
213 213 {'b': 30}
214 214 """
215 215 for k in other.keys():
216 216 if self.has_key(k):
217 217 del self[k]
218 218 return self
219 219
220 220 def __dict_invert(self, data):
221 221 """Helper function for merge.
222 222
223 223 Takes a dictionary whose values are lists and returns a dict with
224 224 the elements of each list as keys and the original keys as values.
225 225 """
226 226 outdict = {}
227 227 for k,lst in data.items():
228 228 if isinstance(lst, str):
229 229 lst = lst.split()
230 230 for entry in lst:
231 231 outdict[entry] = k
232 232 return outdict
233 233
234 234 def dict(self):
235 235 return self
236 236
237 237 def copy(self):
238 238 """Return a copy as a Struct.
239 239
240 240 Examples
241 241 --------
242 242
243 243 >>> s = Struct(a=10,b=30)
244 244 >>> s2 = s.copy()
245 245 >>> s2
246 246 {'a': 10, 'b': 30}
247 247 >>> type(s2).__name__
248 248 'Struct'
249 249 """
250 250 return Struct(dict.copy(self))
251 251
252 252 def hasattr(self, key):
253 253 """hasattr function available as a method.
254 254
255 255 Implemented like has_key.
256 256
257 257 Examples
258 258 --------
259 259
260 260 >>> s = Struct(a=10)
261 261 >>> s.hasattr('a')
262 262 True
263 263 >>> s.hasattr('b')
264 264 False
265 265 >>> s.hasattr('get')
266 266 False
267 267 """
268 268 return self.has_key(key)
269 269
270 270 def allow_new_attr(self, allow = True):
271 271 """Set whether new attributes can be created in this Struct.
272 272
273 273 This can be used to catch typos by verifying that the attribute user
274 274 tries to change already exists in this Struct.
275 275 """
276 276 object.__setattr__(self, '_allownew', allow)
277 277
278 278 def merge(self, __loc_data__=None, __conflict_solve=None, **kw):
279 279 """Merge two Structs with customizable conflict resolution.
280 280
281 281 This is similar to :meth:`update`, but much more flexible. First, a
282 282 dict is made from data+key=value pairs. When merging this dict with
283 283 the Struct S, the optional dictionary 'conflict' is used to decide
284 284 what to do.
285 285
286 286 If conflict is not given, the default behavior is to preserve any keys
287 287 with their current value (the opposite of the :meth:`update` method's
288 288 behavior).
289 289
290 290 Parameters
291 291 ----------
292 292 __loc_data : dict, Struct
293 293 The data to merge into self
294 294 __conflict_solve : dict
295 295 The conflict policy dict. The keys are binary functions used to
296 296 resolve the conflict and the values are lists of strings naming
297 297 the keys the conflict resolution function applies to. Instead of
298 298 a list of strings a space separated string can be used, like
299 299 'a b c'.
300 300 kw : dict
301 301 Additional key, value pairs to merge in
302 302
303 303 Notes
304 304 -----
305 305
306 306 The `__conflict_solve` dict is a dictionary of binary functions which will be used to
307 307 solve key conflicts. Here is an example::
308 308
309 309 __conflict_solve = dict(
310 310 func1=['a','b','c'],
311 311 func2=['d','e']
312 312 )
313 313
314 314 In this case, the function :func:`func1` will be used to resolve
315 315 keys 'a', 'b' and 'c' and the function :func:`func2` will be used for
316 316 keys 'd' and 'e'. This could also be written as::
317 317
318 318 __conflict_solve = dict(func1='a b c',func2='d e')
319 319
320 320 These functions will be called for each key they apply to with the
321 321 form::
322 322
323 323 func1(self['a'], other['a'])
324 324
325 325 The return value is used as the final merged value.
326 326
327 327 As a convenience, merge() provides five (the most commonly needed)
328 328 pre-defined policies: preserve, update, add, add_flip and add_s. The
329 329 easiest explanation is their implementation::
330 330
331 331 preserve = lambda old,new: old
332 332 update = lambda old,new: new
333 333 add = lambda old,new: old + new
334 334 add_flip = lambda old,new: new + old # note change of order!
335 335 add_s = lambda old,new: old + ' ' + new # only for str!
336 336
337 337 You can use those four words (as strings) as keys instead
338 338 of defining them as functions, and the merge method will substitute
339 339 the appropriate functions for you.
340 340
341 341 For more complicated conflict resolution policies, you still need to
342 342 construct your own functions.
343 343
344 344 Examples
345 345 --------
346 346
347 347 This show the default policy:
348 348
349 349 >>> s = Struct(a=10,b=30)
350 350 >>> s2 = Struct(a=20,c=40)
351 351 >>> s.merge(s2)
352 352 >>> s
353 353 {'a': 10, 'c': 40, 'b': 30}
354 354
355 355 Now, show how to specify a conflict dict:
356 356
357 357 >>> s = Struct(a=10,b=30)
358 358 >>> s2 = Struct(a=20,b=40)
359 359 >>> conflict = {'update':'a','add':'b'}
360 360 >>> s.merge(s2,conflict)
361 361 >>> s
362 362 {'a': 20, 'b': 70}
363 363 """
364 364
365 365 data_dict = dict(__loc_data__,**kw)
366 366
367 367 # policies for conflict resolution: two argument functions which return
368 368 # the value that will go in the new struct
369 369 preserve = lambda old,new: old
370 370 update = lambda old,new: new
371 371 add = lambda old,new: old + new
372 372 add_flip = lambda old,new: new + old # note change of order!
373 373 add_s = lambda old,new: old + ' ' + new
374 374
375 375 # default policy is to keep current keys when there's a conflict
376 376 conflict_solve = list2dict2(self.keys(), default = preserve)
377 377
378 378 # the conflict_solve dictionary is given by the user 'inverted': we
379 379 # need a name-function mapping, it comes as a function -> names
380 380 # dict. Make a local copy (b/c we'll make changes), replace user
381 381 # strings for the three builtin policies and invert it.
382 382 if __conflict_solve:
383 383 inv_conflict_solve_user = __conflict_solve.copy()
384 384 for name, func in [('preserve',preserve), ('update',update),
385 385 ('add',add), ('add_flip',add_flip),
386 386 ('add_s',add_s)]:
387 387 if name in inv_conflict_solve_user.keys():
388 388 inv_conflict_solve_user[func] = inv_conflict_solve_user[name]
389 389 del inv_conflict_solve_user[name]
390 390 conflict_solve.update(self.__dict_invert(inv_conflict_solve_user))
391 #print 'merge. conflict_solve: '; pprint(conflict_solve) # dbg
392 #print '*'*50,'in merger. conflict_solver:'; pprint(conflict_solve)
393 391 for key in data_dict:
394 392 if key not in self:
395 393 self[key] = data_dict[key]
396 394 else:
397 395 self[key] = conflict_solve[key](self[key],data_dict[key])
398 396
@@ -1,123 +1,73 b''
1 1 """Produce SVG versions of active plots for display by the rich Qt frontend.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Imports
5 5 #-----------------------------------------------------------------------------
6 6 from __future__ import print_function
7 7
8 8 # Standard library imports
9 from cStringIO import StringIO
10 9
11 # System library imports.
12 10 import matplotlib
13 11 from matplotlib.backends.backend_svg import new_figure_manager
14 12 from matplotlib._pylab_helpers import Gcf
15 13
16 14 # Local imports.
17 15 from IPython.core.displaypub import publish_display_data
16 from IPython.lib.pylabtools import figure_to_svg
18 17
19 18 #-----------------------------------------------------------------------------
20 19 # Functions
21 20 #-----------------------------------------------------------------------------
22 21
23 def show(close=True):
22 def show(close=False):
24 23 """Show all figures as SVG payloads sent to the IPython clients.
25 24
26 25 Parameters
27 26 ----------
28 27 close : bool, optional
29 28 If true, a ``plt.close('all')`` call is automatically issued after
30 sending all the SVG figures.
29 sending all the SVG figures. If this is set, the figures will entirely
30 removed from the internal list of figures.
31 31 """
32 32 for figure_manager in Gcf.get_all_fig_managers():
33 send_svg_canvas(figure_manager.canvas)
33 send_svg_figure(figure_manager.canvas.figure)
34 34 if close:
35 35 matplotlib.pyplot.close('all')
36 36
37
37 38 # This flag will be reset by draw_if_interactive when called
38 39 show._draw_called = False
39 40
40 41
41 def figsize(sizex, sizey):
42 """Set the default figure size to be [sizex, sizey].
43
44 This is just an easy to remember, convenience wrapper that sets::
45
46 matplotlib.rcParams['figure.figsize'] = [sizex, sizey]
47 """
48 matplotlib.rcParams['figure.figsize'] = [sizex, sizey]
49
50
51 def pastefig(*figs):
52 """Paste one or more figures into the console workspace.
53
54 If no arguments are given, all available figures are pasted. If the
55 argument list contains references to invalid figures, a warning is printed
56 but the function continues pasting further figures.
57
58 Parameters
59 ----------
60 figs : tuple
61 A tuple that can contain any mixture of integers and figure objects.
62 """
63 if not figs:
64 show(close=False)
65 else:
66 fig_managers = Gcf.get_all_fig_managers()
67 fig_index = dict( [(fm.canvas.figure, fm.canvas) for fm in fig_managers]
68 + [ (fm.canvas.figure.number, fm.canvas) for fm in fig_managers] )
69
70 for fig in figs:
71 canvas = fig_index.get(fig)
72 if canvas is None:
73 print('Warning: figure %s not available.' % fig)
74 else:
75 send_svg_canvas(canvas)
76
77
78 def send_svg_canvas(canvas):
79 """Draw the current canvas and send it as an SVG payload.
80 """
81 # Set the background to white instead so it looks good on black. We store
82 # the current values to restore them at the end.
83 fc = canvas.figure.get_facecolor()
84 ec = canvas.figure.get_edgecolor()
85 canvas.figure.set_facecolor('white')
86 canvas.figure.set_edgecolor('white')
87 try:
88 publish_display_data(
89 'IPython.zmq.pylab.backend_inline.send_svg_canvas',
90 'Matplotlib Plot',
91 {'image/svg+xml' : svg_from_canvas(canvas)}
92 )
93 finally:
94 canvas.figure.set_facecolor(fc)
95 canvas.figure.set_edgecolor(ec)
96
97
98 def svg_from_canvas(canvas):
99 """ Return a string containing the SVG representation of a FigureCanvasSvg.
100 """
101 string_io = StringIO()
102 canvas.print_figure(string_io, format='svg')
103 return string_io.getvalue()
104
105
106 42 def draw_if_interactive():
107 43 """
108 44 Is called after every pylab drawing command
109 45 """
110 46 # We simply flag we were called and otherwise do nothing. At the end of
111 47 # the code execution, a separate call to show_close() will act upon this.
112 48 show._draw_called = True
113 49
114 50
115 51 def flush_svg():
116 52 """Call show, close all open figures, sending all SVG images.
117 53
118 54 This is meant to be called automatically and will call show() if, during
119 55 prior code execution, there had been any calls to draw_if_interactive.
120 56 """
121 57 if show._draw_called:
122 show(close=True)
58 # Show is called with the default close=False here, otherwise, the
59 # Figure will be closed and not available for future plotting.
60 show()
123 61 show._draw_called = False
62
63
64 def send_svg_figure(fig):
65 """Draw the current figure and send it as an SVG payload.
66 """
67 svg = figure_to_svg(fig)
68 publish_display_data(
69 'IPython.zmq.pylab.backend_inline.send_svg_figure',
70 'Matplotlib Plot',
71 {'image/svg+xml' : svg}
72 )
73
@@ -1,605 +1,610 b''
1 1 """A ZMQ-based subclass of InteractiveShell.
2 2
3 3 This code is meant to ease the refactoring of the base InteractiveShell into
4 4 something with a cleaner architecture for 2-process use, without actually
5 5 breaking InteractiveShell itself. So we're doing something a bit ugly, where
6 6 we subclass and override what we want to fix. Once this is working well, we
7 7 can go back to the base class and refactor the code for a cleaner inheritance
8 8 implementation that doesn't rely on so much monkeypatching.
9 9
10 10 But this lets us maintain a fully working IPython as we develop the new
11 11 machinery. This should thus be thought of as scaffolding.
12 12 """
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16 from __future__ import print_function
17 17
18 18 # Stdlib
19 19 import inspect
20 20 import os
21 21 import re
22 22
23 23 # Our own
24 24 from IPython.core.interactiveshell import (
25 25 InteractiveShell, InteractiveShellABC
26 26 )
27 27 from IPython.core import page
28 28 from IPython.core.displayhook import DisplayHook
29 29 from IPython.core.displaypub import DisplayPublisher
30 30 from IPython.core.macro import Macro
31 31 from IPython.core.payloadpage import install_payload_page
32 32 from IPython.utils import io
33 33 from IPython.utils.path import get_py_filename
34 34 from IPython.utils.text import StringTypes
35 35 from IPython.utils.traitlets import Instance, Type, Dict
36 36 from IPython.utils.warn import warn
37 37 from IPython.zmq.session import extract_header
38 38 from session import Session
39 39
40 40 #-----------------------------------------------------------------------------
41 41 # Globals and side-effects
42 42 #-----------------------------------------------------------------------------
43 43
44 44 # Install the payload version of page.
45 45 install_payload_page()
46 46
47 47 #-----------------------------------------------------------------------------
48 48 # Functions and classes
49 49 #-----------------------------------------------------------------------------
50 50
51 51 class ZMQDisplayHook(DisplayHook):
52 52 """A displayhook subclass that publishes data using ZeroMQ."""
53 53
54 54 session = Instance(Session)
55 55 pub_socket = Instance('zmq.Socket')
56 56 parent_header = Dict({})
57 57
58 58 def set_parent(self, parent):
59 59 """Set the parent for outbound messages."""
60 60 self.parent_header = extract_header(parent)
61 61
62 62 def start_displayhook(self):
63 63 self.msg = self.session.msg(u'pyout', {}, parent=self.parent_header)
64 64
65 65 def write_output_prompt(self):
66 66 """Write the output prompt."""
67 67 if self.do_full_cache:
68 68 self.msg['content']['execution_count'] = self.prompt_count
69 69
70 70 def write_format_data(self, format_dict):
71 71 self.msg['content']['data'] = format_dict
72 72
73 73 def finish_displayhook(self):
74 74 """Finish up all displayhook activities."""
75 75 self.session.send(self.pub_socket, self.msg)
76 76 self.msg = None
77 77
78 78
79 79 class ZMQDisplayPublisher(DisplayPublisher):
80 80 """A display publisher that publishes data using a ZeroMQ PUB socket."""
81 81
82 82 session = Instance(Session)
83 83 pub_socket = Instance('zmq.Socket')
84 84 parent_header = Dict({})
85 85
86 86 def set_parent(self, parent):
87 87 """Set the parent for outbound messages."""
88 88 self.parent_header = extract_header(parent)
89 89
90 90 def publish(self, source, data, metadata=None):
91 91 if metadata is None:
92 92 metadata = {}
93 93 self._validate_data(source, data, metadata)
94 94 msg = self.session.msg(u'display_data', {}, parent=self.parent_header)
95 95 msg['content']['source'] = source
96 96 msg['content']['data'] = data
97 97 msg['content']['metadata'] = metadata
98 98 self.pub_socket.send_json(msg)
99 99
100 100
101 101 class ZMQInteractiveShell(InteractiveShell):
102 102 """A subclass of InteractiveShell for ZMQ."""
103 103
104 104 displayhook_class = Type(ZMQDisplayHook)
105 105 display_pub_class = Type(ZMQDisplayPublisher)
106 106
107 107 keepkernel_on_exit = None
108 108
109 109 def init_environment(self):
110 110 """Configure the user's environment.
111 111
112 112 """
113 113 env = os.environ
114 114 # These two ensure 'ls' produces nice coloring on BSD-derived systems
115 115 env['TERM'] = 'xterm-color'
116 116 env['CLICOLOR'] = '1'
117 117 # Since normal pagers don't work at all (over pexpect we don't have
118 118 # single-key control of the subprocess), try to disable paging in
119 119 # subprocesses as much as possible.
120 120 env['PAGER'] = 'cat'
121 121 env['GIT_PAGER'] = 'cat'
122 122
123 123 def auto_rewrite_input(self, cmd):
124 124 """Called to show the auto-rewritten input for autocall and friends.
125 125
126 126 FIXME: this payload is currently not correctly processed by the
127 127 frontend.
128 128 """
129 129 new = self.displayhook.prompt1.auto_rewrite() + cmd
130 130 payload = dict(
131 131 source='IPython.zmq.zmqshell.ZMQInteractiveShell.auto_rewrite_input',
132 132 transformed_input=new,
133 133 )
134 134 self.payload_manager.write_payload(payload)
135 135
136 136 def ask_exit(self):
137 137 """Engage the exit actions."""
138 138 payload = dict(
139 139 source='IPython.zmq.zmqshell.ZMQInteractiveShell.ask_exit',
140 140 exit=True,
141 141 keepkernel=self.keepkernel_on_exit,
142 142 )
143 143 self.payload_manager.write_payload(payload)
144 144
145 145 def _showtraceback(self, etype, evalue, stb):
146 146
147 147 exc_content = {
148 148 u'traceback' : stb,
149 149 u'ename' : unicode(etype.__name__),
150 150 u'evalue' : unicode(evalue)
151 151 }
152 152
153 153 dh = self.displayhook
154 154 # Send exception info over pub socket for other clients than the caller
155 155 # to pick up
156 156 exc_msg = dh.session.send(dh.pub_socket, u'pyerr', exc_content, dh.parent_header)
157 157
158 158 # FIXME - Hack: store exception info in shell object. Right now, the
159 159 # caller is reading this info after the fact, we need to fix this logic
160 160 # to remove this hack. Even uglier, we need to store the error status
161 161 # here, because in the main loop, the logic that sets it is being
162 162 # skipped because runlines swallows the exceptions.
163 163 exc_content[u'status'] = u'error'
164 164 self._reply_content = exc_content
165 165 # /FIXME
166 166
167 167 return exc_content
168 168
169 169 #------------------------------------------------------------------------
170 170 # Magic overrides
171 171 #------------------------------------------------------------------------
172 172 # Once the base class stops inheriting from magic, this code needs to be
173 173 # moved into a separate machinery as well. For now, at least isolate here
174 174 # the magics which this class needs to implement differently from the base
175 175 # class, or that are unique to it.
176 176
177 177 def magic_doctest_mode(self,parameter_s=''):
178 178 """Toggle doctest mode on and off.
179 179
180 180 This mode is intended to make IPython behave as much as possible like a
181 181 plain Python shell, from the perspective of how its prompts, exceptions
182 182 and output look. This makes it easy to copy and paste parts of a
183 183 session into doctests. It does so by:
184 184
185 185 - Changing the prompts to the classic ``>>>`` ones.
186 186 - Changing the exception reporting mode to 'Plain'.
187 187 - Disabling pretty-printing of output.
188 188
189 189 Note that IPython also supports the pasting of code snippets that have
190 190 leading '>>>' and '...' prompts in them. This means that you can paste
191 191 doctests from files or docstrings (even if they have leading
192 192 whitespace), and the code will execute correctly. You can then use
193 193 '%history -t' to see the translated history; this will give you the
194 194 input after removal of all the leading prompts and whitespace, which
195 195 can be pasted back into an editor.
196 196
197 197 With these features, you can switch into this mode easily whenever you
198 198 need to do testing and changes to doctests, without having to leave
199 199 your existing IPython session.
200 200 """
201 201
202 202 from IPython.utils.ipstruct import Struct
203 203
204 204 # Shorthands
205 205 shell = self.shell
206 disp_formatter = self.shell.display_formatter
207 ptformatter = disp_formatter.formatters['text/plain']
206 208 # dstore is a data store kept in the instance metadata bag to track any
207 209 # changes we make, so we can undo them later.
208 210 dstore = shell.meta.setdefault('doctest_mode', Struct())
209 211 save_dstore = dstore.setdefault
210 212
211 213 # save a few values we'll need to recover later
212 214 mode = save_dstore('mode', False)
213 save_dstore('rc_pprint', shell.pprint)
215 save_dstore('rc_pprint', ptformatter.pprint)
216 save_dstore('rc_plain_text_only',disp_formatter.plain_text_only)
214 217 save_dstore('xmode', shell.InteractiveTB.mode)
215 218
216 219 if mode == False:
217 220 # turn on
218 shell.pprint = False
221 ptformatter.pprint = False
222 disp_formatter.plain_text_only = True
219 223 shell.magic_xmode('Plain')
220 224 else:
221 225 # turn off
222 shell.pprint = dstore.rc_pprint
226 ptformatter.pprint = dstore.rc_pprint
227 disp_formatter.plain_text_only = dstore.rc_plain_text_only
223 228 shell.magic_xmode(dstore.xmode)
224 229
225 230 # Store new mode and inform on console
226 231 dstore.mode = bool(1-int(mode))
227 232 mode_label = ['OFF','ON'][dstore.mode]
228 233 print('Doctest mode is:', mode_label)
229 234
230 235 # Send the payload back so that clients can modify their prompt display
231 236 payload = dict(
232 237 source='IPython.zmq.zmqshell.ZMQInteractiveShell.magic_doctest_mode',
233 238 mode=dstore.mode)
234 239 self.payload_manager.write_payload(payload)
235 240
236 241 def magic_edit(self,parameter_s='',last_call=['','']):
237 242 """Bring up an editor and execute the resulting code.
238 243
239 244 Usage:
240 245 %edit [options] [args]
241 246
242 247 %edit runs IPython's editor hook. The default version of this hook is
243 248 set to call the __IPYTHON__.rc.editor command. This is read from your
244 249 environment variable $EDITOR. If this isn't found, it will default to
245 250 vi under Linux/Unix and to notepad under Windows. See the end of this
246 251 docstring for how to change the editor hook.
247 252
248 253 You can also set the value of this editor via the command line option
249 254 '-editor' or in your ipythonrc file. This is useful if you wish to use
250 255 specifically for IPython an editor different from your typical default
251 256 (and for Windows users who typically don't set environment variables).
252 257
253 258 This command allows you to conveniently edit multi-line code right in
254 259 your IPython session.
255 260
256 261 If called without arguments, %edit opens up an empty editor with a
257 262 temporary file and will execute the contents of this file when you
258 263 close it (don't forget to save it!).
259 264
260 265
261 266 Options:
262 267
263 268 -n <number>: open the editor at a specified line number. By default,
264 269 the IPython editor hook uses the unix syntax 'editor +N filename', but
265 270 you can configure this by providing your own modified hook if your
266 271 favorite editor supports line-number specifications with a different
267 272 syntax.
268 273
269 274 -p: this will call the editor with the same data as the previous time
270 275 it was used, regardless of how long ago (in your current session) it
271 276 was.
272 277
273 278 -r: use 'raw' input. This option only applies to input taken from the
274 279 user's history. By default, the 'processed' history is used, so that
275 280 magics are loaded in their transformed version to valid Python. If
276 281 this option is given, the raw input as typed as the command line is
277 282 used instead. When you exit the editor, it will be executed by
278 283 IPython's own processor.
279 284
280 285 -x: do not execute the edited code immediately upon exit. This is
281 286 mainly useful if you are editing programs which need to be called with
282 287 command line arguments, which you can then do using %run.
283 288
284 289
285 290 Arguments:
286 291
287 292 If arguments are given, the following possibilites exist:
288 293
289 294 - The arguments are numbers or pairs of colon-separated numbers (like
290 295 1 4:8 9). These are interpreted as lines of previous input to be
291 296 loaded into the editor. The syntax is the same of the %macro command.
292 297
293 298 - If the argument doesn't start with a number, it is evaluated as a
294 299 variable and its contents loaded into the editor. You can thus edit
295 300 any string which contains python code (including the result of
296 301 previous edits).
297 302
298 303 - If the argument is the name of an object (other than a string),
299 304 IPython will try to locate the file where it was defined and open the
300 305 editor at the point where it is defined. You can use `%edit function`
301 306 to load an editor exactly at the point where 'function' is defined,
302 307 edit it and have the file be executed automatically.
303 308
304 309 If the object is a macro (see %macro for details), this opens up your
305 310 specified editor with a temporary file containing the macro's data.
306 311 Upon exit, the macro is reloaded with the contents of the file.
307 312
308 313 Note: opening at an exact line is only supported under Unix, and some
309 314 editors (like kedit and gedit up to Gnome 2.8) do not understand the
310 315 '+NUMBER' parameter necessary for this feature. Good editors like
311 316 (X)Emacs, vi, jed, pico and joe all do.
312 317
313 318 - If the argument is not found as a variable, IPython will look for a
314 319 file with that name (adding .py if necessary) and load it into the
315 320 editor. It will execute its contents with execfile() when you exit,
316 321 loading any code in the file into your interactive namespace.
317 322
318 323 After executing your code, %edit will return as output the code you
319 324 typed in the editor (except when it was an existing file). This way
320 325 you can reload the code in further invocations of %edit as a variable,
321 326 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
322 327 the output.
323 328
324 329 Note that %edit is also available through the alias %ed.
325 330
326 331 This is an example of creating a simple function inside the editor and
327 332 then modifying it. First, start up the editor:
328 333
329 334 In [1]: ed
330 335 Editing... done. Executing edited code...
331 336 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
332 337
333 338 We can then call the function foo():
334 339
335 340 In [2]: foo()
336 341 foo() was defined in an editing session
337 342
338 343 Now we edit foo. IPython automatically loads the editor with the
339 344 (temporary) file where foo() was previously defined:
340 345
341 346 In [3]: ed foo
342 347 Editing... done. Executing edited code...
343 348
344 349 And if we call foo() again we get the modified version:
345 350
346 351 In [4]: foo()
347 352 foo() has now been changed!
348 353
349 354 Here is an example of how to edit a code snippet successive
350 355 times. First we call the editor:
351 356
352 357 In [5]: ed
353 358 Editing... done. Executing edited code...
354 359 hello
355 360 Out[5]: "print 'hello'n"
356 361
357 362 Now we call it again with the previous output (stored in _):
358 363
359 364 In [6]: ed _
360 365 Editing... done. Executing edited code...
361 366 hello world
362 367 Out[6]: "print 'hello world'n"
363 368
364 369 Now we call it with the output #8 (stored in _8, also as Out[8]):
365 370
366 371 In [7]: ed _8
367 372 Editing... done. Executing edited code...
368 373 hello again
369 374 Out[7]: "print 'hello again'n"
370 375
371 376
372 377 Changing the default editor hook:
373 378
374 379 If you wish to write your own editor hook, you can put it in a
375 380 configuration file which you load at startup time. The default hook
376 381 is defined in the IPython.core.hooks module, and you can use that as a
377 382 starting example for further modifications. That file also has
378 383 general instructions on how to set a new hook for use once you've
379 384 defined it."""
380 385
381 386 # FIXME: This function has become a convoluted mess. It needs a
382 387 # ground-up rewrite with clean, simple logic.
383 388
384 389 def make_filename(arg):
385 390 "Make a filename from the given args"
386 391 try:
387 392 filename = get_py_filename(arg)
388 393 except IOError:
389 394 if args.endswith('.py'):
390 395 filename = arg
391 396 else:
392 397 filename = None
393 398 return filename
394 399
395 400 # custom exceptions
396 401 class DataIsObject(Exception): pass
397 402
398 403 opts,args = self.parse_options(parameter_s,'prn:')
399 404 # Set a few locals from the options for convenience:
400 405 opts_p = opts.has_key('p')
401 406 opts_r = opts.has_key('r')
402 407
403 408 # Default line number value
404 409 lineno = opts.get('n',None)
405 410 if lineno is not None:
406 411 try:
407 412 lineno = int(lineno)
408 413 except:
409 414 warn("The -n argument must be an integer.")
410 415 return
411 416
412 417 if opts_p:
413 418 args = '_%s' % last_call[0]
414 419 if not self.shell.user_ns.has_key(args):
415 420 args = last_call[1]
416 421
417 422 # use last_call to remember the state of the previous call, but don't
418 423 # let it be clobbered by successive '-p' calls.
419 424 try:
420 425 last_call[0] = self.shell.displayhook.prompt_count
421 426 if not opts_p:
422 427 last_call[1] = parameter_s
423 428 except:
424 429 pass
425 430
426 431 # by default this is done with temp files, except when the given
427 432 # arg is a filename
428 433 use_temp = 1
429 434
430 435 if re.match(r'\d',args):
431 436 # Mode where user specifies ranges of lines, like in %macro.
432 437 # This means that you can't edit files whose names begin with
433 438 # numbers this way. Tough.
434 439 ranges = args.split()
435 440 data = ''.join(self.extract_input_slices(ranges,opts_r))
436 441 elif args.endswith('.py'):
437 442 filename = make_filename(args)
438 443 data = ''
439 444 use_temp = 0
440 445 elif args:
441 446 try:
442 447 # Load the parameter given as a variable. If not a string,
443 448 # process it as an object instead (below)
444 449
445 450 #print '*** args',args,'type',type(args) # dbg
446 451 data = eval(args,self.shell.user_ns)
447 452 if not type(data) in StringTypes:
448 453 raise DataIsObject
449 454
450 455 except (NameError,SyntaxError):
451 456 # given argument is not a variable, try as a filename
452 457 filename = make_filename(args)
453 458 if filename is None:
454 459 warn("Argument given (%s) can't be found as a variable "
455 460 "or as a filename." % args)
456 461 return
457 462
458 463 data = ''
459 464 use_temp = 0
460 465 except DataIsObject:
461 466
462 467 # macros have a special edit function
463 468 if isinstance(data,Macro):
464 469 self._edit_macro(args,data)
465 470 return
466 471
467 472 # For objects, try to edit the file where they are defined
468 473 try:
469 474 filename = inspect.getabsfile(data)
470 475 if 'fakemodule' in filename.lower() and inspect.isclass(data):
471 476 # class created by %edit? Try to find source
472 477 # by looking for method definitions instead, the
473 478 # __module__ in those classes is FakeModule.
474 479 attrs = [getattr(data, aname) for aname in dir(data)]
475 480 for attr in attrs:
476 481 if not inspect.ismethod(attr):
477 482 continue
478 483 filename = inspect.getabsfile(attr)
479 484 if filename and 'fakemodule' not in filename.lower():
480 485 # change the attribute to be the edit target instead
481 486 data = attr
482 487 break
483 488
484 489 datafile = 1
485 490 except TypeError:
486 491 filename = make_filename(args)
487 492 datafile = 1
488 493 warn('Could not find file where `%s` is defined.\n'
489 494 'Opening a file named `%s`' % (args,filename))
490 495 # Now, make sure we can actually read the source (if it was in
491 496 # a temp file it's gone by now).
492 497 if datafile:
493 498 try:
494 499 if lineno is None:
495 500 lineno = inspect.getsourcelines(data)[1]
496 501 except IOError:
497 502 filename = make_filename(args)
498 503 if filename is None:
499 504 warn('The file `%s` where `%s` was defined cannot '
500 505 'be read.' % (filename,data))
501 506 return
502 507 use_temp = 0
503 508 else:
504 509 data = ''
505 510
506 511 if use_temp:
507 512 filename = self.shell.mktempfile(data)
508 513 print('IPython will make a temporary file named:', filename)
509 514
510 515 # Make sure we send to the client an absolute path, in case the working
511 516 # directory of client and kernel don't match
512 517 filename = os.path.abspath(filename)
513 518
514 519 payload = {
515 520 'source' : 'IPython.zmq.zmqshell.ZMQInteractiveShell.edit_magic',
516 521 'filename' : filename,
517 522 'line_number' : lineno
518 523 }
519 524 self.payload_manager.write_payload(payload)
520 525
521 526 def magic_gui(self, *args, **kwargs):
522 527 raise NotImplementedError(
523 528 'GUI support must be enabled in command line options.')
524 529
525 530 def magic_pylab(self, *args, **kwargs):
526 531 raise NotImplementedError(
527 532 'pylab support must be enabled in command line options.')
528 533
529 534 # A few magics that are adapted to the specifics of using pexpect and a
530 535 # remote terminal
531 536
532 537 def magic_clear(self, arg_s):
533 538 """Clear the terminal."""
534 539 if os.name == 'posix':
535 540 self.shell.system("clear")
536 541 else:
537 542 self.shell.system("cls")
538 543
539 544 if os.name == 'nt':
540 545 # This is the usual name in windows
541 546 magic_cls = magic_clear
542 547
543 548 # Terminal pagers won't work over pexpect, but we do have our own pager
544 549
545 550 def magic_less(self, arg_s):
546 551 """Show a file through the pager.
547 552
548 553 Files ending in .py are syntax-highlighted."""
549 554 cont = open(arg_s).read()
550 555 if arg_s.endswith('.py'):
551 556 cont = self.shell.pycolorize(cont)
552 557 page.page(cont)
553 558
554 559 magic_more = magic_less
555 560
556 561 # Man calls a pager, so we also need to redefine it
557 562 if os.name == 'posix':
558 563 def magic_man(self, arg_s):
559 564 """Find the man page for the given command and display in pager."""
560 565 page.page(self.shell.getoutput('man %s | col -b' % arg_s,
561 566 split=False))
562 567
563 568 # FIXME: this is specific to the GUI, so we should let the gui app load
564 569 # magics at startup that are only for the gui. Once the gui app has proper
565 570 # profile and configuration management, we can have it initialize a kernel
566 571 # with a special config file that provides these.
567 572 def magic_guiref(self, arg_s):
568 573 """Show a basic reference about the GUI console."""
569 574 from IPython.core.usage import gui_reference
570 575 page.page(gui_reference, auto_html=True)
571 576
572 577 def magic_loadpy(self, arg_s):
573 578 """Load a .py python script into the GUI console.
574 579
575 580 This magic command can either take a local filename or a url::
576 581
577 582 %loadpy myscript.py
578 583 %loadpy http://www.example.com/myscript.py
579 584 """
580 585 if not arg_s.endswith('.py'):
581 586 raise ValueError('%%load only works with .py files: %s' % arg_s)
582 587 if arg_s.startswith('http'):
583 588 import urllib2
584 589 response = urllib2.urlopen(arg_s)
585 590 content = response.read()
586 591 else:
587 592 content = open(arg_s).read()
588 593 payload = dict(
589 594 source='IPython.zmq.zmqshell.ZMQInteractiveShell.magic_loadpy',
590 595 text=content
591 596 )
592 597 self.payload_manager.write_payload(payload)
593 598
594 599 def magic_Exit(self, parameter_s=''):
595 600 """Exit IPython. If the -k option is provided, the kernel will be left
596 601 running. Otherwise, it will shutdown without prompting.
597 602 """
598 603 opts,args = self.parse_options(parameter_s,'k')
599 604 self.shell.keepkernel_on_exit = opts.has_key('k')
600 605 self.shell.ask_exit()
601 606
602 607 # Add aliases as magics so all common forms work: exit, quit, Exit, Quit.
603 608 magic_exit = magic_quit = magic_Quit = magic_Exit
604 609
605 610 InteractiveShellABC.register(ZMQInteractiveShell)
@@ -1,375 +1,369 b''
1 1 # encoding: utf-8
2 2 """
3 3 This module defines the things that are used in setup.py for building IPython
4 4
5 5 This includes:
6 6
7 7 * The basic arguments to setup
8 8 * Functions for finding things like packages, package data, etc.
9 9 * A function for checking dependencies.
10 10 """
11 11 from __future__ import print_function
12 12
13 13 #-------------------------------------------------------------------------------
14 14 # Copyright (C) 2008 The IPython Development Team
15 15 #
16 16 # Distributed under the terms of the BSD License. The full license is in
17 17 # the file COPYING, distributed as part of this software.
18 18 #-------------------------------------------------------------------------------
19 19
20 20 #-------------------------------------------------------------------------------
21 21 # Imports
22 22 #-------------------------------------------------------------------------------
23 23 import os
24 24 import sys
25 25
26 26 from ConfigParser import ConfigParser
27 27 from distutils.command.build_py import build_py
28 28 from glob import glob
29 29
30 30 from setupext import install_data_ext
31 31
32 32 #-------------------------------------------------------------------------------
33 33 # Useful globals and utility functions
34 34 #-------------------------------------------------------------------------------
35 35
36 36 # A few handy globals
37 37 isfile = os.path.isfile
38 38 pjoin = os.path.join
39 39
40 40 def oscmd(s):
41 41 print(">", s)
42 42 os.system(s)
43 43
44 44 # A little utility we'll need below, since glob() does NOT allow you to do
45 45 # exclusion on multiple endings!
46 46 def file_doesnt_endwith(test,endings):
47 47 """Return true if test is a file and its name does NOT end with any
48 48 of the strings listed in endings."""
49 49 if not isfile(test):
50 50 return False
51 51 for e in endings:
52 52 if test.endswith(e):
53 53 return False
54 54 return True
55 55
56 56 #---------------------------------------------------------------------------
57 57 # Basic project information
58 58 #---------------------------------------------------------------------------
59 59
60 60 # release.py contains version, authors, license, url, keywords, etc.
61 61 execfile(pjoin('IPython','core','release.py'))
62 62
63 63 # Create a dict with the basic information
64 64 # This dict is eventually passed to setup after additional keys are added.
65 65 setup_args = dict(
66 66 name = name,
67 67 version = version,
68 68 description = description,
69 69 long_description = long_description,
70 70 author = author,
71 71 author_email = author_email,
72 72 url = url,
73 73 download_url = download_url,
74 74 license = license,
75 75 platforms = platforms,
76 76 keywords = keywords,
77 77 cmdclass = {'install_data': install_data_ext},
78 78 )
79 79
80 80
81 81 #---------------------------------------------------------------------------
82 82 # Find packages
83 83 #---------------------------------------------------------------------------
84 84
85 85 def add_package(packages,pname,config=False,tests=False,scripts=False,
86 86 others=None):
87 87 """
88 88 Add a package to the list of packages, including certain subpackages.
89 89 """
90 90 packages.append('.'.join(['IPython',pname]))
91 91 if config:
92 92 packages.append('.'.join(['IPython',pname,'config']))
93 93 if tests:
94 94 packages.append('.'.join(['IPython',pname,'tests']))
95 95 if scripts:
96 96 packages.append('.'.join(['IPython',pname,'scripts']))
97 97 if others is not None:
98 98 for o in others:
99 99 packages.append('.'.join(['IPython',pname,o]))
100 100
101 101 def find_packages():
102 102 """
103 103 Find all of IPython's packages.
104 104 """
105 105 packages = ['IPython']
106 106 add_package(packages, 'config', tests=True, others=['default','profile'])
107 107 add_package(packages, 'core', tests=True)
108 108 add_package(packages, 'deathrow', tests=True)
109 109 add_package(packages, 'extensions')
110 110 add_package(packages, 'external')
111 111 add_package(packages, 'frontend')
112 112 add_package(packages, 'frontend.qt')
113 113 add_package(packages, 'frontend.qt.console', tests=True)
114 114 add_package(packages, 'frontend.terminal', tests=True)
115 115 add_package(packages, 'kernel', config=False, tests=True, scripts=True)
116 116 add_package(packages, 'kernel.core', config=False, tests=True)
117 117 add_package(packages, 'lib', tests=True)
118 118 add_package(packages, 'quarantine', tests=True)
119 119 add_package(packages, 'scripts')
120 120 add_package(packages, 'testing', tests=True)
121 121 add_package(packages, 'testing.plugin', tests=False)
122 122 add_package(packages, 'utils', tests=True)
123 123 add_package(packages, 'zmq')
124 124 add_package(packages, 'zmq.pylab')
125 125 return packages
126 126
127 127 #---------------------------------------------------------------------------
128 128 # Find package data
129 129 #---------------------------------------------------------------------------
130 130
131 131 def find_package_data():
132 132 """
133 133 Find IPython's package_data.
134 134 """
135 135 # This is not enough for these things to appear in an sdist.
136 136 # We need to muck with the MANIFEST to get this to work
137 137 package_data = {
138 138 'IPython.config.userconfig' : ['*'],
139 139 'IPython.testing' : ['*.txt']
140 140 }
141 141 return package_data
142 142
143 143
144 144 #---------------------------------------------------------------------------
145 145 # Find data files
146 146 #---------------------------------------------------------------------------
147 147
148 148 def make_dir_struct(tag,base,out_base):
149 149 """Make the directory structure of all files below a starting dir.
150 150
151 151 This is just a convenience routine to help build a nested directory
152 152 hierarchy because distutils is too stupid to do this by itself.
153 153
154 154 XXX - this needs a proper docstring!
155 155 """
156 156
157 157 # we'll use these a lot below
158 158 lbase = len(base)
159 159 pathsep = os.path.sep
160 160 lpathsep = len(pathsep)
161 161
162 162 out = []
163 163 for (dirpath,dirnames,filenames) in os.walk(base):
164 164 # we need to strip out the dirpath from the base to map it to the
165 165 # output (installation) path. This requires possibly stripping the
166 166 # path separator, because otherwise pjoin will not work correctly
167 167 # (pjoin('foo/','/bar') returns '/bar').
168 168
169 169 dp_eff = dirpath[lbase:]
170 170 if dp_eff.startswith(pathsep):
171 171 dp_eff = dp_eff[lpathsep:]
172 172 # The output path must be anchored at the out_base marker
173 173 out_path = pjoin(out_base,dp_eff)
174 174 # Now we can generate the final filenames. Since os.walk only produces
175 175 # filenames, we must join back with the dirpath to get full valid file
176 176 # paths:
177 177 pfiles = [pjoin(dirpath,f) for f in filenames]
178 178 # Finally, generate the entry we need, which is a pari of (output
179 179 # path, files) for use as a data_files parameter in install_data.
180 180 out.append((out_path, pfiles))
181 181
182 182 return out
183 183
184 184
185 185 def find_data_files():
186 186 """
187 187 Find IPython's data_files.
188 188
189 189 Most of these are docs.
190 190 """
191 191
192 192 docdirbase = pjoin('share', 'doc', 'ipython')
193 193 manpagebase = pjoin('share', 'man', 'man1')
194 194
195 195 # Simple file lists can be made by hand
196 196 manpages = filter(isfile, glob(pjoin('docs','man','*.1.gz')))
197 197 igridhelpfiles = filter(isfile,
198 198 glob(pjoin('IPython','extensions','igrid_help.*')))
199 199
200 200 # For nested structures, use the utility above
201 201 example_files = make_dir_struct(
202 202 'data',
203 203 pjoin('docs','examples'),
204 204 pjoin(docdirbase,'examples')
205 205 )
206 206 manual_files = make_dir_struct(
207 207 'data',
208 208 pjoin('docs','dist'),
209 209 pjoin(docdirbase,'manual')
210 210 )
211 211
212 212 # And assemble the entire output list
213 213 data_files = [ (manpagebase, manpages),
214 214 (pjoin(docdirbase, 'extensions'), igridhelpfiles),
215 215 ] + manual_files + example_files
216 216
217 ## import pprint # dbg
218 ## print('*'*80)
219 ## print('data files')
220 ## pprint.pprint(data_files)
221 ## print('*'*80)
222
223 217 return data_files
224 218
225 219
226 220 def make_man_update_target(manpage):
227 221 """Return a target_update-compliant tuple for the given manpage.
228 222
229 223 Parameters
230 224 ----------
231 225 manpage : string
232 226 Name of the manpage, must include the section number (trailing number).
233 227
234 228 Example
235 229 -------
236 230
237 231 >>> make_man_update_target('ipython.1') #doctest: +NORMALIZE_WHITESPACE
238 232 ('docs/man/ipython.1.gz',
239 233 ['docs/man/ipython.1'],
240 234 'cd docs/man && gzip -9c ipython.1 > ipython.1.gz')
241 235 """
242 236 man_dir = pjoin('docs', 'man')
243 237 manpage_gz = manpage + '.gz'
244 238 manpath = pjoin(man_dir, manpage)
245 239 manpath_gz = pjoin(man_dir, manpage_gz)
246 240 gz_cmd = ( "cd %(man_dir)s && gzip -9c %(manpage)s > %(manpage_gz)s" %
247 241 locals() )
248 242 return (manpath_gz, [manpath], gz_cmd)
249 243
250 244 #---------------------------------------------------------------------------
251 245 # Find scripts
252 246 #---------------------------------------------------------------------------
253 247
254 248 def find_scripts():
255 249 """
256 250 Find IPython's scripts.
257 251 """
258 252 kernel_scripts = pjoin('IPython','kernel','scripts')
259 253 main_scripts = pjoin('IPython','scripts')
260 254 scripts = [pjoin(kernel_scripts, 'ipengine'),
261 255 pjoin(kernel_scripts, 'ipcontroller'),
262 256 pjoin(kernel_scripts, 'ipcluster'),
263 257 pjoin(main_scripts, 'ipython'),
264 258 pjoin(main_scripts, 'ipython-qtconsole'),
265 259 pjoin(main_scripts, 'pycolor'),
266 260 pjoin(main_scripts, 'irunner'),
267 261 pjoin(main_scripts, 'iptest')
268 262 ]
269 263
270 264 # Script to be run by the windows binary installer after the default setup
271 265 # routine, to add shortcuts and similar windows-only things. Windows
272 266 # post-install scripts MUST reside in the scripts/ dir, otherwise distutils
273 267 # doesn't find them.
274 268 if 'bdist_wininst' in sys.argv:
275 269 if len(sys.argv) > 2 and \
276 270 ('sdist' in sys.argv or 'bdist_rpm' in sys.argv):
277 271 print("ERROR: bdist_wininst must be run alone. Exiting.",
278 272 file=sys.stderr)
279 273 sys.exit(1)
280 274 scripts.append(pjoin('scripts','ipython_win_post_install.py'))
281 275
282 276 return scripts
283 277
284 278 #---------------------------------------------------------------------------
285 279 # Verify all dependencies
286 280 #---------------------------------------------------------------------------
287 281
288 282 def check_for_dependencies():
289 283 """Check for IPython's dependencies.
290 284
291 285 This function should NOT be called if running under setuptools!
292 286 """
293 287 from setupext.setupext import (
294 288 print_line, print_raw, print_status,
295 289 check_for_zopeinterface, check_for_twisted,
296 290 check_for_foolscap, check_for_pyopenssl,
297 291 check_for_sphinx, check_for_pygments,
298 292 check_for_nose, check_for_pexpect
299 293 )
300 294 print_line()
301 295 print_raw("BUILDING IPYTHON")
302 296 print_status('python', sys.version)
303 297 print_status('platform', sys.platform)
304 298 if sys.platform == 'win32':
305 299 print_status('Windows version', sys.getwindowsversion())
306 300
307 301 print_raw("")
308 302 print_raw("OPTIONAL DEPENDENCIES")
309 303
310 304 check_for_zopeinterface()
311 305 check_for_twisted()
312 306 check_for_foolscap()
313 307 check_for_pyopenssl()
314 308 check_for_sphinx()
315 309 check_for_pygments()
316 310 check_for_nose()
317 311 check_for_pexpect()
318 312
319 313
320 314 def record_commit_info(pkg_dir, build_cmd=build_py):
321 315 """ Return extended build command class for recording commit
322 316
323 317 The extended command tries to run git to find the current commit, getting
324 318 the empty string if it fails. It then writes the commit hash into a file
325 319 in the `pkg_dir` path, named ``.git_commit_info.ini``.
326 320
327 321 In due course this information can be used by the package after it is
328 322 installed, to tell you what commit it was installed from if known.
329 323
330 324 To make use of this system, you need a package with a .git_commit_info.ini
331 325 file - e.g. ``myproject/.git_commit_info.ini`` - that might well look like
332 326 this::
333 327
334 328 # This is an ini file that may contain information about the code state
335 329 [commit hash]
336 330 # The line below may contain a valid hash if it has been substituted
337 331 # during 'git archive'
338 332 archive_subst_hash=$Format:%h$
339 333 # This line may be modified by the install process
340 334 install_hash=
341 335
342 336 The .git_commit_info file above is also designed to be used with git
343 337 substitution - so you probably also want a ``.gitattributes`` file in the
344 338 root directory of your working tree that contains something like this::
345 339
346 340 myproject/.git_commit_info.ini export-subst
347 341
348 342 That will cause the ``.git_commit_info.ini`` file to get filled in by ``git
349 343 archive`` - useful in case someone makes such an archive - for example with
350 344 via the github 'download source' button.
351 345
352 346 Although all the above will work as is, you might consider having something
353 347 like a ``get_info()`` function in your package to display the commit
354 348 information at the terminal. See the ``pkg_info.py`` module in the nipy
355 349 package for an example.
356 350 """
357 351 class MyBuildPy(build_cmd):
358 352 ''' Subclass to write commit data into installation tree '''
359 353 def run(self):
360 354 build_py.run(self)
361 355 import subprocess
362 356 proc = subprocess.Popen('git rev-parse --short HEAD',
363 357 stdout=subprocess.PIPE,
364 358 stderr=subprocess.PIPE,
365 359 shell=True)
366 360 repo_commit, _ = proc.communicate()
367 361 # We write the installation commit even if it's empty
368 362 cfg_parser = ConfigParser()
369 363 cfg_parser.read(pjoin(pkg_dir, '.git_commit_info.ini'))
370 364 cfg_parser.set('commit hash', 'install_hash', repo_commit)
371 365 out_pth = pjoin(self.build_lib, pkg_dir, '.git_commit_info.ini')
372 366 out_file = open(out_pth, 'wt')
373 367 cfg_parser.write(out_file)
374 368 out_file.close()
375 369 return MyBuildPy
General Comments 0
You need to be logged in to leave comments. Login now