##// END OF EJS Templates
remove all trailling spaces
Bernardo B. Marques -
Show More

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

@@ -1,436 +1,436 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 A base class for a configurable application.
3 A base class for a configurable application.
4
4
5 Authors:
5 Authors:
6
6
7 * Brian Granger
7 * Brian Granger
8 * Min RK
8 * Min RK
9 """
9 """
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2008-2011 The IPython Development Team
12 # Copyright (C) 2008-2011 The IPython Development Team
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 import logging
22 import logging
23 import os
23 import os
24 import re
24 import re
25 import sys
25 import sys
26 from copy import deepcopy
26 from copy import deepcopy
27
27
28 from IPython.config.configurable import SingletonConfigurable
28 from IPython.config.configurable import SingletonConfigurable
29 from IPython.config.loader import (
29 from IPython.config.loader import (
30 KVArgParseConfigLoader, PyFileConfigLoader, Config, ArgumentError
30 KVArgParseConfigLoader, PyFileConfigLoader, Config, ArgumentError
31 )
31 )
32
32
33 from IPython.utils.traitlets import (
33 from IPython.utils.traitlets import (
34 Unicode, List, Int, Enum, Dict, Instance, TraitError
34 Unicode, List, Int, Enum, Dict, Instance, TraitError
35 )
35 )
36 from IPython.utils.importstring import import_item
36 from IPython.utils.importstring import import_item
37 from IPython.utils.text import indent, wrap_paragraphs, dedent
37 from IPython.utils.text import indent, wrap_paragraphs, dedent
38
38
39 #-----------------------------------------------------------------------------
39 #-----------------------------------------------------------------------------
40 # function for re-wrapping a helpstring
40 # function for re-wrapping a helpstring
41 #-----------------------------------------------------------------------------
41 #-----------------------------------------------------------------------------
42
42
43 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
44 # Descriptions for the various sections
44 # Descriptions for the various sections
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46
46
47 # merge flags&aliases into options
47 # merge flags&aliases into options
48 option_description = """
48 option_description = """
49 Arguments that take values are actually convenience aliases to full
49 Arguments that take values are actually convenience aliases to full
50 Configurables, whose aliases are listed on the help line. For more information
50 Configurables, whose aliases are listed on the help line. For more information
51 on full configurables, see '--help-all'.
51 on full configurables, see '--help-all'.
52 """.strip() # trim newlines of front and back
52 """.strip() # trim newlines of front and back
53
53
54 keyvalue_description = """
54 keyvalue_description = """
55 Parameters are set from command-line arguments of the form:
55 Parameters are set from command-line arguments of the form:
56 `--Class.trait=value`.
56 `--Class.trait=value`.
57 This line is evaluated in Python, so simple expressions are allowed, e.g.::
57 This line is evaluated in Python, so simple expressions are allowed, e.g.::
58 `--C.a='range(3)'` For setting C.a=[0,1,2].
58 `--C.a='range(3)'` For setting C.a=[0,1,2].
59 """.strip() # trim newlines of front and back
59 """.strip() # trim newlines of front and back
60
60
61 subcommand_description = """
61 subcommand_description = """
62 Subcommands are launched as `{app} cmd [args]`. For information on using
62 Subcommands are launched as `{app} cmd [args]`. For information on using
63 subcommand 'cmd', do: `{app} cmd -h`.
63 subcommand 'cmd', do: `{app} cmd -h`.
64 """.strip().format(app=os.path.basename(sys.argv[0]))
64 """.strip().format(app=os.path.basename(sys.argv[0]))
65 # get running program name
65 # get running program name
66
66
67 #-----------------------------------------------------------------------------
67 #-----------------------------------------------------------------------------
68 # Application class
68 # Application class
69 #-----------------------------------------------------------------------------
69 #-----------------------------------------------------------------------------
70
70
71
71
72 class ApplicationError(Exception):
72 class ApplicationError(Exception):
73 pass
73 pass
74
74
75
75
76 class Application(SingletonConfigurable):
76 class Application(SingletonConfigurable):
77 """A singleton application with full configuration support."""
77 """A singleton application with full configuration support."""
78
78
79 # The name of the application, will usually match the name of the command
79 # The name of the application, will usually match the name of the command
80 # line application
80 # line application
81 name = Unicode(u'application')
81 name = Unicode(u'application')
82
82
83 # The description of the application that is printed at the beginning
83 # The description of the application that is printed at the beginning
84 # of the help.
84 # of the help.
85 description = Unicode(u'This is an application.')
85 description = Unicode(u'This is an application.')
86 # default section descriptions
86 # default section descriptions
87 option_description = Unicode(option_description)
87 option_description = Unicode(option_description)
88 keyvalue_description = Unicode(keyvalue_description)
88 keyvalue_description = Unicode(keyvalue_description)
89 subcommand_description = Unicode(subcommand_description)
89 subcommand_description = Unicode(subcommand_description)
90
90
91 # The usage and example string that goes at the end of the help string.
91 # The usage and example string that goes at the end of the help string.
92 examples = Unicode()
92 examples = Unicode()
93
93
94 # A sequence of Configurable subclasses whose config=True attributes will
94 # A sequence of Configurable subclasses whose config=True attributes will
95 # be exposed at the command line.
95 # be exposed at the command line.
96 classes = List([])
96 classes = List([])
97
97
98 # The version string of this application.
98 # The version string of this application.
99 version = Unicode(u'0.0')
99 version = Unicode(u'0.0')
100
100
101 # The log level for the application
101 # The log level for the application
102 log_level = Enum((0,10,20,30,40,50,'DEBUG','INFO','WARN','ERROR','CRITICAL'),
102 log_level = Enum((0,10,20,30,40,50,'DEBUG','INFO','WARN','ERROR','CRITICAL'),
103 default_value=logging.WARN,
103 default_value=logging.WARN,
104 config=True,
104 config=True,
105 help="Set the log level by value or name.")
105 help="Set the log level by value or name.")
106 def _log_level_changed(self, name, old, new):
106 def _log_level_changed(self, name, old, new):
107 """Adjust the log level when log_level is set."""
107 """Adjust the log level when log_level is set."""
108 if isinstance(new, basestring):
108 if isinstance(new, basestring):
109 new = getattr(logging, new)
109 new = getattr(logging, new)
110 self.log_level = new
110 self.log_level = new
111 self.log.setLevel(new)
111 self.log.setLevel(new)
112
112
113 # the alias map for configurables
113 # the alias map for configurables
114 aliases = Dict({'log-level' : 'Application.log_level'})
114 aliases = Dict({'log-level' : 'Application.log_level'})
115
115
116 # flags for loading Configurables or store_const style flags
116 # flags for loading Configurables or store_const style flags
117 # flags are loaded from this dict by '--key' flags
117 # flags are loaded from this dict by '--key' flags
118 # this must be a dict of two-tuples, the first element being the Config/dict
118 # this must be a dict of two-tuples, the first element being the Config/dict
119 # and the second being the help string for the flag
119 # and the second being the help string for the flag
120 flags = Dict()
120 flags = Dict()
121 def _flags_changed(self, name, old, new):
121 def _flags_changed(self, name, old, new):
122 """ensure flags dict is valid"""
122 """ensure flags dict is valid"""
123 for key,value in new.iteritems():
123 for key,value in new.iteritems():
124 assert len(value) == 2, "Bad flag: %r:%s"%(key,value)
124 assert len(value) == 2, "Bad flag: %r:%s"%(key,value)
125 assert isinstance(value[0], (dict, Config)), "Bad flag: %r:%s"%(key,value)
125 assert isinstance(value[0], (dict, Config)), "Bad flag: %r:%s"%(key,value)
126 assert isinstance(value[1], basestring), "Bad flag: %r:%s"%(key,value)
126 assert isinstance(value[1], basestring), "Bad flag: %r:%s"%(key,value)
127
127
128
128
129 # subcommands for launching other applications
129 # subcommands for launching other applications
130 # if this is not empty, this will be a parent Application
130 # if this is not empty, this will be a parent Application
131 # this must be a dict of two-tuples,
131 # this must be a dict of two-tuples,
132 # the first element being the application class/import string
132 # the first element being the application class/import string
133 # and the second being the help string for the subcommand
133 # and the second being the help string for the subcommand
134 subcommands = Dict()
134 subcommands = Dict()
135 # parse_command_line will initialize a subapp, if requested
135 # parse_command_line will initialize a subapp, if requested
136 subapp = Instance('IPython.config.application.Application', allow_none=True)
136 subapp = Instance('IPython.config.application.Application', allow_none=True)
137
137
138 # extra command-line arguments that don't set config values
138 # extra command-line arguments that don't set config values
139 extra_args = List(Unicode)
139 extra_args = List(Unicode)
140
140
141
141
142 def __init__(self, **kwargs):
142 def __init__(self, **kwargs):
143 SingletonConfigurable.__init__(self, **kwargs)
143 SingletonConfigurable.__init__(self, **kwargs)
144 # Ensure my class is in self.classes, so my attributes appear in command line
144 # Ensure my class is in self.classes, so my attributes appear in command line
145 # options and config files.
145 # options and config files.
146 if self.__class__ not in self.classes:
146 if self.__class__ not in self.classes:
147 self.classes.insert(0, self.__class__)
147 self.classes.insert(0, self.__class__)
148
148
149 self.init_logging()
149 self.init_logging()
150
150
151 def _config_changed(self, name, old, new):
151 def _config_changed(self, name, old, new):
152 SingletonConfigurable._config_changed(self, name, old, new)
152 SingletonConfigurable._config_changed(self, name, old, new)
153 self.log.debug('Config changed:')
153 self.log.debug('Config changed:')
154 self.log.debug(repr(new))
154 self.log.debug(repr(new))
155
155
156 def init_logging(self):
156 def init_logging(self):
157 """Start logging for this application.
157 """Start logging for this application.
158
158
159 The default is to log to stdout using a StreaHandler. The log level
159 The default is to log to stdout using a StreaHandler. The log level
160 starts at loggin.WARN, but this can be adjusted by setting the
160 starts at loggin.WARN, but this can be adjusted by setting the
161 ``log_level`` attribute.
161 ``log_level`` attribute.
162 """
162 """
163 self.log = logging.getLogger(self.__class__.__name__)
163 self.log = logging.getLogger(self.__class__.__name__)
164 self.log.setLevel(self.log_level)
164 self.log.setLevel(self.log_level)
165 if sys.executable.endswith('pythonw.exe'):
165 if sys.executable.endswith('pythonw.exe'):
166 # this should really go to a file, but file-logging is only
166 # this should really go to a file, but file-logging is only
167 # hooked up in parallel applications
167 # hooked up in parallel applications
168 self._log_handler = logging.StreamHandler(open(os.devnull, 'w'))
168 self._log_handler = logging.StreamHandler(open(os.devnull, 'w'))
169 else:
169 else:
170 self._log_handler = logging.StreamHandler()
170 self._log_handler = logging.StreamHandler()
171 self._log_formatter = logging.Formatter("[%(name)s] %(message)s")
171 self._log_formatter = logging.Formatter("[%(name)s] %(message)s")
172 self._log_handler.setFormatter(self._log_formatter)
172 self._log_handler.setFormatter(self._log_formatter)
173 self.log.addHandler(self._log_handler)
173 self.log.addHandler(self._log_handler)
174
174
175 def initialize(self, argv=None):
175 def initialize(self, argv=None):
176 """Do the basic steps to configure me.
176 """Do the basic steps to configure me.
177
177
178 Override in subclasses.
178 Override in subclasses.
179 """
179 """
180 self.parse_command_line(argv)
180 self.parse_command_line(argv)
181
181
182
182
183 def start(self):
183 def start(self):
184 """Start the app mainloop.
184 """Start the app mainloop.
185
185
186 Override in subclasses.
186 Override in subclasses.
187 """
187 """
188 if self.subapp is not None:
188 if self.subapp is not None:
189 return self.subapp.start()
189 return self.subapp.start()
190
190
191 def print_alias_help(self):
191 def print_alias_help(self):
192 """Print the alias part of the help."""
192 """Print the alias part of the help."""
193 if not self.aliases:
193 if not self.aliases:
194 return
194 return
195
195
196 lines = []
196 lines = []
197 classdict = {}
197 classdict = {}
198 for cls in self.classes:
198 for cls in self.classes:
199 # include all parents (up to, but excluding Configurable) in available names
199 # include all parents (up to, but excluding Configurable) in available names
200 for c in cls.mro()[:-3]:
200 for c in cls.mro()[:-3]:
201 classdict[c.__name__] = c
201 classdict[c.__name__] = c
202
202
203 for alias, longname in self.aliases.iteritems():
203 for alias, longname in self.aliases.iteritems():
204 classname, traitname = longname.split('.',1)
204 classname, traitname = longname.split('.',1)
205 cls = classdict[classname]
205 cls = classdict[classname]
206
206
207 trait = cls.class_traits(config=True)[traitname]
207 trait = cls.class_traits(config=True)[traitname]
208 help = cls.class_get_trait_help(trait).splitlines()
208 help = cls.class_get_trait_help(trait).splitlines()
209 # reformat first line
209 # reformat first line
210 help[0] = help[0].replace(longname, alias) + ' (%s)'%longname
210 help[0] = help[0].replace(longname, alias) + ' (%s)'%longname
211 if len(alias) == 1:
211 if len(alias) == 1:
212 help[0] = help[0].replace('--%s='%alias, '-%s '%alias)
212 help[0] = help[0].replace('--%s='%alias, '-%s '%alias)
213 lines.extend(help)
213 lines.extend(help)
214 # lines.append('')
214 # lines.append('')
215 print os.linesep.join(lines)
215 print os.linesep.join(lines)
216
216
217 def print_flag_help(self):
217 def print_flag_help(self):
218 """Print the flag part of the help."""
218 """Print the flag part of the help."""
219 if not self.flags:
219 if not self.flags:
220 return
220 return
221
221
222 lines = []
222 lines = []
223 for m, (cfg,help) in self.flags.iteritems():
223 for m, (cfg,help) in self.flags.iteritems():
224 prefix = '--' if len(m) > 1 else '-'
224 prefix = '--' if len(m) > 1 else '-'
225 lines.append(prefix+m)
225 lines.append(prefix+m)
226 lines.append(indent(dedent(help.strip())))
226 lines.append(indent(dedent(help.strip())))
227 # lines.append('')
227 # lines.append('')
228 print os.linesep.join(lines)
228 print os.linesep.join(lines)
229
229
230 def print_options(self):
230 def print_options(self):
231 if not self.flags and not self.aliases:
231 if not self.flags and not self.aliases:
232 return
232 return
233 lines = ['Options']
233 lines = ['Options']
234 lines.append('-'*len(lines[0]))
234 lines.append('-'*len(lines[0]))
235 lines.append('')
235 lines.append('')
236 for p in wrap_paragraphs(self.option_description):
236 for p in wrap_paragraphs(self.option_description):
237 lines.append(p)
237 lines.append(p)
238 lines.append('')
238 lines.append('')
239 print os.linesep.join(lines)
239 print os.linesep.join(lines)
240 self.print_flag_help()
240 self.print_flag_help()
241 self.print_alias_help()
241 self.print_alias_help()
242 print
242 print
243
243
244 def print_subcommands(self):
244 def print_subcommands(self):
245 """Print the subcommand part of the help."""
245 """Print the subcommand part of the help."""
246 if not self.subcommands:
246 if not self.subcommands:
247 return
247 return
248
248
249 lines = ["Subcommands"]
249 lines = ["Subcommands"]
250 lines.append('-'*len(lines[0]))
250 lines.append('-'*len(lines[0]))
251 lines.append('')
251 lines.append('')
252 for p in wrap_paragraphs(self.subcommand_description):
252 for p in wrap_paragraphs(self.subcommand_description):
253 lines.append(p)
253 lines.append(p)
254 lines.append('')
254 lines.append('')
255 for subc, (cls, help) in self.subcommands.iteritems():
255 for subc, (cls, help) in self.subcommands.iteritems():
256 lines.append(subc)
256 lines.append(subc)
257 if help:
257 if help:
258 lines.append(indent(dedent(help.strip())))
258 lines.append(indent(dedent(help.strip())))
259 lines.append('')
259 lines.append('')
260 print os.linesep.join(lines)
260 print os.linesep.join(lines)
261
261
262 def print_help(self, classes=False):
262 def print_help(self, classes=False):
263 """Print the help for each Configurable class in self.classes.
263 """Print the help for each Configurable class in self.classes.
264
264
265 If classes=False (the default), only flags and aliases are printed.
265 If classes=False (the default), only flags and aliases are printed.
266 """
266 """
267 self.print_subcommands()
267 self.print_subcommands()
268 self.print_options()
268 self.print_options()
269
269
270 if classes:
270 if classes:
271 if self.classes:
271 if self.classes:
272 print "Class parameters"
272 print "Class parameters"
273 print "----------------"
273 print "----------------"
274 print
274 print
275 for p in wrap_paragraphs(self.keyvalue_description):
275 for p in wrap_paragraphs(self.keyvalue_description):
276 print p
276 print p
277 print
277 print
278
278
279 for cls in self.classes:
279 for cls in self.classes:
280 cls.class_print_help()
280 cls.class_print_help()
281 print
281 print
282 else:
282 else:
283 print "To see all available configurables, use `--help-all`"
283 print "To see all available configurables, use `--help-all`"
284 print
284 print
285
285
286 def print_description(self):
286 def print_description(self):
287 """Print the application description."""
287 """Print the application description."""
288 for p in wrap_paragraphs(self.description):
288 for p in wrap_paragraphs(self.description):
289 print p
289 print p
290 print
290 print
291
291
292 def print_examples(self):
292 def print_examples(self):
293 """Print usage and examples.
293 """Print usage and examples.
294
294
295 This usage string goes at the end of the command line help string
295 This usage string goes at the end of the command line help string
296 and should contain examples of the application's usage.
296 and should contain examples of the application's usage.
297 """
297 """
298 if self.examples:
298 if self.examples:
299 print "Examples"
299 print "Examples"
300 print "--------"
300 print "--------"
301 print
301 print
302 print indent(dedent(self.examples.strip()))
302 print indent(dedent(self.examples.strip()))
303 print
303 print
304
304
305 def print_version(self):
305 def print_version(self):
306 """Print the version string."""
306 """Print the version string."""
307 print self.version
307 print self.version
308
308
309 def update_config(self, config):
309 def update_config(self, config):
310 """Fire the traits events when the config is updated."""
310 """Fire the traits events when the config is updated."""
311 # Save a copy of the current config.
311 # Save a copy of the current config.
312 newconfig = deepcopy(self.config)
312 newconfig = deepcopy(self.config)
313 # Merge the new config into the current one.
313 # Merge the new config into the current one.
314 newconfig._merge(config)
314 newconfig._merge(config)
315 # Save the combined config as self.config, which triggers the traits
315 # Save the combined config as self.config, which triggers the traits
316 # events.
316 # events.
317 self.config = newconfig
317 self.config = newconfig
318
318
319 def initialize_subcommand(self, subc, argv=None):
319 def initialize_subcommand(self, subc, argv=None):
320 """Initialize a subcommand with argv."""
320 """Initialize a subcommand with argv."""
321 subapp,help = self.subcommands.get(subc)
321 subapp,help = self.subcommands.get(subc)
322
322
323 if isinstance(subapp, basestring):
323 if isinstance(subapp, basestring):
324 subapp = import_item(subapp)
324 subapp = import_item(subapp)
325
325
326 # clear existing instances
326 # clear existing instances
327 self.__class__.clear_instance()
327 self.__class__.clear_instance()
328 # instantiate
328 # instantiate
329 self.subapp = subapp.instance()
329 self.subapp = subapp.instance()
330 # and initialize subapp
330 # and initialize subapp
331 self.subapp.initialize(argv)
331 self.subapp.initialize(argv)
332
332
333 def parse_command_line(self, argv=None):
333 def parse_command_line(self, argv=None):
334 """Parse the command line arguments."""
334 """Parse the command line arguments."""
335 argv = sys.argv[1:] if argv is None else argv
335 argv = sys.argv[1:] if argv is None else argv
336
336
337 if self.subcommands and len(argv) > 0:
337 if self.subcommands and len(argv) > 0:
338 # we have subcommands, and one may have been specified
338 # we have subcommands, and one may have been specified
339 subc, subargv = argv[0], argv[1:]
339 subc, subargv = argv[0], argv[1:]
340 if re.match(r'^\w(\-?\w)*$', subc) and subc in self.subcommands:
340 if re.match(r'^\w(\-?\w)*$', subc) and subc in self.subcommands:
341 # it's a subcommand, and *not* a flag or class parameter
341 # it's a subcommand, and *not* a flag or class parameter
342 return self.initialize_subcommand(subc, subargv)
342 return self.initialize_subcommand(subc, subargv)
343
343
344 if '-h' in argv or '--help' in argv or '--help-all' in argv:
344 if '-h' in argv or '--help' in argv or '--help-all' in argv:
345 self.print_description()
345 self.print_description()
346 self.print_help('--help-all' in argv)
346 self.print_help('--help-all' in argv)
347 self.print_examples()
347 self.print_examples()
348 self.exit(0)
348 self.exit(0)
349
349
350 if '--version' in argv:
350 if '--version' in argv:
351 self.print_version()
351 self.print_version()
352 self.exit(0)
352 self.exit(0)
353
353
354 loader = KVArgParseConfigLoader(argv=argv, aliases=self.aliases,
354 loader = KVArgParseConfigLoader(argv=argv, aliases=self.aliases,
355 flags=self.flags)
355 flags=self.flags)
356 try:
356 try:
357 config = loader.load_config()
357 config = loader.load_config()
358 self.update_config(config)
358 self.update_config(config)
359 except (TraitError, ArgumentError) as e:
359 except (TraitError, ArgumentError) as e:
360 self.print_description()
360 self.print_description()
361 self.print_help()
361 self.print_help()
362 self.print_examples()
362 self.print_examples()
363 self.log.fatal(str(e))
363 self.log.fatal(str(e))
364 self.exit(1)
364 self.exit(1)
365 # store unparsed args in extra_args
365 # store unparsed args in extra_args
366 self.extra_args = loader.extra_args
366 self.extra_args = loader.extra_args
367
367
368 def load_config_file(self, filename, path=None):
368 def load_config_file(self, filename, path=None):
369 """Load a .py based config file by filename and path."""
369 """Load a .py based config file by filename and path."""
370 loader = PyFileConfigLoader(filename, path=path)
370 loader = PyFileConfigLoader(filename, path=path)
371 try:
371 try:
372 config = loader.load_config()
372 config = loader.load_config()
373 except IOError:
373 except IOError:
374 # problem with the file (probably doesn't exist), raise
374 # problem with the file (probably doesn't exist), raise
375 raise
375 raise
376 except Exception:
376 except Exception:
377 # try to get the full filename, but it will be empty in the
377 # try to get the full filename, but it will be empty in the
378 # unlikely event that the error raised before filefind finished
378 # unlikely event that the error raised before filefind finished
379 filename = loader.full_filename or filename
379 filename = loader.full_filename or filename
380 # problem while running the file
380 # problem while running the file
381 self.log.error("Exception while loading config file %s",
381 self.log.error("Exception while loading config file %s",
382 filename, exc_info=True)
382 filename, exc_info=True)
383 else:
383 else:
384 self.log.debug("Loaded config file: %s", loader.full_filename)
384 self.log.debug("Loaded config file: %s", loader.full_filename)
385 self.update_config(config)
385 self.update_config(config)
386
386
387 def generate_config_file(self):
387 def generate_config_file(self):
388 """generate default config file from Configurables"""
388 """generate default config file from Configurables"""
389 lines = ["# Configuration file for %s."%self.name]
389 lines = ["# Configuration file for %s."%self.name]
390 lines.append('')
390 lines.append('')
391 lines.append('c = get_config()')
391 lines.append('c = get_config()')
392 lines.append('')
392 lines.append('')
393 for cls in self.classes:
393 for cls in self.classes:
394 lines.append(cls.class_config_section())
394 lines.append(cls.class_config_section())
395 return '\n'.join(lines)
395 return '\n'.join(lines)
396
396
397 def exit(self, exit_status=0):
397 def exit(self, exit_status=0):
398 self.log.debug("Exiting application: %s" % self.name)
398 self.log.debug("Exiting application: %s" % self.name)
399 sys.exit(exit_status)
399 sys.exit(exit_status)
400
400
401 #-----------------------------------------------------------------------------
401 #-----------------------------------------------------------------------------
402 # utility functions, for convenience
402 # utility functions, for convenience
403 #-----------------------------------------------------------------------------
403 #-----------------------------------------------------------------------------
404
404
405 def boolean_flag(name, configurable, set_help='', unset_help=''):
405 def boolean_flag(name, configurable, set_help='', unset_help=''):
406 """Helper for building basic --trait, --no-trait flags.
406 """Helper for building basic --trait, --no-trait flags.
407
407
408 Parameters
408 Parameters
409 ----------
409 ----------
410
410
411 name : str
411 name : str
412 The name of the flag.
412 The name of the flag.
413 configurable : str
413 configurable : str
414 The 'Class.trait' string of the trait to be set/unset with the flag
414 The 'Class.trait' string of the trait to be set/unset with the flag
415 set_help : unicode
415 set_help : unicode
416 help string for --name flag
416 help string for --name flag
417 unset_help : unicode
417 unset_help : unicode
418 help string for --no-name flag
418 help string for --no-name flag
419
419
420 Returns
420 Returns
421 -------
421 -------
422
422
423 cfg : dict
423 cfg : dict
424 A dict with two keys: 'name', and 'no-name', for setting and unsetting
424 A dict with two keys: 'name', and 'no-name', for setting and unsetting
425 the trait, respectively.
425 the trait, respectively.
426 """
426 """
427 # default helpstrings
427 # default helpstrings
428 set_help = set_help or "set %s=True"%configurable
428 set_help = set_help or "set %s=True"%configurable
429 unset_help = unset_help or "set %s=False"%configurable
429 unset_help = unset_help or "set %s=False"%configurable
430
430
431 cls,trait = configurable.split('.')
431 cls,trait = configurable.split('.')
432
432
433 setter = {cls : {trait : True}}
433 setter = {cls : {trait : True}}
434 unsetter = {cls : {trait : False}}
434 unsetter = {cls : {trait : False}}
435 return {name : (setter, set_help), 'no-'+name : (unsetter, unset_help)}
435 return {name : (setter, set_help), 'no-'+name : (unsetter, unset_help)}
436
436
@@ -1,328 +1,328 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 A base class for objects that are configurable.
3 A base class for objects that are configurable.
4
4
5 Authors:
5 Authors:
6
6
7 * Brian Granger
7 * Brian Granger
8 * Fernando Perez
8 * Fernando Perez
9 * Min RK
9 * Min RK
10 """
10 """
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2011 The IPython Development Team
13 # Copyright (C) 2008-2011 The IPython Development Team
14 #
14 #
15 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Imports
20 # Imports
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 import datetime
23 import datetime
24 from copy import deepcopy
24 from copy import deepcopy
25
25
26 from loader import Config
26 from loader import Config
27 from IPython.utils.traitlets import HasTraits, Instance
27 from IPython.utils.traitlets import HasTraits, Instance
28 from IPython.utils.text import indent, wrap_paragraphs
28 from IPython.utils.text import indent, wrap_paragraphs
29
29
30
30
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32 # Helper classes for Configurables
32 # Helper classes for Configurables
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34
34
35
35
36 class ConfigurableError(Exception):
36 class ConfigurableError(Exception):
37 pass
37 pass
38
38
39
39
40 class MultipleInstanceError(ConfigurableError):
40 class MultipleInstanceError(ConfigurableError):
41 pass
41 pass
42
42
43 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
44 # Configurable implementation
44 # Configurable implementation
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46
46
47 class Configurable(HasTraits):
47 class Configurable(HasTraits):
48
48
49 config = Instance(Config,(),{})
49 config = Instance(Config,(),{})
50 created = None
50 created = None
51
51
52 def __init__(self, **kwargs):
52 def __init__(self, **kwargs):
53 """Create a configurable given a config config.
53 """Create a configurable given a config config.
54
54
55 Parameters
55 Parameters
56 ----------
56 ----------
57 config : Config
57 config : Config
58 If this is empty, default values are used. If config is a
58 If this is empty, default values are used. If config is a
59 :class:`Config` instance, it will be used to configure the
59 :class:`Config` instance, it will be used to configure the
60 instance.
60 instance.
61
61
62 Notes
62 Notes
63 -----
63 -----
64 Subclasses of Configurable must call the :meth:`__init__` method of
64 Subclasses of Configurable must call the :meth:`__init__` method of
65 :class:`Configurable` *before* doing anything else and using
65 :class:`Configurable` *before* doing anything else and using
66 :func:`super`::
66 :func:`super`::
67
67
68 class MyConfigurable(Configurable):
68 class MyConfigurable(Configurable):
69 def __init__(self, config=None):
69 def __init__(self, config=None):
70 super(MyConfigurable, self).__init__(config)
70 super(MyConfigurable, self).__init__(config)
71 # Then any other code you need to finish initialization.
71 # Then any other code you need to finish initialization.
72
72
73 This ensures that instances will be configured properly.
73 This ensures that instances will be configured properly.
74 """
74 """
75 config = kwargs.pop('config', None)
75 config = kwargs.pop('config', None)
76 if config is not None:
76 if config is not None:
77 # We used to deepcopy, but for now we are trying to just save
77 # We used to deepcopy, but for now we are trying to just save
78 # by reference. This *could* have side effects as all components
78 # by reference. This *could* have side effects as all components
79 # will share config. In fact, I did find such a side effect in
79 # will share config. In fact, I did find such a side effect in
80 # _config_changed below. If a config attribute value was a mutable type
80 # _config_changed below. If a config attribute value was a mutable type
81 # all instances of a component were getting the same copy, effectively
81 # all instances of a component were getting the same copy, effectively
82 # making that a class attribute.
82 # making that a class attribute.
83 # self.config = deepcopy(config)
83 # self.config = deepcopy(config)
84 self.config = config
84 self.config = config
85 # This should go second so individual keyword arguments override
85 # This should go second so individual keyword arguments override
86 # the values in config.
86 # the values in config.
87 super(Configurable, self).__init__(**kwargs)
87 super(Configurable, self).__init__(**kwargs)
88 self.created = datetime.datetime.now()
88 self.created = datetime.datetime.now()
89
89
90 #-------------------------------------------------------------------------
90 #-------------------------------------------------------------------------
91 # Static trait notifiations
91 # Static trait notifiations
92 #-------------------------------------------------------------------------
92 #-------------------------------------------------------------------------
93
93
94 def _config_changed(self, name, old, new):
94 def _config_changed(self, name, old, new):
95 """Update all the class traits having ``config=True`` as metadata.
95 """Update all the class traits having ``config=True`` as metadata.
96
96
97 For any class trait with a ``config`` metadata attribute that is
97 For any class trait with a ``config`` metadata attribute that is
98 ``True``, we update the trait with the value of the corresponding
98 ``True``, we update the trait with the value of the corresponding
99 config entry.
99 config entry.
100 """
100 """
101 # Get all traits with a config metadata entry that is True
101 # Get all traits with a config metadata entry that is True
102 traits = self.traits(config=True)
102 traits = self.traits(config=True)
103
103
104 # We auto-load config section for this class as well as any parent
104 # We auto-load config section for this class as well as any parent
105 # classes that are Configurable subclasses. This starts with Configurable
105 # classes that are Configurable subclasses. This starts with Configurable
106 # and works down the mro loading the config for each section.
106 # and works down the mro loading the config for each section.
107 section_names = [cls.__name__ for cls in \
107 section_names = [cls.__name__ for cls in \
108 reversed(self.__class__.__mro__) if
108 reversed(self.__class__.__mro__) if
109 issubclass(cls, Configurable) and issubclass(self.__class__, cls)]
109 issubclass(cls, Configurable) and issubclass(self.__class__, cls)]
110
110
111 for sname in section_names:
111 for sname in section_names:
112 # Don't do a blind getattr as that would cause the config to
112 # Don't do a blind getattr as that would cause the config to
113 # dynamically create the section with name self.__class__.__name__.
113 # dynamically create the section with name self.__class__.__name__.
114 if new._has_section(sname):
114 if new._has_section(sname):
115 my_config = new[sname]
115 my_config = new[sname]
116 for k, v in traits.iteritems():
116 for k, v in traits.iteritems():
117 # Don't allow traitlets with config=True to start with
117 # Don't allow traitlets with config=True to start with
118 # uppercase. Otherwise, they are confused with Config
118 # uppercase. Otherwise, they are confused with Config
119 # subsections. But, developers shouldn't have uppercase
119 # subsections. But, developers shouldn't have uppercase
120 # attributes anyways! (PEP 6)
120 # attributes anyways! (PEP 6)
121 if k[0].upper()==k[0] and not k.startswith('_'):
121 if k[0].upper()==k[0] and not k.startswith('_'):
122 raise ConfigurableError('Configurable traitlets with '
122 raise ConfigurableError('Configurable traitlets with '
123 'config=True must start with a lowercase so they are '
123 'config=True must start with a lowercase so they are '
124 'not confused with Config subsections: %s.%s' % \
124 'not confused with Config subsections: %s.%s' % \
125 (self.__class__.__name__, k))
125 (self.__class__.__name__, k))
126 try:
126 try:
127 # Here we grab the value from the config
127 # Here we grab the value from the config
128 # If k has the naming convention of a config
128 # If k has the naming convention of a config
129 # section, it will be auto created.
129 # section, it will be auto created.
130 config_value = my_config[k]
130 config_value = my_config[k]
131 except KeyError:
131 except KeyError:
132 pass
132 pass
133 else:
133 else:
134 # print "Setting %s.%s from %s.%s=%r" % \
134 # print "Setting %s.%s from %s.%s=%r" % \
135 # (self.__class__.__name__,k,sname,k,config_value)
135 # (self.__class__.__name__,k,sname,k,config_value)
136 # We have to do a deepcopy here if we don't deepcopy the entire
136 # We have to do a deepcopy here if we don't deepcopy the entire
137 # config object. If we don't, a mutable config_value will be
137 # config object. If we don't, a mutable config_value will be
138 # shared by all instances, effectively making it a class attribute.
138 # shared by all instances, effectively making it a class attribute.
139 setattr(self, k, deepcopy(config_value))
139 setattr(self, k, deepcopy(config_value))
140
140
141 @classmethod
141 @classmethod
142 def class_get_help(cls):
142 def class_get_help(cls):
143 """Get the help string for this class in ReST format."""
143 """Get the help string for this class in ReST format."""
144 cls_traits = cls.class_traits(config=True)
144 cls_traits = cls.class_traits(config=True)
145 final_help = []
145 final_help = []
146 final_help.append(u'%s options' % cls.__name__)
146 final_help.append(u'%s options' % cls.__name__)
147 final_help.append(len(final_help[0])*u'-')
147 final_help.append(len(final_help[0])*u'-')
148 for k,v in cls.class_traits(config=True).iteritems():
148 for k,v in cls.class_traits(config=True).iteritems():
149 help = cls.class_get_trait_help(v)
149 help = cls.class_get_trait_help(v)
150 final_help.append(help)
150 final_help.append(help)
151 return '\n'.join(final_help)
151 return '\n'.join(final_help)
152
152
153 @classmethod
153 @classmethod
154 def class_get_trait_help(cls, trait):
154 def class_get_trait_help(cls, trait):
155 """Get the help string for a single trait."""
155 """Get the help string for a single trait."""
156 lines = []
156 lines = []
157 header = "--%s.%s=<%s>" % (cls.__name__, trait.name, trait.__class__.__name__)
157 header = "--%s.%s=<%s>" % (cls.__name__, trait.name, trait.__class__.__name__)
158 lines.append(header)
158 lines.append(header)
159 try:
159 try:
160 dvr = repr(trait.get_default_value())
160 dvr = repr(trait.get_default_value())
161 except Exception:
161 except Exception:
162 dvr = None # ignore defaults we can't construct
162 dvr = None # ignore defaults we can't construct
163 if dvr is not None:
163 if dvr is not None:
164 if len(dvr) > 64:
164 if len(dvr) > 64:
165 dvr = dvr[:61]+'...'
165 dvr = dvr[:61]+'...'
166 lines.append(indent('Default: %s'%dvr, 4))
166 lines.append(indent('Default: %s'%dvr, 4))
167 if 'Enum' in trait.__class__.__name__:
167 if 'Enum' in trait.__class__.__name__:
168 # include Enum choices
168 # include Enum choices
169 lines.append(indent('Choices: %r'%(trait.values,)))
169 lines.append(indent('Choices: %r'%(trait.values,)))
170
170
171 help = trait.get_metadata('help')
171 help = trait.get_metadata('help')
172 if help is not None:
172 if help is not None:
173 help = '\n'.join(wrap_paragraphs(help, 76))
173 help = '\n'.join(wrap_paragraphs(help, 76))
174 lines.append(indent(help, 4))
174 lines.append(indent(help, 4))
175 return '\n'.join(lines)
175 return '\n'.join(lines)
176
176
177 @classmethod
177 @classmethod
178 def class_print_help(cls):
178 def class_print_help(cls):
179 """Get the help string for a single trait and print it."""
179 """Get the help string for a single trait and print it."""
180 print cls.class_get_help()
180 print cls.class_get_help()
181
181
182 @classmethod
182 @classmethod
183 def class_config_section(cls):
183 def class_config_section(cls):
184 """Get the config class config section"""
184 """Get the config class config section"""
185 def c(s):
185 def c(s):
186 """return a commented, wrapped block."""
186 """return a commented, wrapped block."""
187 s = '\n\n'.join(wrap_paragraphs(s, 78))
187 s = '\n\n'.join(wrap_paragraphs(s, 78))
188
188
189 return '# ' + s.replace('\n', '\n# ')
189 return '# ' + s.replace('\n', '\n# ')
190
190
191 # section header
191 # section header
192 breaker = '#' + '-'*78
192 breaker = '#' + '-'*78
193 s = "# %s configuration"%cls.__name__
193 s = "# %s configuration"%cls.__name__
194 lines = [breaker, s, breaker, '']
194 lines = [breaker, s, breaker, '']
195 # get the description trait
195 # get the description trait
196 desc = cls.class_traits().get('description')
196 desc = cls.class_traits().get('description')
197 if desc:
197 if desc:
198 desc = desc.default_value
198 desc = desc.default_value
199 else:
199 else:
200 # no description trait, use __doc__
200 # no description trait, use __doc__
201 desc = getattr(cls, '__doc__', '')
201 desc = getattr(cls, '__doc__', '')
202 if desc:
202 if desc:
203 lines.append(c(desc))
203 lines.append(c(desc))
204 lines.append('')
204 lines.append('')
205
205
206 parents = []
206 parents = []
207 for parent in cls.mro():
207 for parent in cls.mro():
208 # only include parents that are not base classes
208 # only include parents that are not base classes
209 # and are not the class itself
209 # and are not the class itself
210 # and have some configurable traits to inherit
210 # and have some configurable traits to inherit
211 if parent is not cls and issubclass(parent, Configurable) and \
211 if parent is not cls and issubclass(parent, Configurable) and \
212 parent.class_traits(config=True):
212 parent.class_traits(config=True):
213 parents.append(parent)
213 parents.append(parent)
214
214
215 if parents:
215 if parents:
216 pstr = ', '.join([ p.__name__ for p in parents ])
216 pstr = ', '.join([ p.__name__ for p in parents ])
217 lines.append(c('%s will inherit config from: %s'%(cls.__name__, pstr)))
217 lines.append(c('%s will inherit config from: %s'%(cls.__name__, pstr)))
218 lines.append('')
218 lines.append('')
219
219
220 for name,trait in cls.class_traits(config=True).iteritems():
220 for name,trait in cls.class_traits(config=True).iteritems():
221 help = trait.get_metadata('help') or ''
221 help = trait.get_metadata('help') or ''
222 lines.append(c(help))
222 lines.append(c(help))
223 lines.append('# c.%s.%s = %r'%(cls.__name__, name, trait.get_default_value()))
223 lines.append('# c.%s.%s = %r'%(cls.__name__, name, trait.get_default_value()))
224 lines.append('')
224 lines.append('')
225 return '\n'.join(lines)
225 return '\n'.join(lines)
226
226
227
227
228
228
229 class SingletonConfigurable(Configurable):
229 class SingletonConfigurable(Configurable):
230 """A configurable that only allows one instance.
230 """A configurable that only allows one instance.
231
231
232 This class is for classes that should only have one instance of itself
232 This class is for classes that should only have one instance of itself
233 or *any* subclass. To create and retrieve such a class use the
233 or *any* subclass. To create and retrieve such a class use the
234 :meth:`SingletonConfigurable.instance` method.
234 :meth:`SingletonConfigurable.instance` method.
235 """
235 """
236
236
237 _instance = None
237 _instance = None
238
238
239 @classmethod
239 @classmethod
240 def _walk_mro(cls):
240 def _walk_mro(cls):
241 """Walk the cls.mro() for parent classes that are also singletons
241 """Walk the cls.mro() for parent classes that are also singletons
242
242
243 For use in instance()
243 For use in instance()
244 """
244 """
245
245
246 for subclass in cls.mro():
246 for subclass in cls.mro():
247 if issubclass(cls, subclass) and \
247 if issubclass(cls, subclass) and \
248 issubclass(subclass, SingletonConfigurable) and \
248 issubclass(subclass, SingletonConfigurable) and \
249 subclass != SingletonConfigurable:
249 subclass != SingletonConfigurable:
250 yield subclass
250 yield subclass
251
251
252 @classmethod
252 @classmethod
253 def clear_instance(cls):
253 def clear_instance(cls):
254 """unset _instance for this class and singleton parents.
254 """unset _instance for this class and singleton parents.
255 """
255 """
256 if not cls.initialized():
256 if not cls.initialized():
257 return
257 return
258 for subclass in cls._walk_mro():
258 for subclass in cls._walk_mro():
259 if isinstance(subclass._instance, cls):
259 if isinstance(subclass._instance, cls):
260 # only clear instances that are instances
260 # only clear instances that are instances
261 # of the calling class
261 # of the calling class
262 subclass._instance = None
262 subclass._instance = None
263
263
264 @classmethod
264 @classmethod
265 def instance(cls, *args, **kwargs):
265 def instance(cls, *args, **kwargs):
266 """Returns a global instance of this class.
266 """Returns a global instance of this class.
267
267
268 This method create a new instance if none have previously been created
268 This method create a new instance if none have previously been created
269 and returns a previously created instance is one already exists.
269 and returns a previously created instance is one already exists.
270
270
271 The arguments and keyword arguments passed to this method are passed
271 The arguments and keyword arguments passed to this method are passed
272 on to the :meth:`__init__` method of the class upon instantiation.
272 on to the :meth:`__init__` method of the class upon instantiation.
273
273
274 Examples
274 Examples
275 --------
275 --------
276
276
277 Create a singleton class using instance, and retrieve it::
277 Create a singleton class using instance, and retrieve it::
278
278
279 >>> from IPython.config.configurable import SingletonConfigurable
279 >>> from IPython.config.configurable import SingletonConfigurable
280 >>> class Foo(SingletonConfigurable): pass
280 >>> class Foo(SingletonConfigurable): pass
281 >>> foo = Foo.instance()
281 >>> foo = Foo.instance()
282 >>> foo == Foo.instance()
282 >>> foo == Foo.instance()
283 True
283 True
284
284
285 Create a subclass that is retrived using the base class instance::
285 Create a subclass that is retrived using the base class instance::
286
286
287 >>> class Bar(SingletonConfigurable): pass
287 >>> class Bar(SingletonConfigurable): pass
288 >>> class Bam(Bar): pass
288 >>> class Bam(Bar): pass
289 >>> bam = Bam.instance()
289 >>> bam = Bam.instance()
290 >>> bam == Bar.instance()
290 >>> bam == Bar.instance()
291 True
291 True
292 """
292 """
293 # Create and save the instance
293 # Create and save the instance
294 if cls._instance is None:
294 if cls._instance is None:
295 inst = cls(*args, **kwargs)
295 inst = cls(*args, **kwargs)
296 # Now make sure that the instance will also be returned by
296 # Now make sure that the instance will also be returned by
297 # parent classes' _instance attribute.
297 # parent classes' _instance attribute.
298 for subclass in cls._walk_mro():
298 for subclass in cls._walk_mro():
299 subclass._instance = inst
299 subclass._instance = inst
300
300
301 if isinstance(cls._instance, cls):
301 if isinstance(cls._instance, cls):
302 return cls._instance
302 return cls._instance
303 else:
303 else:
304 raise MultipleInstanceError(
304 raise MultipleInstanceError(
305 'Multiple incompatible subclass instances of '
305 'Multiple incompatible subclass instances of '
306 '%s are being created.' % cls.__name__
306 '%s are being created.' % cls.__name__
307 )
307 )
308
308
309 @classmethod
309 @classmethod
310 def initialized(cls):
310 def initialized(cls):
311 """Has an instance been created?"""
311 """Has an instance been created?"""
312 return hasattr(cls, "_instance") and cls._instance is not None
312 return hasattr(cls, "_instance") and cls._instance is not None
313
313
314
314
315 class LoggingConfigurable(Configurable):
315 class LoggingConfigurable(Configurable):
316 """A parent class for Configurables that log.
316 """A parent class for Configurables that log.
317
317
318 Subclasses have a log trait, and the default behavior
318 Subclasses have a log trait, and the default behavior
319 is to get the logger from the currently running Application
319 is to get the logger from the currently running Application
320 via Application.instance().log.
320 via Application.instance().log.
321 """
321 """
322
322
323 log = Instance('logging.Logger')
323 log = Instance('logging.Logger')
324 def _log_default(self):
324 def _log_default(self):
325 from IPython.config.application import Application
325 from IPython.config.application import Application
326 return Application.instance().log
326 return Application.instance().log
327
327
328
328
@@ -1,661 +1,661 b''
1 """A simple configuration system.
1 """A simple configuration system.
2
2
3 Authors
3 Authors
4 -------
4 -------
5 * Brian Granger
5 * Brian Granger
6 * Fernando Perez
6 * Fernando Perez
7 * Min RK
7 * Min RK
8 """
8 """
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Copyright (C) 2008-2011 The IPython Development Team
11 # Copyright (C) 2008-2011 The IPython Development Team
12 #
12 #
13 # Distributed under the terms of the BSD License. The full license is in
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
14 # the file COPYING, distributed as part of this software.
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 # Imports
18 # Imports
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 import __builtin__ as builtin_mod
21 import __builtin__ as builtin_mod
22 import re
22 import re
23 import sys
23 import sys
24
24
25 from IPython.external import argparse
25 from IPython.external import argparse
26 from IPython.utils.path import filefind, get_ipython_dir
26 from IPython.utils.path import filefind, get_ipython_dir
27 from IPython.utils import py3compat, text, warn
27 from IPython.utils import py3compat, text, warn
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Exceptions
30 # Exceptions
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33
33
34 class ConfigError(Exception):
34 class ConfigError(Exception):
35 pass
35 pass
36
36
37
37
38 class ConfigLoaderError(ConfigError):
38 class ConfigLoaderError(ConfigError):
39 pass
39 pass
40
40
41 class ArgumentError(ConfigLoaderError):
41 class ArgumentError(ConfigLoaderError):
42 pass
42 pass
43
43
44 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
45 # Argparse fix
45 # Argparse fix
46 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
47
47
48 # Unfortunately argparse by default prints help messages to stderr instead of
48 # Unfortunately argparse by default prints help messages to stderr instead of
49 # stdout. This makes it annoying to capture long help screens at the command
49 # stdout. This makes it annoying to capture long help screens at the command
50 # line, since one must know how to pipe stderr, which many users don't know how
50 # line, since one must know how to pipe stderr, which many users don't know how
51 # to do. So we override the print_help method with one that defaults to
51 # to do. So we override the print_help method with one that defaults to
52 # stdout and use our class instead.
52 # stdout and use our class instead.
53
53
54 class ArgumentParser(argparse.ArgumentParser):
54 class ArgumentParser(argparse.ArgumentParser):
55 """Simple argparse subclass that prints help to stdout by default."""
55 """Simple argparse subclass that prints help to stdout by default."""
56
56
57 def print_help(self, file=None):
57 def print_help(self, file=None):
58 if file is None:
58 if file is None:
59 file = sys.stdout
59 file = sys.stdout
60 return super(ArgumentParser, self).print_help(file)
60 return super(ArgumentParser, self).print_help(file)
61
61
62 print_help.__doc__ = argparse.ArgumentParser.print_help.__doc__
62 print_help.__doc__ = argparse.ArgumentParser.print_help.__doc__
63
63
64 #-----------------------------------------------------------------------------
64 #-----------------------------------------------------------------------------
65 # Config class for holding config information
65 # Config class for holding config information
66 #-----------------------------------------------------------------------------
66 #-----------------------------------------------------------------------------
67
67
68
68
69 class Config(dict):
69 class Config(dict):
70 """An attribute based dict that can do smart merges."""
70 """An attribute based dict that can do smart merges."""
71
71
72 def __init__(self, *args, **kwds):
72 def __init__(self, *args, **kwds):
73 dict.__init__(self, *args, **kwds)
73 dict.__init__(self, *args, **kwds)
74 # This sets self.__dict__ = self, but it has to be done this way
74 # This sets self.__dict__ = self, but it has to be done this way
75 # because we are also overriding __setattr__.
75 # because we are also overriding __setattr__.
76 dict.__setattr__(self, '__dict__', self)
76 dict.__setattr__(self, '__dict__', self)
77
77
78 def _merge(self, other):
78 def _merge(self, other):
79 to_update = {}
79 to_update = {}
80 for k, v in other.iteritems():
80 for k, v in other.iteritems():
81 if not self.has_key(k):
81 if not self.has_key(k):
82 to_update[k] = v
82 to_update[k] = v
83 else: # I have this key
83 else: # I have this key
84 if isinstance(v, Config):
84 if isinstance(v, Config):
85 # Recursively merge common sub Configs
85 # Recursively merge common sub Configs
86 self[k]._merge(v)
86 self[k]._merge(v)
87 else:
87 else:
88 # Plain updates for non-Configs
88 # Plain updates for non-Configs
89 to_update[k] = v
89 to_update[k] = v
90
90
91 self.update(to_update)
91 self.update(to_update)
92
92
93 def _is_section_key(self, key):
93 def _is_section_key(self, key):
94 if key[0].upper()==key[0] and not key.startswith('_'):
94 if key[0].upper()==key[0] and not key.startswith('_'):
95 return True
95 return True
96 else:
96 else:
97 return False
97 return False
98
98
99 def __contains__(self, key):
99 def __contains__(self, key):
100 if self._is_section_key(key):
100 if self._is_section_key(key):
101 return True
101 return True
102 else:
102 else:
103 return super(Config, self).__contains__(key)
103 return super(Config, self).__contains__(key)
104 # .has_key is deprecated for dictionaries.
104 # .has_key is deprecated for dictionaries.
105 has_key = __contains__
105 has_key = __contains__
106
106
107 def _has_section(self, key):
107 def _has_section(self, key):
108 if self._is_section_key(key):
108 if self._is_section_key(key):
109 if super(Config, self).__contains__(key):
109 if super(Config, self).__contains__(key):
110 return True
110 return True
111 return False
111 return False
112
112
113 def copy(self):
113 def copy(self):
114 return type(self)(dict.copy(self))
114 return type(self)(dict.copy(self))
115
115
116 def __copy__(self):
116 def __copy__(self):
117 return self.copy()
117 return self.copy()
118
118
119 def __deepcopy__(self, memo):
119 def __deepcopy__(self, memo):
120 import copy
120 import copy
121 return type(self)(copy.deepcopy(self.items()))
121 return type(self)(copy.deepcopy(self.items()))
122
122
123 def __getitem__(self, key):
123 def __getitem__(self, key):
124 # We cannot use directly self._is_section_key, because it triggers
124 # We cannot use directly self._is_section_key, because it triggers
125 # infinite recursion on top of PyPy. Instead, we manually fish the
125 # infinite recursion on top of PyPy. Instead, we manually fish the
126 # bound method.
126 # bound method.
127 is_section_key = self.__class__._is_section_key.__get__(self)
127 is_section_key = self.__class__._is_section_key.__get__(self)
128
128
129 # Because we use this for an exec namespace, we need to delegate
129 # Because we use this for an exec namespace, we need to delegate
130 # the lookup of names in __builtin__ to itself. This means
130 # the lookup of names in __builtin__ to itself. This means
131 # that you can't have section or attribute names that are
131 # that you can't have section or attribute names that are
132 # builtins.
132 # builtins.
133 try:
133 try:
134 return getattr(builtin_mod, key)
134 return getattr(builtin_mod, key)
135 except AttributeError:
135 except AttributeError:
136 pass
136 pass
137 if is_section_key(key):
137 if is_section_key(key):
138 try:
138 try:
139 return dict.__getitem__(self, key)
139 return dict.__getitem__(self, key)
140 except KeyError:
140 except KeyError:
141 c = Config()
141 c = Config()
142 dict.__setitem__(self, key, c)
142 dict.__setitem__(self, key, c)
143 return c
143 return c
144 else:
144 else:
145 return dict.__getitem__(self, key)
145 return dict.__getitem__(self, key)
146
146
147 def __setitem__(self, key, value):
147 def __setitem__(self, key, value):
148 # Don't allow names in __builtin__ to be modified.
148 # Don't allow names in __builtin__ to be modified.
149 if hasattr(builtin_mod, key):
149 if hasattr(builtin_mod, key):
150 raise ConfigError('Config variable names cannot have the same name '
150 raise ConfigError('Config variable names cannot have the same name '
151 'as a Python builtin: %s' % key)
151 'as a Python builtin: %s' % key)
152 if self._is_section_key(key):
152 if self._is_section_key(key):
153 if not isinstance(value, Config):
153 if not isinstance(value, Config):
154 raise ValueError('values whose keys begin with an uppercase '
154 raise ValueError('values whose keys begin with an uppercase '
155 'char must be Config instances: %r, %r' % (key, value))
155 'char must be Config instances: %r, %r' % (key, value))
156 else:
156 else:
157 dict.__setitem__(self, key, value)
157 dict.__setitem__(self, key, value)
158
158
159 def __getattr__(self, key):
159 def __getattr__(self, key):
160 try:
160 try:
161 return self.__getitem__(key)
161 return self.__getitem__(key)
162 except KeyError, e:
162 except KeyError, e:
163 raise AttributeError(e)
163 raise AttributeError(e)
164
164
165 def __setattr__(self, key, value):
165 def __setattr__(self, key, value):
166 try:
166 try:
167 self.__setitem__(key, value)
167 self.__setitem__(key, value)
168 except KeyError, e:
168 except KeyError, e:
169 raise AttributeError(e)
169 raise AttributeError(e)
170
170
171 def __delattr__(self, key):
171 def __delattr__(self, key):
172 try:
172 try:
173 dict.__delitem__(self, key)
173 dict.__delitem__(self, key)
174 except KeyError, e:
174 except KeyError, e:
175 raise AttributeError(e)
175 raise AttributeError(e)
176
176
177
177
178 #-----------------------------------------------------------------------------
178 #-----------------------------------------------------------------------------
179 # Config loading classes
179 # Config loading classes
180 #-----------------------------------------------------------------------------
180 #-----------------------------------------------------------------------------
181
181
182
182
183 class ConfigLoader(object):
183 class ConfigLoader(object):
184 """A object for loading configurations from just about anywhere.
184 """A object for loading configurations from just about anywhere.
185
185
186 The resulting configuration is packaged as a :class:`Struct`.
186 The resulting configuration is packaged as a :class:`Struct`.
187
187
188 Notes
188 Notes
189 -----
189 -----
190 A :class:`ConfigLoader` does one thing: load a config from a source
190 A :class:`ConfigLoader` does one thing: load a config from a source
191 (file, command line arguments) and returns the data as a :class:`Struct`.
191 (file, command line arguments) and returns the data as a :class:`Struct`.
192 There are lots of things that :class:`ConfigLoader` does not do. It does
192 There are lots of things that :class:`ConfigLoader` does not do. It does
193 not implement complex logic for finding config files. It does not handle
193 not implement complex logic for finding config files. It does not handle
194 default values or merge multiple configs. These things need to be
194 default values or merge multiple configs. These things need to be
195 handled elsewhere.
195 handled elsewhere.
196 """
196 """
197
197
198 def __init__(self):
198 def __init__(self):
199 """A base class for config loaders.
199 """A base class for config loaders.
200
200
201 Examples
201 Examples
202 --------
202 --------
203
203
204 >>> cl = ConfigLoader()
204 >>> cl = ConfigLoader()
205 >>> config = cl.load_config()
205 >>> config = cl.load_config()
206 >>> config
206 >>> config
207 {}
207 {}
208 """
208 """
209 self.clear()
209 self.clear()
210
210
211 def clear(self):
211 def clear(self):
212 self.config = Config()
212 self.config = Config()
213
213
214 def load_config(self):
214 def load_config(self):
215 """Load a config from somewhere, return a :class:`Config` instance.
215 """Load a config from somewhere, return a :class:`Config` instance.
216
216
217 Usually, this will cause self.config to be set and then returned.
217 Usually, this will cause self.config to be set and then returned.
218 However, in most cases, :meth:`ConfigLoader.clear` should be called
218 However, in most cases, :meth:`ConfigLoader.clear` should be called
219 to erase any previous state.
219 to erase any previous state.
220 """
220 """
221 self.clear()
221 self.clear()
222 return self.config
222 return self.config
223
223
224
224
225 class FileConfigLoader(ConfigLoader):
225 class FileConfigLoader(ConfigLoader):
226 """A base class for file based configurations.
226 """A base class for file based configurations.
227
227
228 As we add more file based config loaders, the common logic should go
228 As we add more file based config loaders, the common logic should go
229 here.
229 here.
230 """
230 """
231 pass
231 pass
232
232
233
233
234 class PyFileConfigLoader(FileConfigLoader):
234 class PyFileConfigLoader(FileConfigLoader):
235 """A config loader for pure python files.
235 """A config loader for pure python files.
236
236
237 This calls execfile on a plain python file and looks for attributes
237 This calls execfile on a plain python file and looks for attributes
238 that are all caps. These attribute are added to the config Struct.
238 that are all caps. These attribute are added to the config Struct.
239 """
239 """
240
240
241 def __init__(self, filename, path=None):
241 def __init__(self, filename, path=None):
242 """Build a config loader for a filename and path.
242 """Build a config loader for a filename and path.
243
243
244 Parameters
244 Parameters
245 ----------
245 ----------
246 filename : str
246 filename : str
247 The file name of the config file.
247 The file name of the config file.
248 path : str, list, tuple
248 path : str, list, tuple
249 The path to search for the config file on, or a sequence of
249 The path to search for the config file on, or a sequence of
250 paths to try in order.
250 paths to try in order.
251 """
251 """
252 super(PyFileConfigLoader, self).__init__()
252 super(PyFileConfigLoader, self).__init__()
253 self.filename = filename
253 self.filename = filename
254 self.path = path
254 self.path = path
255 self.full_filename = ''
255 self.full_filename = ''
256 self.data = None
256 self.data = None
257
257
258 def load_config(self):
258 def load_config(self):
259 """Load the config from a file and return it as a Struct."""
259 """Load the config from a file and return it as a Struct."""
260 self.clear()
260 self.clear()
261 self._find_file()
261 self._find_file()
262 self._read_file_as_dict()
262 self._read_file_as_dict()
263 self._convert_to_config()
263 self._convert_to_config()
264 return self.config
264 return self.config
265
265
266 def _find_file(self):
266 def _find_file(self):
267 """Try to find the file by searching the paths."""
267 """Try to find the file by searching the paths."""
268 self.full_filename = filefind(self.filename, self.path)
268 self.full_filename = filefind(self.filename, self.path)
269
269
270 def _read_file_as_dict(self):
270 def _read_file_as_dict(self):
271 """Load the config file into self.config, with recursive loading."""
271 """Load the config file into self.config, with recursive loading."""
272 # This closure is made available in the namespace that is used
272 # This closure is made available in the namespace that is used
273 # to exec the config file. It allows users to call
273 # to exec the config file. It allows users to call
274 # load_subconfig('myconfig.py') to load config files recursively.
274 # load_subconfig('myconfig.py') to load config files recursively.
275 # It needs to be a closure because it has references to self.path
275 # It needs to be a closure because it has references to self.path
276 # and self.config. The sub-config is loaded with the same path
276 # and self.config. The sub-config is loaded with the same path
277 # as the parent, but it uses an empty config which is then merged
277 # as the parent, but it uses an empty config which is then merged
278 # with the parents.
278 # with the parents.
279
279
280 # If a profile is specified, the config file will be loaded
280 # If a profile is specified, the config file will be loaded
281 # from that profile
281 # from that profile
282
282
283 def load_subconfig(fname, profile=None):
283 def load_subconfig(fname, profile=None):
284 # import here to prevent circular imports
284 # import here to prevent circular imports
285 from IPython.core.profiledir import ProfileDir, ProfileDirError
285 from IPython.core.profiledir import ProfileDir, ProfileDirError
286 if profile is not None:
286 if profile is not None:
287 try:
287 try:
288 profile_dir = ProfileDir.find_profile_dir_by_name(
288 profile_dir = ProfileDir.find_profile_dir_by_name(
289 get_ipython_dir(),
289 get_ipython_dir(),
290 profile,
290 profile,
291 )
291 )
292 except ProfileDirError:
292 except ProfileDirError:
293 return
293 return
294 path = profile_dir.location
294 path = profile_dir.location
295 else:
295 else:
296 path = self.path
296 path = self.path
297 loader = PyFileConfigLoader(fname, path)
297 loader = PyFileConfigLoader(fname, path)
298 try:
298 try:
299 sub_config = loader.load_config()
299 sub_config = loader.load_config()
300 except IOError:
300 except IOError:
301 # Pass silently if the sub config is not there. This happens
301 # Pass silently if the sub config is not there. This happens
302 # when a user s using a profile, but not the default config.
302 # when a user s using a profile, but not the default config.
303 pass
303 pass
304 else:
304 else:
305 self.config._merge(sub_config)
305 self.config._merge(sub_config)
306
306
307 # Again, this needs to be a closure and should be used in config
307 # Again, this needs to be a closure and should be used in config
308 # files to get the config being loaded.
308 # files to get the config being loaded.
309 def get_config():
309 def get_config():
310 return self.config
310 return self.config
311
311
312 namespace = dict(load_subconfig=load_subconfig, get_config=get_config)
312 namespace = dict(load_subconfig=load_subconfig, get_config=get_config)
313 fs_encoding = sys.getfilesystemencoding() or 'ascii'
313 fs_encoding = sys.getfilesystemencoding() or 'ascii'
314 conf_filename = self.full_filename.encode(fs_encoding)
314 conf_filename = self.full_filename.encode(fs_encoding)
315 py3compat.execfile(conf_filename, namespace)
315 py3compat.execfile(conf_filename, namespace)
316
316
317 def _convert_to_config(self):
317 def _convert_to_config(self):
318 if self.data is None:
318 if self.data is None:
319 ConfigLoaderError('self.data does not exist')
319 ConfigLoaderError('self.data does not exist')
320
320
321
321
322 class CommandLineConfigLoader(ConfigLoader):
322 class CommandLineConfigLoader(ConfigLoader):
323 """A config loader for command line arguments.
323 """A config loader for command line arguments.
324
324
325 As we add more command line based loaders, the common logic should go
325 As we add more command line based loaders, the common logic should go
326 here.
326 here.
327 """
327 """
328
328
329 def _exec_config_str(self, lhs, rhs):
329 def _exec_config_str(self, lhs, rhs):
330 exec_str = 'self.config.' + lhs + '=' + rhs
330 exec_str = 'self.config.' + lhs + '=' + rhs
331 try:
331 try:
332 # Try to see if regular Python syntax will work. This
332 # Try to see if regular Python syntax will work. This
333 # won't handle strings as the quote marks are removed
333 # won't handle strings as the quote marks are removed
334 # by the system shell.
334 # by the system shell.
335 exec exec_str in locals(), globals()
335 exec exec_str in locals(), globals()
336 except (NameError, SyntaxError):
336 except (NameError, SyntaxError):
337 # This case happens if the rhs is a string but without
337 # This case happens if the rhs is a string but without
338 # the quote marks. Use repr, to get quote marks, and
338 # the quote marks. Use repr, to get quote marks, and
339 # 'u' prefix and see if
339 # 'u' prefix and see if
340 # it succeeds. If it still fails, we let it raise.
340 # it succeeds. If it still fails, we let it raise.
341 exec_str = u'self.config.' + lhs + '=' + repr(rhs)
341 exec_str = u'self.config.' + lhs + '=' + repr(rhs)
342 exec exec_str in locals(), globals()
342 exec exec_str in locals(), globals()
343
343
344 def _load_flag(self, cfg):
344 def _load_flag(self, cfg):
345 """update self.config from a flag, which can be a dict or Config"""
345 """update self.config from a flag, which can be a dict or Config"""
346 if isinstance(cfg, (dict, Config)):
346 if isinstance(cfg, (dict, Config)):
347 # don't clobber whole config sections, update
347 # don't clobber whole config sections, update
348 # each section from config:
348 # each section from config:
349 for sec,c in cfg.iteritems():
349 for sec,c in cfg.iteritems():
350 self.config[sec].update(c)
350 self.config[sec].update(c)
351 else:
351 else:
352 raise ValueError("Invalid flag: '%s'"%raw)
352 raise ValueError("Invalid flag: '%s'"%raw)
353
353
354 # raw --identifier=value pattern
354 # raw --identifier=value pattern
355 # but *also* accept '-' as wordsep, for aliases
355 # but *also* accept '-' as wordsep, for aliases
356 # accepts: --foo=a
356 # accepts: --foo=a
357 # --Class.trait=value
357 # --Class.trait=value
358 # --alias-name=value
358 # --alias-name=value
359 # rejects: -foo=value
359 # rejects: -foo=value
360 # --foo
360 # --foo
361 # --Class.trait
361 # --Class.trait
362 kv_pattern = re.compile(r'\-\-[A-Za-z][\w\-]*(\.[\w\-]+)*\=.*')
362 kv_pattern = re.compile(r'\-\-[A-Za-z][\w\-]*(\.[\w\-]+)*\=.*')
363
363
364 # just flags, no assignments, with two *or one* leading '-'
364 # just flags, no assignments, with two *or one* leading '-'
365 # accepts: --foo
365 # accepts: --foo
366 # -foo-bar-again
366 # -foo-bar-again
367 # rejects: --anything=anything
367 # rejects: --anything=anything
368 # --two.word
368 # --two.word
369
369
370 flag_pattern = re.compile(r'\-\-?\w+[\-\w]*$')
370 flag_pattern = re.compile(r'\-\-?\w+[\-\w]*$')
371
371
372 class KeyValueConfigLoader(CommandLineConfigLoader):
372 class KeyValueConfigLoader(CommandLineConfigLoader):
373 """A config loader that loads key value pairs from the command line.
373 """A config loader that loads key value pairs from the command line.
374
374
375 This allows command line options to be gives in the following form::
375 This allows command line options to be gives in the following form::
376
376
377 ipython --profile="foo" --InteractiveShell.autocall=False
377 ipython --profile="foo" --InteractiveShell.autocall=False
378 """
378 """
379
379
380 def __init__(self, argv=None, aliases=None, flags=None):
380 def __init__(self, argv=None, aliases=None, flags=None):
381 """Create a key value pair config loader.
381 """Create a key value pair config loader.
382
382
383 Parameters
383 Parameters
384 ----------
384 ----------
385 argv : list
385 argv : list
386 A list that has the form of sys.argv[1:] which has unicode
386 A list that has the form of sys.argv[1:] which has unicode
387 elements of the form u"key=value". If this is None (default),
387 elements of the form u"key=value". If this is None (default),
388 then sys.argv[1:] will be used.
388 then sys.argv[1:] will be used.
389 aliases : dict
389 aliases : dict
390 A dict of aliases for configurable traits.
390 A dict of aliases for configurable traits.
391 Keys are the short aliases, Values are the resolved trait.
391 Keys are the short aliases, Values are the resolved trait.
392 Of the form: `{'alias' : 'Configurable.trait'}`
392 Of the form: `{'alias' : 'Configurable.trait'}`
393 flags : dict
393 flags : dict
394 A dict of flags, keyed by str name. Vaues can be Config objects,
394 A dict of flags, keyed by str name. Vaues can be Config objects,
395 dicts, or "key=value" strings. If Config or dict, when the flag
395 dicts, or "key=value" strings. If Config or dict, when the flag
396 is triggered, The flag is loaded as `self.config.update(m)`.
396 is triggered, The flag is loaded as `self.config.update(m)`.
397
397
398 Returns
398 Returns
399 -------
399 -------
400 config : Config
400 config : Config
401 The resulting Config object.
401 The resulting Config object.
402
402
403 Examples
403 Examples
404 --------
404 --------
405
405
406 >>> from IPython.config.loader import KeyValueConfigLoader
406 >>> from IPython.config.loader import KeyValueConfigLoader
407 >>> cl = KeyValueConfigLoader()
407 >>> cl = KeyValueConfigLoader()
408 >>> cl.load_config(["--A.name='brian'","--B.number=0"])
408 >>> cl.load_config(["--A.name='brian'","--B.number=0"])
409 {'A': {'name': 'brian'}, 'B': {'number': 0}}
409 {'A': {'name': 'brian'}, 'B': {'number': 0}}
410 """
410 """
411 self.clear()
411 self.clear()
412 if argv is None:
412 if argv is None:
413 argv = sys.argv[1:]
413 argv = sys.argv[1:]
414 self.argv = argv
414 self.argv = argv
415 self.aliases = aliases or {}
415 self.aliases = aliases or {}
416 self.flags = flags or {}
416 self.flags = flags or {}
417
417
418
418
419 def clear(self):
419 def clear(self):
420 super(KeyValueConfigLoader, self).clear()
420 super(KeyValueConfigLoader, self).clear()
421 self.extra_args = []
421 self.extra_args = []
422
422
423
423
424 def _decode_argv(self, argv, enc=None):
424 def _decode_argv(self, argv, enc=None):
425 """decode argv if bytes, using stin.encoding, falling back on default enc"""
425 """decode argv if bytes, using stin.encoding, falling back on default enc"""
426 uargv = []
426 uargv = []
427 if enc is None:
427 if enc is None:
428 enc = text.getdefaultencoding()
428 enc = text.getdefaultencoding()
429 for arg in argv:
429 for arg in argv:
430 if not isinstance(arg, unicode):
430 if not isinstance(arg, unicode):
431 # only decode if not already decoded
431 # only decode if not already decoded
432 arg = arg.decode(enc)
432 arg = arg.decode(enc)
433 uargv.append(arg)
433 uargv.append(arg)
434 return uargv
434 return uargv
435
435
436
436
437 def load_config(self, argv=None, aliases=None, flags=None):
437 def load_config(self, argv=None, aliases=None, flags=None):
438 """Parse the configuration and generate the Config object.
438 """Parse the configuration and generate the Config object.
439
439
440 After loading, any arguments that are not key-value or
440 After loading, any arguments that are not key-value or
441 flags will be stored in self.extra_args - a list of
441 flags will be stored in self.extra_args - a list of
442 unparsed command-line arguments. This is used for
442 unparsed command-line arguments. This is used for
443 arguments such as input files or subcommands.
443 arguments such as input files or subcommands.
444
444
445 Parameters
445 Parameters
446 ----------
446 ----------
447 argv : list, optional
447 argv : list, optional
448 A list that has the form of sys.argv[1:] which has unicode
448 A list that has the form of sys.argv[1:] which has unicode
449 elements of the form u"key=value". If this is None (default),
449 elements of the form u"key=value". If this is None (default),
450 then self.argv will be used.
450 then self.argv will be used.
451 aliases : dict
451 aliases : dict
452 A dict of aliases for configurable traits.
452 A dict of aliases for configurable traits.
453 Keys are the short aliases, Values are the resolved trait.
453 Keys are the short aliases, Values are the resolved trait.
454 Of the form: `{'alias' : 'Configurable.trait'}`
454 Of the form: `{'alias' : 'Configurable.trait'}`
455 flags : dict
455 flags : dict
456 A dict of flags, keyed by str name. Values can be Config objects
456 A dict of flags, keyed by str name. Values can be Config objects
457 or dicts. When the flag is triggered, The config is loaded as
457 or dicts. When the flag is triggered, The config is loaded as
458 `self.config.update(cfg)`.
458 `self.config.update(cfg)`.
459 """
459 """
460 from IPython.config.configurable import Configurable
460 from IPython.config.configurable import Configurable
461
461
462 self.clear()
462 self.clear()
463 if argv is None:
463 if argv is None:
464 argv = self.argv
464 argv = self.argv
465 if aliases is None:
465 if aliases is None:
466 aliases = self.aliases
466 aliases = self.aliases
467 if flags is None:
467 if flags is None:
468 flags = self.flags
468 flags = self.flags
469
469
470 # ensure argv is a list of unicode strings:
470 # ensure argv is a list of unicode strings:
471 uargv = self._decode_argv(argv)
471 uargv = self._decode_argv(argv)
472 for idx,raw in enumerate(uargv):
472 for idx,raw in enumerate(uargv):
473 # strip leading '-'
473 # strip leading '-'
474 item = raw.lstrip('-')
474 item = raw.lstrip('-')
475
475
476 if raw == '--':
476 if raw == '--':
477 # don't parse arguments after '--'
477 # don't parse arguments after '--'
478 # this is useful for relaying arguments to scripts, e.g.
478 # this is useful for relaying arguments to scripts, e.g.
479 # ipython -i foo.py --pylab=qt -- args after '--' go-to-foo.py
479 # ipython -i foo.py --pylab=qt -- args after '--' go-to-foo.py
480 self.extra_args.extend(uargv[idx+1:])
480 self.extra_args.extend(uargv[idx+1:])
481 break
481 break
482
482
483 if kv_pattern.match(raw):
483 if kv_pattern.match(raw):
484 lhs,rhs = item.split('=',1)
484 lhs,rhs = item.split('=',1)
485 # Substitute longnames for aliases.
485 # Substitute longnames for aliases.
486 if lhs in aliases:
486 if lhs in aliases:
487 lhs = aliases[lhs]
487 lhs = aliases[lhs]
488 if '.' not in lhs:
488 if '.' not in lhs:
489 # probably a mistyped alias, but not technically illegal
489 # probably a mistyped alias, but not technically illegal
490 warn.warn("Unrecognized alias: '%s', it will probably have no effect."%lhs)
490 warn.warn("Unrecognized alias: '%s', it will probably have no effect."%lhs)
491 self._exec_config_str(lhs, rhs)
491 self._exec_config_str(lhs, rhs)
492
492
493 elif flag_pattern.match(raw):
493 elif flag_pattern.match(raw):
494 if item in flags:
494 if item in flags:
495 cfg,help = flags[item]
495 cfg,help = flags[item]
496 self._load_flag(cfg)
496 self._load_flag(cfg)
497 else:
497 else:
498 raise ArgumentError("Unrecognized flag: '%s'"%raw)
498 raise ArgumentError("Unrecognized flag: '%s'"%raw)
499 elif raw.startswith('-'):
499 elif raw.startswith('-'):
500 kv = '--'+item
500 kv = '--'+item
501 if kv_pattern.match(kv):
501 if kv_pattern.match(kv):
502 raise ArgumentError("Invalid argument: '%s', did you mean '%s'?"%(raw, kv))
502 raise ArgumentError("Invalid argument: '%s', did you mean '%s'?"%(raw, kv))
503 else:
503 else:
504 raise ArgumentError("Invalid argument: '%s'"%raw)
504 raise ArgumentError("Invalid argument: '%s'"%raw)
505 else:
505 else:
506 # keep all args that aren't valid in a list,
506 # keep all args that aren't valid in a list,
507 # in case our parent knows what to do with them.
507 # in case our parent knows what to do with them.
508 self.extra_args.append(item)
508 self.extra_args.append(item)
509 return self.config
509 return self.config
510
510
511 class ArgParseConfigLoader(CommandLineConfigLoader):
511 class ArgParseConfigLoader(CommandLineConfigLoader):
512 """A loader that uses the argparse module to load from the command line."""
512 """A loader that uses the argparse module to load from the command line."""
513
513
514 def __init__(self, argv=None, aliases=None, flags=None, *parser_args, **parser_kw):
514 def __init__(self, argv=None, aliases=None, flags=None, *parser_args, **parser_kw):
515 """Create a config loader for use with argparse.
515 """Create a config loader for use with argparse.
516
516
517 Parameters
517 Parameters
518 ----------
518 ----------
519
519
520 argv : optional, list
520 argv : optional, list
521 If given, used to read command-line arguments from, otherwise
521 If given, used to read command-line arguments from, otherwise
522 sys.argv[1:] is used.
522 sys.argv[1:] is used.
523
523
524 parser_args : tuple
524 parser_args : tuple
525 A tuple of positional arguments that will be passed to the
525 A tuple of positional arguments that will be passed to the
526 constructor of :class:`argparse.ArgumentParser`.
526 constructor of :class:`argparse.ArgumentParser`.
527
527
528 parser_kw : dict
528 parser_kw : dict
529 A tuple of keyword arguments that will be passed to the
529 A tuple of keyword arguments that will be passed to the
530 constructor of :class:`argparse.ArgumentParser`.
530 constructor of :class:`argparse.ArgumentParser`.
531
531
532 Returns
532 Returns
533 -------
533 -------
534 config : Config
534 config : Config
535 The resulting Config object.
535 The resulting Config object.
536 """
536 """
537 super(CommandLineConfigLoader, self).__init__()
537 super(CommandLineConfigLoader, self).__init__()
538 self.clear()
538 self.clear()
539 if argv is None:
539 if argv is None:
540 argv = sys.argv[1:]
540 argv = sys.argv[1:]
541 self.argv = argv
541 self.argv = argv
542 self.aliases = aliases or {}
542 self.aliases = aliases or {}
543 self.flags = flags or {}
543 self.flags = flags or {}
544
544
545 self.parser_args = parser_args
545 self.parser_args = parser_args
546 self.version = parser_kw.pop("version", None)
546 self.version = parser_kw.pop("version", None)
547 kwargs = dict(argument_default=argparse.SUPPRESS)
547 kwargs = dict(argument_default=argparse.SUPPRESS)
548 kwargs.update(parser_kw)
548 kwargs.update(parser_kw)
549 self.parser_kw = kwargs
549 self.parser_kw = kwargs
550
550
551 def load_config(self, argv=None, aliases=None, flags=None):
551 def load_config(self, argv=None, aliases=None, flags=None):
552 """Parse command line arguments and return as a Config object.
552 """Parse command line arguments and return as a Config object.
553
553
554 Parameters
554 Parameters
555 ----------
555 ----------
556
556
557 args : optional, list
557 args : optional, list
558 If given, a list with the structure of sys.argv[1:] to parse
558 If given, a list with the structure of sys.argv[1:] to parse
559 arguments from. If not given, the instance's self.argv attribute
559 arguments from. If not given, the instance's self.argv attribute
560 (given at construction time) is used."""
560 (given at construction time) is used."""
561 self.clear()
561 self.clear()
562 if argv is None:
562 if argv is None:
563 argv = self.argv
563 argv = self.argv
564 if aliases is None:
564 if aliases is None:
565 aliases = self.aliases
565 aliases = self.aliases
566 if flags is None:
566 if flags is None:
567 flags = self.flags
567 flags = self.flags
568 self._create_parser(aliases, flags)
568 self._create_parser(aliases, flags)
569 self._parse_args(argv)
569 self._parse_args(argv)
570 self._convert_to_config()
570 self._convert_to_config()
571 return self.config
571 return self.config
572
572
573 def get_extra_args(self):
573 def get_extra_args(self):
574 if hasattr(self, 'extra_args'):
574 if hasattr(self, 'extra_args'):
575 return self.extra_args
575 return self.extra_args
576 else:
576 else:
577 return []
577 return []
578
578
579 def _create_parser(self, aliases=None, flags=None):
579 def _create_parser(self, aliases=None, flags=None):
580 self.parser = ArgumentParser(*self.parser_args, **self.parser_kw)
580 self.parser = ArgumentParser(*self.parser_args, **self.parser_kw)
581 self._add_arguments(aliases, flags)
581 self._add_arguments(aliases, flags)
582
582
583 def _add_arguments(self, aliases=None, flags=None):
583 def _add_arguments(self, aliases=None, flags=None):
584 raise NotImplementedError("subclasses must implement _add_arguments")
584 raise NotImplementedError("subclasses must implement _add_arguments")
585
585
586 def _parse_args(self, args):
586 def _parse_args(self, args):
587 """self.parser->self.parsed_data"""
587 """self.parser->self.parsed_data"""
588 # decode sys.argv to support unicode command-line options
588 # decode sys.argv to support unicode command-line options
589 enc = text.getdefaultencoding()
589 enc = text.getdefaultencoding()
590 uargs = [py3compat.cast_unicode(a, enc) for a in args]
590 uargs = [py3compat.cast_unicode(a, enc) for a in args]
591 self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs)
591 self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs)
592
592
593 def _convert_to_config(self):
593 def _convert_to_config(self):
594 """self.parsed_data->self.config"""
594 """self.parsed_data->self.config"""
595 for k, v in vars(self.parsed_data).iteritems():
595 for k, v in vars(self.parsed_data).iteritems():
596 exec "self.config.%s = v"%k in locals(), globals()
596 exec "self.config.%s = v"%k in locals(), globals()
597
597
598 class KVArgParseConfigLoader(ArgParseConfigLoader):
598 class KVArgParseConfigLoader(ArgParseConfigLoader):
599 """A config loader that loads aliases and flags with argparse,
599 """A config loader that loads aliases and flags with argparse,
600 but will use KVLoader for the rest. This allows better parsing
600 but will use KVLoader for the rest. This allows better parsing
601 of common args, such as `ipython -c 'print 5'`, but still gets
601 of common args, such as `ipython -c 'print 5'`, but still gets
602 arbitrary config with `ipython --InteractiveShell.use_readline=False`"""
602 arbitrary config with `ipython --InteractiveShell.use_readline=False`"""
603
603
604 def _convert_to_config(self):
604 def _convert_to_config(self):
605 """self.parsed_data->self.config"""
605 """self.parsed_data->self.config"""
606 for k, v in vars(self.parsed_data).iteritems():
606 for k, v in vars(self.parsed_data).iteritems():
607 self._exec_config_str(k, v)
607 self._exec_config_str(k, v)
608
608
609 def _add_arguments(self, aliases=None, flags=None):
609 def _add_arguments(self, aliases=None, flags=None):
610 self.alias_flags = {}
610 self.alias_flags = {}
611 # print aliases, flags
611 # print aliases, flags
612 if aliases is None:
612 if aliases is None:
613 aliases = self.aliases
613 aliases = self.aliases
614 if flags is None:
614 if flags is None:
615 flags = self.flags
615 flags = self.flags
616 paa = self.parser.add_argument
616 paa = self.parser.add_argument
617 for key,value in aliases.iteritems():
617 for key,value in aliases.iteritems():
618 if key in flags:
618 if key in flags:
619 # flags
619 # flags
620 nargs = '?'
620 nargs = '?'
621 else:
621 else:
622 nargs = None
622 nargs = None
623 if len(key) is 1:
623 if len(key) is 1:
624 paa('-'+key, '--'+key, type=unicode, dest=value, nargs=nargs)
624 paa('-'+key, '--'+key, type=unicode, dest=value, nargs=nargs)
625 else:
625 else:
626 paa('--'+key, type=unicode, dest=value, nargs=nargs)
626 paa('--'+key, type=unicode, dest=value, nargs=nargs)
627 for key, (value, help) in flags.iteritems():
627 for key, (value, help) in flags.iteritems():
628 if key in self.aliases:
628 if key in self.aliases:
629 #
629 #
630 self.alias_flags[self.aliases[key]] = value
630 self.alias_flags[self.aliases[key]] = value
631 continue
631 continue
632 if len(key) is 1:
632 if len(key) is 1:
633 paa('-'+key, '--'+key, action='append_const', dest='_flags', const=value)
633 paa('-'+key, '--'+key, action='append_const', dest='_flags', const=value)
634 else:
634 else:
635 paa('--'+key, action='append_const', dest='_flags', const=value)
635 paa('--'+key, action='append_const', dest='_flags', const=value)
636
636
637 def _convert_to_config(self):
637 def _convert_to_config(self):
638 """self.parsed_data->self.config, parse unrecognized extra args via KVLoader."""
638 """self.parsed_data->self.config, parse unrecognized extra args via KVLoader."""
639 # remove subconfigs list from namespace before transforming the Namespace
639 # remove subconfigs list from namespace before transforming the Namespace
640 if '_flags' in self.parsed_data:
640 if '_flags' in self.parsed_data:
641 subcs = self.parsed_data._flags
641 subcs = self.parsed_data._flags
642 del self.parsed_data._flags
642 del self.parsed_data._flags
643 else:
643 else:
644 subcs = []
644 subcs = []
645
645
646 for k, v in vars(self.parsed_data).iteritems():
646 for k, v in vars(self.parsed_data).iteritems():
647 if v is None:
647 if v is None:
648 # it was a flag that shares the name of an alias
648 # it was a flag that shares the name of an alias
649 subcs.append(self.alias_flags[k])
649 subcs.append(self.alias_flags[k])
650 else:
650 else:
651 # eval the KV assignment
651 # eval the KV assignment
652 self._exec_config_str(k, v)
652 self._exec_config_str(k, v)
653
653
654 for subc in subcs:
654 for subc in subcs:
655 self._load_flag(subc)
655 self._load_flag(subc)
656
656
657 if self.extra_args:
657 if self.extra_args:
658 sub_parser = KeyValueConfigLoader()
658 sub_parser = KeyValueConfigLoader()
659 sub_parser.load_config(self.extra_args)
659 sub_parser.load_config(self.extra_args)
660 self.config._merge(sub_parser.config)
660 self.config._merge(sub_parser.config)
661 self.extra_args = sub_parser.extra_args
661 self.extra_args = sub_parser.extra_args
@@ -1,263 +1,263 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 System command aliases.
3 System command aliases.
4
4
5 Authors:
5 Authors:
6
6
7 * Fernando Perez
7 * Fernando Perez
8 * Brian Granger
8 * Brian Granger
9 """
9 """
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2008-2010 The IPython Development Team
12 # Copyright (C) 2008-2010 The IPython Development Team
13 #
13 #
14 # Distributed under the terms of the BSD License.
14 # Distributed under the terms of the BSD License.
15 #
15 #
16 # The full license is in the file COPYING.txt, distributed with this software.
16 # The full license is in the file COPYING.txt, distributed with this software.
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Imports
20 # Imports
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 import __builtin__
23 import __builtin__
24 import keyword
24 import keyword
25 import os
25 import os
26 import re
26 import re
27 import sys
27 import sys
28
28
29 from IPython.config.configurable import Configurable
29 from IPython.config.configurable import Configurable
30 from IPython.core.splitinput import split_user_input
30 from IPython.core.splitinput import split_user_input
31
31
32 from IPython.utils.traitlets import List, Instance
32 from IPython.utils.traitlets import List, Instance
33 from IPython.utils.autoattr import auto_attr
33 from IPython.utils.autoattr import auto_attr
34 from IPython.utils.warn import warn, error
34 from IPython.utils.warn import warn, error
35
35
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37 # Utilities
37 # Utilities
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39
39
40 # This is used as the pattern for calls to split_user_input.
40 # This is used as the pattern for calls to split_user_input.
41 shell_line_split = re.compile(r'^(\s*)()(\S+)(.*$)')
41 shell_line_split = re.compile(r'^(\s*)()(\S+)(.*$)')
42
42
43 def default_aliases():
43 def default_aliases():
44 """Return list of shell aliases to auto-define.
44 """Return list of shell aliases to auto-define.
45 """
45 """
46 # Note: the aliases defined here should be safe to use on a kernel
46 # Note: the aliases defined here should be safe to use on a kernel
47 # regardless of what frontend it is attached to. Frontends that use a
47 # regardless of what frontend it is attached to. Frontends that use a
48 # kernel in-process can define additional aliases that will only work in
48 # kernel in-process can define additional aliases that will only work in
49 # their case. For example, things like 'less' or 'clear' that manipulate
49 # their case. For example, things like 'less' or 'clear' that manipulate
50 # the terminal should NOT be declared here, as they will only work if the
50 # the terminal should NOT be declared here, as they will only work if the
51 # kernel is running inside a true terminal, and not over the network.
51 # kernel is running inside a true terminal, and not over the network.
52
52
53 if os.name == 'posix':
53 if os.name == 'posix':
54 default_aliases = [('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
54 default_aliases = [('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
55 ('mv', 'mv -i'), ('rm', 'rm -i'), ('cp', 'cp -i'),
55 ('mv', 'mv -i'), ('rm', 'rm -i'), ('cp', 'cp -i'),
56 ('cat', 'cat'),
56 ('cat', 'cat'),
57 ]
57 ]
58 # Useful set of ls aliases. The GNU and BSD options are a little
58 # Useful set of ls aliases. The GNU and BSD options are a little
59 # different, so we make aliases that provide as similar as possible
59 # different, so we make aliases that provide as similar as possible
60 # behavior in ipython, by passing the right flags for each platform
60 # behavior in ipython, by passing the right flags for each platform
61 if sys.platform.startswith('linux'):
61 if sys.platform.startswith('linux'):
62 ls_aliases = [('ls', 'ls -F --color'),
62 ls_aliases = [('ls', 'ls -F --color'),
63 # long ls
63 # long ls
64 ('ll', 'ls -F -o --color'),
64 ('ll', 'ls -F -o --color'),
65 # ls normal files only
65 # ls normal files only
66 ('lf', 'ls -F -o --color %l | grep ^-'),
66 ('lf', 'ls -F -o --color %l | grep ^-'),
67 # ls symbolic links
67 # ls symbolic links
68 ('lk', 'ls -F -o --color %l | grep ^l'),
68 ('lk', 'ls -F -o --color %l | grep ^l'),
69 # directories or links to directories,
69 # directories or links to directories,
70 ('ldir', 'ls -F -o --color %l | grep /$'),
70 ('ldir', 'ls -F -o --color %l | grep /$'),
71 # things which are executable
71 # things which are executable
72 ('lx', 'ls -F -o --color %l | grep ^-..x'),
72 ('lx', 'ls -F -o --color %l | grep ^-..x'),
73 ]
73 ]
74 else:
74 else:
75 # BSD, OSX, etc.
75 # BSD, OSX, etc.
76 ls_aliases = [('ls', 'ls -F'),
76 ls_aliases = [('ls', 'ls -F'),
77 # long ls
77 # long ls
78 ('ll', 'ls -F -l'),
78 ('ll', 'ls -F -l'),
79 # ls normal files only
79 # ls normal files only
80 ('lf', 'ls -F -l %l | grep ^-'),
80 ('lf', 'ls -F -l %l | grep ^-'),
81 # ls symbolic links
81 # ls symbolic links
82 ('lk', 'ls -F -l %l | grep ^l'),
82 ('lk', 'ls -F -l %l | grep ^l'),
83 # directories or links to directories,
83 # directories or links to directories,
84 ('ldir', 'ls -F -l %l | grep /$'),
84 ('ldir', 'ls -F -l %l | grep /$'),
85 # things which are executable
85 # things which are executable
86 ('lx', 'ls -F -l %l | grep ^-..x'),
86 ('lx', 'ls -F -l %l | grep ^-..x'),
87 ]
87 ]
88 default_aliases = default_aliases + ls_aliases
88 default_aliases = default_aliases + ls_aliases
89 elif os.name in ['nt', 'dos']:
89 elif os.name in ['nt', 'dos']:
90 default_aliases = [('ls', 'dir /on'),
90 default_aliases = [('ls', 'dir /on'),
91 ('ddir', 'dir /ad /on'), ('ldir', 'dir /ad /on'),
91 ('ddir', 'dir /ad /on'), ('ldir', 'dir /ad /on'),
92 ('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
92 ('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
93 ('echo', 'echo'), ('ren', 'ren'), ('copy', 'copy'),
93 ('echo', 'echo'), ('ren', 'ren'), ('copy', 'copy'),
94 ]
94 ]
95 else:
95 else:
96 default_aliases = []
96 default_aliases = []
97
97
98 return default_aliases
98 return default_aliases
99
99
100
100
101 class AliasError(Exception):
101 class AliasError(Exception):
102 pass
102 pass
103
103
104
104
105 class InvalidAliasError(AliasError):
105 class InvalidAliasError(AliasError):
106 pass
106 pass
107
107
108 #-----------------------------------------------------------------------------
108 #-----------------------------------------------------------------------------
109 # Main AliasManager class
109 # Main AliasManager class
110 #-----------------------------------------------------------------------------
110 #-----------------------------------------------------------------------------
111
111
112 class AliasManager(Configurable):
112 class AliasManager(Configurable):
113
113
114 default_aliases = List(default_aliases(), config=True)
114 default_aliases = List(default_aliases(), config=True)
115 user_aliases = List(default_value=[], config=True)
115 user_aliases = List(default_value=[], config=True)
116 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
116 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
117
117
118 def __init__(self, shell=None, config=None):
118 def __init__(self, shell=None, config=None):
119 super(AliasManager, self).__init__(shell=shell, config=config)
119 super(AliasManager, self).__init__(shell=shell, config=config)
120 self.alias_table = {}
120 self.alias_table = {}
121 self.exclude_aliases()
121 self.exclude_aliases()
122 self.init_aliases()
122 self.init_aliases()
123
123
124 def __contains__(self, name):
124 def __contains__(self, name):
125 return name in self.alias_table
125 return name in self.alias_table
126
126
127 @property
127 @property
128 def aliases(self):
128 def aliases(self):
129 return [(item[0], item[1][1]) for item in self.alias_table.iteritems()]
129 return [(item[0], item[1][1]) for item in self.alias_table.iteritems()]
130
130
131 def exclude_aliases(self):
131 def exclude_aliases(self):
132 # set of things NOT to alias (keywords, builtins and some magics)
132 # set of things NOT to alias (keywords, builtins and some magics)
133 no_alias = set(['cd','popd','pushd','dhist','alias','unalias'])
133 no_alias = set(['cd','popd','pushd','dhist','alias','unalias'])
134 no_alias.update(set(keyword.kwlist))
134 no_alias.update(set(keyword.kwlist))
135 no_alias.update(set(__builtin__.__dict__.keys()))
135 no_alias.update(set(__builtin__.__dict__.keys()))
136 self.no_alias = no_alias
136 self.no_alias = no_alias
137
137
138 def init_aliases(self):
138 def init_aliases(self):
139 # Load default aliases
139 # Load default aliases
140 for name, cmd in self.default_aliases:
140 for name, cmd in self.default_aliases:
141 self.soft_define_alias(name, cmd)
141 self.soft_define_alias(name, cmd)
142
142
143 # Load user aliases
143 # Load user aliases
144 for name, cmd in self.user_aliases:
144 for name, cmd in self.user_aliases:
145 self.soft_define_alias(name, cmd)
145 self.soft_define_alias(name, cmd)
146
146
147 def clear_aliases(self):
147 def clear_aliases(self):
148 self.alias_table.clear()
148 self.alias_table.clear()
149
149
150 def soft_define_alias(self, name, cmd):
150 def soft_define_alias(self, name, cmd):
151 """Define an alias, but don't raise on an AliasError."""
151 """Define an alias, but don't raise on an AliasError."""
152 try:
152 try:
153 self.define_alias(name, cmd)
153 self.define_alias(name, cmd)
154 except AliasError, e:
154 except AliasError, e:
155 error("Invalid alias: %s" % e)
155 error("Invalid alias: %s" % e)
156
156
157 def define_alias(self, name, cmd):
157 def define_alias(self, name, cmd):
158 """Define a new alias after validating it.
158 """Define a new alias after validating it.
159
159
160 This will raise an :exc:`AliasError` if there are validation
160 This will raise an :exc:`AliasError` if there are validation
161 problems.
161 problems.
162 """
162 """
163 nargs = self.validate_alias(name, cmd)
163 nargs = self.validate_alias(name, cmd)
164 self.alias_table[name] = (nargs, cmd)
164 self.alias_table[name] = (nargs, cmd)
165
165
166 def undefine_alias(self, name):
166 def undefine_alias(self, name):
167 if self.alias_table.has_key(name):
167 if self.alias_table.has_key(name):
168 del self.alias_table[name]
168 del self.alias_table[name]
169
169
170 def validate_alias(self, name, cmd):
170 def validate_alias(self, name, cmd):
171 """Validate an alias and return the its number of arguments."""
171 """Validate an alias and return the its number of arguments."""
172 if name in self.no_alias:
172 if name in self.no_alias:
173 raise InvalidAliasError("The name %s can't be aliased "
173 raise InvalidAliasError("The name %s can't be aliased "
174 "because it is a keyword or builtin." % name)
174 "because it is a keyword or builtin." % name)
175 if not (isinstance(cmd, basestring)):
175 if not (isinstance(cmd, basestring)):
176 raise InvalidAliasError("An alias command must be a string, "
176 raise InvalidAliasError("An alias command must be a string, "
177 "got: %r" % name)
177 "got: %r" % name)
178 nargs = cmd.count('%s')
178 nargs = cmd.count('%s')
179 if nargs>0 and cmd.find('%l')>=0:
179 if nargs>0 and cmd.find('%l')>=0:
180 raise InvalidAliasError('The %s and %l specifiers are mutually '
180 raise InvalidAliasError('The %s and %l specifiers are mutually '
181 'exclusive in alias definitions.')
181 'exclusive in alias definitions.')
182 return nargs
182 return nargs
183
183
184 def call_alias(self, alias, rest=''):
184 def call_alias(self, alias, rest=''):
185 """Call an alias given its name and the rest of the line."""
185 """Call an alias given its name and the rest of the line."""
186 cmd = self.transform_alias(alias, rest)
186 cmd = self.transform_alias(alias, rest)
187 try:
187 try:
188 self.shell.system(cmd)
188 self.shell.system(cmd)
189 except:
189 except:
190 self.shell.showtraceback()
190 self.shell.showtraceback()
191
191
192 def transform_alias(self, alias,rest=''):
192 def transform_alias(self, alias,rest=''):
193 """Transform alias to system command string."""
193 """Transform alias to system command string."""
194 nargs, cmd = self.alias_table[alias]
194 nargs, cmd = self.alias_table[alias]
195
195
196 if ' ' in cmd and os.path.isfile(cmd):
196 if ' ' in cmd and os.path.isfile(cmd):
197 cmd = '"%s"' % cmd
197 cmd = '"%s"' % cmd
198
198
199 # Expand the %l special to be the user's input line
199 # Expand the %l special to be the user's input line
200 if cmd.find('%l') >= 0:
200 if cmd.find('%l') >= 0:
201 cmd = cmd.replace('%l', rest)
201 cmd = cmd.replace('%l', rest)
202 rest = ''
202 rest = ''
203 if nargs==0:
203 if nargs==0:
204 # Simple, argument-less aliases
204 # Simple, argument-less aliases
205 cmd = '%s %s' % (cmd, rest)
205 cmd = '%s %s' % (cmd, rest)
206 else:
206 else:
207 # Handle aliases with positional arguments
207 # Handle aliases with positional arguments
208 args = rest.split(None, nargs)
208 args = rest.split(None, nargs)
209 if len(args) < nargs:
209 if len(args) < nargs:
210 raise AliasError('Alias <%s> requires %s arguments, %s given.' %
210 raise AliasError('Alias <%s> requires %s arguments, %s given.' %
211 (alias, nargs, len(args)))
211 (alias, nargs, len(args)))
212 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
212 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
213 return cmd
213 return cmd
214
214
215 def expand_alias(self, line):
215 def expand_alias(self, line):
216 """ Expand an alias in the command line
216 """ Expand an alias in the command line
217
217
218 Returns the provided command line, possibly with the first word
218 Returns the provided command line, possibly with the first word
219 (command) translated according to alias expansion rules.
219 (command) translated according to alias expansion rules.
220
220
221 [ipython]|16> _ip.expand_aliases("np myfile.txt")
221 [ipython]|16> _ip.expand_aliases("np myfile.txt")
222 <16> 'q:/opt/np/notepad++.exe myfile.txt'
222 <16> 'q:/opt/np/notepad++.exe myfile.txt'
223 """
223 """
224
224
225 pre,_,fn,rest = split_user_input(line)
225 pre,_,fn,rest = split_user_input(line)
226 res = pre + self.expand_aliases(fn, rest)
226 res = pre + self.expand_aliases(fn, rest)
227 return res
227 return res
228
228
229 def expand_aliases(self, fn, rest):
229 def expand_aliases(self, fn, rest):
230 """Expand multiple levels of aliases:
230 """Expand multiple levels of aliases:
231
231
232 if:
232 if:
233
233
234 alias foo bar /tmp
234 alias foo bar /tmp
235 alias baz foo
235 alias baz foo
236
236
237 then:
237 then:
238
238
239 baz huhhahhei -> bar /tmp huhhahhei
239 baz huhhahhei -> bar /tmp huhhahhei
240 """
240 """
241 line = fn + " " + rest
241 line = fn + " " + rest
242
242
243 done = set()
243 done = set()
244 while 1:
244 while 1:
245 pre,_,fn,rest = split_user_input(line, shell_line_split)
245 pre,_,fn,rest = split_user_input(line, shell_line_split)
246 if fn in self.alias_table:
246 if fn in self.alias_table:
247 if fn in done:
247 if fn in done:
248 warn("Cyclic alias definition, repeated '%s'" % fn)
248 warn("Cyclic alias definition, repeated '%s'" % fn)
249 return ""
249 return ""
250 done.add(fn)
250 done.add(fn)
251
251
252 l2 = self.transform_alias(fn, rest)
252 l2 = self.transform_alias(fn, rest)
253 if l2 == line:
253 if l2 == line:
254 break
254 break
255 # ls -> ls -F should not recurse forever
255 # ls -> ls -F should not recurse forever
256 if l2.split(None,1)[0] == line.split(None,1)[0]:
256 if l2.split(None,1)[0] == line.split(None,1)[0]:
257 line = l2
257 line = l2
258 break
258 break
259 line=l2
259 line=l2
260 else:
260 else:
261 break
261 break
262
262
263 return line
263 return line
@@ -1,317 +1,317 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 An application for IPython.
3 An application for IPython.
4
4
5 All top-level applications should use the classes in this module for
5 All top-level applications should use the classes in this module for
6 handling configuration and creating componenets.
6 handling configuration and creating componenets.
7
7
8 The job of an :class:`Application` is to create the master configuration
8 The job of an :class:`Application` is to create the master configuration
9 object and then create the configurable objects, passing the config to them.
9 object and then create the configurable objects, passing the config to them.
10
10
11 Authors:
11 Authors:
12
12
13 * Brian Granger
13 * Brian Granger
14 * Fernando Perez
14 * Fernando Perez
15 * Min RK
15 * Min RK
16
16
17 """
17 """
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Copyright (C) 2008-2011 The IPython Development Team
20 # Copyright (C) 2008-2011 The IPython Development Team
21 #
21 #
22 # Distributed under the terms of the BSD License. The full license is in
22 # Distributed under the terms of the BSD License. The full license is in
23 # the file COPYING, distributed as part of this software.
23 # the file COPYING, distributed as part of this software.
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Imports
27 # Imports
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 import glob
30 import glob
31 import logging
31 import logging
32 import os
32 import os
33 import shutil
33 import shutil
34 import sys
34 import sys
35
35
36 from IPython.config.application import Application
36 from IPython.config.application import Application
37 from IPython.config.configurable import Configurable
37 from IPython.config.configurable import Configurable
38 from IPython.config.loader import Config
38 from IPython.config.loader import Config
39 from IPython.core import release, crashhandler
39 from IPython.core import release, crashhandler
40 from IPython.core.profiledir import ProfileDir, ProfileDirError
40 from IPython.core.profiledir import ProfileDir, ProfileDirError
41 from IPython.utils.path import get_ipython_dir, get_ipython_package_dir
41 from IPython.utils.path import get_ipython_dir, get_ipython_package_dir
42 from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict
42 from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict
43 from IPython.utils import py3compat
43 from IPython.utils import py3compat
44
44
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46 # Classes and functions
46 # Classes and functions
47 #-----------------------------------------------------------------------------
47 #-----------------------------------------------------------------------------
48
48
49
49
50 #-----------------------------------------------------------------------------
50 #-----------------------------------------------------------------------------
51 # Base Application Class
51 # Base Application Class
52 #-----------------------------------------------------------------------------
52 #-----------------------------------------------------------------------------
53
53
54 # aliases and flags
54 # aliases and flags
55
55
56 base_aliases = {
56 base_aliases = {
57 'profile' : 'BaseIPythonApplication.profile',
57 'profile' : 'BaseIPythonApplication.profile',
58 'ipython-dir' : 'BaseIPythonApplication.ipython_dir',
58 'ipython-dir' : 'BaseIPythonApplication.ipython_dir',
59 'log-level' : 'Application.log_level',
59 'log-level' : 'Application.log_level',
60 }
60 }
61
61
62 base_flags = dict(
62 base_flags = dict(
63 debug = ({'Application' : {'log_level' : logging.DEBUG}},
63 debug = ({'Application' : {'log_level' : logging.DEBUG}},
64 "set log level to logging.DEBUG (maximize logging output)"),
64 "set log level to logging.DEBUG (maximize logging output)"),
65 quiet = ({'Application' : {'log_level' : logging.CRITICAL}},
65 quiet = ({'Application' : {'log_level' : logging.CRITICAL}},
66 "set log level to logging.CRITICAL (minimize logging output)"),
66 "set log level to logging.CRITICAL (minimize logging output)"),
67 init = ({'BaseIPythonApplication' : {
67 init = ({'BaseIPythonApplication' : {
68 'copy_config_files' : True,
68 'copy_config_files' : True,
69 'auto_create' : True}
69 'auto_create' : True}
70 }, """Initialize profile with default config files. This is equivalent
70 }, """Initialize profile with default config files. This is equivalent
71 to running `ipython profile create <profile>` prior to startup.
71 to running `ipython profile create <profile>` prior to startup.
72 """)
72 """)
73 )
73 )
74
74
75
75
76 class BaseIPythonApplication(Application):
76 class BaseIPythonApplication(Application):
77
77
78 name = Unicode(u'ipython')
78 name = Unicode(u'ipython')
79 description = Unicode(u'IPython: an enhanced interactive Python shell.')
79 description = Unicode(u'IPython: an enhanced interactive Python shell.')
80 version = Unicode(release.version)
80 version = Unicode(release.version)
81
81
82 aliases = Dict(base_aliases)
82 aliases = Dict(base_aliases)
83 flags = Dict(base_flags)
83 flags = Dict(base_flags)
84 classes = List([ProfileDir])
84 classes = List([ProfileDir])
85
85
86 # Track whether the config_file has changed,
86 # Track whether the config_file has changed,
87 # because some logic happens only if we aren't using the default.
87 # because some logic happens only if we aren't using the default.
88 config_file_specified = Bool(False)
88 config_file_specified = Bool(False)
89
89
90 config_file_name = Unicode(u'ipython_config.py')
90 config_file_name = Unicode(u'ipython_config.py')
91 def _config_file_name_default(self):
91 def _config_file_name_default(self):
92 return self.name.replace('-','_') + u'_config.py'
92 return self.name.replace('-','_') + u'_config.py'
93 def _config_file_name_changed(self, name, old, new):
93 def _config_file_name_changed(self, name, old, new):
94 if new != old:
94 if new != old:
95 self.config_file_specified = True
95 self.config_file_specified = True
96
96
97 # The directory that contains IPython's builtin profiles.
97 # The directory that contains IPython's builtin profiles.
98 builtin_profile_dir = Unicode(
98 builtin_profile_dir = Unicode(
99 os.path.join(get_ipython_package_dir(), u'config', u'profile', u'default')
99 os.path.join(get_ipython_package_dir(), u'config', u'profile', u'default')
100 )
100 )
101
101
102 config_file_paths = List(Unicode)
102 config_file_paths = List(Unicode)
103 def _config_file_paths_default(self):
103 def _config_file_paths_default(self):
104 return [os.getcwdu()]
104 return [os.getcwdu()]
105
105
106 profile = Unicode(u'', config=True,
106 profile = Unicode(u'', config=True,
107 help="""The IPython profile to use."""
107 help="""The IPython profile to use."""
108 )
108 )
109 def _profile_default(self):
109 def _profile_default(self):
110 return "python3" if py3compat.PY3 else "default"
110 return "python3" if py3compat.PY3 else "default"
111
111
112 def _profile_changed(self, name, old, new):
112 def _profile_changed(self, name, old, new):
113 self.builtin_profile_dir = os.path.join(
113 self.builtin_profile_dir = os.path.join(
114 get_ipython_package_dir(), u'config', u'profile', new
114 get_ipython_package_dir(), u'config', u'profile', new
115 )
115 )
116
116
117 ipython_dir = Unicode(get_ipython_dir(), config=True,
117 ipython_dir = Unicode(get_ipython_dir(), config=True,
118 help="""
118 help="""
119 The name of the IPython directory. This directory is used for logging
119 The name of the IPython directory. This directory is used for logging
120 configuration (through profiles), history storage, etc. The default
120 configuration (through profiles), history storage, etc. The default
121 is usually $HOME/.ipython. This options can also be specified through
121 is usually $HOME/.ipython. This options can also be specified through
122 the environment variable IPYTHON_DIR.
122 the environment variable IPYTHON_DIR.
123 """
123 """
124 )
124 )
125
125
126 overwrite = Bool(False, config=True,
126 overwrite = Bool(False, config=True,
127 help="""Whether to overwrite existing config files when copying""")
127 help="""Whether to overwrite existing config files when copying""")
128 auto_create = Bool(False, config=True,
128 auto_create = Bool(False, config=True,
129 help="""Whether to create profile dir if it doesn't exist""")
129 help="""Whether to create profile dir if it doesn't exist""")
130
130
131 config_files = List(Unicode)
131 config_files = List(Unicode)
132 def _config_files_default(self):
132 def _config_files_default(self):
133 return [u'ipython_config.py']
133 return [u'ipython_config.py']
134
134
135 copy_config_files = Bool(False, config=True,
135 copy_config_files = Bool(False, config=True,
136 help="""Whether to install the default config files into the profile dir.
136 help="""Whether to install the default config files into the profile dir.
137 If a new profile is being created, and IPython contains config files for that
137 If a new profile is being created, and IPython contains config files for that
138 profile, then they will be staged into the new directory. Otherwise,
138 profile, then they will be staged into the new directory. Otherwise,
139 default config files will be automatically generated.
139 default config files will be automatically generated.
140 """)
140 """)
141
141
142 # The class to use as the crash handler.
142 # The class to use as the crash handler.
143 crash_handler_class = Type(crashhandler.CrashHandler)
143 crash_handler_class = Type(crashhandler.CrashHandler)
144
144
145 def __init__(self, **kwargs):
145 def __init__(self, **kwargs):
146 super(BaseIPythonApplication, self).__init__(**kwargs)
146 super(BaseIPythonApplication, self).__init__(**kwargs)
147 # ensure even default IPYTHON_DIR exists
147 # ensure even default IPYTHON_DIR exists
148 if not os.path.exists(self.ipython_dir):
148 if not os.path.exists(self.ipython_dir):
149 self._ipython_dir_changed('ipython_dir', self.ipython_dir, self.ipython_dir)
149 self._ipython_dir_changed('ipython_dir', self.ipython_dir, self.ipython_dir)
150
150
151 #-------------------------------------------------------------------------
151 #-------------------------------------------------------------------------
152 # Various stages of Application creation
152 # Various stages of Application creation
153 #-------------------------------------------------------------------------
153 #-------------------------------------------------------------------------
154
154
155 def init_crash_handler(self):
155 def init_crash_handler(self):
156 """Create a crash handler, typically setting sys.excepthook to it."""
156 """Create a crash handler, typically setting sys.excepthook to it."""
157 self.crash_handler = self.crash_handler_class(self)
157 self.crash_handler = self.crash_handler_class(self)
158 sys.excepthook = self.crash_handler
158 sys.excepthook = self.crash_handler
159
159
160 def _ipython_dir_changed(self, name, old, new):
160 def _ipython_dir_changed(self, name, old, new):
161 if old in sys.path:
161 if old in sys.path:
162 sys.path.remove(old)
162 sys.path.remove(old)
163 sys.path.append(os.path.abspath(new))
163 sys.path.append(os.path.abspath(new))
164 if not os.path.isdir(new):
164 if not os.path.isdir(new):
165 os.makedirs(new, mode=0777)
165 os.makedirs(new, mode=0777)
166 readme = os.path.join(new, 'README')
166 readme = os.path.join(new, 'README')
167 if not os.path.exists(readme):
167 if not os.path.exists(readme):
168 path = os.path.join(get_ipython_package_dir(), u'config', u'profile')
168 path = os.path.join(get_ipython_package_dir(), u'config', u'profile')
169 shutil.copy(os.path.join(path, 'README'), readme)
169 shutil.copy(os.path.join(path, 'README'), readme)
170 self.log.debug("IPYTHON_DIR set to: %s" % new)
170 self.log.debug("IPYTHON_DIR set to: %s" % new)
171
171
172 def load_config_file(self, suppress_errors=True):
172 def load_config_file(self, suppress_errors=True):
173 """Load the config file.
173 """Load the config file.
174
174
175 By default, errors in loading config are handled, and a warning
175 By default, errors in loading config are handled, and a warning
176 printed on screen. For testing, the suppress_errors option is set
176 printed on screen. For testing, the suppress_errors option is set
177 to False, so errors will make tests fail.
177 to False, so errors will make tests fail.
178 """
178 """
179 self.log.debug("Searching path %s for config files", self.config_file_paths)
179 self.log.debug("Searching path %s for config files", self.config_file_paths)
180 base_config = 'ipython_config.py'
180 base_config = 'ipython_config.py'
181 self.log.debug("Attempting to load config file: %s" %
181 self.log.debug("Attempting to load config file: %s" %
182 base_config)
182 base_config)
183 try:
183 try:
184 Application.load_config_file(
184 Application.load_config_file(
185 self,
185 self,
186 base_config,
186 base_config,
187 path=self.config_file_paths
187 path=self.config_file_paths
188 )
188 )
189 except IOError:
189 except IOError:
190 # ignore errors loading parent
190 # ignore errors loading parent
191 self.log.debug("Config file %s not found", base_config)
191 self.log.debug("Config file %s not found", base_config)
192 pass
192 pass
193 if self.config_file_name == base_config:
193 if self.config_file_name == base_config:
194 # don't load secondary config
194 # don't load secondary config
195 return
195 return
196 self.log.debug("Attempting to load config file: %s" %
196 self.log.debug("Attempting to load config file: %s" %
197 self.config_file_name)
197 self.config_file_name)
198 try:
198 try:
199 Application.load_config_file(
199 Application.load_config_file(
200 self,
200 self,
201 self.config_file_name,
201 self.config_file_name,
202 path=self.config_file_paths
202 path=self.config_file_paths
203 )
203 )
204 except IOError:
204 except IOError:
205 # Only warn if the default config file was NOT being used.
205 # Only warn if the default config file was NOT being used.
206 if self.config_file_specified:
206 if self.config_file_specified:
207 msg = self.log.warn
207 msg = self.log.warn
208 else:
208 else:
209 msg = self.log.debug
209 msg = self.log.debug
210 msg("Config file not found, skipping: %s", self.config_file_name)
210 msg("Config file not found, skipping: %s", self.config_file_name)
211 except:
211 except:
212 # For testing purposes.
212 # For testing purposes.
213 if not suppress_errors:
213 if not suppress_errors:
214 raise
214 raise
215 self.log.warn("Error loading config file: %s" %
215 self.log.warn("Error loading config file: %s" %
216 self.config_file_name, exc_info=True)
216 self.config_file_name, exc_info=True)
217
217
218 def init_profile_dir(self):
218 def init_profile_dir(self):
219 """initialize the profile dir"""
219 """initialize the profile dir"""
220 try:
220 try:
221 # location explicitly specified:
221 # location explicitly specified:
222 location = self.config.ProfileDir.location
222 location = self.config.ProfileDir.location
223 except AttributeError:
223 except AttributeError:
224 # location not specified, find by profile name
224 # location not specified, find by profile name
225 try:
225 try:
226 p = ProfileDir.find_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
226 p = ProfileDir.find_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
227 except ProfileDirError:
227 except ProfileDirError:
228 # not found, maybe create it (always create default profile)
228 # not found, maybe create it (always create default profile)
229 if self.auto_create or self.profile==self._profile_default():
229 if self.auto_create or self.profile==self._profile_default():
230 try:
230 try:
231 p = ProfileDir.create_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
231 p = ProfileDir.create_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
232 except ProfileDirError:
232 except ProfileDirError:
233 self.log.fatal("Could not create profile: %r"%self.profile)
233 self.log.fatal("Could not create profile: %r"%self.profile)
234 self.exit(1)
234 self.exit(1)
235 else:
235 else:
236 self.log.info("Created profile dir: %r"%p.location)
236 self.log.info("Created profile dir: %r"%p.location)
237 else:
237 else:
238 self.log.fatal("Profile %r not found."%self.profile)
238 self.log.fatal("Profile %r not found."%self.profile)
239 self.exit(1)
239 self.exit(1)
240 else:
240 else:
241 self.log.info("Using existing profile dir: %r"%p.location)
241 self.log.info("Using existing profile dir: %r"%p.location)
242 else:
242 else:
243 # location is fully specified
243 # location is fully specified
244 try:
244 try:
245 p = ProfileDir.find_profile_dir(location, self.config)
245 p = ProfileDir.find_profile_dir(location, self.config)
246 except ProfileDirError:
246 except ProfileDirError:
247 # not found, maybe create it
247 # not found, maybe create it
248 if self.auto_create:
248 if self.auto_create:
249 try:
249 try:
250 p = ProfileDir.create_profile_dir(location, self.config)
250 p = ProfileDir.create_profile_dir(location, self.config)
251 except ProfileDirError:
251 except ProfileDirError:
252 self.log.fatal("Could not create profile directory: %r"%location)
252 self.log.fatal("Could not create profile directory: %r"%location)
253 self.exit(1)
253 self.exit(1)
254 else:
254 else:
255 self.log.info("Creating new profile dir: %r"%location)
255 self.log.info("Creating new profile dir: %r"%location)
256 else:
256 else:
257 self.log.fatal("Profile directory %r not found."%location)
257 self.log.fatal("Profile directory %r not found."%location)
258 self.exit(1)
258 self.exit(1)
259 else:
259 else:
260 self.log.info("Using existing profile dir: %r"%location)
260 self.log.info("Using existing profile dir: %r"%location)
261
261
262 self.profile_dir = p
262 self.profile_dir = p
263 self.config_file_paths.append(p.location)
263 self.config_file_paths.append(p.location)
264
264
265 def init_config_files(self):
265 def init_config_files(self):
266 """[optionally] copy default config files into profile dir."""
266 """[optionally] copy default config files into profile dir."""
267 # copy config files
267 # copy config files
268 path = self.builtin_profile_dir
268 path = self.builtin_profile_dir
269 if self.copy_config_files:
269 if self.copy_config_files:
270 src = self.profile
270 src = self.profile
271
271
272 cfg = self.config_file_name
272 cfg = self.config_file_name
273 if path and os.path.exists(os.path.join(path, cfg)):
273 if path and os.path.exists(os.path.join(path, cfg)):
274 self.log.warn("Staging %r from %s into %r [overwrite=%s]"%(
274 self.log.warn("Staging %r from %s into %r [overwrite=%s]"%(
275 cfg, src, self.profile_dir.location, self.overwrite)
275 cfg, src, self.profile_dir.location, self.overwrite)
276 )
276 )
277 self.profile_dir.copy_config_file(cfg, path=path, overwrite=self.overwrite)
277 self.profile_dir.copy_config_file(cfg, path=path, overwrite=self.overwrite)
278 else:
278 else:
279 self.stage_default_config_file()
279 self.stage_default_config_file()
280 else:
280 else:
281 # Still stage *bundled* config files, but not generated ones
281 # Still stage *bundled* config files, but not generated ones
282 # This is necessary for `ipython profile=sympy` to load the profile
282 # This is necessary for `ipython profile=sympy` to load the profile
283 # on the first go
283 # on the first go
284 files = glob.glob(os.path.join(path, '*.py'))
284 files = glob.glob(os.path.join(path, '*.py'))
285 for fullpath in files:
285 for fullpath in files:
286 cfg = os.path.basename(fullpath)
286 cfg = os.path.basename(fullpath)
287 if self.profile_dir.copy_config_file(cfg, path=path, overwrite=False):
287 if self.profile_dir.copy_config_file(cfg, path=path, overwrite=False):
288 # file was copied
288 # file was copied
289 self.log.warn("Staging bundled %s from %s into %r"%(
289 self.log.warn("Staging bundled %s from %s into %r"%(
290 cfg, self.profile, self.profile_dir.location)
290 cfg, self.profile, self.profile_dir.location)
291 )
291 )
292
292
293
293
294 def stage_default_config_file(self):
294 def stage_default_config_file(self):
295 """auto generate default config file, and stage it into the profile."""
295 """auto generate default config file, and stage it into the profile."""
296 s = self.generate_config_file()
296 s = self.generate_config_file()
297 fname = os.path.join(self.profile_dir.location, self.config_file_name)
297 fname = os.path.join(self.profile_dir.location, self.config_file_name)
298 if self.overwrite or not os.path.exists(fname):
298 if self.overwrite or not os.path.exists(fname):
299 self.log.warn("Generating default config file: %r"%(fname))
299 self.log.warn("Generating default config file: %r"%(fname))
300 with open(fname, 'w') as f:
300 with open(fname, 'w') as f:
301 f.write(s)
301 f.write(s)
302
302
303
303
304 def initialize(self, argv=None):
304 def initialize(self, argv=None):
305 # don't hook up crash handler before parsing command-line
305 # don't hook up crash handler before parsing command-line
306 self.parse_command_line(argv)
306 self.parse_command_line(argv)
307 self.init_crash_handler()
307 self.init_crash_handler()
308 if self.subapp is not None:
308 if self.subapp is not None:
309 # stop here if subapp is taking over
309 # stop here if subapp is taking over
310 return
310 return
311 cl_config = self.config
311 cl_config = self.config
312 self.init_profile_dir()
312 self.init_profile_dir()
313 self.init_config_files()
313 self.init_config_files()
314 self.load_config_file()
314 self.load_config_file()
315 # enforce cl-opts override configfile opts:
315 # enforce cl-opts override configfile opts:
316 self.update_config(cl_config)
316 self.update_config(cl_config)
317
317
@@ -1,903 +1,903 b''
1 """Word completion for IPython.
1 """Word completion for IPython.
2
2
3 This module is a fork of the rlcompleter module in the Python standard
3 This module is a fork of the rlcompleter module in the Python standard
4 library. The original enhancements made to rlcompleter have been sent
4 library. The original enhancements made to rlcompleter have been sent
5 upstream and were accepted as of Python 2.3, but we need a lot more
5 upstream and were accepted as of Python 2.3, but we need a lot more
6 functionality specific to IPython, so this module will continue to live as an
6 functionality specific to IPython, so this module will continue to live as an
7 IPython-specific utility.
7 IPython-specific utility.
8
8
9 Original rlcompleter documentation:
9 Original rlcompleter documentation:
10
10
11 This requires the latest extension to the readline module (the
11 This requires the latest extension to the readline module (the
12 completes keywords, built-ins and globals in __main__; when completing
12 completes keywords, built-ins and globals in __main__; when completing
13 NAME.NAME..., it evaluates (!) the expression up to the last dot and
13 NAME.NAME..., it evaluates (!) the expression up to the last dot and
14 completes its attributes.
14 completes its attributes.
15
15
16 It's very cool to do "import string" type "string.", hit the
16 It's very cool to do "import string" type "string.", hit the
17 completion key (twice), and see the list of names defined by the
17 completion key (twice), and see the list of names defined by the
18 string module!
18 string module!
19
19
20 Tip: to use the tab key as the completion key, call
20 Tip: to use the tab key as the completion key, call
21
21
22 readline.parse_and_bind("tab: complete")
22 readline.parse_and_bind("tab: complete")
23
23
24 Notes:
24 Notes:
25
25
26 - Exceptions raised by the completer function are *ignored* (and
26 - Exceptions raised by the completer function are *ignored* (and
27 generally cause the completion to fail). This is a feature -- since
27 generally cause the completion to fail). This is a feature -- since
28 readline sets the tty device in raw (or cbreak) mode, printing a
28 readline sets the tty device in raw (or cbreak) mode, printing a
29 traceback wouldn't work well without some complicated hoopla to save,
29 traceback wouldn't work well without some complicated hoopla to save,
30 reset and restore the tty state.
30 reset and restore the tty state.
31
31
32 - The evaluation of the NAME.NAME... form may cause arbitrary
32 - The evaluation of the NAME.NAME... form may cause arbitrary
33 application defined code to be executed if an object with a
33 application defined code to be executed if an object with a
34 __getattr__ hook is found. Since it is the responsibility of the
34 __getattr__ hook is found. Since it is the responsibility of the
35 application (or the user) to enable this feature, I consider this an
35 application (or the user) to enable this feature, I consider this an
36 acceptable risk. More complicated expressions (e.g. function calls or
36 acceptable risk. More complicated expressions (e.g. function calls or
37 indexing operations) are *not* evaluated.
37 indexing operations) are *not* evaluated.
38
38
39 - GNU readline is also used by the built-in functions input() and
39 - GNU readline is also used by the built-in functions input() and
40 raw_input(), and thus these also benefit/suffer from the completer
40 raw_input(), and thus these also benefit/suffer from the completer
41 features. Clearly an interactive application can benefit by
41 features. Clearly an interactive application can benefit by
42 specifying its own completer function and using raw_input() for all
42 specifying its own completer function and using raw_input() for all
43 its input.
43 its input.
44
44
45 - When the original stdin is not a tty device, GNU readline is never
45 - When the original stdin is not a tty device, GNU readline is never
46 used, and this module (and the readline module) are silently inactive.
46 used, and this module (and the readline module) are silently inactive.
47 """
47 """
48
48
49 #*****************************************************************************
49 #*****************************************************************************
50 #
50 #
51 # Since this file is essentially a minimally modified copy of the rlcompleter
51 # Since this file is essentially a minimally modified copy of the rlcompleter
52 # module which is part of the standard Python distribution, I assume that the
52 # module which is part of the standard Python distribution, I assume that the
53 # proper procedure is to maintain its copyright as belonging to the Python
53 # proper procedure is to maintain its copyright as belonging to the Python
54 # Software Foundation (in addition to my own, for all new code).
54 # Software Foundation (in addition to my own, for all new code).
55 #
55 #
56 # Copyright (C) 2008-2010 IPython Development Team
56 # Copyright (C) 2008-2010 IPython Development Team
57 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
57 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
59 #
59 #
60 # Distributed under the terms of the BSD License. The full license is in
60 # Distributed under the terms of the BSD License. The full license is in
61 # the file COPYING, distributed as part of this software.
61 # the file COPYING, distributed as part of this software.
62 #
62 #
63 #*****************************************************************************
63 #*****************************************************************************
64 from __future__ import print_function
64 from __future__ import print_function
65
65
66 #-----------------------------------------------------------------------------
66 #-----------------------------------------------------------------------------
67 # Imports
67 # Imports
68 #-----------------------------------------------------------------------------
68 #-----------------------------------------------------------------------------
69
69
70 import __builtin__
70 import __builtin__
71 import __main__
71 import __main__
72 import glob
72 import glob
73 import inspect
73 import inspect
74 import itertools
74 import itertools
75 import keyword
75 import keyword
76 import os
76 import os
77 import re
77 import re
78 import shlex
78 import shlex
79 import sys
79 import sys
80
80
81 from IPython.config.configurable import Configurable
81 from IPython.config.configurable import Configurable
82 from IPython.core.error import TryNext
82 from IPython.core.error import TryNext
83 from IPython.core.prefilter import ESC_MAGIC
83 from IPython.core.prefilter import ESC_MAGIC
84 from IPython.utils import generics
84 from IPython.utils import generics
85 from IPython.utils import io
85 from IPython.utils import io
86 from IPython.utils.dir2 import dir2
86 from IPython.utils.dir2 import dir2
87 from IPython.utils.process import arg_split
87 from IPython.utils.process import arg_split
88 from IPython.utils.traitlets import CBool
88 from IPython.utils.traitlets import CBool
89
89
90 #-----------------------------------------------------------------------------
90 #-----------------------------------------------------------------------------
91 # Globals
91 # Globals
92 #-----------------------------------------------------------------------------
92 #-----------------------------------------------------------------------------
93
93
94 # Public API
94 # Public API
95 __all__ = ['Completer','IPCompleter']
95 __all__ = ['Completer','IPCompleter']
96
96
97 if sys.platform == 'win32':
97 if sys.platform == 'win32':
98 PROTECTABLES = ' '
98 PROTECTABLES = ' '
99 else:
99 else:
100 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
100 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
101
101
102 #-----------------------------------------------------------------------------
102 #-----------------------------------------------------------------------------
103 # Main functions and classes
103 # Main functions and classes
104 #-----------------------------------------------------------------------------
104 #-----------------------------------------------------------------------------
105
105
106 def has_open_quotes(s):
106 def has_open_quotes(s):
107 """Return whether a string has open quotes.
107 """Return whether a string has open quotes.
108
108
109 This simply counts whether the number of quote characters of either type in
109 This simply counts whether the number of quote characters of either type in
110 the string is odd.
110 the string is odd.
111
111
112 Returns
112 Returns
113 -------
113 -------
114 If there is an open quote, the quote character is returned. Else, return
114 If there is an open quote, the quote character is returned. Else, return
115 False.
115 False.
116 """
116 """
117 # We check " first, then ', so complex cases with nested quotes will get
117 # We check " first, then ', so complex cases with nested quotes will get
118 # the " to take precedence.
118 # the " to take precedence.
119 if s.count('"') % 2:
119 if s.count('"') % 2:
120 return '"'
120 return '"'
121 elif s.count("'") % 2:
121 elif s.count("'") % 2:
122 return "'"
122 return "'"
123 else:
123 else:
124 return False
124 return False
125
125
126
126
127 def protect_filename(s):
127 def protect_filename(s):
128 """Escape a string to protect certain characters."""
128 """Escape a string to protect certain characters."""
129
129
130 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
130 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
131 for ch in s])
131 for ch in s])
132
132
133
133
134 def mark_dirs(matches):
134 def mark_dirs(matches):
135 """Mark directories in input list by appending '/' to their names."""
135 """Mark directories in input list by appending '/' to their names."""
136 out = []
136 out = []
137 isdir = os.path.isdir
137 isdir = os.path.isdir
138 for x in matches:
138 for x in matches:
139 if isdir(x):
139 if isdir(x):
140 out.append(x+'/')
140 out.append(x+'/')
141 else:
141 else:
142 out.append(x)
142 out.append(x)
143 return out
143 return out
144
144
145
145
146 def expand_user(path):
146 def expand_user(path):
147 """Expand '~'-style usernames in strings.
147 """Expand '~'-style usernames in strings.
148
148
149 This is similar to :func:`os.path.expanduser`, but it computes and returns
149 This is similar to :func:`os.path.expanduser`, but it computes and returns
150 extra information that will be useful if the input was being used in
150 extra information that will be useful if the input was being used in
151 computing completions, and you wish to return the completions with the
151 computing completions, and you wish to return the completions with the
152 original '~' instead of its expanded value.
152 original '~' instead of its expanded value.
153
153
154 Parameters
154 Parameters
155 ----------
155 ----------
156 path : str
156 path : str
157 String to be expanded. If no ~ is present, the output is the same as the
157 String to be expanded. If no ~ is present, the output is the same as the
158 input.
158 input.
159
159
160 Returns
160 Returns
161 -------
161 -------
162 newpath : str
162 newpath : str
163 Result of ~ expansion in the input path.
163 Result of ~ expansion in the input path.
164 tilde_expand : bool
164 tilde_expand : bool
165 Whether any expansion was performed or not.
165 Whether any expansion was performed or not.
166 tilde_val : str
166 tilde_val : str
167 The value that ~ was replaced with.
167 The value that ~ was replaced with.
168 """
168 """
169 # Default values
169 # Default values
170 tilde_expand = False
170 tilde_expand = False
171 tilde_val = ''
171 tilde_val = ''
172 newpath = path
172 newpath = path
173
173
174 if path.startswith('~'):
174 if path.startswith('~'):
175 tilde_expand = True
175 tilde_expand = True
176 rest = path[1:]
176 rest = path[1:]
177 newpath = os.path.expanduser(path)
177 newpath = os.path.expanduser(path)
178 tilde_val = newpath.replace(rest, '')
178 tilde_val = newpath.replace(rest, '')
179
179
180 return newpath, tilde_expand, tilde_val
180 return newpath, tilde_expand, tilde_val
181
181
182
182
183 def compress_user(path, tilde_expand, tilde_val):
183 def compress_user(path, tilde_expand, tilde_val):
184 """Does the opposite of expand_user, with its outputs.
184 """Does the opposite of expand_user, with its outputs.
185 """
185 """
186 if tilde_expand:
186 if tilde_expand:
187 return path.replace(tilde_val, '~')
187 return path.replace(tilde_val, '~')
188 else:
188 else:
189 return path
189 return path
190
190
191
191
192 def single_dir_expand(matches):
192 def single_dir_expand(matches):
193 "Recursively expand match lists containing a single dir."
193 "Recursively expand match lists containing a single dir."
194
194
195 if len(matches) == 1 and os.path.isdir(matches[0]):
195 if len(matches) == 1 and os.path.isdir(matches[0]):
196 # Takes care of links to directories also. Use '/'
196 # Takes care of links to directories also. Use '/'
197 # explicitly, even under Windows, so that name completions
197 # explicitly, even under Windows, so that name completions
198 # don't end up escaped.
198 # don't end up escaped.
199 d = matches[0]
199 d = matches[0]
200 if d[-1] in ['/','\\']:
200 if d[-1] in ['/','\\']:
201 d = d[:-1]
201 d = d[:-1]
202
202
203 subdirs = os.listdir(d)
203 subdirs = os.listdir(d)
204 if subdirs:
204 if subdirs:
205 matches = [ (d + '/' + p) for p in subdirs]
205 matches = [ (d + '/' + p) for p in subdirs]
206 return single_dir_expand(matches)
206 return single_dir_expand(matches)
207 else:
207 else:
208 return matches
208 return matches
209 else:
209 else:
210 return matches
210 return matches
211
211
212
212
213 class Bunch(object): pass
213 class Bunch(object): pass
214
214
215 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
215 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
216 GREEDY_DELIMS = ' \r\n'
216 GREEDY_DELIMS = ' \r\n'
217
217
218 class CompletionSplitter(object):
218 class CompletionSplitter(object):
219 """An object to split an input line in a manner similar to readline.
219 """An object to split an input line in a manner similar to readline.
220
220
221 By having our own implementation, we can expose readline-like completion in
221 By having our own implementation, we can expose readline-like completion in
222 a uniform manner to all frontends. This object only needs to be given the
222 a uniform manner to all frontends. This object only needs to be given the
223 line of text to be split and the cursor position on said line, and it
223 line of text to be split and the cursor position on said line, and it
224 returns the 'word' to be completed on at the cursor after splitting the
224 returns the 'word' to be completed on at the cursor after splitting the
225 entire line.
225 entire line.
226
226
227 What characters are used as splitting delimiters can be controlled by
227 What characters are used as splitting delimiters can be controlled by
228 setting the `delims` attribute (this is a property that internally
228 setting the `delims` attribute (this is a property that internally
229 automatically builds the necessary """
229 automatically builds the necessary """
230
230
231 # Private interface
231 # Private interface
232
232
233 # A string of delimiter characters. The default value makes sense for
233 # A string of delimiter characters. The default value makes sense for
234 # IPython's most typical usage patterns.
234 # IPython's most typical usage patterns.
235 _delims = DELIMS
235 _delims = DELIMS
236
236
237 # The expression (a normal string) to be compiled into a regular expression
237 # The expression (a normal string) to be compiled into a regular expression
238 # for actual splitting. We store it as an attribute mostly for ease of
238 # for actual splitting. We store it as an attribute mostly for ease of
239 # debugging, since this type of code can be so tricky to debug.
239 # debugging, since this type of code can be so tricky to debug.
240 _delim_expr = None
240 _delim_expr = None
241
241
242 # The regular expression that does the actual splitting
242 # The regular expression that does the actual splitting
243 _delim_re = None
243 _delim_re = None
244
244
245 def __init__(self, delims=None):
245 def __init__(self, delims=None):
246 delims = CompletionSplitter._delims if delims is None else delims
246 delims = CompletionSplitter._delims if delims is None else delims
247 self.set_delims(delims)
247 self.set_delims(delims)
248
248
249 def set_delims(self, delims):
249 def set_delims(self, delims):
250 """Set the delimiters for line splitting."""
250 """Set the delimiters for line splitting."""
251 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
251 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
252 self._delim_re = re.compile(expr)
252 self._delim_re = re.compile(expr)
253 self._delims = delims
253 self._delims = delims
254 self._delim_expr = expr
254 self._delim_expr = expr
255
255
256 def get_delims(self):
256 def get_delims(self):
257 """Return the string of delimiter characters."""
257 """Return the string of delimiter characters."""
258 return self._delims
258 return self._delims
259
259
260 def split_line(self, line, cursor_pos=None):
260 def split_line(self, line, cursor_pos=None):
261 """Split a line of text with a cursor at the given position.
261 """Split a line of text with a cursor at the given position.
262 """
262 """
263 l = line if cursor_pos is None else line[:cursor_pos]
263 l = line if cursor_pos is None else line[:cursor_pos]
264 return self._delim_re.split(l)[-1]
264 return self._delim_re.split(l)[-1]
265
265
266
266
267 class Completer(Configurable):
267 class Completer(Configurable):
268
268
269 greedy = CBool(False, config=True,
269 greedy = CBool(False, config=True,
270 help="""Activate greedy completion
270 help="""Activate greedy completion
271
271
272 This will enable completion on elements of lists, results of function calls, etc.,
272 This will enable completion on elements of lists, results of function calls, etc.,
273 but can be unsafe because the code is actually evaluated on TAB.
273 but can be unsafe because the code is actually evaluated on TAB.
274 """
274 """
275 )
275 )
276
276
277 def __init__(self, namespace=None, global_namespace=None, config=None):
277 def __init__(self, namespace=None, global_namespace=None, config=None):
278 """Create a new completer for the command line.
278 """Create a new completer for the command line.
279
279
280 Completer(namespace=ns,global_namespace=ns2) -> completer instance.
280 Completer(namespace=ns,global_namespace=ns2) -> completer instance.
281
281
282 If unspecified, the default namespace where completions are performed
282 If unspecified, the default namespace where completions are performed
283 is __main__ (technically, __main__.__dict__). Namespaces should be
283 is __main__ (technically, __main__.__dict__). Namespaces should be
284 given as dictionaries.
284 given as dictionaries.
285
285
286 An optional second namespace can be given. This allows the completer
286 An optional second namespace can be given. This allows the completer
287 to handle cases where both the local and global scopes need to be
287 to handle cases where both the local and global scopes need to be
288 distinguished.
288 distinguished.
289
289
290 Completer instances should be used as the completion mechanism of
290 Completer instances should be used as the completion mechanism of
291 readline via the set_completer() call:
291 readline via the set_completer() call:
292
292
293 readline.set_completer(Completer(my_namespace).complete)
293 readline.set_completer(Completer(my_namespace).complete)
294 """
294 """
295
295
296 # Don't bind to namespace quite yet, but flag whether the user wants a
296 # Don't bind to namespace quite yet, but flag whether the user wants a
297 # specific namespace or to use __main__.__dict__. This will allow us
297 # specific namespace or to use __main__.__dict__. This will allow us
298 # to bind to __main__.__dict__ at completion time, not now.
298 # to bind to __main__.__dict__ at completion time, not now.
299 if namespace is None:
299 if namespace is None:
300 self.use_main_ns = 1
300 self.use_main_ns = 1
301 else:
301 else:
302 self.use_main_ns = 0
302 self.use_main_ns = 0
303 self.namespace = namespace
303 self.namespace = namespace
304
304
305 # The global namespace, if given, can be bound directly
305 # The global namespace, if given, can be bound directly
306 if global_namespace is None:
306 if global_namespace is None:
307 self.global_namespace = {}
307 self.global_namespace = {}
308 else:
308 else:
309 self.global_namespace = global_namespace
309 self.global_namespace = global_namespace
310
310
311 super(Completer, self).__init__(config=config)
311 super(Completer, self).__init__(config=config)
312
312
313 def complete(self, text, state):
313 def complete(self, text, state):
314 """Return the next possible completion for 'text'.
314 """Return the next possible completion for 'text'.
315
315
316 This is called successively with state == 0, 1, 2, ... until it
316 This is called successively with state == 0, 1, 2, ... until it
317 returns None. The completion should begin with 'text'.
317 returns None. The completion should begin with 'text'.
318
318
319 """
319 """
320 if self.use_main_ns:
320 if self.use_main_ns:
321 self.namespace = __main__.__dict__
321 self.namespace = __main__.__dict__
322
322
323 if state == 0:
323 if state == 0:
324 if "." in text:
324 if "." in text:
325 self.matches = self.attr_matches(text)
325 self.matches = self.attr_matches(text)
326 else:
326 else:
327 self.matches = self.global_matches(text)
327 self.matches = self.global_matches(text)
328 try:
328 try:
329 return self.matches[state]
329 return self.matches[state]
330 except IndexError:
330 except IndexError:
331 return None
331 return None
332
332
333 def global_matches(self, text):
333 def global_matches(self, text):
334 """Compute matches when text is a simple name.
334 """Compute matches when text is a simple name.
335
335
336 Return a list of all keywords, built-in functions and names currently
336 Return a list of all keywords, built-in functions and names currently
337 defined in self.namespace or self.global_namespace that match.
337 defined in self.namespace or self.global_namespace that match.
338
338
339 """
339 """
340 #print 'Completer->global_matches, txt=%r' % text # dbg
340 #print 'Completer->global_matches, txt=%r' % text # dbg
341 matches = []
341 matches = []
342 match_append = matches.append
342 match_append = matches.append
343 n = len(text)
343 n = len(text)
344 for lst in [keyword.kwlist,
344 for lst in [keyword.kwlist,
345 __builtin__.__dict__.keys(),
345 __builtin__.__dict__.keys(),
346 self.namespace.keys(),
346 self.namespace.keys(),
347 self.global_namespace.keys()]:
347 self.global_namespace.keys()]:
348 for word in lst:
348 for word in lst:
349 if word[:n] == text and word != "__builtins__":
349 if word[:n] == text and word != "__builtins__":
350 match_append(word)
350 match_append(word)
351 return matches
351 return matches
352
352
353 def attr_matches(self, text):
353 def attr_matches(self, text):
354 """Compute matches when text contains a dot.
354 """Compute matches when text contains a dot.
355
355
356 Assuming the text is of the form NAME.NAME....[NAME], and is
356 Assuming the text is of the form NAME.NAME....[NAME], and is
357 evaluatable in self.namespace or self.global_namespace, it will be
357 evaluatable in self.namespace or self.global_namespace, it will be
358 evaluated and its attributes (as revealed by dir()) are used as
358 evaluated and its attributes (as revealed by dir()) are used as
359 possible completions. (For class instances, class members are are
359 possible completions. (For class instances, class members are are
360 also considered.)
360 also considered.)
361
361
362 WARNING: this can still invoke arbitrary C code, if an object
362 WARNING: this can still invoke arbitrary C code, if an object
363 with a __getattr__ hook is evaluated.
363 with a __getattr__ hook is evaluated.
364
364
365 """
365 """
366
366
367 #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
367 #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
368 # Another option, seems to work great. Catches things like ''.<tab>
368 # Another option, seems to work great. Catches things like ''.<tab>
369 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
369 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
370
370
371 if m:
371 if m:
372 expr, attr = m.group(1, 3)
372 expr, attr = m.group(1, 3)
373 elif self.greedy:
373 elif self.greedy:
374 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
374 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
375 if not m2:
375 if not m2:
376 return []
376 return []
377 expr, attr = m2.group(1,2)
377 expr, attr = m2.group(1,2)
378 else:
378 else:
379 return []
379 return []
380
380
381 try:
381 try:
382 obj = eval(expr, self.namespace)
382 obj = eval(expr, self.namespace)
383 except:
383 except:
384 try:
384 try:
385 obj = eval(expr, self.global_namespace)
385 obj = eval(expr, self.global_namespace)
386 except:
386 except:
387 return []
387 return []
388
388
389 words = dir2(obj)
389 words = dir2(obj)
390
390
391 try:
391 try:
392 words = generics.complete_object(obj, words)
392 words = generics.complete_object(obj, words)
393 except TryNext:
393 except TryNext:
394 pass
394 pass
395 # Build match list to return
395 # Build match list to return
396 n = len(attr)
396 n = len(attr)
397 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
397 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
398 return res
398 return res
399
399
400
400
401 class IPCompleter(Completer):
401 class IPCompleter(Completer):
402 """Extension of the completer class with IPython-specific features"""
402 """Extension of the completer class with IPython-specific features"""
403
403
404 def _greedy_changed(self, name, old, new):
404 def _greedy_changed(self, name, old, new):
405 """update the splitter and readline delims when greedy is changed"""
405 """update the splitter and readline delims when greedy is changed"""
406 if new:
406 if new:
407 self.splitter.set_delims(GREEDY_DELIMS)
407 self.splitter.set_delims(GREEDY_DELIMS)
408 else:
408 else:
409 self.splitter.set_delims(DELIMS)
409 self.splitter.set_delims(DELIMS)
410
410
411 if self.readline:
411 if self.readline:
412 self.readline.set_completer_delims(self.splitter.get_delims())
412 self.readline.set_completer_delims(self.splitter.get_delims())
413
413
414 def __init__(self, shell=None, namespace=None, global_namespace=None,
414 def __init__(self, shell=None, namespace=None, global_namespace=None,
415 omit__names=True, alias_table=None, use_readline=True,
415 omit__names=True, alias_table=None, use_readline=True,
416 config=None):
416 config=None):
417 """IPCompleter() -> completer
417 """IPCompleter() -> completer
418
418
419 Return a completer object suitable for use by the readline library
419 Return a completer object suitable for use by the readline library
420 via readline.set_completer().
420 via readline.set_completer().
421
421
422 Inputs:
422 Inputs:
423
423
424 - shell: a pointer to the ipython shell itself. This is needed
424 - shell: a pointer to the ipython shell itself. This is needed
425 because this completer knows about magic functions, and those can
425 because this completer knows about magic functions, and those can
426 only be accessed via the ipython instance.
426 only be accessed via the ipython instance.
427
427
428 - namespace: an optional dict where completions are performed.
428 - namespace: an optional dict where completions are performed.
429
429
430 - global_namespace: secondary optional dict for completions, to
430 - global_namespace: secondary optional dict for completions, to
431 handle cases (such as IPython embedded inside functions) where
431 handle cases (such as IPython embedded inside functions) where
432 both Python scopes are visible.
432 both Python scopes are visible.
433
433
434 - The optional omit__names parameter sets the completer to omit the
434 - The optional omit__names parameter sets the completer to omit the
435 'magic' names (__magicname__) for python objects unless the text
435 'magic' names (__magicname__) for python objects unless the text
436 to be completed explicitly starts with one or more underscores.
436 to be completed explicitly starts with one or more underscores.
437
437
438 - If alias_table is supplied, it should be a dictionary of aliases
438 - If alias_table is supplied, it should be a dictionary of aliases
439 to complete.
439 to complete.
440
440
441 use_readline : bool, optional
441 use_readline : bool, optional
442 If true, use the readline library. This completer can still function
442 If true, use the readline library. This completer can still function
443 without readline, though in that case callers must provide some extra
443 without readline, though in that case callers must provide some extra
444 information on each call about the current line."""
444 information on each call about the current line."""
445
445
446 self.magic_escape = ESC_MAGIC
446 self.magic_escape = ESC_MAGIC
447 self.splitter = CompletionSplitter()
447 self.splitter = CompletionSplitter()
448
448
449 # Readline configuration, only used by the rlcompleter method.
449 # Readline configuration, only used by the rlcompleter method.
450 if use_readline:
450 if use_readline:
451 # We store the right version of readline so that later code
451 # We store the right version of readline so that later code
452 import IPython.utils.rlineimpl as readline
452 import IPython.utils.rlineimpl as readline
453 self.readline = readline
453 self.readline = readline
454 else:
454 else:
455 self.readline = None
455 self.readline = None
456
456
457 # _greedy_changed() depends on splitter and readline being defined:
457 # _greedy_changed() depends on splitter and readline being defined:
458 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
458 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
459 config=config)
459 config=config)
460
460
461 # List where completion matches will be stored
461 # List where completion matches will be stored
462 self.matches = []
462 self.matches = []
463 self.omit__names = omit__names
463 self.omit__names = omit__names
464 self.merge_completions = shell.readline_merge_completions
464 self.merge_completions = shell.readline_merge_completions
465 self.shell = shell.shell
465 self.shell = shell.shell
466 if alias_table is None:
466 if alias_table is None:
467 alias_table = {}
467 alias_table = {}
468 self.alias_table = alias_table
468 self.alias_table = alias_table
469 # Regexp to split filenames with spaces in them
469 # Regexp to split filenames with spaces in them
470 self.space_name_re = re.compile(r'([^\\] )')
470 self.space_name_re = re.compile(r'([^\\] )')
471 # Hold a local ref. to glob.glob for speed
471 # Hold a local ref. to glob.glob for speed
472 self.glob = glob.glob
472 self.glob = glob.glob
473
473
474 # Determine if we are running on 'dumb' terminals, like (X)Emacs
474 # Determine if we are running on 'dumb' terminals, like (X)Emacs
475 # buffers, to avoid completion problems.
475 # buffers, to avoid completion problems.
476 term = os.environ.get('TERM','xterm')
476 term = os.environ.get('TERM','xterm')
477 self.dumb_terminal = term in ['dumb','emacs']
477 self.dumb_terminal = term in ['dumb','emacs']
478
478
479 # Special handling of backslashes needed in win32 platforms
479 # Special handling of backslashes needed in win32 platforms
480 if sys.platform == "win32":
480 if sys.platform == "win32":
481 self.clean_glob = self._clean_glob_win32
481 self.clean_glob = self._clean_glob_win32
482 else:
482 else:
483 self.clean_glob = self._clean_glob
483 self.clean_glob = self._clean_glob
484
484
485 # All active matcher routines for completion
485 # All active matcher routines for completion
486 self.matchers = [self.python_matches,
486 self.matchers = [self.python_matches,
487 self.file_matches,
487 self.file_matches,
488 self.magic_matches,
488 self.magic_matches,
489 self.alias_matches,
489 self.alias_matches,
490 self.python_func_kw_matches,
490 self.python_func_kw_matches,
491 ]
491 ]
492
492
493 def all_completions(self, text):
493 def all_completions(self, text):
494 """
494 """
495 Wrapper around the complete method for the benefit of emacs
495 Wrapper around the complete method for the benefit of emacs
496 and pydb.
496 and pydb.
497 """
497 """
498 return self.complete(text)[1]
498 return self.complete(text)[1]
499
499
500 def _clean_glob(self,text):
500 def _clean_glob(self,text):
501 return self.glob("%s*" % text)
501 return self.glob("%s*" % text)
502
502
503 def _clean_glob_win32(self,text):
503 def _clean_glob_win32(self,text):
504 return [f.replace("\\","/")
504 return [f.replace("\\","/")
505 for f in self.glob("%s*" % text)]
505 for f in self.glob("%s*" % text)]
506
506
507 def file_matches(self, text):
507 def file_matches(self, text):
508 """Match filenames, expanding ~USER type strings.
508 """Match filenames, expanding ~USER type strings.
509
509
510 Most of the seemingly convoluted logic in this completer is an
510 Most of the seemingly convoluted logic in this completer is an
511 attempt to handle filenames with spaces in them. And yet it's not
511 attempt to handle filenames with spaces in them. And yet it's not
512 quite perfect, because Python's readline doesn't expose all of the
512 quite perfect, because Python's readline doesn't expose all of the
513 GNU readline details needed for this to be done correctly.
513 GNU readline details needed for this to be done correctly.
514
514
515 For a filename with a space in it, the printed completions will be
515 For a filename with a space in it, the printed completions will be
516 only the parts after what's already been typed (instead of the
516 only the parts after what's already been typed (instead of the
517 full completions, as is normally done). I don't think with the
517 full completions, as is normally done). I don't think with the
518 current (as of Python 2.3) Python readline it's possible to do
518 current (as of Python 2.3) Python readline it's possible to do
519 better."""
519 better."""
520
520
521 #io.rprint('Completer->file_matches: <%r>' % text) # dbg
521 #io.rprint('Completer->file_matches: <%r>' % text) # dbg
522
522
523 # chars that require escaping with backslash - i.e. chars
523 # chars that require escaping with backslash - i.e. chars
524 # that readline treats incorrectly as delimiters, but we
524 # that readline treats incorrectly as delimiters, but we
525 # don't want to treat as delimiters in filename matching
525 # don't want to treat as delimiters in filename matching
526 # when escaped with backslash
526 # when escaped with backslash
527 if text.startswith('!'):
527 if text.startswith('!'):
528 text = text[1:]
528 text = text[1:]
529 text_prefix = '!'
529 text_prefix = '!'
530 else:
530 else:
531 text_prefix = ''
531 text_prefix = ''
532
532
533 text_until_cursor = self.text_until_cursor
533 text_until_cursor = self.text_until_cursor
534 # track strings with open quotes
534 # track strings with open quotes
535 open_quotes = has_open_quotes(text_until_cursor)
535 open_quotes = has_open_quotes(text_until_cursor)
536
536
537 if '(' in text_until_cursor or '[' in text_until_cursor:
537 if '(' in text_until_cursor or '[' in text_until_cursor:
538 lsplit = text
538 lsplit = text
539 else:
539 else:
540 try:
540 try:
541 # arg_split ~ shlex.split, but with unicode bugs fixed by us
541 # arg_split ~ shlex.split, but with unicode bugs fixed by us
542 lsplit = arg_split(text_until_cursor)[-1]
542 lsplit = arg_split(text_until_cursor)[-1]
543 except ValueError:
543 except ValueError:
544 # typically an unmatched ", or backslash without escaped char.
544 # typically an unmatched ", or backslash without escaped char.
545 if open_quotes:
545 if open_quotes:
546 lsplit = text_until_cursor.split(open_quotes)[-1]
546 lsplit = text_until_cursor.split(open_quotes)[-1]
547 else:
547 else:
548 return []
548 return []
549 except IndexError:
549 except IndexError:
550 # tab pressed on empty line
550 # tab pressed on empty line
551 lsplit = ""
551 lsplit = ""
552
552
553 if not open_quotes and lsplit != protect_filename(lsplit):
553 if not open_quotes and lsplit != protect_filename(lsplit):
554 # if protectables are found, do matching on the whole escaped name
554 # if protectables are found, do matching on the whole escaped name
555 has_protectables = True
555 has_protectables = True
556 text0,text = text,lsplit
556 text0,text = text,lsplit
557 else:
557 else:
558 has_protectables = False
558 has_protectables = False
559 text = os.path.expanduser(text)
559 text = os.path.expanduser(text)
560
560
561 if text == "":
561 if text == "":
562 return [text_prefix + protect_filename(f) for f in self.glob("*")]
562 return [text_prefix + protect_filename(f) for f in self.glob("*")]
563
563
564 # Compute the matches from the filesystem
564 # Compute the matches from the filesystem
565 m0 = self.clean_glob(text.replace('\\',''))
565 m0 = self.clean_glob(text.replace('\\',''))
566
566
567 if has_protectables:
567 if has_protectables:
568 # If we had protectables, we need to revert our changes to the
568 # If we had protectables, we need to revert our changes to the
569 # beginning of filename so that we don't double-write the part
569 # beginning of filename so that we don't double-write the part
570 # of the filename we have so far
570 # of the filename we have so far
571 len_lsplit = len(lsplit)
571 len_lsplit = len(lsplit)
572 matches = [text_prefix + text0 +
572 matches = [text_prefix + text0 +
573 protect_filename(f[len_lsplit:]) for f in m0]
573 protect_filename(f[len_lsplit:]) for f in m0]
574 else:
574 else:
575 if open_quotes:
575 if open_quotes:
576 # if we have a string with an open quote, we don't need to
576 # if we have a string with an open quote, we don't need to
577 # protect the names at all (and we _shouldn't_, as it
577 # protect the names at all (and we _shouldn't_, as it
578 # would cause bugs when the filesystem call is made).
578 # would cause bugs when the filesystem call is made).
579 matches = m0
579 matches = m0
580 else:
580 else:
581 matches = [text_prefix +
581 matches = [text_prefix +
582 protect_filename(f) for f in m0]
582 protect_filename(f) for f in m0]
583
583
584 #io.rprint('mm', matches) # dbg
584 #io.rprint('mm', matches) # dbg
585 return mark_dirs(matches)
585 return mark_dirs(matches)
586
586
587 def magic_matches(self, text):
587 def magic_matches(self, text):
588 """Match magics"""
588 """Match magics"""
589 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
589 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
590 # Get all shell magics now rather than statically, so magics loaded at
590 # Get all shell magics now rather than statically, so magics loaded at
591 # runtime show up too
591 # runtime show up too
592 magics = self.shell.lsmagic()
592 magics = self.shell.lsmagic()
593 pre = self.magic_escape
593 pre = self.magic_escape
594 baretext = text.lstrip(pre)
594 baretext = text.lstrip(pre)
595 return [ pre+m for m in magics if m.startswith(baretext)]
595 return [ pre+m for m in magics if m.startswith(baretext)]
596
596
597 def alias_matches(self, text):
597 def alias_matches(self, text):
598 """Match internal system aliases"""
598 """Match internal system aliases"""
599 #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg
599 #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg
600
600
601 # if we are not in the first 'item', alias matching
601 # if we are not in the first 'item', alias matching
602 # doesn't make sense - unless we are starting with 'sudo' command.
602 # doesn't make sense - unless we are starting with 'sudo' command.
603 main_text = self.text_until_cursor.lstrip()
603 main_text = self.text_until_cursor.lstrip()
604 if ' ' in main_text and not main_text.startswith('sudo'):
604 if ' ' in main_text and not main_text.startswith('sudo'):
605 return []
605 return []
606 text = os.path.expanduser(text)
606 text = os.path.expanduser(text)
607 aliases = self.alias_table.keys()
607 aliases = self.alias_table.keys()
608 if text == '':
608 if text == '':
609 return aliases
609 return aliases
610 else:
610 else:
611 return [a for a in aliases if a.startswith(text)]
611 return [a for a in aliases if a.startswith(text)]
612
612
613 def python_matches(self,text):
613 def python_matches(self,text):
614 """Match attributes or global python names"""
614 """Match attributes or global python names"""
615
615
616 #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
616 #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
617 if "." in text:
617 if "." in text:
618 try:
618 try:
619 matches = self.attr_matches(text)
619 matches = self.attr_matches(text)
620 if text.endswith('.') and self.omit__names:
620 if text.endswith('.') and self.omit__names:
621 if self.omit__names == 1:
621 if self.omit__names == 1:
622 # true if txt is _not_ a __ name, false otherwise:
622 # true if txt is _not_ a __ name, false otherwise:
623 no__name = (lambda txt:
623 no__name = (lambda txt:
624 re.match(r'.*\.__.*?__',txt) is None)
624 re.match(r'.*\.__.*?__',txt) is None)
625 else:
625 else:
626 # true if txt is _not_ a _ name, false otherwise:
626 # true if txt is _not_ a _ name, false otherwise:
627 no__name = (lambda txt:
627 no__name = (lambda txt:
628 re.match(r'.*\._.*?',txt) is None)
628 re.match(r'.*\._.*?',txt) is None)
629 matches = filter(no__name, matches)
629 matches = filter(no__name, matches)
630 except NameError:
630 except NameError:
631 # catches <undefined attributes>.<tab>
631 # catches <undefined attributes>.<tab>
632 matches = []
632 matches = []
633 else:
633 else:
634 matches = self.global_matches(text)
634 matches = self.global_matches(text)
635
635
636 return matches
636 return matches
637
637
638 def _default_arguments(self, obj):
638 def _default_arguments(self, obj):
639 """Return the list of default arguments of obj if it is callable,
639 """Return the list of default arguments of obj if it is callable,
640 or empty list otherwise."""
640 or empty list otherwise."""
641
641
642 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
642 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
643 # for classes, check for __init__,__new__
643 # for classes, check for __init__,__new__
644 if inspect.isclass(obj):
644 if inspect.isclass(obj):
645 obj = (getattr(obj,'__init__',None) or
645 obj = (getattr(obj,'__init__',None) or
646 getattr(obj,'__new__',None))
646 getattr(obj,'__new__',None))
647 # for all others, check if they are __call__able
647 # for all others, check if they are __call__able
648 elif hasattr(obj, '__call__'):
648 elif hasattr(obj, '__call__'):
649 obj = obj.__call__
649 obj = obj.__call__
650 # XXX: is there a way to handle the builtins ?
650 # XXX: is there a way to handle the builtins ?
651 try:
651 try:
652 args,_,_1,defaults = inspect.getargspec(obj)
652 args,_,_1,defaults = inspect.getargspec(obj)
653 if defaults:
653 if defaults:
654 return args[-len(defaults):]
654 return args[-len(defaults):]
655 except TypeError: pass
655 except TypeError: pass
656 return []
656 return []
657
657
658 def python_func_kw_matches(self,text):
658 def python_func_kw_matches(self,text):
659 """Match named parameters (kwargs) of the last open function"""
659 """Match named parameters (kwargs) of the last open function"""
660
660
661 if "." in text: # a parameter cannot be dotted
661 if "." in text: # a parameter cannot be dotted
662 return []
662 return []
663 try: regexp = self.__funcParamsRegex
663 try: regexp = self.__funcParamsRegex
664 except AttributeError:
664 except AttributeError:
665 regexp = self.__funcParamsRegex = re.compile(r'''
665 regexp = self.__funcParamsRegex = re.compile(r'''
666 '.*?' | # single quoted strings or
666 '.*?' | # single quoted strings or
667 ".*?" | # double quoted strings or
667 ".*?" | # double quoted strings or
668 \w+ | # identifier
668 \w+ | # identifier
669 \S # other characters
669 \S # other characters
670 ''', re.VERBOSE | re.DOTALL)
670 ''', re.VERBOSE | re.DOTALL)
671 # 1. find the nearest identifier that comes before an unclosed
671 # 1. find the nearest identifier that comes before an unclosed
672 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
672 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
673 tokens = regexp.findall(self.line_buffer)
673 tokens = regexp.findall(self.line_buffer)
674 tokens.reverse()
674 tokens.reverse()
675 iterTokens = iter(tokens); openPar = 0
675 iterTokens = iter(tokens); openPar = 0
676 for token in iterTokens:
676 for token in iterTokens:
677 if token == ')':
677 if token == ')':
678 openPar -= 1
678 openPar -= 1
679 elif token == '(':
679 elif token == '(':
680 openPar += 1
680 openPar += 1
681 if openPar > 0:
681 if openPar > 0:
682 # found the last unclosed parenthesis
682 # found the last unclosed parenthesis
683 break
683 break
684 else:
684 else:
685 return []
685 return []
686 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
686 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
687 ids = []
687 ids = []
688 isId = re.compile(r'\w+$').match
688 isId = re.compile(r'\w+$').match
689 while True:
689 while True:
690 try:
690 try:
691 ids.append(iterTokens.next())
691 ids.append(iterTokens.next())
692 if not isId(ids[-1]):
692 if not isId(ids[-1]):
693 ids.pop(); break
693 ids.pop(); break
694 if not iterTokens.next() == '.':
694 if not iterTokens.next() == '.':
695 break
695 break
696 except StopIteration:
696 except StopIteration:
697 break
697 break
698 # lookup the candidate callable matches either using global_matches
698 # lookup the candidate callable matches either using global_matches
699 # or attr_matches for dotted names
699 # or attr_matches for dotted names
700 if len(ids) == 1:
700 if len(ids) == 1:
701 callableMatches = self.global_matches(ids[0])
701 callableMatches = self.global_matches(ids[0])
702 else:
702 else:
703 callableMatches = self.attr_matches('.'.join(ids[::-1]))
703 callableMatches = self.attr_matches('.'.join(ids[::-1]))
704 argMatches = []
704 argMatches = []
705 for callableMatch in callableMatches:
705 for callableMatch in callableMatches:
706 try:
706 try:
707 namedArgs = self._default_arguments(eval(callableMatch,
707 namedArgs = self._default_arguments(eval(callableMatch,
708 self.namespace))
708 self.namespace))
709 except:
709 except:
710 continue
710 continue
711 for namedArg in namedArgs:
711 for namedArg in namedArgs:
712 if namedArg.startswith(text):
712 if namedArg.startswith(text):
713 argMatches.append("%s=" %namedArg)
713 argMatches.append("%s=" %namedArg)
714 return argMatches
714 return argMatches
715
715
716 def dispatch_custom_completer(self, text):
716 def dispatch_custom_completer(self, text):
717 #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg
717 #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg
718 line = self.line_buffer
718 line = self.line_buffer
719 if not line.strip():
719 if not line.strip():
720 return None
720 return None
721
721
722 # Create a little structure to pass all the relevant information about
722 # Create a little structure to pass all the relevant information about
723 # the current completion to any custom completer.
723 # the current completion to any custom completer.
724 event = Bunch()
724 event = Bunch()
725 event.line = line
725 event.line = line
726 event.symbol = text
726 event.symbol = text
727 cmd = line.split(None,1)[0]
727 cmd = line.split(None,1)[0]
728 event.command = cmd
728 event.command = cmd
729 event.text_until_cursor = self.text_until_cursor
729 event.text_until_cursor = self.text_until_cursor
730
730
731 #print "\ncustom:{%s]\n" % event # dbg
731 #print "\ncustom:{%s]\n" % event # dbg
732
732
733 # for foo etc, try also to find completer for %foo
733 # for foo etc, try also to find completer for %foo
734 if not cmd.startswith(self.magic_escape):
734 if not cmd.startswith(self.magic_escape):
735 try_magic = self.custom_completers.s_matches(
735 try_magic = self.custom_completers.s_matches(
736 self.magic_escape + cmd)
736 self.magic_escape + cmd)
737 else:
737 else:
738 try_magic = []
738 try_magic = []
739
739
740 for c in itertools.chain(self.custom_completers.s_matches(cmd),
740 for c in itertools.chain(self.custom_completers.s_matches(cmd),
741 try_magic,
741 try_magic,
742 self.custom_completers.flat_matches(self.text_until_cursor)):
742 self.custom_completers.flat_matches(self.text_until_cursor)):
743 #print "try",c # dbg
743 #print "try",c # dbg
744 try:
744 try:
745 res = c(event)
745 res = c(event)
746 if res:
746 if res:
747 # first, try case sensitive match
747 # first, try case sensitive match
748 withcase = [r for r in res if r.startswith(text)]
748 withcase = [r for r in res if r.startswith(text)]
749 if withcase:
749 if withcase:
750 return withcase
750 return withcase
751 # if none, then case insensitive ones are ok too
751 # if none, then case insensitive ones are ok too
752 text_low = text.lower()
752 text_low = text.lower()
753 return [r for r in res if r.lower().startswith(text_low)]
753 return [r for r in res if r.lower().startswith(text_low)]
754 except TryNext:
754 except TryNext:
755 pass
755 pass
756
756
757 return None
757 return None
758
758
759 def complete(self, text=None, line_buffer=None, cursor_pos=None):
759 def complete(self, text=None, line_buffer=None, cursor_pos=None):
760 """Find completions for the given text and line context.
760 """Find completions for the given text and line context.
761
761
762 This is called successively with state == 0, 1, 2, ... until it
762 This is called successively with state == 0, 1, 2, ... until it
763 returns None. The completion should begin with 'text'.
763 returns None. The completion should begin with 'text'.
764
764
765 Note that both the text and the line_buffer are optional, but at least
765 Note that both the text and the line_buffer are optional, but at least
766 one of them must be given.
766 one of them must be given.
767
767
768 Parameters
768 Parameters
769 ----------
769 ----------
770 text : string, optional
770 text : string, optional
771 Text to perform the completion on. If not given, the line buffer
771 Text to perform the completion on. If not given, the line buffer
772 is split using the instance's CompletionSplitter object.
772 is split using the instance's CompletionSplitter object.
773
773
774 line_buffer : string, optional
774 line_buffer : string, optional
775 If not given, the completer attempts to obtain the current line
775 If not given, the completer attempts to obtain the current line
776 buffer via readline. This keyword allows clients which are
776 buffer via readline. This keyword allows clients which are
777 requesting for text completions in non-readline contexts to inform
777 requesting for text completions in non-readline contexts to inform
778 the completer of the entire text.
778 the completer of the entire text.
779
779
780 cursor_pos : int, optional
780 cursor_pos : int, optional
781 Index of the cursor in the full line buffer. Should be provided by
781 Index of the cursor in the full line buffer. Should be provided by
782 remote frontends where kernel has no access to frontend state.
782 remote frontends where kernel has no access to frontend state.
783
783
784 Returns
784 Returns
785 -------
785 -------
786 text : str
786 text : str
787 Text that was actually used in the completion.
787 Text that was actually used in the completion.
788
788
789 matches : list
789 matches : list
790 A list of completion matches.
790 A list of completion matches.
791 """
791 """
792 #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
792 #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
793
793
794 # if the cursor position isn't given, the only sane assumption we can
794 # if the cursor position isn't given, the only sane assumption we can
795 # make is that it's at the end of the line (the common case)
795 # make is that it's at the end of the line (the common case)
796 if cursor_pos is None:
796 if cursor_pos is None:
797 cursor_pos = len(line_buffer) if text is None else len(text)
797 cursor_pos = len(line_buffer) if text is None else len(text)
798
798
799 # if text is either None or an empty string, rely on the line buffer
799 # if text is either None or an empty string, rely on the line buffer
800 if not text:
800 if not text:
801 text = self.splitter.split_line(line_buffer, cursor_pos)
801 text = self.splitter.split_line(line_buffer, cursor_pos)
802
802
803 # If no line buffer is given, assume the input text is all there was
803 # If no line buffer is given, assume the input text is all there was
804 if line_buffer is None:
804 if line_buffer is None:
805 line_buffer = text
805 line_buffer = text
806
806
807 self.line_buffer = line_buffer
807 self.line_buffer = line_buffer
808 self.text_until_cursor = self.line_buffer[:cursor_pos]
808 self.text_until_cursor = self.line_buffer[:cursor_pos]
809 #io.rprint('\nCOMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
809 #io.rprint('\nCOMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
810
810
811 # Start with a clean slate of completions
811 # Start with a clean slate of completions
812 self.matches[:] = []
812 self.matches[:] = []
813 custom_res = self.dispatch_custom_completer(text)
813 custom_res = self.dispatch_custom_completer(text)
814 if custom_res is not None:
814 if custom_res is not None:
815 # did custom completers produce something?
815 # did custom completers produce something?
816 self.matches = custom_res
816 self.matches = custom_res
817 else:
817 else:
818 # Extend the list of completions with the results of each
818 # Extend the list of completions with the results of each
819 # matcher, so we return results to the user from all
819 # matcher, so we return results to the user from all
820 # namespaces.
820 # namespaces.
821 if self.merge_completions:
821 if self.merge_completions:
822 self.matches = []
822 self.matches = []
823 for matcher in self.matchers:
823 for matcher in self.matchers:
824 try:
824 try:
825 self.matches.extend(matcher(text))
825 self.matches.extend(matcher(text))
826 except:
826 except:
827 # Show the ugly traceback if the matcher causes an
827 # Show the ugly traceback if the matcher causes an
828 # exception, but do NOT crash the kernel!
828 # exception, but do NOT crash the kernel!
829 sys.excepthook(*sys.exc_info())
829 sys.excepthook(*sys.exc_info())
830 else:
830 else:
831 for matcher in self.matchers:
831 for matcher in self.matchers:
832 self.matches = matcher(text)
832 self.matches = matcher(text)
833 if self.matches:
833 if self.matches:
834 break
834 break
835 # FIXME: we should extend our api to return a dict with completions for
835 # FIXME: we should extend our api to return a dict with completions for
836 # different types of objects. The rlcomplete() method could then
836 # different types of objects. The rlcomplete() method could then
837 # simply collapse the dict into a list for readline, but we'd have
837 # simply collapse the dict into a list for readline, but we'd have
838 # richer completion semantics in other evironments.
838 # richer completion semantics in other evironments.
839 self.matches = sorted(set(self.matches))
839 self.matches = sorted(set(self.matches))
840 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
840 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
841 return text, self.matches
841 return text, self.matches
842
842
843 def rlcomplete(self, text, state):
843 def rlcomplete(self, text, state):
844 """Return the state-th possible completion for 'text'.
844 """Return the state-th possible completion for 'text'.
845
845
846 This is called successively with state == 0, 1, 2, ... until it
846 This is called successively with state == 0, 1, 2, ... until it
847 returns None. The completion should begin with 'text'.
847 returns None. The completion should begin with 'text'.
848
848
849 Parameters
849 Parameters
850 ----------
850 ----------
851 text : string
851 text : string
852 Text to perform the completion on.
852 Text to perform the completion on.
853
853
854 state : int
854 state : int
855 Counter used by readline.
855 Counter used by readline.
856 """
856 """
857 if state==0:
857 if state==0:
858
858
859 self.line_buffer = line_buffer = self.readline.get_line_buffer()
859 self.line_buffer = line_buffer = self.readline.get_line_buffer()
860 cursor_pos = self.readline.get_endidx()
860 cursor_pos = self.readline.get_endidx()
861
861
862 #io.rprint("\nRLCOMPLETE: %r %r %r" %
862 #io.rprint("\nRLCOMPLETE: %r %r %r" %
863 # (text, line_buffer, cursor_pos) ) # dbg
863 # (text, line_buffer, cursor_pos) ) # dbg
864
864
865 # if there is only a tab on a line with only whitespace, instead of
865 # if there is only a tab on a line with only whitespace, instead of
866 # the mostly useless 'do you want to see all million completions'
866 # the mostly useless 'do you want to see all million completions'
867 # message, just do the right thing and give the user his tab!
867 # message, just do the right thing and give the user his tab!
868 # Incidentally, this enables pasting of tabbed text from an editor
868 # Incidentally, this enables pasting of tabbed text from an editor
869 # (as long as autoindent is off).
869 # (as long as autoindent is off).
870
870
871 # It should be noted that at least pyreadline still shows file
871 # It should be noted that at least pyreadline still shows file
872 # completions - is there a way around it?
872 # completions - is there a way around it?
873
873
874 # don't apply this on 'dumb' terminals, such as emacs buffers, so
874 # don't apply this on 'dumb' terminals, such as emacs buffers, so
875 # we don't interfere with their own tab-completion mechanism.
875 # we don't interfere with their own tab-completion mechanism.
876 if not (self.dumb_terminal or line_buffer.strip()):
876 if not (self.dumb_terminal or line_buffer.strip()):
877 self.readline.insert_text('\t')
877 self.readline.insert_text('\t')
878 sys.stdout.flush()
878 sys.stdout.flush()
879 return None
879 return None
880
880
881 # Note: debugging exceptions that may occur in completion is very
881 # Note: debugging exceptions that may occur in completion is very
882 # tricky, because readline unconditionally silences them. So if
882 # tricky, because readline unconditionally silences them. So if
883 # during development you suspect a bug in the completion code, turn
883 # during development you suspect a bug in the completion code, turn
884 # this flag on temporarily by uncommenting the second form (don't
884 # this flag on temporarily by uncommenting the second form (don't
885 # flip the value in the first line, as the '# dbg' marker can be
885 # flip the value in the first line, as the '# dbg' marker can be
886 # automatically detected and is used elsewhere).
886 # automatically detected and is used elsewhere).
887 DEBUG = False
887 DEBUG = False
888 #DEBUG = True # dbg
888 #DEBUG = True # dbg
889 if DEBUG:
889 if DEBUG:
890 try:
890 try:
891 self.complete(text, line_buffer, cursor_pos)
891 self.complete(text, line_buffer, cursor_pos)
892 except:
892 except:
893 import traceback; traceback.print_exc()
893 import traceback; traceback.print_exc()
894 else:
894 else:
895 # The normal production version is here
895 # The normal production version is here
896
896
897 # This method computes the self.matches array
897 # This method computes the self.matches array
898 self.complete(text, line_buffer, cursor_pos)
898 self.complete(text, line_buffer, cursor_pos)
899
899
900 try:
900 try:
901 return self.matches[state]
901 return self.matches[state]
902 except IndexError:
902 except IndexError:
903 return None
903 return None
@@ -1,347 +1,347 b''
1 """Implementations for various useful completers.
1 """Implementations for various useful completers.
2
2
3 These are all loaded by default by IPython.
3 These are all loaded by default by IPython.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (C) 2010 The IPython Development Team.
6 # Copyright (C) 2010 The IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the BSD License.
8 # Distributed under the terms of the BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 from __future__ import print_function
16 from __future__ import print_function
17
17
18 # Stdlib imports
18 # Stdlib imports
19 import glob
19 import glob
20 import inspect
20 import inspect
21 import os
21 import os
22 import re
22 import re
23 import shlex
23 import shlex
24 import sys
24 import sys
25
25
26 # Third-party imports
26 # Third-party imports
27 from time import time
27 from time import time
28 from zipimport import zipimporter
28 from zipimport import zipimporter
29
29
30 # Our own imports
30 # Our own imports
31 from IPython.core.completer import expand_user, compress_user
31 from IPython.core.completer import expand_user, compress_user
32 from IPython.core.error import TryNext
32 from IPython.core.error import TryNext
33 from IPython.utils import py3compat
33 from IPython.utils import py3compat
34
34
35 # FIXME: this should be pulled in with the right call via the component system
35 # FIXME: this should be pulled in with the right call via the component system
36 from IPython.core.ipapi import get as get_ipython
36 from IPython.core.ipapi import get as get_ipython
37
37
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39 # Globals and constants
39 # Globals and constants
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41
41
42 # Time in seconds after which the rootmodules will be stored permanently in the
42 # Time in seconds after which the rootmodules will be stored permanently in the
43 # ipython ip.db database (kept in the user's .ipython dir).
43 # ipython ip.db database (kept in the user's .ipython dir).
44 TIMEOUT_STORAGE = 2
44 TIMEOUT_STORAGE = 2
45
45
46 # Time in seconds after which we give up
46 # Time in seconds after which we give up
47 TIMEOUT_GIVEUP = 20
47 TIMEOUT_GIVEUP = 20
48
48
49 # Regular expression for the python import statement
49 # Regular expression for the python import statement
50 import_re = re.compile(r'.*(\.so|\.py[cod]?)$')
50 import_re = re.compile(r'.*(\.so|\.py[cod]?)$')
51
51
52 # RE for the ipython %run command (python + ipython scripts)
52 # RE for the ipython %run command (python + ipython scripts)
53 magic_run_re = re.compile(r'.*(\.ipy|\.py[w]?)$')
53 magic_run_re = re.compile(r'.*(\.ipy|\.py[w]?)$')
54
54
55 #-----------------------------------------------------------------------------
55 #-----------------------------------------------------------------------------
56 # Local utilities
56 # Local utilities
57 #-----------------------------------------------------------------------------
57 #-----------------------------------------------------------------------------
58
58
59 def shlex_split(x):
59 def shlex_split(x):
60 """Helper function to split lines into segments.
60 """Helper function to split lines into segments.
61 """
61 """
62 # shlex.split raises an exception if there is a syntax error in sh syntax
62 # shlex.split raises an exception if there is a syntax error in sh syntax
63 # for example if no closing " is found. This function keeps dropping the
63 # for example if no closing " is found. This function keeps dropping the
64 # last character of the line until shlex.split does not raise
64 # last character of the line until shlex.split does not raise
65 # an exception. It adds end of the line to the result of shlex.split
65 # an exception. It adds end of the line to the result of shlex.split
66 #
66 #
67 # Example:
67 # Example:
68 # %run "c:/python -> ['%run','"c:/python']
68 # %run "c:/python -> ['%run','"c:/python']
69
69
70 # shlex.split has unicode bugs in Python 2, so encode first to str
70 # shlex.split has unicode bugs in Python 2, so encode first to str
71 if not py3compat.PY3:
71 if not py3compat.PY3:
72 x = py3compat.cast_bytes(x)
72 x = py3compat.cast_bytes(x)
73
73
74 endofline = []
74 endofline = []
75 while x != '':
75 while x != '':
76 try:
76 try:
77 comps = shlex.split(x)
77 comps = shlex.split(x)
78 if len(endofline) >= 1:
78 if len(endofline) >= 1:
79 comps.append(''.join(endofline))
79 comps.append(''.join(endofline))
80 return comps
80 return comps
81
81
82 except ValueError:
82 except ValueError:
83 endofline = [x[-1:]]+endofline
83 endofline = [x[-1:]]+endofline
84 x = x[:-1]
84 x = x[:-1]
85
85
86 return [''.join(endofline)]
86 return [''.join(endofline)]
87
87
88 def module_list(path):
88 def module_list(path):
89 """
89 """
90 Return the list containing the names of the modules available in the given
90 Return the list containing the names of the modules available in the given
91 folder.
91 folder.
92 """
92 """
93
93
94 if os.path.isdir(path):
94 if os.path.isdir(path):
95 folder_list = os.listdir(path)
95 folder_list = os.listdir(path)
96 elif path.endswith('.egg'):
96 elif path.endswith('.egg'):
97 try:
97 try:
98 folder_list = [f for f in zipimporter(path)._files]
98 folder_list = [f for f in zipimporter(path)._files]
99 except:
99 except:
100 folder_list = []
100 folder_list = []
101 else:
101 else:
102 folder_list = []
102 folder_list = []
103
103
104 if not folder_list:
104 if not folder_list:
105 return []
105 return []
106
106
107 # A few local constants to be used in loops below
107 # A few local constants to be used in loops below
108 isfile = os.path.isfile
108 isfile = os.path.isfile
109 pjoin = os.path.join
109 pjoin = os.path.join
110 basename = os.path.basename
110 basename = os.path.basename
111
111
112 # Now find actual path matches for packages or modules
112 # Now find actual path matches for packages or modules
113 folder_list = [p for p in folder_list
113 folder_list = [p for p in folder_list
114 if isfile(pjoin(path, p,'__init__.py'))
114 if isfile(pjoin(path, p,'__init__.py'))
115 or import_re.match(p) ]
115 or import_re.match(p) ]
116
116
117 return [basename(p).split('.')[0] for p in folder_list]
117 return [basename(p).split('.')[0] for p in folder_list]
118
118
119 def get_root_modules():
119 def get_root_modules():
120 """
120 """
121 Returns a list containing the names of all the modules available in the
121 Returns a list containing the names of all the modules available in the
122 folders of the pythonpath.
122 folders of the pythonpath.
123 """
123 """
124 ip = get_ipython()
124 ip = get_ipython()
125
125
126 if 'rootmodules' in ip.db:
126 if 'rootmodules' in ip.db:
127 return ip.db['rootmodules']
127 return ip.db['rootmodules']
128
128
129 t = time()
129 t = time()
130 store = False
130 store = False
131 modules = list(sys.builtin_module_names)
131 modules = list(sys.builtin_module_names)
132 for path in sys.path:
132 for path in sys.path:
133 modules += module_list(path)
133 modules += module_list(path)
134 if time() - t >= TIMEOUT_STORAGE and not store:
134 if time() - t >= TIMEOUT_STORAGE and not store:
135 store = True
135 store = True
136 print("\nCaching the list of root modules, please wait!")
136 print("\nCaching the list of root modules, please wait!")
137 print("(This will only be done once - type '%rehashx' to "
137 print("(This will only be done once - type '%rehashx' to "
138 "reset cache!)\n")
138 "reset cache!)\n")
139 sys.stdout.flush()
139 sys.stdout.flush()
140 if time() - t > TIMEOUT_GIVEUP:
140 if time() - t > TIMEOUT_GIVEUP:
141 print("This is taking too long, we give up.\n")
141 print("This is taking too long, we give up.\n")
142 ip.db['rootmodules'] = []
142 ip.db['rootmodules'] = []
143 return []
143 return []
144
144
145 modules = set(modules)
145 modules = set(modules)
146 if '__init__' in modules:
146 if '__init__' in modules:
147 modules.remove('__init__')
147 modules.remove('__init__')
148 modules = list(modules)
148 modules = list(modules)
149 if store:
149 if store:
150 ip.db['rootmodules'] = modules
150 ip.db['rootmodules'] = modules
151 return modules
151 return modules
152
152
153
153
154 def is_importable(module, attr, only_modules):
154 def is_importable(module, attr, only_modules):
155 if only_modules:
155 if only_modules:
156 return inspect.ismodule(getattr(module, attr))
156 return inspect.ismodule(getattr(module, attr))
157 else:
157 else:
158 return not(attr[:2] == '__' and attr[-2:] == '__')
158 return not(attr[:2] == '__' and attr[-2:] == '__')
159
159
160
160
161 def try_import(mod, only_modules=False):
161 def try_import(mod, only_modules=False):
162 try:
162 try:
163 m = __import__(mod)
163 m = __import__(mod)
164 except:
164 except:
165 return []
165 return []
166 mods = mod.split('.')
166 mods = mod.split('.')
167 for module in mods[1:]:
167 for module in mods[1:]:
168 m = getattr(m, module)
168 m = getattr(m, module)
169
169
170 m_is_init = hasattr(m, '__file__') and '__init__' in m.__file__
170 m_is_init = hasattr(m, '__file__') and '__init__' in m.__file__
171
171
172 completions = []
172 completions = []
173 if (not hasattr(m, '__file__')) or (not only_modules) or m_is_init:
173 if (not hasattr(m, '__file__')) or (not only_modules) or m_is_init:
174 completions.extend( [attr for attr in dir(m) if
174 completions.extend( [attr for attr in dir(m) if
175 is_importable(m, attr, only_modules)])
175 is_importable(m, attr, only_modules)])
176
176
177 completions.extend(getattr(m, '__all__', []))
177 completions.extend(getattr(m, '__all__', []))
178 if m_is_init:
178 if m_is_init:
179 completions.extend(module_list(os.path.dirname(m.__file__)))
179 completions.extend(module_list(os.path.dirname(m.__file__)))
180 completions = set(completions)
180 completions = set(completions)
181 if '__init__' in completions:
181 if '__init__' in completions:
182 completions.remove('__init__')
182 completions.remove('__init__')
183 return list(completions)
183 return list(completions)
184
184
185
185
186 #-----------------------------------------------------------------------------
186 #-----------------------------------------------------------------------------
187 # Completion-related functions.
187 # Completion-related functions.
188 #-----------------------------------------------------------------------------
188 #-----------------------------------------------------------------------------
189
189
190 def quick_completer(cmd, completions):
190 def quick_completer(cmd, completions):
191 """ Easily create a trivial completer for a command.
191 """ Easily create a trivial completer for a command.
192
192
193 Takes either a list of completions, or all completions in string (that will
193 Takes either a list of completions, or all completions in string (that will
194 be split on whitespace).
194 be split on whitespace).
195
195
196 Example::
196 Example::
197
197
198 [d:\ipython]|1> import ipy_completers
198 [d:\ipython]|1> import ipy_completers
199 [d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz'])
199 [d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz'])
200 [d:\ipython]|3> foo b<TAB>
200 [d:\ipython]|3> foo b<TAB>
201 bar baz
201 bar baz
202 [d:\ipython]|3> foo ba
202 [d:\ipython]|3> foo ba
203 """
203 """
204
204
205 if isinstance(completions, basestring):
205 if isinstance(completions, basestring):
206 completions = completions.split()
206 completions = completions.split()
207
207
208 def do_complete(self, event):
208 def do_complete(self, event):
209 return completions
209 return completions
210
210
211 get_ipython().set_hook('complete_command',do_complete, str_key = cmd)
211 get_ipython().set_hook('complete_command',do_complete, str_key = cmd)
212
212
213
213
214 def module_completion(line):
214 def module_completion(line):
215 """
215 """
216 Returns a list containing the completion possibilities for an import line.
216 Returns a list containing the completion possibilities for an import line.
217
217
218 The line looks like this :
218 The line looks like this :
219 'import xml.d'
219 'import xml.d'
220 'from xml.dom import'
220 'from xml.dom import'
221 """
221 """
222
222
223 words = line.split(' ')
223 words = line.split(' ')
224 nwords = len(words)
224 nwords = len(words)
225
225
226 # from whatever <tab> -> 'import '
226 # from whatever <tab> -> 'import '
227 if nwords == 3 and words[0] == 'from':
227 if nwords == 3 and words[0] == 'from':
228 return ['import ']
228 return ['import ']
229
229
230 # 'from xy<tab>' or 'import xy<tab>'
230 # 'from xy<tab>' or 'import xy<tab>'
231 if nwords < 3 and (words[0] in ['import','from']) :
231 if nwords < 3 and (words[0] in ['import','from']) :
232 if nwords == 1:
232 if nwords == 1:
233 return get_root_modules()
233 return get_root_modules()
234 mod = words[1].split('.')
234 mod = words[1].split('.')
235 if len(mod) < 2:
235 if len(mod) < 2:
236 return get_root_modules()
236 return get_root_modules()
237 completion_list = try_import('.'.join(mod[:-1]), True)
237 completion_list = try_import('.'.join(mod[:-1]), True)
238 return ['.'.join(mod[:-1] + [el]) for el in completion_list]
238 return ['.'.join(mod[:-1] + [el]) for el in completion_list]
239
239
240 # 'from xyz import abc<tab>'
240 # 'from xyz import abc<tab>'
241 if nwords >= 3 and words[0] == 'from':
241 if nwords >= 3 and words[0] == 'from':
242 mod = words[1]
242 mod = words[1]
243 return try_import(mod)
243 return try_import(mod)
244
244
245 #-----------------------------------------------------------------------------
245 #-----------------------------------------------------------------------------
246 # Completers
246 # Completers
247 #-----------------------------------------------------------------------------
247 #-----------------------------------------------------------------------------
248 # These all have the func(self, event) signature to be used as custom
248 # These all have the func(self, event) signature to be used as custom
249 # completers
249 # completers
250
250
251 def module_completer(self,event):
251 def module_completer(self,event):
252 """Give completions after user has typed 'import ...' or 'from ...'"""
252 """Give completions after user has typed 'import ...' or 'from ...'"""
253
253
254 # This works in all versions of python. While 2.5 has
254 # This works in all versions of python. While 2.5 has
255 # pkgutil.walk_packages(), that particular routine is fairly dangerous,
255 # pkgutil.walk_packages(), that particular routine is fairly dangerous,
256 # since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full
256 # since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full
257 # of possibly problematic side effects.
257 # of possibly problematic side effects.
258 # This search the folders in the sys.path for available modules.
258 # This search the folders in the sys.path for available modules.
259
259
260 return module_completion(event.line)
260 return module_completion(event.line)
261
261
262 # FIXME: there's a lot of logic common to the run, cd and builtin file
262 # FIXME: there's a lot of logic common to the run, cd and builtin file
263 # completers, that is currently reimplemented in each.
263 # completers, that is currently reimplemented in each.
264
264
265 def magic_run_completer(self, event):
265 def magic_run_completer(self, event):
266 """Complete files that end in .py or .ipy for the %run command.
266 """Complete files that end in .py or .ipy for the %run command.
267 """
267 """
268 comps = shlex_split(event.line)
268 comps = shlex_split(event.line)
269 relpath = (len(comps) > 1 and comps[-1] or '').strip("'\"")
269 relpath = (len(comps) > 1 and comps[-1] or '').strip("'\"")
270
270
271 #print("\nev=", event) # dbg
271 #print("\nev=", event) # dbg
272 #print("rp=", relpath) # dbg
272 #print("rp=", relpath) # dbg
273 #print('comps=', comps) # dbg
273 #print('comps=', comps) # dbg
274
274
275 lglob = glob.glob
275 lglob = glob.glob
276 isdir = os.path.isdir
276 isdir = os.path.isdir
277 relpath, tilde_expand, tilde_val = expand_user(relpath)
277 relpath, tilde_expand, tilde_val = expand_user(relpath)
278
278
279 dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)]
279 dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)]
280
280
281 # Find if the user has already typed the first filename, after which we
281 # Find if the user has already typed the first filename, after which we
282 # should complete on all files, since after the first one other files may
282 # should complete on all files, since after the first one other files may
283 # be arguments to the input script.
283 # be arguments to the input script.
284
284
285 if filter(magic_run_re.match, comps):
285 if filter(magic_run_re.match, comps):
286 pys = [f.replace('\\','/') for f in lglob('*')]
286 pys = [f.replace('\\','/') for f in lglob('*')]
287 else:
287 else:
288 pys = [f.replace('\\','/')
288 pys = [f.replace('\\','/')
289 for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') +
289 for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') +
290 lglob(relpath + '*.pyw')]
290 lglob(relpath + '*.pyw')]
291 #print('run comp:', dirs+pys) # dbg
291 #print('run comp:', dirs+pys) # dbg
292 return [compress_user(p, tilde_expand, tilde_val) for p in dirs+pys]
292 return [compress_user(p, tilde_expand, tilde_val) for p in dirs+pys]
293
293
294
294
295 def cd_completer(self, event):
295 def cd_completer(self, event):
296 """Completer function for cd, which only returns directories."""
296 """Completer function for cd, which only returns directories."""
297 ip = get_ipython()
297 ip = get_ipython()
298 relpath = event.symbol
298 relpath = event.symbol
299
299
300 #print(event) # dbg
300 #print(event) # dbg
301 if event.line.endswith('-b') or ' -b ' in event.line:
301 if event.line.endswith('-b') or ' -b ' in event.line:
302 # return only bookmark completions
302 # return only bookmark completions
303 bkms = self.db.get('bookmarks', None)
303 bkms = self.db.get('bookmarks', None)
304 if bkms:
304 if bkms:
305 return bkms.keys()
305 return bkms.keys()
306 else:
306 else:
307 return []
307 return []
308
308
309 if event.symbol == '-':
309 if event.symbol == '-':
310 width_dh = str(len(str(len(ip.user_ns['_dh']) + 1)))
310 width_dh = str(len(str(len(ip.user_ns['_dh']) + 1)))
311 # jump in directory history by number
311 # jump in directory history by number
312 fmt = '-%0' + width_dh +'d [%s]'
312 fmt = '-%0' + width_dh +'d [%s]'
313 ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])]
313 ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])]
314 if len(ents) > 1:
314 if len(ents) > 1:
315 return ents
315 return ents
316 return []
316 return []
317
317
318 if event.symbol.startswith('--'):
318 if event.symbol.startswith('--'):
319 return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']]
319 return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']]
320
320
321 # Expand ~ in path and normalize directory separators.
321 # Expand ~ in path and normalize directory separators.
322 relpath, tilde_expand, tilde_val = expand_user(relpath)
322 relpath, tilde_expand, tilde_val = expand_user(relpath)
323 relpath = relpath.replace('\\','/')
323 relpath = relpath.replace('\\','/')
324
324
325 found = []
325 found = []
326 for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*')
326 for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*')
327 if os.path.isdir(f)]:
327 if os.path.isdir(f)]:
328 if ' ' in d:
328 if ' ' in d:
329 # we don't want to deal with any of that, complex code
329 # we don't want to deal with any of that, complex code
330 # for this is elsewhere
330 # for this is elsewhere
331 raise TryNext
331 raise TryNext
332
332
333 found.append(d)
333 found.append(d)
334
334
335 if not found:
335 if not found:
336 if os.path.isdir(relpath):
336 if os.path.isdir(relpath):
337 return [compress_user(relpath, tilde_expand, tilde_val)]
337 return [compress_user(relpath, tilde_expand, tilde_val)]
338
338
339 # if no completions so far, try bookmarks
339 # if no completions so far, try bookmarks
340 bks = self.db.get('bookmarks',{}).iterkeys()
340 bks = self.db.get('bookmarks',{}).iterkeys()
341 bkmatches = [s for s in bks if s.startswith(event.symbol)]
341 bkmatches = [s for s in bks if s.startswith(event.symbol)]
342 if bkmatches:
342 if bkmatches:
343 return bkmatches
343 return bkmatches
344
344
345 raise TryNext
345 raise TryNext
346
346
347 return [compress_user(p, tilde_expand, tilde_val) for p in found]
347 return [compress_user(p, tilde_expand, tilde_val) for p in found]
@@ -1,181 +1,181 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """sys.excepthook for IPython itself, leaves a detailed report on disk.
2 """sys.excepthook for IPython itself, leaves a detailed report on disk.
3
3
4 Authors:
4 Authors:
5
5
6 * Fernando Perez
6 * Fernando Perez
7 * Brian E. Granger
7 * Brian E. Granger
8 """
8 """
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
11 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
12 # Copyright (C) 2008-2010 The IPython Development Team
12 # Copyright (C) 2008-2010 The IPython Development Team
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 import os
22 import os
23 import sys
23 import sys
24 from pprint import pformat
24 from pprint import pformat
25
25
26 from IPython.core import ultratb
26 from IPython.core import ultratb
27 from IPython.utils.sysinfo import sys_info
27 from IPython.utils.sysinfo import sys_info
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Code
30 # Code
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 # Template for the user message.
33 # Template for the user message.
34 _default_message_template = """\
34 _default_message_template = """\
35 Oops, {app_name} crashed. We do our best to make it stable, but...
35 Oops, {app_name} crashed. We do our best to make it stable, but...
36
36
37 A crash report was automatically generated with the following information:
37 A crash report was automatically generated with the following information:
38 - A verbatim copy of the crash traceback.
38 - A verbatim copy of the crash traceback.
39 - A copy of your input history during this session.
39 - A copy of your input history during this session.
40 - Data on your current {app_name} configuration.
40 - Data on your current {app_name} configuration.
41
41
42 It was left in the file named:
42 It was left in the file named:
43 \t'{crash_report_fname}'
43 \t'{crash_report_fname}'
44 If you can email this file to the developers, the information in it will help
44 If you can email this file to the developers, the information in it will help
45 them in understanding and correcting the problem.
45 them in understanding and correcting the problem.
46
46
47 You can mail it to: {contact_name} at {contact_email}
47 You can mail it to: {contact_name} at {contact_email}
48 with the subject '{app_name} Crash Report'.
48 with the subject '{app_name} Crash Report'.
49
49
50 If you want to do it now, the following command will work (under Unix):
50 If you want to do it now, the following command will work (under Unix):
51 mail -s '{app_name} Crash Report' {contact_email} < {crash_report_fname}
51 mail -s '{app_name} Crash Report' {contact_email} < {crash_report_fname}
52
52
53 To ensure accurate tracking of this issue, please file a report about it at:
53 To ensure accurate tracking of this issue, please file a report about it at:
54 {bug_tracker}
54 {bug_tracker}
55 """
55 """
56
56
57
57
58 class CrashHandler(object):
58 class CrashHandler(object):
59 """Customizable crash handlers for IPython applications.
59 """Customizable crash handlers for IPython applications.
60
60
61 Instances of this class provide a :meth:`__call__` method which can be
61 Instances of this class provide a :meth:`__call__` method which can be
62 used as a ``sys.excepthook``. The :meth:`__call__` signature is::
62 used as a ``sys.excepthook``. The :meth:`__call__` signature is::
63
63
64 def __call__(self, etype, evalue, etb)
64 def __call__(self, etype, evalue, etb)
65 """
65 """
66
66
67 message_template = _default_message_template
67 message_template = _default_message_template
68 section_sep = '\n\n'+'*'*75+'\n\n'
68 section_sep = '\n\n'+'*'*75+'\n\n'
69
69
70 def __init__(self, app, contact_name=None, contact_email=None,
70 def __init__(self, app, contact_name=None, contact_email=None,
71 bug_tracker=None, show_crash_traceback=True, call_pdb=False):
71 bug_tracker=None, show_crash_traceback=True, call_pdb=False):
72 """Create a new crash handler
72 """Create a new crash handler
73
73
74 Parameters
74 Parameters
75 ----------
75 ----------
76 app : Application
76 app : Application
77 A running :class:`Application` instance, which will be queried at
77 A running :class:`Application` instance, which will be queried at
78 crash time for internal information.
78 crash time for internal information.
79
79
80 contact_name : str
80 contact_name : str
81 A string with the name of the person to contact.
81 A string with the name of the person to contact.
82
82
83 contact_email : str
83 contact_email : str
84 A string with the email address of the contact.
84 A string with the email address of the contact.
85
85
86 bug_tracker : str
86 bug_tracker : str
87 A string with the URL for your project's bug tracker.
87 A string with the URL for your project's bug tracker.
88
88
89 show_crash_traceback : bool
89 show_crash_traceback : bool
90 If false, don't print the crash traceback on stderr, only generate
90 If false, don't print the crash traceback on stderr, only generate
91 the on-disk report
91 the on-disk report
92
92
93 Non-argument instance attributes:
93 Non-argument instance attributes:
94
94
95 These instances contain some non-argument attributes which allow for
95 These instances contain some non-argument attributes which allow for
96 further customization of the crash handler's behavior. Please see the
96 further customization of the crash handler's behavior. Please see the
97 source for further details.
97 source for further details.
98 """
98 """
99 self.crash_report_fname = "Crash_report_%s.txt" % app.name
99 self.crash_report_fname = "Crash_report_%s.txt" % app.name
100 self.app = app
100 self.app = app
101 self.call_pdb = call_pdb
101 self.call_pdb = call_pdb
102 #self.call_pdb = True # dbg
102 #self.call_pdb = True # dbg
103 self.show_crash_traceback = show_crash_traceback
103 self.show_crash_traceback = show_crash_traceback
104 self.info = dict(app_name = app.name,
104 self.info = dict(app_name = app.name,
105 contact_name = contact_name,
105 contact_name = contact_name,
106 contact_email = contact_email,
106 contact_email = contact_email,
107 bug_tracker = bug_tracker,
107 bug_tracker = bug_tracker,
108 crash_report_fname = self.crash_report_fname)
108 crash_report_fname = self.crash_report_fname)
109
109
110
110
111 def __call__(self, etype, evalue, etb):
111 def __call__(self, etype, evalue, etb):
112 """Handle an exception, call for compatible with sys.excepthook"""
112 """Handle an exception, call for compatible with sys.excepthook"""
113
113
114 # Report tracebacks shouldn't use color in general (safer for users)
114 # Report tracebacks shouldn't use color in general (safer for users)
115 color_scheme = 'NoColor'
115 color_scheme = 'NoColor'
116
116
117 # Use this ONLY for developer debugging (keep commented out for release)
117 # Use this ONLY for developer debugging (keep commented out for release)
118 #color_scheme = 'Linux' # dbg
118 #color_scheme = 'Linux' # dbg
119 try:
119 try:
120 rptdir = self.app.ipython_dir
120 rptdir = self.app.ipython_dir
121 except:
121 except:
122 rptdir = os.getcwdu()
122 rptdir = os.getcwdu()
123 if rptdir is None or not os.path.isdir(rptdir):
123 if rptdir is None or not os.path.isdir(rptdir):
124 rptdir = os.getcwdu()
124 rptdir = os.getcwdu()
125 report_name = os.path.join(rptdir,self.crash_report_fname)
125 report_name = os.path.join(rptdir,self.crash_report_fname)
126 # write the report filename into the instance dict so it can get
126 # write the report filename into the instance dict so it can get
127 # properly expanded out in the user message template
127 # properly expanded out in the user message template
128 self.crash_report_fname = report_name
128 self.crash_report_fname = report_name
129 self.info['crash_report_fname'] = report_name
129 self.info['crash_report_fname'] = report_name
130 TBhandler = ultratb.VerboseTB(
130 TBhandler = ultratb.VerboseTB(
131 color_scheme=color_scheme,
131 color_scheme=color_scheme,
132 long_header=1,
132 long_header=1,
133 call_pdb=self.call_pdb,
133 call_pdb=self.call_pdb,
134 )
134 )
135 if self.call_pdb:
135 if self.call_pdb:
136 TBhandler(etype,evalue,etb)
136 TBhandler(etype,evalue,etb)
137 return
137 return
138 else:
138 else:
139 traceback = TBhandler.text(etype,evalue,etb,context=31)
139 traceback = TBhandler.text(etype,evalue,etb,context=31)
140
140
141 # print traceback to screen
141 # print traceback to screen
142 if self.show_crash_traceback:
142 if self.show_crash_traceback:
143 print >> sys.stderr, traceback
143 print >> sys.stderr, traceback
144
144
145 # and generate a complete report on disk
145 # and generate a complete report on disk
146 try:
146 try:
147 report = open(report_name,'w')
147 report = open(report_name,'w')
148 except:
148 except:
149 print >> sys.stderr, 'Could not create crash report on disk.'
149 print >> sys.stderr, 'Could not create crash report on disk.'
150 return
150 return
151
151
152 # Inform user on stderr of what happened
152 # Inform user on stderr of what happened
153 print >> sys.stderr, '\n'+'*'*70+'\n'
153 print >> sys.stderr, '\n'+'*'*70+'\n'
154 print >> sys.stderr, self.message_template.format(**self.info)
154 print >> sys.stderr, self.message_template.format(**self.info)
155
155
156 # Construct report on disk
156 # Construct report on disk
157 report.write(self.make_report(traceback))
157 report.write(self.make_report(traceback))
158 report.close()
158 report.close()
159 raw_input("Hit <Enter> to quit this message (your terminal may close):")
159 raw_input("Hit <Enter> to quit this message (your terminal may close):")
160
160
161 def make_report(self,traceback):
161 def make_report(self,traceback):
162 """Return a string containing a crash report."""
162 """Return a string containing a crash report."""
163
163
164 sec_sep = self.section_sep
164 sec_sep = self.section_sep
165
165
166 report = ['*'*75+'\n\n'+'IPython post-mortem report\n\n']
166 report = ['*'*75+'\n\n'+'IPython post-mortem report\n\n']
167 rpt_add = report.append
167 rpt_add = report.append
168 rpt_add(sys_info())
168 rpt_add(sys_info())
169
169
170 try:
170 try:
171 config = pformat(self.app.config)
171 config = pformat(self.app.config)
172 rpt_add(sec_sep)
172 rpt_add(sec_sep)
173 rpt_add('Application name: %s\n\n' % self.app_name)
173 rpt_add('Application name: %s\n\n' % self.app_name)
174 rpt_add('Current user configuration structure:\n\n')
174 rpt_add('Current user configuration structure:\n\n')
175 rpt_add(config)
175 rpt_add(config)
176 except:
176 except:
177 pass
177 pass
178 rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
178 rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
179
179
180 return ''.join(report)
180 return ''.join(report)
181
181
@@ -1,509 +1,509 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Pdb debugger class.
3 Pdb debugger class.
4
4
5 Modified from the standard pdb.Pdb class to avoid including readline, so that
5 Modified from the standard pdb.Pdb class to avoid including readline, so that
6 the command line completion of other programs which include this isn't
6 the command line completion of other programs which include this isn't
7 damaged.
7 damaged.
8
8
9 In the future, this class will be expanded with improvements over the standard
9 In the future, this class will be expanded with improvements over the standard
10 pdb.
10 pdb.
11
11
12 The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor
12 The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor
13 changes. Licensing should therefore be under the standard Python terms. For
13 changes. Licensing should therefore be under the standard Python terms. For
14 details on the PSF (Python Software Foundation) standard license, see:
14 details on the PSF (Python Software Foundation) standard license, see:
15
15
16 http://www.python.org/2.2.3/license.html"""
16 http://www.python.org/2.2.3/license.html"""
17
17
18 #*****************************************************************************
18 #*****************************************************************************
19 #
19 #
20 # This file is licensed under the PSF license.
20 # This file is licensed under the PSF license.
21 #
21 #
22 # Copyright (C) 2001 Python Software Foundation, www.python.org
22 # Copyright (C) 2001 Python Software Foundation, www.python.org
23 # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu>
23 # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu>
24 #
24 #
25 #
25 #
26 #*****************************************************************************
26 #*****************************************************************************
27
27
28 import bdb
28 import bdb
29 import linecache
29 import linecache
30 import sys
30 import sys
31
31
32 from IPython.utils import PyColorize
32 from IPython.utils import PyColorize
33 from IPython.core import ipapi
33 from IPython.core import ipapi
34 from IPython.utils import coloransi, io
34 from IPython.utils import coloransi, io
35 from IPython.core.excolors import exception_colors
35 from IPython.core.excolors import exception_colors
36
36
37 # See if we can use pydb.
37 # See if we can use pydb.
38 has_pydb = False
38 has_pydb = False
39 prompt = 'ipdb> '
39 prompt = 'ipdb> '
40 #We have to check this directly from sys.argv, config struct not yet available
40 #We have to check this directly from sys.argv, config struct not yet available
41 if '-pydb' in sys.argv:
41 if '-pydb' in sys.argv:
42 try:
42 try:
43 import pydb
43 import pydb
44 if hasattr(pydb.pydb, "runl") and pydb.version>'1.17':
44 if hasattr(pydb.pydb, "runl") and pydb.version>'1.17':
45 # Version 1.17 is broken, and that's what ships with Ubuntu Edgy, so we
45 # Version 1.17 is broken, and that's what ships with Ubuntu Edgy, so we
46 # better protect against it.
46 # better protect against it.
47 has_pydb = True
47 has_pydb = True
48 except ImportError:
48 except ImportError:
49 print "Pydb (http://bashdb.sourceforge.net/pydb/) does not seem to be available"
49 print "Pydb (http://bashdb.sourceforge.net/pydb/) does not seem to be available"
50
50
51 if has_pydb:
51 if has_pydb:
52 from pydb import Pdb as OldPdb
52 from pydb import Pdb as OldPdb
53 #print "Using pydb for %run -d and post-mortem" #dbg
53 #print "Using pydb for %run -d and post-mortem" #dbg
54 prompt = 'ipydb> '
54 prompt = 'ipydb> '
55 else:
55 else:
56 from pdb import Pdb as OldPdb
56 from pdb import Pdb as OldPdb
57
57
58 # Allow the set_trace code to operate outside of an ipython instance, even if
58 # Allow the set_trace code to operate outside of an ipython instance, even if
59 # it does so with some limitations. The rest of this support is implemented in
59 # it does so with some limitations. The rest of this support is implemented in
60 # the Tracer constructor.
60 # the Tracer constructor.
61 def BdbQuit_excepthook(et,ev,tb):
61 def BdbQuit_excepthook(et,ev,tb):
62 if et==bdb.BdbQuit:
62 if et==bdb.BdbQuit:
63 print 'Exiting Debugger.'
63 print 'Exiting Debugger.'
64 else:
64 else:
65 BdbQuit_excepthook.excepthook_ori(et,ev,tb)
65 BdbQuit_excepthook.excepthook_ori(et,ev,tb)
66
66
67 def BdbQuit_IPython_excepthook(self,et,ev,tb):
67 def BdbQuit_IPython_excepthook(self,et,ev,tb):
68 print 'Exiting Debugger.'
68 print 'Exiting Debugger.'
69
69
70
70
71 class Tracer(object):
71 class Tracer(object):
72 """Class for local debugging, similar to pdb.set_trace.
72 """Class for local debugging, similar to pdb.set_trace.
73
73
74 Instances of this class, when called, behave like pdb.set_trace, but
74 Instances of this class, when called, behave like pdb.set_trace, but
75 providing IPython's enhanced capabilities.
75 providing IPython's enhanced capabilities.
76
76
77 This is implemented as a class which must be initialized in your own code
77 This is implemented as a class which must be initialized in your own code
78 and not as a standalone function because we need to detect at runtime
78 and not as a standalone function because we need to detect at runtime
79 whether IPython is already active or not. That detection is done in the
79 whether IPython is already active or not. That detection is done in the
80 constructor, ensuring that this code plays nicely with a running IPython,
80 constructor, ensuring that this code plays nicely with a running IPython,
81 while functioning acceptably (though with limitations) if outside of it.
81 while functioning acceptably (though with limitations) if outside of it.
82 """
82 """
83
83
84 def __init__(self,colors=None):
84 def __init__(self,colors=None):
85 """Create a local debugger instance.
85 """Create a local debugger instance.
86
86
87 :Parameters:
87 :Parameters:
88
88
89 - `colors` (None): a string containing the name of the color scheme to
89 - `colors` (None): a string containing the name of the color scheme to
90 use, it must be one of IPython's valid color schemes. If not given, the
90 use, it must be one of IPython's valid color schemes. If not given, the
91 function will default to the current IPython scheme when running inside
91 function will default to the current IPython scheme when running inside
92 IPython, and to 'NoColor' otherwise.
92 IPython, and to 'NoColor' otherwise.
93
93
94 Usage example:
94 Usage example:
95
95
96 from IPython.core.debugger import Tracer; debug_here = Tracer()
96 from IPython.core.debugger import Tracer; debug_here = Tracer()
97
97
98 ... later in your code
98 ... later in your code
99 debug_here() # -> will open up the debugger at that point.
99 debug_here() # -> will open up the debugger at that point.
100
100
101 Once the debugger activates, you can use all of its regular commands to
101 Once the debugger activates, you can use all of its regular commands to
102 step through code, set breakpoints, etc. See the pdb documentation
102 step through code, set breakpoints, etc. See the pdb documentation
103 from the Python standard library for usage details.
103 from the Python standard library for usage details.
104 """
104 """
105
105
106 try:
106 try:
107 ip = ipapi.get()
107 ip = ipapi.get()
108 except:
108 except:
109 # Outside of ipython, we set our own exception hook manually
109 # Outside of ipython, we set our own exception hook manually
110 BdbQuit_excepthook.excepthook_ori = sys.excepthook
110 BdbQuit_excepthook.excepthook_ori = sys.excepthook
111 sys.excepthook = BdbQuit_excepthook
111 sys.excepthook = BdbQuit_excepthook
112 def_colors = 'NoColor'
112 def_colors = 'NoColor'
113 try:
113 try:
114 # Limited tab completion support
114 # Limited tab completion support
115 import readline
115 import readline
116 readline.parse_and_bind('tab: complete')
116 readline.parse_and_bind('tab: complete')
117 except ImportError:
117 except ImportError:
118 pass
118 pass
119 else:
119 else:
120 # In ipython, we use its custom exception handler mechanism
120 # In ipython, we use its custom exception handler mechanism
121 def_colors = ip.colors
121 def_colors = ip.colors
122 ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook)
122 ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook)
123
123
124 if colors is None:
124 if colors is None:
125 colors = def_colors
125 colors = def_colors
126 self.debugger = Pdb(colors)
126 self.debugger = Pdb(colors)
127
127
128 def __call__(self):
128 def __call__(self):
129 """Starts an interactive debugger at the point where called.
129 """Starts an interactive debugger at the point where called.
130
130
131 This is similar to the pdb.set_trace() function from the std lib, but
131 This is similar to the pdb.set_trace() function from the std lib, but
132 using IPython's enhanced debugger."""
132 using IPython's enhanced debugger."""
133
133
134 self.debugger.set_trace(sys._getframe().f_back)
134 self.debugger.set_trace(sys._getframe().f_back)
135
135
136
136
137 def decorate_fn_with_doc(new_fn, old_fn, additional_text=""):
137 def decorate_fn_with_doc(new_fn, old_fn, additional_text=""):
138 """Make new_fn have old_fn's doc string. This is particularly useful
138 """Make new_fn have old_fn's doc string. This is particularly useful
139 for the do_... commands that hook into the help system.
139 for the do_... commands that hook into the help system.
140 Adapted from from a comp.lang.python posting
140 Adapted from from a comp.lang.python posting
141 by Duncan Booth."""
141 by Duncan Booth."""
142 def wrapper(*args, **kw):
142 def wrapper(*args, **kw):
143 return new_fn(*args, **kw)
143 return new_fn(*args, **kw)
144 if old_fn.__doc__:
144 if old_fn.__doc__:
145 wrapper.__doc__ = old_fn.__doc__ + additional_text
145 wrapper.__doc__ = old_fn.__doc__ + additional_text
146 return wrapper
146 return wrapper
147
147
148
148
149 def _file_lines(fname):
149 def _file_lines(fname):
150 """Return the contents of a named file as a list of lines.
150 """Return the contents of a named file as a list of lines.
151
151
152 This function never raises an IOError exception: if the file can't be
152 This function never raises an IOError exception: if the file can't be
153 read, it simply returns an empty list."""
153 read, it simply returns an empty list."""
154
154
155 try:
155 try:
156 outfile = open(fname)
156 outfile = open(fname)
157 except IOError:
157 except IOError:
158 return []
158 return []
159 else:
159 else:
160 out = outfile.readlines()
160 out = outfile.readlines()
161 outfile.close()
161 outfile.close()
162 return out
162 return out
163
163
164
164
165 class Pdb(OldPdb):
165 class Pdb(OldPdb):
166 """Modified Pdb class, does not load readline."""
166 """Modified Pdb class, does not load readline."""
167
167
168 def __init__(self,color_scheme='NoColor',completekey=None,
168 def __init__(self,color_scheme='NoColor',completekey=None,
169 stdin=None, stdout=None):
169 stdin=None, stdout=None):
170
170
171 # Parent constructor:
171 # Parent constructor:
172 if has_pydb and completekey is None:
172 if has_pydb and completekey is None:
173 OldPdb.__init__(self,stdin=stdin,stdout=io.stdout)
173 OldPdb.__init__(self,stdin=stdin,stdout=io.stdout)
174 else:
174 else:
175 OldPdb.__init__(self,completekey,stdin,stdout)
175 OldPdb.__init__(self,completekey,stdin,stdout)
176
176
177 self.prompt = prompt # The default prompt is '(Pdb)'
177 self.prompt = prompt # The default prompt is '(Pdb)'
178
178
179 # IPython changes...
179 # IPython changes...
180 self.is_pydb = has_pydb
180 self.is_pydb = has_pydb
181
181
182 self.shell = ipapi.get()
182 self.shell = ipapi.get()
183
183
184 if self.is_pydb:
184 if self.is_pydb:
185
185
186 # interactiveshell.py's ipalias seems to want pdb's checkline
186 # interactiveshell.py's ipalias seems to want pdb's checkline
187 # which located in pydb.fn
187 # which located in pydb.fn
188 import pydb.fns
188 import pydb.fns
189 self.checkline = lambda filename, lineno: \
189 self.checkline = lambda filename, lineno: \
190 pydb.fns.checkline(self, filename, lineno)
190 pydb.fns.checkline(self, filename, lineno)
191
191
192 self.curframe = None
192 self.curframe = None
193 self.do_restart = self.new_do_restart
193 self.do_restart = self.new_do_restart
194
194
195 self.old_all_completions = self.shell.Completer.all_completions
195 self.old_all_completions = self.shell.Completer.all_completions
196 self.shell.Completer.all_completions=self.all_completions
196 self.shell.Completer.all_completions=self.all_completions
197
197
198 self.do_list = decorate_fn_with_doc(self.list_command_pydb,
198 self.do_list = decorate_fn_with_doc(self.list_command_pydb,
199 OldPdb.do_list)
199 OldPdb.do_list)
200 self.do_l = self.do_list
200 self.do_l = self.do_list
201 self.do_frame = decorate_fn_with_doc(self.new_do_frame,
201 self.do_frame = decorate_fn_with_doc(self.new_do_frame,
202 OldPdb.do_frame)
202 OldPdb.do_frame)
203
203
204 self.aliases = {}
204 self.aliases = {}
205
205
206 # Create color table: we copy the default one from the traceback
206 # Create color table: we copy the default one from the traceback
207 # module and add a few attributes needed for debugging
207 # module and add a few attributes needed for debugging
208 self.color_scheme_table = exception_colors()
208 self.color_scheme_table = exception_colors()
209
209
210 # shorthands
210 # shorthands
211 C = coloransi.TermColors
211 C = coloransi.TermColors
212 cst = self.color_scheme_table
212 cst = self.color_scheme_table
213
213
214 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
214 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
215 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
215 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
216
216
217 cst['Linux'].colors.breakpoint_enabled = C.LightRed
217 cst['Linux'].colors.breakpoint_enabled = C.LightRed
218 cst['Linux'].colors.breakpoint_disabled = C.Red
218 cst['Linux'].colors.breakpoint_disabled = C.Red
219
219
220 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
220 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
221 cst['LightBG'].colors.breakpoint_disabled = C.Red
221 cst['LightBG'].colors.breakpoint_disabled = C.Red
222
222
223 self.set_colors(color_scheme)
223 self.set_colors(color_scheme)
224
224
225 # Add a python parser so we can syntax highlight source while
225 # Add a python parser so we can syntax highlight source while
226 # debugging.
226 # debugging.
227 self.parser = PyColorize.Parser()
227 self.parser = PyColorize.Parser()
228
228
229 def set_colors(self, scheme):
229 def set_colors(self, scheme):
230 """Shorthand access to the color table scheme selector method."""
230 """Shorthand access to the color table scheme selector method."""
231 self.color_scheme_table.set_active_scheme(scheme)
231 self.color_scheme_table.set_active_scheme(scheme)
232
232
233 def interaction(self, frame, traceback):
233 def interaction(self, frame, traceback):
234 self.shell.set_completer_frame(frame)
234 self.shell.set_completer_frame(frame)
235 OldPdb.interaction(self, frame, traceback)
235 OldPdb.interaction(self, frame, traceback)
236
236
237 def new_do_up(self, arg):
237 def new_do_up(self, arg):
238 OldPdb.do_up(self, arg)
238 OldPdb.do_up(self, arg)
239 self.shell.set_completer_frame(self.curframe)
239 self.shell.set_completer_frame(self.curframe)
240 do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up)
240 do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up)
241
241
242 def new_do_down(self, arg):
242 def new_do_down(self, arg):
243 OldPdb.do_down(self, arg)
243 OldPdb.do_down(self, arg)
244 self.shell.set_completer_frame(self.curframe)
244 self.shell.set_completer_frame(self.curframe)
245
245
246 do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down)
246 do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down)
247
247
248 def new_do_frame(self, arg):
248 def new_do_frame(self, arg):
249 OldPdb.do_frame(self, arg)
249 OldPdb.do_frame(self, arg)
250 self.shell.set_completer_frame(self.curframe)
250 self.shell.set_completer_frame(self.curframe)
251
251
252 def new_do_quit(self, arg):
252 def new_do_quit(self, arg):
253
253
254 if hasattr(self, 'old_all_completions'):
254 if hasattr(self, 'old_all_completions'):
255 self.shell.Completer.all_completions=self.old_all_completions
255 self.shell.Completer.all_completions=self.old_all_completions
256
256
257
257
258 return OldPdb.do_quit(self, arg)
258 return OldPdb.do_quit(self, arg)
259
259
260 do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit)
260 do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit)
261
261
262 def new_do_restart(self, arg):
262 def new_do_restart(self, arg):
263 """Restart command. In the context of ipython this is exactly the same
263 """Restart command. In the context of ipython this is exactly the same
264 thing as 'quit'."""
264 thing as 'quit'."""
265 self.msg("Restart doesn't make sense here. Using 'quit' instead.")
265 self.msg("Restart doesn't make sense here. Using 'quit' instead.")
266 return self.do_quit(arg)
266 return self.do_quit(arg)
267
267
268 def postloop(self):
268 def postloop(self):
269 self.shell.set_completer_frame(None)
269 self.shell.set_completer_frame(None)
270
270
271 def print_stack_trace(self):
271 def print_stack_trace(self):
272 try:
272 try:
273 for frame_lineno in self.stack:
273 for frame_lineno in self.stack:
274 self.print_stack_entry(frame_lineno, context = 5)
274 self.print_stack_entry(frame_lineno, context = 5)
275 except KeyboardInterrupt:
275 except KeyboardInterrupt:
276 pass
276 pass
277
277
278 def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ',
278 def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ',
279 context = 3):
279 context = 3):
280 #frame, lineno = frame_lineno
280 #frame, lineno = frame_lineno
281 print >>io.stdout, self.format_stack_entry(frame_lineno, '', context)
281 print >>io.stdout, self.format_stack_entry(frame_lineno, '', context)
282
282
283 # vds: >>
283 # vds: >>
284 frame, lineno = frame_lineno
284 frame, lineno = frame_lineno
285 filename = frame.f_code.co_filename
285 filename = frame.f_code.co_filename
286 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
286 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
287 # vds: <<
287 # vds: <<
288
288
289 def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
289 def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
290 import linecache, repr
290 import linecache, repr
291
291
292 ret = []
292 ret = []
293
293
294 Colors = self.color_scheme_table.active_colors
294 Colors = self.color_scheme_table.active_colors
295 ColorsNormal = Colors.Normal
295 ColorsNormal = Colors.Normal
296 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
296 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
297 tpl_call = '%s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal)
297 tpl_call = '%s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal)
298 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
298 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
299 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line,
299 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line,
300 ColorsNormal)
300 ColorsNormal)
301
301
302 frame, lineno = frame_lineno
302 frame, lineno = frame_lineno
303
303
304 return_value = ''
304 return_value = ''
305 if '__return__' in frame.f_locals:
305 if '__return__' in frame.f_locals:
306 rv = frame.f_locals['__return__']
306 rv = frame.f_locals['__return__']
307 #return_value += '->'
307 #return_value += '->'
308 return_value += repr.repr(rv) + '\n'
308 return_value += repr.repr(rv) + '\n'
309 ret.append(return_value)
309 ret.append(return_value)
310
310
311 #s = filename + '(' + `lineno` + ')'
311 #s = filename + '(' + `lineno` + ')'
312 filename = self.canonic(frame.f_code.co_filename)
312 filename = self.canonic(frame.f_code.co_filename)
313 link = tpl_link % filename
313 link = tpl_link % filename
314
314
315 if frame.f_code.co_name:
315 if frame.f_code.co_name:
316 func = frame.f_code.co_name
316 func = frame.f_code.co_name
317 else:
317 else:
318 func = "<lambda>"
318 func = "<lambda>"
319
319
320 call = ''
320 call = ''
321 if func != '?':
321 if func != '?':
322 if '__args__' in frame.f_locals:
322 if '__args__' in frame.f_locals:
323 args = repr.repr(frame.f_locals['__args__'])
323 args = repr.repr(frame.f_locals['__args__'])
324 else:
324 else:
325 args = '()'
325 args = '()'
326 call = tpl_call % (func, args)
326 call = tpl_call % (func, args)
327
327
328 # The level info should be generated in the same format pdb uses, to
328 # The level info should be generated in the same format pdb uses, to
329 # avoid breaking the pdbtrack functionality of python-mode in *emacs.
329 # avoid breaking the pdbtrack functionality of python-mode in *emacs.
330 if frame is self.curframe:
330 if frame is self.curframe:
331 ret.append('> ')
331 ret.append('> ')
332 else:
332 else:
333 ret.append(' ')
333 ret.append(' ')
334 ret.append('%s(%s)%s\n' % (link,lineno,call))
334 ret.append('%s(%s)%s\n' % (link,lineno,call))
335
335
336 start = lineno - 1 - context//2
336 start = lineno - 1 - context//2
337 lines = linecache.getlines(filename)
337 lines = linecache.getlines(filename)
338 start = max(start, 0)
338 start = max(start, 0)
339 start = min(start, len(lines) - context)
339 start = min(start, len(lines) - context)
340 lines = lines[start : start + context]
340 lines = lines[start : start + context]
341
341
342 for i,line in enumerate(lines):
342 for i,line in enumerate(lines):
343 show_arrow = (start + 1 + i == lineno)
343 show_arrow = (start + 1 + i == lineno)
344 linetpl = (frame is self.curframe or show_arrow) \
344 linetpl = (frame is self.curframe or show_arrow) \
345 and tpl_line_em \
345 and tpl_line_em \
346 or tpl_line
346 or tpl_line
347 ret.append(self.__format_line(linetpl, filename,
347 ret.append(self.__format_line(linetpl, filename,
348 start + 1 + i, line,
348 start + 1 + i, line,
349 arrow = show_arrow) )
349 arrow = show_arrow) )
350
350
351 return ''.join(ret)
351 return ''.join(ret)
352
352
353 def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
353 def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
354 bp_mark = ""
354 bp_mark = ""
355 bp_mark_color = ""
355 bp_mark_color = ""
356
356
357 scheme = self.color_scheme_table.active_scheme_name
357 scheme = self.color_scheme_table.active_scheme_name
358 new_line, err = self.parser.format2(line, 'str', scheme)
358 new_line, err = self.parser.format2(line, 'str', scheme)
359 if not err: line = new_line
359 if not err: line = new_line
360
360
361 bp = None
361 bp = None
362 if lineno in self.get_file_breaks(filename):
362 if lineno in self.get_file_breaks(filename):
363 bps = self.get_breaks(filename, lineno)
363 bps = self.get_breaks(filename, lineno)
364 bp = bps[-1]
364 bp = bps[-1]
365
365
366 if bp:
366 if bp:
367 Colors = self.color_scheme_table.active_colors
367 Colors = self.color_scheme_table.active_colors
368 bp_mark = str(bp.number)
368 bp_mark = str(bp.number)
369 bp_mark_color = Colors.breakpoint_enabled
369 bp_mark_color = Colors.breakpoint_enabled
370 if not bp.enabled:
370 if not bp.enabled:
371 bp_mark_color = Colors.breakpoint_disabled
371 bp_mark_color = Colors.breakpoint_disabled
372
372
373 numbers_width = 7
373 numbers_width = 7
374 if arrow:
374 if arrow:
375 # This is the line with the error
375 # This is the line with the error
376 pad = numbers_width - len(str(lineno)) - len(bp_mark)
376 pad = numbers_width - len(str(lineno)) - len(bp_mark)
377 if pad >= 3:
377 if pad >= 3:
378 marker = '-'*(pad-3) + '-> '
378 marker = '-'*(pad-3) + '-> '
379 elif pad == 2:
379 elif pad == 2:
380 marker = '> '
380 marker = '> '
381 elif pad == 1:
381 elif pad == 1:
382 marker = '>'
382 marker = '>'
383 else:
383 else:
384 marker = ''
384 marker = ''
385 num = '%s%s' % (marker, str(lineno))
385 num = '%s%s' % (marker, str(lineno))
386 line = tpl_line % (bp_mark_color + bp_mark, num, line)
386 line = tpl_line % (bp_mark_color + bp_mark, num, line)
387 else:
387 else:
388 num = '%*s' % (numbers_width - len(bp_mark), str(lineno))
388 num = '%*s' % (numbers_width - len(bp_mark), str(lineno))
389 line = tpl_line % (bp_mark_color + bp_mark, num, line)
389 line = tpl_line % (bp_mark_color + bp_mark, num, line)
390
390
391 return line
391 return line
392
392
393 def list_command_pydb(self, arg):
393 def list_command_pydb(self, arg):
394 """List command to use if we have a newer pydb installed"""
394 """List command to use if we have a newer pydb installed"""
395 filename, first, last = OldPdb.parse_list_cmd(self, arg)
395 filename, first, last = OldPdb.parse_list_cmd(self, arg)
396 if filename is not None:
396 if filename is not None:
397 self.print_list_lines(filename, first, last)
397 self.print_list_lines(filename, first, last)
398
398
399 def print_list_lines(self, filename, first, last):
399 def print_list_lines(self, filename, first, last):
400 """The printing (as opposed to the parsing part of a 'list'
400 """The printing (as opposed to the parsing part of a 'list'
401 command."""
401 command."""
402 try:
402 try:
403 Colors = self.color_scheme_table.active_colors
403 Colors = self.color_scheme_table.active_colors
404 ColorsNormal = Colors.Normal
404 ColorsNormal = Colors.Normal
405 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
405 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
406 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
406 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
407 src = []
407 src = []
408 for lineno in range(first, last+1):
408 for lineno in range(first, last+1):
409 line = linecache.getline(filename, lineno)
409 line = linecache.getline(filename, lineno)
410 if not line:
410 if not line:
411 break
411 break
412
412
413 if lineno == self.curframe.f_lineno:
413 if lineno == self.curframe.f_lineno:
414 line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True)
414 line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True)
415 else:
415 else:
416 line = self.__format_line(tpl_line, filename, lineno, line, arrow = False)
416 line = self.__format_line(tpl_line, filename, lineno, line, arrow = False)
417
417
418 src.append(line)
418 src.append(line)
419 self.lineno = lineno
419 self.lineno = lineno
420
420
421 print >>io.stdout, ''.join(src)
421 print >>io.stdout, ''.join(src)
422
422
423 except KeyboardInterrupt:
423 except KeyboardInterrupt:
424 pass
424 pass
425
425
426 def do_list(self, arg):
426 def do_list(self, arg):
427 self.lastcmd = 'list'
427 self.lastcmd = 'list'
428 last = None
428 last = None
429 if arg:
429 if arg:
430 try:
430 try:
431 x = eval(arg, {}, {})
431 x = eval(arg, {}, {})
432 if type(x) == type(()):
432 if type(x) == type(()):
433 first, last = x
433 first, last = x
434 first = int(first)
434 first = int(first)
435 last = int(last)
435 last = int(last)
436 if last < first:
436 if last < first:
437 # Assume it's a count
437 # Assume it's a count
438 last = first + last
438 last = first + last
439 else:
439 else:
440 first = max(1, int(x) - 5)
440 first = max(1, int(x) - 5)
441 except:
441 except:
442 print '*** Error in argument:', `arg`
442 print '*** Error in argument:', `arg`
443 return
443 return
444 elif self.lineno is None:
444 elif self.lineno is None:
445 first = max(1, self.curframe.f_lineno - 5)
445 first = max(1, self.curframe.f_lineno - 5)
446 else:
446 else:
447 first = self.lineno + 1
447 first = self.lineno + 1
448 if last is None:
448 if last is None:
449 last = first + 10
449 last = first + 10
450 self.print_list_lines(self.curframe.f_code.co_filename, first, last)
450 self.print_list_lines(self.curframe.f_code.co_filename, first, last)
451
451
452 # vds: >>
452 # vds: >>
453 lineno = first
453 lineno = first
454 filename = self.curframe.f_code.co_filename
454 filename = self.curframe.f_code.co_filename
455 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
455 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
456 # vds: <<
456 # vds: <<
457
457
458 do_l = do_list
458 do_l = do_list
459
459
460 def do_pdef(self, arg):
460 def do_pdef(self, arg):
461 """The debugger interface to magic_pdef"""
461 """The debugger interface to magic_pdef"""
462 namespaces = [('Locals', self.curframe.f_locals),
462 namespaces = [('Locals', self.curframe.f_locals),
463 ('Globals', self.curframe.f_globals)]
463 ('Globals', self.curframe.f_globals)]
464 self.shell.magic_pdef(arg, namespaces=namespaces)
464 self.shell.magic_pdef(arg, namespaces=namespaces)
465
465
466 def do_pdoc(self, arg):
466 def do_pdoc(self, arg):
467 """The debugger interface to magic_pdoc"""
467 """The debugger interface to magic_pdoc"""
468 namespaces = [('Locals', self.curframe.f_locals),
468 namespaces = [('Locals', self.curframe.f_locals),
469 ('Globals', self.curframe.f_globals)]
469 ('Globals', self.curframe.f_globals)]
470 self.shell.magic_pdoc(arg, namespaces=namespaces)
470 self.shell.magic_pdoc(arg, namespaces=namespaces)
471
471
472 def do_pinfo(self, arg):
472 def do_pinfo(self, arg):
473 """The debugger equivalant of ?obj"""
473 """The debugger equivalant of ?obj"""
474 namespaces = [('Locals', self.curframe.f_locals),
474 namespaces = [('Locals', self.curframe.f_locals),
475 ('Globals', self.curframe.f_globals)]
475 ('Globals', self.curframe.f_globals)]
476 self.shell.magic_pinfo("pinfo %s" % arg, namespaces=namespaces)
476 self.shell.magic_pinfo("pinfo %s" % arg, namespaces=namespaces)
477
477
478 def checkline(self, filename, lineno):
478 def checkline(self, filename, lineno):
479 """Check whether specified line seems to be executable.
479 """Check whether specified line seems to be executable.
480
480
481 Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
481 Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
482 line or EOF). Warning: testing is not comprehensive.
482 line or EOF). Warning: testing is not comprehensive.
483 """
483 """
484 #######################################################################
484 #######################################################################
485 # XXX Hack! Use python-2.5 compatible code for this call, because with
485 # XXX Hack! Use python-2.5 compatible code for this call, because with
486 # all of our changes, we've drifted from the pdb api in 2.6. For now,
486 # all of our changes, we've drifted from the pdb api in 2.6. For now,
487 # changing:
487 # changing:
488 #
488 #
489 #line = linecache.getline(filename, lineno, self.curframe.f_globals)
489 #line = linecache.getline(filename, lineno, self.curframe.f_globals)
490 # to:
490 # to:
491 #
491 #
492 line = linecache.getline(filename, lineno)
492 line = linecache.getline(filename, lineno)
493 #
493 #
494 # does the trick. But in reality, we need to fix this by reconciling
494 # does the trick. But in reality, we need to fix this by reconciling
495 # our updates with the new Pdb APIs in Python 2.6.
495 # our updates with the new Pdb APIs in Python 2.6.
496 #
496 #
497 # End hack. The rest of this method is copied verbatim from 2.6 pdb.py
497 # End hack. The rest of this method is copied verbatim from 2.6 pdb.py
498 #######################################################################
498 #######################################################################
499
499
500 if not line:
500 if not line:
501 print >>self.stdout, 'End of file'
501 print >>self.stdout, 'End of file'
502 return 0
502 return 0
503 line = line.strip()
503 line = line.strip()
504 # Don't allow setting breakpoint at a blank line
504 # Don't allow setting breakpoint at a blank line
505 if (not line or (line[0] == '#') or
505 if (not line or (line[0] == '#') or
506 (line[:3] == '"""') or line[:3] == "'''"):
506 (line[:3] == '"""') or line[:3] == "'''"):
507 print >>self.stdout, '*** Blank or comment'
507 print >>self.stdout, '*** Blank or comment'
508 return 0
508 return 0
509 return lineno
509 return lineno
@@ -1,383 +1,383 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Top-level display functions for displaying object in different formats.
2 """Top-level display functions for displaying object in different formats.
3
3
4 Authors:
4 Authors:
5
5
6 * Brian Granger
6 * Brian Granger
7 """
7 """
8
8
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10 # Copyright (C) 2008-2010 The IPython Development Team
10 # Copyright (C) 2008-2010 The IPython Development Team
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Imports
17 # Imports
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 from .displaypub import (
20 from .displaypub import (
21 publish_pretty, publish_html,
21 publish_pretty, publish_html,
22 publish_latex, publish_svg,
22 publish_latex, publish_svg,
23 publish_png, publish_json,
23 publish_png, publish_json,
24 publish_javascript, publish_jpeg
24 publish_javascript, publish_jpeg
25 )
25 )
26
26
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 # Main functions
28 # Main functions
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30
30
31 def display(*objs, **kwargs):
31 def display(*objs, **kwargs):
32 """Display a Python object in all frontends.
32 """Display a Python object in all frontends.
33
33
34 By default all representations will be computed and sent to the frontends.
34 By default all representations will be computed and sent to the frontends.
35 Frontends can decide which representation is used and how.
35 Frontends can decide which representation is used and how.
36
36
37 Parameters
37 Parameters
38 ----------
38 ----------
39 objs : tuple of objects
39 objs : tuple of objects
40 The Python objects to display.
40 The Python objects to display.
41 include : list or tuple, optional
41 include : list or tuple, optional
42 A list of format type strings (MIME types) to include in the
42 A list of format type strings (MIME types) to include in the
43 format data dict. If this is set *only* the format types included
43 format data dict. If this is set *only* the format types included
44 in this list will be computed.
44 in this list will be computed.
45 exclude : list or tuple, optional
45 exclude : list or tuple, optional
46 A list of format type string (MIME types) to exclue in the format
46 A list of format type string (MIME types) to exclue in the format
47 data dict. If this is set all format types will be computed,
47 data dict. If this is set all format types will be computed,
48 except for those included in this argument.
48 except for those included in this argument.
49 """
49 """
50 include = kwargs.get('include')
50 include = kwargs.get('include')
51 exclude = kwargs.get('exclude')
51 exclude = kwargs.get('exclude')
52
52
53 from IPython.core.interactiveshell import InteractiveShell
53 from IPython.core.interactiveshell import InteractiveShell
54 inst = InteractiveShell.instance()
54 inst = InteractiveShell.instance()
55 format = inst.display_formatter.format
55 format = inst.display_formatter.format
56 publish = inst.display_pub.publish
56 publish = inst.display_pub.publish
57
57
58 for obj in objs:
58 for obj in objs:
59 format_dict = format(obj, include=include, exclude=exclude)
59 format_dict = format(obj, include=include, exclude=exclude)
60 publish('IPython.core.display.display', format_dict)
60 publish('IPython.core.display.display', format_dict)
61
61
62
62
63 def display_pretty(*objs, **kwargs):
63 def display_pretty(*objs, **kwargs):
64 """Display the pretty (default) representation of an object.
64 """Display the pretty (default) representation of an object.
65
65
66 Parameters
66 Parameters
67 ----------
67 ----------
68 objs : tuple of objects
68 objs : tuple of objects
69 The Python objects to display, or if raw=True raw text data to
69 The Python objects to display, or if raw=True raw text data to
70 display.
70 display.
71 raw : bool
71 raw : bool
72 Are the data objects raw data or Python objects that need to be
72 Are the data objects raw data or Python objects that need to be
73 formatted before display? [default: False]
73 formatted before display? [default: False]
74 """
74 """
75 raw = kwargs.pop('raw',False)
75 raw = kwargs.pop('raw',False)
76 if raw:
76 if raw:
77 for obj in objs:
77 for obj in objs:
78 publish_pretty(obj)
78 publish_pretty(obj)
79 else:
79 else:
80 display(*objs, include=['text/plain'])
80 display(*objs, include=['text/plain'])
81
81
82
82
83 def display_html(*objs, **kwargs):
83 def display_html(*objs, **kwargs):
84 """Display the HTML representation of an object.
84 """Display the HTML representation of an object.
85
85
86 Parameters
86 Parameters
87 ----------
87 ----------
88 objs : tuple of objects
88 objs : tuple of objects
89 The Python objects to display, or if raw=True raw HTML data to
89 The Python objects to display, or if raw=True raw HTML data to
90 display.
90 display.
91 raw : bool
91 raw : bool
92 Are the data objects raw data or Python objects that need to be
92 Are the data objects raw data or Python objects that need to be
93 formatted before display? [default: False]
93 formatted before display? [default: False]
94 """
94 """
95 raw = kwargs.pop('raw',False)
95 raw = kwargs.pop('raw',False)
96 if raw:
96 if raw:
97 for obj in objs:
97 for obj in objs:
98 publish_html(obj)
98 publish_html(obj)
99 else:
99 else:
100 display(*objs, include=['text/plain','text/html'])
100 display(*objs, include=['text/plain','text/html'])
101
101
102
102
103 def display_svg(*objs, **kwargs):
103 def display_svg(*objs, **kwargs):
104 """Display the SVG representation of an object.
104 """Display the SVG representation of an object.
105
105
106 Parameters
106 Parameters
107 ----------
107 ----------
108 objs : tuple of objects
108 objs : tuple of objects
109 The Python objects to display, or if raw=True raw svg data to
109 The Python objects to display, or if raw=True raw svg data to
110 display.
110 display.
111 raw : bool
111 raw : bool
112 Are the data objects raw data or Python objects that need to be
112 Are the data objects raw data or Python objects that need to be
113 formatted before display? [default: False]
113 formatted before display? [default: False]
114 """
114 """
115 raw = kwargs.pop('raw',False)
115 raw = kwargs.pop('raw',False)
116 if raw:
116 if raw:
117 for obj in objs:
117 for obj in objs:
118 publish_svg(obj)
118 publish_svg(obj)
119 else:
119 else:
120 display(*objs, include=['text/plain','image/svg+xml'])
120 display(*objs, include=['text/plain','image/svg+xml'])
121
121
122
122
123 def display_png(*objs, **kwargs):
123 def display_png(*objs, **kwargs):
124 """Display the PNG representation of an object.
124 """Display the PNG representation of an object.
125
125
126 Parameters
126 Parameters
127 ----------
127 ----------
128 objs : tuple of objects
128 objs : tuple of objects
129 The Python objects to display, or if raw=True raw png data to
129 The Python objects to display, or if raw=True raw png data to
130 display.
130 display.
131 raw : bool
131 raw : bool
132 Are the data objects raw data or Python objects that need to be
132 Are the data objects raw data or Python objects that need to be
133 formatted before display? [default: False]
133 formatted before display? [default: False]
134 """
134 """
135 raw = kwargs.pop('raw',False)
135 raw = kwargs.pop('raw',False)
136 if raw:
136 if raw:
137 for obj in objs:
137 for obj in objs:
138 publish_png(obj)
138 publish_png(obj)
139 else:
139 else:
140 display(*objs, include=['text/plain','image/png'])
140 display(*objs, include=['text/plain','image/png'])
141
141
142
142
143 def display_jpeg(*objs, **kwargs):
143 def display_jpeg(*objs, **kwargs):
144 """Display the JPEG representation of an object.
144 """Display the JPEG representation of an object.
145
145
146 Parameters
146 Parameters
147 ----------
147 ----------
148 objs : tuple of objects
148 objs : tuple of objects
149 The Python objects to display, or if raw=True raw JPEG data to
149 The Python objects to display, or if raw=True raw JPEG data to
150 display.
150 display.
151 raw : bool
151 raw : bool
152 Are the data objects raw data or Python objects that need to be
152 Are the data objects raw data or Python objects that need to be
153 formatted before display? [default: False]
153 formatted before display? [default: False]
154 """
154 """
155 raw = kwargs.pop('raw',False)
155 raw = kwargs.pop('raw',False)
156 if raw:
156 if raw:
157 for obj in objs:
157 for obj in objs:
158 publish_jpeg(obj)
158 publish_jpeg(obj)
159 else:
159 else:
160 display(*objs, include=['text/plain','image/jpeg'])
160 display(*objs, include=['text/plain','image/jpeg'])
161
161
162
162
163 def display_latex(*objs, **kwargs):
163 def display_latex(*objs, **kwargs):
164 """Display the LaTeX representation of an object.
164 """Display the LaTeX representation of an object.
165
165
166 Parameters
166 Parameters
167 ----------
167 ----------
168 objs : tuple of objects
168 objs : tuple of objects
169 The Python objects to display, or if raw=True raw latex data to
169 The Python objects to display, or if raw=True raw latex data to
170 display.
170 display.
171 raw : bool
171 raw : bool
172 Are the data objects raw data or Python objects that need to be
172 Are the data objects raw data or Python objects that need to be
173 formatted before display? [default: False]
173 formatted before display? [default: False]
174 """
174 """
175 raw = kwargs.pop('raw',False)
175 raw = kwargs.pop('raw',False)
176 if raw:
176 if raw:
177 for obj in objs:
177 for obj in objs:
178 publish_latex(obj)
178 publish_latex(obj)
179 else:
179 else:
180 display(*objs, include=['text/plain','text/latex'])
180 display(*objs, include=['text/plain','text/latex'])
181
181
182
182
183 def display_json(*objs, **kwargs):
183 def display_json(*objs, **kwargs):
184 """Display the JSON representation of an object.
184 """Display the JSON representation of an object.
185
185
186 Parameters
186 Parameters
187 ----------
187 ----------
188 objs : tuple of objects
188 objs : tuple of objects
189 The Python objects to display, or if raw=True raw json data to
189 The Python objects to display, or if raw=True raw json data to
190 display.
190 display.
191 raw : bool
191 raw : bool
192 Are the data objects raw data or Python objects that need to be
192 Are the data objects raw data or Python objects that need to be
193 formatted before display? [default: False]
193 formatted before display? [default: False]
194 """
194 """
195 raw = kwargs.pop('raw',False)
195 raw = kwargs.pop('raw',False)
196 if raw:
196 if raw:
197 for obj in objs:
197 for obj in objs:
198 publish_json(obj)
198 publish_json(obj)
199 else:
199 else:
200 display(*objs, include=['text/plain','application/json'])
200 display(*objs, include=['text/plain','application/json'])
201
201
202
202
203 def display_javascript(*objs, **kwargs):
203 def display_javascript(*objs, **kwargs):
204 """Display the Javascript representation of an object.
204 """Display the Javascript representation of an object.
205
205
206 Parameters
206 Parameters
207 ----------
207 ----------
208 objs : tuple of objects
208 objs : tuple of objects
209 The Python objects to display, or if raw=True raw javascript data to
209 The Python objects to display, or if raw=True raw javascript data to
210 display.
210 display.
211 raw : bool
211 raw : bool
212 Are the data objects raw data or Python objects that need to be
212 Are the data objects raw data or Python objects that need to be
213 formatted before display? [default: False]
213 formatted before display? [default: False]
214 """
214 """
215 raw = kwargs.pop('raw',False)
215 raw = kwargs.pop('raw',False)
216 if raw:
216 if raw:
217 for obj in objs:
217 for obj in objs:
218 publish_javascript(obj)
218 publish_javascript(obj)
219 else:
219 else:
220 display(*objs, include=['text/plain','application/javascript'])
220 display(*objs, include=['text/plain','application/javascript'])
221
221
222 #-----------------------------------------------------------------------------
222 #-----------------------------------------------------------------------------
223 # Smart classes
223 # Smart classes
224 #-----------------------------------------------------------------------------
224 #-----------------------------------------------------------------------------
225
225
226
226
227 class DisplayObject(object):
227 class DisplayObject(object):
228 """An object that wraps data to be displayed."""
228 """An object that wraps data to be displayed."""
229
229
230 _read_flags = 'r'
230 _read_flags = 'r'
231
231
232 def __init__(self, data=None, url=None, filename=None):
232 def __init__(self, data=None, url=None, filename=None):
233 """Create a display object given raw data.
233 """Create a display object given raw data.
234
234
235 When this object is returned by an expression or passed to the
235 When this object is returned by an expression or passed to the
236 display function, it will result in the data being displayed
236 display function, it will result in the data being displayed
237 in the frontend. The MIME type of the data should match the
237 in the frontend. The MIME type of the data should match the
238 subclasses used, so the Png subclass should be used for 'image/png'
238 subclasses used, so the Png subclass should be used for 'image/png'
239 data. If the data is a URL, the data will first be downloaded
239 data. If the data is a URL, the data will first be downloaded
240 and then displayed. If
240 and then displayed. If
241
241
242 Parameters
242 Parameters
243 ----------
243 ----------
244 data : unicode, str or bytes
244 data : unicode, str or bytes
245 The raw data or a URL to download the data from.
245 The raw data or a URL to download the data from.
246 url : unicode
246 url : unicode
247 A URL to download the data from.
247 A URL to download the data from.
248 filename : unicode
248 filename : unicode
249 Path to a local file to load the data from.
249 Path to a local file to load the data from.
250 """
250 """
251 if data is not None and data.startswith('http'):
251 if data is not None and data.startswith('http'):
252 self.url = data
252 self.url = data
253 self.filename = None
253 self.filename = None
254 self.data = None
254 self.data = None
255 else:
255 else:
256 self.data = data
256 self.data = data
257 self.url = url
257 self.url = url
258 self.filename = None if filename is None else unicode(filename)
258 self.filename = None if filename is None else unicode(filename)
259 self.reload()
259 self.reload()
260
260
261 def reload(self):
261 def reload(self):
262 """Reload the raw data from file or URL."""
262 """Reload the raw data from file or URL."""
263 if self.filename is not None:
263 if self.filename is not None:
264 with open(self.filename, self._read_flags) as f:
264 with open(self.filename, self._read_flags) as f:
265 self.data = f.read()
265 self.data = f.read()
266 elif self.url is not None:
266 elif self.url is not None:
267 try:
267 try:
268 import urllib2
268 import urllib2
269 response = urllib2.urlopen(self.url)
269 response = urllib2.urlopen(self.url)
270 self.data = response.read()
270 self.data = response.read()
271 # extract encoding from header, if there is one:
271 # extract encoding from header, if there is one:
272 encoding = None
272 encoding = None
273 for sub in response.headers['content-type'].split(';'):
273 for sub in response.headers['content-type'].split(';'):
274 sub = sub.strip()
274 sub = sub.strip()
275 if sub.startswith('charset'):
275 if sub.startswith('charset'):
276 encoding = sub.split('=')[-1].strip()
276 encoding = sub.split('=')[-1].strip()
277 break
277 break
278 # decode data, if an encoding was specified
278 # decode data, if an encoding was specified
279 if encoding:
279 if encoding:
280 self.data = self.data.decode(encoding, 'replace')
280 self.data = self.data.decode(encoding, 'replace')
281 except:
281 except:
282 self.data = None
282 self.data = None
283
283
284 class Pretty(DisplayObject):
284 class Pretty(DisplayObject):
285
285
286 def _repr_pretty_(self):
286 def _repr_pretty_(self):
287 return self.data
287 return self.data
288
288
289
289
290 class HTML(DisplayObject):
290 class HTML(DisplayObject):
291
291
292 def _repr_html_(self):
292 def _repr_html_(self):
293 return self.data
293 return self.data
294
294
295
295
296 class Math(DisplayObject):
296 class Math(DisplayObject):
297
297
298 def _repr_latex_(self):
298 def _repr_latex_(self):
299 return self.data
299 return self.data
300
300
301
301
302 class SVG(DisplayObject):
302 class SVG(DisplayObject):
303
303
304 def _repr_svg_(self):
304 def _repr_svg_(self):
305 return self.data
305 return self.data
306
306
307
307
308 class JSON(DisplayObject):
308 class JSON(DisplayObject):
309
309
310 def _repr_json_(self):
310 def _repr_json_(self):
311 return self.data
311 return self.data
312
312
313
313
314 class Javascript(DisplayObject):
314 class Javascript(DisplayObject):
315
315
316 def _repr_javascript_(self):
316 def _repr_javascript_(self):
317 return self.data
317 return self.data
318
318
319
319
320 class Image(DisplayObject):
320 class Image(DisplayObject):
321
321
322 _read_flags = 'rb'
322 _read_flags = 'rb'
323
323
324 def __init__(self, data=None, url=None, filename=None, format=u'png', embed=False):
324 def __init__(self, data=None, url=None, filename=None, format=u'png', embed=False):
325 """Create a display an PNG/JPEG image given raw data.
325 """Create a display an PNG/JPEG image given raw data.
326
326
327 When this object is returned by an expression or passed to the
327 When this object is returned by an expression or passed to the
328 display function, it will result in the image being displayed
328 display function, it will result in the image being displayed
329 in the frontend.
329 in the frontend.
330
330
331 Parameters
331 Parameters
332 ----------
332 ----------
333 data : unicode, str or bytes
333 data : unicode, str or bytes
334 The raw data or a URL to download the data from.
334 The raw data or a URL to download the data from.
335 url : unicode
335 url : unicode
336 A URL to download the data from.
336 A URL to download the data from.
337 filename : unicode
337 filename : unicode
338 Path to a local file to load the data from.
338 Path to a local file to load the data from.
339 format : unicode
339 format : unicode
340 The format of the image data (png/jpeg/jpg). If a filename or URL is given
340 The format of the image data (png/jpeg/jpg). If a filename or URL is given
341 for format will be inferred from the filename extension.
341 for format will be inferred from the filename extension.
342 embed : bool
342 embed : bool
343 Should the image data be embedded in the notebook using a data URI (True)
343 Should the image data be embedded in the notebook using a data URI (True)
344 or be loaded using an <img> tag. Set this to True if you want the image
344 or be loaded using an <img> tag. Set this to True if you want the image
345 to be viewable later with no internet connection. If a filename is given
345 to be viewable later with no internet connection. If a filename is given
346 embed is always set to True.
346 embed is always set to True.
347 """
347 """
348 if filename is not None:
348 if filename is not None:
349 ext = self._find_ext(filename)
349 ext = self._find_ext(filename)
350 elif url is not None:
350 elif url is not None:
351 ext = self._find_ext(url)
351 ext = self._find_ext(url)
352 elif data.startswith('http'):
352 elif data.startswith('http'):
353 ext = self._find_ext(data)
353 ext = self._find_ext(data)
354 else:
354 else:
355 ext = None
355 ext = None
356 if ext is not None:
356 if ext is not None:
357 if ext == u'jpg' or ext == u'jpeg':
357 if ext == u'jpg' or ext == u'jpeg':
358 format = u'jpeg'
358 format = u'jpeg'
359 if ext == u'png':
359 if ext == u'png':
360 format = u'png'
360 format = u'png'
361 self.format = unicode(format).lower()
361 self.format = unicode(format).lower()
362 self.embed = True if filename is not None else embed
362 self.embed = True if filename is not None else embed
363 super(Image, self).__init__(data=data, url=url, filename=filename)
363 super(Image, self).__init__(data=data, url=url, filename=filename)
364
364
365 def reload(self):
365 def reload(self):
366 """Reload the raw data from file or URL."""
366 """Reload the raw data from file or URL."""
367 if self.embed:
367 if self.embed:
368 super(Image,self).reload()
368 super(Image,self).reload()
369
369
370 def _repr_html_(self):
370 def _repr_html_(self):
371 if not self.embed:
371 if not self.embed:
372 return u'<img src="%s" />' % self.url
372 return u'<img src="%s" />' % self.url
373
373
374 def _repr_png_(self):
374 def _repr_png_(self):
375 if self.embed and self.format == u'png':
375 if self.embed and self.format == u'png':
376 return self.data
376 return self.data
377
377
378 def _repr_jpeg_(self):
378 def _repr_jpeg_(self):
379 if self.embed and (self.format == u'jpeg' or self.format == u'jpg'):
379 if self.embed and (self.format == u'jpeg' or self.format == u'jpg'):
380 return self.data
380 return self.data
381
381
382 def _find_ext(self, s):
382 def _find_ext(self, s):
383 return unicode(s.split('.')[-1].lower())
383 return unicode(s.split('.')[-1].lower())
@@ -1,329 +1,329 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Displayhook for IPython.
2 """Displayhook for IPython.
3
3
4 This defines a callable class that IPython uses for `sys.displayhook`.
4 This defines a callable class that IPython uses for `sys.displayhook`.
5
5
6 Authors:
6 Authors:
7
7
8 * Fernando Perez
8 * Fernando Perez
9 * Brian Granger
9 * Brian Granger
10 * Robert Kern
10 * Robert Kern
11 """
11 """
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Copyright (C) 2008-2010 The IPython Development Team
14 # Copyright (C) 2008-2010 The IPython Development Team
15 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
15 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
16 #
16 #
17 # Distributed under the terms of the BSD License. The full license is in
17 # Distributed under the terms of the BSD License. The full license is in
18 # the file COPYING, distributed as part of this software.
18 # the file COPYING, distributed as part of this software.
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Imports
22 # Imports
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 import __builtin__
25 import __builtin__
26
26
27 from IPython.config.configurable import Configurable
27 from IPython.config.configurable import Configurable
28 from IPython.core import prompts
28 from IPython.core import prompts
29 from IPython.utils import io
29 from IPython.utils import io
30 from IPython.utils.traitlets import Instance, List
30 from IPython.utils.traitlets import Instance, List
31 from IPython.utils.warn import warn
31 from IPython.utils.warn import warn
32
32
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34 # Main displayhook class
34 # Main displayhook class
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36
36
37 # TODO: The DisplayHook class should be split into two classes, one that
37 # TODO: The DisplayHook class should be split into two classes, one that
38 # manages the prompts and their synchronization and another that just does the
38 # manages the prompts and their synchronization and another that just does the
39 # displayhook logic and calls into the prompt manager.
39 # displayhook logic and calls into the prompt manager.
40
40
41 # TODO: Move the various attributes (cache_size, colors, input_sep,
41 # TODO: Move the various attributes (cache_size, colors, input_sep,
42 # output_sep, output_sep2, ps1, ps2, ps_out, pad_left). Some of these are also
42 # output_sep, output_sep2, ps1, ps2, ps_out, pad_left). Some of these are also
43 # attributes of InteractiveShell. They should be on ONE object only and the
43 # attributes of InteractiveShell. They should be on ONE object only and the
44 # other objects should ask that one object for their values.
44 # other objects should ask that one object for their values.
45
45
46 class DisplayHook(Configurable):
46 class DisplayHook(Configurable):
47 """The custom IPython displayhook to replace sys.displayhook.
47 """The custom IPython displayhook to replace sys.displayhook.
48
48
49 This class does many things, but the basic idea is that it is a callable
49 This class does many things, but the basic idea is that it is a callable
50 that gets called anytime user code returns a value.
50 that gets called anytime user code returns a value.
51
51
52 Currently this class does more than just the displayhook logic and that
52 Currently this class does more than just the displayhook logic and that
53 extra logic should eventually be moved out of here.
53 extra logic should eventually be moved out of here.
54 """
54 """
55
55
56 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
56 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
57
57
58 def __init__(self, shell=None, cache_size=1000,
58 def __init__(self, shell=None, cache_size=1000,
59 colors='NoColor', input_sep='\n',
59 colors='NoColor', input_sep='\n',
60 output_sep='\n', output_sep2='',
60 output_sep='\n', output_sep2='',
61 ps1 = None, ps2 = None, ps_out = None, pad_left=True,
61 ps1 = None, ps2 = None, ps_out = None, pad_left=True,
62 config=None):
62 config=None):
63 super(DisplayHook, self).__init__(shell=shell, config=config)
63 super(DisplayHook, self).__init__(shell=shell, config=config)
64
64
65 cache_size_min = 3
65 cache_size_min = 3
66 if cache_size <= 0:
66 if cache_size <= 0:
67 self.do_full_cache = 0
67 self.do_full_cache = 0
68 cache_size = 0
68 cache_size = 0
69 elif cache_size < cache_size_min:
69 elif cache_size < cache_size_min:
70 self.do_full_cache = 0
70 self.do_full_cache = 0
71 cache_size = 0
71 cache_size = 0
72 warn('caching was disabled (min value for cache size is %s).' %
72 warn('caching was disabled (min value for cache size is %s).' %
73 cache_size_min,level=3)
73 cache_size_min,level=3)
74 else:
74 else:
75 self.do_full_cache = 1
75 self.do_full_cache = 1
76
76
77 self.cache_size = cache_size
77 self.cache_size = cache_size
78 self.input_sep = input_sep
78 self.input_sep = input_sep
79
79
80 # we need a reference to the user-level namespace
80 # we need a reference to the user-level namespace
81 self.shell = shell
81 self.shell = shell
82
82
83 # Set input prompt strings and colors
83 # Set input prompt strings and colors
84 if cache_size == 0:
84 if cache_size == 0:
85 if ps1.find('%n') > -1 or ps1.find(r'\#') > -1 \
85 if ps1.find('%n') > -1 or ps1.find(r'\#') > -1 \
86 or ps1.find(r'\N') > -1:
86 or ps1.find(r'\N') > -1:
87 ps1 = '>>> '
87 ps1 = '>>> '
88 if ps2.find('%n') > -1 or ps2.find(r'\#') > -1 \
88 if ps2.find('%n') > -1 or ps2.find(r'\#') > -1 \
89 or ps2.find(r'\N') > -1:
89 or ps2.find(r'\N') > -1:
90 ps2 = '... '
90 ps2 = '... '
91 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
91 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
92 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
92 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
93 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
93 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
94
94
95 self.color_table = prompts.PromptColors
95 self.color_table = prompts.PromptColors
96 self.prompt1 = prompts.Prompt1(self,sep=input_sep,prompt=self.ps1_str,
96 self.prompt1 = prompts.Prompt1(self,sep=input_sep,prompt=self.ps1_str,
97 pad_left=pad_left)
97 pad_left=pad_left)
98 self.prompt2 = prompts.Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
98 self.prompt2 = prompts.Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
99 self.prompt_out = prompts.PromptOut(self,sep='',prompt=self.ps_out_str,
99 self.prompt_out = prompts.PromptOut(self,sep='',prompt=self.ps_out_str,
100 pad_left=pad_left)
100 pad_left=pad_left)
101 self.set_colors(colors)
101 self.set_colors(colors)
102
102
103 # Store the last prompt string each time, we need it for aligning
103 # Store the last prompt string each time, we need it for aligning
104 # continuation and auto-rewrite prompts
104 # continuation and auto-rewrite prompts
105 self.last_prompt = ''
105 self.last_prompt = ''
106 self.output_sep = output_sep
106 self.output_sep = output_sep
107 self.output_sep2 = output_sep2
107 self.output_sep2 = output_sep2
108 self._,self.__,self.___ = '','',''
108 self._,self.__,self.___ = '','',''
109
109
110 # these are deliberately global:
110 # these are deliberately global:
111 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
111 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
112 self.shell.user_ns.update(to_user_ns)
112 self.shell.user_ns.update(to_user_ns)
113
113
114 @property
114 @property
115 def prompt_count(self):
115 def prompt_count(self):
116 return self.shell.execution_count
116 return self.shell.execution_count
117
117
118 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
118 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
119 if p_str is None:
119 if p_str is None:
120 if self.do_full_cache:
120 if self.do_full_cache:
121 return cache_def
121 return cache_def
122 else:
122 else:
123 return no_cache_def
123 return no_cache_def
124 else:
124 else:
125 return p_str
125 return p_str
126
126
127 def set_colors(self, colors):
127 def set_colors(self, colors):
128 """Set the active color scheme and configure colors for the three
128 """Set the active color scheme and configure colors for the three
129 prompt subsystems."""
129 prompt subsystems."""
130
130
131 # FIXME: This modifying of the global prompts.prompt_specials needs
131 # FIXME: This modifying of the global prompts.prompt_specials needs
132 # to be fixed. We need to refactor all of the prompts stuff to use
132 # to be fixed. We need to refactor all of the prompts stuff to use
133 # proper configuration and traits notifications.
133 # proper configuration and traits notifications.
134 if colors.lower()=='nocolor':
134 if colors.lower()=='nocolor':
135 prompts.prompt_specials = prompts.prompt_specials_nocolor
135 prompts.prompt_specials = prompts.prompt_specials_nocolor
136 else:
136 else:
137 prompts.prompt_specials = prompts.prompt_specials_color
137 prompts.prompt_specials = prompts.prompt_specials_color
138
138
139 self.color_table.set_active_scheme(colors)
139 self.color_table.set_active_scheme(colors)
140 self.prompt1.set_colors()
140 self.prompt1.set_colors()
141 self.prompt2.set_colors()
141 self.prompt2.set_colors()
142 self.prompt_out.set_colors()
142 self.prompt_out.set_colors()
143
143
144 #-------------------------------------------------------------------------
144 #-------------------------------------------------------------------------
145 # Methods used in __call__. Override these methods to modify the behavior
145 # Methods used in __call__. Override these methods to modify the behavior
146 # of the displayhook.
146 # of the displayhook.
147 #-------------------------------------------------------------------------
147 #-------------------------------------------------------------------------
148
148
149 def check_for_underscore(self):
149 def check_for_underscore(self):
150 """Check if the user has set the '_' variable by hand."""
150 """Check if the user has set the '_' variable by hand."""
151 # If something injected a '_' variable in __builtin__, delete
151 # If something injected a '_' variable in __builtin__, delete
152 # ipython's automatic one so we don't clobber that. gettext() in
152 # ipython's automatic one so we don't clobber that. gettext() in
153 # particular uses _, so we need to stay away from it.
153 # particular uses _, so we need to stay away from it.
154 if '_' in __builtin__.__dict__:
154 if '_' in __builtin__.__dict__:
155 try:
155 try:
156 del self.shell.user_ns['_']
156 del self.shell.user_ns['_']
157 except KeyError:
157 except KeyError:
158 pass
158 pass
159
159
160 def quiet(self):
160 def quiet(self):
161 """Should we silence the display hook because of ';'?"""
161 """Should we silence the display hook because of ';'?"""
162 # do not print output if input ends in ';'
162 # do not print output if input ends in ';'
163 try:
163 try:
164 cell = self.shell.history_manager.input_hist_parsed[self.prompt_count]
164 cell = self.shell.history_manager.input_hist_parsed[self.prompt_count]
165 if cell.rstrip().endswith(';'):
165 if cell.rstrip().endswith(';'):
166 return True
166 return True
167 except IndexError:
167 except IndexError:
168 # some uses of ipshellembed may fail here
168 # some uses of ipshellembed may fail here
169 pass
169 pass
170 return False
170 return False
171
171
172 def start_displayhook(self):
172 def start_displayhook(self):
173 """Start the displayhook, initializing resources."""
173 """Start the displayhook, initializing resources."""
174 pass
174 pass
175
175
176 def write_output_prompt(self):
176 def write_output_prompt(self):
177 """Write the output prompt.
177 """Write the output prompt.
178
178
179 The default implementation simply writes the prompt to
179 The default implementation simply writes the prompt to
180 ``io.stdout``.
180 ``io.stdout``.
181 """
181 """
182 # Use write, not print which adds an extra space.
182 # Use write, not print which adds an extra space.
183 io.stdout.write(self.output_sep)
183 io.stdout.write(self.output_sep)
184 outprompt = str(self.prompt_out)
184 outprompt = str(self.prompt_out)
185 if self.do_full_cache:
185 if self.do_full_cache:
186 io.stdout.write(outprompt)
186 io.stdout.write(outprompt)
187
187
188 def compute_format_data(self, result):
188 def compute_format_data(self, result):
189 """Compute format data of the object to be displayed.
189 """Compute format data of the object to be displayed.
190
190
191 The format data is a generalization of the :func:`repr` of an object.
191 The format data is a generalization of the :func:`repr` of an object.
192 In the default implementation the format data is a :class:`dict` of
192 In the default implementation the format data is a :class:`dict` of
193 key value pair where the keys are valid MIME types and the values
193 key value pair where the keys are valid MIME types and the values
194 are JSON'able data structure containing the raw data for that MIME
194 are JSON'able data structure containing the raw data for that MIME
195 type. It is up to frontends to determine pick a MIME to to use and
195 type. It is up to frontends to determine pick a MIME to to use and
196 display that data in an appropriate manner.
196 display that data in an appropriate manner.
197
197
198 This method only computes the format data for the object and should
198 This method only computes the format data for the object and should
199 NOT actually print or write that to a stream.
199 NOT actually print or write that to a stream.
200
200
201 Parameters
201 Parameters
202 ----------
202 ----------
203 result : object
203 result : object
204 The Python object passed to the display hook, whose format will be
204 The Python object passed to the display hook, whose format will be
205 computed.
205 computed.
206
206
207 Returns
207 Returns
208 -------
208 -------
209 format_data : dict
209 format_data : dict
210 A :class:`dict` whose keys are valid MIME types and values are
210 A :class:`dict` whose keys are valid MIME types and values are
211 JSON'able raw data for that MIME type. It is recommended that
211 JSON'able raw data for that MIME type. It is recommended that
212 all return values of this should always include the "text/plain"
212 all return values of this should always include the "text/plain"
213 MIME type representation of the object.
213 MIME type representation of the object.
214 """
214 """
215 return self.shell.display_formatter.format(result)
215 return self.shell.display_formatter.format(result)
216
216
217 def write_format_data(self, format_dict):
217 def write_format_data(self, format_dict):
218 """Write the format data dict to the frontend.
218 """Write the format data dict to the frontend.
219
219
220 This default version of this method simply writes the plain text
220 This default version of this method simply writes the plain text
221 representation of the object to ``io.stdout``. Subclasses should
221 representation of the object to ``io.stdout``. Subclasses should
222 override this method to send the entire `format_dict` to the
222 override this method to send the entire `format_dict` to the
223 frontends.
223 frontends.
224
224
225 Parameters
225 Parameters
226 ----------
226 ----------
227 format_dict : dict
227 format_dict : dict
228 The format dict for the object passed to `sys.displayhook`.
228 The format dict for the object passed to `sys.displayhook`.
229 """
229 """
230 # We want to print because we want to always make sure we have a
230 # We want to print because we want to always make sure we have a
231 # newline, even if all the prompt separators are ''. This is the
231 # newline, even if all the prompt separators are ''. This is the
232 # standard IPython behavior.
232 # standard IPython behavior.
233 result_repr = format_dict['text/plain']
233 result_repr = format_dict['text/plain']
234 if '\n' in result_repr:
234 if '\n' in result_repr:
235 # So that multi-line strings line up with the left column of
235 # So that multi-line strings line up with the left column of
236 # the screen, instead of having the output prompt mess up
236 # the screen, instead of having the output prompt mess up
237 # their first line.
237 # their first line.
238 # We use the ps_out_str template instead of the expanded prompt
238 # We use the ps_out_str template instead of the expanded prompt
239 # because the expansion may add ANSI escapes that will interfere
239 # because the expansion may add ANSI escapes that will interfere
240 # with our ability to determine whether or not we should add
240 # with our ability to determine whether or not we should add
241 # a newline.
241 # a newline.
242 if self.ps_out_str and not self.ps_out_str.endswith('\n'):
242 if self.ps_out_str and not self.ps_out_str.endswith('\n'):
243 # But avoid extraneous empty lines.
243 # But avoid extraneous empty lines.
244 result_repr = '\n' + result_repr
244 result_repr = '\n' + result_repr
245
245
246 print >>io.stdout, result_repr
246 print >>io.stdout, result_repr
247
247
248 def update_user_ns(self, result):
248 def update_user_ns(self, result):
249 """Update user_ns with various things like _, __, _1, etc."""
249 """Update user_ns with various things like _, __, _1, etc."""
250
250
251 # Avoid recursive reference when displaying _oh/Out
251 # Avoid recursive reference when displaying _oh/Out
252 if result is not self.shell.user_ns['_oh']:
252 if result is not self.shell.user_ns['_oh']:
253 if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
253 if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
254 warn('Output cache limit (currently '+
254 warn('Output cache limit (currently '+
255 `self.cache_size`+' entries) hit.\n'
255 `self.cache_size`+' entries) hit.\n'
256 'Flushing cache and resetting history counter...\n'
256 'Flushing cache and resetting history counter...\n'
257 'The only history variables available will be _,__,___ and _1\n'
257 'The only history variables available will be _,__,___ and _1\n'
258 'with the current result.')
258 'with the current result.')
259
259
260 self.flush()
260 self.flush()
261 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
261 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
262 # we cause buggy behavior for things like gettext).
262 # we cause buggy behavior for things like gettext).
263
263
264 if '_' not in __builtin__.__dict__:
264 if '_' not in __builtin__.__dict__:
265 self.___ = self.__
265 self.___ = self.__
266 self.__ = self._
266 self.__ = self._
267 self._ = result
267 self._ = result
268 self.shell.user_ns.update({'_':self._,
268 self.shell.user_ns.update({'_':self._,
269 '__':self.__,
269 '__':self.__,
270 '___':self.___})
270 '___':self.___})
271
271
272 # hackish access to top-level namespace to create _1,_2... dynamically
272 # hackish access to top-level namespace to create _1,_2... dynamically
273 to_main = {}
273 to_main = {}
274 if self.do_full_cache:
274 if self.do_full_cache:
275 new_result = '_'+`self.prompt_count`
275 new_result = '_'+`self.prompt_count`
276 to_main[new_result] = result
276 to_main[new_result] = result
277 self.shell.user_ns.update(to_main)
277 self.shell.user_ns.update(to_main)
278 self.shell.user_ns['_oh'][self.prompt_count] = result
278 self.shell.user_ns['_oh'][self.prompt_count] = result
279
279
280 def log_output(self, format_dict):
280 def log_output(self, format_dict):
281 """Log the output."""
281 """Log the output."""
282 if self.shell.logger.log_output:
282 if self.shell.logger.log_output:
283 self.shell.logger.log_write(format_dict['text/plain'], 'output')
283 self.shell.logger.log_write(format_dict['text/plain'], 'output')
284 self.shell.history_manager.output_hist_reprs[self.prompt_count] = \
284 self.shell.history_manager.output_hist_reprs[self.prompt_count] = \
285 format_dict['text/plain']
285 format_dict['text/plain']
286
286
287 def finish_displayhook(self):
287 def finish_displayhook(self):
288 """Finish up all displayhook activities."""
288 """Finish up all displayhook activities."""
289 io.stdout.write(self.output_sep2)
289 io.stdout.write(self.output_sep2)
290 io.stdout.flush()
290 io.stdout.flush()
291
291
292 def __call__(self, result=None):
292 def __call__(self, result=None):
293 """Printing with history cache management.
293 """Printing with history cache management.
294
294
295 This is invoked everytime the interpreter needs to print, and is
295 This is invoked everytime the interpreter needs to print, and is
296 activated by setting the variable sys.displayhook to it.
296 activated by setting the variable sys.displayhook to it.
297 """
297 """
298 self.check_for_underscore()
298 self.check_for_underscore()
299 if result is not None and not self.quiet():
299 if result is not None and not self.quiet():
300 self.start_displayhook()
300 self.start_displayhook()
301 self.write_output_prompt()
301 self.write_output_prompt()
302 format_dict = self.compute_format_data(result)
302 format_dict = self.compute_format_data(result)
303 self.write_format_data(format_dict)
303 self.write_format_data(format_dict)
304 self.update_user_ns(result)
304 self.update_user_ns(result)
305 self.log_output(format_dict)
305 self.log_output(format_dict)
306 self.finish_displayhook()
306 self.finish_displayhook()
307
307
308 def flush(self):
308 def flush(self):
309 if not self.do_full_cache:
309 if not self.do_full_cache:
310 raise ValueError,"You shouldn't have reached the cache flush "\
310 raise ValueError,"You shouldn't have reached the cache flush "\
311 "if full caching is not enabled!"
311 "if full caching is not enabled!"
312 # delete auto-generated vars from global namespace
312 # delete auto-generated vars from global namespace
313
313
314 for n in range(1,self.prompt_count + 1):
314 for n in range(1,self.prompt_count + 1):
315 key = '_'+`n`
315 key = '_'+`n`
316 try:
316 try:
317 del self.shell.user_ns[key]
317 del self.shell.user_ns[key]
318 except: pass
318 except: pass
319 self.shell.user_ns['_oh'].clear()
319 self.shell.user_ns['_oh'].clear()
320
320
321 # Release our own references to objects:
321 # Release our own references to objects:
322 self._, self.__, self.___ = '', '', ''
322 self._, self.__, self.___ = '', '', ''
323
323
324 if '_' not in __builtin__.__dict__:
324 if '_' not in __builtin__.__dict__:
325 self.shell.user_ns.update({'_':None,'__':None, '___':None})
325 self.shell.user_ns.update({'_':None,'__':None, '___':None})
326 import gc
326 import gc
327 # TODO: Is this really needed?
327 # TODO: Is this really needed?
328 gc.collect()
328 gc.collect()
329
329
@@ -1,298 +1,298 b''
1 """An interface for publishing rich data to frontends.
1 """An interface for publishing rich data to frontends.
2
2
3 There are two components of the display system:
3 There are two components of the display system:
4
4
5 * Display formatters, which take a Python object and compute the
5 * Display formatters, which take a Python object and compute the
6 representation of the object in various formats (text, HTML, SVg, etc.).
6 representation of the object in various formats (text, HTML, SVg, etc.).
7 * The display publisher that is used to send the representation data to the
7 * The display publisher that is used to send the representation data to the
8 various frontends.
8 various frontends.
9
9
10 This module defines the logic display publishing. The display publisher uses
10 This module defines the logic display publishing. The display publisher uses
11 the ``display_data`` message type that is defined in the IPython messaging
11 the ``display_data`` message type that is defined in the IPython messaging
12 spec.
12 spec.
13
13
14 Authors:
14 Authors:
15
15
16 * Brian Granger
16 * Brian Granger
17 """
17 """
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Copyright (C) 2008-2010 The IPython Development Team
20 # Copyright (C) 2008-2010 The IPython Development Team
21 #
21 #
22 # Distributed under the terms of the BSD License. The full license is in
22 # Distributed under the terms of the BSD License. The full license is in
23 # the file COPYING, distributed as part of this software.
23 # the file COPYING, distributed as part of this software.
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Imports
27 # Imports
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 from __future__ import print_function
30 from __future__ import print_function
31
31
32 from IPython.config.configurable import Configurable
32 from IPython.config.configurable import Configurable
33
33
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35 # Main payload class
35 # Main payload class
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37
37
38 class DisplayPublisher(Configurable):
38 class DisplayPublisher(Configurable):
39 """A traited class that publishes display data to frontends.
39 """A traited class that publishes display data to frontends.
40
40
41 Instances of this class are created by the main IPython object and should
41 Instances of this class are created by the main IPython object and should
42 be accessed there.
42 be accessed there.
43 """
43 """
44
44
45 def _validate_data(self, source, data, metadata=None):
45 def _validate_data(self, source, data, metadata=None):
46 """Validate the display data.
46 """Validate the display data.
47
47
48 Parameters
48 Parameters
49 ----------
49 ----------
50 source : str
50 source : str
51 The fully dotted name of the callable that created the data, like
51 The fully dotted name of the callable that created the data, like
52 :func:`foo.bar.my_formatter`.
52 :func:`foo.bar.my_formatter`.
53 data : dict
53 data : dict
54 The formata data dictionary.
54 The formata data dictionary.
55 metadata : dict
55 metadata : dict
56 Any metadata for the data.
56 Any metadata for the data.
57 """
57 """
58
58
59 if not isinstance(source, basestring):
59 if not isinstance(source, basestring):
60 raise TypeError('source must be a str, got: %r' % source)
60 raise TypeError('source must be a str, got: %r' % source)
61 if not isinstance(data, dict):
61 if not isinstance(data, dict):
62 raise TypeError('data must be a dict, got: %r' % data)
62 raise TypeError('data must be a dict, got: %r' % data)
63 if metadata is not None:
63 if metadata is not None:
64 if not isinstance(metadata, dict):
64 if not isinstance(metadata, dict):
65 raise TypeError('metadata must be a dict, got: %r' % data)
65 raise TypeError('metadata must be a dict, got: %r' % data)
66
66
67 def publish(self, source, data, metadata=None):
67 def publish(self, source, data, metadata=None):
68 """Publish data and metadata to all frontends.
68 """Publish data and metadata to all frontends.
69
69
70 See the ``display_data`` message in the messaging documentation for
70 See the ``display_data`` message in the messaging documentation for
71 more details about this message type.
71 more details about this message type.
72
72
73 The following MIME types are currently implemented:
73 The following MIME types are currently implemented:
74
74
75 * text/plain
75 * text/plain
76 * text/html
76 * text/html
77 * text/latex
77 * text/latex
78 * application/json
78 * application/json
79 * application/javascript
79 * application/javascript
80 * image/png
80 * image/png
81 * image/jpeg
81 * image/jpeg
82 * image/svg+xml
82 * image/svg+xml
83
83
84 Parameters
84 Parameters
85 ----------
85 ----------
86 source : str
86 source : str
87 A string that give the function or method that created the data,
87 A string that give the function or method that created the data,
88 such as 'IPython.core.page'.
88 such as 'IPython.core.page'.
89 data : dict
89 data : dict
90 A dictionary having keys that are valid MIME types (like
90 A dictionary having keys that are valid MIME types (like
91 'text/plain' or 'image/svg+xml') and values that are the data for
91 'text/plain' or 'image/svg+xml') and values that are the data for
92 that MIME type. The data itself must be a JSON'able data
92 that MIME type. The data itself must be a JSON'able data
93 structure. Minimally all data should have the 'text/plain' data,
93 structure. Minimally all data should have the 'text/plain' data,
94 which can be displayed by all frontends. If more than the plain
94 which can be displayed by all frontends. If more than the plain
95 text is given, it is up to the frontend to decide which
95 text is given, it is up to the frontend to decide which
96 representation to use.
96 representation to use.
97 metadata : dict
97 metadata : dict
98 A dictionary for metadata related to the data. This can contain
98 A dictionary for metadata related to the data. This can contain
99 arbitrary key, value pairs that frontends can use to interpret
99 arbitrary key, value pairs that frontends can use to interpret
100 the data.
100 the data.
101 """
101 """
102 from IPython.utils import io
102 from IPython.utils import io
103 # The default is to simply write the plain text data using io.stdout.
103 # The default is to simply write the plain text data using io.stdout.
104 if data.has_key('text/plain'):
104 if data.has_key('text/plain'):
105 print(data['text/plain'], file=io.stdout)
105 print(data['text/plain'], file=io.stdout)
106
106
107
107
108 def publish_display_data(source, data, metadata=None):
108 def publish_display_data(source, data, metadata=None):
109 """Publish data and metadata to all frontends.
109 """Publish data and metadata to all frontends.
110
110
111 See the ``display_data`` message in the messaging documentation for
111 See the ``display_data`` message in the messaging documentation for
112 more details about this message type.
112 more details about this message type.
113
113
114 The following MIME types are currently implemented:
114 The following MIME types are currently implemented:
115
115
116 * text/plain
116 * text/plain
117 * text/html
117 * text/html
118 * text/latex
118 * text/latex
119 * application/json
119 * application/json
120 * application/javascript
120 * application/javascript
121 * image/png
121 * image/png
122 * image/jpeg
122 * image/jpeg
123 * image/svg+xml
123 * image/svg+xml
124
124
125 Parameters
125 Parameters
126 ----------
126 ----------
127 source : str
127 source : str
128 A string that give the function or method that created the data,
128 A string that give the function or method that created the data,
129 such as 'IPython.core.page'.
129 such as 'IPython.core.page'.
130 data : dict
130 data : dict
131 A dictionary having keys that are valid MIME types (like
131 A dictionary having keys that are valid MIME types (like
132 'text/plain' or 'image/svg+xml') and values that are the data for
132 'text/plain' or 'image/svg+xml') and values that are the data for
133 that MIME type. The data itself must be a JSON'able data
133 that MIME type. The data itself must be a JSON'able data
134 structure. Minimally all data should have the 'text/plain' data,
134 structure. Minimally all data should have the 'text/plain' data,
135 which can be displayed by all frontends. If more than the plain
135 which can be displayed by all frontends. If more than the plain
136 text is given, it is up to the frontend to decide which
136 text is given, it is up to the frontend to decide which
137 representation to use.
137 representation to use.
138 metadata : dict
138 metadata : dict
139 A dictionary for metadata related to the data. This can contain
139 A dictionary for metadata related to the data. This can contain
140 arbitrary key, value pairs that frontends can use to interpret
140 arbitrary key, value pairs that frontends can use to interpret
141 the data.
141 the data.
142 """
142 """
143 from IPython.core.interactiveshell import InteractiveShell
143 from IPython.core.interactiveshell import InteractiveShell
144 InteractiveShell.instance().display_pub.publish(
144 InteractiveShell.instance().display_pub.publish(
145 source,
145 source,
146 data,
146 data,
147 metadata
147 metadata
148 )
148 )
149
149
150
150
151 def publish_pretty(data, metadata=None):
151 def publish_pretty(data, metadata=None):
152 """Publish raw text data to all frontends.
152 """Publish raw text data to all frontends.
153
153
154 Parameters
154 Parameters
155 ----------
155 ----------
156 data : unicode
156 data : unicode
157 The raw text data to publish.
157 The raw text data to publish.
158 metadata : dict
158 metadata : dict
159 A dictionary for metadata related to the data. This can contain
159 A dictionary for metadata related to the data. This can contain
160 arbitrary key, value pairs that frontends can use to interpret
160 arbitrary key, value pairs that frontends can use to interpret
161 the data.
161 the data.
162 """
162 """
163 publish_display_data(
163 publish_display_data(
164 u'IPython.core.displaypub.publish_pretty',
164 u'IPython.core.displaypub.publish_pretty',
165 {'text/plain':data},
165 {'text/plain':data},
166 metadata=metadata
166 metadata=metadata
167 )
167 )
168
168
169
169
170 def publish_html(data, metadata=None):
170 def publish_html(data, metadata=None):
171 """Publish raw HTML data to all frontends.
171 """Publish raw HTML data to all frontends.
172
172
173 Parameters
173 Parameters
174 ----------
174 ----------
175 data : unicode
175 data : unicode
176 The raw HTML data to publish.
176 The raw HTML data to publish.
177 metadata : dict
177 metadata : dict
178 A dictionary for metadata related to the data. This can contain
178 A dictionary for metadata related to the data. This can contain
179 arbitrary key, value pairs that frontends can use to interpret
179 arbitrary key, value pairs that frontends can use to interpret
180 the data.
180 the data.
181 """
181 """
182 publish_display_data(
182 publish_display_data(
183 u'IPython.core.displaypub.publish_html',
183 u'IPython.core.displaypub.publish_html',
184 {'text/html':data},
184 {'text/html':data},
185 metadata=metadata
185 metadata=metadata
186 )
186 )
187
187
188
188
189 def publish_latex(data, metadata=None):
189 def publish_latex(data, metadata=None):
190 """Publish raw LaTeX data to all frontends.
190 """Publish raw LaTeX data to all frontends.
191
191
192 Parameters
192 Parameters
193 ----------
193 ----------
194 data : unicode
194 data : unicode
195 The raw LaTeX data to publish.
195 The raw LaTeX data to publish.
196 metadata : dict
196 metadata : dict
197 A dictionary for metadata related to the data. This can contain
197 A dictionary for metadata related to the data. This can contain
198 arbitrary key, value pairs that frontends can use to interpret
198 arbitrary key, value pairs that frontends can use to interpret
199 the data.
199 the data.
200 """
200 """
201 publish_display_data(
201 publish_display_data(
202 u'IPython.core.displaypub.publish_latex',
202 u'IPython.core.displaypub.publish_latex',
203 {'text/latex':data},
203 {'text/latex':data},
204 metadata=metadata
204 metadata=metadata
205 )
205 )
206
206
207 def publish_png(data, metadata=None):
207 def publish_png(data, metadata=None):
208 """Publish raw binary PNG data to all frontends.
208 """Publish raw binary PNG data to all frontends.
209
209
210 Parameters
210 Parameters
211 ----------
211 ----------
212 data : str/bytes
212 data : str/bytes
213 The raw binary PNG data to publish.
213 The raw binary PNG data to publish.
214 metadata : dict
214 metadata : dict
215 A dictionary for metadata related to the data. This can contain
215 A dictionary for metadata related to the data. This can contain
216 arbitrary key, value pairs that frontends can use to interpret
216 arbitrary key, value pairs that frontends can use to interpret
217 the data.
217 the data.
218 """
218 """
219 publish_display_data(
219 publish_display_data(
220 u'IPython.core.displaypub.publish_png',
220 u'IPython.core.displaypub.publish_png',
221 {'image/png':data},
221 {'image/png':data},
222 metadata=metadata
222 metadata=metadata
223 )
223 )
224
224
225
225
226 def publish_jpeg(data, metadata=None):
226 def publish_jpeg(data, metadata=None):
227 """Publish raw binary JPEG data to all frontends.
227 """Publish raw binary JPEG data to all frontends.
228
228
229 Parameters
229 Parameters
230 ----------
230 ----------
231 data : str/bytes
231 data : str/bytes
232 The raw binary JPEG data to publish.
232 The raw binary JPEG data to publish.
233 metadata : dict
233 metadata : dict
234 A dictionary for metadata related to the data. This can contain
234 A dictionary for metadata related to the data. This can contain
235 arbitrary key, value pairs that frontends can use to interpret
235 arbitrary key, value pairs that frontends can use to interpret
236 the data.
236 the data.
237 """
237 """
238 publish_display_data(
238 publish_display_data(
239 u'IPython.core.displaypub.publish_jpeg',
239 u'IPython.core.displaypub.publish_jpeg',
240 {'image/jpeg':data},
240 {'image/jpeg':data},
241 metadata=metadata
241 metadata=metadata
242 )
242 )
243
243
244
244
245 def publish_svg(data, metadata=None):
245 def publish_svg(data, metadata=None):
246 """Publish raw SVG data to all frontends.
246 """Publish raw SVG data to all frontends.
247
247
248 Parameters
248 Parameters
249 ----------
249 ----------
250 data : unicode
250 data : unicode
251 The raw SVG data to publish.
251 The raw SVG data to publish.
252 metadata : dict
252 metadata : dict
253 A dictionary for metadata related to the data. This can contain
253 A dictionary for metadata related to the data. This can contain
254 arbitrary key, value pairs that frontends can use to interpret
254 arbitrary key, value pairs that frontends can use to interpret
255 the data.
255 the data.
256 """
256 """
257 publish_display_data(
257 publish_display_data(
258 u'IPython.core.displaypub.publish_svg',
258 u'IPython.core.displaypub.publish_svg',
259 {'image/svg+xml':data},
259 {'image/svg+xml':data},
260 metadata=metadata
260 metadata=metadata
261 )
261 )
262
262
263 def publish_json(data, metadata=None):
263 def publish_json(data, metadata=None):
264 """Publish raw JSON data to all frontends.
264 """Publish raw JSON data to all frontends.
265
265
266 Parameters
266 Parameters
267 ----------
267 ----------
268 data : unicode
268 data : unicode
269 The raw JSON data to publish.
269 The raw JSON data to publish.
270 metadata : dict
270 metadata : dict
271 A dictionary for metadata related to the data. This can contain
271 A dictionary for metadata related to the data. This can contain
272 arbitrary key, value pairs that frontends can use to interpret
272 arbitrary key, value pairs that frontends can use to interpret
273 the data.
273 the data.
274 """
274 """
275 publish_display_data(
275 publish_display_data(
276 u'IPython.core.displaypub.publish_json',
276 u'IPython.core.displaypub.publish_json',
277 {'application/json':data},
277 {'application/json':data},
278 metadata=metadata
278 metadata=metadata
279 )
279 )
280
280
281 def publish_javascript(data, metadata=None):
281 def publish_javascript(data, metadata=None):
282 """Publish raw Javascript data to all frontends.
282 """Publish raw Javascript data to all frontends.
283
283
284 Parameters
284 Parameters
285 ----------
285 ----------
286 data : unicode
286 data : unicode
287 The raw Javascript data to publish.
287 The raw Javascript data to publish.
288 metadata : dict
288 metadata : dict
289 A dictionary for metadata related to the data. This can contain
289 A dictionary for metadata related to the data. This can contain
290 arbitrary key, value pairs that frontends can use to interpret
290 arbitrary key, value pairs that frontends can use to interpret
291 the data.
291 the data.
292 """
292 """
293 publish_display_data(
293 publish_display_data(
294 u'IPython.core.displaypub.publish_javascript',
294 u'IPython.core.displaypub.publish_javascript',
295 {'application/javascript':data},
295 {'application/javascript':data},
296 metadata=metadata
296 metadata=metadata
297 )
297 )
298
298
@@ -1,51 +1,51 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Global exception classes for IPython.core.
3 Global exception classes for IPython.core.
4
4
5 Authors:
5 Authors:
6
6
7 * Brian Granger
7 * Brian Granger
8 * Fernando Perez
8 * Fernando Perez
9
9
10 Notes
10 Notes
11 -----
11 -----
12 """
12 """
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Copyright (C) 2008-2009 The IPython Development Team
15 # Copyright (C) 2008-2009 The IPython Development Team
16 #
16 #
17 # Distributed under the terms of the BSD License. The full license is in
17 # Distributed under the terms of the BSD License. The full license is in
18 # the file COPYING, distributed as part of this software.
18 # the file COPYING, distributed as part of this software.
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Imports
22 # Imports
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26 # Exception classes
26 # Exception classes
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28
28
29 class IPythonCoreError(Exception):
29 class IPythonCoreError(Exception):
30 pass
30 pass
31
31
32
32
33 class TryNext(IPythonCoreError):
33 class TryNext(IPythonCoreError):
34 """Try next hook exception.
34 """Try next hook exception.
35
35
36 Raise this in your hook function to indicate that the next hook handler
36 Raise this in your hook function to indicate that the next hook handler
37 should be used to handle the operation. If you pass arguments to the
37 should be used to handle the operation. If you pass arguments to the
38 constructor those arguments will be used by the next hook instead of the
38 constructor those arguments will be used by the next hook instead of the
39 original ones.
39 original ones.
40 """
40 """
41
41
42 def __init__(self, *args, **kwargs):
42 def __init__(self, *args, **kwargs):
43 self.args = args
43 self.args = args
44 self.kwargs = kwargs
44 self.kwargs = kwargs
45
45
46 class UsageError(IPythonCoreError):
46 class UsageError(IPythonCoreError):
47 """Error in magic function arguments, etc.
47 """Error in magic function arguments, etc.
48
48
49 Something that probably won't warrant a full traceback, but should
49 Something that probably won't warrant a full traceback, but should
50 nevertheless interrupt a macro / batch file.
50 nevertheless interrupt a macro / batch file.
51 """
51 """
@@ -1,135 +1,135 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Color schemes for exception handling code in IPython.
3 Color schemes for exception handling code in IPython.
4 """
4 """
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2005-2006 Fernando Perez <fperez@colorado.edu>
7 # Copyright (C) 2005-2006 Fernando Perez <fperez@colorado.edu>
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #*****************************************************************************
11 #*****************************************************************************
12
12
13 from IPython.utils.coloransi import ColorSchemeTable, TermColors, ColorScheme
13 from IPython.utils.coloransi import ColorSchemeTable, TermColors, ColorScheme
14
14
15 def exception_colors():
15 def exception_colors():
16 """Return a color table with fields for exception reporting.
16 """Return a color table with fields for exception reporting.
17
17
18 The table is an instance of ColorSchemeTable with schemes added for
18 The table is an instance of ColorSchemeTable with schemes added for
19 'Linux', 'LightBG' and 'NoColor' and fields for exception handling filled
19 'Linux', 'LightBG' and 'NoColor' and fields for exception handling filled
20 in.
20 in.
21
21
22 Examples:
22 Examples:
23
23
24 >>> ec = exception_colors()
24 >>> ec = exception_colors()
25 >>> ec.active_scheme_name
25 >>> ec.active_scheme_name
26 ''
26 ''
27 >>> print ec.active_colors
27 >>> print ec.active_colors
28 None
28 None
29
29
30 Now we activate a color scheme:
30 Now we activate a color scheme:
31 >>> ec.set_active_scheme('NoColor')
31 >>> ec.set_active_scheme('NoColor')
32 >>> ec.active_scheme_name
32 >>> ec.active_scheme_name
33 'NoColor'
33 'NoColor'
34 >>> ec.active_colors.keys()
34 >>> ec.active_colors.keys()
35 ['em', 'filenameEm', 'excName', 'valEm', 'nameEm', 'line', 'topline',
35 ['em', 'filenameEm', 'excName', 'valEm', 'nameEm', 'line', 'topline',
36 'name', 'caret', 'val', 'vName', 'Normal', 'filename', 'linenoEm',
36 'name', 'caret', 'val', 'vName', 'Normal', 'filename', 'linenoEm',
37 'lineno', 'normalEm']
37 'lineno', 'normalEm']
38 """
38 """
39
39
40 ex_colors = ColorSchemeTable()
40 ex_colors = ColorSchemeTable()
41
41
42 # Populate it with color schemes
42 # Populate it with color schemes
43 C = TermColors # shorthand and local lookup
43 C = TermColors # shorthand and local lookup
44 ex_colors.add_scheme(ColorScheme(
44 ex_colors.add_scheme(ColorScheme(
45 'NoColor',
45 'NoColor',
46 # The color to be used for the top line
46 # The color to be used for the top line
47 topline = C.NoColor,
47 topline = C.NoColor,
48
48
49 # The colors to be used in the traceback
49 # The colors to be used in the traceback
50 filename = C.NoColor,
50 filename = C.NoColor,
51 lineno = C.NoColor,
51 lineno = C.NoColor,
52 name = C.NoColor,
52 name = C.NoColor,
53 vName = C.NoColor,
53 vName = C.NoColor,
54 val = C.NoColor,
54 val = C.NoColor,
55 em = C.NoColor,
55 em = C.NoColor,
56
56
57 # Emphasized colors for the last frame of the traceback
57 # Emphasized colors for the last frame of the traceback
58 normalEm = C.NoColor,
58 normalEm = C.NoColor,
59 filenameEm = C.NoColor,
59 filenameEm = C.NoColor,
60 linenoEm = C.NoColor,
60 linenoEm = C.NoColor,
61 nameEm = C.NoColor,
61 nameEm = C.NoColor,
62 valEm = C.NoColor,
62 valEm = C.NoColor,
63
63
64 # Colors for printing the exception
64 # Colors for printing the exception
65 excName = C.NoColor,
65 excName = C.NoColor,
66 line = C.NoColor,
66 line = C.NoColor,
67 caret = C.NoColor,
67 caret = C.NoColor,
68 Normal = C.NoColor
68 Normal = C.NoColor
69 ))
69 ))
70
70
71 # make some schemes as instances so we can copy them for modification easily
71 # make some schemes as instances so we can copy them for modification easily
72 ex_colors.add_scheme(ColorScheme(
72 ex_colors.add_scheme(ColorScheme(
73 'Linux',
73 'Linux',
74 # The color to be used for the top line
74 # The color to be used for the top line
75 topline = C.LightRed,
75 topline = C.LightRed,
76
76
77 # The colors to be used in the traceback
77 # The colors to be used in the traceback
78 filename = C.Green,
78 filename = C.Green,
79 lineno = C.Green,
79 lineno = C.Green,
80 name = C.Purple,
80 name = C.Purple,
81 vName = C.Cyan,
81 vName = C.Cyan,
82 val = C.Green,
82 val = C.Green,
83 em = C.LightCyan,
83 em = C.LightCyan,
84
84
85 # Emphasized colors for the last frame of the traceback
85 # Emphasized colors for the last frame of the traceback
86 normalEm = C.LightCyan,
86 normalEm = C.LightCyan,
87 filenameEm = C.LightGreen,
87 filenameEm = C.LightGreen,
88 linenoEm = C.LightGreen,
88 linenoEm = C.LightGreen,
89 nameEm = C.LightPurple,
89 nameEm = C.LightPurple,
90 valEm = C.LightBlue,
90 valEm = C.LightBlue,
91
91
92 # Colors for printing the exception
92 # Colors for printing the exception
93 excName = C.LightRed,
93 excName = C.LightRed,
94 line = C.Yellow,
94 line = C.Yellow,
95 caret = C.White,
95 caret = C.White,
96 Normal = C.Normal
96 Normal = C.Normal
97 ))
97 ))
98
98
99 # For light backgrounds, swap dark/light colors
99 # For light backgrounds, swap dark/light colors
100 ex_colors.add_scheme(ColorScheme(
100 ex_colors.add_scheme(ColorScheme(
101 'LightBG',
101 'LightBG',
102 # The color to be used for the top line
102 # The color to be used for the top line
103 topline = C.Red,
103 topline = C.Red,
104
104
105 # The colors to be used in the traceback
105 # The colors to be used in the traceback
106 filename = C.LightGreen,
106 filename = C.LightGreen,
107 lineno = C.LightGreen,
107 lineno = C.LightGreen,
108 name = C.LightPurple,
108 name = C.LightPurple,
109 vName = C.Cyan,
109 vName = C.Cyan,
110 val = C.LightGreen,
110 val = C.LightGreen,
111 em = C.Cyan,
111 em = C.Cyan,
112
112
113 # Emphasized colors for the last frame of the traceback
113 # Emphasized colors for the last frame of the traceback
114 normalEm = C.Cyan,
114 normalEm = C.Cyan,
115 filenameEm = C.Green,
115 filenameEm = C.Green,
116 linenoEm = C.Green,
116 linenoEm = C.Green,
117 nameEm = C.Purple,
117 nameEm = C.Purple,
118 valEm = C.Blue,
118 valEm = C.Blue,
119
119
120 # Colors for printing the exception
120 # Colors for printing the exception
121 excName = C.Red,
121 excName = C.Red,
122 #line = C.Brown, # brown often is displayed as yellow
122 #line = C.Brown, # brown often is displayed as yellow
123 line = C.Red,
123 line = C.Red,
124 caret = C.Normal,
124 caret = C.Normal,
125 Normal = C.Normal,
125 Normal = C.Normal,
126 ))
126 ))
127
127
128 return ex_colors
128 return ex_colors
129
129
130
130
131 # For backwards compatibility, keep around a single global object. Note that
131 # For backwards compatibility, keep around a single global object. Note that
132 # this should NOT be used, the factory function should be used instead, since
132 # this should NOT be used, the factory function should be used instead, since
133 # these objects are stateful and it's very easy to get strange bugs if any code
133 # these objects are stateful and it's very easy to get strange bugs if any code
134 # modifies the module-level object's state.
134 # modifies the module-level object's state.
135 ExceptionColors = exception_colors()
135 ExceptionColors = exception_colors()
@@ -1,125 +1,125 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """A class for managing IPython extensions.
2 """A class for managing IPython extensions.
3
3
4 Authors:
4 Authors:
5
5
6 * Brian Granger
6 * Brian Granger
7 """
7 """
8
8
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10 # Copyright (C) 2010 The IPython Development Team
10 # Copyright (C) 2010 The IPython Development Team
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Imports
17 # Imports
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 import os
20 import os
21 import sys
21 import sys
22
22
23 from IPython.config.configurable import Configurable
23 from IPython.config.configurable import Configurable
24 from IPython.utils.traitlets import Instance
24 from IPython.utils.traitlets import Instance
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Main class
27 # Main class
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 class ExtensionManager(Configurable):
30 class ExtensionManager(Configurable):
31 """A class to manage IPython extensions.
31 """A class to manage IPython extensions.
32
32
33 An IPython extension is an importable Python module that has
33 An IPython extension is an importable Python module that has
34 a function with the signature::
34 a function with the signature::
35
35
36 def load_ipython_extension(ipython):
36 def load_ipython_extension(ipython):
37 # Do things with ipython
37 # Do things with ipython
38
38
39 This function is called after your extension is imported and the
39 This function is called after your extension is imported and the
40 currently active :class:`InteractiveShell` instance is passed as
40 currently active :class:`InteractiveShell` instance is passed as
41 the only argument. You can do anything you want with IPython at
41 the only argument. You can do anything you want with IPython at
42 that point, including defining new magic and aliases, adding new
42 that point, including defining new magic and aliases, adding new
43 components, etc.
43 components, etc.
44
44
45 The :func:`load_ipython_extension` will be called again is you
45 The :func:`load_ipython_extension` will be called again is you
46 load or reload the extension again. It is up to the extension
46 load or reload the extension again. It is up to the extension
47 author to add code to manage that.
47 author to add code to manage that.
48
48
49 You can put your extension modules anywhere you want, as long as
49 You can put your extension modules anywhere you want, as long as
50 they can be imported by Python's standard import mechanism. However,
50 they can be imported by Python's standard import mechanism. However,
51 to make it easy to write extensions, you can also put your extensions
51 to make it easy to write extensions, you can also put your extensions
52 in ``os.path.join(self.ipython_dir, 'extensions')``. This directory
52 in ``os.path.join(self.ipython_dir, 'extensions')``. This directory
53 is added to ``sys.path`` automatically.
53 is added to ``sys.path`` automatically.
54 """
54 """
55
55
56 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
56 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
57
57
58 def __init__(self, shell=None, config=None):
58 def __init__(self, shell=None, config=None):
59 super(ExtensionManager, self).__init__(shell=shell, config=config)
59 super(ExtensionManager, self).__init__(shell=shell, config=config)
60 self.shell.on_trait_change(
60 self.shell.on_trait_change(
61 self._on_ipython_dir_changed, 'ipython_dir'
61 self._on_ipython_dir_changed, 'ipython_dir'
62 )
62 )
63
63
64 def __del__(self):
64 def __del__(self):
65 self.shell.on_trait_change(
65 self.shell.on_trait_change(
66 self._on_ipython_dir_changed, 'ipython_dir', remove=True
66 self._on_ipython_dir_changed, 'ipython_dir', remove=True
67 )
67 )
68
68
69 @property
69 @property
70 def ipython_extension_dir(self):
70 def ipython_extension_dir(self):
71 return os.path.join(self.shell.ipython_dir, u'extensions')
71 return os.path.join(self.shell.ipython_dir, u'extensions')
72
72
73 def _on_ipython_dir_changed(self):
73 def _on_ipython_dir_changed(self):
74 if not os.path.isdir(self.ipython_extension_dir):
74 if not os.path.isdir(self.ipython_extension_dir):
75 os.makedirs(self.ipython_extension_dir, mode = 0777)
75 os.makedirs(self.ipython_extension_dir, mode = 0777)
76
76
77 def load_extension(self, module_str):
77 def load_extension(self, module_str):
78 """Load an IPython extension by its module name.
78 """Load an IPython extension by its module name.
79
79
80 If :func:`load_ipython_extension` returns anything, this function
80 If :func:`load_ipython_extension` returns anything, this function
81 will return that object.
81 will return that object.
82 """
82 """
83 from IPython.utils.syspathcontext import prepended_to_syspath
83 from IPython.utils.syspathcontext import prepended_to_syspath
84
84
85 if module_str not in sys.modules:
85 if module_str not in sys.modules:
86 with prepended_to_syspath(self.ipython_extension_dir):
86 with prepended_to_syspath(self.ipython_extension_dir):
87 __import__(module_str)
87 __import__(module_str)
88 mod = sys.modules[module_str]
88 mod = sys.modules[module_str]
89 return self._call_load_ipython_extension(mod)
89 return self._call_load_ipython_extension(mod)
90
90
91 def unload_extension(self, module_str):
91 def unload_extension(self, module_str):
92 """Unload an IPython extension by its module name.
92 """Unload an IPython extension by its module name.
93
93
94 This function looks up the extension's name in ``sys.modules`` and
94 This function looks up the extension's name in ``sys.modules`` and
95 simply calls ``mod.unload_ipython_extension(self)``.
95 simply calls ``mod.unload_ipython_extension(self)``.
96 """
96 """
97 if module_str in sys.modules:
97 if module_str in sys.modules:
98 mod = sys.modules[module_str]
98 mod = sys.modules[module_str]
99 self._call_unload_ipython_extension(mod)
99 self._call_unload_ipython_extension(mod)
100
100
101 def reload_extension(self, module_str):
101 def reload_extension(self, module_str):
102 """Reload an IPython extension by calling reload.
102 """Reload an IPython extension by calling reload.
103
103
104 If the module has not been loaded before,
104 If the module has not been loaded before,
105 :meth:`InteractiveShell.load_extension` is called. Otherwise
105 :meth:`InteractiveShell.load_extension` is called. Otherwise
106 :func:`reload` is called and then the :func:`load_ipython_extension`
106 :func:`reload` is called and then the :func:`load_ipython_extension`
107 function of the module, if it exists is called.
107 function of the module, if it exists is called.
108 """
108 """
109 from IPython.utils.syspathcontext import prepended_to_syspath
109 from IPython.utils.syspathcontext import prepended_to_syspath
110
110
111 with prepended_to_syspath(self.ipython_extension_dir):
111 with prepended_to_syspath(self.ipython_extension_dir):
112 if module_str in sys.modules:
112 if module_str in sys.modules:
113 mod = sys.modules[module_str]
113 mod = sys.modules[module_str]
114 reload(mod)
114 reload(mod)
115 self._call_load_ipython_extension(mod)
115 self._call_load_ipython_extension(mod)
116 else:
116 else:
117 self.load_extension(module_str)
117 self.load_extension(module_str)
118
118
119 def _call_load_ipython_extension(self, mod):
119 def _call_load_ipython_extension(self, mod):
120 if hasattr(mod, 'load_ipython_extension'):
120 if hasattr(mod, 'load_ipython_extension'):
121 return mod.load_ipython_extension(self.shell)
121 return mod.load_ipython_extension(self.shell)
122
122
123 def _call_unload_ipython_extension(self, mod):
123 def _call_unload_ipython_extension(self, mod):
124 if hasattr(mod, 'unload_ipython_extension'):
124 if hasattr(mod, 'unload_ipython_extension'):
125 return mod.unload_ipython_extension(self.shell)
125 return mod.unload_ipython_extension(self.shell)
@@ -1,620 +1,620 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Display formatters.
2 """Display formatters.
3
3
4
4
5 Authors:
5 Authors:
6
6
7 * Robert Kern
7 * Robert Kern
8 * Brian Granger
8 * Brian Granger
9 """
9 """
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Copyright (c) 2010, IPython Development Team.
11 # Copyright (c) 2010, IPython Development Team.
12 #
12 #
13 # Distributed under the terms of the Modified BSD License.
13 # Distributed under the terms of the Modified BSD License.
14 #
14 #
15 # The full license is in the file COPYING.txt, distributed with this software.
15 # The full license is in the file COPYING.txt, distributed with this software.
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 # Stdlib imports
22 # Stdlib imports
23 import abc
23 import abc
24 import sys
24 import sys
25 # We must use StringIO, as cStringIO doesn't handle unicode properly.
25 # We must use StringIO, as cStringIO doesn't handle unicode properly.
26 from StringIO import StringIO
26 from StringIO import StringIO
27
27
28 # Our own imports
28 # Our own imports
29 from IPython.config.configurable import Configurable
29 from IPython.config.configurable import Configurable
30 from IPython.lib import pretty
30 from IPython.lib import pretty
31 from IPython.utils.traitlets import Bool, Dict, Int, Unicode, CUnicode, ObjectName
31 from IPython.utils.traitlets import Bool, Dict, Int, Unicode, CUnicode, ObjectName
32 from IPython.utils.py3compat import unicode_to_str
32 from IPython.utils.py3compat import unicode_to_str
33
33
34
34
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36 # The main DisplayFormatter class
36 # The main DisplayFormatter class
37 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
38
38
39
39
40 class DisplayFormatter(Configurable):
40 class DisplayFormatter(Configurable):
41
41
42 # When set to true only the default plain text formatter will be used.
42 # When set to true only the default plain text formatter will be used.
43 plain_text_only = Bool(False, config=True)
43 plain_text_only = Bool(False, config=True)
44
44
45 # A dict of formatter whose keys are format types (MIME types) and whose
45 # A dict of formatter whose keys are format types (MIME types) and whose
46 # values are subclasses of BaseFormatter.
46 # values are subclasses of BaseFormatter.
47 formatters = Dict(config=True)
47 formatters = Dict(config=True)
48 def _formatters_default(self):
48 def _formatters_default(self):
49 """Activate the default formatters."""
49 """Activate the default formatters."""
50 formatter_classes = [
50 formatter_classes = [
51 PlainTextFormatter,
51 PlainTextFormatter,
52 HTMLFormatter,
52 HTMLFormatter,
53 SVGFormatter,
53 SVGFormatter,
54 PNGFormatter,
54 PNGFormatter,
55 JPEGFormatter,
55 JPEGFormatter,
56 LatexFormatter,
56 LatexFormatter,
57 JSONFormatter,
57 JSONFormatter,
58 JavascriptFormatter
58 JavascriptFormatter
59 ]
59 ]
60 d = {}
60 d = {}
61 for cls in formatter_classes:
61 for cls in formatter_classes:
62 f = cls(config=self.config)
62 f = cls(config=self.config)
63 d[f.format_type] = f
63 d[f.format_type] = f
64 return d
64 return d
65
65
66 def format(self, obj, include=None, exclude=None):
66 def format(self, obj, include=None, exclude=None):
67 """Return a format data dict for an object.
67 """Return a format data dict for an object.
68
68
69 By default all format types will be computed.
69 By default all format types will be computed.
70
70
71 The following MIME types are currently implemented:
71 The following MIME types are currently implemented:
72
72
73 * text/plain
73 * text/plain
74 * text/html
74 * text/html
75 * text/latex
75 * text/latex
76 * application/json
76 * application/json
77 * application/javascript
77 * application/javascript
78 * image/png
78 * image/png
79 * image/jpeg
79 * image/jpeg
80 * image/svg+xml
80 * image/svg+xml
81
81
82 Parameters
82 Parameters
83 ----------
83 ----------
84 obj : object
84 obj : object
85 The Python object whose format data will be computed.
85 The Python object whose format data will be computed.
86 include : list or tuple, optional
86 include : list or tuple, optional
87 A list of format type strings (MIME types) to include in the
87 A list of format type strings (MIME types) to include in the
88 format data dict. If this is set *only* the format types included
88 format data dict. If this is set *only* the format types included
89 in this list will be computed.
89 in this list will be computed.
90 exclude : list or tuple, optional
90 exclude : list or tuple, optional
91 A list of format type string (MIME types) to exclue in the format
91 A list of format type string (MIME types) to exclue in the format
92 data dict. If this is set all format types will be computed,
92 data dict. If this is set all format types will be computed,
93 except for those included in this argument.
93 except for those included in this argument.
94
94
95 Returns
95 Returns
96 -------
96 -------
97 format_dict : dict
97 format_dict : dict
98 A dictionary of key/value pairs, one or each format that was
98 A dictionary of key/value pairs, one or each format that was
99 generated for the object. The keys are the format types, which
99 generated for the object. The keys are the format types, which
100 will usually be MIME type strings and the values and JSON'able
100 will usually be MIME type strings and the values and JSON'able
101 data structure containing the raw data for the representation in
101 data structure containing the raw data for the representation in
102 that format.
102 that format.
103 """
103 """
104 format_dict = {}
104 format_dict = {}
105
105
106 # If plain text only is active
106 # If plain text only is active
107 if self.plain_text_only:
107 if self.plain_text_only:
108 formatter = self.formatters['text/plain']
108 formatter = self.formatters['text/plain']
109 try:
109 try:
110 data = formatter(obj)
110 data = formatter(obj)
111 except:
111 except:
112 # FIXME: log the exception
112 # FIXME: log the exception
113 raise
113 raise
114 if data is not None:
114 if data is not None:
115 format_dict['text/plain'] = data
115 format_dict['text/plain'] = data
116 return format_dict
116 return format_dict
117
117
118 for format_type, formatter in self.formatters.items():
118 for format_type, formatter in self.formatters.items():
119 if include is not None:
119 if include is not None:
120 if format_type not in include:
120 if format_type not in include:
121 continue
121 continue
122 if exclude is not None:
122 if exclude is not None:
123 if format_type in exclude:
123 if format_type in exclude:
124 continue
124 continue
125 try:
125 try:
126 data = formatter(obj)
126 data = formatter(obj)
127 except:
127 except:
128 # FIXME: log the exception
128 # FIXME: log the exception
129 raise
129 raise
130 if data is not None:
130 if data is not None:
131 format_dict[format_type] = data
131 format_dict[format_type] = data
132 return format_dict
132 return format_dict
133
133
134 @property
134 @property
135 def format_types(self):
135 def format_types(self):
136 """Return the format types (MIME types) of the active formatters."""
136 """Return the format types (MIME types) of the active formatters."""
137 return self.formatters.keys()
137 return self.formatters.keys()
138
138
139
139
140 #-----------------------------------------------------------------------------
140 #-----------------------------------------------------------------------------
141 # Formatters for specific format types (text, html, svg, etc.)
141 # Formatters for specific format types (text, html, svg, etc.)
142 #-----------------------------------------------------------------------------
142 #-----------------------------------------------------------------------------
143
143
144
144
145 class FormatterABC(object):
145 class FormatterABC(object):
146 """ Abstract base class for Formatters.
146 """ Abstract base class for Formatters.
147
147
148 A formatter is a callable class that is responsible for computing the
148 A formatter is a callable class that is responsible for computing the
149 raw format data for a particular format type (MIME type). For example,
149 raw format data for a particular format type (MIME type). For example,
150 an HTML formatter would have a format type of `text/html` and would return
150 an HTML formatter would have a format type of `text/html` and would return
151 the HTML representation of the object when called.
151 the HTML representation of the object when called.
152 """
152 """
153 __metaclass__ = abc.ABCMeta
153 __metaclass__ = abc.ABCMeta
154
154
155 # The format type of the data returned, usually a MIME type.
155 # The format type of the data returned, usually a MIME type.
156 format_type = 'text/plain'
156 format_type = 'text/plain'
157
157
158 # Is the formatter enabled...
158 # Is the formatter enabled...
159 enabled = True
159 enabled = True
160
160
161 @abc.abstractmethod
161 @abc.abstractmethod
162 def __call__(self, obj):
162 def __call__(self, obj):
163 """Return a JSON'able representation of the object.
163 """Return a JSON'able representation of the object.
164
164
165 If the object cannot be formatted by this formatter, then return None
165 If the object cannot be formatted by this formatter, then return None
166 """
166 """
167 try:
167 try:
168 return repr(obj)
168 return repr(obj)
169 except TypeError:
169 except TypeError:
170 return None
170 return None
171
171
172
172
173 class BaseFormatter(Configurable):
173 class BaseFormatter(Configurable):
174 """A base formatter class that is configurable.
174 """A base formatter class that is configurable.
175
175
176 This formatter should usually be used as the base class of all formatters.
176 This formatter should usually be used as the base class of all formatters.
177 It is a traited :class:`Configurable` class and includes an extensible
177 It is a traited :class:`Configurable` class and includes an extensible
178 API for users to determine how their objects are formatted. The following
178 API for users to determine how their objects are formatted. The following
179 logic is used to find a function to format an given object.
179 logic is used to find a function to format an given object.
180
180
181 1. The object is introspected to see if it has a method with the name
181 1. The object is introspected to see if it has a method with the name
182 :attr:`print_method`. If is does, that object is passed to that method
182 :attr:`print_method`. If is does, that object is passed to that method
183 for formatting.
183 for formatting.
184 2. If no print method is found, three internal dictionaries are consulted
184 2. If no print method is found, three internal dictionaries are consulted
185 to find print method: :attr:`singleton_printers`, :attr:`type_printers`
185 to find print method: :attr:`singleton_printers`, :attr:`type_printers`
186 and :attr:`deferred_printers`.
186 and :attr:`deferred_printers`.
187
187
188 Users should use these dictionaries to register functions that will be
188 Users should use these dictionaries to register functions that will be
189 used to compute the format data for their objects (if those objects don't
189 used to compute the format data for their objects (if those objects don't
190 have the special print methods). The easiest way of using these
190 have the special print methods). The easiest way of using these
191 dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name`
191 dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name`
192 methods.
192 methods.
193
193
194 If no function/callable is found to compute the format data, ``None`` is
194 If no function/callable is found to compute the format data, ``None`` is
195 returned and this format type is not used.
195 returned and this format type is not used.
196 """
196 """
197
197
198 format_type = Unicode('text/plain')
198 format_type = Unicode('text/plain')
199
199
200 enabled = Bool(True, config=True)
200 enabled = Bool(True, config=True)
201
201
202 print_method = ObjectName('__repr__')
202 print_method = ObjectName('__repr__')
203
203
204 # The singleton printers.
204 # The singleton printers.
205 # Maps the IDs of the builtin singleton objects to the format functions.
205 # Maps the IDs of the builtin singleton objects to the format functions.
206 singleton_printers = Dict(config=True)
206 singleton_printers = Dict(config=True)
207 def _singleton_printers_default(self):
207 def _singleton_printers_default(self):
208 return {}
208 return {}
209
209
210 # The type-specific printers.
210 # The type-specific printers.
211 # Map type objects to the format functions.
211 # Map type objects to the format functions.
212 type_printers = Dict(config=True)
212 type_printers = Dict(config=True)
213 def _type_printers_default(self):
213 def _type_printers_default(self):
214 return {}
214 return {}
215
215
216 # The deferred-import type-specific printers.
216 # The deferred-import type-specific printers.
217 # Map (modulename, classname) pairs to the format functions.
217 # Map (modulename, classname) pairs to the format functions.
218 deferred_printers = Dict(config=True)
218 deferred_printers = Dict(config=True)
219 def _deferred_printers_default(self):
219 def _deferred_printers_default(self):
220 return {}
220 return {}
221
221
222 def __call__(self, obj):
222 def __call__(self, obj):
223 """Compute the format for an object."""
223 """Compute the format for an object."""
224 if self.enabled:
224 if self.enabled:
225 obj_id = id(obj)
225 obj_id = id(obj)
226 try:
226 try:
227 obj_class = getattr(obj, '__class__', None) or type(obj)
227 obj_class = getattr(obj, '__class__', None) or type(obj)
228 # First try to find registered singleton printers for the type.
228 # First try to find registered singleton printers for the type.
229 try:
229 try:
230 printer = self.singleton_printers[obj_id]
230 printer = self.singleton_printers[obj_id]
231 except (TypeError, KeyError):
231 except (TypeError, KeyError):
232 pass
232 pass
233 else:
233 else:
234 return printer(obj)
234 return printer(obj)
235 # Next look for type_printers.
235 # Next look for type_printers.
236 for cls in pretty._get_mro(obj_class):
236 for cls in pretty._get_mro(obj_class):
237 if cls in self.type_printers:
237 if cls in self.type_printers:
238 return self.type_printers[cls](obj)
238 return self.type_printers[cls](obj)
239 else:
239 else:
240 printer = self._in_deferred_types(cls)
240 printer = self._in_deferred_types(cls)
241 if printer is not None:
241 if printer is not None:
242 return printer(obj)
242 return printer(obj)
243 # Finally look for special method names.
243 # Finally look for special method names.
244 if hasattr(obj_class, self.print_method):
244 if hasattr(obj_class, self.print_method):
245 printer = getattr(obj_class, self.print_method)
245 printer = getattr(obj_class, self.print_method)
246 return printer(obj)
246 return printer(obj)
247 return None
247 return None
248 except Exception:
248 except Exception:
249 pass
249 pass
250 else:
250 else:
251 return None
251 return None
252
252
253 def for_type(self, typ, func):
253 def for_type(self, typ, func):
254 """Add a format function for a given type.
254 """Add a format function for a given type.
255
255
256 Parameters
256 Parameters
257 -----------
257 -----------
258 typ : class
258 typ : class
259 The class of the object that will be formatted using `func`.
259 The class of the object that will be formatted using `func`.
260 func : callable
260 func : callable
261 The callable that will be called to compute the format data. The
261 The callable that will be called to compute the format data. The
262 call signature of this function is simple, it must take the
262 call signature of this function is simple, it must take the
263 object to be formatted and return the raw data for the given
263 object to be formatted and return the raw data for the given
264 format. Subclasses may use a different call signature for the
264 format. Subclasses may use a different call signature for the
265 `func` argument.
265 `func` argument.
266 """
266 """
267 oldfunc = self.type_printers.get(typ, None)
267 oldfunc = self.type_printers.get(typ, None)
268 if func is not None:
268 if func is not None:
269 # To support easy restoration of old printers, we need to ignore
269 # To support easy restoration of old printers, we need to ignore
270 # Nones.
270 # Nones.
271 self.type_printers[typ] = func
271 self.type_printers[typ] = func
272 return oldfunc
272 return oldfunc
273
273
274 def for_type_by_name(self, type_module, type_name, func):
274 def for_type_by_name(self, type_module, type_name, func):
275 """Add a format function for a type specified by the full dotted
275 """Add a format function for a type specified by the full dotted
276 module and name of the type, rather than the type of the object.
276 module and name of the type, rather than the type of the object.
277
277
278 Parameters
278 Parameters
279 ----------
279 ----------
280 type_module : str
280 type_module : str
281 The full dotted name of the module the type is defined in, like
281 The full dotted name of the module the type is defined in, like
282 ``numpy``.
282 ``numpy``.
283 type_name : str
283 type_name : str
284 The name of the type (the class name), like ``dtype``
284 The name of the type (the class name), like ``dtype``
285 func : callable
285 func : callable
286 The callable that will be called to compute the format data. The
286 The callable that will be called to compute the format data. The
287 call signature of this function is simple, it must take the
287 call signature of this function is simple, it must take the
288 object to be formatted and return the raw data for the given
288 object to be formatted and return the raw data for the given
289 format. Subclasses may use a different call signature for the
289 format. Subclasses may use a different call signature for the
290 `func` argument.
290 `func` argument.
291 """
291 """
292 key = (type_module, type_name)
292 key = (type_module, type_name)
293 oldfunc = self.deferred_printers.get(key, None)
293 oldfunc = self.deferred_printers.get(key, None)
294 if func is not None:
294 if func is not None:
295 # To support easy restoration of old printers, we need to ignore
295 # To support easy restoration of old printers, we need to ignore
296 # Nones.
296 # Nones.
297 self.deferred_printers[key] = func
297 self.deferred_printers[key] = func
298 return oldfunc
298 return oldfunc
299
299
300 def _in_deferred_types(self, cls):
300 def _in_deferred_types(self, cls):
301 """
301 """
302 Check if the given class is specified in the deferred type registry.
302 Check if the given class is specified in the deferred type registry.
303
303
304 Returns the printer from the registry if it exists, and None if the
304 Returns the printer from the registry if it exists, and None if the
305 class is not in the registry. Successful matches will be moved to the
305 class is not in the registry. Successful matches will be moved to the
306 regular type registry for future use.
306 regular type registry for future use.
307 """
307 """
308 mod = getattr(cls, '__module__', None)
308 mod = getattr(cls, '__module__', None)
309 name = getattr(cls, '__name__', None)
309 name = getattr(cls, '__name__', None)
310 key = (mod, name)
310 key = (mod, name)
311 printer = None
311 printer = None
312 if key in self.deferred_printers:
312 if key in self.deferred_printers:
313 # Move the printer over to the regular registry.
313 # Move the printer over to the regular registry.
314 printer = self.deferred_printers.pop(key)
314 printer = self.deferred_printers.pop(key)
315 self.type_printers[cls] = printer
315 self.type_printers[cls] = printer
316 return printer
316 return printer
317
317
318
318
319 class PlainTextFormatter(BaseFormatter):
319 class PlainTextFormatter(BaseFormatter):
320 """The default pretty-printer.
320 """The default pretty-printer.
321
321
322 This uses :mod:`IPython.external.pretty` to compute the format data of
322 This uses :mod:`IPython.external.pretty` to compute the format data of
323 the object. If the object cannot be pretty printed, :func:`repr` is used.
323 the object. If the object cannot be pretty printed, :func:`repr` is used.
324 See the documentation of :mod:`IPython.external.pretty` for details on
324 See the documentation of :mod:`IPython.external.pretty` for details on
325 how to write pretty printers. Here is a simple example::
325 how to write pretty printers. Here is a simple example::
326
326
327 def dtype_pprinter(obj, p, cycle):
327 def dtype_pprinter(obj, p, cycle):
328 if cycle:
328 if cycle:
329 return p.text('dtype(...)')
329 return p.text('dtype(...)')
330 if hasattr(obj, 'fields'):
330 if hasattr(obj, 'fields'):
331 if obj.fields is None:
331 if obj.fields is None:
332 p.text(repr(obj))
332 p.text(repr(obj))
333 else:
333 else:
334 p.begin_group(7, 'dtype([')
334 p.begin_group(7, 'dtype([')
335 for i, field in enumerate(obj.descr):
335 for i, field in enumerate(obj.descr):
336 if i > 0:
336 if i > 0:
337 p.text(',')
337 p.text(',')
338 p.breakable()
338 p.breakable()
339 p.pretty(field)
339 p.pretty(field)
340 p.end_group(7, '])')
340 p.end_group(7, '])')
341 """
341 """
342
342
343 # The format type of data returned.
343 # The format type of data returned.
344 format_type = Unicode('text/plain')
344 format_type = Unicode('text/plain')
345
345
346 # This subclass ignores this attribute as it always need to return
346 # This subclass ignores this attribute as it always need to return
347 # something.
347 # something.
348 enabled = Bool(True, config=False)
348 enabled = Bool(True, config=False)
349
349
350 # Look for a _repr_pretty_ methods to use for pretty printing.
350 # Look for a _repr_pretty_ methods to use for pretty printing.
351 print_method = ObjectName('_repr_pretty_')
351 print_method = ObjectName('_repr_pretty_')
352
352
353 # Whether to pretty-print or not.
353 # Whether to pretty-print or not.
354 pprint = Bool(True, config=True)
354 pprint = Bool(True, config=True)
355
355
356 # Whether to be verbose or not.
356 # Whether to be verbose or not.
357 verbose = Bool(False, config=True)
357 verbose = Bool(False, config=True)
358
358
359 # The maximum width.
359 # The maximum width.
360 max_width = Int(79, config=True)
360 max_width = Int(79, config=True)
361
361
362 # The newline character.
362 # The newline character.
363 newline = Unicode('\n', config=True)
363 newline = Unicode('\n', config=True)
364
364
365 # format-string for pprinting floats
365 # format-string for pprinting floats
366 float_format = Unicode('%r')
366 float_format = Unicode('%r')
367 # setter for float precision, either int or direct format-string
367 # setter for float precision, either int or direct format-string
368 float_precision = CUnicode('', config=True)
368 float_precision = CUnicode('', config=True)
369
369
370 def _float_precision_changed(self, name, old, new):
370 def _float_precision_changed(self, name, old, new):
371 """float_precision changed, set float_format accordingly.
371 """float_precision changed, set float_format accordingly.
372
372
373 float_precision can be set by int or str.
373 float_precision can be set by int or str.
374 This will set float_format, after interpreting input.
374 This will set float_format, after interpreting input.
375 If numpy has been imported, numpy print precision will also be set.
375 If numpy has been imported, numpy print precision will also be set.
376
376
377 integer `n` sets format to '%.nf', otherwise, format set directly.
377 integer `n` sets format to '%.nf', otherwise, format set directly.
378
378
379 An empty string returns to defaults (repr for float, 8 for numpy).
379 An empty string returns to defaults (repr for float, 8 for numpy).
380
380
381 This parameter can be set via the '%precision' magic.
381 This parameter can be set via the '%precision' magic.
382 """
382 """
383
383
384 if '%' in new:
384 if '%' in new:
385 # got explicit format string
385 # got explicit format string
386 fmt = new
386 fmt = new
387 try:
387 try:
388 fmt%3.14159
388 fmt%3.14159
389 except Exception:
389 except Exception:
390 raise ValueError("Precision must be int or format string, not %r"%new)
390 raise ValueError("Precision must be int or format string, not %r"%new)
391 elif new:
391 elif new:
392 # otherwise, should be an int
392 # otherwise, should be an int
393 try:
393 try:
394 i = int(new)
394 i = int(new)
395 assert i >= 0
395 assert i >= 0
396 except ValueError:
396 except ValueError:
397 raise ValueError("Precision must be int or format string, not %r"%new)
397 raise ValueError("Precision must be int or format string, not %r"%new)
398 except AssertionError:
398 except AssertionError:
399 raise ValueError("int precision must be non-negative, not %r"%i)
399 raise ValueError("int precision must be non-negative, not %r"%i)
400
400
401 fmt = '%%.%if'%i
401 fmt = '%%.%if'%i
402 if 'numpy' in sys.modules:
402 if 'numpy' in sys.modules:
403 # set numpy precision if it has been imported
403 # set numpy precision if it has been imported
404 import numpy
404 import numpy
405 numpy.set_printoptions(precision=i)
405 numpy.set_printoptions(precision=i)
406 else:
406 else:
407 # default back to repr
407 # default back to repr
408 fmt = '%r'
408 fmt = '%r'
409 if 'numpy' in sys.modules:
409 if 'numpy' in sys.modules:
410 import numpy
410 import numpy
411 # numpy default is 8
411 # numpy default is 8
412 numpy.set_printoptions(precision=8)
412 numpy.set_printoptions(precision=8)
413 self.float_format = fmt
413 self.float_format = fmt
414
414
415 # Use the default pretty printers from IPython.external.pretty.
415 # Use the default pretty printers from IPython.external.pretty.
416 def _singleton_printers_default(self):
416 def _singleton_printers_default(self):
417 return pretty._singleton_pprinters.copy()
417 return pretty._singleton_pprinters.copy()
418
418
419 def _type_printers_default(self):
419 def _type_printers_default(self):
420 d = pretty._type_pprinters.copy()
420 d = pretty._type_pprinters.copy()
421 d[float] = lambda obj,p,cycle: p.text(self.float_format%obj)
421 d[float] = lambda obj,p,cycle: p.text(self.float_format%obj)
422 return d
422 return d
423
423
424 def _deferred_printers_default(self):
424 def _deferred_printers_default(self):
425 return pretty._deferred_type_pprinters.copy()
425 return pretty._deferred_type_pprinters.copy()
426
426
427 #### FormatterABC interface ####
427 #### FormatterABC interface ####
428
428
429 def __call__(self, obj):
429 def __call__(self, obj):
430 """Compute the pretty representation of the object."""
430 """Compute the pretty representation of the object."""
431 if not self.pprint:
431 if not self.pprint:
432 try:
432 try:
433 return repr(obj)
433 return repr(obj)
434 except TypeError:
434 except TypeError:
435 return ''
435 return ''
436 else:
436 else:
437 # This uses use StringIO, as cStringIO doesn't handle unicode.
437 # This uses use StringIO, as cStringIO doesn't handle unicode.
438 stream = StringIO()
438 stream = StringIO()
439 # self.newline.encode() is a quick fix for issue gh-597. We need to
439 # self.newline.encode() is a quick fix for issue gh-597. We need to
440 # ensure that stream does not get a mix of unicode and bytestrings,
440 # ensure that stream does not get a mix of unicode and bytestrings,
441 # or it will cause trouble.
441 # or it will cause trouble.
442 printer = pretty.RepresentationPrinter(stream, self.verbose,
442 printer = pretty.RepresentationPrinter(stream, self.verbose,
443 self.max_width, unicode_to_str(self.newline),
443 self.max_width, unicode_to_str(self.newline),
444 singleton_pprinters=self.singleton_printers,
444 singleton_pprinters=self.singleton_printers,
445 type_pprinters=self.type_printers,
445 type_pprinters=self.type_printers,
446 deferred_pprinters=self.deferred_printers)
446 deferred_pprinters=self.deferred_printers)
447 printer.pretty(obj)
447 printer.pretty(obj)
448 printer.flush()
448 printer.flush()
449 return stream.getvalue()
449 return stream.getvalue()
450
450
451
451
452 class HTMLFormatter(BaseFormatter):
452 class HTMLFormatter(BaseFormatter):
453 """An HTML formatter.
453 """An HTML formatter.
454
454
455 To define the callables that compute the HTML representation of your
455 To define the callables that compute the HTML representation of your
456 objects, define a :meth:`_repr_html_` method or use the :meth:`for_type`
456 objects, define a :meth:`_repr_html_` method or use the :meth:`for_type`
457 or :meth:`for_type_by_name` methods to register functions that handle
457 or :meth:`for_type_by_name` methods to register functions that handle
458 this.
458 this.
459
459
460 The return value of this formatter should be a valid HTML snippet that
460 The return value of this formatter should be a valid HTML snippet that
461 could be injected into an existing DOM. It should *not* include the
461 could be injected into an existing DOM. It should *not* include the
462 ```<html>`` or ```<body>`` tags.
462 ```<html>`` or ```<body>`` tags.
463 """
463 """
464 format_type = Unicode('text/html')
464 format_type = Unicode('text/html')
465
465
466 print_method = ObjectName('_repr_html_')
466 print_method = ObjectName('_repr_html_')
467
467
468
468
469 class SVGFormatter(BaseFormatter):
469 class SVGFormatter(BaseFormatter):
470 """An SVG formatter.
470 """An SVG formatter.
471
471
472 To define the callables that compute the SVG representation of your
472 To define the callables that compute the SVG representation of your
473 objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type`
473 objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type`
474 or :meth:`for_type_by_name` methods to register functions that handle
474 or :meth:`for_type_by_name` methods to register functions that handle
475 this.
475 this.
476
476
477 The return value of this formatter should be valid SVG enclosed in
477 The return value of this formatter should be valid SVG enclosed in
478 ```<svg>``` tags, that could be injected into an existing DOM. It should
478 ```<svg>``` tags, that could be injected into an existing DOM. It should
479 *not* include the ```<html>`` or ```<body>`` tags.
479 *not* include the ```<html>`` or ```<body>`` tags.
480 """
480 """
481 format_type = Unicode('image/svg+xml')
481 format_type = Unicode('image/svg+xml')
482
482
483 print_method = ObjectName('_repr_svg_')
483 print_method = ObjectName('_repr_svg_')
484
484
485
485
486 class PNGFormatter(BaseFormatter):
486 class PNGFormatter(BaseFormatter):
487 """A PNG formatter.
487 """A PNG formatter.
488
488
489 To define the callables that compute the PNG representation of your
489 To define the callables that compute the PNG representation of your
490 objects, define a :meth:`_repr_png_` method or use the :meth:`for_type`
490 objects, define a :meth:`_repr_png_` method or use the :meth:`for_type`
491 or :meth:`for_type_by_name` methods to register functions that handle
491 or :meth:`for_type_by_name` methods to register functions that handle
492 this.
492 this.
493
493
494 The return value of this formatter should be raw PNG data, *not*
494 The return value of this formatter should be raw PNG data, *not*
495 base64 encoded.
495 base64 encoded.
496 """
496 """
497 format_type = Unicode('image/png')
497 format_type = Unicode('image/png')
498
498
499 print_method = ObjectName('_repr_png_')
499 print_method = ObjectName('_repr_png_')
500
500
501
501
502 class JPEGFormatter(BaseFormatter):
502 class JPEGFormatter(BaseFormatter):
503 """A JPEG formatter.
503 """A JPEG formatter.
504
504
505 To define the callables that compute the JPEG representation of your
505 To define the callables that compute the JPEG representation of your
506 objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type`
506 objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type`
507 or :meth:`for_type_by_name` methods to register functions that handle
507 or :meth:`for_type_by_name` methods to register functions that handle
508 this.
508 this.
509
509
510 The return value of this formatter should be raw JPEG data, *not*
510 The return value of this formatter should be raw JPEG data, *not*
511 base64 encoded.
511 base64 encoded.
512 """
512 """
513 format_type = Unicode('image/jpeg')
513 format_type = Unicode('image/jpeg')
514
514
515 print_method = ObjectName('_repr_jpeg_')
515 print_method = ObjectName('_repr_jpeg_')
516
516
517
517
518 class LatexFormatter(BaseFormatter):
518 class LatexFormatter(BaseFormatter):
519 """A LaTeX formatter.
519 """A LaTeX formatter.
520
520
521 To define the callables that compute the LaTeX representation of your
521 To define the callables that compute the LaTeX representation of your
522 objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type`
522 objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type`
523 or :meth:`for_type_by_name` methods to register functions that handle
523 or :meth:`for_type_by_name` methods to register functions that handle
524 this.
524 this.
525
525
526 The return value of this formatter should be a valid LaTeX equation,
526 The return value of this formatter should be a valid LaTeX equation,
527 enclosed in either ```$``` or ```$$```.
527 enclosed in either ```$``` or ```$$```.
528 """
528 """
529 format_type = Unicode('text/latex')
529 format_type = Unicode('text/latex')
530
530
531 print_method = ObjectName('_repr_latex_')
531 print_method = ObjectName('_repr_latex_')
532
532
533
533
534 class JSONFormatter(BaseFormatter):
534 class JSONFormatter(BaseFormatter):
535 """A JSON string formatter.
535 """A JSON string formatter.
536
536
537 To define the callables that compute the JSON string representation of
537 To define the callables that compute the JSON string representation of
538 your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type`
538 your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type`
539 or :meth:`for_type_by_name` methods to register functions that handle
539 or :meth:`for_type_by_name` methods to register functions that handle
540 this.
540 this.
541
541
542 The return value of this formatter should be a valid JSON string.
542 The return value of this formatter should be a valid JSON string.
543 """
543 """
544 format_type = Unicode('application/json')
544 format_type = Unicode('application/json')
545
545
546 print_method = ObjectName('_repr_json_')
546 print_method = ObjectName('_repr_json_')
547
547
548
548
549 class JavascriptFormatter(BaseFormatter):
549 class JavascriptFormatter(BaseFormatter):
550 """A Javascript formatter.
550 """A Javascript formatter.
551
551
552 To define the callables that compute the Javascript representation of
552 To define the callables that compute the Javascript representation of
553 your objects, define a :meth:`_repr_javascript_` method or use the
553 your objects, define a :meth:`_repr_javascript_` method or use the
554 :meth:`for_type` or :meth:`for_type_by_name` methods to register functions
554 :meth:`for_type` or :meth:`for_type_by_name` methods to register functions
555 that handle this.
555 that handle this.
556
556
557 The return value of this formatter should be valid Javascript code and
557 The return value of this formatter should be valid Javascript code and
558 should *not* be enclosed in ```<script>``` tags.
558 should *not* be enclosed in ```<script>``` tags.
559 """
559 """
560 format_type = Unicode('application/javascript')
560 format_type = Unicode('application/javascript')
561
561
562 print_method = ObjectName('_repr_javascript_')
562 print_method = ObjectName('_repr_javascript_')
563
563
564 FormatterABC.register(BaseFormatter)
564 FormatterABC.register(BaseFormatter)
565 FormatterABC.register(PlainTextFormatter)
565 FormatterABC.register(PlainTextFormatter)
566 FormatterABC.register(HTMLFormatter)
566 FormatterABC.register(HTMLFormatter)
567 FormatterABC.register(SVGFormatter)
567 FormatterABC.register(SVGFormatter)
568 FormatterABC.register(PNGFormatter)
568 FormatterABC.register(PNGFormatter)
569 FormatterABC.register(JPEGFormatter)
569 FormatterABC.register(JPEGFormatter)
570 FormatterABC.register(LatexFormatter)
570 FormatterABC.register(LatexFormatter)
571 FormatterABC.register(JSONFormatter)
571 FormatterABC.register(JSONFormatter)
572 FormatterABC.register(JavascriptFormatter)
572 FormatterABC.register(JavascriptFormatter)
573
573
574
574
575 def format_display_data(obj, include=None, exclude=None):
575 def format_display_data(obj, include=None, exclude=None):
576 """Return a format data dict for an object.
576 """Return a format data dict for an object.
577
577
578 By default all format types will be computed.
578 By default all format types will be computed.
579
579
580 The following MIME types are currently implemented:
580 The following MIME types are currently implemented:
581
581
582 * text/plain
582 * text/plain
583 * text/html
583 * text/html
584 * text/latex
584 * text/latex
585 * application/json
585 * application/json
586 * application/javascript
586 * application/javascript
587 * image/png
587 * image/png
588 * image/jpeg
588 * image/jpeg
589 * image/svg+xml
589 * image/svg+xml
590
590
591 Parameters
591 Parameters
592 ----------
592 ----------
593 obj : object
593 obj : object
594 The Python object whose format data will be computed.
594 The Python object whose format data will be computed.
595
595
596 Returns
596 Returns
597 -------
597 -------
598 format_dict : dict
598 format_dict : dict
599 A dictionary of key/value pairs, one or each format that was
599 A dictionary of key/value pairs, one or each format that was
600 generated for the object. The keys are the format types, which
600 generated for the object. The keys are the format types, which
601 will usually be MIME type strings and the values and JSON'able
601 will usually be MIME type strings and the values and JSON'able
602 data structure containing the raw data for the representation in
602 data structure containing the raw data for the representation in
603 that format.
603 that format.
604 include : list or tuple, optional
604 include : list or tuple, optional
605 A list of format type strings (MIME types) to include in the
605 A list of format type strings (MIME types) to include in the
606 format data dict. If this is set *only* the format types included
606 format data dict. If this is set *only* the format types included
607 in this list will be computed.
607 in this list will be computed.
608 exclude : list or tuple, optional
608 exclude : list or tuple, optional
609 A list of format type string (MIME types) to exclue in the format
609 A list of format type string (MIME types) to exclue in the format
610 data dict. If this is set all format types will be computed,
610 data dict. If this is set all format types will be computed,
611 except for those included in this argument.
611 except for those included in this argument.
612 """
612 """
613 from IPython.core.interactiveshell import InteractiveShell
613 from IPython.core.interactiveshell import InteractiveShell
614
614
615 InteractiveShell.instance().display_formatter.format(
615 InteractiveShell.instance().display_formatter.format(
616 obj,
616 obj,
617 include,
617 include,
618 exclude
618 exclude
619 )
619 )
620
620
@@ -1,831 +1,831 b''
1 """ History related magics and functionality """
1 """ History related magics and functionality """
2 #-----------------------------------------------------------------------------
2 #-----------------------------------------------------------------------------
3 # Copyright (C) 2010 The IPython Development Team.
3 # Copyright (C) 2010 The IPython Development Team.
4 #
4 #
5 # Distributed under the terms of the BSD License.
5 # Distributed under the terms of the BSD License.
6 #
6 #
7 # The full license is in the file COPYING.txt, distributed with this software.
7 # The full license is in the file COPYING.txt, distributed with this software.
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Imports
11 # Imports
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 from __future__ import print_function
13 from __future__ import print_function
14
14
15 # Stdlib imports
15 # Stdlib imports
16 import atexit
16 import atexit
17 import datetime
17 import datetime
18 import os
18 import os
19 import re
19 import re
20 import sqlite3
20 import sqlite3
21 import threading
21 import threading
22
22
23 # Our own packages
23 # Our own packages
24 from IPython.config.configurable import Configurable
24 from IPython.config.configurable import Configurable
25
25
26 from IPython.testing.skipdoctest import skip_doctest
26 from IPython.testing.skipdoctest import skip_doctest
27 from IPython.utils import io
27 from IPython.utils import io
28 from IPython.utils.traitlets import Bool, Dict, Instance, Int, CInt, List, Unicode
28 from IPython.utils.traitlets import Bool, Dict, Instance, Int, CInt, List, Unicode
29 from IPython.utils.warn import warn
29 from IPython.utils.warn import warn
30
30
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32 # Classes and functions
32 # Classes and functions
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34
34
35 class HistoryManager(Configurable):
35 class HistoryManager(Configurable):
36 """A class to organize all history-related functionality in one place.
36 """A class to organize all history-related functionality in one place.
37 """
37 """
38 # Public interface
38 # Public interface
39
39
40 # An instance of the IPython shell we are attached to
40 # An instance of the IPython shell we are attached to
41 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
41 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
42 # Lists to hold processed and raw history. These start with a blank entry
42 # Lists to hold processed and raw history. These start with a blank entry
43 # so that we can index them starting from 1
43 # so that we can index them starting from 1
44 input_hist_parsed = List([""])
44 input_hist_parsed = List([""])
45 input_hist_raw = List([""])
45 input_hist_raw = List([""])
46 # A list of directories visited during session
46 # A list of directories visited during session
47 dir_hist = List()
47 dir_hist = List()
48 def _dir_hist_default(self):
48 def _dir_hist_default(self):
49 try:
49 try:
50 return [os.getcwdu()]
50 return [os.getcwdu()]
51 except OSError:
51 except OSError:
52 return []
52 return []
53
53
54 # A dict of output history, keyed with ints from the shell's
54 # A dict of output history, keyed with ints from the shell's
55 # execution count.
55 # execution count.
56 output_hist = Dict()
56 output_hist = Dict()
57 # The text/plain repr of outputs.
57 # The text/plain repr of outputs.
58 output_hist_reprs = Dict()
58 output_hist_reprs = Dict()
59
59
60 # String holding the path to the history file
60 # String holding the path to the history file
61 hist_file = Unicode(config=True)
61 hist_file = Unicode(config=True)
62
62
63 # The SQLite database
63 # The SQLite database
64 db = Instance(sqlite3.Connection)
64 db = Instance(sqlite3.Connection)
65 # The number of the current session in the history database
65 # The number of the current session in the history database
66 session_number = CInt()
66 session_number = CInt()
67 # Should we log output to the database? (default no)
67 # Should we log output to the database? (default no)
68 db_log_output = Bool(False, config=True)
68 db_log_output = Bool(False, config=True)
69 # Write to database every x commands (higher values save disk access & power)
69 # Write to database every x commands (higher values save disk access & power)
70 # Values of 1 or less effectively disable caching.
70 # Values of 1 or less effectively disable caching.
71 db_cache_size = Int(0, config=True)
71 db_cache_size = Int(0, config=True)
72 # The input and output caches
72 # The input and output caches
73 db_input_cache = List()
73 db_input_cache = List()
74 db_output_cache = List()
74 db_output_cache = List()
75
75
76 # History saving in separate thread
76 # History saving in separate thread
77 save_thread = Instance('IPython.core.history.HistorySavingThread')
77 save_thread = Instance('IPython.core.history.HistorySavingThread')
78 try: # Event is a function returning an instance of _Event...
78 try: # Event is a function returning an instance of _Event...
79 save_flag = Instance(threading._Event)
79 save_flag = Instance(threading._Event)
80 except AttributeError: # ...until Python 3.3, when it's a class.
80 except AttributeError: # ...until Python 3.3, when it's a class.
81 save_flag = Instance(threading.Event)
81 save_flag = Instance(threading.Event)
82
82
83 # Private interface
83 # Private interface
84 # Variables used to store the three last inputs from the user. On each new
84 # Variables used to store the three last inputs from the user. On each new
85 # history update, we populate the user's namespace with these, shifted as
85 # history update, we populate the user's namespace with these, shifted as
86 # necessary.
86 # necessary.
87 _i00 = Unicode(u'')
87 _i00 = Unicode(u'')
88 _i = Unicode(u'')
88 _i = Unicode(u'')
89 _ii = Unicode(u'')
89 _ii = Unicode(u'')
90 _iii = Unicode(u'')
90 _iii = Unicode(u'')
91
91
92 # A regex matching all forms of the exit command, so that we don't store
92 # A regex matching all forms of the exit command, so that we don't store
93 # them in the history (it's annoying to rewind the first entry and land on
93 # them in the history (it's annoying to rewind the first entry and land on
94 # an exit call).
94 # an exit call).
95 _exit_re = re.compile(r"(exit|quit)(\s*\(.*\))?$")
95 _exit_re = re.compile(r"(exit|quit)(\s*\(.*\))?$")
96
96
97 def __init__(self, shell, config=None, **traits):
97 def __init__(self, shell, config=None, **traits):
98 """Create a new history manager associated with a shell instance.
98 """Create a new history manager associated with a shell instance.
99 """
99 """
100 # We need a pointer back to the shell for various tasks.
100 # We need a pointer back to the shell for various tasks.
101 super(HistoryManager, self).__init__(shell=shell, config=config,
101 super(HistoryManager, self).__init__(shell=shell, config=config,
102 **traits)
102 **traits)
103
103
104 if self.hist_file == u'':
104 if self.hist_file == u'':
105 # No one has set the hist_file, yet.
105 # No one has set the hist_file, yet.
106 histfname = 'history'
106 histfname = 'history'
107 self.hist_file = os.path.join(shell.profile_dir.location, histfname + '.sqlite')
107 self.hist_file = os.path.join(shell.profile_dir.location, histfname + '.sqlite')
108
108
109 try:
109 try:
110 self.init_db()
110 self.init_db()
111 except sqlite3.DatabaseError:
111 except sqlite3.DatabaseError:
112 if os.path.isfile(self.hist_file):
112 if os.path.isfile(self.hist_file):
113 # Try to move the file out of the way.
113 # Try to move the file out of the way.
114 newpath = os.path.join(self.shell.profile_dir.location, "hist-corrupt.sqlite")
114 newpath = os.path.join(self.shell.profile_dir.location, "hist-corrupt.sqlite")
115 os.rename(self.hist_file, newpath)
115 os.rename(self.hist_file, newpath)
116 print("ERROR! History file wasn't a valid SQLite database.",
116 print("ERROR! History file wasn't a valid SQLite database.",
117 "It was moved to %s" % newpath, "and a new file created.")
117 "It was moved to %s" % newpath, "and a new file created.")
118 self.init_db()
118 self.init_db()
119 else:
119 else:
120 # The hist_file is probably :memory: or something else.
120 # The hist_file is probably :memory: or something else.
121 raise
121 raise
122
122
123 self.save_flag = threading.Event()
123 self.save_flag = threading.Event()
124 self.db_input_cache_lock = threading.Lock()
124 self.db_input_cache_lock = threading.Lock()
125 self.db_output_cache_lock = threading.Lock()
125 self.db_output_cache_lock = threading.Lock()
126 self.save_thread = HistorySavingThread(self)
126 self.save_thread = HistorySavingThread(self)
127 self.save_thread.start()
127 self.save_thread.start()
128
128
129 self.new_session()
129 self.new_session()
130
130
131
131
132 def init_db(self):
132 def init_db(self):
133 """Connect to the database, and create tables if necessary."""
133 """Connect to the database, and create tables if necessary."""
134 # use detect_types so that timestamps return datetime objects
134 # use detect_types so that timestamps return datetime objects
135 self.db = sqlite3.connect(self.hist_file, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
135 self.db = sqlite3.connect(self.hist_file, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
136 self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
136 self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
137 primary key autoincrement, start timestamp,
137 primary key autoincrement, start timestamp,
138 end timestamp, num_cmds integer, remark text)""")
138 end timestamp, num_cmds integer, remark text)""")
139 self.db.execute("""CREATE TABLE IF NOT EXISTS history
139 self.db.execute("""CREATE TABLE IF NOT EXISTS history
140 (session integer, line integer, source text, source_raw text,
140 (session integer, line integer, source text, source_raw text,
141 PRIMARY KEY (session, line))""")
141 PRIMARY KEY (session, line))""")
142 # Output history is optional, but ensure the table's there so it can be
142 # Output history is optional, but ensure the table's there so it can be
143 # enabled later.
143 # enabled later.
144 self.db.execute("""CREATE TABLE IF NOT EXISTS output_history
144 self.db.execute("""CREATE TABLE IF NOT EXISTS output_history
145 (session integer, line integer, output text,
145 (session integer, line integer, output text,
146 PRIMARY KEY (session, line))""")
146 PRIMARY KEY (session, line))""")
147 self.db.commit()
147 self.db.commit()
148
148
149 def new_session(self, conn=None):
149 def new_session(self, conn=None):
150 """Get a new session number."""
150 """Get a new session number."""
151 if conn is None:
151 if conn is None:
152 conn = self.db
152 conn = self.db
153
153
154 with conn:
154 with conn:
155 cur = conn.execute("""INSERT INTO sessions VALUES (NULL, ?, NULL,
155 cur = conn.execute("""INSERT INTO sessions VALUES (NULL, ?, NULL,
156 NULL, "") """, (datetime.datetime.now(),))
156 NULL, "") """, (datetime.datetime.now(),))
157 self.session_number = cur.lastrowid
157 self.session_number = cur.lastrowid
158
158
159 def end_session(self):
159 def end_session(self):
160 """Close the database session, filling in the end time and line count."""
160 """Close the database session, filling in the end time and line count."""
161 self.writeout_cache()
161 self.writeout_cache()
162 with self.db:
162 with self.db:
163 self.db.execute("""UPDATE sessions SET end=?, num_cmds=? WHERE
163 self.db.execute("""UPDATE sessions SET end=?, num_cmds=? WHERE
164 session==?""", (datetime.datetime.now(),
164 session==?""", (datetime.datetime.now(),
165 len(self.input_hist_parsed)-1, self.session_number))
165 len(self.input_hist_parsed)-1, self.session_number))
166 self.session_number = 0
166 self.session_number = 0
167
167
168 def name_session(self, name):
168 def name_session(self, name):
169 """Give the current session a name in the history database."""
169 """Give the current session a name in the history database."""
170 with self.db:
170 with self.db:
171 self.db.execute("UPDATE sessions SET remark=? WHERE session==?",
171 self.db.execute("UPDATE sessions SET remark=? WHERE session==?",
172 (name, self.session_number))
172 (name, self.session_number))
173
173
174 def reset(self, new_session=True):
174 def reset(self, new_session=True):
175 """Clear the session history, releasing all object references, and
175 """Clear the session history, releasing all object references, and
176 optionally open a new session."""
176 optionally open a new session."""
177 self.output_hist.clear()
177 self.output_hist.clear()
178 # The directory history can't be completely empty
178 # The directory history can't be completely empty
179 self.dir_hist[:] = [os.getcwdu()]
179 self.dir_hist[:] = [os.getcwdu()]
180
180
181 if new_session:
181 if new_session:
182 if self.session_number:
182 if self.session_number:
183 self.end_session()
183 self.end_session()
184 self.input_hist_parsed[:] = [""]
184 self.input_hist_parsed[:] = [""]
185 self.input_hist_raw[:] = [""]
185 self.input_hist_raw[:] = [""]
186 self.new_session()
186 self.new_session()
187
187
188 ## -------------------------------
188 ## -------------------------------
189 ## Methods for retrieving history:
189 ## Methods for retrieving history:
190 ## -------------------------------
190 ## -------------------------------
191 def _run_sql(self, sql, params, raw=True, output=False):
191 def _run_sql(self, sql, params, raw=True, output=False):
192 """Prepares and runs an SQL query for the history database.
192 """Prepares and runs an SQL query for the history database.
193
193
194 Parameters
194 Parameters
195 ----------
195 ----------
196 sql : str
196 sql : str
197 Any filtering expressions to go after SELECT ... FROM ...
197 Any filtering expressions to go after SELECT ... FROM ...
198 params : tuple
198 params : tuple
199 Parameters passed to the SQL query (to replace "?")
199 Parameters passed to the SQL query (to replace "?")
200 raw, output : bool
200 raw, output : bool
201 See :meth:`get_range`
201 See :meth:`get_range`
202
202
203 Returns
203 Returns
204 -------
204 -------
205 Tuples as :meth:`get_range`
205 Tuples as :meth:`get_range`
206 """
206 """
207 toget = 'source_raw' if raw else 'source'
207 toget = 'source_raw' if raw else 'source'
208 sqlfrom = "history"
208 sqlfrom = "history"
209 if output:
209 if output:
210 sqlfrom = "history LEFT JOIN output_history USING (session, line)"
210 sqlfrom = "history LEFT JOIN output_history USING (session, line)"
211 toget = "history.%s, output_history.output" % toget
211 toget = "history.%s, output_history.output" % toget
212 cur = self.db.execute("SELECT session, line, %s FROM %s " %\
212 cur = self.db.execute("SELECT session, line, %s FROM %s " %\
213 (toget, sqlfrom) + sql, params)
213 (toget, sqlfrom) + sql, params)
214 if output: # Regroup into 3-tuples, and parse JSON
214 if output: # Regroup into 3-tuples, and parse JSON
215 return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur)
215 return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur)
216 return cur
216 return cur
217
217
218
218
219 def get_session_info(self, session=0):
219 def get_session_info(self, session=0):
220 """get info about a session
220 """get info about a session
221
221
222 Parameters
222 Parameters
223 ----------
223 ----------
224
224
225 session : int
225 session : int
226 Session number to retrieve. The current session is 0, and negative
226 Session number to retrieve. The current session is 0, and negative
227 numbers count back from current session, so -1 is previous session.
227 numbers count back from current session, so -1 is previous session.
228
228
229 Returns
229 Returns
230 -------
230 -------
231
231
232 (session_id [int], start [datetime], end [datetime], num_cmds [int], remark [unicode])
232 (session_id [int], start [datetime], end [datetime], num_cmds [int], remark [unicode])
233
233
234 Sessions that are running or did not exit cleanly will have `end=None`
234 Sessions that are running or did not exit cleanly will have `end=None`
235 and `num_cmds=None`.
235 and `num_cmds=None`.
236
236
237 """
237 """
238
238
239 if session <= 0:
239 if session <= 0:
240 session += self.session_number
240 session += self.session_number
241
241
242 query = "SELECT * from sessions where session == ?"
242 query = "SELECT * from sessions where session == ?"
243 return self.db.execute(query, (session,)).fetchone()
243 return self.db.execute(query, (session,)).fetchone()
244
244
245
245
246 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
246 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
247 """Get the last n lines from the history database.
247 """Get the last n lines from the history database.
248
248
249 Parameters
249 Parameters
250 ----------
250 ----------
251 n : int
251 n : int
252 The number of lines to get
252 The number of lines to get
253 raw, output : bool
253 raw, output : bool
254 See :meth:`get_range`
254 See :meth:`get_range`
255 include_latest : bool
255 include_latest : bool
256 If False (default), n+1 lines are fetched, and the latest one
256 If False (default), n+1 lines are fetched, and the latest one
257 is discarded. This is intended to be used where the function
257 is discarded. This is intended to be used where the function
258 is called by a user command, which it should not return.
258 is called by a user command, which it should not return.
259
259
260 Returns
260 Returns
261 -------
261 -------
262 Tuples as :meth:`get_range`
262 Tuples as :meth:`get_range`
263 """
263 """
264 self.writeout_cache()
264 self.writeout_cache()
265 if not include_latest:
265 if not include_latest:
266 n += 1
266 n += 1
267 cur = self._run_sql("ORDER BY session DESC, line DESC LIMIT ?",
267 cur = self._run_sql("ORDER BY session DESC, line DESC LIMIT ?",
268 (n,), raw=raw, output=output)
268 (n,), raw=raw, output=output)
269 if not include_latest:
269 if not include_latest:
270 return reversed(list(cur)[1:])
270 return reversed(list(cur)[1:])
271 return reversed(list(cur))
271 return reversed(list(cur))
272
272
273 def search(self, pattern="*", raw=True, search_raw=True,
273 def search(self, pattern="*", raw=True, search_raw=True,
274 output=False):
274 output=False):
275 """Search the database using unix glob-style matching (wildcards
275 """Search the database using unix glob-style matching (wildcards
276 * and ?).
276 * and ?).
277
277
278 Parameters
278 Parameters
279 ----------
279 ----------
280 pattern : str
280 pattern : str
281 The wildcarded pattern to match when searching
281 The wildcarded pattern to match when searching
282 search_raw : bool
282 search_raw : bool
283 If True, search the raw input, otherwise, the parsed input
283 If True, search the raw input, otherwise, the parsed input
284 raw, output : bool
284 raw, output : bool
285 See :meth:`get_range`
285 See :meth:`get_range`
286
286
287 Returns
287 Returns
288 -------
288 -------
289 Tuples as :meth:`get_range`
289 Tuples as :meth:`get_range`
290 """
290 """
291 tosearch = "source_raw" if search_raw else "source"
291 tosearch = "source_raw" if search_raw else "source"
292 if output:
292 if output:
293 tosearch = "history." + tosearch
293 tosearch = "history." + tosearch
294 self.writeout_cache()
294 self.writeout_cache()
295 return self._run_sql("WHERE %s GLOB ?" % tosearch, (pattern,),
295 return self._run_sql("WHERE %s GLOB ?" % tosearch, (pattern,),
296 raw=raw, output=output)
296 raw=raw, output=output)
297
297
298 def _get_range_session(self, start=1, stop=None, raw=True, output=False):
298 def _get_range_session(self, start=1, stop=None, raw=True, output=False):
299 """Get input and output history from the current session. Called by
299 """Get input and output history from the current session. Called by
300 get_range, and takes similar parameters."""
300 get_range, and takes similar parameters."""
301 input_hist = self.input_hist_raw if raw else self.input_hist_parsed
301 input_hist = self.input_hist_raw if raw else self.input_hist_parsed
302
302
303 n = len(input_hist)
303 n = len(input_hist)
304 if start < 0:
304 if start < 0:
305 start += n
305 start += n
306 if not stop or (stop > n):
306 if not stop or (stop > n):
307 stop = n
307 stop = n
308 elif stop < 0:
308 elif stop < 0:
309 stop += n
309 stop += n
310
310
311 for i in range(start, stop):
311 for i in range(start, stop):
312 if output:
312 if output:
313 line = (input_hist[i], self.output_hist_reprs.get(i))
313 line = (input_hist[i], self.output_hist_reprs.get(i))
314 else:
314 else:
315 line = input_hist[i]
315 line = input_hist[i]
316 yield (0, i, line)
316 yield (0, i, line)
317
317
318 def get_range(self, session=0, start=1, stop=None, raw=True,output=False):
318 def get_range(self, session=0, start=1, stop=None, raw=True,output=False):
319 """Retrieve input by session.
319 """Retrieve input by session.
320
320
321 Parameters
321 Parameters
322 ----------
322 ----------
323 session : int
323 session : int
324 Session number to retrieve. The current session is 0, and negative
324 Session number to retrieve. The current session is 0, and negative
325 numbers count back from current session, so -1 is previous session.
325 numbers count back from current session, so -1 is previous session.
326 start : int
326 start : int
327 First line to retrieve.
327 First line to retrieve.
328 stop : int
328 stop : int
329 End of line range (excluded from output itself). If None, retrieve
329 End of line range (excluded from output itself). If None, retrieve
330 to the end of the session.
330 to the end of the session.
331 raw : bool
331 raw : bool
332 If True, return untranslated input
332 If True, return untranslated input
333 output : bool
333 output : bool
334 If True, attempt to include output. This will be 'real' Python
334 If True, attempt to include output. This will be 'real' Python
335 objects for the current session, or text reprs from previous
335 objects for the current session, or text reprs from previous
336 sessions if db_log_output was enabled at the time. Where no output
336 sessions if db_log_output was enabled at the time. Where no output
337 is found, None is used.
337 is found, None is used.
338
338
339 Returns
339 Returns
340 -------
340 -------
341 An iterator over the desired lines. Each line is a 3-tuple, either
341 An iterator over the desired lines. Each line is a 3-tuple, either
342 (session, line, input) if output is False, or
342 (session, line, input) if output is False, or
343 (session, line, (input, output)) if output is True.
343 (session, line, (input, output)) if output is True.
344 """
344 """
345 if session == 0 or session==self.session_number: # Current session
345 if session == 0 or session==self.session_number: # Current session
346 return self._get_range_session(start, stop, raw, output)
346 return self._get_range_session(start, stop, raw, output)
347 if session < 0:
347 if session < 0:
348 session += self.session_number
348 session += self.session_number
349
349
350 if stop:
350 if stop:
351 lineclause = "line >= ? AND line < ?"
351 lineclause = "line >= ? AND line < ?"
352 params = (session, start, stop)
352 params = (session, start, stop)
353 else:
353 else:
354 lineclause = "line>=?"
354 lineclause = "line>=?"
355 params = (session, start)
355 params = (session, start)
356
356
357 return self._run_sql("WHERE session==? AND %s""" % lineclause,
357 return self._run_sql("WHERE session==? AND %s""" % lineclause,
358 params, raw=raw, output=output)
358 params, raw=raw, output=output)
359
359
360 def get_range_by_str(self, rangestr, raw=True, output=False):
360 def get_range_by_str(self, rangestr, raw=True, output=False):
361 """Get lines of history from a string of ranges, as used by magic
361 """Get lines of history from a string of ranges, as used by magic
362 commands %hist, %save, %macro, etc.
362 commands %hist, %save, %macro, etc.
363
363
364 Parameters
364 Parameters
365 ----------
365 ----------
366 rangestr : str
366 rangestr : str
367 A string specifying ranges, e.g. "5 ~2/1-4". See
367 A string specifying ranges, e.g. "5 ~2/1-4". See
368 :func:`magic_history` for full details.
368 :func:`magic_history` for full details.
369 raw, output : bool
369 raw, output : bool
370 As :meth:`get_range`
370 As :meth:`get_range`
371
371
372 Returns
372 Returns
373 -------
373 -------
374 Tuples as :meth:`get_range`
374 Tuples as :meth:`get_range`
375 """
375 """
376 for sess, s, e in extract_hist_ranges(rangestr):
376 for sess, s, e in extract_hist_ranges(rangestr):
377 for line in self.get_range(sess, s, e, raw=raw, output=output):
377 for line in self.get_range(sess, s, e, raw=raw, output=output):
378 yield line
378 yield line
379
379
380 ## ----------------------------
380 ## ----------------------------
381 ## Methods for storing history:
381 ## Methods for storing history:
382 ## ----------------------------
382 ## ----------------------------
383 def store_inputs(self, line_num, source, source_raw=None):
383 def store_inputs(self, line_num, source, source_raw=None):
384 """Store source and raw input in history and create input cache
384 """Store source and raw input in history and create input cache
385 variables _i*.
385 variables _i*.
386
386
387 Parameters
387 Parameters
388 ----------
388 ----------
389 line_num : int
389 line_num : int
390 The prompt number of this input.
390 The prompt number of this input.
391
391
392 source : str
392 source : str
393 Python input.
393 Python input.
394
394
395 source_raw : str, optional
395 source_raw : str, optional
396 If given, this is the raw input without any IPython transformations
396 If given, this is the raw input without any IPython transformations
397 applied to it. If not given, ``source`` is used.
397 applied to it. If not given, ``source`` is used.
398 """
398 """
399 if source_raw is None:
399 if source_raw is None:
400 source_raw = source
400 source_raw = source
401 source = source.rstrip('\n')
401 source = source.rstrip('\n')
402 source_raw = source_raw.rstrip('\n')
402 source_raw = source_raw.rstrip('\n')
403
403
404 # do not store exit/quit commands
404 # do not store exit/quit commands
405 if self._exit_re.match(source_raw.strip()):
405 if self._exit_re.match(source_raw.strip()):
406 return
406 return
407
407
408 self.input_hist_parsed.append(source)
408 self.input_hist_parsed.append(source)
409 self.input_hist_raw.append(source_raw)
409 self.input_hist_raw.append(source_raw)
410
410
411 with self.db_input_cache_lock:
411 with self.db_input_cache_lock:
412 self.db_input_cache.append((line_num, source, source_raw))
412 self.db_input_cache.append((line_num, source, source_raw))
413 # Trigger to flush cache and write to DB.
413 # Trigger to flush cache and write to DB.
414 if len(self.db_input_cache) >= self.db_cache_size:
414 if len(self.db_input_cache) >= self.db_cache_size:
415 self.save_flag.set()
415 self.save_flag.set()
416
416
417 # update the auto _i variables
417 # update the auto _i variables
418 self._iii = self._ii
418 self._iii = self._ii
419 self._ii = self._i
419 self._ii = self._i
420 self._i = self._i00
420 self._i = self._i00
421 self._i00 = source_raw
421 self._i00 = source_raw
422
422
423 # hackish access to user namespace to create _i1,_i2... dynamically
423 # hackish access to user namespace to create _i1,_i2... dynamically
424 new_i = '_i%s' % line_num
424 new_i = '_i%s' % line_num
425 to_main = {'_i': self._i,
425 to_main = {'_i': self._i,
426 '_ii': self._ii,
426 '_ii': self._ii,
427 '_iii': self._iii,
427 '_iii': self._iii,
428 new_i : self._i00 }
428 new_i : self._i00 }
429 self.shell.user_ns.update(to_main)
429 self.shell.user_ns.update(to_main)
430
430
431 def store_output(self, line_num):
431 def store_output(self, line_num):
432 """If database output logging is enabled, this saves all the
432 """If database output logging is enabled, this saves all the
433 outputs from the indicated prompt number to the database. It's
433 outputs from the indicated prompt number to the database. It's
434 called by run_cell after code has been executed.
434 called by run_cell after code has been executed.
435
435
436 Parameters
436 Parameters
437 ----------
437 ----------
438 line_num : int
438 line_num : int
439 The line number from which to save outputs
439 The line number from which to save outputs
440 """
440 """
441 if (not self.db_log_output) or (line_num not in self.output_hist_reprs):
441 if (not self.db_log_output) or (line_num not in self.output_hist_reprs):
442 return
442 return
443 output = self.output_hist_reprs[line_num]
443 output = self.output_hist_reprs[line_num]
444
444
445 with self.db_output_cache_lock:
445 with self.db_output_cache_lock:
446 self.db_output_cache.append((line_num, output))
446 self.db_output_cache.append((line_num, output))
447 if self.db_cache_size <= 1:
447 if self.db_cache_size <= 1:
448 self.save_flag.set()
448 self.save_flag.set()
449
449
450 def _writeout_input_cache(self, conn):
450 def _writeout_input_cache(self, conn):
451 with conn:
451 with conn:
452 for line in self.db_input_cache:
452 for line in self.db_input_cache:
453 conn.execute("INSERT INTO history VALUES (?, ?, ?, ?)",
453 conn.execute("INSERT INTO history VALUES (?, ?, ?, ?)",
454 (self.session_number,)+line)
454 (self.session_number,)+line)
455
455
456 def _writeout_output_cache(self, conn):
456 def _writeout_output_cache(self, conn):
457 with conn:
457 with conn:
458 for line in self.db_output_cache:
458 for line in self.db_output_cache:
459 conn.execute("INSERT INTO output_history VALUES (?, ?, ?)",
459 conn.execute("INSERT INTO output_history VALUES (?, ?, ?)",
460 (self.session_number,)+line)
460 (self.session_number,)+line)
461
461
462 def writeout_cache(self, conn=None):
462 def writeout_cache(self, conn=None):
463 """Write any entries in the cache to the database."""
463 """Write any entries in the cache to the database."""
464 if conn is None:
464 if conn is None:
465 conn = self.db
465 conn = self.db
466
466
467 with self.db_input_cache_lock:
467 with self.db_input_cache_lock:
468 try:
468 try:
469 self._writeout_input_cache(conn)
469 self._writeout_input_cache(conn)
470 except sqlite3.IntegrityError:
470 except sqlite3.IntegrityError:
471 self.new_session(conn)
471 self.new_session(conn)
472 print("ERROR! Session/line number was not unique in",
472 print("ERROR! Session/line number was not unique in",
473 "database. History logging moved to new session",
473 "database. History logging moved to new session",
474 self.session_number)
474 self.session_number)
475 try: # Try writing to the new session. If this fails, don't recurse
475 try: # Try writing to the new session. If this fails, don't recurse
476 self._writeout_input_cache(conn)
476 self._writeout_input_cache(conn)
477 except sqlite3.IntegrityError:
477 except sqlite3.IntegrityError:
478 pass
478 pass
479 finally:
479 finally:
480 self.db_input_cache = []
480 self.db_input_cache = []
481
481
482 with self.db_output_cache_lock:
482 with self.db_output_cache_lock:
483 try:
483 try:
484 self._writeout_output_cache(conn)
484 self._writeout_output_cache(conn)
485 except sqlite3.IntegrityError:
485 except sqlite3.IntegrityError:
486 print("!! Session/line number for output was not unique",
486 print("!! Session/line number for output was not unique",
487 "in database. Output will not be stored.")
487 "in database. Output will not be stored.")
488 finally:
488 finally:
489 self.db_output_cache = []
489 self.db_output_cache = []
490
490
491
491
492 class HistorySavingThread(threading.Thread):
492 class HistorySavingThread(threading.Thread):
493 """This thread takes care of writing history to the database, so that
493 """This thread takes care of writing history to the database, so that
494 the UI isn't held up while that happens.
494 the UI isn't held up while that happens.
495
495
496 It waits for the HistoryManager's save_flag to be set, then writes out
496 It waits for the HistoryManager's save_flag to be set, then writes out
497 the history cache. The main thread is responsible for setting the flag when
497 the history cache. The main thread is responsible for setting the flag when
498 the cache size reaches a defined threshold."""
498 the cache size reaches a defined threshold."""
499 daemon = True
499 daemon = True
500 stop_now = False
500 stop_now = False
501 def __init__(self, history_manager):
501 def __init__(self, history_manager):
502 super(HistorySavingThread, self).__init__()
502 super(HistorySavingThread, self).__init__()
503 self.history_manager = history_manager
503 self.history_manager = history_manager
504 atexit.register(self.stop)
504 atexit.register(self.stop)
505
505
506 def run(self):
506 def run(self):
507 # We need a separate db connection per thread:
507 # We need a separate db connection per thread:
508 try:
508 try:
509 self.db = sqlite3.connect(self.history_manager.hist_file)
509 self.db = sqlite3.connect(self.history_manager.hist_file)
510 while True:
510 while True:
511 self.history_manager.save_flag.wait()
511 self.history_manager.save_flag.wait()
512 if self.stop_now:
512 if self.stop_now:
513 return
513 return
514 self.history_manager.save_flag.clear()
514 self.history_manager.save_flag.clear()
515 self.history_manager.writeout_cache(self.db)
515 self.history_manager.writeout_cache(self.db)
516 except Exception as e:
516 except Exception as e:
517 print(("The history saving thread hit an unexpected error (%s)."
517 print(("The history saving thread hit an unexpected error (%s)."
518 "History will not be written to the database.") % repr(e))
518 "History will not be written to the database.") % repr(e))
519
519
520 def stop(self):
520 def stop(self):
521 """This can be called from the main thread to safely stop this thread.
521 """This can be called from the main thread to safely stop this thread.
522
522
523 Note that it does not attempt to write out remaining history before
523 Note that it does not attempt to write out remaining history before
524 exiting. That should be done by calling the HistoryManager's
524 exiting. That should be done by calling the HistoryManager's
525 end_session method."""
525 end_session method."""
526 self.stop_now = True
526 self.stop_now = True
527 self.history_manager.save_flag.set()
527 self.history_manager.save_flag.set()
528 self.join()
528 self.join()
529
529
530
530
531 # To match, e.g. ~5/8-~2/3
531 # To match, e.g. ~5/8-~2/3
532 range_re = re.compile(r"""
532 range_re = re.compile(r"""
533 ((?P<startsess>~?\d+)/)?
533 ((?P<startsess>~?\d+)/)?
534 (?P<start>\d+) # Only the start line num is compulsory
534 (?P<start>\d+) # Only the start line num is compulsory
535 ((?P<sep>[\-:])
535 ((?P<sep>[\-:])
536 ((?P<endsess>~?\d+)/)?
536 ((?P<endsess>~?\d+)/)?
537 (?P<end>\d+))?
537 (?P<end>\d+))?
538 $""", re.VERBOSE)
538 $""", re.VERBOSE)
539
539
540 def extract_hist_ranges(ranges_str):
540 def extract_hist_ranges(ranges_str):
541 """Turn a string of history ranges into 3-tuples of (session, start, stop).
541 """Turn a string of history ranges into 3-tuples of (session, start, stop).
542
542
543 Examples
543 Examples
544 --------
544 --------
545 list(extract_input_ranges("~8/5-~7/4 2"))
545 list(extract_input_ranges("~8/5-~7/4 2"))
546 [(-8, 5, None), (-7, 1, 4), (0, 2, 3)]
546 [(-8, 5, None), (-7, 1, 4), (0, 2, 3)]
547 """
547 """
548 for range_str in ranges_str.split():
548 for range_str in ranges_str.split():
549 rmatch = range_re.match(range_str)
549 rmatch = range_re.match(range_str)
550 if not rmatch:
550 if not rmatch:
551 continue
551 continue
552 start = int(rmatch.group("start"))
552 start = int(rmatch.group("start"))
553 end = rmatch.group("end")
553 end = rmatch.group("end")
554 end = int(end) if end else start+1 # If no end specified, get (a, a+1)
554 end = int(end) if end else start+1 # If no end specified, get (a, a+1)
555 if rmatch.group("sep") == "-": # 1-3 == 1:4 --> [1, 2, 3]
555 if rmatch.group("sep") == "-": # 1-3 == 1:4 --> [1, 2, 3]
556 end += 1
556 end += 1
557 startsess = rmatch.group("startsess") or "0"
557 startsess = rmatch.group("startsess") or "0"
558 endsess = rmatch.group("endsess") or startsess
558 endsess = rmatch.group("endsess") or startsess
559 startsess = int(startsess.replace("~","-"))
559 startsess = int(startsess.replace("~","-"))
560 endsess = int(endsess.replace("~","-"))
560 endsess = int(endsess.replace("~","-"))
561 assert endsess >= startsess
561 assert endsess >= startsess
562
562
563 if endsess == startsess:
563 if endsess == startsess:
564 yield (startsess, start, end)
564 yield (startsess, start, end)
565 continue
565 continue
566 # Multiple sessions in one range:
566 # Multiple sessions in one range:
567 yield (startsess, start, None)
567 yield (startsess, start, None)
568 for sess in range(startsess+1, endsess):
568 for sess in range(startsess+1, endsess):
569 yield (sess, 1, None)
569 yield (sess, 1, None)
570 yield (endsess, 1, end)
570 yield (endsess, 1, end)
571
571
572 def _format_lineno(session, line):
572 def _format_lineno(session, line):
573 """Helper function to format line numbers properly."""
573 """Helper function to format line numbers properly."""
574 if session == 0:
574 if session == 0:
575 return str(line)
575 return str(line)
576 return "%s#%s" % (session, line)
576 return "%s#%s" % (session, line)
577
577
578 @skip_doctest
578 @skip_doctest
579 def magic_history(self, parameter_s = ''):
579 def magic_history(self, parameter_s = ''):
580 """Print input history (_i<n> variables), with most recent last.
580 """Print input history (_i<n> variables), with most recent last.
581
581
582 %history -> print at most 40 inputs (some may be multi-line)\\
582 %history -> print at most 40 inputs (some may be multi-line)\\
583 %history n -> print at most n inputs\\
583 %history n -> print at most n inputs\\
584 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
584 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
585
585
586 By default, input history is printed without line numbers so it can be
586 By default, input history is printed without line numbers so it can be
587 directly pasted into an editor. Use -n to show them.
587 directly pasted into an editor. Use -n to show them.
588
588
589 Ranges of history can be indicated using the syntax:
589 Ranges of history can be indicated using the syntax:
590 4 : Line 4, current session
590 4 : Line 4, current session
591 4-6 : Lines 4-6, current session
591 4-6 : Lines 4-6, current session
592 243/1-5: Lines 1-5, session 243
592 243/1-5: Lines 1-5, session 243
593 ~2/7 : Line 7, session 2 before current
593 ~2/7 : Line 7, session 2 before current
594 ~8/1-~6/5 : From the first line of 8 sessions ago, to the fifth line
594 ~8/1-~6/5 : From the first line of 8 sessions ago, to the fifth line
595 of 6 sessions ago.
595 of 6 sessions ago.
596 Multiple ranges can be entered, separated by spaces
596 Multiple ranges can be entered, separated by spaces
597
597
598 The same syntax is used by %macro, %save, %edit, %rerun
598 The same syntax is used by %macro, %save, %edit, %rerun
599
599
600 Options:
600 Options:
601
601
602 -n: print line numbers for each input.
602 -n: print line numbers for each input.
603 This feature is only available if numbered prompts are in use.
603 This feature is only available if numbered prompts are in use.
604
604
605 -o: also print outputs for each input.
605 -o: also print outputs for each input.
606
606
607 -p: print classic '>>>' python prompts before each input. This is useful
607 -p: print classic '>>>' python prompts before each input. This is useful
608 for making documentation, and in conjunction with -o, for producing
608 for making documentation, and in conjunction with -o, for producing
609 doctest-ready output.
609 doctest-ready output.
610
610
611 -r: (default) print the 'raw' history, i.e. the actual commands you typed.
611 -r: (default) print the 'raw' history, i.e. the actual commands you typed.
612
612
613 -t: print the 'translated' history, as IPython understands it. IPython
613 -t: print the 'translated' history, as IPython understands it. IPython
614 filters your input and converts it all into valid Python source before
614 filters your input and converts it all into valid Python source before
615 executing it (things like magics or aliases are turned into function
615 executing it (things like magics or aliases are turned into function
616 calls, for example). With this option, you'll see the native history
616 calls, for example). With this option, you'll see the native history
617 instead of the user-entered version: '%cd /' will be seen as
617 instead of the user-entered version: '%cd /' will be seen as
618 'get_ipython().magic("%cd /")' instead of '%cd /'.
618 'get_ipython().magic("%cd /")' instead of '%cd /'.
619
619
620 -g: treat the arg as a pattern to grep for in (full) history.
620 -g: treat the arg as a pattern to grep for in (full) history.
621 This includes the saved history (almost all commands ever written).
621 This includes the saved history (almost all commands ever written).
622 Use '%hist -g' to show full saved history (may be very long).
622 Use '%hist -g' to show full saved history (may be very long).
623
623
624 -l: get the last n lines from all sessions. Specify n as a single arg, or
624 -l: get the last n lines from all sessions. Specify n as a single arg, or
625 the default is the last 10 lines.
625 the default is the last 10 lines.
626
626
627 -f FILENAME: instead of printing the output to the screen, redirect it to
627 -f FILENAME: instead of printing the output to the screen, redirect it to
628 the given file. The file is always overwritten, though IPython asks for
628 the given file. The file is always overwritten, though IPython asks for
629 confirmation first if it already exists.
629 confirmation first if it already exists.
630
630
631 Examples
631 Examples
632 --------
632 --------
633 ::
633 ::
634
634
635 In [6]: %hist -n 4 6
635 In [6]: %hist -n 4 6
636 4:a = 12
636 4:a = 12
637 5:print a**2
637 5:print a**2
638
638
639 """
639 """
640
640
641 if not self.shell.displayhook.do_full_cache:
641 if not self.shell.displayhook.do_full_cache:
642 print('This feature is only available if numbered prompts are in use.')
642 print('This feature is only available if numbered prompts are in use.')
643 return
643 return
644 opts,args = self.parse_options(parameter_s,'noprtglf:',mode='string')
644 opts,args = self.parse_options(parameter_s,'noprtglf:',mode='string')
645
645
646 # For brevity
646 # For brevity
647 history_manager = self.shell.history_manager
647 history_manager = self.shell.history_manager
648
648
649 def _format_lineno(session, line):
649 def _format_lineno(session, line):
650 """Helper function to format line numbers properly."""
650 """Helper function to format line numbers properly."""
651 if session in (0, history_manager.session_number):
651 if session in (0, history_manager.session_number):
652 return str(line)
652 return str(line)
653 return "%s/%s" % (session, line)
653 return "%s/%s" % (session, line)
654
654
655 # Check if output to specific file was requested.
655 # Check if output to specific file was requested.
656 try:
656 try:
657 outfname = opts['f']
657 outfname = opts['f']
658 except KeyError:
658 except KeyError:
659 outfile = io.stdout # default
659 outfile = io.stdout # default
660 # We don't want to close stdout at the end!
660 # We don't want to close stdout at the end!
661 close_at_end = False
661 close_at_end = False
662 else:
662 else:
663 if os.path.exists(outfname):
663 if os.path.exists(outfname):
664 if not io.ask_yes_no("File %r exists. Overwrite?" % outfname):
664 if not io.ask_yes_no("File %r exists. Overwrite?" % outfname):
665 print('Aborting.')
665 print('Aborting.')
666 return
666 return
667
667
668 outfile = open(outfname,'w')
668 outfile = open(outfname,'w')
669 close_at_end = True
669 close_at_end = True
670
670
671 print_nums = 'n' in opts
671 print_nums = 'n' in opts
672 get_output = 'o' in opts
672 get_output = 'o' in opts
673 pyprompts = 'p' in opts
673 pyprompts = 'p' in opts
674 # Raw history is the default
674 # Raw history is the default
675 raw = not('t' in opts)
675 raw = not('t' in opts)
676
676
677 default_length = 40
677 default_length = 40
678 pattern = None
678 pattern = None
679
679
680 if 'g' in opts: # Glob search
680 if 'g' in opts: # Glob search
681 pattern = "*" + args + "*" if args else "*"
681 pattern = "*" + args + "*" if args else "*"
682 hist = history_manager.search(pattern, raw=raw, output=get_output)
682 hist = history_manager.search(pattern, raw=raw, output=get_output)
683 print_nums = True
683 print_nums = True
684 elif 'l' in opts: # Get 'tail'
684 elif 'l' in opts: # Get 'tail'
685 try:
685 try:
686 n = int(args)
686 n = int(args)
687 except ValueError, IndexError:
687 except ValueError, IndexError:
688 n = 10
688 n = 10
689 hist = history_manager.get_tail(n, raw=raw, output=get_output)
689 hist = history_manager.get_tail(n, raw=raw, output=get_output)
690 else:
690 else:
691 if args: # Get history by ranges
691 if args: # Get history by ranges
692 hist = history_manager.get_range_by_str(args, raw, get_output)
692 hist = history_manager.get_range_by_str(args, raw, get_output)
693 else: # Just get history for the current session
693 else: # Just get history for the current session
694 hist = history_manager.get_range(raw=raw, output=get_output)
694 hist = history_manager.get_range(raw=raw, output=get_output)
695
695
696 # We could be displaying the entire history, so let's not try to pull it
696 # We could be displaying the entire history, so let's not try to pull it
697 # into a list in memory. Anything that needs more space will just misalign.
697 # into a list in memory. Anything that needs more space will just misalign.
698 width = 4
698 width = 4
699
699
700 for session, lineno, inline in hist:
700 for session, lineno, inline in hist:
701 # Print user history with tabs expanded to 4 spaces. The GUI clients
701 # Print user history with tabs expanded to 4 spaces. The GUI clients
702 # use hard tabs for easier usability in auto-indented code, but we want
702 # use hard tabs for easier usability in auto-indented code, but we want
703 # to produce PEP-8 compliant history for safe pasting into an editor.
703 # to produce PEP-8 compliant history for safe pasting into an editor.
704 if get_output:
704 if get_output:
705 inline, output = inline
705 inline, output = inline
706 inline = inline.expandtabs(4).rstrip()
706 inline = inline.expandtabs(4).rstrip()
707
707
708 multiline = "\n" in inline
708 multiline = "\n" in inline
709 line_sep = '\n' if multiline else ' '
709 line_sep = '\n' if multiline else ' '
710 if print_nums:
710 if print_nums:
711 print('%s:%s' % (_format_lineno(session, lineno).rjust(width),
711 print('%s:%s' % (_format_lineno(session, lineno).rjust(width),
712 line_sep), file=outfile, end='')
712 line_sep), file=outfile, end='')
713 if pyprompts:
713 if pyprompts:
714 print(">>> ", end="", file=outfile)
714 print(">>> ", end="", file=outfile)
715 if multiline:
715 if multiline:
716 inline = "\n... ".join(inline.splitlines()) + "\n..."
716 inline = "\n... ".join(inline.splitlines()) + "\n..."
717 print(inline, file=outfile)
717 print(inline, file=outfile)
718 if get_output and output:
718 if get_output and output:
719 print(output, file=outfile)
719 print(output, file=outfile)
720
720
721 if close_at_end:
721 if close_at_end:
722 outfile.close()
722 outfile.close()
723
723
724
724
725 def magic_rep(self, arg):
725 def magic_rep(self, arg):
726 r"""Repeat a command, or get command to input line for editing. %recall and
726 r"""Repeat a command, or get command to input line for editing. %recall and
727 %rep are equivalent.
727 %rep are equivalent.
728
728
729 - %recall (no arguments):
729 - %recall (no arguments):
730
730
731 Place a string version of last computation result (stored in the special '_'
731 Place a string version of last computation result (stored in the special '_'
732 variable) to the next input prompt. Allows you to create elaborate command
732 variable) to the next input prompt. Allows you to create elaborate command
733 lines without using copy-paste::
733 lines without using copy-paste::
734
734
735 In[1]: l = ["hei", "vaan"]
735 In[1]: l = ["hei", "vaan"]
736 In[2]: "".join(l)
736 In[2]: "".join(l)
737 Out[2]: heivaan
737 Out[2]: heivaan
738 In[3]: %rep
738 In[3]: %rep
739 In[4]: heivaan_ <== cursor blinking
739 In[4]: heivaan_ <== cursor blinking
740
740
741 %recall 45
741 %recall 45
742
742
743 Place history line 45 on the next input prompt. Use %hist to find
743 Place history line 45 on the next input prompt. Use %hist to find
744 out the number.
744 out the number.
745
745
746 %recall 1-4
746 %recall 1-4
747
747
748 Combine the specified lines into one cell, and place it on the next
748 Combine the specified lines into one cell, and place it on the next
749 input prompt. See %history for the slice syntax.
749 input prompt. See %history for the slice syntax.
750
750
751 %recall foo+bar
751 %recall foo+bar
752
752
753 If foo+bar can be evaluated in the user namespace, the result is
753 If foo+bar can be evaluated in the user namespace, the result is
754 placed at the next input prompt. Otherwise, the history is searched
754 placed at the next input prompt. Otherwise, the history is searched
755 for lines which contain that substring, and the most recent one is
755 for lines which contain that substring, and the most recent one is
756 placed at the next input prompt.
756 placed at the next input prompt.
757 """
757 """
758 if not arg: # Last output
758 if not arg: # Last output
759 self.set_next_input(str(self.shell.user_ns["_"]))
759 self.set_next_input(str(self.shell.user_ns["_"]))
760 return
760 return
761 # Get history range
761 # Get history range
762 histlines = self.history_manager.get_range_by_str(arg)
762 histlines = self.history_manager.get_range_by_str(arg)
763 cmd = "\n".join(x[2] for x in histlines)
763 cmd = "\n".join(x[2] for x in histlines)
764 if cmd:
764 if cmd:
765 self.set_next_input(cmd.rstrip())
765 self.set_next_input(cmd.rstrip())
766 return
766 return
767
767
768 try: # Variable in user namespace
768 try: # Variable in user namespace
769 cmd = str(eval(arg, self.shell.user_ns))
769 cmd = str(eval(arg, self.shell.user_ns))
770 except Exception: # Search for term in history
770 except Exception: # Search for term in history
771 histlines = self.history_manager.search("*"+arg+"*")
771 histlines = self.history_manager.search("*"+arg+"*")
772 for h in reversed([x[2] for x in histlines]):
772 for h in reversed([x[2] for x in histlines]):
773 if 'rep' in h:
773 if 'rep' in h:
774 continue
774 continue
775 self.set_next_input(h.rstrip())
775 self.set_next_input(h.rstrip())
776 return
776 return
777 else:
777 else:
778 self.set_next_input(cmd.rstrip())
778 self.set_next_input(cmd.rstrip())
779 print("Couldn't evaluate or find in history:", arg)
779 print("Couldn't evaluate or find in history:", arg)
780
780
781 def magic_rerun(self, parameter_s=''):
781 def magic_rerun(self, parameter_s=''):
782 """Re-run previous input
782 """Re-run previous input
783
783
784 By default, you can specify ranges of input history to be repeated
784 By default, you can specify ranges of input history to be repeated
785 (as with %history). With no arguments, it will repeat the last line.
785 (as with %history). With no arguments, it will repeat the last line.
786
786
787 Options:
787 Options:
788
788
789 -l <n> : Repeat the last n lines of input, not including the
789 -l <n> : Repeat the last n lines of input, not including the
790 current command.
790 current command.
791
791
792 -g foo : Repeat the most recent line which contains foo
792 -g foo : Repeat the most recent line which contains foo
793 """
793 """
794 opts, args = self.parse_options(parameter_s, 'l:g:', mode='string')
794 opts, args = self.parse_options(parameter_s, 'l:g:', mode='string')
795 if "l" in opts: # Last n lines
795 if "l" in opts: # Last n lines
796 n = int(opts['l'])
796 n = int(opts['l'])
797 hist = self.history_manager.get_tail(n)
797 hist = self.history_manager.get_tail(n)
798 elif "g" in opts: # Search
798 elif "g" in opts: # Search
799 p = "*"+opts['g']+"*"
799 p = "*"+opts['g']+"*"
800 hist = list(self.history_manager.search(p))
800 hist = list(self.history_manager.search(p))
801 for l in reversed(hist):
801 for l in reversed(hist):
802 if "rerun" not in l[2]:
802 if "rerun" not in l[2]:
803 hist = [l] # The last match which isn't a %rerun
803 hist = [l] # The last match which isn't a %rerun
804 break
804 break
805 else:
805 else:
806 hist = [] # No matches except %rerun
806 hist = [] # No matches except %rerun
807 elif args: # Specify history ranges
807 elif args: # Specify history ranges
808 hist = self.history_manager.get_range_by_str(args)
808 hist = self.history_manager.get_range_by_str(args)
809 else: # Last line
809 else: # Last line
810 hist = self.history_manager.get_tail(1)
810 hist = self.history_manager.get_tail(1)
811 hist = [x[2] for x in hist]
811 hist = [x[2] for x in hist]
812 if not hist:
812 if not hist:
813 print("No lines in history match specification")
813 print("No lines in history match specification")
814 return
814 return
815 histlines = "\n".join(hist)
815 histlines = "\n".join(hist)
816 print("=== Executing: ===")
816 print("=== Executing: ===")
817 print(histlines)
817 print(histlines)
818 print("=== Output: ===")
818 print("=== Output: ===")
819 self.run_cell("\n".join(hist), store_history=False)
819 self.run_cell("\n".join(hist), store_history=False)
820
820
821
821
822 def init_ipython(ip):
822 def init_ipython(ip):
823 ip.define_magic("rep", magic_rep)
823 ip.define_magic("rep", magic_rep)
824 ip.define_magic("recall", magic_rep)
824 ip.define_magic("recall", magic_rep)
825 ip.define_magic("rerun", magic_rerun)
825 ip.define_magic("rerun", magic_rerun)
826 ip.define_magic("hist",magic_history) # Alternative name
826 ip.define_magic("hist",magic_history) # Alternative name
827 ip.define_magic("history",magic_history)
827 ip.define_magic("history",magic_history)
828
828
829 # XXX - ipy_completers are in quarantine, need to be updated to new apis
829 # XXX - ipy_completers are in quarantine, need to be updated to new apis
830 #import ipy_completers
830 #import ipy_completers
831 #ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
831 #ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
@@ -1,235 +1,235 b''
1 """hooks for IPython.
1 """hooks for IPython.
2
2
3 In Python, it is possible to overwrite any method of any object if you really
3 In Python, it is possible to overwrite any method of any object if you really
4 want to. But IPython exposes a few 'hooks', methods which are _designed_ to
4 want to. But IPython exposes a few 'hooks', methods which are _designed_ to
5 be overwritten by users for customization purposes. This module defines the
5 be overwritten by users for customization purposes. This module defines the
6 default versions of all such hooks, which get used by IPython if not
6 default versions of all such hooks, which get used by IPython if not
7 overridden by the user.
7 overridden by the user.
8
8
9 hooks are simple functions, but they should be declared with 'self' as their
9 hooks are simple functions, but they should be declared with 'self' as their
10 first argument, because when activated they are registered into IPython as
10 first argument, because when activated they are registered into IPython as
11 instance methods. The self argument will be the IPython running instance
11 instance methods. The self argument will be the IPython running instance
12 itself, so hooks have full access to the entire IPython object.
12 itself, so hooks have full access to the entire IPython object.
13
13
14 If you wish to define a new hook and activate it, you need to put the
14 If you wish to define a new hook and activate it, you need to put the
15 necessary code into a python file which can be either imported or execfile()'d
15 necessary code into a python file which can be either imported or execfile()'d
16 from within your profile's ipython_config.py configuration.
16 from within your profile's ipython_config.py configuration.
17
17
18 For example, suppose that you have a module called 'myiphooks' in your
18 For example, suppose that you have a module called 'myiphooks' in your
19 PYTHONPATH, which contains the following definition:
19 PYTHONPATH, which contains the following definition:
20
20
21 import os
21 import os
22 from IPython.core import ipapi
22 from IPython.core import ipapi
23 ip = ipapi.get()
23 ip = ipapi.get()
24
24
25 def calljed(self,filename, linenum):
25 def calljed(self,filename, linenum):
26 "My editor hook calls the jed editor directly."
26 "My editor hook calls the jed editor directly."
27 print "Calling my own editor, jed ..."
27 print "Calling my own editor, jed ..."
28 if os.system('jed +%d %s' % (linenum,filename)) != 0:
28 if os.system('jed +%d %s' % (linenum,filename)) != 0:
29 raise TryNext()
29 raise TryNext()
30
30
31 ip.set_hook('editor', calljed)
31 ip.set_hook('editor', calljed)
32
32
33 You can then enable the functionality by doing 'import myiphooks'
33 You can then enable the functionality by doing 'import myiphooks'
34 somewhere in your configuration files or ipython command line.
34 somewhere in your configuration files or ipython command line.
35 """
35 """
36
36
37 #*****************************************************************************
37 #*****************************************************************************
38 # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
38 # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
39 #
39 #
40 # Distributed under the terms of the BSD License. The full license is in
40 # Distributed under the terms of the BSD License. The full license is in
41 # the file COPYING, distributed as part of this software.
41 # the file COPYING, distributed as part of this software.
42 #*****************************************************************************
42 #*****************************************************************************
43
43
44 import os, bisect
44 import os, bisect
45 import sys
45 import sys
46
46
47 from IPython.core.error import TryNext
47 from IPython.core.error import TryNext
48
48
49 # List here all the default hooks. For now it's just the editor functions
49 # List here all the default hooks. For now it's just the editor functions
50 # but over time we'll move here all the public API for user-accessible things.
50 # but over time we'll move here all the public API for user-accessible things.
51
51
52 __all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor',
52 __all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor',
53 'input_prefilter', 'shutdown_hook', 'late_startup_hook',
53 'input_prefilter', 'shutdown_hook', 'late_startup_hook',
54 'generate_prompt', 'show_in_pager','pre_prompt_hook',
54 'generate_prompt', 'show_in_pager','pre_prompt_hook',
55 'pre_run_code_hook', 'clipboard_get']
55 'pre_run_code_hook', 'clipboard_get']
56
56
57 def editor(self,filename, linenum=None):
57 def editor(self,filename, linenum=None):
58 """Open the default editor at the given filename and linenumber.
58 """Open the default editor at the given filename and linenumber.
59
59
60 This is IPython's default editor hook, you can use it as an example to
60 This is IPython's default editor hook, you can use it as an example to
61 write your own modified one. To set your own editor function as the
61 write your own modified one. To set your own editor function as the
62 new editor hook, call ip.set_hook('editor',yourfunc)."""
62 new editor hook, call ip.set_hook('editor',yourfunc)."""
63
63
64 # IPython configures a default editor at startup by reading $EDITOR from
64 # IPython configures a default editor at startup by reading $EDITOR from
65 # the environment, and falling back on vi (unix) or notepad (win32).
65 # the environment, and falling back on vi (unix) or notepad (win32).
66 editor = self.editor
66 editor = self.editor
67
67
68 # marker for at which line to open the file (for existing objects)
68 # marker for at which line to open the file (for existing objects)
69 if linenum is None or editor=='notepad':
69 if linenum is None or editor=='notepad':
70 linemark = ''
70 linemark = ''
71 else:
71 else:
72 linemark = '+%d' % int(linenum)
72 linemark = '+%d' % int(linenum)
73
73
74 # Enclose in quotes if necessary and legal
74 # Enclose in quotes if necessary and legal
75 if ' ' in editor and os.path.isfile(editor) and editor[0] != '"':
75 if ' ' in editor and os.path.isfile(editor) and editor[0] != '"':
76 editor = '"%s"' % editor
76 editor = '"%s"' % editor
77
77
78 # Call the actual editor
78 # Call the actual editor
79 if os.system('%s %s %s' % (editor,linemark,filename)) != 0:
79 if os.system('%s %s %s' % (editor,linemark,filename)) != 0:
80 raise TryNext()
80 raise TryNext()
81
81
82 import tempfile
82 import tempfile
83 def fix_error_editor(self,filename,linenum,column,msg):
83 def fix_error_editor(self,filename,linenum,column,msg):
84 """Open the editor at the given filename, linenumber, column and
84 """Open the editor at the given filename, linenumber, column and
85 show an error message. This is used for correcting syntax errors.
85 show an error message. This is used for correcting syntax errors.
86 The current implementation only has special support for the VIM editor,
86 The current implementation only has special support for the VIM editor,
87 and falls back on the 'editor' hook if VIM is not used.
87 and falls back on the 'editor' hook if VIM is not used.
88
88
89 Call ip.set_hook('fix_error_editor',youfunc) to use your own function,
89 Call ip.set_hook('fix_error_editor',youfunc) to use your own function,
90 """
90 """
91 def vim_quickfix_file():
91 def vim_quickfix_file():
92 t = tempfile.NamedTemporaryFile()
92 t = tempfile.NamedTemporaryFile()
93 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
93 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
94 t.flush()
94 t.flush()
95 return t
95 return t
96 if os.path.basename(self.editor) != 'vim':
96 if os.path.basename(self.editor) != 'vim':
97 self.hooks.editor(filename,linenum)
97 self.hooks.editor(filename,linenum)
98 return
98 return
99 t = vim_quickfix_file()
99 t = vim_quickfix_file()
100 try:
100 try:
101 if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
101 if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
102 raise TryNext()
102 raise TryNext()
103 finally:
103 finally:
104 t.close()
104 t.close()
105
105
106
106
107 def synchronize_with_editor(self, filename, linenum, column):
107 def synchronize_with_editor(self, filename, linenum, column):
108 pass
108 pass
109
109
110
110
111 class CommandChainDispatcher:
111 class CommandChainDispatcher:
112 """ Dispatch calls to a chain of commands until some func can handle it
112 """ Dispatch calls to a chain of commands until some func can handle it
113
113
114 Usage: instantiate, execute "add" to add commands (with optional
114 Usage: instantiate, execute "add" to add commands (with optional
115 priority), execute normally via f() calling mechanism.
115 priority), execute normally via f() calling mechanism.
116
116
117 """
117 """
118 def __init__(self,commands=None):
118 def __init__(self,commands=None):
119 if commands is None:
119 if commands is None:
120 self.chain = []
120 self.chain = []
121 else:
121 else:
122 self.chain = commands
122 self.chain = commands
123
123
124
124
125 def __call__(self,*args, **kw):
125 def __call__(self,*args, **kw):
126 """ Command chain is called just like normal func.
126 """ Command chain is called just like normal func.
127
127
128 This will call all funcs in chain with the same args as were given to this
128 This will call all funcs in chain with the same args as were given to this
129 function, and return the result of first func that didn't raise
129 function, and return the result of first func that didn't raise
130 TryNext """
130 TryNext """
131
131
132 for prio,cmd in self.chain:
132 for prio,cmd in self.chain:
133 #print "prio",prio,"cmd",cmd #dbg
133 #print "prio",prio,"cmd",cmd #dbg
134 try:
134 try:
135 return cmd(*args, **kw)
135 return cmd(*args, **kw)
136 except TryNext, exc:
136 except TryNext, exc:
137 if exc.args or exc.kwargs:
137 if exc.args or exc.kwargs:
138 args = exc.args
138 args = exc.args
139 kw = exc.kwargs
139 kw = exc.kwargs
140 # if no function will accept it, raise TryNext up to the caller
140 # if no function will accept it, raise TryNext up to the caller
141 raise TryNext
141 raise TryNext
142
142
143 def __str__(self):
143 def __str__(self):
144 return str(self.chain)
144 return str(self.chain)
145
145
146 def add(self, func, priority=0):
146 def add(self, func, priority=0):
147 """ Add a func to the cmd chain with given priority """
147 """ Add a func to the cmd chain with given priority """
148 bisect.insort(self.chain,(priority,func))
148 bisect.insort(self.chain,(priority,func))
149
149
150 def __iter__(self):
150 def __iter__(self):
151 """ Return all objects in chain.
151 """ Return all objects in chain.
152
152
153 Handy if the objects are not callable.
153 Handy if the objects are not callable.
154 """
154 """
155 return iter(self.chain)
155 return iter(self.chain)
156
156
157
157
158 def input_prefilter(self,line):
158 def input_prefilter(self,line):
159 """ Default input prefilter
159 """ Default input prefilter
160
160
161 This returns the line as unchanged, so that the interpreter
161 This returns the line as unchanged, so that the interpreter
162 knows that nothing was done and proceeds with "classic" prefiltering
162 knows that nothing was done and proceeds with "classic" prefiltering
163 (%magics, !shell commands etc.).
163 (%magics, !shell commands etc.).
164
164
165 Note that leading whitespace is not passed to this hook. Prefilter
165 Note that leading whitespace is not passed to this hook. Prefilter
166 can't alter indentation.
166 can't alter indentation.
167
167
168 """
168 """
169 #print "attempt to rewrite",line #dbg
169 #print "attempt to rewrite",line #dbg
170 return line
170 return line
171
171
172
172
173 def shutdown_hook(self):
173 def shutdown_hook(self):
174 """ default shutdown hook
174 """ default shutdown hook
175
175
176 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
176 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
177 """
177 """
178
178
179 #print "default shutdown hook ok" # dbg
179 #print "default shutdown hook ok" # dbg
180 return
180 return
181
181
182
182
183 def late_startup_hook(self):
183 def late_startup_hook(self):
184 """ Executed after ipython has been constructed and configured
184 """ Executed after ipython has been constructed and configured
185
185
186 """
186 """
187 #print "default startup hook ok" # dbg
187 #print "default startup hook ok" # dbg
188
188
189
189
190 def generate_prompt(self, is_continuation):
190 def generate_prompt(self, is_continuation):
191 """ calculate and return a string with the prompt to display """
191 """ calculate and return a string with the prompt to display """
192 if is_continuation:
192 if is_continuation:
193 return str(self.displayhook.prompt2)
193 return str(self.displayhook.prompt2)
194 return str(self.displayhook.prompt1)
194 return str(self.displayhook.prompt1)
195
195
196
196
197 def show_in_pager(self,s):
197 def show_in_pager(self,s):
198 """ Run a string through pager """
198 """ Run a string through pager """
199 # raising TryNext here will use the default paging functionality
199 # raising TryNext here will use the default paging functionality
200 raise TryNext
200 raise TryNext
201
201
202
202
203 def pre_prompt_hook(self):
203 def pre_prompt_hook(self):
204 """ Run before displaying the next prompt
204 """ Run before displaying the next prompt
205
205
206 Use this e.g. to display output from asynchronous operations (in order
206 Use this e.g. to display output from asynchronous operations (in order
207 to not mess up text entry)
207 to not mess up text entry)
208 """
208 """
209
209
210 return None
210 return None
211
211
212
212
213 def pre_run_code_hook(self):
213 def pre_run_code_hook(self):
214 """ Executed before running the (prefiltered) code in IPython """
214 """ Executed before running the (prefiltered) code in IPython """
215 return None
215 return None
216
216
217
217
218 def clipboard_get(self):
218 def clipboard_get(self):
219 """ Get text from the clipboard.
219 """ Get text from the clipboard.
220 """
220 """
221 from IPython.lib.clipboard import (
221 from IPython.lib.clipboard import (
222 osx_clipboard_get, tkinter_clipboard_get,
222 osx_clipboard_get, tkinter_clipboard_get,
223 win32_clipboard_get
223 win32_clipboard_get
224 )
224 )
225 if sys.platform == 'win32':
225 if sys.platform == 'win32':
226 chain = [win32_clipboard_get, tkinter_clipboard_get]
226 chain = [win32_clipboard_get, tkinter_clipboard_get]
227 elif sys.platform == 'darwin':
227 elif sys.platform == 'darwin':
228 chain = [osx_clipboard_get, tkinter_clipboard_get]
228 chain = [osx_clipboard_get, tkinter_clipboard_get]
229 else:
229 else:
230 chain = [tkinter_clipboard_get]
230 chain = [tkinter_clipboard_get]
231 dispatcher = CommandChainDispatcher()
231 dispatcher = CommandChainDispatcher()
232 for func in chain:
232 for func in chain:
233 dispatcher.add(func)
233 dispatcher.add(func)
234 text = dispatcher()
234 text = dispatcher()
235 return text
235 return text
@@ -1,2598 +1,2598 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Main IPython class."""
2 """Main IPython class."""
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2008-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from __future__ import with_statement
17 from __future__ import with_statement
18 from __future__ import absolute_import
18 from __future__ import absolute_import
19
19
20 import __builtin__ as builtin_mod
20 import __builtin__ as builtin_mod
21 import __future__
21 import __future__
22 import abc
22 import abc
23 import ast
23 import ast
24 import atexit
24 import atexit
25 import codeop
25 import codeop
26 import inspect
26 import inspect
27 import os
27 import os
28 import re
28 import re
29 import sys
29 import sys
30 import tempfile
30 import tempfile
31 import types
31 import types
32 try:
32 try:
33 from contextlib import nested
33 from contextlib import nested
34 except:
34 except:
35 from IPython.utils.nested_context import nested
35 from IPython.utils.nested_context import nested
36
36
37 from IPython.config.configurable import SingletonConfigurable
37 from IPython.config.configurable import SingletonConfigurable
38 from IPython.core import debugger, oinspect
38 from IPython.core import debugger, oinspect
39 from IPython.core import history as ipcorehist
39 from IPython.core import history as ipcorehist
40 from IPython.core import page
40 from IPython.core import page
41 from IPython.core import prefilter
41 from IPython.core import prefilter
42 from IPython.core import shadowns
42 from IPython.core import shadowns
43 from IPython.core import ultratb
43 from IPython.core import ultratb
44 from IPython.core.alias import AliasManager, AliasError
44 from IPython.core.alias import AliasManager, AliasError
45 from IPython.core.autocall import ExitAutocall
45 from IPython.core.autocall import ExitAutocall
46 from IPython.core.builtin_trap import BuiltinTrap
46 from IPython.core.builtin_trap import BuiltinTrap
47 from IPython.core.compilerop import CachingCompiler
47 from IPython.core.compilerop import CachingCompiler
48 from IPython.core.display_trap import DisplayTrap
48 from IPython.core.display_trap import DisplayTrap
49 from IPython.core.displayhook import DisplayHook
49 from IPython.core.displayhook import DisplayHook
50 from IPython.core.displaypub import DisplayPublisher
50 from IPython.core.displaypub import DisplayPublisher
51 from IPython.core.error import TryNext, UsageError
51 from IPython.core.error import TryNext, UsageError
52 from IPython.core.extensions import ExtensionManager
52 from IPython.core.extensions import ExtensionManager
53 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
53 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
54 from IPython.core.formatters import DisplayFormatter
54 from IPython.core.formatters import DisplayFormatter
55 from IPython.core.history import HistoryManager
55 from IPython.core.history import HistoryManager
56 from IPython.core.inputsplitter import IPythonInputSplitter
56 from IPython.core.inputsplitter import IPythonInputSplitter
57 from IPython.core.logger import Logger
57 from IPython.core.logger import Logger
58 from IPython.core.macro import Macro
58 from IPython.core.macro import Macro
59 from IPython.core.magic import Magic
59 from IPython.core.magic import Magic
60 from IPython.core.payload import PayloadManager
60 from IPython.core.payload import PayloadManager
61 from IPython.core.plugin import PluginManager
61 from IPython.core.plugin import PluginManager
62 from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
62 from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
63 from IPython.core.profiledir import ProfileDir
63 from IPython.core.profiledir import ProfileDir
64 from IPython.external.Itpl import ItplNS
64 from IPython.external.Itpl import ItplNS
65 from IPython.utils import PyColorize
65 from IPython.utils import PyColorize
66 from IPython.utils import io
66 from IPython.utils import io
67 from IPython.utils import py3compat
67 from IPython.utils import py3compat
68 from IPython.utils.doctestreload import doctest_reload
68 from IPython.utils.doctestreload import doctest_reload
69 from IPython.utils.io import ask_yes_no, rprint
69 from IPython.utils.io import ask_yes_no, rprint
70 from IPython.utils.ipstruct import Struct
70 from IPython.utils.ipstruct import Struct
71 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
71 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
72 from IPython.utils.pickleshare import PickleShareDB
72 from IPython.utils.pickleshare import PickleShareDB
73 from IPython.utils.process import system, getoutput
73 from IPython.utils.process import system, getoutput
74 from IPython.utils.strdispatch import StrDispatch
74 from IPython.utils.strdispatch import StrDispatch
75 from IPython.utils.syspathcontext import prepended_to_syspath
75 from IPython.utils.syspathcontext import prepended_to_syspath
76 from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList
76 from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList
77 from IPython.utils.traitlets import (Int, CBool, CaselessStrEnum, Enum,
77 from IPython.utils.traitlets import (Int, CBool, CaselessStrEnum, Enum,
78 List, Unicode, Instance, Type)
78 List, Unicode, Instance, Type)
79 from IPython.utils.warn import warn, error, fatal
79 from IPython.utils.warn import warn, error, fatal
80 import IPython.core.hooks
80 import IPython.core.hooks
81
81
82 #-----------------------------------------------------------------------------
82 #-----------------------------------------------------------------------------
83 # Globals
83 # Globals
84 #-----------------------------------------------------------------------------
84 #-----------------------------------------------------------------------------
85
85
86 # compiled regexps for autoindent management
86 # compiled regexps for autoindent management
87 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
87 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
88
88
89 #-----------------------------------------------------------------------------
89 #-----------------------------------------------------------------------------
90 # Utilities
90 # Utilities
91 #-----------------------------------------------------------------------------
91 #-----------------------------------------------------------------------------
92
92
93 def softspace(file, newvalue):
93 def softspace(file, newvalue):
94 """Copied from code.py, to remove the dependency"""
94 """Copied from code.py, to remove the dependency"""
95
95
96 oldvalue = 0
96 oldvalue = 0
97 try:
97 try:
98 oldvalue = file.softspace
98 oldvalue = file.softspace
99 except AttributeError:
99 except AttributeError:
100 pass
100 pass
101 try:
101 try:
102 file.softspace = newvalue
102 file.softspace = newvalue
103 except (AttributeError, TypeError):
103 except (AttributeError, TypeError):
104 # "attribute-less object" or "read-only attributes"
104 # "attribute-less object" or "read-only attributes"
105 pass
105 pass
106 return oldvalue
106 return oldvalue
107
107
108
108
109 def no_op(*a, **kw): pass
109 def no_op(*a, **kw): pass
110
110
111 class NoOpContext(object):
111 class NoOpContext(object):
112 def __enter__(self): pass
112 def __enter__(self): pass
113 def __exit__(self, type, value, traceback): pass
113 def __exit__(self, type, value, traceback): pass
114 no_op_context = NoOpContext()
114 no_op_context = NoOpContext()
115
115
116 class SpaceInInput(Exception): pass
116 class SpaceInInput(Exception): pass
117
117
118 class Bunch: pass
118 class Bunch: pass
119
119
120
120
121 def get_default_colors():
121 def get_default_colors():
122 if sys.platform=='darwin':
122 if sys.platform=='darwin':
123 return "LightBG"
123 return "LightBG"
124 elif os.name=='nt':
124 elif os.name=='nt':
125 return 'Linux'
125 return 'Linux'
126 else:
126 else:
127 return 'Linux'
127 return 'Linux'
128
128
129
129
130 class SeparateUnicode(Unicode):
130 class SeparateUnicode(Unicode):
131 """A Unicode subclass to validate separate_in, separate_out, etc.
131 """A Unicode subclass to validate separate_in, separate_out, etc.
132
132
133 This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'.
133 This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'.
134 """
134 """
135
135
136 def validate(self, obj, value):
136 def validate(self, obj, value):
137 if value == '0': value = ''
137 if value == '0': value = ''
138 value = value.replace('\\n','\n')
138 value = value.replace('\\n','\n')
139 return super(SeparateUnicode, self).validate(obj, value)
139 return super(SeparateUnicode, self).validate(obj, value)
140
140
141
141
142 class ReadlineNoRecord(object):
142 class ReadlineNoRecord(object):
143 """Context manager to execute some code, then reload readline history
143 """Context manager to execute some code, then reload readline history
144 so that interactive input to the code doesn't appear when pressing up."""
144 so that interactive input to the code doesn't appear when pressing up."""
145 def __init__(self, shell):
145 def __init__(self, shell):
146 self.shell = shell
146 self.shell = shell
147 self._nested_level = 0
147 self._nested_level = 0
148
148
149 def __enter__(self):
149 def __enter__(self):
150 if self._nested_level == 0:
150 if self._nested_level == 0:
151 try:
151 try:
152 self.orig_length = self.current_length()
152 self.orig_length = self.current_length()
153 self.readline_tail = self.get_readline_tail()
153 self.readline_tail = self.get_readline_tail()
154 except (AttributeError, IndexError): # Can fail with pyreadline
154 except (AttributeError, IndexError): # Can fail with pyreadline
155 self.orig_length, self.readline_tail = 999999, []
155 self.orig_length, self.readline_tail = 999999, []
156 self._nested_level += 1
156 self._nested_level += 1
157
157
158 def __exit__(self, type, value, traceback):
158 def __exit__(self, type, value, traceback):
159 self._nested_level -= 1
159 self._nested_level -= 1
160 if self._nested_level == 0:
160 if self._nested_level == 0:
161 # Try clipping the end if it's got longer
161 # Try clipping the end if it's got longer
162 try:
162 try:
163 e = self.current_length() - self.orig_length
163 e = self.current_length() - self.orig_length
164 if e > 0:
164 if e > 0:
165 for _ in range(e):
165 for _ in range(e):
166 self.shell.readline.remove_history_item(self.orig_length)
166 self.shell.readline.remove_history_item(self.orig_length)
167
167
168 # If it still doesn't match, just reload readline history.
168 # If it still doesn't match, just reload readline history.
169 if self.current_length() != self.orig_length \
169 if self.current_length() != self.orig_length \
170 or self.get_readline_tail() != self.readline_tail:
170 or self.get_readline_tail() != self.readline_tail:
171 self.shell.refill_readline_hist()
171 self.shell.refill_readline_hist()
172 except (AttributeError, IndexError):
172 except (AttributeError, IndexError):
173 pass
173 pass
174 # Returning False will cause exceptions to propagate
174 # Returning False will cause exceptions to propagate
175 return False
175 return False
176
176
177 def current_length(self):
177 def current_length(self):
178 return self.shell.readline.get_current_history_length()
178 return self.shell.readline.get_current_history_length()
179
179
180 def get_readline_tail(self, n=10):
180 def get_readline_tail(self, n=10):
181 """Get the last n items in readline history."""
181 """Get the last n items in readline history."""
182 end = self.shell.readline.get_current_history_length() + 1
182 end = self.shell.readline.get_current_history_length() + 1
183 start = max(end-n, 1)
183 start = max(end-n, 1)
184 ghi = self.shell.readline.get_history_item
184 ghi = self.shell.readline.get_history_item
185 return [ghi(x) for x in range(start, end)]
185 return [ghi(x) for x in range(start, end)]
186
186
187
187
188 _autocall_help = """
188 _autocall_help = """
189 Make IPython automatically call any callable object even if
189 Make IPython automatically call any callable object even if
190 you didn't type explicit parentheses. For example, 'str 43' becomes 'str(43)'
190 you didn't type explicit parentheses. For example, 'str 43' becomes 'str(43)'
191 automatically. The value can be '0' to disable the feature, '1' for 'smart'
191 automatically. The value can be '0' to disable the feature, '1' for 'smart'
192 autocall, where it is not applied if there are no more arguments on the line,
192 autocall, where it is not applied if there are no more arguments on the line,
193 and '2' for 'full' autocall, where all callable objects are automatically
193 and '2' for 'full' autocall, where all callable objects are automatically
194 called (even if no arguments are present). The default is '1'.
194 called (even if no arguments are present). The default is '1'.
195 """
195 """
196
196
197 #-----------------------------------------------------------------------------
197 #-----------------------------------------------------------------------------
198 # Main IPython class
198 # Main IPython class
199 #-----------------------------------------------------------------------------
199 #-----------------------------------------------------------------------------
200
200
201 class InteractiveShell(SingletonConfigurable, Magic):
201 class InteractiveShell(SingletonConfigurable, Magic):
202 """An enhanced, interactive shell for Python."""
202 """An enhanced, interactive shell for Python."""
203
203
204 _instance = None
204 _instance = None
205
205
206 autocall = Enum((0,1,2), default_value=1, config=True, help=
206 autocall = Enum((0,1,2), default_value=1, config=True, help=
207 """
207 """
208 Make IPython automatically call any callable object even if you didn't
208 Make IPython automatically call any callable object even if you didn't
209 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
209 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
210 automatically. The value can be '0' to disable the feature, '1' for
210 automatically. The value can be '0' to disable the feature, '1' for
211 'smart' autocall, where it is not applied if there are no more
211 'smart' autocall, where it is not applied if there are no more
212 arguments on the line, and '2' for 'full' autocall, where all callable
212 arguments on the line, and '2' for 'full' autocall, where all callable
213 objects are automatically called (even if no arguments are present).
213 objects are automatically called (even if no arguments are present).
214 The default is '1'.
214 The default is '1'.
215 """
215 """
216 )
216 )
217 # TODO: remove all autoindent logic and put into frontends.
217 # TODO: remove all autoindent logic and put into frontends.
218 # We can't do this yet because even runlines uses the autoindent.
218 # We can't do this yet because even runlines uses the autoindent.
219 autoindent = CBool(True, config=True, help=
219 autoindent = CBool(True, config=True, help=
220 """
220 """
221 Autoindent IPython code entered interactively.
221 Autoindent IPython code entered interactively.
222 """
222 """
223 )
223 )
224 automagic = CBool(True, config=True, help=
224 automagic = CBool(True, config=True, help=
225 """
225 """
226 Enable magic commands to be called without the leading %.
226 Enable magic commands to be called without the leading %.
227 """
227 """
228 )
228 )
229 cache_size = Int(1000, config=True, help=
229 cache_size = Int(1000, config=True, help=
230 """
230 """
231 Set the size of the output cache. The default is 1000, you can
231 Set the size of the output cache. The default is 1000, you can
232 change it permanently in your config file. Setting it to 0 completely
232 change it permanently in your config file. Setting it to 0 completely
233 disables the caching system, and the minimum value accepted is 20 (if
233 disables the caching system, and the minimum value accepted is 20 (if
234 you provide a value less than 20, it is reset to 0 and a warning is
234 you provide a value less than 20, it is reset to 0 and a warning is
235 issued). This limit is defined because otherwise you'll spend more
235 issued). This limit is defined because otherwise you'll spend more
236 time re-flushing a too small cache than working
236 time re-flushing a too small cache than working
237 """
237 """
238 )
238 )
239 color_info = CBool(True, config=True, help=
239 color_info = CBool(True, config=True, help=
240 """
240 """
241 Use colors for displaying information about objects. Because this
241 Use colors for displaying information about objects. Because this
242 information is passed through a pager (like 'less'), and some pagers
242 information is passed through a pager (like 'less'), and some pagers
243 get confused with color codes, this capability can be turned off.
243 get confused with color codes, this capability can be turned off.
244 """
244 """
245 )
245 )
246 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
246 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
247 default_value=get_default_colors(), config=True,
247 default_value=get_default_colors(), config=True,
248 help="Set the color scheme (NoColor, Linux, or LightBG)."
248 help="Set the color scheme (NoColor, Linux, or LightBG)."
249 )
249 )
250 colors_force = CBool(False, help=
250 colors_force = CBool(False, help=
251 """
251 """
252 Force use of ANSI color codes, regardless of OS and readline
252 Force use of ANSI color codes, regardless of OS and readline
253 availability.
253 availability.
254 """
254 """
255 # FIXME: This is essentially a hack to allow ZMQShell to show colors
255 # FIXME: This is essentially a hack to allow ZMQShell to show colors
256 # without readline on Win32. When the ZMQ formatting system is
256 # without readline on Win32. When the ZMQ formatting system is
257 # refactored, this should be removed.
257 # refactored, this should be removed.
258 )
258 )
259 debug = CBool(False, config=True)
259 debug = CBool(False, config=True)
260 deep_reload = CBool(False, config=True, help=
260 deep_reload = CBool(False, config=True, help=
261 """
261 """
262 Enable deep (recursive) reloading by default. IPython can use the
262 Enable deep (recursive) reloading by default. IPython can use the
263 deep_reload module which reloads changes in modules recursively (it
263 deep_reload module which reloads changes in modules recursively (it
264 replaces the reload() function, so you don't need to change anything to
264 replaces the reload() function, so you don't need to change anything to
265 use it). deep_reload() forces a full reload of modules whose code may
265 use it). deep_reload() forces a full reload of modules whose code may
266 have changed, which the default reload() function does not. When
266 have changed, which the default reload() function does not. When
267 deep_reload is off, IPython will use the normal reload(), but
267 deep_reload is off, IPython will use the normal reload(), but
268 deep_reload will still be available as dreload().
268 deep_reload will still be available as dreload().
269 """
269 """
270 )
270 )
271 display_formatter = Instance(DisplayFormatter)
271 display_formatter = Instance(DisplayFormatter)
272 displayhook_class = Type(DisplayHook)
272 displayhook_class = Type(DisplayHook)
273 display_pub_class = Type(DisplayPublisher)
273 display_pub_class = Type(DisplayPublisher)
274
274
275 exit_now = CBool(False)
275 exit_now = CBool(False)
276 exiter = Instance(ExitAutocall)
276 exiter = Instance(ExitAutocall)
277 def _exiter_default(self):
277 def _exiter_default(self):
278 return ExitAutocall(self)
278 return ExitAutocall(self)
279 # Monotonically increasing execution counter
279 # Monotonically increasing execution counter
280 execution_count = Int(1)
280 execution_count = Int(1)
281 filename = Unicode("<ipython console>")
281 filename = Unicode("<ipython console>")
282 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
282 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
283
283
284 # Input splitter, to split entire cells of input into either individual
284 # Input splitter, to split entire cells of input into either individual
285 # interactive statements or whole blocks.
285 # interactive statements or whole blocks.
286 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
286 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
287 (), {})
287 (), {})
288 logstart = CBool(False, config=True, help=
288 logstart = CBool(False, config=True, help=
289 """
289 """
290 Start logging to the default log file.
290 Start logging to the default log file.
291 """
291 """
292 )
292 )
293 logfile = Unicode('', config=True, help=
293 logfile = Unicode('', config=True, help=
294 """
294 """
295 The name of the logfile to use.
295 The name of the logfile to use.
296 """
296 """
297 )
297 )
298 logappend = Unicode('', config=True, help=
298 logappend = Unicode('', config=True, help=
299 """
299 """
300 Start logging to the given file in append mode.
300 Start logging to the given file in append mode.
301 """
301 """
302 )
302 )
303 object_info_string_level = Enum((0,1,2), default_value=0,
303 object_info_string_level = Enum((0,1,2), default_value=0,
304 config=True)
304 config=True)
305 pdb = CBool(False, config=True, help=
305 pdb = CBool(False, config=True, help=
306 """
306 """
307 Automatically call the pdb debugger after every exception.
307 Automatically call the pdb debugger after every exception.
308 """
308 """
309 )
309 )
310
310
311 prompt_in1 = Unicode('In [\\#]: ', config=True)
311 prompt_in1 = Unicode('In [\\#]: ', config=True)
312 prompt_in2 = Unicode(' .\\D.: ', config=True)
312 prompt_in2 = Unicode(' .\\D.: ', config=True)
313 prompt_out = Unicode('Out[\\#]: ', config=True)
313 prompt_out = Unicode('Out[\\#]: ', config=True)
314 prompts_pad_left = CBool(True, config=True)
314 prompts_pad_left = CBool(True, config=True)
315 quiet = CBool(False, config=True)
315 quiet = CBool(False, config=True)
316
316
317 history_length = Int(10000, config=True)
317 history_length = Int(10000, config=True)
318
318
319 # The readline stuff will eventually be moved to the terminal subclass
319 # The readline stuff will eventually be moved to the terminal subclass
320 # but for now, we can't do that as readline is welded in everywhere.
320 # but for now, we can't do that as readline is welded in everywhere.
321 readline_use = CBool(True, config=True)
321 readline_use = CBool(True, config=True)
322 readline_merge_completions = CBool(True, config=True)
322 readline_merge_completions = CBool(True, config=True)
323 readline_omit__names = Enum((0,1,2), default_value=2, config=True)
323 readline_omit__names = Enum((0,1,2), default_value=2, config=True)
324 readline_remove_delims = Unicode('-/~', config=True)
324 readline_remove_delims = Unicode('-/~', config=True)
325 # don't use \M- bindings by default, because they
325 # don't use \M- bindings by default, because they
326 # conflict with 8-bit encodings. See gh-58,gh-88
326 # conflict with 8-bit encodings. See gh-58,gh-88
327 readline_parse_and_bind = List([
327 readline_parse_and_bind = List([
328 'tab: complete',
328 'tab: complete',
329 '"\C-l": clear-screen',
329 '"\C-l": clear-screen',
330 'set show-all-if-ambiguous on',
330 'set show-all-if-ambiguous on',
331 '"\C-o": tab-insert',
331 '"\C-o": tab-insert',
332 '"\C-r": reverse-search-history',
332 '"\C-r": reverse-search-history',
333 '"\C-s": forward-search-history',
333 '"\C-s": forward-search-history',
334 '"\C-p": history-search-backward',
334 '"\C-p": history-search-backward',
335 '"\C-n": history-search-forward',
335 '"\C-n": history-search-forward',
336 '"\e[A": history-search-backward',
336 '"\e[A": history-search-backward',
337 '"\e[B": history-search-forward',
337 '"\e[B": history-search-forward',
338 '"\C-k": kill-line',
338 '"\C-k": kill-line',
339 '"\C-u": unix-line-discard',
339 '"\C-u": unix-line-discard',
340 ], allow_none=False, config=True)
340 ], allow_none=False, config=True)
341
341
342 # TODO: this part of prompt management should be moved to the frontends.
342 # TODO: this part of prompt management should be moved to the frontends.
343 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
343 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
344 separate_in = SeparateUnicode('\n', config=True)
344 separate_in = SeparateUnicode('\n', config=True)
345 separate_out = SeparateUnicode('', config=True)
345 separate_out = SeparateUnicode('', config=True)
346 separate_out2 = SeparateUnicode('', config=True)
346 separate_out2 = SeparateUnicode('', config=True)
347 wildcards_case_sensitive = CBool(True, config=True)
347 wildcards_case_sensitive = CBool(True, config=True)
348 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
348 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
349 default_value='Context', config=True)
349 default_value='Context', config=True)
350
350
351 # Subcomponents of InteractiveShell
351 # Subcomponents of InteractiveShell
352 alias_manager = Instance('IPython.core.alias.AliasManager')
352 alias_manager = Instance('IPython.core.alias.AliasManager')
353 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
353 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
354 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
354 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
355 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
355 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
356 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
356 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
357 plugin_manager = Instance('IPython.core.plugin.PluginManager')
357 plugin_manager = Instance('IPython.core.plugin.PluginManager')
358 payload_manager = Instance('IPython.core.payload.PayloadManager')
358 payload_manager = Instance('IPython.core.payload.PayloadManager')
359 history_manager = Instance('IPython.core.history.HistoryManager')
359 history_manager = Instance('IPython.core.history.HistoryManager')
360
360
361 profile_dir = Instance('IPython.core.application.ProfileDir')
361 profile_dir = Instance('IPython.core.application.ProfileDir')
362 @property
362 @property
363 def profile(self):
363 def profile(self):
364 if self.profile_dir is not None:
364 if self.profile_dir is not None:
365 name = os.path.basename(self.profile_dir.location)
365 name = os.path.basename(self.profile_dir.location)
366 return name.replace('profile_','')
366 return name.replace('profile_','')
367
367
368
368
369 # Private interface
369 # Private interface
370 _post_execute = Instance(dict)
370 _post_execute = Instance(dict)
371
371
372 def __init__(self, config=None, ipython_dir=None, profile_dir=None,
372 def __init__(self, config=None, ipython_dir=None, profile_dir=None,
373 user_ns=None, user_global_ns=None,
373 user_ns=None, user_global_ns=None,
374 custom_exceptions=((), None)):
374 custom_exceptions=((), None)):
375
375
376 # This is where traits with a config_key argument are updated
376 # This is where traits with a config_key argument are updated
377 # from the values on config.
377 # from the values on config.
378 super(InteractiveShell, self).__init__(config=config)
378 super(InteractiveShell, self).__init__(config=config)
379
379
380 # These are relatively independent and stateless
380 # These are relatively independent and stateless
381 self.init_ipython_dir(ipython_dir)
381 self.init_ipython_dir(ipython_dir)
382 self.init_profile_dir(profile_dir)
382 self.init_profile_dir(profile_dir)
383 self.init_instance_attrs()
383 self.init_instance_attrs()
384 self.init_environment()
384 self.init_environment()
385
385
386 # Create namespaces (user_ns, user_global_ns, etc.)
386 # Create namespaces (user_ns, user_global_ns, etc.)
387 self.init_create_namespaces(user_ns, user_global_ns)
387 self.init_create_namespaces(user_ns, user_global_ns)
388 # This has to be done after init_create_namespaces because it uses
388 # This has to be done after init_create_namespaces because it uses
389 # something in self.user_ns, but before init_sys_modules, which
389 # something in self.user_ns, but before init_sys_modules, which
390 # is the first thing to modify sys.
390 # is the first thing to modify sys.
391 # TODO: When we override sys.stdout and sys.stderr before this class
391 # TODO: When we override sys.stdout and sys.stderr before this class
392 # is created, we are saving the overridden ones here. Not sure if this
392 # is created, we are saving the overridden ones here. Not sure if this
393 # is what we want to do.
393 # is what we want to do.
394 self.save_sys_module_state()
394 self.save_sys_module_state()
395 self.init_sys_modules()
395 self.init_sys_modules()
396
396
397 # While we're trying to have each part of the code directly access what
397 # While we're trying to have each part of the code directly access what
398 # it needs without keeping redundant references to objects, we have too
398 # it needs without keeping redundant references to objects, we have too
399 # much legacy code that expects ip.db to exist.
399 # much legacy code that expects ip.db to exist.
400 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
400 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
401
401
402 self.init_history()
402 self.init_history()
403 self.init_encoding()
403 self.init_encoding()
404 self.init_prefilter()
404 self.init_prefilter()
405
405
406 Magic.__init__(self, self)
406 Magic.__init__(self, self)
407
407
408 self.init_syntax_highlighting()
408 self.init_syntax_highlighting()
409 self.init_hooks()
409 self.init_hooks()
410 self.init_pushd_popd_magic()
410 self.init_pushd_popd_magic()
411 # self.init_traceback_handlers use to be here, but we moved it below
411 # self.init_traceback_handlers use to be here, but we moved it below
412 # because it and init_io have to come after init_readline.
412 # because it and init_io have to come after init_readline.
413 self.init_user_ns()
413 self.init_user_ns()
414 self.init_logger()
414 self.init_logger()
415 self.init_alias()
415 self.init_alias()
416 self.init_builtins()
416 self.init_builtins()
417
417
418 # pre_config_initialization
418 # pre_config_initialization
419
419
420 # The next section should contain everything that was in ipmaker.
420 # The next section should contain everything that was in ipmaker.
421 self.init_logstart()
421 self.init_logstart()
422
422
423 # The following was in post_config_initialization
423 # The following was in post_config_initialization
424 self.init_inspector()
424 self.init_inspector()
425 # init_readline() must come before init_io(), because init_io uses
425 # init_readline() must come before init_io(), because init_io uses
426 # readline related things.
426 # readline related things.
427 self.init_readline()
427 self.init_readline()
428 # We save this here in case user code replaces raw_input, but it needs
428 # We save this here in case user code replaces raw_input, but it needs
429 # to be after init_readline(), because PyPy's readline works by replacing
429 # to be after init_readline(), because PyPy's readline works by replacing
430 # raw_input.
430 # raw_input.
431 if py3compat.PY3:
431 if py3compat.PY3:
432 self.raw_input_original = input
432 self.raw_input_original = input
433 else:
433 else:
434 self.raw_input_original = raw_input
434 self.raw_input_original = raw_input
435 # init_completer must come after init_readline, because it needs to
435 # init_completer must come after init_readline, because it needs to
436 # know whether readline is present or not system-wide to configure the
436 # know whether readline is present or not system-wide to configure the
437 # completers, since the completion machinery can now operate
437 # completers, since the completion machinery can now operate
438 # independently of readline (e.g. over the network)
438 # independently of readline (e.g. over the network)
439 self.init_completer()
439 self.init_completer()
440 # TODO: init_io() needs to happen before init_traceback handlers
440 # TODO: init_io() needs to happen before init_traceback handlers
441 # because the traceback handlers hardcode the stdout/stderr streams.
441 # because the traceback handlers hardcode the stdout/stderr streams.
442 # This logic in in debugger.Pdb and should eventually be changed.
442 # This logic in in debugger.Pdb and should eventually be changed.
443 self.init_io()
443 self.init_io()
444 self.init_traceback_handlers(custom_exceptions)
444 self.init_traceback_handlers(custom_exceptions)
445 self.init_prompts()
445 self.init_prompts()
446 self.init_display_formatter()
446 self.init_display_formatter()
447 self.init_display_pub()
447 self.init_display_pub()
448 self.init_displayhook()
448 self.init_displayhook()
449 self.init_reload_doctest()
449 self.init_reload_doctest()
450 self.init_magics()
450 self.init_magics()
451 self.init_pdb()
451 self.init_pdb()
452 self.init_extension_manager()
452 self.init_extension_manager()
453 self.init_plugin_manager()
453 self.init_plugin_manager()
454 self.init_payload()
454 self.init_payload()
455 self.hooks.late_startup_hook()
455 self.hooks.late_startup_hook()
456 atexit.register(self.atexit_operations)
456 atexit.register(self.atexit_operations)
457
457
458 def get_ipython(self):
458 def get_ipython(self):
459 """Return the currently running IPython instance."""
459 """Return the currently running IPython instance."""
460 return self
460 return self
461
461
462 #-------------------------------------------------------------------------
462 #-------------------------------------------------------------------------
463 # Trait changed handlers
463 # Trait changed handlers
464 #-------------------------------------------------------------------------
464 #-------------------------------------------------------------------------
465
465
466 def _ipython_dir_changed(self, name, new):
466 def _ipython_dir_changed(self, name, new):
467 if not os.path.isdir(new):
467 if not os.path.isdir(new):
468 os.makedirs(new, mode = 0777)
468 os.makedirs(new, mode = 0777)
469
469
470 def set_autoindent(self,value=None):
470 def set_autoindent(self,value=None):
471 """Set the autoindent flag, checking for readline support.
471 """Set the autoindent flag, checking for readline support.
472
472
473 If called with no arguments, it acts as a toggle."""
473 If called with no arguments, it acts as a toggle."""
474
474
475 if value != 0 and not self.has_readline:
475 if value != 0 and not self.has_readline:
476 if os.name == 'posix':
476 if os.name == 'posix':
477 warn("The auto-indent feature requires the readline library")
477 warn("The auto-indent feature requires the readline library")
478 self.autoindent = 0
478 self.autoindent = 0
479 return
479 return
480 if value is None:
480 if value is None:
481 self.autoindent = not self.autoindent
481 self.autoindent = not self.autoindent
482 else:
482 else:
483 self.autoindent = value
483 self.autoindent = value
484
484
485 #-------------------------------------------------------------------------
485 #-------------------------------------------------------------------------
486 # init_* methods called by __init__
486 # init_* methods called by __init__
487 #-------------------------------------------------------------------------
487 #-------------------------------------------------------------------------
488
488
489 def init_ipython_dir(self, ipython_dir):
489 def init_ipython_dir(self, ipython_dir):
490 if ipython_dir is not None:
490 if ipython_dir is not None:
491 self.ipython_dir = ipython_dir
491 self.ipython_dir = ipython_dir
492 return
492 return
493
493
494 self.ipython_dir = get_ipython_dir()
494 self.ipython_dir = get_ipython_dir()
495
495
496 def init_profile_dir(self, profile_dir):
496 def init_profile_dir(self, profile_dir):
497 if profile_dir is not None:
497 if profile_dir is not None:
498 self.profile_dir = profile_dir
498 self.profile_dir = profile_dir
499 return
499 return
500 self.profile_dir =\
500 self.profile_dir =\
501 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
501 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
502
502
503 def init_instance_attrs(self):
503 def init_instance_attrs(self):
504 self.more = False
504 self.more = False
505
505
506 # command compiler
506 # command compiler
507 self.compile = CachingCompiler()
507 self.compile = CachingCompiler()
508
508
509 # Make an empty namespace, which extension writers can rely on both
509 # Make an empty namespace, which extension writers can rely on both
510 # existing and NEVER being used by ipython itself. This gives them a
510 # existing and NEVER being used by ipython itself. This gives them a
511 # convenient location for storing additional information and state
511 # convenient location for storing additional information and state
512 # their extensions may require, without fear of collisions with other
512 # their extensions may require, without fear of collisions with other
513 # ipython names that may develop later.
513 # ipython names that may develop later.
514 self.meta = Struct()
514 self.meta = Struct()
515
515
516 # Temporary files used for various purposes. Deleted at exit.
516 # Temporary files used for various purposes. Deleted at exit.
517 self.tempfiles = []
517 self.tempfiles = []
518
518
519 # Keep track of readline usage (later set by init_readline)
519 # Keep track of readline usage (later set by init_readline)
520 self.has_readline = False
520 self.has_readline = False
521
521
522 # keep track of where we started running (mainly for crash post-mortem)
522 # keep track of where we started running (mainly for crash post-mortem)
523 # This is not being used anywhere currently.
523 # This is not being used anywhere currently.
524 self.starting_dir = os.getcwdu()
524 self.starting_dir = os.getcwdu()
525
525
526 # Indentation management
526 # Indentation management
527 self.indent_current_nsp = 0
527 self.indent_current_nsp = 0
528
528
529 # Dict to track post-execution functions that have been registered
529 # Dict to track post-execution functions that have been registered
530 self._post_execute = {}
530 self._post_execute = {}
531
531
532 def init_environment(self):
532 def init_environment(self):
533 """Any changes we need to make to the user's environment."""
533 """Any changes we need to make to the user's environment."""
534 pass
534 pass
535
535
536 def init_encoding(self):
536 def init_encoding(self):
537 # Get system encoding at startup time. Certain terminals (like Emacs
537 # Get system encoding at startup time. Certain terminals (like Emacs
538 # under Win32 have it set to None, and we need to have a known valid
538 # under Win32 have it set to None, and we need to have a known valid
539 # encoding to use in the raw_input() method
539 # encoding to use in the raw_input() method
540 try:
540 try:
541 self.stdin_encoding = sys.stdin.encoding or 'ascii'
541 self.stdin_encoding = sys.stdin.encoding or 'ascii'
542 except AttributeError:
542 except AttributeError:
543 self.stdin_encoding = 'ascii'
543 self.stdin_encoding = 'ascii'
544
544
545 def init_syntax_highlighting(self):
545 def init_syntax_highlighting(self):
546 # Python source parser/formatter for syntax highlighting
546 # Python source parser/formatter for syntax highlighting
547 pyformat = PyColorize.Parser().format
547 pyformat = PyColorize.Parser().format
548 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
548 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
549
549
550 def init_pushd_popd_magic(self):
550 def init_pushd_popd_magic(self):
551 # for pushd/popd management
551 # for pushd/popd management
552 try:
552 try:
553 self.home_dir = get_home_dir()
553 self.home_dir = get_home_dir()
554 except HomeDirError, msg:
554 except HomeDirError, msg:
555 fatal(msg)
555 fatal(msg)
556
556
557 self.dir_stack = []
557 self.dir_stack = []
558
558
559 def init_logger(self):
559 def init_logger(self):
560 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
560 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
561 logmode='rotate')
561 logmode='rotate')
562
562
563 def init_logstart(self):
563 def init_logstart(self):
564 """Initialize logging in case it was requested at the command line.
564 """Initialize logging in case it was requested at the command line.
565 """
565 """
566 if self.logappend:
566 if self.logappend:
567 self.magic_logstart(self.logappend + ' append')
567 self.magic_logstart(self.logappend + ' append')
568 elif self.logfile:
568 elif self.logfile:
569 self.magic_logstart(self.logfile)
569 self.magic_logstart(self.logfile)
570 elif self.logstart:
570 elif self.logstart:
571 self.magic_logstart()
571 self.magic_logstart()
572
572
573 def init_builtins(self):
573 def init_builtins(self):
574 self.builtin_trap = BuiltinTrap(shell=self)
574 self.builtin_trap = BuiltinTrap(shell=self)
575
575
576 def init_inspector(self):
576 def init_inspector(self):
577 # Object inspector
577 # Object inspector
578 self.inspector = oinspect.Inspector(oinspect.InspectColors,
578 self.inspector = oinspect.Inspector(oinspect.InspectColors,
579 PyColorize.ANSICodeColors,
579 PyColorize.ANSICodeColors,
580 'NoColor',
580 'NoColor',
581 self.object_info_string_level)
581 self.object_info_string_level)
582
582
583 def init_io(self):
583 def init_io(self):
584 # This will just use sys.stdout and sys.stderr. If you want to
584 # This will just use sys.stdout and sys.stderr. If you want to
585 # override sys.stdout and sys.stderr themselves, you need to do that
585 # override sys.stdout and sys.stderr themselves, you need to do that
586 # *before* instantiating this class, because io holds onto
586 # *before* instantiating this class, because io holds onto
587 # references to the underlying streams.
587 # references to the underlying streams.
588 if sys.platform == 'win32' and self.has_readline:
588 if sys.platform == 'win32' and self.has_readline:
589 io.stdout = io.stderr = io.IOStream(self.readline._outputfile)
589 io.stdout = io.stderr = io.IOStream(self.readline._outputfile)
590 else:
590 else:
591 io.stdout = io.IOStream(sys.stdout)
591 io.stdout = io.IOStream(sys.stdout)
592 io.stderr = io.IOStream(sys.stderr)
592 io.stderr = io.IOStream(sys.stderr)
593
593
594 def init_prompts(self):
594 def init_prompts(self):
595 # TODO: This is a pass for now because the prompts are managed inside
595 # TODO: This is a pass for now because the prompts are managed inside
596 # the DisplayHook. Once there is a separate prompt manager, this
596 # the DisplayHook. Once there is a separate prompt manager, this
597 # will initialize that object and all prompt related information.
597 # will initialize that object and all prompt related information.
598 pass
598 pass
599
599
600 def init_display_formatter(self):
600 def init_display_formatter(self):
601 self.display_formatter = DisplayFormatter(config=self.config)
601 self.display_formatter = DisplayFormatter(config=self.config)
602
602
603 def init_display_pub(self):
603 def init_display_pub(self):
604 self.display_pub = self.display_pub_class(config=self.config)
604 self.display_pub = self.display_pub_class(config=self.config)
605
605
606 def init_displayhook(self):
606 def init_displayhook(self):
607 # Initialize displayhook, set in/out prompts and printing system
607 # Initialize displayhook, set in/out prompts and printing system
608 self.displayhook = self.displayhook_class(
608 self.displayhook = self.displayhook_class(
609 config=self.config,
609 config=self.config,
610 shell=self,
610 shell=self,
611 cache_size=self.cache_size,
611 cache_size=self.cache_size,
612 input_sep = self.separate_in,
612 input_sep = self.separate_in,
613 output_sep = self.separate_out,
613 output_sep = self.separate_out,
614 output_sep2 = self.separate_out2,
614 output_sep2 = self.separate_out2,
615 ps1 = self.prompt_in1,
615 ps1 = self.prompt_in1,
616 ps2 = self.prompt_in2,
616 ps2 = self.prompt_in2,
617 ps_out = self.prompt_out,
617 ps_out = self.prompt_out,
618 pad_left = self.prompts_pad_left
618 pad_left = self.prompts_pad_left
619 )
619 )
620 # This is a context manager that installs/revmoes the displayhook at
620 # This is a context manager that installs/revmoes the displayhook at
621 # the appropriate time.
621 # the appropriate time.
622 self.display_trap = DisplayTrap(hook=self.displayhook)
622 self.display_trap = DisplayTrap(hook=self.displayhook)
623
623
624 def init_reload_doctest(self):
624 def init_reload_doctest(self):
625 # Do a proper resetting of doctest, including the necessary displayhook
625 # Do a proper resetting of doctest, including the necessary displayhook
626 # monkeypatching
626 # monkeypatching
627 try:
627 try:
628 doctest_reload()
628 doctest_reload()
629 except ImportError:
629 except ImportError:
630 warn("doctest module does not exist.")
630 warn("doctest module does not exist.")
631
631
632 #-------------------------------------------------------------------------
632 #-------------------------------------------------------------------------
633 # Things related to injections into the sys module
633 # Things related to injections into the sys module
634 #-------------------------------------------------------------------------
634 #-------------------------------------------------------------------------
635
635
636 def save_sys_module_state(self):
636 def save_sys_module_state(self):
637 """Save the state of hooks in the sys module.
637 """Save the state of hooks in the sys module.
638
638
639 This has to be called after self.user_ns is created.
639 This has to be called after self.user_ns is created.
640 """
640 """
641 self._orig_sys_module_state = {}
641 self._orig_sys_module_state = {}
642 self._orig_sys_module_state['stdin'] = sys.stdin
642 self._orig_sys_module_state['stdin'] = sys.stdin
643 self._orig_sys_module_state['stdout'] = sys.stdout
643 self._orig_sys_module_state['stdout'] = sys.stdout
644 self._orig_sys_module_state['stderr'] = sys.stderr
644 self._orig_sys_module_state['stderr'] = sys.stderr
645 self._orig_sys_module_state['excepthook'] = sys.excepthook
645 self._orig_sys_module_state['excepthook'] = sys.excepthook
646 try:
646 try:
647 self._orig_sys_modules_main_name = self.user_ns['__name__']
647 self._orig_sys_modules_main_name = self.user_ns['__name__']
648 except KeyError:
648 except KeyError:
649 pass
649 pass
650
650
651 def restore_sys_module_state(self):
651 def restore_sys_module_state(self):
652 """Restore the state of the sys module."""
652 """Restore the state of the sys module."""
653 try:
653 try:
654 for k, v in self._orig_sys_module_state.iteritems():
654 for k, v in self._orig_sys_module_state.iteritems():
655 setattr(sys, k, v)
655 setattr(sys, k, v)
656 except AttributeError:
656 except AttributeError:
657 pass
657 pass
658 # Reset what what done in self.init_sys_modules
658 # Reset what what done in self.init_sys_modules
659 try:
659 try:
660 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
660 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
661 except (AttributeError, KeyError):
661 except (AttributeError, KeyError):
662 pass
662 pass
663
663
664 #-------------------------------------------------------------------------
664 #-------------------------------------------------------------------------
665 # Things related to hooks
665 # Things related to hooks
666 #-------------------------------------------------------------------------
666 #-------------------------------------------------------------------------
667
667
668 def init_hooks(self):
668 def init_hooks(self):
669 # hooks holds pointers used for user-side customizations
669 # hooks holds pointers used for user-side customizations
670 self.hooks = Struct()
670 self.hooks = Struct()
671
671
672 self.strdispatchers = {}
672 self.strdispatchers = {}
673
673
674 # Set all default hooks, defined in the IPython.hooks module.
674 # Set all default hooks, defined in the IPython.hooks module.
675 hooks = IPython.core.hooks
675 hooks = IPython.core.hooks
676 for hook_name in hooks.__all__:
676 for hook_name in hooks.__all__:
677 # default hooks have priority 100, i.e. low; user hooks should have
677 # default hooks have priority 100, i.e. low; user hooks should have
678 # 0-100 priority
678 # 0-100 priority
679 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
679 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
680
680
681 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
681 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
682 """set_hook(name,hook) -> sets an internal IPython hook.
682 """set_hook(name,hook) -> sets an internal IPython hook.
683
683
684 IPython exposes some of its internal API as user-modifiable hooks. By
684 IPython exposes some of its internal API as user-modifiable hooks. By
685 adding your function to one of these hooks, you can modify IPython's
685 adding your function to one of these hooks, you can modify IPython's
686 behavior to call at runtime your own routines."""
686 behavior to call at runtime your own routines."""
687
687
688 # At some point in the future, this should validate the hook before it
688 # At some point in the future, this should validate the hook before it
689 # accepts it. Probably at least check that the hook takes the number
689 # accepts it. Probably at least check that the hook takes the number
690 # of args it's supposed to.
690 # of args it's supposed to.
691
691
692 f = types.MethodType(hook,self)
692 f = types.MethodType(hook,self)
693
693
694 # check if the hook is for strdispatcher first
694 # check if the hook is for strdispatcher first
695 if str_key is not None:
695 if str_key is not None:
696 sdp = self.strdispatchers.get(name, StrDispatch())
696 sdp = self.strdispatchers.get(name, StrDispatch())
697 sdp.add_s(str_key, f, priority )
697 sdp.add_s(str_key, f, priority )
698 self.strdispatchers[name] = sdp
698 self.strdispatchers[name] = sdp
699 return
699 return
700 if re_key is not None:
700 if re_key is not None:
701 sdp = self.strdispatchers.get(name, StrDispatch())
701 sdp = self.strdispatchers.get(name, StrDispatch())
702 sdp.add_re(re.compile(re_key), f, priority )
702 sdp.add_re(re.compile(re_key), f, priority )
703 self.strdispatchers[name] = sdp
703 self.strdispatchers[name] = sdp
704 return
704 return
705
705
706 dp = getattr(self.hooks, name, None)
706 dp = getattr(self.hooks, name, None)
707 if name not in IPython.core.hooks.__all__:
707 if name not in IPython.core.hooks.__all__:
708 print "Warning! Hook '%s' is not one of %s" % \
708 print "Warning! Hook '%s' is not one of %s" % \
709 (name, IPython.core.hooks.__all__ )
709 (name, IPython.core.hooks.__all__ )
710 if not dp:
710 if not dp:
711 dp = IPython.core.hooks.CommandChainDispatcher()
711 dp = IPython.core.hooks.CommandChainDispatcher()
712
712
713 try:
713 try:
714 dp.add(f,priority)
714 dp.add(f,priority)
715 except AttributeError:
715 except AttributeError:
716 # it was not commandchain, plain old func - replace
716 # it was not commandchain, plain old func - replace
717 dp = f
717 dp = f
718
718
719 setattr(self.hooks,name, dp)
719 setattr(self.hooks,name, dp)
720
720
721 def register_post_execute(self, func):
721 def register_post_execute(self, func):
722 """Register a function for calling after code execution.
722 """Register a function for calling after code execution.
723 """
723 """
724 if not callable(func):
724 if not callable(func):
725 raise ValueError('argument %s must be callable' % func)
725 raise ValueError('argument %s must be callable' % func)
726 self._post_execute[func] = True
726 self._post_execute[func] = True
727
727
728 #-------------------------------------------------------------------------
728 #-------------------------------------------------------------------------
729 # Things related to the "main" module
729 # Things related to the "main" module
730 #-------------------------------------------------------------------------
730 #-------------------------------------------------------------------------
731
731
732 def new_main_mod(self,ns=None):
732 def new_main_mod(self,ns=None):
733 """Return a new 'main' module object for user code execution.
733 """Return a new 'main' module object for user code execution.
734 """
734 """
735 main_mod = self._user_main_module
735 main_mod = self._user_main_module
736 init_fakemod_dict(main_mod,ns)
736 init_fakemod_dict(main_mod,ns)
737 return main_mod
737 return main_mod
738
738
739 def cache_main_mod(self,ns,fname):
739 def cache_main_mod(self,ns,fname):
740 """Cache a main module's namespace.
740 """Cache a main module's namespace.
741
741
742 When scripts are executed via %run, we must keep a reference to the
742 When scripts are executed via %run, we must keep a reference to the
743 namespace of their __main__ module (a FakeModule instance) around so
743 namespace of their __main__ module (a FakeModule instance) around so
744 that Python doesn't clear it, rendering objects defined therein
744 that Python doesn't clear it, rendering objects defined therein
745 useless.
745 useless.
746
746
747 This method keeps said reference in a private dict, keyed by the
747 This method keeps said reference in a private dict, keyed by the
748 absolute path of the module object (which corresponds to the script
748 absolute path of the module object (which corresponds to the script
749 path). This way, for multiple executions of the same script we only
749 path). This way, for multiple executions of the same script we only
750 keep one copy of the namespace (the last one), thus preventing memory
750 keep one copy of the namespace (the last one), thus preventing memory
751 leaks from old references while allowing the objects from the last
751 leaks from old references while allowing the objects from the last
752 execution to be accessible.
752 execution to be accessible.
753
753
754 Note: we can not allow the actual FakeModule instances to be deleted,
754 Note: we can not allow the actual FakeModule instances to be deleted,
755 because of how Python tears down modules (it hard-sets all their
755 because of how Python tears down modules (it hard-sets all their
756 references to None without regard for reference counts). This method
756 references to None without regard for reference counts). This method
757 must therefore make a *copy* of the given namespace, to allow the
757 must therefore make a *copy* of the given namespace, to allow the
758 original module's __dict__ to be cleared and reused.
758 original module's __dict__ to be cleared and reused.
759
759
760
760
761 Parameters
761 Parameters
762 ----------
762 ----------
763 ns : a namespace (a dict, typically)
763 ns : a namespace (a dict, typically)
764
764
765 fname : str
765 fname : str
766 Filename associated with the namespace.
766 Filename associated with the namespace.
767
767
768 Examples
768 Examples
769 --------
769 --------
770
770
771 In [10]: import IPython
771 In [10]: import IPython
772
772
773 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
773 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
774
774
775 In [12]: IPython.__file__ in _ip._main_ns_cache
775 In [12]: IPython.__file__ in _ip._main_ns_cache
776 Out[12]: True
776 Out[12]: True
777 """
777 """
778 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
778 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
779
779
780 def clear_main_mod_cache(self):
780 def clear_main_mod_cache(self):
781 """Clear the cache of main modules.
781 """Clear the cache of main modules.
782
782
783 Mainly for use by utilities like %reset.
783 Mainly for use by utilities like %reset.
784
784
785 Examples
785 Examples
786 --------
786 --------
787
787
788 In [15]: import IPython
788 In [15]: import IPython
789
789
790 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
790 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
791
791
792 In [17]: len(_ip._main_ns_cache) > 0
792 In [17]: len(_ip._main_ns_cache) > 0
793 Out[17]: True
793 Out[17]: True
794
794
795 In [18]: _ip.clear_main_mod_cache()
795 In [18]: _ip.clear_main_mod_cache()
796
796
797 In [19]: len(_ip._main_ns_cache) == 0
797 In [19]: len(_ip._main_ns_cache) == 0
798 Out[19]: True
798 Out[19]: True
799 """
799 """
800 self._main_ns_cache.clear()
800 self._main_ns_cache.clear()
801
801
802 #-------------------------------------------------------------------------
802 #-------------------------------------------------------------------------
803 # Things related to debugging
803 # Things related to debugging
804 #-------------------------------------------------------------------------
804 #-------------------------------------------------------------------------
805
805
806 def init_pdb(self):
806 def init_pdb(self):
807 # Set calling of pdb on exceptions
807 # Set calling of pdb on exceptions
808 # self.call_pdb is a property
808 # self.call_pdb is a property
809 self.call_pdb = self.pdb
809 self.call_pdb = self.pdb
810
810
811 def _get_call_pdb(self):
811 def _get_call_pdb(self):
812 return self._call_pdb
812 return self._call_pdb
813
813
814 def _set_call_pdb(self,val):
814 def _set_call_pdb(self,val):
815
815
816 if val not in (0,1,False,True):
816 if val not in (0,1,False,True):
817 raise ValueError,'new call_pdb value must be boolean'
817 raise ValueError,'new call_pdb value must be boolean'
818
818
819 # store value in instance
819 # store value in instance
820 self._call_pdb = val
820 self._call_pdb = val
821
821
822 # notify the actual exception handlers
822 # notify the actual exception handlers
823 self.InteractiveTB.call_pdb = val
823 self.InteractiveTB.call_pdb = val
824
824
825 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
825 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
826 'Control auto-activation of pdb at exceptions')
826 'Control auto-activation of pdb at exceptions')
827
827
828 def debugger(self,force=False):
828 def debugger(self,force=False):
829 """Call the pydb/pdb debugger.
829 """Call the pydb/pdb debugger.
830
830
831 Keywords:
831 Keywords:
832
832
833 - force(False): by default, this routine checks the instance call_pdb
833 - force(False): by default, this routine checks the instance call_pdb
834 flag and does not actually invoke the debugger if the flag is false.
834 flag and does not actually invoke the debugger if the flag is false.
835 The 'force' option forces the debugger to activate even if the flag
835 The 'force' option forces the debugger to activate even if the flag
836 is false.
836 is false.
837 """
837 """
838
838
839 if not (force or self.call_pdb):
839 if not (force or self.call_pdb):
840 return
840 return
841
841
842 if not hasattr(sys,'last_traceback'):
842 if not hasattr(sys,'last_traceback'):
843 error('No traceback has been produced, nothing to debug.')
843 error('No traceback has been produced, nothing to debug.')
844 return
844 return
845
845
846 # use pydb if available
846 # use pydb if available
847 if debugger.has_pydb:
847 if debugger.has_pydb:
848 from pydb import pm
848 from pydb import pm
849 else:
849 else:
850 # fallback to our internal debugger
850 # fallback to our internal debugger
851 pm = lambda : self.InteractiveTB.debugger(force=True)
851 pm = lambda : self.InteractiveTB.debugger(force=True)
852
852
853 with self.readline_no_record:
853 with self.readline_no_record:
854 pm()
854 pm()
855
855
856 #-------------------------------------------------------------------------
856 #-------------------------------------------------------------------------
857 # Things related to IPython's various namespaces
857 # Things related to IPython's various namespaces
858 #-------------------------------------------------------------------------
858 #-------------------------------------------------------------------------
859
859
860 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
860 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
861 # Create the namespace where the user will operate. user_ns is
861 # Create the namespace where the user will operate. user_ns is
862 # normally the only one used, and it is passed to the exec calls as
862 # normally the only one used, and it is passed to the exec calls as
863 # the locals argument. But we do carry a user_global_ns namespace
863 # the locals argument. But we do carry a user_global_ns namespace
864 # given as the exec 'globals' argument, This is useful in embedding
864 # given as the exec 'globals' argument, This is useful in embedding
865 # situations where the ipython shell opens in a context where the
865 # situations where the ipython shell opens in a context where the
866 # distinction between locals and globals is meaningful. For
866 # distinction between locals and globals is meaningful. For
867 # non-embedded contexts, it is just the same object as the user_ns dict.
867 # non-embedded contexts, it is just the same object as the user_ns dict.
868
868
869 # FIXME. For some strange reason, __builtins__ is showing up at user
869 # FIXME. For some strange reason, __builtins__ is showing up at user
870 # level as a dict instead of a module. This is a manual fix, but I
870 # level as a dict instead of a module. This is a manual fix, but I
871 # should really track down where the problem is coming from. Alex
871 # should really track down where the problem is coming from. Alex
872 # Schmolck reported this problem first.
872 # Schmolck reported this problem first.
873
873
874 # A useful post by Alex Martelli on this topic:
874 # A useful post by Alex Martelli on this topic:
875 # Re: inconsistent value from __builtins__
875 # Re: inconsistent value from __builtins__
876 # Von: Alex Martelli <aleaxit@yahoo.com>
876 # Von: Alex Martelli <aleaxit@yahoo.com>
877 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
877 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
878 # Gruppen: comp.lang.python
878 # Gruppen: comp.lang.python
879
879
880 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
880 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
881 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
881 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
882 # > <type 'dict'>
882 # > <type 'dict'>
883 # > >>> print type(__builtins__)
883 # > >>> print type(__builtins__)
884 # > <type 'module'>
884 # > <type 'module'>
885 # > Is this difference in return value intentional?
885 # > Is this difference in return value intentional?
886
886
887 # Well, it's documented that '__builtins__' can be either a dictionary
887 # Well, it's documented that '__builtins__' can be either a dictionary
888 # or a module, and it's been that way for a long time. Whether it's
888 # or a module, and it's been that way for a long time. Whether it's
889 # intentional (or sensible), I don't know. In any case, the idea is
889 # intentional (or sensible), I don't know. In any case, the idea is
890 # that if you need to access the built-in namespace directly, you
890 # that if you need to access the built-in namespace directly, you
891 # should start with "import __builtin__" (note, no 's') which will
891 # should start with "import __builtin__" (note, no 's') which will
892 # definitely give you a module. Yeah, it's somewhat confusing:-(.
892 # definitely give you a module. Yeah, it's somewhat confusing:-(.
893
893
894 # These routines return properly built dicts as needed by the rest of
894 # These routines return properly built dicts as needed by the rest of
895 # the code, and can also be used by extension writers to generate
895 # the code, and can also be used by extension writers to generate
896 # properly initialized namespaces.
896 # properly initialized namespaces.
897 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
897 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
898 user_global_ns)
898 user_global_ns)
899
899
900 # Assign namespaces
900 # Assign namespaces
901 # This is the namespace where all normal user variables live
901 # This is the namespace where all normal user variables live
902 self.user_ns = user_ns
902 self.user_ns = user_ns
903 self.user_global_ns = user_global_ns
903 self.user_global_ns = user_global_ns
904
904
905 # An auxiliary namespace that checks what parts of the user_ns were
905 # An auxiliary namespace that checks what parts of the user_ns were
906 # loaded at startup, so we can list later only variables defined in
906 # loaded at startup, so we can list later only variables defined in
907 # actual interactive use. Since it is always a subset of user_ns, it
907 # actual interactive use. Since it is always a subset of user_ns, it
908 # doesn't need to be separately tracked in the ns_table.
908 # doesn't need to be separately tracked in the ns_table.
909 self.user_ns_hidden = {}
909 self.user_ns_hidden = {}
910
910
911 # A namespace to keep track of internal data structures to prevent
911 # A namespace to keep track of internal data structures to prevent
912 # them from cluttering user-visible stuff. Will be updated later
912 # them from cluttering user-visible stuff. Will be updated later
913 self.internal_ns = {}
913 self.internal_ns = {}
914
914
915 # Now that FakeModule produces a real module, we've run into a nasty
915 # Now that FakeModule produces a real module, we've run into a nasty
916 # problem: after script execution (via %run), the module where the user
916 # problem: after script execution (via %run), the module where the user
917 # code ran is deleted. Now that this object is a true module (needed
917 # code ran is deleted. Now that this object is a true module (needed
918 # so docetst and other tools work correctly), the Python module
918 # so docetst and other tools work correctly), the Python module
919 # teardown mechanism runs over it, and sets to None every variable
919 # teardown mechanism runs over it, and sets to None every variable
920 # present in that module. Top-level references to objects from the
920 # present in that module. Top-level references to objects from the
921 # script survive, because the user_ns is updated with them. However,
921 # script survive, because the user_ns is updated with them. However,
922 # calling functions defined in the script that use other things from
922 # calling functions defined in the script that use other things from
923 # the script will fail, because the function's closure had references
923 # the script will fail, because the function's closure had references
924 # to the original objects, which are now all None. So we must protect
924 # to the original objects, which are now all None. So we must protect
925 # these modules from deletion by keeping a cache.
925 # these modules from deletion by keeping a cache.
926 #
926 #
927 # To avoid keeping stale modules around (we only need the one from the
927 # To avoid keeping stale modules around (we only need the one from the
928 # last run), we use a dict keyed with the full path to the script, so
928 # last run), we use a dict keyed with the full path to the script, so
929 # only the last version of the module is held in the cache. Note,
929 # only the last version of the module is held in the cache. Note,
930 # however, that we must cache the module *namespace contents* (their
930 # however, that we must cache the module *namespace contents* (their
931 # __dict__). Because if we try to cache the actual modules, old ones
931 # __dict__). Because if we try to cache the actual modules, old ones
932 # (uncached) could be destroyed while still holding references (such as
932 # (uncached) could be destroyed while still holding references (such as
933 # those held by GUI objects that tend to be long-lived)>
933 # those held by GUI objects that tend to be long-lived)>
934 #
934 #
935 # The %reset command will flush this cache. See the cache_main_mod()
935 # The %reset command will flush this cache. See the cache_main_mod()
936 # and clear_main_mod_cache() methods for details on use.
936 # and clear_main_mod_cache() methods for details on use.
937
937
938 # This is the cache used for 'main' namespaces
938 # This is the cache used for 'main' namespaces
939 self._main_ns_cache = {}
939 self._main_ns_cache = {}
940 # And this is the single instance of FakeModule whose __dict__ we keep
940 # And this is the single instance of FakeModule whose __dict__ we keep
941 # copying and clearing for reuse on each %run
941 # copying and clearing for reuse on each %run
942 self._user_main_module = FakeModule()
942 self._user_main_module = FakeModule()
943
943
944 # A table holding all the namespaces IPython deals with, so that
944 # A table holding all the namespaces IPython deals with, so that
945 # introspection facilities can search easily.
945 # introspection facilities can search easily.
946 self.ns_table = {'user':user_ns,
946 self.ns_table = {'user':user_ns,
947 'user_global':user_global_ns,
947 'user_global':user_global_ns,
948 'internal':self.internal_ns,
948 'internal':self.internal_ns,
949 'builtin':builtin_mod.__dict__
949 'builtin':builtin_mod.__dict__
950 }
950 }
951
951
952 # Similarly, track all namespaces where references can be held and that
952 # Similarly, track all namespaces where references can be held and that
953 # we can safely clear (so it can NOT include builtin). This one can be
953 # we can safely clear (so it can NOT include builtin). This one can be
954 # a simple list. Note that the main execution namespaces, user_ns and
954 # a simple list. Note that the main execution namespaces, user_ns and
955 # user_global_ns, can NOT be listed here, as clearing them blindly
955 # user_global_ns, can NOT be listed here, as clearing them blindly
956 # causes errors in object __del__ methods. Instead, the reset() method
956 # causes errors in object __del__ methods. Instead, the reset() method
957 # clears them manually and carefully.
957 # clears them manually and carefully.
958 self.ns_refs_table = [ self.user_ns_hidden,
958 self.ns_refs_table = [ self.user_ns_hidden,
959 self.internal_ns, self._main_ns_cache ]
959 self.internal_ns, self._main_ns_cache ]
960
960
961 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
961 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
962 """Return a valid local and global user interactive namespaces.
962 """Return a valid local and global user interactive namespaces.
963
963
964 This builds a dict with the minimal information needed to operate as a
964 This builds a dict with the minimal information needed to operate as a
965 valid IPython user namespace, which you can pass to the various
965 valid IPython user namespace, which you can pass to the various
966 embedding classes in ipython. The default implementation returns the
966 embedding classes in ipython. The default implementation returns the
967 same dict for both the locals and the globals to allow functions to
967 same dict for both the locals and the globals to allow functions to
968 refer to variables in the namespace. Customized implementations can
968 refer to variables in the namespace. Customized implementations can
969 return different dicts. The locals dictionary can actually be anything
969 return different dicts. The locals dictionary can actually be anything
970 following the basic mapping protocol of a dict, but the globals dict
970 following the basic mapping protocol of a dict, but the globals dict
971 must be a true dict, not even a subclass. It is recommended that any
971 must be a true dict, not even a subclass. It is recommended that any
972 custom object for the locals namespace synchronize with the globals
972 custom object for the locals namespace synchronize with the globals
973 dict somehow.
973 dict somehow.
974
974
975 Raises TypeError if the provided globals namespace is not a true dict.
975 Raises TypeError if the provided globals namespace is not a true dict.
976
976
977 Parameters
977 Parameters
978 ----------
978 ----------
979 user_ns : dict-like, optional
979 user_ns : dict-like, optional
980 The current user namespace. The items in this namespace should
980 The current user namespace. The items in this namespace should
981 be included in the output. If None, an appropriate blank
981 be included in the output. If None, an appropriate blank
982 namespace should be created.
982 namespace should be created.
983 user_global_ns : dict, optional
983 user_global_ns : dict, optional
984 The current user global namespace. The items in this namespace
984 The current user global namespace. The items in this namespace
985 should be included in the output. If None, an appropriate
985 should be included in the output. If None, an appropriate
986 blank namespace should be created.
986 blank namespace should be created.
987
987
988 Returns
988 Returns
989 -------
989 -------
990 A pair of dictionary-like object to be used as the local namespace
990 A pair of dictionary-like object to be used as the local namespace
991 of the interpreter and a dict to be used as the global namespace.
991 of the interpreter and a dict to be used as the global namespace.
992 """
992 """
993
993
994
994
995 # We must ensure that __builtin__ (without the final 's') is always
995 # We must ensure that __builtin__ (without the final 's') is always
996 # available and pointing to the __builtin__ *module*. For more details:
996 # available and pointing to the __builtin__ *module*. For more details:
997 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
997 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
998
998
999 if user_ns is None:
999 if user_ns is None:
1000 # Set __name__ to __main__ to better match the behavior of the
1000 # Set __name__ to __main__ to better match the behavior of the
1001 # normal interpreter.
1001 # normal interpreter.
1002 user_ns = {'__name__' :'__main__',
1002 user_ns = {'__name__' :'__main__',
1003 py3compat.builtin_mod_name: builtin_mod,
1003 py3compat.builtin_mod_name: builtin_mod,
1004 '__builtins__' : builtin_mod,
1004 '__builtins__' : builtin_mod,
1005 }
1005 }
1006 else:
1006 else:
1007 user_ns.setdefault('__name__','__main__')
1007 user_ns.setdefault('__name__','__main__')
1008 user_ns.setdefault(py3compat.builtin_mod_name,builtin_mod)
1008 user_ns.setdefault(py3compat.builtin_mod_name,builtin_mod)
1009 user_ns.setdefault('__builtins__',builtin_mod)
1009 user_ns.setdefault('__builtins__',builtin_mod)
1010
1010
1011 if user_global_ns is None:
1011 if user_global_ns is None:
1012 user_global_ns = user_ns
1012 user_global_ns = user_ns
1013 if type(user_global_ns) is not dict:
1013 if type(user_global_ns) is not dict:
1014 raise TypeError("user_global_ns must be a true dict; got %r"
1014 raise TypeError("user_global_ns must be a true dict; got %r"
1015 % type(user_global_ns))
1015 % type(user_global_ns))
1016
1016
1017 return user_ns, user_global_ns
1017 return user_ns, user_global_ns
1018
1018
1019 def init_sys_modules(self):
1019 def init_sys_modules(self):
1020 # We need to insert into sys.modules something that looks like a
1020 # We need to insert into sys.modules something that looks like a
1021 # module but which accesses the IPython namespace, for shelve and
1021 # module but which accesses the IPython namespace, for shelve and
1022 # pickle to work interactively. Normally they rely on getting
1022 # pickle to work interactively. Normally they rely on getting
1023 # everything out of __main__, but for embedding purposes each IPython
1023 # everything out of __main__, but for embedding purposes each IPython
1024 # instance has its own private namespace, so we can't go shoving
1024 # instance has its own private namespace, so we can't go shoving
1025 # everything into __main__.
1025 # everything into __main__.
1026
1026
1027 # note, however, that we should only do this for non-embedded
1027 # note, however, that we should only do this for non-embedded
1028 # ipythons, which really mimic the __main__.__dict__ with their own
1028 # ipythons, which really mimic the __main__.__dict__ with their own
1029 # namespace. Embedded instances, on the other hand, should not do
1029 # namespace. Embedded instances, on the other hand, should not do
1030 # this because they need to manage the user local/global namespaces
1030 # this because they need to manage the user local/global namespaces
1031 # only, but they live within a 'normal' __main__ (meaning, they
1031 # only, but they live within a 'normal' __main__ (meaning, they
1032 # shouldn't overtake the execution environment of the script they're
1032 # shouldn't overtake the execution environment of the script they're
1033 # embedded in).
1033 # embedded in).
1034
1034
1035 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1035 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1036
1036
1037 try:
1037 try:
1038 main_name = self.user_ns['__name__']
1038 main_name = self.user_ns['__name__']
1039 except KeyError:
1039 except KeyError:
1040 raise KeyError('user_ns dictionary MUST have a "__name__" key')
1040 raise KeyError('user_ns dictionary MUST have a "__name__" key')
1041 else:
1041 else:
1042 sys.modules[main_name] = FakeModule(self.user_ns)
1042 sys.modules[main_name] = FakeModule(self.user_ns)
1043
1043
1044 def init_user_ns(self):
1044 def init_user_ns(self):
1045 """Initialize all user-visible namespaces to their minimum defaults.
1045 """Initialize all user-visible namespaces to their minimum defaults.
1046
1046
1047 Certain history lists are also initialized here, as they effectively
1047 Certain history lists are also initialized here, as they effectively
1048 act as user namespaces.
1048 act as user namespaces.
1049
1049
1050 Notes
1050 Notes
1051 -----
1051 -----
1052 All data structures here are only filled in, they are NOT reset by this
1052 All data structures here are only filled in, they are NOT reset by this
1053 method. If they were not empty before, data will simply be added to
1053 method. If they were not empty before, data will simply be added to
1054 therm.
1054 therm.
1055 """
1055 """
1056 # This function works in two parts: first we put a few things in
1056 # This function works in two parts: first we put a few things in
1057 # user_ns, and we sync that contents into user_ns_hidden so that these
1057 # user_ns, and we sync that contents into user_ns_hidden so that these
1058 # initial variables aren't shown by %who. After the sync, we add the
1058 # initial variables aren't shown by %who. After the sync, we add the
1059 # rest of what we *do* want the user to see with %who even on a new
1059 # rest of what we *do* want the user to see with %who even on a new
1060 # session (probably nothing, so theye really only see their own stuff)
1060 # session (probably nothing, so theye really only see their own stuff)
1061
1061
1062 # The user dict must *always* have a __builtin__ reference to the
1062 # The user dict must *always* have a __builtin__ reference to the
1063 # Python standard __builtin__ namespace, which must be imported.
1063 # Python standard __builtin__ namespace, which must be imported.
1064 # This is so that certain operations in prompt evaluation can be
1064 # This is so that certain operations in prompt evaluation can be
1065 # reliably executed with builtins. Note that we can NOT use
1065 # reliably executed with builtins. Note that we can NOT use
1066 # __builtins__ (note the 's'), because that can either be a dict or a
1066 # __builtins__ (note the 's'), because that can either be a dict or a
1067 # module, and can even mutate at runtime, depending on the context
1067 # module, and can even mutate at runtime, depending on the context
1068 # (Python makes no guarantees on it). In contrast, __builtin__ is
1068 # (Python makes no guarantees on it). In contrast, __builtin__ is
1069 # always a module object, though it must be explicitly imported.
1069 # always a module object, though it must be explicitly imported.
1070
1070
1071 # For more details:
1071 # For more details:
1072 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1072 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1073 ns = dict(__builtin__ = builtin_mod)
1073 ns = dict(__builtin__ = builtin_mod)
1074
1074
1075 # Put 'help' in the user namespace
1075 # Put 'help' in the user namespace
1076 try:
1076 try:
1077 from site import _Helper
1077 from site import _Helper
1078 ns['help'] = _Helper()
1078 ns['help'] = _Helper()
1079 except ImportError:
1079 except ImportError:
1080 warn('help() not available - check site.py')
1080 warn('help() not available - check site.py')
1081
1081
1082 # make global variables for user access to the histories
1082 # make global variables for user access to the histories
1083 ns['_ih'] = self.history_manager.input_hist_parsed
1083 ns['_ih'] = self.history_manager.input_hist_parsed
1084 ns['_oh'] = self.history_manager.output_hist
1084 ns['_oh'] = self.history_manager.output_hist
1085 ns['_dh'] = self.history_manager.dir_hist
1085 ns['_dh'] = self.history_manager.dir_hist
1086
1086
1087 ns['_sh'] = shadowns
1087 ns['_sh'] = shadowns
1088
1088
1089 # user aliases to input and output histories. These shouldn't show up
1089 # user aliases to input and output histories. These shouldn't show up
1090 # in %who, as they can have very large reprs.
1090 # in %who, as they can have very large reprs.
1091 ns['In'] = self.history_manager.input_hist_parsed
1091 ns['In'] = self.history_manager.input_hist_parsed
1092 ns['Out'] = self.history_manager.output_hist
1092 ns['Out'] = self.history_manager.output_hist
1093
1093
1094 # Store myself as the public api!!!
1094 # Store myself as the public api!!!
1095 ns['get_ipython'] = self.get_ipython
1095 ns['get_ipython'] = self.get_ipython
1096
1096
1097 ns['exit'] = self.exiter
1097 ns['exit'] = self.exiter
1098 ns['quit'] = self.exiter
1098 ns['quit'] = self.exiter
1099
1099
1100 # Sync what we've added so far to user_ns_hidden so these aren't seen
1100 # Sync what we've added so far to user_ns_hidden so these aren't seen
1101 # by %who
1101 # by %who
1102 self.user_ns_hidden.update(ns)
1102 self.user_ns_hidden.update(ns)
1103
1103
1104 # Anything put into ns now would show up in %who. Think twice before
1104 # Anything put into ns now would show up in %who. Think twice before
1105 # putting anything here, as we really want %who to show the user their
1105 # putting anything here, as we really want %who to show the user their
1106 # stuff, not our variables.
1106 # stuff, not our variables.
1107
1107
1108 # Finally, update the real user's namespace
1108 # Finally, update the real user's namespace
1109 self.user_ns.update(ns)
1109 self.user_ns.update(ns)
1110
1110
1111 def reset(self, new_session=True):
1111 def reset(self, new_session=True):
1112 """Clear all internal namespaces, and attempt to release references to
1112 """Clear all internal namespaces, and attempt to release references to
1113 user objects.
1113 user objects.
1114
1114
1115 If new_session is True, a new history session will be opened.
1115 If new_session is True, a new history session will be opened.
1116 """
1116 """
1117 # Clear histories
1117 # Clear histories
1118 self.history_manager.reset(new_session)
1118 self.history_manager.reset(new_session)
1119 # Reset counter used to index all histories
1119 # Reset counter used to index all histories
1120 if new_session:
1120 if new_session:
1121 self.execution_count = 1
1121 self.execution_count = 1
1122
1122
1123 # Flush cached output items
1123 # Flush cached output items
1124 if self.displayhook.do_full_cache:
1124 if self.displayhook.do_full_cache:
1125 self.displayhook.flush()
1125 self.displayhook.flush()
1126
1126
1127 # Restore the user namespaces to minimal usability
1127 # Restore the user namespaces to minimal usability
1128 for ns in self.ns_refs_table:
1128 for ns in self.ns_refs_table:
1129 ns.clear()
1129 ns.clear()
1130
1130
1131 # The main execution namespaces must be cleared very carefully,
1131 # The main execution namespaces must be cleared very carefully,
1132 # skipping the deletion of the builtin-related keys, because doing so
1132 # skipping the deletion of the builtin-related keys, because doing so
1133 # would cause errors in many object's __del__ methods.
1133 # would cause errors in many object's __del__ methods.
1134 for ns in [self.user_ns, self.user_global_ns]:
1134 for ns in [self.user_ns, self.user_global_ns]:
1135 drop_keys = set(ns.keys())
1135 drop_keys = set(ns.keys())
1136 drop_keys.discard('__builtin__')
1136 drop_keys.discard('__builtin__')
1137 drop_keys.discard('__builtins__')
1137 drop_keys.discard('__builtins__')
1138 for k in drop_keys:
1138 for k in drop_keys:
1139 del ns[k]
1139 del ns[k]
1140
1140
1141 # Restore the user namespaces to minimal usability
1141 # Restore the user namespaces to minimal usability
1142 self.init_user_ns()
1142 self.init_user_ns()
1143
1143
1144 # Restore the default and user aliases
1144 # Restore the default and user aliases
1145 self.alias_manager.clear_aliases()
1145 self.alias_manager.clear_aliases()
1146 self.alias_manager.init_aliases()
1146 self.alias_manager.init_aliases()
1147
1147
1148 # Flush the private list of module references kept for script
1148 # Flush the private list of module references kept for script
1149 # execution protection
1149 # execution protection
1150 self.clear_main_mod_cache()
1150 self.clear_main_mod_cache()
1151
1151
1152 # Clear out the namespace from the last %run
1152 # Clear out the namespace from the last %run
1153 self.new_main_mod()
1153 self.new_main_mod()
1154
1154
1155 def del_var(self, varname, by_name=False):
1155 def del_var(self, varname, by_name=False):
1156 """Delete a variable from the various namespaces, so that, as
1156 """Delete a variable from the various namespaces, so that, as
1157 far as possible, we're not keeping any hidden references to it.
1157 far as possible, we're not keeping any hidden references to it.
1158
1158
1159 Parameters
1159 Parameters
1160 ----------
1160 ----------
1161 varname : str
1161 varname : str
1162 The name of the variable to delete.
1162 The name of the variable to delete.
1163 by_name : bool
1163 by_name : bool
1164 If True, delete variables with the given name in each
1164 If True, delete variables with the given name in each
1165 namespace. If False (default), find the variable in the user
1165 namespace. If False (default), find the variable in the user
1166 namespace, and delete references to it.
1166 namespace, and delete references to it.
1167 """
1167 """
1168 if varname in ('__builtin__', '__builtins__'):
1168 if varname in ('__builtin__', '__builtins__'):
1169 raise ValueError("Refusing to delete %s" % varname)
1169 raise ValueError("Refusing to delete %s" % varname)
1170 ns_refs = self.ns_refs_table + [self.user_ns,
1170 ns_refs = self.ns_refs_table + [self.user_ns,
1171 self.user_global_ns, self._user_main_module.__dict__] +\
1171 self.user_global_ns, self._user_main_module.__dict__] +\
1172 self._main_ns_cache.values()
1172 self._main_ns_cache.values()
1173
1173
1174 if by_name: # Delete by name
1174 if by_name: # Delete by name
1175 for ns in ns_refs:
1175 for ns in ns_refs:
1176 try:
1176 try:
1177 del ns[varname]
1177 del ns[varname]
1178 except KeyError:
1178 except KeyError:
1179 pass
1179 pass
1180 else: # Delete by object
1180 else: # Delete by object
1181 try:
1181 try:
1182 obj = self.user_ns[varname]
1182 obj = self.user_ns[varname]
1183 except KeyError:
1183 except KeyError:
1184 raise NameError("name '%s' is not defined" % varname)
1184 raise NameError("name '%s' is not defined" % varname)
1185 # Also check in output history
1185 # Also check in output history
1186 ns_refs.append(self.history_manager.output_hist)
1186 ns_refs.append(self.history_manager.output_hist)
1187 for ns in ns_refs:
1187 for ns in ns_refs:
1188 to_delete = [n for n, o in ns.iteritems() if o is obj]
1188 to_delete = [n for n, o in ns.iteritems() if o is obj]
1189 for name in to_delete:
1189 for name in to_delete:
1190 del ns[name]
1190 del ns[name]
1191
1191
1192 # displayhook keeps extra references, but not in a dictionary
1192 # displayhook keeps extra references, but not in a dictionary
1193 for name in ('_', '__', '___'):
1193 for name in ('_', '__', '___'):
1194 if getattr(self.displayhook, name) is obj:
1194 if getattr(self.displayhook, name) is obj:
1195 setattr(self.displayhook, name, None)
1195 setattr(self.displayhook, name, None)
1196
1196
1197 def reset_selective(self, regex=None):
1197 def reset_selective(self, regex=None):
1198 """Clear selective variables from internal namespaces based on a
1198 """Clear selective variables from internal namespaces based on a
1199 specified regular expression.
1199 specified regular expression.
1200
1200
1201 Parameters
1201 Parameters
1202 ----------
1202 ----------
1203 regex : string or compiled pattern, optional
1203 regex : string or compiled pattern, optional
1204 A regular expression pattern that will be used in searching
1204 A regular expression pattern that will be used in searching
1205 variable names in the users namespaces.
1205 variable names in the users namespaces.
1206 """
1206 """
1207 if regex is not None:
1207 if regex is not None:
1208 try:
1208 try:
1209 m = re.compile(regex)
1209 m = re.compile(regex)
1210 except TypeError:
1210 except TypeError:
1211 raise TypeError('regex must be a string or compiled pattern')
1211 raise TypeError('regex must be a string or compiled pattern')
1212 # Search for keys in each namespace that match the given regex
1212 # Search for keys in each namespace that match the given regex
1213 # If a match is found, delete the key/value pair.
1213 # If a match is found, delete the key/value pair.
1214 for ns in self.ns_refs_table:
1214 for ns in self.ns_refs_table:
1215 for var in ns:
1215 for var in ns:
1216 if m.search(var):
1216 if m.search(var):
1217 del ns[var]
1217 del ns[var]
1218
1218
1219 def push(self, variables, interactive=True):
1219 def push(self, variables, interactive=True):
1220 """Inject a group of variables into the IPython user namespace.
1220 """Inject a group of variables into the IPython user namespace.
1221
1221
1222 Parameters
1222 Parameters
1223 ----------
1223 ----------
1224 variables : dict, str or list/tuple of str
1224 variables : dict, str or list/tuple of str
1225 The variables to inject into the user's namespace. If a dict, a
1225 The variables to inject into the user's namespace. If a dict, a
1226 simple update is done. If a str, the string is assumed to have
1226 simple update is done. If a str, the string is assumed to have
1227 variable names separated by spaces. A list/tuple of str can also
1227 variable names separated by spaces. A list/tuple of str can also
1228 be used to give the variable names. If just the variable names are
1228 be used to give the variable names. If just the variable names are
1229 give (list/tuple/str) then the variable values looked up in the
1229 give (list/tuple/str) then the variable values looked up in the
1230 callers frame.
1230 callers frame.
1231 interactive : bool
1231 interactive : bool
1232 If True (default), the variables will be listed with the ``who``
1232 If True (default), the variables will be listed with the ``who``
1233 magic.
1233 magic.
1234 """
1234 """
1235 vdict = None
1235 vdict = None
1236
1236
1237 # We need a dict of name/value pairs to do namespace updates.
1237 # We need a dict of name/value pairs to do namespace updates.
1238 if isinstance(variables, dict):
1238 if isinstance(variables, dict):
1239 vdict = variables
1239 vdict = variables
1240 elif isinstance(variables, (basestring, list, tuple)):
1240 elif isinstance(variables, (basestring, list, tuple)):
1241 if isinstance(variables, basestring):
1241 if isinstance(variables, basestring):
1242 vlist = variables.split()
1242 vlist = variables.split()
1243 else:
1243 else:
1244 vlist = variables
1244 vlist = variables
1245 vdict = {}
1245 vdict = {}
1246 cf = sys._getframe(1)
1246 cf = sys._getframe(1)
1247 for name in vlist:
1247 for name in vlist:
1248 try:
1248 try:
1249 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1249 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1250 except:
1250 except:
1251 print ('Could not get variable %s from %s' %
1251 print ('Could not get variable %s from %s' %
1252 (name,cf.f_code.co_name))
1252 (name,cf.f_code.co_name))
1253 else:
1253 else:
1254 raise ValueError('variables must be a dict/str/list/tuple')
1254 raise ValueError('variables must be a dict/str/list/tuple')
1255
1255
1256 # Propagate variables to user namespace
1256 # Propagate variables to user namespace
1257 self.user_ns.update(vdict)
1257 self.user_ns.update(vdict)
1258
1258
1259 # And configure interactive visibility
1259 # And configure interactive visibility
1260 config_ns = self.user_ns_hidden
1260 config_ns = self.user_ns_hidden
1261 if interactive:
1261 if interactive:
1262 for name, val in vdict.iteritems():
1262 for name, val in vdict.iteritems():
1263 config_ns.pop(name, None)
1263 config_ns.pop(name, None)
1264 else:
1264 else:
1265 for name,val in vdict.iteritems():
1265 for name,val in vdict.iteritems():
1266 config_ns[name] = val
1266 config_ns[name] = val
1267
1267
1268 #-------------------------------------------------------------------------
1268 #-------------------------------------------------------------------------
1269 # Things related to object introspection
1269 # Things related to object introspection
1270 #-------------------------------------------------------------------------
1270 #-------------------------------------------------------------------------
1271
1271
1272 def _ofind(self, oname, namespaces=None):
1272 def _ofind(self, oname, namespaces=None):
1273 """Find an object in the available namespaces.
1273 """Find an object in the available namespaces.
1274
1274
1275 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1275 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1276
1276
1277 Has special code to detect magic functions.
1277 Has special code to detect magic functions.
1278 """
1278 """
1279 oname = oname.strip()
1279 oname = oname.strip()
1280 #print '1- oname: <%r>' % oname # dbg
1280 #print '1- oname: <%r>' % oname # dbg
1281 if not py3compat.isidentifier(oname.lstrip(ESC_MAGIC), dotted=True):
1281 if not py3compat.isidentifier(oname.lstrip(ESC_MAGIC), dotted=True):
1282 return dict(found=False)
1282 return dict(found=False)
1283
1283
1284 alias_ns = None
1284 alias_ns = None
1285 if namespaces is None:
1285 if namespaces is None:
1286 # Namespaces to search in:
1286 # Namespaces to search in:
1287 # Put them in a list. The order is important so that we
1287 # Put them in a list. The order is important so that we
1288 # find things in the same order that Python finds them.
1288 # find things in the same order that Python finds them.
1289 namespaces = [ ('Interactive', self.user_ns),
1289 namespaces = [ ('Interactive', self.user_ns),
1290 ('IPython internal', self.internal_ns),
1290 ('IPython internal', self.internal_ns),
1291 ('Python builtin', builtin_mod.__dict__),
1291 ('Python builtin', builtin_mod.__dict__),
1292 ('Alias', self.alias_manager.alias_table),
1292 ('Alias', self.alias_manager.alias_table),
1293 ]
1293 ]
1294 alias_ns = self.alias_manager.alias_table
1294 alias_ns = self.alias_manager.alias_table
1295
1295
1296 # initialize results to 'null'
1296 # initialize results to 'null'
1297 found = False; obj = None; ospace = None; ds = None;
1297 found = False; obj = None; ospace = None; ds = None;
1298 ismagic = False; isalias = False; parent = None
1298 ismagic = False; isalias = False; parent = None
1299
1299
1300 # We need to special-case 'print', which as of python2.6 registers as a
1300 # We need to special-case 'print', which as of python2.6 registers as a
1301 # function but should only be treated as one if print_function was
1301 # function but should only be treated as one if print_function was
1302 # loaded with a future import. In this case, just bail.
1302 # loaded with a future import. In this case, just bail.
1303 if (oname == 'print' and not py3compat.PY3 and not \
1303 if (oname == 'print' and not py3compat.PY3 and not \
1304 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1304 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1305 return {'found':found, 'obj':obj, 'namespace':ospace,
1305 return {'found':found, 'obj':obj, 'namespace':ospace,
1306 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1306 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1307
1307
1308 # Look for the given name by splitting it in parts. If the head is
1308 # Look for the given name by splitting it in parts. If the head is
1309 # found, then we look for all the remaining parts as members, and only
1309 # found, then we look for all the remaining parts as members, and only
1310 # declare success if we can find them all.
1310 # declare success if we can find them all.
1311 oname_parts = oname.split('.')
1311 oname_parts = oname.split('.')
1312 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1312 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1313 for nsname,ns in namespaces:
1313 for nsname,ns in namespaces:
1314 try:
1314 try:
1315 obj = ns[oname_head]
1315 obj = ns[oname_head]
1316 except KeyError:
1316 except KeyError:
1317 continue
1317 continue
1318 else:
1318 else:
1319 #print 'oname_rest:', oname_rest # dbg
1319 #print 'oname_rest:', oname_rest # dbg
1320 for part in oname_rest:
1320 for part in oname_rest:
1321 try:
1321 try:
1322 parent = obj
1322 parent = obj
1323 obj = getattr(obj,part)
1323 obj = getattr(obj,part)
1324 except:
1324 except:
1325 # Blanket except b/c some badly implemented objects
1325 # Blanket except b/c some badly implemented objects
1326 # allow __getattr__ to raise exceptions other than
1326 # allow __getattr__ to raise exceptions other than
1327 # AttributeError, which then crashes IPython.
1327 # AttributeError, which then crashes IPython.
1328 break
1328 break
1329 else:
1329 else:
1330 # If we finish the for loop (no break), we got all members
1330 # If we finish the for loop (no break), we got all members
1331 found = True
1331 found = True
1332 ospace = nsname
1332 ospace = nsname
1333 if ns == alias_ns:
1333 if ns == alias_ns:
1334 isalias = True
1334 isalias = True
1335 break # namespace loop
1335 break # namespace loop
1336
1336
1337 # Try to see if it's magic
1337 # Try to see if it's magic
1338 if not found:
1338 if not found:
1339 if oname.startswith(ESC_MAGIC):
1339 if oname.startswith(ESC_MAGIC):
1340 oname = oname[1:]
1340 oname = oname[1:]
1341 obj = getattr(self,'magic_'+oname,None)
1341 obj = getattr(self,'magic_'+oname,None)
1342 if obj is not None:
1342 if obj is not None:
1343 found = True
1343 found = True
1344 ospace = 'IPython internal'
1344 ospace = 'IPython internal'
1345 ismagic = True
1345 ismagic = True
1346
1346
1347 # Last try: special-case some literals like '', [], {}, etc:
1347 # Last try: special-case some literals like '', [], {}, etc:
1348 if not found and oname_head in ["''",'""','[]','{}','()']:
1348 if not found and oname_head in ["''",'""','[]','{}','()']:
1349 obj = eval(oname_head)
1349 obj = eval(oname_head)
1350 found = True
1350 found = True
1351 ospace = 'Interactive'
1351 ospace = 'Interactive'
1352
1352
1353 return {'found':found, 'obj':obj, 'namespace':ospace,
1353 return {'found':found, 'obj':obj, 'namespace':ospace,
1354 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1354 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1355
1355
1356 def _ofind_property(self, oname, info):
1356 def _ofind_property(self, oname, info):
1357 """Second part of object finding, to look for property details."""
1357 """Second part of object finding, to look for property details."""
1358 if info.found:
1358 if info.found:
1359 # Get the docstring of the class property if it exists.
1359 # Get the docstring of the class property if it exists.
1360 path = oname.split('.')
1360 path = oname.split('.')
1361 root = '.'.join(path[:-1])
1361 root = '.'.join(path[:-1])
1362 if info.parent is not None:
1362 if info.parent is not None:
1363 try:
1363 try:
1364 target = getattr(info.parent, '__class__')
1364 target = getattr(info.parent, '__class__')
1365 # The object belongs to a class instance.
1365 # The object belongs to a class instance.
1366 try:
1366 try:
1367 target = getattr(target, path[-1])
1367 target = getattr(target, path[-1])
1368 # The class defines the object.
1368 # The class defines the object.
1369 if isinstance(target, property):
1369 if isinstance(target, property):
1370 oname = root + '.__class__.' + path[-1]
1370 oname = root + '.__class__.' + path[-1]
1371 info = Struct(self._ofind(oname))
1371 info = Struct(self._ofind(oname))
1372 except AttributeError: pass
1372 except AttributeError: pass
1373 except AttributeError: pass
1373 except AttributeError: pass
1374
1374
1375 # We return either the new info or the unmodified input if the object
1375 # We return either the new info or the unmodified input if the object
1376 # hadn't been found
1376 # hadn't been found
1377 return info
1377 return info
1378
1378
1379 def _object_find(self, oname, namespaces=None):
1379 def _object_find(self, oname, namespaces=None):
1380 """Find an object and return a struct with info about it."""
1380 """Find an object and return a struct with info about it."""
1381 inf = Struct(self._ofind(oname, namespaces))
1381 inf = Struct(self._ofind(oname, namespaces))
1382 return Struct(self._ofind_property(oname, inf))
1382 return Struct(self._ofind_property(oname, inf))
1383
1383
1384 def _inspect(self, meth, oname, namespaces=None, **kw):
1384 def _inspect(self, meth, oname, namespaces=None, **kw):
1385 """Generic interface to the inspector system.
1385 """Generic interface to the inspector system.
1386
1386
1387 This function is meant to be called by pdef, pdoc & friends."""
1387 This function is meant to be called by pdef, pdoc & friends."""
1388 info = self._object_find(oname)
1388 info = self._object_find(oname)
1389 if info.found:
1389 if info.found:
1390 pmethod = getattr(self.inspector, meth)
1390 pmethod = getattr(self.inspector, meth)
1391 formatter = format_screen if info.ismagic else None
1391 formatter = format_screen if info.ismagic else None
1392 if meth == 'pdoc':
1392 if meth == 'pdoc':
1393 pmethod(info.obj, oname, formatter)
1393 pmethod(info.obj, oname, formatter)
1394 elif meth == 'pinfo':
1394 elif meth == 'pinfo':
1395 pmethod(info.obj, oname, formatter, info, **kw)
1395 pmethod(info.obj, oname, formatter, info, **kw)
1396 else:
1396 else:
1397 pmethod(info.obj, oname)
1397 pmethod(info.obj, oname)
1398 else:
1398 else:
1399 print 'Object `%s` not found.' % oname
1399 print 'Object `%s` not found.' % oname
1400 return 'not found' # so callers can take other action
1400 return 'not found' # so callers can take other action
1401
1401
1402 def object_inspect(self, oname):
1402 def object_inspect(self, oname):
1403 with self.builtin_trap:
1403 with self.builtin_trap:
1404 info = self._object_find(oname)
1404 info = self._object_find(oname)
1405 if info.found:
1405 if info.found:
1406 return self.inspector.info(info.obj, oname, info=info)
1406 return self.inspector.info(info.obj, oname, info=info)
1407 else:
1407 else:
1408 return oinspect.object_info(name=oname, found=False)
1408 return oinspect.object_info(name=oname, found=False)
1409
1409
1410 #-------------------------------------------------------------------------
1410 #-------------------------------------------------------------------------
1411 # Things related to history management
1411 # Things related to history management
1412 #-------------------------------------------------------------------------
1412 #-------------------------------------------------------------------------
1413
1413
1414 def init_history(self):
1414 def init_history(self):
1415 """Sets up the command history, and starts regular autosaves."""
1415 """Sets up the command history, and starts regular autosaves."""
1416 self.history_manager = HistoryManager(shell=self, config=self.config)
1416 self.history_manager = HistoryManager(shell=self, config=self.config)
1417
1417
1418 #-------------------------------------------------------------------------
1418 #-------------------------------------------------------------------------
1419 # Things related to exception handling and tracebacks (not debugging)
1419 # Things related to exception handling and tracebacks (not debugging)
1420 #-------------------------------------------------------------------------
1420 #-------------------------------------------------------------------------
1421
1421
1422 def init_traceback_handlers(self, custom_exceptions):
1422 def init_traceback_handlers(self, custom_exceptions):
1423 # Syntax error handler.
1423 # Syntax error handler.
1424 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1424 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1425
1425
1426 # The interactive one is initialized with an offset, meaning we always
1426 # The interactive one is initialized with an offset, meaning we always
1427 # want to remove the topmost item in the traceback, which is our own
1427 # want to remove the topmost item in the traceback, which is our own
1428 # internal code. Valid modes: ['Plain','Context','Verbose']
1428 # internal code. Valid modes: ['Plain','Context','Verbose']
1429 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1429 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1430 color_scheme='NoColor',
1430 color_scheme='NoColor',
1431 tb_offset = 1,
1431 tb_offset = 1,
1432 check_cache=self.compile.check_cache)
1432 check_cache=self.compile.check_cache)
1433
1433
1434 # The instance will store a pointer to the system-wide exception hook,
1434 # The instance will store a pointer to the system-wide exception hook,
1435 # so that runtime code (such as magics) can access it. This is because
1435 # so that runtime code (such as magics) can access it. This is because
1436 # during the read-eval loop, it may get temporarily overwritten.
1436 # during the read-eval loop, it may get temporarily overwritten.
1437 self.sys_excepthook = sys.excepthook
1437 self.sys_excepthook = sys.excepthook
1438
1438
1439 # and add any custom exception handlers the user may have specified
1439 # and add any custom exception handlers the user may have specified
1440 self.set_custom_exc(*custom_exceptions)
1440 self.set_custom_exc(*custom_exceptions)
1441
1441
1442 # Set the exception mode
1442 # Set the exception mode
1443 self.InteractiveTB.set_mode(mode=self.xmode)
1443 self.InteractiveTB.set_mode(mode=self.xmode)
1444
1444
1445 def set_custom_exc(self, exc_tuple, handler):
1445 def set_custom_exc(self, exc_tuple, handler):
1446 """set_custom_exc(exc_tuple,handler)
1446 """set_custom_exc(exc_tuple,handler)
1447
1447
1448 Set a custom exception handler, which will be called if any of the
1448 Set a custom exception handler, which will be called if any of the
1449 exceptions in exc_tuple occur in the mainloop (specifically, in the
1449 exceptions in exc_tuple occur in the mainloop (specifically, in the
1450 run_code() method.
1450 run_code() method.
1451
1451
1452 Inputs:
1452 Inputs:
1453
1453
1454 - exc_tuple: a *tuple* of valid exceptions to call the defined
1454 - exc_tuple: a *tuple* of valid exceptions to call the defined
1455 handler for. It is very important that you use a tuple, and NOT A
1455 handler for. It is very important that you use a tuple, and NOT A
1456 LIST here, because of the way Python's except statement works. If
1456 LIST here, because of the way Python's except statement works. If
1457 you only want to trap a single exception, use a singleton tuple:
1457 you only want to trap a single exception, use a singleton tuple:
1458
1458
1459 exc_tuple == (MyCustomException,)
1459 exc_tuple == (MyCustomException,)
1460
1460
1461 - handler: this must be defined as a function with the following
1461 - handler: this must be defined as a function with the following
1462 basic interface::
1462 basic interface::
1463
1463
1464 def my_handler(self, etype, value, tb, tb_offset=None)
1464 def my_handler(self, etype, value, tb, tb_offset=None)
1465 ...
1465 ...
1466 # The return value must be
1466 # The return value must be
1467 return structured_traceback
1467 return structured_traceback
1468
1468
1469 This will be made into an instance method (via types.MethodType)
1469 This will be made into an instance method (via types.MethodType)
1470 of IPython itself, and it will be called if any of the exceptions
1470 of IPython itself, and it will be called if any of the exceptions
1471 listed in the exc_tuple are caught. If the handler is None, an
1471 listed in the exc_tuple are caught. If the handler is None, an
1472 internal basic one is used, which just prints basic info.
1472 internal basic one is used, which just prints basic info.
1473
1473
1474 WARNING: by putting in your own exception handler into IPython's main
1474 WARNING: by putting in your own exception handler into IPython's main
1475 execution loop, you run a very good chance of nasty crashes. This
1475 execution loop, you run a very good chance of nasty crashes. This
1476 facility should only be used if you really know what you are doing."""
1476 facility should only be used if you really know what you are doing."""
1477
1477
1478 assert type(exc_tuple)==type(()) , \
1478 assert type(exc_tuple)==type(()) , \
1479 "The custom exceptions must be given AS A TUPLE."
1479 "The custom exceptions must be given AS A TUPLE."
1480
1480
1481 def dummy_handler(self,etype,value,tb):
1481 def dummy_handler(self,etype,value,tb):
1482 print '*** Simple custom exception handler ***'
1482 print '*** Simple custom exception handler ***'
1483 print 'Exception type :',etype
1483 print 'Exception type :',etype
1484 print 'Exception value:',value
1484 print 'Exception value:',value
1485 print 'Traceback :',tb
1485 print 'Traceback :',tb
1486 #print 'Source code :','\n'.join(self.buffer)
1486 #print 'Source code :','\n'.join(self.buffer)
1487
1487
1488 if handler is None: handler = dummy_handler
1488 if handler is None: handler = dummy_handler
1489
1489
1490 self.CustomTB = types.MethodType(handler,self)
1490 self.CustomTB = types.MethodType(handler,self)
1491 self.custom_exceptions = exc_tuple
1491 self.custom_exceptions = exc_tuple
1492
1492
1493 def excepthook(self, etype, value, tb):
1493 def excepthook(self, etype, value, tb):
1494 """One more defense for GUI apps that call sys.excepthook.
1494 """One more defense for GUI apps that call sys.excepthook.
1495
1495
1496 GUI frameworks like wxPython trap exceptions and call
1496 GUI frameworks like wxPython trap exceptions and call
1497 sys.excepthook themselves. I guess this is a feature that
1497 sys.excepthook themselves. I guess this is a feature that
1498 enables them to keep running after exceptions that would
1498 enables them to keep running after exceptions that would
1499 otherwise kill their mainloop. This is a bother for IPython
1499 otherwise kill their mainloop. This is a bother for IPython
1500 which excepts to catch all of the program exceptions with a try:
1500 which excepts to catch all of the program exceptions with a try:
1501 except: statement.
1501 except: statement.
1502
1502
1503 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1503 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1504 any app directly invokes sys.excepthook, it will look to the user like
1504 any app directly invokes sys.excepthook, it will look to the user like
1505 IPython crashed. In order to work around this, we can disable the
1505 IPython crashed. In order to work around this, we can disable the
1506 CrashHandler and replace it with this excepthook instead, which prints a
1506 CrashHandler and replace it with this excepthook instead, which prints a
1507 regular traceback using our InteractiveTB. In this fashion, apps which
1507 regular traceback using our InteractiveTB. In this fashion, apps which
1508 call sys.excepthook will generate a regular-looking exception from
1508 call sys.excepthook will generate a regular-looking exception from
1509 IPython, and the CrashHandler will only be triggered by real IPython
1509 IPython, and the CrashHandler will only be triggered by real IPython
1510 crashes.
1510 crashes.
1511
1511
1512 This hook should be used sparingly, only in places which are not likely
1512 This hook should be used sparingly, only in places which are not likely
1513 to be true IPython errors.
1513 to be true IPython errors.
1514 """
1514 """
1515 self.showtraceback((etype,value,tb),tb_offset=0)
1515 self.showtraceback((etype,value,tb),tb_offset=0)
1516
1516
1517 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1517 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1518 exception_only=False):
1518 exception_only=False):
1519 """Display the exception that just occurred.
1519 """Display the exception that just occurred.
1520
1520
1521 If nothing is known about the exception, this is the method which
1521 If nothing is known about the exception, this is the method which
1522 should be used throughout the code for presenting user tracebacks,
1522 should be used throughout the code for presenting user tracebacks,
1523 rather than directly invoking the InteractiveTB object.
1523 rather than directly invoking the InteractiveTB object.
1524
1524
1525 A specific showsyntaxerror() also exists, but this method can take
1525 A specific showsyntaxerror() also exists, but this method can take
1526 care of calling it if needed, so unless you are explicitly catching a
1526 care of calling it if needed, so unless you are explicitly catching a
1527 SyntaxError exception, don't try to analyze the stack manually and
1527 SyntaxError exception, don't try to analyze the stack manually and
1528 simply call this method."""
1528 simply call this method."""
1529
1529
1530 try:
1530 try:
1531 if exc_tuple is None:
1531 if exc_tuple is None:
1532 etype, value, tb = sys.exc_info()
1532 etype, value, tb = sys.exc_info()
1533 else:
1533 else:
1534 etype, value, tb = exc_tuple
1534 etype, value, tb = exc_tuple
1535
1535
1536 if etype is None:
1536 if etype is None:
1537 if hasattr(sys, 'last_type'):
1537 if hasattr(sys, 'last_type'):
1538 etype, value, tb = sys.last_type, sys.last_value, \
1538 etype, value, tb = sys.last_type, sys.last_value, \
1539 sys.last_traceback
1539 sys.last_traceback
1540 else:
1540 else:
1541 self.write_err('No traceback available to show.\n')
1541 self.write_err('No traceback available to show.\n')
1542 return
1542 return
1543
1543
1544 if etype is SyntaxError:
1544 if etype is SyntaxError:
1545 # Though this won't be called by syntax errors in the input
1545 # Though this won't be called by syntax errors in the input
1546 # line, there may be SyntaxError cases with imported code.
1546 # line, there may be SyntaxError cases with imported code.
1547 self.showsyntaxerror(filename)
1547 self.showsyntaxerror(filename)
1548 elif etype is UsageError:
1548 elif etype is UsageError:
1549 print "UsageError:", value
1549 print "UsageError:", value
1550 else:
1550 else:
1551 # WARNING: these variables are somewhat deprecated and not
1551 # WARNING: these variables are somewhat deprecated and not
1552 # necessarily safe to use in a threaded environment, but tools
1552 # necessarily safe to use in a threaded environment, but tools
1553 # like pdb depend on their existence, so let's set them. If we
1553 # like pdb depend on their existence, so let's set them. If we
1554 # find problems in the field, we'll need to revisit their use.
1554 # find problems in the field, we'll need to revisit their use.
1555 sys.last_type = etype
1555 sys.last_type = etype
1556 sys.last_value = value
1556 sys.last_value = value
1557 sys.last_traceback = tb
1557 sys.last_traceback = tb
1558 if etype in self.custom_exceptions:
1558 if etype in self.custom_exceptions:
1559 # FIXME: Old custom traceback objects may just return a
1559 # FIXME: Old custom traceback objects may just return a
1560 # string, in that case we just put it into a list
1560 # string, in that case we just put it into a list
1561 stb = self.CustomTB(etype, value, tb, tb_offset)
1561 stb = self.CustomTB(etype, value, tb, tb_offset)
1562 if isinstance(ctb, basestring):
1562 if isinstance(ctb, basestring):
1563 stb = [stb]
1563 stb = [stb]
1564 else:
1564 else:
1565 if exception_only:
1565 if exception_only:
1566 stb = ['An exception has occurred, use %tb to see '
1566 stb = ['An exception has occurred, use %tb to see '
1567 'the full traceback.\n']
1567 'the full traceback.\n']
1568 stb.extend(self.InteractiveTB.get_exception_only(etype,
1568 stb.extend(self.InteractiveTB.get_exception_only(etype,
1569 value))
1569 value))
1570 else:
1570 else:
1571 stb = self.InteractiveTB.structured_traceback(etype,
1571 stb = self.InteractiveTB.structured_traceback(etype,
1572 value, tb, tb_offset=tb_offset)
1572 value, tb, tb_offset=tb_offset)
1573
1573
1574 if self.call_pdb:
1574 if self.call_pdb:
1575 # drop into debugger
1575 # drop into debugger
1576 self.debugger(force=True)
1576 self.debugger(force=True)
1577
1577
1578 # Actually show the traceback
1578 # Actually show the traceback
1579 self._showtraceback(etype, value, stb)
1579 self._showtraceback(etype, value, stb)
1580
1580
1581 except KeyboardInterrupt:
1581 except KeyboardInterrupt:
1582 self.write_err("\nKeyboardInterrupt\n")
1582 self.write_err("\nKeyboardInterrupt\n")
1583
1583
1584 def _showtraceback(self, etype, evalue, stb):
1584 def _showtraceback(self, etype, evalue, stb):
1585 """Actually show a traceback.
1585 """Actually show a traceback.
1586
1586
1587 Subclasses may override this method to put the traceback on a different
1587 Subclasses may override this method to put the traceback on a different
1588 place, like a side channel.
1588 place, like a side channel.
1589 """
1589 """
1590 print >> io.stdout, self.InteractiveTB.stb2text(stb)
1590 print >> io.stdout, self.InteractiveTB.stb2text(stb)
1591
1591
1592 def showsyntaxerror(self, filename=None):
1592 def showsyntaxerror(self, filename=None):
1593 """Display the syntax error that just occurred.
1593 """Display the syntax error that just occurred.
1594
1594
1595 This doesn't display a stack trace because there isn't one.
1595 This doesn't display a stack trace because there isn't one.
1596
1596
1597 If a filename is given, it is stuffed in the exception instead
1597 If a filename is given, it is stuffed in the exception instead
1598 of what was there before (because Python's parser always uses
1598 of what was there before (because Python's parser always uses
1599 "<string>" when reading from a string).
1599 "<string>" when reading from a string).
1600 """
1600 """
1601 etype, value, last_traceback = sys.exc_info()
1601 etype, value, last_traceback = sys.exc_info()
1602
1602
1603 # See note about these variables in showtraceback() above
1603 # See note about these variables in showtraceback() above
1604 sys.last_type = etype
1604 sys.last_type = etype
1605 sys.last_value = value
1605 sys.last_value = value
1606 sys.last_traceback = last_traceback
1606 sys.last_traceback = last_traceback
1607
1607
1608 if filename and etype is SyntaxError:
1608 if filename and etype is SyntaxError:
1609 # Work hard to stuff the correct filename in the exception
1609 # Work hard to stuff the correct filename in the exception
1610 try:
1610 try:
1611 msg, (dummy_filename, lineno, offset, line) = value
1611 msg, (dummy_filename, lineno, offset, line) = value
1612 except:
1612 except:
1613 # Not the format we expect; leave it alone
1613 # Not the format we expect; leave it alone
1614 pass
1614 pass
1615 else:
1615 else:
1616 # Stuff in the right filename
1616 # Stuff in the right filename
1617 try:
1617 try:
1618 # Assume SyntaxError is a class exception
1618 # Assume SyntaxError is a class exception
1619 value = SyntaxError(msg, (filename, lineno, offset, line))
1619 value = SyntaxError(msg, (filename, lineno, offset, line))
1620 except:
1620 except:
1621 # If that failed, assume SyntaxError is a string
1621 # If that failed, assume SyntaxError is a string
1622 value = msg, (filename, lineno, offset, line)
1622 value = msg, (filename, lineno, offset, line)
1623 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1623 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1624 self._showtraceback(etype, value, stb)
1624 self._showtraceback(etype, value, stb)
1625
1625
1626 # This is overridden in TerminalInteractiveShell to show a message about
1626 # This is overridden in TerminalInteractiveShell to show a message about
1627 # the %paste magic.
1627 # the %paste magic.
1628 def showindentationerror(self):
1628 def showindentationerror(self):
1629 """Called by run_cell when there's an IndentationError in code entered
1629 """Called by run_cell when there's an IndentationError in code entered
1630 at the prompt.
1630 at the prompt.
1631
1631
1632 This is overridden in TerminalInteractiveShell to show a message about
1632 This is overridden in TerminalInteractiveShell to show a message about
1633 the %paste magic."""
1633 the %paste magic."""
1634 self.showsyntaxerror()
1634 self.showsyntaxerror()
1635
1635
1636 #-------------------------------------------------------------------------
1636 #-------------------------------------------------------------------------
1637 # Things related to readline
1637 # Things related to readline
1638 #-------------------------------------------------------------------------
1638 #-------------------------------------------------------------------------
1639
1639
1640 def init_readline(self):
1640 def init_readline(self):
1641 """Command history completion/saving/reloading."""
1641 """Command history completion/saving/reloading."""
1642
1642
1643 if self.readline_use:
1643 if self.readline_use:
1644 import IPython.utils.rlineimpl as readline
1644 import IPython.utils.rlineimpl as readline
1645
1645
1646 self.rl_next_input = None
1646 self.rl_next_input = None
1647 self.rl_do_indent = False
1647 self.rl_do_indent = False
1648
1648
1649 if not self.readline_use or not readline.have_readline:
1649 if not self.readline_use or not readline.have_readline:
1650 self.has_readline = False
1650 self.has_readline = False
1651 self.readline = None
1651 self.readline = None
1652 # Set a number of methods that depend on readline to be no-op
1652 # Set a number of methods that depend on readline to be no-op
1653 self.readline_no_record = no_op_context
1653 self.readline_no_record = no_op_context
1654 self.set_readline_completer = no_op
1654 self.set_readline_completer = no_op
1655 self.set_custom_completer = no_op
1655 self.set_custom_completer = no_op
1656 self.set_completer_frame = no_op
1656 self.set_completer_frame = no_op
1657 if self.readline_use:
1657 if self.readline_use:
1658 warn('Readline services not available or not loaded.')
1658 warn('Readline services not available or not loaded.')
1659 else:
1659 else:
1660 self.has_readline = True
1660 self.has_readline = True
1661 self.readline = readline
1661 self.readline = readline
1662 sys.modules['readline'] = readline
1662 sys.modules['readline'] = readline
1663
1663
1664 # Platform-specific configuration
1664 # Platform-specific configuration
1665 if os.name == 'nt':
1665 if os.name == 'nt':
1666 # FIXME - check with Frederick to see if we can harmonize
1666 # FIXME - check with Frederick to see if we can harmonize
1667 # naming conventions with pyreadline to avoid this
1667 # naming conventions with pyreadline to avoid this
1668 # platform-dependent check
1668 # platform-dependent check
1669 self.readline_startup_hook = readline.set_pre_input_hook
1669 self.readline_startup_hook = readline.set_pre_input_hook
1670 else:
1670 else:
1671 self.readline_startup_hook = readline.set_startup_hook
1671 self.readline_startup_hook = readline.set_startup_hook
1672
1672
1673 # Load user's initrc file (readline config)
1673 # Load user's initrc file (readline config)
1674 # Or if libedit is used, load editrc.
1674 # Or if libedit is used, load editrc.
1675 inputrc_name = os.environ.get('INPUTRC')
1675 inputrc_name = os.environ.get('INPUTRC')
1676 if inputrc_name is None:
1676 if inputrc_name is None:
1677 home_dir = get_home_dir()
1677 home_dir = get_home_dir()
1678 if home_dir is not None:
1678 if home_dir is not None:
1679 inputrc_name = '.inputrc'
1679 inputrc_name = '.inputrc'
1680 if readline.uses_libedit:
1680 if readline.uses_libedit:
1681 inputrc_name = '.editrc'
1681 inputrc_name = '.editrc'
1682 inputrc_name = os.path.join(home_dir, inputrc_name)
1682 inputrc_name = os.path.join(home_dir, inputrc_name)
1683 if os.path.isfile(inputrc_name):
1683 if os.path.isfile(inputrc_name):
1684 try:
1684 try:
1685 readline.read_init_file(inputrc_name)
1685 readline.read_init_file(inputrc_name)
1686 except:
1686 except:
1687 warn('Problems reading readline initialization file <%s>'
1687 warn('Problems reading readline initialization file <%s>'
1688 % inputrc_name)
1688 % inputrc_name)
1689
1689
1690 # Configure readline according to user's prefs
1690 # Configure readline according to user's prefs
1691 # This is only done if GNU readline is being used. If libedit
1691 # This is only done if GNU readline is being used. If libedit
1692 # is being used (as on Leopard) the readline config is
1692 # is being used (as on Leopard) the readline config is
1693 # not run as the syntax for libedit is different.
1693 # not run as the syntax for libedit is different.
1694 if not readline.uses_libedit:
1694 if not readline.uses_libedit:
1695 for rlcommand in self.readline_parse_and_bind:
1695 for rlcommand in self.readline_parse_and_bind:
1696 #print "loading rl:",rlcommand # dbg
1696 #print "loading rl:",rlcommand # dbg
1697 readline.parse_and_bind(rlcommand)
1697 readline.parse_and_bind(rlcommand)
1698
1698
1699 # Remove some chars from the delimiters list. If we encounter
1699 # Remove some chars from the delimiters list. If we encounter
1700 # unicode chars, discard them.
1700 # unicode chars, discard them.
1701 delims = readline.get_completer_delims()
1701 delims = readline.get_completer_delims()
1702 if not py3compat.PY3:
1702 if not py3compat.PY3:
1703 delims = delims.encode("ascii", "ignore")
1703 delims = delims.encode("ascii", "ignore")
1704 for d in self.readline_remove_delims:
1704 for d in self.readline_remove_delims:
1705 delims = delims.replace(d, "")
1705 delims = delims.replace(d, "")
1706 delims = delims.replace(ESC_MAGIC, '')
1706 delims = delims.replace(ESC_MAGIC, '')
1707 readline.set_completer_delims(delims)
1707 readline.set_completer_delims(delims)
1708 # otherwise we end up with a monster history after a while:
1708 # otherwise we end up with a monster history after a while:
1709 readline.set_history_length(self.history_length)
1709 readline.set_history_length(self.history_length)
1710
1710
1711 self.refill_readline_hist()
1711 self.refill_readline_hist()
1712 self.readline_no_record = ReadlineNoRecord(self)
1712 self.readline_no_record = ReadlineNoRecord(self)
1713
1713
1714 # Configure auto-indent for all platforms
1714 # Configure auto-indent for all platforms
1715 self.set_autoindent(self.autoindent)
1715 self.set_autoindent(self.autoindent)
1716
1716
1717 def refill_readline_hist(self):
1717 def refill_readline_hist(self):
1718 # Load the last 1000 lines from history
1718 # Load the last 1000 lines from history
1719 self.readline.clear_history()
1719 self.readline.clear_history()
1720 stdin_encoding = sys.stdin.encoding or "utf-8"
1720 stdin_encoding = sys.stdin.encoding or "utf-8"
1721 for _, _, cell in self.history_manager.get_tail(1000,
1721 for _, _, cell in self.history_manager.get_tail(1000,
1722 include_latest=True):
1722 include_latest=True):
1723 if cell.strip(): # Ignore blank lines
1723 if cell.strip(): # Ignore blank lines
1724 for line in cell.splitlines():
1724 for line in cell.splitlines():
1725 self.readline.add_history(py3compat.unicode_to_str(line,
1725 self.readline.add_history(py3compat.unicode_to_str(line,
1726 stdin_encoding))
1726 stdin_encoding))
1727
1727
1728 def set_next_input(self, s):
1728 def set_next_input(self, s):
1729 """ Sets the 'default' input string for the next command line.
1729 """ Sets the 'default' input string for the next command line.
1730
1730
1731 Requires readline.
1731 Requires readline.
1732
1732
1733 Example:
1733 Example:
1734
1734
1735 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1735 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1736 [D:\ipython]|2> Hello Word_ # cursor is here
1736 [D:\ipython]|2> Hello Word_ # cursor is here
1737 """
1737 """
1738 if isinstance(s, unicode):
1738 if isinstance(s, unicode):
1739 s = s.encode(self.stdin_encoding, 'replace')
1739 s = s.encode(self.stdin_encoding, 'replace')
1740 self.rl_next_input = s
1740 self.rl_next_input = s
1741
1741
1742 # Maybe move this to the terminal subclass?
1742 # Maybe move this to the terminal subclass?
1743 def pre_readline(self):
1743 def pre_readline(self):
1744 """readline hook to be used at the start of each line.
1744 """readline hook to be used at the start of each line.
1745
1745
1746 Currently it handles auto-indent only."""
1746 Currently it handles auto-indent only."""
1747
1747
1748 if self.rl_do_indent:
1748 if self.rl_do_indent:
1749 self.readline.insert_text(self._indent_current_str())
1749 self.readline.insert_text(self._indent_current_str())
1750 if self.rl_next_input is not None:
1750 if self.rl_next_input is not None:
1751 self.readline.insert_text(self.rl_next_input)
1751 self.readline.insert_text(self.rl_next_input)
1752 self.rl_next_input = None
1752 self.rl_next_input = None
1753
1753
1754 def _indent_current_str(self):
1754 def _indent_current_str(self):
1755 """return the current level of indentation as a string"""
1755 """return the current level of indentation as a string"""
1756 return self.input_splitter.indent_spaces * ' '
1756 return self.input_splitter.indent_spaces * ' '
1757
1757
1758 #-------------------------------------------------------------------------
1758 #-------------------------------------------------------------------------
1759 # Things related to text completion
1759 # Things related to text completion
1760 #-------------------------------------------------------------------------
1760 #-------------------------------------------------------------------------
1761
1761
1762 def init_completer(self):
1762 def init_completer(self):
1763 """Initialize the completion machinery.
1763 """Initialize the completion machinery.
1764
1764
1765 This creates completion machinery that can be used by client code,
1765 This creates completion machinery that can be used by client code,
1766 either interactively in-process (typically triggered by the readline
1766 either interactively in-process (typically triggered by the readline
1767 library), programatically (such as in test suites) or out-of-prcess
1767 library), programatically (such as in test suites) or out-of-prcess
1768 (typically over the network by remote frontends).
1768 (typically over the network by remote frontends).
1769 """
1769 """
1770 from IPython.core.completer import IPCompleter
1770 from IPython.core.completer import IPCompleter
1771 from IPython.core.completerlib import (module_completer,
1771 from IPython.core.completerlib import (module_completer,
1772 magic_run_completer, cd_completer)
1772 magic_run_completer, cd_completer)
1773
1773
1774 self.Completer = IPCompleter(shell=self,
1774 self.Completer = IPCompleter(shell=self,
1775 namespace=self.user_ns,
1775 namespace=self.user_ns,
1776 global_namespace=self.user_global_ns,
1776 global_namespace=self.user_global_ns,
1777 omit__names=self.readline_omit__names,
1777 omit__names=self.readline_omit__names,
1778 alias_table=self.alias_manager.alias_table,
1778 alias_table=self.alias_manager.alias_table,
1779 use_readline=self.has_readline,
1779 use_readline=self.has_readline,
1780 config=self.config,
1780 config=self.config,
1781 )
1781 )
1782
1782
1783 # Add custom completers to the basic ones built into IPCompleter
1783 # Add custom completers to the basic ones built into IPCompleter
1784 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1784 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1785 self.strdispatchers['complete_command'] = sdisp
1785 self.strdispatchers['complete_command'] = sdisp
1786 self.Completer.custom_completers = sdisp
1786 self.Completer.custom_completers = sdisp
1787
1787
1788 self.set_hook('complete_command', module_completer, str_key = 'import')
1788 self.set_hook('complete_command', module_completer, str_key = 'import')
1789 self.set_hook('complete_command', module_completer, str_key = 'from')
1789 self.set_hook('complete_command', module_completer, str_key = 'from')
1790 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1790 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1791 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1791 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1792
1792
1793 # Only configure readline if we truly are using readline. IPython can
1793 # Only configure readline if we truly are using readline. IPython can
1794 # do tab-completion over the network, in GUIs, etc, where readline
1794 # do tab-completion over the network, in GUIs, etc, where readline
1795 # itself may be absent
1795 # itself may be absent
1796 if self.has_readline:
1796 if self.has_readline:
1797 self.set_readline_completer()
1797 self.set_readline_completer()
1798
1798
1799 def complete(self, text, line=None, cursor_pos=None):
1799 def complete(self, text, line=None, cursor_pos=None):
1800 """Return the completed text and a list of completions.
1800 """Return the completed text and a list of completions.
1801
1801
1802 Parameters
1802 Parameters
1803 ----------
1803 ----------
1804
1804
1805 text : string
1805 text : string
1806 A string of text to be completed on. It can be given as empty and
1806 A string of text to be completed on. It can be given as empty and
1807 instead a line/position pair are given. In this case, the
1807 instead a line/position pair are given. In this case, the
1808 completer itself will split the line like readline does.
1808 completer itself will split the line like readline does.
1809
1809
1810 line : string, optional
1810 line : string, optional
1811 The complete line that text is part of.
1811 The complete line that text is part of.
1812
1812
1813 cursor_pos : int, optional
1813 cursor_pos : int, optional
1814 The position of the cursor on the input line.
1814 The position of the cursor on the input line.
1815
1815
1816 Returns
1816 Returns
1817 -------
1817 -------
1818 text : string
1818 text : string
1819 The actual text that was completed.
1819 The actual text that was completed.
1820
1820
1821 matches : list
1821 matches : list
1822 A sorted list with all possible completions.
1822 A sorted list with all possible completions.
1823
1823
1824 The optional arguments allow the completion to take more context into
1824 The optional arguments allow the completion to take more context into
1825 account, and are part of the low-level completion API.
1825 account, and are part of the low-level completion API.
1826
1826
1827 This is a wrapper around the completion mechanism, similar to what
1827 This is a wrapper around the completion mechanism, similar to what
1828 readline does at the command line when the TAB key is hit. By
1828 readline does at the command line when the TAB key is hit. By
1829 exposing it as a method, it can be used by other non-readline
1829 exposing it as a method, it can be used by other non-readline
1830 environments (such as GUIs) for text completion.
1830 environments (such as GUIs) for text completion.
1831
1831
1832 Simple usage example:
1832 Simple usage example:
1833
1833
1834 In [1]: x = 'hello'
1834 In [1]: x = 'hello'
1835
1835
1836 In [2]: _ip.complete('x.l')
1836 In [2]: _ip.complete('x.l')
1837 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1837 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1838 """
1838 """
1839
1839
1840 # Inject names into __builtin__ so we can complete on the added names.
1840 # Inject names into __builtin__ so we can complete on the added names.
1841 with self.builtin_trap:
1841 with self.builtin_trap:
1842 return self.Completer.complete(text, line, cursor_pos)
1842 return self.Completer.complete(text, line, cursor_pos)
1843
1843
1844 def set_custom_completer(self, completer, pos=0):
1844 def set_custom_completer(self, completer, pos=0):
1845 """Adds a new custom completer function.
1845 """Adds a new custom completer function.
1846
1846
1847 The position argument (defaults to 0) is the index in the completers
1847 The position argument (defaults to 0) is the index in the completers
1848 list where you want the completer to be inserted."""
1848 list where you want the completer to be inserted."""
1849
1849
1850 newcomp = types.MethodType(completer,self.Completer)
1850 newcomp = types.MethodType(completer,self.Completer)
1851 self.Completer.matchers.insert(pos,newcomp)
1851 self.Completer.matchers.insert(pos,newcomp)
1852
1852
1853 def set_readline_completer(self):
1853 def set_readline_completer(self):
1854 """Reset readline's completer to be our own."""
1854 """Reset readline's completer to be our own."""
1855 self.readline.set_completer(self.Completer.rlcomplete)
1855 self.readline.set_completer(self.Completer.rlcomplete)
1856
1856
1857 def set_completer_frame(self, frame=None):
1857 def set_completer_frame(self, frame=None):
1858 """Set the frame of the completer."""
1858 """Set the frame of the completer."""
1859 if frame:
1859 if frame:
1860 self.Completer.namespace = frame.f_locals
1860 self.Completer.namespace = frame.f_locals
1861 self.Completer.global_namespace = frame.f_globals
1861 self.Completer.global_namespace = frame.f_globals
1862 else:
1862 else:
1863 self.Completer.namespace = self.user_ns
1863 self.Completer.namespace = self.user_ns
1864 self.Completer.global_namespace = self.user_global_ns
1864 self.Completer.global_namespace = self.user_global_ns
1865
1865
1866 #-------------------------------------------------------------------------
1866 #-------------------------------------------------------------------------
1867 # Things related to magics
1867 # Things related to magics
1868 #-------------------------------------------------------------------------
1868 #-------------------------------------------------------------------------
1869
1869
1870 def init_magics(self):
1870 def init_magics(self):
1871 # FIXME: Move the color initialization to the DisplayHook, which
1871 # FIXME: Move the color initialization to the DisplayHook, which
1872 # should be split into a prompt manager and displayhook. We probably
1872 # should be split into a prompt manager and displayhook. We probably
1873 # even need a centralize colors management object.
1873 # even need a centralize colors management object.
1874 self.magic_colors(self.colors)
1874 self.magic_colors(self.colors)
1875 # History was moved to a separate module
1875 # History was moved to a separate module
1876 from . import history
1876 from . import history
1877 history.init_ipython(self)
1877 history.init_ipython(self)
1878
1878
1879 def magic(self, arg_s, next_input=None):
1879 def magic(self, arg_s, next_input=None):
1880 """Call a magic function by name.
1880 """Call a magic function by name.
1881
1881
1882 Input: a string containing the name of the magic function to call and
1882 Input: a string containing the name of the magic function to call and
1883 any additional arguments to be passed to the magic.
1883 any additional arguments to be passed to the magic.
1884
1884
1885 magic('name -opt foo bar') is equivalent to typing at the ipython
1885 magic('name -opt foo bar') is equivalent to typing at the ipython
1886 prompt:
1886 prompt:
1887
1887
1888 In[1]: %name -opt foo bar
1888 In[1]: %name -opt foo bar
1889
1889
1890 To call a magic without arguments, simply use magic('name').
1890 To call a magic without arguments, simply use magic('name').
1891
1891
1892 This provides a proper Python function to call IPython's magics in any
1892 This provides a proper Python function to call IPython's magics in any
1893 valid Python code you can type at the interpreter, including loops and
1893 valid Python code you can type at the interpreter, including loops and
1894 compound statements.
1894 compound statements.
1895 """
1895 """
1896 # Allow setting the next input - this is used if the user does `a=abs?`.
1896 # Allow setting the next input - this is used if the user does `a=abs?`.
1897 # We do this first so that magic functions can override it.
1897 # We do this first so that magic functions can override it.
1898 if next_input:
1898 if next_input:
1899 self.set_next_input(next_input)
1899 self.set_next_input(next_input)
1900
1900
1901 args = arg_s.split(' ',1)
1901 args = arg_s.split(' ',1)
1902 magic_name = args[0]
1902 magic_name = args[0]
1903 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1903 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1904
1904
1905 try:
1905 try:
1906 magic_args = args[1]
1906 magic_args = args[1]
1907 except IndexError:
1907 except IndexError:
1908 magic_args = ''
1908 magic_args = ''
1909 fn = getattr(self,'magic_'+magic_name,None)
1909 fn = getattr(self,'magic_'+magic_name,None)
1910 if fn is None:
1910 if fn is None:
1911 error("Magic function `%s` not found." % magic_name)
1911 error("Magic function `%s` not found." % magic_name)
1912 else:
1912 else:
1913 magic_args = self.var_expand(magic_args,1)
1913 magic_args = self.var_expand(magic_args,1)
1914 # Grab local namespace if we need it:
1914 # Grab local namespace if we need it:
1915 if getattr(fn, "needs_local_scope", False):
1915 if getattr(fn, "needs_local_scope", False):
1916 self._magic_locals = sys._getframe(1).f_locals
1916 self._magic_locals = sys._getframe(1).f_locals
1917 with self.builtin_trap:
1917 with self.builtin_trap:
1918 result = fn(magic_args)
1918 result = fn(magic_args)
1919 # Ensure we're not keeping object references around:
1919 # Ensure we're not keeping object references around:
1920 self._magic_locals = {}
1920 self._magic_locals = {}
1921 return result
1921 return result
1922
1922
1923 def define_magic(self, magicname, func):
1923 def define_magic(self, magicname, func):
1924 """Expose own function as magic function for ipython
1924 """Expose own function as magic function for ipython
1925
1925
1926 def foo_impl(self,parameter_s=''):
1926 def foo_impl(self,parameter_s=''):
1927 'My very own magic!. (Use docstrings, IPython reads them).'
1927 'My very own magic!. (Use docstrings, IPython reads them).'
1928 print 'Magic function. Passed parameter is between < >:'
1928 print 'Magic function. Passed parameter is between < >:'
1929 print '<%s>' % parameter_s
1929 print '<%s>' % parameter_s
1930 print 'The self object is:',self
1930 print 'The self object is:',self
1931
1931
1932 self.define_magic('foo',foo_impl)
1932 self.define_magic('foo',foo_impl)
1933 """
1933 """
1934 im = types.MethodType(func,self)
1934 im = types.MethodType(func,self)
1935 old = getattr(self, "magic_" + magicname, None)
1935 old = getattr(self, "magic_" + magicname, None)
1936 setattr(self, "magic_" + magicname, im)
1936 setattr(self, "magic_" + magicname, im)
1937 return old
1937 return old
1938
1938
1939 #-------------------------------------------------------------------------
1939 #-------------------------------------------------------------------------
1940 # Things related to macros
1940 # Things related to macros
1941 #-------------------------------------------------------------------------
1941 #-------------------------------------------------------------------------
1942
1942
1943 def define_macro(self, name, themacro):
1943 def define_macro(self, name, themacro):
1944 """Define a new macro
1944 """Define a new macro
1945
1945
1946 Parameters
1946 Parameters
1947 ----------
1947 ----------
1948 name : str
1948 name : str
1949 The name of the macro.
1949 The name of the macro.
1950 themacro : str or Macro
1950 themacro : str or Macro
1951 The action to do upon invoking the macro. If a string, a new
1951 The action to do upon invoking the macro. If a string, a new
1952 Macro object is created by passing the string to it.
1952 Macro object is created by passing the string to it.
1953 """
1953 """
1954
1954
1955 from IPython.core import macro
1955 from IPython.core import macro
1956
1956
1957 if isinstance(themacro, basestring):
1957 if isinstance(themacro, basestring):
1958 themacro = macro.Macro(themacro)
1958 themacro = macro.Macro(themacro)
1959 if not isinstance(themacro, macro.Macro):
1959 if not isinstance(themacro, macro.Macro):
1960 raise ValueError('A macro must be a string or a Macro instance.')
1960 raise ValueError('A macro must be a string or a Macro instance.')
1961 self.user_ns[name] = themacro
1961 self.user_ns[name] = themacro
1962
1962
1963 #-------------------------------------------------------------------------
1963 #-------------------------------------------------------------------------
1964 # Things related to the running of system commands
1964 # Things related to the running of system commands
1965 #-------------------------------------------------------------------------
1965 #-------------------------------------------------------------------------
1966
1966
1967 def system_piped(self, cmd):
1967 def system_piped(self, cmd):
1968 """Call the given cmd in a subprocess, piping stdout/err
1968 """Call the given cmd in a subprocess, piping stdout/err
1969
1969
1970 Parameters
1970 Parameters
1971 ----------
1971 ----------
1972 cmd : str
1972 cmd : str
1973 Command to execute (can not end in '&', as background processes are
1973 Command to execute (can not end in '&', as background processes are
1974 not supported. Should not be a command that expects input
1974 not supported. Should not be a command that expects input
1975 other than simple text.
1975 other than simple text.
1976 """
1976 """
1977 if cmd.rstrip().endswith('&'):
1977 if cmd.rstrip().endswith('&'):
1978 # this is *far* from a rigorous test
1978 # this is *far* from a rigorous test
1979 # We do not support backgrounding processes because we either use
1979 # We do not support backgrounding processes because we either use
1980 # pexpect or pipes to read from. Users can always just call
1980 # pexpect or pipes to read from. Users can always just call
1981 # os.system() or use ip.system=ip.system_raw
1981 # os.system() or use ip.system=ip.system_raw
1982 # if they really want a background process.
1982 # if they really want a background process.
1983 raise OSError("Background processes not supported.")
1983 raise OSError("Background processes not supported.")
1984
1984
1985 # we explicitly do NOT return the subprocess status code, because
1985 # we explicitly do NOT return the subprocess status code, because
1986 # a non-None value would trigger :func:`sys.displayhook` calls.
1986 # a non-None value would trigger :func:`sys.displayhook` calls.
1987 # Instead, we store the exit_code in user_ns.
1987 # Instead, we store the exit_code in user_ns.
1988 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=2))
1988 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=2))
1989
1989
1990 def system_raw(self, cmd):
1990 def system_raw(self, cmd):
1991 """Call the given cmd in a subprocess using os.system
1991 """Call the given cmd in a subprocess using os.system
1992
1992
1993 Parameters
1993 Parameters
1994 ----------
1994 ----------
1995 cmd : str
1995 cmd : str
1996 Command to execute.
1996 Command to execute.
1997 """
1997 """
1998 # We explicitly do NOT return the subprocess status code, because
1998 # We explicitly do NOT return the subprocess status code, because
1999 # a non-None value would trigger :func:`sys.displayhook` calls.
1999 # a non-None value would trigger :func:`sys.displayhook` calls.
2000 # Instead, we store the exit_code in user_ns.
2000 # Instead, we store the exit_code in user_ns.
2001 self.user_ns['_exit_code'] = os.system(self.var_expand(cmd, depth=2))
2001 self.user_ns['_exit_code'] = os.system(self.var_expand(cmd, depth=2))
2002
2002
2003 # use piped system by default, because it is better behaved
2003 # use piped system by default, because it is better behaved
2004 system = system_piped
2004 system = system_piped
2005
2005
2006 def getoutput(self, cmd, split=True):
2006 def getoutput(self, cmd, split=True):
2007 """Get output (possibly including stderr) from a subprocess.
2007 """Get output (possibly including stderr) from a subprocess.
2008
2008
2009 Parameters
2009 Parameters
2010 ----------
2010 ----------
2011 cmd : str
2011 cmd : str
2012 Command to execute (can not end in '&', as background processes are
2012 Command to execute (can not end in '&', as background processes are
2013 not supported.
2013 not supported.
2014 split : bool, optional
2014 split : bool, optional
2015
2015
2016 If True, split the output into an IPython SList. Otherwise, an
2016 If True, split the output into an IPython SList. Otherwise, an
2017 IPython LSString is returned. These are objects similar to normal
2017 IPython LSString is returned. These are objects similar to normal
2018 lists and strings, with a few convenience attributes for easier
2018 lists and strings, with a few convenience attributes for easier
2019 manipulation of line-based output. You can use '?' on them for
2019 manipulation of line-based output. You can use '?' on them for
2020 details.
2020 details.
2021 """
2021 """
2022 if cmd.rstrip().endswith('&'):
2022 if cmd.rstrip().endswith('&'):
2023 # this is *far* from a rigorous test
2023 # this is *far* from a rigorous test
2024 raise OSError("Background processes not supported.")
2024 raise OSError("Background processes not supported.")
2025 out = getoutput(self.var_expand(cmd, depth=2))
2025 out = getoutput(self.var_expand(cmd, depth=2))
2026 if split:
2026 if split:
2027 out = SList(out.splitlines())
2027 out = SList(out.splitlines())
2028 else:
2028 else:
2029 out = LSString(out)
2029 out = LSString(out)
2030 return out
2030 return out
2031
2031
2032 #-------------------------------------------------------------------------
2032 #-------------------------------------------------------------------------
2033 # Things related to aliases
2033 # Things related to aliases
2034 #-------------------------------------------------------------------------
2034 #-------------------------------------------------------------------------
2035
2035
2036 def init_alias(self):
2036 def init_alias(self):
2037 self.alias_manager = AliasManager(shell=self, config=self.config)
2037 self.alias_manager = AliasManager(shell=self, config=self.config)
2038 self.ns_table['alias'] = self.alias_manager.alias_table,
2038 self.ns_table['alias'] = self.alias_manager.alias_table,
2039
2039
2040 #-------------------------------------------------------------------------
2040 #-------------------------------------------------------------------------
2041 # Things related to extensions and plugins
2041 # Things related to extensions and plugins
2042 #-------------------------------------------------------------------------
2042 #-------------------------------------------------------------------------
2043
2043
2044 def init_extension_manager(self):
2044 def init_extension_manager(self):
2045 self.extension_manager = ExtensionManager(shell=self, config=self.config)
2045 self.extension_manager = ExtensionManager(shell=self, config=self.config)
2046
2046
2047 def init_plugin_manager(self):
2047 def init_plugin_manager(self):
2048 self.plugin_manager = PluginManager(config=self.config)
2048 self.plugin_manager = PluginManager(config=self.config)
2049
2049
2050 #-------------------------------------------------------------------------
2050 #-------------------------------------------------------------------------
2051 # Things related to payloads
2051 # Things related to payloads
2052 #-------------------------------------------------------------------------
2052 #-------------------------------------------------------------------------
2053
2053
2054 def init_payload(self):
2054 def init_payload(self):
2055 self.payload_manager = PayloadManager(config=self.config)
2055 self.payload_manager = PayloadManager(config=self.config)
2056
2056
2057 #-------------------------------------------------------------------------
2057 #-------------------------------------------------------------------------
2058 # Things related to the prefilter
2058 # Things related to the prefilter
2059 #-------------------------------------------------------------------------
2059 #-------------------------------------------------------------------------
2060
2060
2061 def init_prefilter(self):
2061 def init_prefilter(self):
2062 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
2062 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
2063 # Ultimately this will be refactored in the new interpreter code, but
2063 # Ultimately this will be refactored in the new interpreter code, but
2064 # for now, we should expose the main prefilter method (there's legacy
2064 # for now, we should expose the main prefilter method (there's legacy
2065 # code out there that may rely on this).
2065 # code out there that may rely on this).
2066 self.prefilter = self.prefilter_manager.prefilter_lines
2066 self.prefilter = self.prefilter_manager.prefilter_lines
2067
2067
2068 def auto_rewrite_input(self, cmd):
2068 def auto_rewrite_input(self, cmd):
2069 """Print to the screen the rewritten form of the user's command.
2069 """Print to the screen the rewritten form of the user's command.
2070
2070
2071 This shows visual feedback by rewriting input lines that cause
2071 This shows visual feedback by rewriting input lines that cause
2072 automatic calling to kick in, like::
2072 automatic calling to kick in, like::
2073
2073
2074 /f x
2074 /f x
2075
2075
2076 into::
2076 into::
2077
2077
2078 ------> f(x)
2078 ------> f(x)
2079
2079
2080 after the user's input prompt. This helps the user understand that the
2080 after the user's input prompt. This helps the user understand that the
2081 input line was transformed automatically by IPython.
2081 input line was transformed automatically by IPython.
2082 """
2082 """
2083 rw = self.displayhook.prompt1.auto_rewrite() + cmd
2083 rw = self.displayhook.prompt1.auto_rewrite() + cmd
2084
2084
2085 try:
2085 try:
2086 # plain ascii works better w/ pyreadline, on some machines, so
2086 # plain ascii works better w/ pyreadline, on some machines, so
2087 # we use it and only print uncolored rewrite if we have unicode
2087 # we use it and only print uncolored rewrite if we have unicode
2088 rw = str(rw)
2088 rw = str(rw)
2089 print >> io.stdout, rw
2089 print >> io.stdout, rw
2090 except UnicodeEncodeError:
2090 except UnicodeEncodeError:
2091 print "------> " + cmd
2091 print "------> " + cmd
2092
2092
2093 #-------------------------------------------------------------------------
2093 #-------------------------------------------------------------------------
2094 # Things related to extracting values/expressions from kernel and user_ns
2094 # Things related to extracting values/expressions from kernel and user_ns
2095 #-------------------------------------------------------------------------
2095 #-------------------------------------------------------------------------
2096
2096
2097 def _simple_error(self):
2097 def _simple_error(self):
2098 etype, value = sys.exc_info()[:2]
2098 etype, value = sys.exc_info()[:2]
2099 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
2099 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
2100
2100
2101 def user_variables(self, names):
2101 def user_variables(self, names):
2102 """Get a list of variable names from the user's namespace.
2102 """Get a list of variable names from the user's namespace.
2103
2103
2104 Parameters
2104 Parameters
2105 ----------
2105 ----------
2106 names : list of strings
2106 names : list of strings
2107 A list of names of variables to be read from the user namespace.
2107 A list of names of variables to be read from the user namespace.
2108
2108
2109 Returns
2109 Returns
2110 -------
2110 -------
2111 A dict, keyed by the input names and with the repr() of each value.
2111 A dict, keyed by the input names and with the repr() of each value.
2112 """
2112 """
2113 out = {}
2113 out = {}
2114 user_ns = self.user_ns
2114 user_ns = self.user_ns
2115 for varname in names:
2115 for varname in names:
2116 try:
2116 try:
2117 value = repr(user_ns[varname])
2117 value = repr(user_ns[varname])
2118 except:
2118 except:
2119 value = self._simple_error()
2119 value = self._simple_error()
2120 out[varname] = value
2120 out[varname] = value
2121 return out
2121 return out
2122
2122
2123 def user_expressions(self, expressions):
2123 def user_expressions(self, expressions):
2124 """Evaluate a dict of expressions in the user's namespace.
2124 """Evaluate a dict of expressions in the user's namespace.
2125
2125
2126 Parameters
2126 Parameters
2127 ----------
2127 ----------
2128 expressions : dict
2128 expressions : dict
2129 A dict with string keys and string values. The expression values
2129 A dict with string keys and string values. The expression values
2130 should be valid Python expressions, each of which will be evaluated
2130 should be valid Python expressions, each of which will be evaluated
2131 in the user namespace.
2131 in the user namespace.
2132
2132
2133 Returns
2133 Returns
2134 -------
2134 -------
2135 A dict, keyed like the input expressions dict, with the repr() of each
2135 A dict, keyed like the input expressions dict, with the repr() of each
2136 value.
2136 value.
2137 """
2137 """
2138 out = {}
2138 out = {}
2139 user_ns = self.user_ns
2139 user_ns = self.user_ns
2140 global_ns = self.user_global_ns
2140 global_ns = self.user_global_ns
2141 for key, expr in expressions.iteritems():
2141 for key, expr in expressions.iteritems():
2142 try:
2142 try:
2143 value = repr(eval(expr, global_ns, user_ns))
2143 value = repr(eval(expr, global_ns, user_ns))
2144 except:
2144 except:
2145 value = self._simple_error()
2145 value = self._simple_error()
2146 out[key] = value
2146 out[key] = value
2147 return out
2147 return out
2148
2148
2149 #-------------------------------------------------------------------------
2149 #-------------------------------------------------------------------------
2150 # Things related to the running of code
2150 # Things related to the running of code
2151 #-------------------------------------------------------------------------
2151 #-------------------------------------------------------------------------
2152
2152
2153 def ex(self, cmd):
2153 def ex(self, cmd):
2154 """Execute a normal python statement in user namespace."""
2154 """Execute a normal python statement in user namespace."""
2155 with self.builtin_trap:
2155 with self.builtin_trap:
2156 exec cmd in self.user_global_ns, self.user_ns
2156 exec cmd in self.user_global_ns, self.user_ns
2157
2157
2158 def ev(self, expr):
2158 def ev(self, expr):
2159 """Evaluate python expression expr in user namespace.
2159 """Evaluate python expression expr in user namespace.
2160
2160
2161 Returns the result of evaluation
2161 Returns the result of evaluation
2162 """
2162 """
2163 with self.builtin_trap:
2163 with self.builtin_trap:
2164 return eval(expr, self.user_global_ns, self.user_ns)
2164 return eval(expr, self.user_global_ns, self.user_ns)
2165
2165
2166 def safe_execfile(self, fname, *where, **kw):
2166 def safe_execfile(self, fname, *where, **kw):
2167 """A safe version of the builtin execfile().
2167 """A safe version of the builtin execfile().
2168
2168
2169 This version will never throw an exception, but instead print
2169 This version will never throw an exception, but instead print
2170 helpful error messages to the screen. This only works on pure
2170 helpful error messages to the screen. This only works on pure
2171 Python files with the .py extension.
2171 Python files with the .py extension.
2172
2172
2173 Parameters
2173 Parameters
2174 ----------
2174 ----------
2175 fname : string
2175 fname : string
2176 The name of the file to be executed.
2176 The name of the file to be executed.
2177 where : tuple
2177 where : tuple
2178 One or two namespaces, passed to execfile() as (globals,locals).
2178 One or two namespaces, passed to execfile() as (globals,locals).
2179 If only one is given, it is passed as both.
2179 If only one is given, it is passed as both.
2180 exit_ignore : bool (False)
2180 exit_ignore : bool (False)
2181 If True, then silence SystemExit for non-zero status (it is always
2181 If True, then silence SystemExit for non-zero status (it is always
2182 silenced for zero status, as it is so common).
2182 silenced for zero status, as it is so common).
2183 """
2183 """
2184 kw.setdefault('exit_ignore', False)
2184 kw.setdefault('exit_ignore', False)
2185
2185
2186 fname = os.path.abspath(os.path.expanduser(fname))
2186 fname = os.path.abspath(os.path.expanduser(fname))
2187
2187
2188 # Make sure we can open the file
2188 # Make sure we can open the file
2189 try:
2189 try:
2190 with open(fname) as thefile:
2190 with open(fname) as thefile:
2191 pass
2191 pass
2192 except:
2192 except:
2193 warn('Could not open file <%s> for safe execution.' % fname)
2193 warn('Could not open file <%s> for safe execution.' % fname)
2194 return
2194 return
2195
2195
2196 # Find things also in current directory. This is needed to mimic the
2196 # Find things also in current directory. This is needed to mimic the
2197 # behavior of running a script from the system command line, where
2197 # behavior of running a script from the system command line, where
2198 # Python inserts the script's directory into sys.path
2198 # Python inserts the script's directory into sys.path
2199 dname = os.path.dirname(fname)
2199 dname = os.path.dirname(fname)
2200
2200
2201 with prepended_to_syspath(dname):
2201 with prepended_to_syspath(dname):
2202 try:
2202 try:
2203 py3compat.execfile(fname,*where)
2203 py3compat.execfile(fname,*where)
2204 except SystemExit, status:
2204 except SystemExit, status:
2205 # If the call was made with 0 or None exit status (sys.exit(0)
2205 # If the call was made with 0 or None exit status (sys.exit(0)
2206 # or sys.exit() ), don't bother showing a traceback, as both of
2206 # or sys.exit() ), don't bother showing a traceback, as both of
2207 # these are considered normal by the OS:
2207 # these are considered normal by the OS:
2208 # > python -c'import sys;sys.exit(0)'; echo $?
2208 # > python -c'import sys;sys.exit(0)'; echo $?
2209 # 0
2209 # 0
2210 # > python -c'import sys;sys.exit()'; echo $?
2210 # > python -c'import sys;sys.exit()'; echo $?
2211 # 0
2211 # 0
2212 # For other exit status, we show the exception unless
2212 # For other exit status, we show the exception unless
2213 # explicitly silenced, but only in short form.
2213 # explicitly silenced, but only in short form.
2214 if status.code not in (0, None) and not kw['exit_ignore']:
2214 if status.code not in (0, None) and not kw['exit_ignore']:
2215 self.showtraceback(exception_only=True)
2215 self.showtraceback(exception_only=True)
2216 except:
2216 except:
2217 self.showtraceback()
2217 self.showtraceback()
2218
2218
2219 def safe_execfile_ipy(self, fname):
2219 def safe_execfile_ipy(self, fname):
2220 """Like safe_execfile, but for .ipy files with IPython syntax.
2220 """Like safe_execfile, but for .ipy files with IPython syntax.
2221
2221
2222 Parameters
2222 Parameters
2223 ----------
2223 ----------
2224 fname : str
2224 fname : str
2225 The name of the file to execute. The filename must have a
2225 The name of the file to execute. The filename must have a
2226 .ipy extension.
2226 .ipy extension.
2227 """
2227 """
2228 fname = os.path.abspath(os.path.expanduser(fname))
2228 fname = os.path.abspath(os.path.expanduser(fname))
2229
2229
2230 # Make sure we can open the file
2230 # Make sure we can open the file
2231 try:
2231 try:
2232 with open(fname) as thefile:
2232 with open(fname) as thefile:
2233 pass
2233 pass
2234 except:
2234 except:
2235 warn('Could not open file <%s> for safe execution.' % fname)
2235 warn('Could not open file <%s> for safe execution.' % fname)
2236 return
2236 return
2237
2237
2238 # Find things also in current directory. This is needed to mimic the
2238 # Find things also in current directory. This is needed to mimic the
2239 # behavior of running a script from the system command line, where
2239 # behavior of running a script from the system command line, where
2240 # Python inserts the script's directory into sys.path
2240 # Python inserts the script's directory into sys.path
2241 dname = os.path.dirname(fname)
2241 dname = os.path.dirname(fname)
2242
2242
2243 with prepended_to_syspath(dname):
2243 with prepended_to_syspath(dname):
2244 try:
2244 try:
2245 with open(fname) as thefile:
2245 with open(fname) as thefile:
2246 # self.run_cell currently captures all exceptions
2246 # self.run_cell currently captures all exceptions
2247 # raised in user code. It would be nice if there were
2247 # raised in user code. It would be nice if there were
2248 # versions of runlines, execfile that did raise, so
2248 # versions of runlines, execfile that did raise, so
2249 # we could catch the errors.
2249 # we could catch the errors.
2250 self.run_cell(thefile.read(), store_history=False)
2250 self.run_cell(thefile.read(), store_history=False)
2251 except:
2251 except:
2252 self.showtraceback()
2252 self.showtraceback()
2253 warn('Unknown failure executing file: <%s>' % fname)
2253 warn('Unknown failure executing file: <%s>' % fname)
2254
2254
2255 def run_cell(self, raw_cell, store_history=True):
2255 def run_cell(self, raw_cell, store_history=True):
2256 """Run a complete IPython cell.
2256 """Run a complete IPython cell.
2257
2257
2258 Parameters
2258 Parameters
2259 ----------
2259 ----------
2260 raw_cell : str
2260 raw_cell : str
2261 The code (including IPython code such as %magic functions) to run.
2261 The code (including IPython code such as %magic functions) to run.
2262 store_history : bool
2262 store_history : bool
2263 If True, the raw and translated cell will be stored in IPython's
2263 If True, the raw and translated cell will be stored in IPython's
2264 history. For user code calling back into IPython's machinery, this
2264 history. For user code calling back into IPython's machinery, this
2265 should be set to False.
2265 should be set to False.
2266 """
2266 """
2267 if (not raw_cell) or raw_cell.isspace():
2267 if (not raw_cell) or raw_cell.isspace():
2268 return
2268 return
2269
2269
2270 for line in raw_cell.splitlines():
2270 for line in raw_cell.splitlines():
2271 self.input_splitter.push(line)
2271 self.input_splitter.push(line)
2272 cell = self.input_splitter.source_reset()
2272 cell = self.input_splitter.source_reset()
2273
2273
2274 with self.builtin_trap:
2274 with self.builtin_trap:
2275 prefilter_failed = False
2275 prefilter_failed = False
2276 if len(cell.splitlines()) == 1:
2276 if len(cell.splitlines()) == 1:
2277 try:
2277 try:
2278 # use prefilter_lines to handle trailing newlines
2278 # use prefilter_lines to handle trailing newlines
2279 # restore trailing newline for ast.parse
2279 # restore trailing newline for ast.parse
2280 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2280 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2281 except AliasError as e:
2281 except AliasError as e:
2282 error(e)
2282 error(e)
2283 prefilter_failed = True
2283 prefilter_failed = True
2284 except Exception:
2284 except Exception:
2285 # don't allow prefilter errors to crash IPython
2285 # don't allow prefilter errors to crash IPython
2286 self.showtraceback()
2286 self.showtraceback()
2287 prefilter_failed = True
2287 prefilter_failed = True
2288
2288
2289 # Store raw and processed history
2289 # Store raw and processed history
2290 if store_history:
2290 if store_history:
2291 self.history_manager.store_inputs(self.execution_count,
2291 self.history_manager.store_inputs(self.execution_count,
2292 cell, raw_cell)
2292 cell, raw_cell)
2293
2293
2294 self.logger.log(cell, raw_cell)
2294 self.logger.log(cell, raw_cell)
2295
2295
2296 if not prefilter_failed:
2296 if not prefilter_failed:
2297 # don't run if prefilter failed
2297 # don't run if prefilter failed
2298 cell_name = self.compile.cache(cell, self.execution_count)
2298 cell_name = self.compile.cache(cell, self.execution_count)
2299
2299
2300 with self.display_trap:
2300 with self.display_trap:
2301 try:
2301 try:
2302 code_ast = self.compile.ast_parse(cell, filename=cell_name)
2302 code_ast = self.compile.ast_parse(cell, filename=cell_name)
2303 except IndentationError:
2303 except IndentationError:
2304 self.showindentationerror()
2304 self.showindentationerror()
2305 self.execution_count += 1
2305 self.execution_count += 1
2306 return None
2306 return None
2307 except (OverflowError, SyntaxError, ValueError, TypeError,
2307 except (OverflowError, SyntaxError, ValueError, TypeError,
2308 MemoryError):
2308 MemoryError):
2309 self.showsyntaxerror()
2309 self.showsyntaxerror()
2310 self.execution_count += 1
2310 self.execution_count += 1
2311 return None
2311 return None
2312
2312
2313 self.run_ast_nodes(code_ast.body, cell_name,
2313 self.run_ast_nodes(code_ast.body, cell_name,
2314 interactivity="last_expr")
2314 interactivity="last_expr")
2315
2315
2316 # Execute any registered post-execution functions.
2316 # Execute any registered post-execution functions.
2317 for func, status in self._post_execute.iteritems():
2317 for func, status in self._post_execute.iteritems():
2318 if not status:
2318 if not status:
2319 continue
2319 continue
2320 try:
2320 try:
2321 func()
2321 func()
2322 except:
2322 except:
2323 self.showtraceback()
2323 self.showtraceback()
2324 # Deactivate failing function
2324 # Deactivate failing function
2325 self._post_execute[func] = False
2325 self._post_execute[func] = False
2326
2326
2327 if store_history:
2327 if store_history:
2328 # Write output to the database. Does nothing unless
2328 # Write output to the database. Does nothing unless
2329 # history output logging is enabled.
2329 # history output logging is enabled.
2330 self.history_manager.store_output(self.execution_count)
2330 self.history_manager.store_output(self.execution_count)
2331 # Each cell is a *single* input, regardless of how many lines it has
2331 # Each cell is a *single* input, regardless of how many lines it has
2332 self.execution_count += 1
2332 self.execution_count += 1
2333
2333
2334 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'):
2334 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'):
2335 """Run a sequence of AST nodes. The execution mode depends on the
2335 """Run a sequence of AST nodes. The execution mode depends on the
2336 interactivity parameter.
2336 interactivity parameter.
2337
2337
2338 Parameters
2338 Parameters
2339 ----------
2339 ----------
2340 nodelist : list
2340 nodelist : list
2341 A sequence of AST nodes to run.
2341 A sequence of AST nodes to run.
2342 cell_name : str
2342 cell_name : str
2343 Will be passed to the compiler as the filename of the cell. Typically
2343 Will be passed to the compiler as the filename of the cell. Typically
2344 the value returned by ip.compile.cache(cell).
2344 the value returned by ip.compile.cache(cell).
2345 interactivity : str
2345 interactivity : str
2346 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2346 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2347 run interactively (displaying output from expressions). 'last_expr'
2347 run interactively (displaying output from expressions). 'last_expr'
2348 will run the last node interactively only if it is an expression (i.e.
2348 will run the last node interactively only if it is an expression (i.e.
2349 expressions in loops or other blocks are not displayed. Other values
2349 expressions in loops or other blocks are not displayed. Other values
2350 for this parameter will raise a ValueError.
2350 for this parameter will raise a ValueError.
2351 """
2351 """
2352 if not nodelist:
2352 if not nodelist:
2353 return
2353 return
2354
2354
2355 if interactivity == 'last_expr':
2355 if interactivity == 'last_expr':
2356 if isinstance(nodelist[-1], ast.Expr):
2356 if isinstance(nodelist[-1], ast.Expr):
2357 interactivity = "last"
2357 interactivity = "last"
2358 else:
2358 else:
2359 interactivity = "none"
2359 interactivity = "none"
2360
2360
2361 if interactivity == 'none':
2361 if interactivity == 'none':
2362 to_run_exec, to_run_interactive = nodelist, []
2362 to_run_exec, to_run_interactive = nodelist, []
2363 elif interactivity == 'last':
2363 elif interactivity == 'last':
2364 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2364 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2365 elif interactivity == 'all':
2365 elif interactivity == 'all':
2366 to_run_exec, to_run_interactive = [], nodelist
2366 to_run_exec, to_run_interactive = [], nodelist
2367 else:
2367 else:
2368 raise ValueError("Interactivity was %r" % interactivity)
2368 raise ValueError("Interactivity was %r" % interactivity)
2369
2369
2370 exec_count = self.execution_count
2370 exec_count = self.execution_count
2371
2371
2372 try:
2372 try:
2373 for i, node in enumerate(to_run_exec):
2373 for i, node in enumerate(to_run_exec):
2374 mod = ast.Module([node])
2374 mod = ast.Module([node])
2375 code = self.compile(mod, cell_name, "exec")
2375 code = self.compile(mod, cell_name, "exec")
2376 if self.run_code(code):
2376 if self.run_code(code):
2377 return True
2377 return True
2378
2378
2379 for i, node in enumerate(to_run_interactive):
2379 for i, node in enumerate(to_run_interactive):
2380 mod = ast.Interactive([node])
2380 mod = ast.Interactive([node])
2381 code = self.compile(mod, cell_name, "single")
2381 code = self.compile(mod, cell_name, "single")
2382 if self.run_code(code):
2382 if self.run_code(code):
2383 return True
2383 return True
2384 except:
2384 except:
2385 # It's possible to have exceptions raised here, typically by
2385 # It's possible to have exceptions raised here, typically by
2386 # compilation of odd code (such as a naked 'return' outside a
2386 # compilation of odd code (such as a naked 'return' outside a
2387 # function) that did parse but isn't valid. Typically the exception
2387 # function) that did parse but isn't valid. Typically the exception
2388 # is a SyntaxError, but it's safest just to catch anything and show
2388 # is a SyntaxError, but it's safest just to catch anything and show
2389 # the user a traceback.
2389 # the user a traceback.
2390
2390
2391 # We do only one try/except outside the loop to minimize the impact
2391 # We do only one try/except outside the loop to minimize the impact
2392 # on runtime, and also because if any node in the node list is
2392 # on runtime, and also because if any node in the node list is
2393 # broken, we should stop execution completely.
2393 # broken, we should stop execution completely.
2394 self.showtraceback()
2394 self.showtraceback()
2395
2395
2396 return False
2396 return False
2397
2397
2398 def run_code(self, code_obj):
2398 def run_code(self, code_obj):
2399 """Execute a code object.
2399 """Execute a code object.
2400
2400
2401 When an exception occurs, self.showtraceback() is called to display a
2401 When an exception occurs, self.showtraceback() is called to display a
2402 traceback.
2402 traceback.
2403
2403
2404 Parameters
2404 Parameters
2405 ----------
2405 ----------
2406 code_obj : code object
2406 code_obj : code object
2407 A compiled code object, to be executed
2407 A compiled code object, to be executed
2408 post_execute : bool [default: True]
2408 post_execute : bool [default: True]
2409 whether to call post_execute hooks after this particular execution.
2409 whether to call post_execute hooks after this particular execution.
2410
2410
2411 Returns
2411 Returns
2412 -------
2412 -------
2413 False : successful execution.
2413 False : successful execution.
2414 True : an error occurred.
2414 True : an error occurred.
2415 """
2415 """
2416
2416
2417 # Set our own excepthook in case the user code tries to call it
2417 # Set our own excepthook in case the user code tries to call it
2418 # directly, so that the IPython crash handler doesn't get triggered
2418 # directly, so that the IPython crash handler doesn't get triggered
2419 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2419 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2420
2420
2421 # we save the original sys.excepthook in the instance, in case config
2421 # we save the original sys.excepthook in the instance, in case config
2422 # code (such as magics) needs access to it.
2422 # code (such as magics) needs access to it.
2423 self.sys_excepthook = old_excepthook
2423 self.sys_excepthook = old_excepthook
2424 outflag = 1 # happens in more places, so it's easier as default
2424 outflag = 1 # happens in more places, so it's easier as default
2425 try:
2425 try:
2426 try:
2426 try:
2427 self.hooks.pre_run_code_hook()
2427 self.hooks.pre_run_code_hook()
2428 #rprint('Running code', repr(code_obj)) # dbg
2428 #rprint('Running code', repr(code_obj)) # dbg
2429 exec code_obj in self.user_global_ns, self.user_ns
2429 exec code_obj in self.user_global_ns, self.user_ns
2430 finally:
2430 finally:
2431 # Reset our crash handler in place
2431 # Reset our crash handler in place
2432 sys.excepthook = old_excepthook
2432 sys.excepthook = old_excepthook
2433 except SystemExit:
2433 except SystemExit:
2434 self.showtraceback(exception_only=True)
2434 self.showtraceback(exception_only=True)
2435 warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1)
2435 warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1)
2436 except self.custom_exceptions:
2436 except self.custom_exceptions:
2437 etype,value,tb = sys.exc_info()
2437 etype,value,tb = sys.exc_info()
2438 self.CustomTB(etype,value,tb)
2438 self.CustomTB(etype,value,tb)
2439 except:
2439 except:
2440 self.showtraceback()
2440 self.showtraceback()
2441 else:
2441 else:
2442 outflag = 0
2442 outflag = 0
2443 if softspace(sys.stdout, 0):
2443 if softspace(sys.stdout, 0):
2444 print
2444 print
2445
2445
2446 return outflag
2446 return outflag
2447
2447
2448 # For backwards compatibility
2448 # For backwards compatibility
2449 runcode = run_code
2449 runcode = run_code
2450
2450
2451 #-------------------------------------------------------------------------
2451 #-------------------------------------------------------------------------
2452 # Things related to GUI support and pylab
2452 # Things related to GUI support and pylab
2453 #-------------------------------------------------------------------------
2453 #-------------------------------------------------------------------------
2454
2454
2455 def enable_pylab(self, gui=None, import_all=True):
2455 def enable_pylab(self, gui=None, import_all=True):
2456 raise NotImplementedError('Implement enable_pylab in a subclass')
2456 raise NotImplementedError('Implement enable_pylab in a subclass')
2457
2457
2458 #-------------------------------------------------------------------------
2458 #-------------------------------------------------------------------------
2459 # Utilities
2459 # Utilities
2460 #-------------------------------------------------------------------------
2460 #-------------------------------------------------------------------------
2461
2461
2462 def var_expand(self,cmd,depth=0):
2462 def var_expand(self,cmd,depth=0):
2463 """Expand python variables in a string.
2463 """Expand python variables in a string.
2464
2464
2465 The depth argument indicates how many frames above the caller should
2465 The depth argument indicates how many frames above the caller should
2466 be walked to look for the local namespace where to expand variables.
2466 be walked to look for the local namespace where to expand variables.
2467
2467
2468 The global namespace for expansion is always the user's interactive
2468 The global namespace for expansion is always the user's interactive
2469 namespace.
2469 namespace.
2470 """
2470 """
2471 res = ItplNS(cmd, self.user_ns, # globals
2471 res = ItplNS(cmd, self.user_ns, # globals
2472 # Skip our own frame in searching for locals:
2472 # Skip our own frame in searching for locals:
2473 sys._getframe(depth+1).f_locals # locals
2473 sys._getframe(depth+1).f_locals # locals
2474 )
2474 )
2475 return py3compat.str_to_unicode(str(res), res.codec)
2475 return py3compat.str_to_unicode(str(res), res.codec)
2476
2476
2477 def mktempfile(self, data=None, prefix='ipython_edit_'):
2477 def mktempfile(self, data=None, prefix='ipython_edit_'):
2478 """Make a new tempfile and return its filename.
2478 """Make a new tempfile and return its filename.
2479
2479
2480 This makes a call to tempfile.mktemp, but it registers the created
2480 This makes a call to tempfile.mktemp, but it registers the created
2481 filename internally so ipython cleans it up at exit time.
2481 filename internally so ipython cleans it up at exit time.
2482
2482
2483 Optional inputs:
2483 Optional inputs:
2484
2484
2485 - data(None): if data is given, it gets written out to the temp file
2485 - data(None): if data is given, it gets written out to the temp file
2486 immediately, and the file is closed again."""
2486 immediately, and the file is closed again."""
2487
2487
2488 filename = tempfile.mktemp('.py', prefix)
2488 filename = tempfile.mktemp('.py', prefix)
2489 self.tempfiles.append(filename)
2489 self.tempfiles.append(filename)
2490
2490
2491 if data:
2491 if data:
2492 tmp_file = open(filename,'w')
2492 tmp_file = open(filename,'w')
2493 tmp_file.write(data)
2493 tmp_file.write(data)
2494 tmp_file.close()
2494 tmp_file.close()
2495 return filename
2495 return filename
2496
2496
2497 # TODO: This should be removed when Term is refactored.
2497 # TODO: This should be removed when Term is refactored.
2498 def write(self,data):
2498 def write(self,data):
2499 """Write a string to the default output"""
2499 """Write a string to the default output"""
2500 io.stdout.write(data)
2500 io.stdout.write(data)
2501
2501
2502 # TODO: This should be removed when Term is refactored.
2502 # TODO: This should be removed when Term is refactored.
2503 def write_err(self,data):
2503 def write_err(self,data):
2504 """Write a string to the default error output"""
2504 """Write a string to the default error output"""
2505 io.stderr.write(data)
2505 io.stderr.write(data)
2506
2506
2507 def ask_yes_no(self,prompt,default=True):
2507 def ask_yes_no(self,prompt,default=True):
2508 if self.quiet:
2508 if self.quiet:
2509 return True
2509 return True
2510 return ask_yes_no(prompt,default)
2510 return ask_yes_no(prompt,default)
2511
2511
2512 def show_usage(self):
2512 def show_usage(self):
2513 """Show a usage message"""
2513 """Show a usage message"""
2514 page.page(IPython.core.usage.interactive_usage)
2514 page.page(IPython.core.usage.interactive_usage)
2515
2515
2516 def find_user_code(self, target, raw=True):
2516 def find_user_code(self, target, raw=True):
2517 """Get a code string from history, file, or a string or macro.
2517 """Get a code string from history, file, or a string or macro.
2518
2518
2519 This is mainly used by magic functions.
2519 This is mainly used by magic functions.
2520
2520
2521 Parameters
2521 Parameters
2522 ----------
2522 ----------
2523 target : str
2523 target : str
2524 A string specifying code to retrieve. This will be tried respectively
2524 A string specifying code to retrieve. This will be tried respectively
2525 as: ranges of input history (see %history for syntax), a filename, or
2525 as: ranges of input history (see %history for syntax), a filename, or
2526 an expression evaluating to a string or Macro in the user namespace.
2526 an expression evaluating to a string or Macro in the user namespace.
2527 raw : bool
2527 raw : bool
2528 If true (default), retrieve raw history. Has no effect on the other
2528 If true (default), retrieve raw history. Has no effect on the other
2529 retrieval mechanisms.
2529 retrieval mechanisms.
2530
2530
2531 Returns
2531 Returns
2532 -------
2532 -------
2533 A string of code.
2533 A string of code.
2534
2534
2535 ValueError is raised if nothing is found, and TypeError if it evaluates
2535 ValueError is raised if nothing is found, and TypeError if it evaluates
2536 to an object of another type. In each case, .args[0] is a printable
2536 to an object of another type. In each case, .args[0] is a printable
2537 message.
2537 message.
2538 """
2538 """
2539 code = self.extract_input_lines(target, raw=raw) # Grab history
2539 code = self.extract_input_lines(target, raw=raw) # Grab history
2540 if code:
2540 if code:
2541 return code
2541 return code
2542 if os.path.isfile(target): # Read file
2542 if os.path.isfile(target): # Read file
2543 return open(target, "r").read()
2543 return open(target, "r").read()
2544
2544
2545 try: # User namespace
2545 try: # User namespace
2546 codeobj = eval(target, self.user_ns)
2546 codeobj = eval(target, self.user_ns)
2547 except Exception:
2547 except Exception:
2548 raise ValueError(("'%s' was not found in history, as a file, nor in"
2548 raise ValueError(("'%s' was not found in history, as a file, nor in"
2549 " the user namespace.") % target)
2549 " the user namespace.") % target)
2550 if isinstance(codeobj, basestring):
2550 if isinstance(codeobj, basestring):
2551 return codeobj
2551 return codeobj
2552 elif isinstance(codeobj, Macro):
2552 elif isinstance(codeobj, Macro):
2553 return codeobj.value
2553 return codeobj.value
2554
2554
2555 raise TypeError("%s is neither a string nor a macro." % target,
2555 raise TypeError("%s is neither a string nor a macro." % target,
2556 codeobj)
2556 codeobj)
2557
2557
2558 #-------------------------------------------------------------------------
2558 #-------------------------------------------------------------------------
2559 # Things related to IPython exiting
2559 # Things related to IPython exiting
2560 #-------------------------------------------------------------------------
2560 #-------------------------------------------------------------------------
2561 def atexit_operations(self):
2561 def atexit_operations(self):
2562 """This will be executed at the time of exit.
2562 """This will be executed at the time of exit.
2563
2563
2564 Cleanup operations and saving of persistent data that is done
2564 Cleanup operations and saving of persistent data that is done
2565 unconditionally by IPython should be performed here.
2565 unconditionally by IPython should be performed here.
2566
2566
2567 For things that may depend on startup flags or platform specifics (such
2567 For things that may depend on startup flags or platform specifics (such
2568 as having readline or not), register a separate atexit function in the
2568 as having readline or not), register a separate atexit function in the
2569 code that has the appropriate information, rather than trying to
2569 code that has the appropriate information, rather than trying to
2570 clutter
2570 clutter
2571 """
2571 """
2572 # Close the history session (this stores the end time and line count)
2572 # Close the history session (this stores the end time and line count)
2573 # this must be *before* the tempfile cleanup, in case of temporary
2573 # this must be *before* the tempfile cleanup, in case of temporary
2574 # history db
2574 # history db
2575 self.history_manager.end_session()
2575 self.history_manager.end_session()
2576
2576
2577 # Cleanup all tempfiles left around
2577 # Cleanup all tempfiles left around
2578 for tfile in self.tempfiles:
2578 for tfile in self.tempfiles:
2579 try:
2579 try:
2580 os.unlink(tfile)
2580 os.unlink(tfile)
2581 except OSError:
2581 except OSError:
2582 pass
2582 pass
2583
2583
2584 # Clear all user namespaces to release all references cleanly.
2584 # Clear all user namespaces to release all references cleanly.
2585 self.reset(new_session=False)
2585 self.reset(new_session=False)
2586
2586
2587 # Run user hooks
2587 # Run user hooks
2588 self.hooks.shutdown_hook()
2588 self.hooks.shutdown_hook()
2589
2589
2590 def cleanup(self):
2590 def cleanup(self):
2591 self.restore_sys_module_state()
2591 self.restore_sys_module_state()
2592
2592
2593
2593
2594 class InteractiveShellABC(object):
2594 class InteractiveShellABC(object):
2595 """An abstract base class for InteractiveShell."""
2595 """An abstract base class for InteractiveShell."""
2596 __metaclass__ = abc.ABCMeta
2596 __metaclass__ = abc.ABCMeta
2597
2597
2598 InteractiveShellABC.register(InteractiveShell)
2598 InteractiveShellABC.register(InteractiveShell)
@@ -1,217 +1,217 b''
1 """Logger class for IPython's logging facilities.
1 """Logger class for IPython's logging facilities.
2 """
2 """
3
3
4 #*****************************************************************************
4 #*****************************************************************************
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
6 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
6 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
7 #
7 #
8 # Distributed under the terms of the BSD License. The full license is in
8 # Distributed under the terms of the BSD License. The full license is in
9 # the file COPYING, distributed as part of this software.
9 # the file COPYING, distributed as part of this software.
10 #*****************************************************************************
10 #*****************************************************************************
11
11
12 #****************************************************************************
12 #****************************************************************************
13 # Modules and globals
13 # Modules and globals
14
14
15 # Python standard modules
15 # Python standard modules
16 import glob
16 import glob
17 import os
17 import os
18 import time
18 import time
19
19
20 #****************************************************************************
20 #****************************************************************************
21 # FIXME: This class isn't a mixin anymore, but it still needs attributes from
21 # FIXME: This class isn't a mixin anymore, but it still needs attributes from
22 # ipython and does input cache management. Finish cleanup later...
22 # ipython and does input cache management. Finish cleanup later...
23
23
24 class Logger(object):
24 class Logger(object):
25 """A Logfile class with different policies for file creation"""
25 """A Logfile class with different policies for file creation"""
26
26
27 def __init__(self, home_dir, logfname='Logger.log', loghead='',
27 def __init__(self, home_dir, logfname='Logger.log', loghead='',
28 logmode='over'):
28 logmode='over'):
29
29
30 # this is the full ipython instance, we need some attributes from it
30 # this is the full ipython instance, we need some attributes from it
31 # which won't exist until later. What a mess, clean up later...
31 # which won't exist until later. What a mess, clean up later...
32 self.home_dir = home_dir
32 self.home_dir = home_dir
33
33
34 self.logfname = logfname
34 self.logfname = logfname
35 self.loghead = loghead
35 self.loghead = loghead
36 self.logmode = logmode
36 self.logmode = logmode
37 self.logfile = None
37 self.logfile = None
38
38
39 # Whether to log raw or processed input
39 # Whether to log raw or processed input
40 self.log_raw_input = False
40 self.log_raw_input = False
41
41
42 # whether to also log output
42 # whether to also log output
43 self.log_output = False
43 self.log_output = False
44
44
45 # whether to put timestamps before each log entry
45 # whether to put timestamps before each log entry
46 self.timestamp = False
46 self.timestamp = False
47
47
48 # activity control flags
48 # activity control flags
49 self.log_active = False
49 self.log_active = False
50
50
51 # logmode is a validated property
51 # logmode is a validated property
52 def _set_mode(self,mode):
52 def _set_mode(self,mode):
53 if mode not in ['append','backup','global','over','rotate']:
53 if mode not in ['append','backup','global','over','rotate']:
54 raise ValueError,'invalid log mode %s given' % mode
54 raise ValueError,'invalid log mode %s given' % mode
55 self._logmode = mode
55 self._logmode = mode
56
56
57 def _get_mode(self):
57 def _get_mode(self):
58 return self._logmode
58 return self._logmode
59
59
60 logmode = property(_get_mode,_set_mode)
60 logmode = property(_get_mode,_set_mode)
61
61
62 def logstart(self,logfname=None,loghead=None,logmode=None,
62 def logstart(self,logfname=None,loghead=None,logmode=None,
63 log_output=False,timestamp=False,log_raw_input=False):
63 log_output=False,timestamp=False,log_raw_input=False):
64 """Generate a new log-file with a default header.
64 """Generate a new log-file with a default header.
65
65
66 Raises RuntimeError if the log has already been started"""
66 Raises RuntimeError if the log has already been started"""
67
67
68 if self.logfile is not None:
68 if self.logfile is not None:
69 raise RuntimeError('Log file is already active: %s' %
69 raise RuntimeError('Log file is already active: %s' %
70 self.logfname)
70 self.logfname)
71
71
72 # The parameters can override constructor defaults
72 # The parameters can override constructor defaults
73 if logfname is not None: self.logfname = logfname
73 if logfname is not None: self.logfname = logfname
74 if loghead is not None: self.loghead = loghead
74 if loghead is not None: self.loghead = loghead
75 if logmode is not None: self.logmode = logmode
75 if logmode is not None: self.logmode = logmode
76
76
77 # Parameters not part of the constructor
77 # Parameters not part of the constructor
78 self.timestamp = timestamp
78 self.timestamp = timestamp
79 self.log_output = log_output
79 self.log_output = log_output
80 self.log_raw_input = log_raw_input
80 self.log_raw_input = log_raw_input
81
81
82 # init depending on the log mode requested
82 # init depending on the log mode requested
83 isfile = os.path.isfile
83 isfile = os.path.isfile
84 logmode = self.logmode
84 logmode = self.logmode
85
85
86 if logmode == 'append':
86 if logmode == 'append':
87 self.logfile = open(self.logfname,'a')
87 self.logfile = open(self.logfname,'a')
88
88
89 elif logmode == 'backup':
89 elif logmode == 'backup':
90 if isfile(self.logfname):
90 if isfile(self.logfname):
91 backup_logname = self.logfname+'~'
91 backup_logname = self.logfname+'~'
92 # Manually remove any old backup, since os.rename may fail
92 # Manually remove any old backup, since os.rename may fail
93 # under Windows.
93 # under Windows.
94 if isfile(backup_logname):
94 if isfile(backup_logname):
95 os.remove(backup_logname)
95 os.remove(backup_logname)
96 os.rename(self.logfname,backup_logname)
96 os.rename(self.logfname,backup_logname)
97 self.logfile = open(self.logfname,'w')
97 self.logfile = open(self.logfname,'w')
98
98
99 elif logmode == 'global':
99 elif logmode == 'global':
100 self.logfname = os.path.join(self.home_dir,self.logfname)
100 self.logfname = os.path.join(self.home_dir,self.logfname)
101 self.logfile = open(self.logfname, 'a')
101 self.logfile = open(self.logfname, 'a')
102
102
103 elif logmode == 'over':
103 elif logmode == 'over':
104 if isfile(self.logfname):
104 if isfile(self.logfname):
105 os.remove(self.logfname)
105 os.remove(self.logfname)
106 self.logfile = open(self.logfname,'w')
106 self.logfile = open(self.logfname,'w')
107
107
108 elif logmode == 'rotate':
108 elif logmode == 'rotate':
109 if isfile(self.logfname):
109 if isfile(self.logfname):
110 if isfile(self.logfname+'.001~'):
110 if isfile(self.logfname+'.001~'):
111 old = glob.glob(self.logfname+'.*~')
111 old = glob.glob(self.logfname+'.*~')
112 old.sort()
112 old.sort()
113 old.reverse()
113 old.reverse()
114 for f in old:
114 for f in old:
115 root, ext = os.path.splitext(f)
115 root, ext = os.path.splitext(f)
116 num = int(ext[1:-1])+1
116 num = int(ext[1:-1])+1
117 os.rename(f, root+'.'+`num`.zfill(3)+'~')
117 os.rename(f, root+'.'+`num`.zfill(3)+'~')
118 os.rename(self.logfname, self.logfname+'.001~')
118 os.rename(self.logfname, self.logfname+'.001~')
119 self.logfile = open(self.logfname,'w')
119 self.logfile = open(self.logfname,'w')
120
120
121 if logmode != 'append':
121 if logmode != 'append':
122 self.logfile.write(self.loghead)
122 self.logfile.write(self.loghead)
123
123
124 self.logfile.flush()
124 self.logfile.flush()
125 self.log_active = True
125 self.log_active = True
126
126
127 def switch_log(self,val):
127 def switch_log(self,val):
128 """Switch logging on/off. val should be ONLY a boolean."""
128 """Switch logging on/off. val should be ONLY a boolean."""
129
129
130 if val not in [False,True,0,1]:
130 if val not in [False,True,0,1]:
131 raise ValueError, \
131 raise ValueError, \
132 'Call switch_log ONLY with a boolean argument, not with:',val
132 'Call switch_log ONLY with a boolean argument, not with:',val
133
133
134 label = {0:'OFF',1:'ON',False:'OFF',True:'ON'}
134 label = {0:'OFF',1:'ON',False:'OFF',True:'ON'}
135
135
136 if self.logfile is None:
136 if self.logfile is None:
137 print """
137 print """
138 Logging hasn't been started yet (use logstart for that).
138 Logging hasn't been started yet (use logstart for that).
139
139
140 %logon/%logoff are for temporarily starting and stopping logging for a logfile
140 %logon/%logoff are for temporarily starting and stopping logging for a logfile
141 which already exists. But you must first start the logging process with
141 which already exists. But you must first start the logging process with
142 %logstart (optionally giving a logfile name)."""
142 %logstart (optionally giving a logfile name)."""
143
143
144 else:
144 else:
145 if self.log_active == val:
145 if self.log_active == val:
146 print 'Logging is already',label[val]
146 print 'Logging is already',label[val]
147 else:
147 else:
148 print 'Switching logging',label[val]
148 print 'Switching logging',label[val]
149 self.log_active = not self.log_active
149 self.log_active = not self.log_active
150 self.log_active_out = self.log_active
150 self.log_active_out = self.log_active
151
151
152 def logstate(self):
152 def logstate(self):
153 """Print a status message about the logger."""
153 """Print a status message about the logger."""
154 if self.logfile is None:
154 if self.logfile is None:
155 print 'Logging has not been activated.'
155 print 'Logging has not been activated.'
156 else:
156 else:
157 state = self.log_active and 'active' or 'temporarily suspended'
157 state = self.log_active and 'active' or 'temporarily suspended'
158 print 'Filename :',self.logfname
158 print 'Filename :',self.logfname
159 print 'Mode :',self.logmode
159 print 'Mode :',self.logmode
160 print 'Output logging :',self.log_output
160 print 'Output logging :',self.log_output
161 print 'Raw input log :',self.log_raw_input
161 print 'Raw input log :',self.log_raw_input
162 print 'Timestamping :',self.timestamp
162 print 'Timestamping :',self.timestamp
163 print 'State :',state
163 print 'State :',state
164
164
165 def log(self, line_mod, line_ori):
165 def log(self, line_mod, line_ori):
166 """Write the sources to a log.
166 """Write the sources to a log.
167
167
168 Inputs:
168 Inputs:
169
169
170 - line_mod: possibly modified input, such as the transformations made
170 - line_mod: possibly modified input, such as the transformations made
171 by input prefilters or input handlers of various kinds. This should
171 by input prefilters or input handlers of various kinds. This should
172 always be valid Python.
172 always be valid Python.
173
173
174 - line_ori: unmodified input line from the user. This is not
174 - line_ori: unmodified input line from the user. This is not
175 necessarily valid Python.
175 necessarily valid Python.
176 """
176 """
177
177
178 # Write the log line, but decide which one according to the
178 # Write the log line, but decide which one according to the
179 # log_raw_input flag, set when the log is started.
179 # log_raw_input flag, set when the log is started.
180 if self.log_raw_input:
180 if self.log_raw_input:
181 self.log_write(line_ori)
181 self.log_write(line_ori)
182 else:
182 else:
183 self.log_write(line_mod)
183 self.log_write(line_mod)
184
184
185 def log_write(self, data, kind='input'):
185 def log_write(self, data, kind='input'):
186 """Write data to the log file, if active"""
186 """Write data to the log file, if active"""
187
187
188 #print 'data: %r' % data # dbg
188 #print 'data: %r' % data # dbg
189 if self.log_active and data:
189 if self.log_active and data:
190 write = self.logfile.write
190 write = self.logfile.write
191 if kind=='input':
191 if kind=='input':
192 if self.timestamp:
192 if self.timestamp:
193 write(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
193 write(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
194 time.localtime()))
194 time.localtime()))
195 write(data)
195 write(data)
196 elif kind=='output' and self.log_output:
196 elif kind=='output' and self.log_output:
197 odata = '\n'.join(['#[Out]# %s' % s
197 odata = '\n'.join(['#[Out]# %s' % s
198 for s in data.splitlines()])
198 for s in data.splitlines()])
199 write('%s\n' % odata)
199 write('%s\n' % odata)
200 self.logfile.flush()
200 self.logfile.flush()
201
201
202 def logstop(self):
202 def logstop(self):
203 """Fully stop logging and close log file.
203 """Fully stop logging and close log file.
204
204
205 In order to start logging again, a new logstart() call needs to be
205 In order to start logging again, a new logstart() call needs to be
206 made, possibly (though not necessarily) with a new filename, mode and
206 made, possibly (though not necessarily) with a new filename, mode and
207 other options."""
207 other options."""
208
208
209 if self.logfile is not None:
209 if self.logfile is not None:
210 self.logfile.close()
210 self.logfile.close()
211 self.logfile = None
211 self.logfile = None
212 else:
212 else:
213 print "Logging hadn't been started."
213 print "Logging hadn't been started."
214 self.log_active = False
214 self.log_active = False
215
215
216 # For backwards compatibility, in case anyone was using this.
216 # For backwards compatibility, in case anyone was using this.
217 close_log = logstop
217 close_log = logstop
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now