Show More
The requested changes are too big and content was truncated. Show full diff
@@ -1,436 +1,436 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | A base class for a configurable application. |
|
4 | 4 | |
|
5 | 5 | Authors: |
|
6 | 6 | |
|
7 | 7 | * Brian Granger |
|
8 | 8 | * Min RK |
|
9 | 9 | """ |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Copyright (C) 2008-2011 The IPython Development Team |
|
13 | 13 | # |
|
14 | 14 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | 15 | # the file COPYING, distributed as part of this software. |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | # Imports |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | |
|
22 | 22 | import logging |
|
23 | 23 | import os |
|
24 | 24 | import re |
|
25 | 25 | import sys |
|
26 | 26 | from copy import deepcopy |
|
27 | 27 | |
|
28 | 28 | from IPython.config.configurable import SingletonConfigurable |
|
29 | 29 | from IPython.config.loader import ( |
|
30 | 30 | KVArgParseConfigLoader, PyFileConfigLoader, Config, ArgumentError |
|
31 | 31 | ) |
|
32 | 32 | |
|
33 | 33 | from IPython.utils.traitlets import ( |
|
34 | 34 | Unicode, List, Int, Enum, Dict, Instance, TraitError |
|
35 | 35 | ) |
|
36 | 36 | from IPython.utils.importstring import import_item |
|
37 | 37 | from IPython.utils.text import indent, wrap_paragraphs, dedent |
|
38 | 38 | |
|
39 | 39 | #----------------------------------------------------------------------------- |
|
40 | 40 | # function for re-wrapping a helpstring |
|
41 | 41 | #----------------------------------------------------------------------------- |
|
42 | 42 | |
|
43 | 43 | #----------------------------------------------------------------------------- |
|
44 | 44 | # Descriptions for the various sections |
|
45 | 45 | #----------------------------------------------------------------------------- |
|
46 | 46 | |
|
47 | 47 | # merge flags&aliases into options |
|
48 | 48 | option_description = """ |
|
49 | 49 | Arguments that take values are actually convenience aliases to full |
|
50 | 50 | Configurables, whose aliases are listed on the help line. For more information |
|
51 | 51 | on full configurables, see '--help-all'. |
|
52 | 52 | """.strip() # trim newlines of front and back |
|
53 | 53 | |
|
54 | 54 | keyvalue_description = """ |
|
55 | 55 | Parameters are set from command-line arguments of the form: |
|
56 | 56 | `--Class.trait=value`. |
|
57 | 57 | This line is evaluated in Python, so simple expressions are allowed, e.g.:: |
|
58 | 58 | `--C.a='range(3)'` For setting C.a=[0,1,2]. |
|
59 | 59 | """.strip() # trim newlines of front and back |
|
60 | 60 | |
|
61 | 61 | subcommand_description = """ |
|
62 | 62 | Subcommands are launched as `{app} cmd [args]`. For information on using |
|
63 | 63 | subcommand 'cmd', do: `{app} cmd -h`. |
|
64 | 64 | """.strip().format(app=os.path.basename(sys.argv[0])) |
|
65 | 65 | # get running program name |
|
66 | 66 | |
|
67 | 67 | #----------------------------------------------------------------------------- |
|
68 | 68 | # Application class |
|
69 | 69 | #----------------------------------------------------------------------------- |
|
70 | 70 | |
|
71 | 71 | |
|
72 | 72 | class ApplicationError(Exception): |
|
73 | 73 | pass |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | class Application(SingletonConfigurable): |
|
77 | 77 | """A singleton application with full configuration support.""" |
|
78 | 78 | |
|
79 | 79 | # The name of the application, will usually match the name of the command |
|
80 | 80 | # line application |
|
81 | 81 | name = Unicode(u'application') |
|
82 | 82 | |
|
83 | 83 | # The description of the application that is printed at the beginning |
|
84 | 84 | # of the help. |
|
85 | 85 | description = Unicode(u'This is an application.') |
|
86 | 86 | # default section descriptions |
|
87 | 87 | option_description = Unicode(option_description) |
|
88 | 88 | keyvalue_description = Unicode(keyvalue_description) |
|
89 | 89 | subcommand_description = Unicode(subcommand_description) |
|
90 | 90 | |
|
91 | 91 | # The usage and example string that goes at the end of the help string. |
|
92 | 92 | examples = Unicode() |
|
93 | 93 | |
|
94 | 94 | # A sequence of Configurable subclasses whose config=True attributes will |
|
95 | 95 | # be exposed at the command line. |
|
96 | 96 | classes = List([]) |
|
97 | 97 | |
|
98 | 98 | # The version string of this application. |
|
99 | 99 | version = Unicode(u'0.0') |
|
100 | 100 | |
|
101 | 101 | # The log level for the application |
|
102 | 102 | log_level = Enum((0,10,20,30,40,50,'DEBUG','INFO','WARN','ERROR','CRITICAL'), |
|
103 | 103 | default_value=logging.WARN, |
|
104 | 104 | config=True, |
|
105 | 105 | help="Set the log level by value or name.") |
|
106 | 106 | def _log_level_changed(self, name, old, new): |
|
107 | 107 | """Adjust the log level when log_level is set.""" |
|
108 | 108 | if isinstance(new, basestring): |
|
109 | 109 | new = getattr(logging, new) |
|
110 | 110 | self.log_level = new |
|
111 | 111 | self.log.setLevel(new) |
|
112 | ||
|
112 | ||
|
113 | 113 | # the alias map for configurables |
|
114 | 114 | aliases = Dict({'log-level' : 'Application.log_level'}) |
|
115 | ||
|
115 | ||
|
116 | 116 | # flags for loading Configurables or store_const style flags |
|
117 | 117 | # flags are loaded from this dict by '--key' flags |
|
118 | 118 | # this must be a dict of two-tuples, the first element being the Config/dict |
|
119 | 119 | # and the second being the help string for the flag |
|
120 | 120 | flags = Dict() |
|
121 | 121 | def _flags_changed(self, name, old, new): |
|
122 | 122 | """ensure flags dict is valid""" |
|
123 | 123 | for key,value in new.iteritems(): |
|
124 | 124 | assert len(value) == 2, "Bad flag: %r:%s"%(key,value) |
|
125 | 125 | assert isinstance(value[0], (dict, Config)), "Bad flag: %r:%s"%(key,value) |
|
126 | 126 | assert isinstance(value[1], basestring), "Bad flag: %r:%s"%(key,value) |
|
127 | ||
|
128 | ||
|
127 | ||
|
128 | ||
|
129 | 129 | # subcommands for launching other applications |
|
130 | 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 | 132 | # the first element being the application class/import string |
|
133 | 133 | # and the second being the help string for the subcommand |
|
134 | 134 | subcommands = Dict() |
|
135 | 135 | # parse_command_line will initialize a subapp, if requested |
|
136 | 136 | subapp = Instance('IPython.config.application.Application', allow_none=True) |
|
137 | ||
|
137 | ||
|
138 | 138 | # extra command-line arguments that don't set config values |
|
139 | 139 | extra_args = List(Unicode) |
|
140 | ||
|
140 | ||
|
141 | 141 | |
|
142 | 142 | def __init__(self, **kwargs): |
|
143 | 143 | SingletonConfigurable.__init__(self, **kwargs) |
|
144 | 144 | # Ensure my class is in self.classes, so my attributes appear in command line |
|
145 | 145 | # options and config files. |
|
146 | 146 | if self.__class__ not in self.classes: |
|
147 | 147 | self.classes.insert(0, self.__class__) |
|
148 | ||
|
148 | ||
|
149 | 149 | self.init_logging() |
|
150 | 150 | |
|
151 | 151 | def _config_changed(self, name, old, new): |
|
152 | 152 | SingletonConfigurable._config_changed(self, name, old, new) |
|
153 | 153 | self.log.debug('Config changed:') |
|
154 | 154 | self.log.debug(repr(new)) |
|
155 | 155 | |
|
156 | 156 | def init_logging(self): |
|
157 | 157 | """Start logging for this application. |
|
158 | 158 | |
|
159 | 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 | 161 | ``log_level`` attribute. |
|
162 | 162 | """ |
|
163 | 163 | self.log = logging.getLogger(self.__class__.__name__) |
|
164 | 164 | self.log.setLevel(self.log_level) |
|
165 | 165 | if sys.executable.endswith('pythonw.exe'): |
|
166 | 166 | # this should really go to a file, but file-logging is only |
|
167 | 167 | # hooked up in parallel applications |
|
168 | 168 | self._log_handler = logging.StreamHandler(open(os.devnull, 'w')) |
|
169 | 169 | else: |
|
170 | 170 | self._log_handler = logging.StreamHandler() |
|
171 | 171 | self._log_formatter = logging.Formatter("[%(name)s] %(message)s") |
|
172 | 172 | self._log_handler.setFormatter(self._log_formatter) |
|
173 | 173 | self.log.addHandler(self._log_handler) |
|
174 | 174 | |
|
175 | 175 | def initialize(self, argv=None): |
|
176 | 176 | """Do the basic steps to configure me. |
|
177 | ||
|
177 | ||
|
178 | 178 | Override in subclasses. |
|
179 | 179 | """ |
|
180 | 180 | self.parse_command_line(argv) |
|
181 | ||
|
182 | ||
|
181 | ||
|
182 | ||
|
183 | 183 | def start(self): |
|
184 | 184 | """Start the app mainloop. |
|
185 | ||
|
185 | ||
|
186 | 186 | Override in subclasses. |
|
187 | 187 | """ |
|
188 | 188 | if self.subapp is not None: |
|
189 | 189 | return self.subapp.start() |
|
190 | ||
|
190 | ||
|
191 | 191 | def print_alias_help(self): |
|
192 | 192 | """Print the alias part of the help.""" |
|
193 | 193 | if not self.aliases: |
|
194 | 194 | return |
|
195 | ||
|
195 | ||
|
196 | 196 | lines = [] |
|
197 | 197 | classdict = {} |
|
198 | 198 | for cls in self.classes: |
|
199 | 199 | # include all parents (up to, but excluding Configurable) in available names |
|
200 | 200 | for c in cls.mro()[:-3]: |
|
201 | 201 | classdict[c.__name__] = c |
|
202 | ||
|
202 | ||
|
203 | 203 | for alias, longname in self.aliases.iteritems(): |
|
204 | 204 | classname, traitname = longname.split('.',1) |
|
205 | 205 | cls = classdict[classname] |
|
206 | ||
|
206 | ||
|
207 | 207 | trait = cls.class_traits(config=True)[traitname] |
|
208 | 208 | help = cls.class_get_trait_help(trait).splitlines() |
|
209 | 209 | # reformat first line |
|
210 | 210 | help[0] = help[0].replace(longname, alias) + ' (%s)'%longname |
|
211 | 211 | if len(alias) == 1: |
|
212 | 212 | help[0] = help[0].replace('--%s='%alias, '-%s '%alias) |
|
213 | 213 | lines.extend(help) |
|
214 | 214 | # lines.append('') |
|
215 | 215 | print os.linesep.join(lines) |
|
216 | ||
|
216 | ||
|
217 | 217 | def print_flag_help(self): |
|
218 | 218 | """Print the flag part of the help.""" |
|
219 | 219 | if not self.flags: |
|
220 | 220 | return |
|
221 | ||
|
221 | ||
|
222 | 222 | lines = [] |
|
223 | 223 | for m, (cfg,help) in self.flags.iteritems(): |
|
224 | 224 | prefix = '--' if len(m) > 1 else '-' |
|
225 | 225 | lines.append(prefix+m) |
|
226 | 226 | lines.append(indent(dedent(help.strip()))) |
|
227 | 227 | # lines.append('') |
|
228 | 228 | print os.linesep.join(lines) |
|
229 | ||
|
229 | ||
|
230 | 230 | def print_options(self): |
|
231 | 231 | if not self.flags and not self.aliases: |
|
232 | 232 | return |
|
233 | 233 | lines = ['Options'] |
|
234 | 234 | lines.append('-'*len(lines[0])) |
|
235 | 235 | lines.append('') |
|
236 | 236 | for p in wrap_paragraphs(self.option_description): |
|
237 | 237 | lines.append(p) |
|
238 | 238 | lines.append('') |
|
239 | 239 | print os.linesep.join(lines) |
|
240 | 240 | self.print_flag_help() |
|
241 | 241 | self.print_alias_help() |
|
242 | 242 | |
|
243 | ||
|
243 | ||
|
244 | 244 | def print_subcommands(self): |
|
245 | 245 | """Print the subcommand part of the help.""" |
|
246 | 246 | if not self.subcommands: |
|
247 | 247 | return |
|
248 | ||
|
248 | ||
|
249 | 249 | lines = ["Subcommands"] |
|
250 | 250 | lines.append('-'*len(lines[0])) |
|
251 | 251 | lines.append('') |
|
252 | 252 | for p in wrap_paragraphs(self.subcommand_description): |
|
253 | 253 | lines.append(p) |
|
254 | 254 | lines.append('') |
|
255 | 255 | for subc, (cls, help) in self.subcommands.iteritems(): |
|
256 | 256 | lines.append(subc) |
|
257 | 257 | if help: |
|
258 | 258 | lines.append(indent(dedent(help.strip()))) |
|
259 | 259 | lines.append('') |
|
260 | 260 | print os.linesep.join(lines) |
|
261 | ||
|
261 | ||
|
262 | 262 | def print_help(self, classes=False): |
|
263 | 263 | """Print the help for each Configurable class in self.classes. |
|
264 | ||
|
264 | ||
|
265 | 265 | If classes=False (the default), only flags and aliases are printed. |
|
266 | 266 | """ |
|
267 | 267 | self.print_subcommands() |
|
268 | 268 | self.print_options() |
|
269 | ||
|
269 | ||
|
270 | 270 | if classes: |
|
271 | 271 | if self.classes: |
|
272 | 272 | print "Class parameters" |
|
273 | 273 | print "----------------" |
|
274 | 274 | |
|
275 | 275 | for p in wrap_paragraphs(self.keyvalue_description): |
|
276 | 276 | print p |
|
277 | 277 | |
|
278 | ||
|
278 | ||
|
279 | 279 | for cls in self.classes: |
|
280 | 280 | cls.class_print_help() |
|
281 | 281 | |
|
282 | 282 | else: |
|
283 | 283 | print "To see all available configurables, use `--help-all`" |
|
284 | 284 | |
|
285 | 285 | |
|
286 | 286 | def print_description(self): |
|
287 | 287 | """Print the application description.""" |
|
288 | 288 | for p in wrap_paragraphs(self.description): |
|
289 | 289 | print p |
|
290 | 290 | |
|
291 | 291 | |
|
292 | 292 | def print_examples(self): |
|
293 | 293 | """Print usage and examples. |
|
294 | 294 | |
|
295 | 295 | This usage string goes at the end of the command line help string |
|
296 | 296 | and should contain examples of the application's usage. |
|
297 | 297 | """ |
|
298 | 298 | if self.examples: |
|
299 | 299 | print "Examples" |
|
300 | 300 | print "--------" |
|
301 | 301 | |
|
302 | 302 | print indent(dedent(self.examples.strip())) |
|
303 | 303 | |
|
304 | 304 | |
|
305 | 305 | def print_version(self): |
|
306 | 306 | """Print the version string.""" |
|
307 | 307 | print self.version |
|
308 | 308 | |
|
309 | 309 | def update_config(self, config): |
|
310 | 310 | """Fire the traits events when the config is updated.""" |
|
311 | 311 | # Save a copy of the current config. |
|
312 | 312 | newconfig = deepcopy(self.config) |
|
313 | 313 | # Merge the new config into the current one. |
|
314 | 314 | newconfig._merge(config) |
|
315 | 315 | # Save the combined config as self.config, which triggers the traits |
|
316 | 316 | # events. |
|
317 | 317 | self.config = newconfig |
|
318 | ||
|
318 | ||
|
319 | 319 | def initialize_subcommand(self, subc, argv=None): |
|
320 | 320 | """Initialize a subcommand with argv.""" |
|
321 | 321 | subapp,help = self.subcommands.get(subc) |
|
322 | ||
|
322 | ||
|
323 | 323 | if isinstance(subapp, basestring): |
|
324 | 324 | subapp = import_item(subapp) |
|
325 | ||
|
325 | ||
|
326 | 326 | # clear existing instances |
|
327 | 327 | self.__class__.clear_instance() |
|
328 | 328 | # instantiate |
|
329 | 329 | self.subapp = subapp.instance() |
|
330 | 330 | # and initialize subapp |
|
331 | 331 | self.subapp.initialize(argv) |
|
332 | ||
|
332 | ||
|
333 | 333 | def parse_command_line(self, argv=None): |
|
334 | 334 | """Parse the command line arguments.""" |
|
335 | 335 | argv = sys.argv[1:] if argv is None else argv |
|
336 | 336 | |
|
337 | 337 | if self.subcommands and len(argv) > 0: |
|
338 | 338 | # we have subcommands, and one may have been specified |
|
339 | 339 | subc, subargv = argv[0], argv[1:] |
|
340 | 340 | if re.match(r'^\w(\-?\w)*$', subc) and subc in self.subcommands: |
|
341 | 341 | # it's a subcommand, and *not* a flag or class parameter |
|
342 | 342 | return self.initialize_subcommand(subc, subargv) |
|
343 | ||
|
343 | ||
|
344 | 344 | if '-h' in argv or '--help' in argv or '--help-all' in argv: |
|
345 | 345 | self.print_description() |
|
346 | 346 | self.print_help('--help-all' in argv) |
|
347 | 347 | self.print_examples() |
|
348 | 348 | self.exit(0) |
|
349 | 349 | |
|
350 | 350 | if '--version' in argv: |
|
351 | 351 | self.print_version() |
|
352 | 352 | self.exit(0) |
|
353 | ||
|
353 | ||
|
354 | 354 | loader = KVArgParseConfigLoader(argv=argv, aliases=self.aliases, |
|
355 | 355 | flags=self.flags) |
|
356 | 356 | try: |
|
357 | 357 | config = loader.load_config() |
|
358 | 358 | self.update_config(config) |
|
359 | 359 | except (TraitError, ArgumentError) as e: |
|
360 | 360 | self.print_description() |
|
361 | 361 | self.print_help() |
|
362 | 362 | self.print_examples() |
|
363 | 363 | self.log.fatal(str(e)) |
|
364 | 364 | self.exit(1) |
|
365 | 365 | # store unparsed args in extra_args |
|
366 | 366 | self.extra_args = loader.extra_args |
|
367 | 367 | |
|
368 | 368 | def load_config_file(self, filename, path=None): |
|
369 | 369 | """Load a .py based config file by filename and path.""" |
|
370 | 370 | loader = PyFileConfigLoader(filename, path=path) |
|
371 | 371 | try: |
|
372 | 372 | config = loader.load_config() |
|
373 | 373 | except IOError: |
|
374 | 374 | # problem with the file (probably doesn't exist), raise |
|
375 | 375 | raise |
|
376 | 376 | except Exception: |
|
377 | 377 | # try to get the full filename, but it will be empty in the |
|
378 | 378 | # unlikely event that the error raised before filefind finished |
|
379 | 379 | filename = loader.full_filename or filename |
|
380 | 380 | # problem while running the file |
|
381 | 381 | self.log.error("Exception while loading config file %s", |
|
382 | 382 | filename, exc_info=True) |
|
383 | 383 | else: |
|
384 | 384 | self.log.debug("Loaded config file: %s", loader.full_filename) |
|
385 | 385 | self.update_config(config) |
|
386 | ||
|
386 | ||
|
387 | 387 | def generate_config_file(self): |
|
388 | 388 | """generate default config file from Configurables""" |
|
389 | 389 | lines = ["# Configuration file for %s."%self.name] |
|
390 | 390 | lines.append('') |
|
391 | 391 | lines.append('c = get_config()') |
|
392 | 392 | lines.append('') |
|
393 | 393 | for cls in self.classes: |
|
394 | 394 | lines.append(cls.class_config_section()) |
|
395 | 395 | return '\n'.join(lines) |
|
396 | 396 | |
|
397 | 397 | def exit(self, exit_status=0): |
|
398 | 398 | self.log.debug("Exiting application: %s" % self.name) |
|
399 | 399 | sys.exit(exit_status) |
|
400 | 400 | |
|
401 | 401 | #----------------------------------------------------------------------------- |
|
402 | 402 | # utility functions, for convenience |
|
403 | 403 | #----------------------------------------------------------------------------- |
|
404 | 404 | |
|
405 | 405 | def boolean_flag(name, configurable, set_help='', unset_help=''): |
|
406 | 406 | """Helper for building basic --trait, --no-trait flags. |
|
407 | ||
|
407 | ||
|
408 | 408 | Parameters |
|
409 | 409 | ---------- |
|
410 | ||
|
410 | ||
|
411 | 411 | name : str |
|
412 | 412 | The name of the flag. |
|
413 | 413 | configurable : str |
|
414 | 414 | The 'Class.trait' string of the trait to be set/unset with the flag |
|
415 | 415 | set_help : unicode |
|
416 | 416 | help string for --name flag |
|
417 | 417 | unset_help : unicode |
|
418 | 418 | help string for --no-name flag |
|
419 | ||
|
419 | ||
|
420 | 420 | Returns |
|
421 | 421 | ------- |
|
422 | ||
|
422 | ||
|
423 | 423 | cfg : dict |
|
424 | 424 | A dict with two keys: 'name', and 'no-name', for setting and unsetting |
|
425 | 425 | the trait, respectively. |
|
426 | 426 | """ |
|
427 | 427 | # default helpstrings |
|
428 | 428 | set_help = set_help or "set %s=True"%configurable |
|
429 | 429 | unset_help = unset_help or "set %s=False"%configurable |
|
430 | ||
|
430 | ||
|
431 | 431 | cls,trait = configurable.split('.') |
|
432 | ||
|
432 | ||
|
433 | 433 | setter = {cls : {trait : True}} |
|
434 | 434 | unsetter = {cls : {trait : False}} |
|
435 | 435 | return {name : (setter, set_help), 'no-'+name : (unsetter, unset_help)} |
|
436 | 436 |
@@ -1,328 +1,328 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | A base class for objects that are configurable. |
|
4 | 4 | |
|
5 | 5 | Authors: |
|
6 | 6 | |
|
7 | 7 | * Brian Granger |
|
8 | 8 | * Fernando Perez |
|
9 | 9 | * Min RK |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Copyright (C) 2008-2011 The IPython Development Team |
|
14 | 14 | # |
|
15 | 15 | # Distributed under the terms of the BSD License. The full license is in |
|
16 | 16 | # the file COPYING, distributed as part of this software. |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Imports |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | |
|
23 | 23 | import datetime |
|
24 | 24 | from copy import deepcopy |
|
25 | 25 | |
|
26 | 26 | from loader import Config |
|
27 | 27 | from IPython.utils.traitlets import HasTraits, Instance |
|
28 | 28 | from IPython.utils.text import indent, wrap_paragraphs |
|
29 | 29 | |
|
30 | 30 | |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | # Helper classes for Configurables |
|
33 | 33 | #----------------------------------------------------------------------------- |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | class ConfigurableError(Exception): |
|
37 | 37 | pass |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | class MultipleInstanceError(ConfigurableError): |
|
41 | 41 | pass |
|
42 | 42 | |
|
43 | 43 | #----------------------------------------------------------------------------- |
|
44 | 44 | # Configurable implementation |
|
45 | 45 | #----------------------------------------------------------------------------- |
|
46 | 46 | |
|
47 | 47 | class Configurable(HasTraits): |
|
48 | 48 | |
|
49 | 49 | config = Instance(Config,(),{}) |
|
50 | 50 | created = None |
|
51 | 51 | |
|
52 | 52 | def __init__(self, **kwargs): |
|
53 | 53 | """Create a configurable given a config config. |
|
54 | 54 | |
|
55 | 55 | Parameters |
|
56 | 56 | ---------- |
|
57 | 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 | 59 | :class:`Config` instance, it will be used to configure the |
|
60 | 60 | instance. |
|
61 | ||
|
61 | ||
|
62 | 62 | Notes |
|
63 | 63 | ----- |
|
64 | 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 | 66 | :func:`super`:: |
|
67 | ||
|
67 | ||
|
68 | 68 | class MyConfigurable(Configurable): |
|
69 | 69 | def __init__(self, config=None): |
|
70 | 70 | super(MyConfigurable, self).__init__(config) |
|
71 | 71 | # Then any other code you need to finish initialization. |
|
72 | 72 | |
|
73 | 73 | This ensures that instances will be configured properly. |
|
74 | 74 | """ |
|
75 | 75 | config = kwargs.pop('config', None) |
|
76 | 76 | if config is not None: |
|
77 | 77 | # We used to deepcopy, but for now we are trying to just save |
|
78 | 78 | # by reference. This *could* have side effects as all components |
|
79 | 79 | # will share config. In fact, I did find such a side effect in |
|
80 | 80 | # _config_changed below. If a config attribute value was a mutable type |
|
81 | 81 | # all instances of a component were getting the same copy, effectively |
|
82 | 82 | # making that a class attribute. |
|
83 | 83 | # self.config = deepcopy(config) |
|
84 | 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 | 86 | # the values in config. |
|
87 | 87 | super(Configurable, self).__init__(**kwargs) |
|
88 | 88 | self.created = datetime.datetime.now() |
|
89 | 89 | |
|
90 | 90 | #------------------------------------------------------------------------- |
|
91 | 91 | # Static trait notifiations |
|
92 | 92 | #------------------------------------------------------------------------- |
|
93 | 93 | |
|
94 | 94 | def _config_changed(self, name, old, new): |
|
95 | 95 | """Update all the class traits having ``config=True`` as metadata. |
|
96 | 96 | |
|
97 | 97 | For any class trait with a ``config`` metadata attribute that is |
|
98 | 98 | ``True``, we update the trait with the value of the corresponding |
|
99 | 99 | config entry. |
|
100 | 100 | """ |
|
101 | 101 | # Get all traits with a config metadata entry that is True |
|
102 | 102 | traits = self.traits(config=True) |
|
103 | 103 | |
|
104 | 104 | # We auto-load config section for this class as well as any parent |
|
105 | 105 | # classes that are Configurable subclasses. This starts with Configurable |
|
106 | 106 | # and works down the mro loading the config for each section. |
|
107 | 107 | section_names = [cls.__name__ for cls in \ |
|
108 |
reversed(self.__class__.__mro__) if |
|
|
108 | reversed(self.__class__.__mro__) if | |
|
109 | 109 | issubclass(cls, Configurable) and issubclass(self.__class__, cls)] |
|
110 | 110 | |
|
111 | 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 | 113 | # dynamically create the section with name self.__class__.__name__. |
|
114 | 114 | if new._has_section(sname): |
|
115 | 115 | my_config = new[sname] |
|
116 | 116 | for k, v in traits.iteritems(): |
|
117 | 117 | # Don't allow traitlets with config=True to start with |
|
118 | 118 | # uppercase. Otherwise, they are confused with Config |
|
119 | 119 | # subsections. But, developers shouldn't have uppercase |
|
120 | 120 | # attributes anyways! (PEP 6) |
|
121 | 121 | if k[0].upper()==k[0] and not k.startswith('_'): |
|
122 | 122 | raise ConfigurableError('Configurable traitlets with ' |
|
123 | 123 | 'config=True must start with a lowercase so they are ' |
|
124 | 124 | 'not confused with Config subsections: %s.%s' % \ |
|
125 | 125 | (self.__class__.__name__, k)) |
|
126 | 126 | try: |
|
127 | 127 | # Here we grab the value from the config |
|
128 | 128 | # If k has the naming convention of a config |
|
129 | 129 | # section, it will be auto created. |
|
130 | 130 | config_value = my_config[k] |
|
131 | 131 | except KeyError: |
|
132 | 132 | pass |
|
133 | 133 | else: |
|
134 | 134 | # print "Setting %s.%s from %s.%s=%r" % \ |
|
135 | 135 | # (self.__class__.__name__,k,sname,k,config_value) |
|
136 | 136 | # We have to do a deepcopy here if we don't deepcopy the entire |
|
137 | 137 | # config object. If we don't, a mutable config_value will be |
|
138 | 138 | # shared by all instances, effectively making it a class attribute. |
|
139 | 139 | setattr(self, k, deepcopy(config_value)) |
|
140 | 140 | |
|
141 | 141 | @classmethod |
|
142 | 142 | def class_get_help(cls): |
|
143 | 143 | """Get the help string for this class in ReST format.""" |
|
144 | 144 | cls_traits = cls.class_traits(config=True) |
|
145 | 145 | final_help = [] |
|
146 | 146 | final_help.append(u'%s options' % cls.__name__) |
|
147 | 147 | final_help.append(len(final_help[0])*u'-') |
|
148 | 148 | for k,v in cls.class_traits(config=True).iteritems(): |
|
149 | 149 | help = cls.class_get_trait_help(v) |
|
150 | 150 | final_help.append(help) |
|
151 | 151 | return '\n'.join(final_help) |
|
152 | ||
|
152 | ||
|
153 | 153 | @classmethod |
|
154 | 154 | def class_get_trait_help(cls, trait): |
|
155 | 155 | """Get the help string for a single trait.""" |
|
156 | 156 | lines = [] |
|
157 | 157 | header = "--%s.%s=<%s>" % (cls.__name__, trait.name, trait.__class__.__name__) |
|
158 | 158 | lines.append(header) |
|
159 | 159 | try: |
|
160 | 160 | dvr = repr(trait.get_default_value()) |
|
161 | 161 | except Exception: |
|
162 | 162 | dvr = None # ignore defaults we can't construct |
|
163 | 163 | if dvr is not None: |
|
164 | 164 | if len(dvr) > 64: |
|
165 | 165 | dvr = dvr[:61]+'...' |
|
166 | 166 | lines.append(indent('Default: %s'%dvr, 4)) |
|
167 | 167 | if 'Enum' in trait.__class__.__name__: |
|
168 | 168 | # include Enum choices |
|
169 | 169 | lines.append(indent('Choices: %r'%(trait.values,))) |
|
170 | ||
|
170 | ||
|
171 | 171 | help = trait.get_metadata('help') |
|
172 | 172 | if help is not None: |
|
173 | 173 | help = '\n'.join(wrap_paragraphs(help, 76)) |
|
174 | 174 | lines.append(indent(help, 4)) |
|
175 | 175 | return '\n'.join(lines) |
|
176 | 176 | |
|
177 | 177 | @classmethod |
|
178 | 178 | def class_print_help(cls): |
|
179 | 179 | """Get the help string for a single trait and print it.""" |
|
180 | 180 | print cls.class_get_help() |
|
181 | 181 | |
|
182 | 182 | @classmethod |
|
183 | 183 | def class_config_section(cls): |
|
184 | 184 | """Get the config class config section""" |
|
185 | 185 | def c(s): |
|
186 | 186 | """return a commented, wrapped block.""" |
|
187 | 187 | s = '\n\n'.join(wrap_paragraphs(s, 78)) |
|
188 | ||
|
188 | ||
|
189 | 189 | return '# ' + s.replace('\n', '\n# ') |
|
190 | ||
|
190 | ||
|
191 | 191 | # section header |
|
192 | 192 | breaker = '#' + '-'*78 |
|
193 | 193 | s = "# %s configuration"%cls.__name__ |
|
194 | 194 | lines = [breaker, s, breaker, ''] |
|
195 | 195 | # get the description trait |
|
196 | 196 | desc = cls.class_traits().get('description') |
|
197 | 197 | if desc: |
|
198 | 198 | desc = desc.default_value |
|
199 | 199 | else: |
|
200 | 200 | # no description trait, use __doc__ |
|
201 | 201 | desc = getattr(cls, '__doc__', '') |
|
202 | 202 | if desc: |
|
203 | 203 | lines.append(c(desc)) |
|
204 | 204 | lines.append('') |
|
205 | ||
|
205 | ||
|
206 | 206 | parents = [] |
|
207 | 207 | for parent in cls.mro(): |
|
208 | 208 | # only include parents that are not base classes |
|
209 | 209 | # and are not the class itself |
|
210 | 210 | # and have some configurable traits to inherit |
|
211 | 211 | if parent is not cls and issubclass(parent, Configurable) and \ |
|
212 | 212 | parent.class_traits(config=True): |
|
213 | 213 | parents.append(parent) |
|
214 | ||
|
214 | ||
|
215 | 215 | if parents: |
|
216 | 216 | pstr = ', '.join([ p.__name__ for p in parents ]) |
|
217 | 217 | lines.append(c('%s will inherit config from: %s'%(cls.__name__, pstr))) |
|
218 | 218 | lines.append('') |
|
219 | ||
|
219 | ||
|
220 | 220 | for name,trait in cls.class_traits(config=True).iteritems(): |
|
221 | 221 | help = trait.get_metadata('help') or '' |
|
222 | 222 | lines.append(c(help)) |
|
223 | 223 | lines.append('# c.%s.%s = %r'%(cls.__name__, name, trait.get_default_value())) |
|
224 | 224 | lines.append('') |
|
225 | 225 | return '\n'.join(lines) |
|
226 | ||
|
227 | ||
|
226 | ||
|
227 | ||
|
228 | 228 | |
|
229 | 229 | class SingletonConfigurable(Configurable): |
|
230 | 230 | """A configurable that only allows one instance. |
|
231 | 231 | |
|
232 | 232 | This class is for classes that should only have one instance of itself |
|
233 | 233 | or *any* subclass. To create and retrieve such a class use the |
|
234 | 234 | :meth:`SingletonConfigurable.instance` method. |
|
235 | 235 | """ |
|
236 | 236 | |
|
237 | 237 | _instance = None |
|
238 | ||
|
238 | ||
|
239 | 239 | @classmethod |
|
240 | 240 | def _walk_mro(cls): |
|
241 | 241 | """Walk the cls.mro() for parent classes that are also singletons |
|
242 | ||
|
242 | ||
|
243 | 243 | For use in instance() |
|
244 | 244 | """ |
|
245 | ||
|
245 | ||
|
246 | 246 | for subclass in cls.mro(): |
|
247 | 247 | if issubclass(cls, subclass) and \ |
|
248 | 248 | issubclass(subclass, SingletonConfigurable) and \ |
|
249 | 249 | subclass != SingletonConfigurable: |
|
250 | 250 | yield subclass |
|
251 | ||
|
251 | ||
|
252 | 252 | @classmethod |
|
253 | 253 | def clear_instance(cls): |
|
254 | 254 | """unset _instance for this class and singleton parents. |
|
255 | 255 | """ |
|
256 | 256 | if not cls.initialized(): |
|
257 | 257 | return |
|
258 | 258 | for subclass in cls._walk_mro(): |
|
259 | 259 | if isinstance(subclass._instance, cls): |
|
260 | 260 | # only clear instances that are instances |
|
261 | 261 | # of the calling class |
|
262 | 262 | subclass._instance = None |
|
263 | ||
|
263 | ||
|
264 | 264 | @classmethod |
|
265 | 265 | def instance(cls, *args, **kwargs): |
|
266 | 266 | """Returns a global instance of this class. |
|
267 | 267 | |
|
268 | 268 | This method create a new instance if none have previously been created |
|
269 | 269 | and returns a previously created instance is one already exists. |
|
270 | 270 | |
|
271 | 271 | The arguments and keyword arguments passed to this method are passed |
|
272 | 272 | on to the :meth:`__init__` method of the class upon instantiation. |
|
273 | 273 | |
|
274 | 274 | Examples |
|
275 | 275 | -------- |
|
276 | 276 | |
|
277 | 277 | Create a singleton class using instance, and retrieve it:: |
|
278 | 278 | |
|
279 | 279 | >>> from IPython.config.configurable import SingletonConfigurable |
|
280 | 280 | >>> class Foo(SingletonConfigurable): pass |
|
281 | 281 | >>> foo = Foo.instance() |
|
282 | 282 | >>> foo == Foo.instance() |
|
283 | 283 | True |
|
284 | 284 | |
|
285 | 285 | Create a subclass that is retrived using the base class instance:: |
|
286 | 286 | |
|
287 | 287 | >>> class Bar(SingletonConfigurable): pass |
|
288 | 288 | >>> class Bam(Bar): pass |
|
289 | 289 | >>> bam = Bam.instance() |
|
290 | 290 | >>> bam == Bar.instance() |
|
291 | 291 | True |
|
292 | 292 | """ |
|
293 | 293 | # Create and save the instance |
|
294 | 294 | if cls._instance is None: |
|
295 | 295 | inst = cls(*args, **kwargs) |
|
296 | 296 | # Now make sure that the instance will also be returned by |
|
297 | 297 | # parent classes' _instance attribute. |
|
298 | 298 | for subclass in cls._walk_mro(): |
|
299 | 299 | subclass._instance = inst |
|
300 | ||
|
300 | ||
|
301 | 301 | if isinstance(cls._instance, cls): |
|
302 | 302 | return cls._instance |
|
303 | 303 | else: |
|
304 | 304 | raise MultipleInstanceError( |
|
305 | 305 | 'Multiple incompatible subclass instances of ' |
|
306 | 306 | '%s are being created.' % cls.__name__ |
|
307 | 307 | ) |
|
308 | 308 | |
|
309 | 309 | @classmethod |
|
310 | 310 | def initialized(cls): |
|
311 | 311 | """Has an instance been created?""" |
|
312 | 312 | return hasattr(cls, "_instance") and cls._instance is not None |
|
313 | 313 | |
|
314 | 314 | |
|
315 | 315 | class LoggingConfigurable(Configurable): |
|
316 | 316 | """A parent class for Configurables that log. |
|
317 | ||
|
317 | ||
|
318 | 318 | Subclasses have a log trait, and the default behavior |
|
319 | 319 | is to get the logger from the currently running Application |
|
320 | 320 | via Application.instance().log. |
|
321 | 321 | """ |
|
322 | ||
|
322 | ||
|
323 | 323 | log = Instance('logging.Logger') |
|
324 | 324 | def _log_default(self): |
|
325 | 325 | from IPython.config.application import Application |
|
326 | 326 | return Application.instance().log |
|
327 | ||
|
328 | ||
|
327 | ||
|
328 |
@@ -1,661 +1,661 b'' | |||
|
1 | 1 | """A simple configuration system. |
|
2 | 2 | |
|
3 | 3 | Authors |
|
4 | 4 | ------- |
|
5 | 5 | * Brian Granger |
|
6 | 6 | * Fernando Perez |
|
7 | 7 | * Min RK |
|
8 | 8 | """ |
|
9 | 9 | |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | # Copyright (C) 2008-2011 The IPython Development Team |
|
12 | 12 | # |
|
13 | 13 | # Distributed under the terms of the BSD License. The full license is in |
|
14 | 14 | # the file COPYING, distributed as part of this software. |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | # Imports |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | |
|
21 | 21 | import __builtin__ as builtin_mod |
|
22 | 22 | import re |
|
23 | 23 | import sys |
|
24 | 24 | |
|
25 | 25 | from IPython.external import argparse |
|
26 | 26 | from IPython.utils.path import filefind, get_ipython_dir |
|
27 | 27 | from IPython.utils import py3compat, text, warn |
|
28 | 28 | |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | # Exceptions |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | |
|
34 | 34 | class ConfigError(Exception): |
|
35 | 35 | pass |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | class ConfigLoaderError(ConfigError): |
|
39 | 39 | pass |
|
40 | 40 | |
|
41 | 41 | class ArgumentError(ConfigLoaderError): |
|
42 | 42 | pass |
|
43 | 43 | |
|
44 | 44 | #----------------------------------------------------------------------------- |
|
45 | 45 | # Argparse fix |
|
46 | 46 | #----------------------------------------------------------------------------- |
|
47 | 47 | |
|
48 | 48 | # Unfortunately argparse by default prints help messages to stderr instead of |
|
49 | 49 | # stdout. This makes it annoying to capture long help screens at the command |
|
50 | 50 | # line, since one must know how to pipe stderr, which many users don't know how |
|
51 | 51 | # to do. So we override the print_help method with one that defaults to |
|
52 | 52 | # stdout and use our class instead. |
|
53 | 53 | |
|
54 | 54 | class ArgumentParser(argparse.ArgumentParser): |
|
55 | 55 | """Simple argparse subclass that prints help to stdout by default.""" |
|
56 | ||
|
56 | ||
|
57 | 57 | def print_help(self, file=None): |
|
58 | 58 | if file is None: |
|
59 | 59 | file = sys.stdout |
|
60 | 60 | return super(ArgumentParser, self).print_help(file) |
|
61 | ||
|
61 | ||
|
62 | 62 | print_help.__doc__ = argparse.ArgumentParser.print_help.__doc__ |
|
63 | ||
|
63 | ||
|
64 | 64 | #----------------------------------------------------------------------------- |
|
65 | 65 | # Config class for holding config information |
|
66 | 66 | #----------------------------------------------------------------------------- |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | class Config(dict): |
|
70 | 70 | """An attribute based dict that can do smart merges.""" |
|
71 | 71 | |
|
72 | 72 | def __init__(self, *args, **kwds): |
|
73 | 73 | dict.__init__(self, *args, **kwds) |
|
74 | 74 | # This sets self.__dict__ = self, but it has to be done this way |
|
75 | 75 | # because we are also overriding __setattr__. |
|
76 | 76 | dict.__setattr__(self, '__dict__', self) |
|
77 | 77 | |
|
78 | 78 | def _merge(self, other): |
|
79 | 79 | to_update = {} |
|
80 | 80 | for k, v in other.iteritems(): |
|
81 | 81 | if not self.has_key(k): |
|
82 | 82 | to_update[k] = v |
|
83 | 83 | else: # I have this key |
|
84 | 84 | if isinstance(v, Config): |
|
85 | 85 | # Recursively merge common sub Configs |
|
86 | 86 | self[k]._merge(v) |
|
87 | 87 | else: |
|
88 | 88 | # Plain updates for non-Configs |
|
89 | 89 | to_update[k] = v |
|
90 | 90 | |
|
91 | 91 | self.update(to_update) |
|
92 | 92 | |
|
93 | 93 | def _is_section_key(self, key): |
|
94 | 94 | if key[0].upper()==key[0] and not key.startswith('_'): |
|
95 | 95 | return True |
|
96 | 96 | else: |
|
97 | 97 | return False |
|
98 | 98 | |
|
99 | 99 | def __contains__(self, key): |
|
100 | 100 | if self._is_section_key(key): |
|
101 | 101 | return True |
|
102 | 102 | else: |
|
103 | 103 | return super(Config, self).__contains__(key) |
|
104 | 104 | # .has_key is deprecated for dictionaries. |
|
105 | 105 | has_key = __contains__ |
|
106 | 106 | |
|
107 | 107 | def _has_section(self, key): |
|
108 | 108 | if self._is_section_key(key): |
|
109 | 109 | if super(Config, self).__contains__(key): |
|
110 | 110 | return True |
|
111 | 111 | return False |
|
112 | 112 | |
|
113 | 113 | def copy(self): |
|
114 | 114 | return type(self)(dict.copy(self)) |
|
115 | 115 | |
|
116 | 116 | def __copy__(self): |
|
117 | 117 | return self.copy() |
|
118 | 118 | |
|
119 | 119 | def __deepcopy__(self, memo): |
|
120 | 120 | import copy |
|
121 | 121 | return type(self)(copy.deepcopy(self.items())) |
|
122 | 122 | |
|
123 | 123 | def __getitem__(self, key): |
|
124 | 124 | # We cannot use directly self._is_section_key, because it triggers |
|
125 | 125 | # infinite recursion on top of PyPy. Instead, we manually fish the |
|
126 | 126 | # bound method. |
|
127 | 127 | is_section_key = self.__class__._is_section_key.__get__(self) |
|
128 | ||
|
128 | ||
|
129 | 129 | # Because we use this for an exec namespace, we need to delegate |
|
130 | 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 | 132 | # builtins. |
|
133 | 133 | try: |
|
134 | 134 | return getattr(builtin_mod, key) |
|
135 | 135 | except AttributeError: |
|
136 | 136 | pass |
|
137 | 137 | if is_section_key(key): |
|
138 | 138 | try: |
|
139 | 139 | return dict.__getitem__(self, key) |
|
140 | 140 | except KeyError: |
|
141 | 141 | c = Config() |
|
142 | 142 | dict.__setitem__(self, key, c) |
|
143 | 143 | return c |
|
144 | 144 | else: |
|
145 | 145 | return dict.__getitem__(self, key) |
|
146 | 146 | |
|
147 | 147 | def __setitem__(self, key, value): |
|
148 | 148 | # Don't allow names in __builtin__ to be modified. |
|
149 | 149 | if hasattr(builtin_mod, key): |
|
150 | 150 | raise ConfigError('Config variable names cannot have the same name ' |
|
151 | 151 | 'as a Python builtin: %s' % key) |
|
152 | 152 | if self._is_section_key(key): |
|
153 | 153 | if not isinstance(value, Config): |
|
154 | 154 | raise ValueError('values whose keys begin with an uppercase ' |
|
155 | 155 | 'char must be Config instances: %r, %r' % (key, value)) |
|
156 | 156 | else: |
|
157 | 157 | dict.__setitem__(self, key, value) |
|
158 | 158 | |
|
159 | 159 | def __getattr__(self, key): |
|
160 | 160 | try: |
|
161 | 161 | return self.__getitem__(key) |
|
162 | 162 | except KeyError, e: |
|
163 | 163 | raise AttributeError(e) |
|
164 | 164 | |
|
165 | 165 | def __setattr__(self, key, value): |
|
166 | 166 | try: |
|
167 | 167 | self.__setitem__(key, value) |
|
168 | 168 | except KeyError, e: |
|
169 | 169 | raise AttributeError(e) |
|
170 | 170 | |
|
171 | 171 | def __delattr__(self, key): |
|
172 | 172 | try: |
|
173 | 173 | dict.__delitem__(self, key) |
|
174 | 174 | except KeyError, e: |
|
175 | 175 | raise AttributeError(e) |
|
176 | 176 | |
|
177 | 177 | |
|
178 | 178 | #----------------------------------------------------------------------------- |
|
179 | 179 | # Config loading classes |
|
180 | 180 | #----------------------------------------------------------------------------- |
|
181 | 181 | |
|
182 | 182 | |
|
183 | 183 | class ConfigLoader(object): |
|
184 | 184 | """A object for loading configurations from just about anywhere. |
|
185 | ||
|
185 | ||
|
186 | 186 | The resulting configuration is packaged as a :class:`Struct`. |
|
187 | ||
|
187 | ||
|
188 | 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 | 191 | (file, command line arguments) and returns the data as a :class:`Struct`. |
|
192 | 192 | There are lots of things that :class:`ConfigLoader` does not do. It does |
|
193 | 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 | 195 | handled elsewhere. |
|
196 | 196 | """ |
|
197 | 197 | |
|
198 | 198 | def __init__(self): |
|
199 | 199 | """A base class for config loaders. |
|
200 | ||
|
200 | ||
|
201 | 201 | Examples |
|
202 | 202 | -------- |
|
203 | ||
|
203 | ||
|
204 | 204 | >>> cl = ConfigLoader() |
|
205 | 205 | >>> config = cl.load_config() |
|
206 | 206 | >>> config |
|
207 | 207 | {} |
|
208 | 208 | """ |
|
209 | 209 | self.clear() |
|
210 | 210 | |
|
211 | 211 | def clear(self): |
|
212 | 212 | self.config = Config() |
|
213 | 213 | |
|
214 | 214 | def load_config(self): |
|
215 | 215 | """Load a config from somewhere, return a :class:`Config` instance. |
|
216 | ||
|
216 | ||
|
217 | 217 | Usually, this will cause self.config to be set and then returned. |
|
218 | 218 | However, in most cases, :meth:`ConfigLoader.clear` should be called |
|
219 | 219 | to erase any previous state. |
|
220 | 220 | """ |
|
221 | 221 | self.clear() |
|
222 | 222 | return self.config |
|
223 | 223 | |
|
224 | 224 | |
|
225 | 225 | class FileConfigLoader(ConfigLoader): |
|
226 | 226 | """A base class for file based configurations. |
|
227 | 227 | |
|
228 | 228 | As we add more file based config loaders, the common logic should go |
|
229 | 229 | here. |
|
230 | 230 | """ |
|
231 | 231 | pass |
|
232 | 232 | |
|
233 | 233 | |
|
234 | 234 | class PyFileConfigLoader(FileConfigLoader): |
|
235 | 235 | """A config loader for pure python files. |
|
236 | ||
|
236 | ||
|
237 | 237 | This calls execfile on a plain python file and looks for attributes |
|
238 | 238 | that are all caps. These attribute are added to the config Struct. |
|
239 | 239 | """ |
|
240 | 240 | |
|
241 | 241 | def __init__(self, filename, path=None): |
|
242 | 242 | """Build a config loader for a filename and path. |
|
243 | 243 | |
|
244 | 244 | Parameters |
|
245 | 245 | ---------- |
|
246 | 246 | filename : str |
|
247 | 247 | The file name of the config file. |
|
248 | 248 | path : str, list, tuple |
|
249 | 249 | The path to search for the config file on, or a sequence of |
|
250 | 250 | paths to try in order. |
|
251 | 251 | """ |
|
252 | 252 | super(PyFileConfigLoader, self).__init__() |
|
253 | 253 | self.filename = filename |
|
254 | 254 | self.path = path |
|
255 | 255 | self.full_filename = '' |
|
256 | 256 | self.data = None |
|
257 | 257 | |
|
258 | 258 | def load_config(self): |
|
259 | 259 | """Load the config from a file and return it as a Struct.""" |
|
260 | 260 | self.clear() |
|
261 | 261 | self._find_file() |
|
262 | 262 | self._read_file_as_dict() |
|
263 | 263 | self._convert_to_config() |
|
264 | 264 | return self.config |
|
265 | 265 | |
|
266 | 266 | def _find_file(self): |
|
267 | 267 | """Try to find the file by searching the paths.""" |
|
268 | 268 | self.full_filename = filefind(self.filename, self.path) |
|
269 | 269 | |
|
270 | 270 | def _read_file_as_dict(self): |
|
271 | 271 | """Load the config file into self.config, with recursive loading.""" |
|
272 | 272 | # This closure is made available in the namespace that is used |
|
273 | 273 | # to exec the config file. It allows users to call |
|
274 | 274 | # load_subconfig('myconfig.py') to load config files recursively. |
|
275 | 275 | # It needs to be a closure because it has references to self.path |
|
276 | 276 | # and self.config. The sub-config is loaded with the same path |
|
277 | 277 | # as the parent, but it uses an empty config which is then merged |
|
278 | 278 | # with the parents. |
|
279 | ||
|
279 | ||
|
280 | 280 | # If a profile is specified, the config file will be loaded |
|
281 | 281 | # from that profile |
|
282 | ||
|
282 | ||
|
283 | 283 | def load_subconfig(fname, profile=None): |
|
284 | 284 | # import here to prevent circular imports |
|
285 | 285 | from IPython.core.profiledir import ProfileDir, ProfileDirError |
|
286 | 286 | if profile is not None: |
|
287 | 287 | try: |
|
288 | 288 | profile_dir = ProfileDir.find_profile_dir_by_name( |
|
289 | 289 | get_ipython_dir(), |
|
290 | 290 | profile, |
|
291 | 291 | ) |
|
292 | 292 | except ProfileDirError: |
|
293 | 293 | return |
|
294 | 294 | path = profile_dir.location |
|
295 | 295 | else: |
|
296 | 296 | path = self.path |
|
297 | 297 | loader = PyFileConfigLoader(fname, path) |
|
298 | 298 | try: |
|
299 | 299 | sub_config = loader.load_config() |
|
300 | 300 | except IOError: |
|
301 | 301 | # Pass silently if the sub config is not there. This happens |
|
302 | 302 | # when a user s using a profile, but not the default config. |
|
303 | 303 | pass |
|
304 | 304 | else: |
|
305 | 305 | self.config._merge(sub_config) |
|
306 | ||
|
306 | ||
|
307 | 307 | # Again, this needs to be a closure and should be used in config |
|
308 | 308 | # files to get the config being loaded. |
|
309 | 309 | def get_config(): |
|
310 | 310 | return self.config |
|
311 | 311 | |
|
312 | 312 | namespace = dict(load_subconfig=load_subconfig, get_config=get_config) |
|
313 | 313 | fs_encoding = sys.getfilesystemencoding() or 'ascii' |
|
314 | 314 | conf_filename = self.full_filename.encode(fs_encoding) |
|
315 | 315 | py3compat.execfile(conf_filename, namespace) |
|
316 | 316 | |
|
317 | 317 | def _convert_to_config(self): |
|
318 | 318 | if self.data is None: |
|
319 | 319 | ConfigLoaderError('self.data does not exist') |
|
320 | 320 | |
|
321 | 321 | |
|
322 | 322 | class CommandLineConfigLoader(ConfigLoader): |
|
323 | 323 | """A config loader for command line arguments. |
|
324 | 324 | |
|
325 | 325 | As we add more command line based loaders, the common logic should go |
|
326 | 326 | here. |
|
327 | 327 | """ |
|
328 | 328 | |
|
329 | 329 | def _exec_config_str(self, lhs, rhs): |
|
330 | 330 | exec_str = 'self.config.' + lhs + '=' + rhs |
|
331 | 331 | try: |
|
332 | 332 | # Try to see if regular Python syntax will work. This |
|
333 | 333 | # won't handle strings as the quote marks are removed |
|
334 | 334 | # by the system shell. |
|
335 | 335 | exec exec_str in locals(), globals() |
|
336 | 336 | except (NameError, SyntaxError): |
|
337 | 337 | # This case happens if the rhs is a string but without |
|
338 | 338 | # the quote marks. Use repr, to get quote marks, and |
|
339 | 339 | # 'u' prefix and see if |
|
340 | 340 | # it succeeds. If it still fails, we let it raise. |
|
341 | 341 | exec_str = u'self.config.' + lhs + '=' + repr(rhs) |
|
342 | 342 | exec exec_str in locals(), globals() |
|
343 | ||
|
343 | ||
|
344 | 344 | def _load_flag(self, cfg): |
|
345 | 345 | """update self.config from a flag, which can be a dict or Config""" |
|
346 | 346 | if isinstance(cfg, (dict, Config)): |
|
347 | 347 | # don't clobber whole config sections, update |
|
348 | 348 | # each section from config: |
|
349 | 349 | for sec,c in cfg.iteritems(): |
|
350 | 350 | self.config[sec].update(c) |
|
351 | 351 | else: |
|
352 | 352 | raise ValueError("Invalid flag: '%s'"%raw) |
|
353 | 353 | |
|
354 | 354 | # raw --identifier=value pattern |
|
355 | 355 | # but *also* accept '-' as wordsep, for aliases |
|
356 | 356 | # accepts: --foo=a |
|
357 | 357 | # --Class.trait=value |
|
358 | 358 | # --alias-name=value |
|
359 | 359 | # rejects: -foo=value |
|
360 | 360 | # --foo |
|
361 | 361 | # --Class.trait |
|
362 | 362 | kv_pattern = re.compile(r'\-\-[A-Za-z][\w\-]*(\.[\w\-]+)*\=.*') |
|
363 | 363 | |
|
364 | 364 | # just flags, no assignments, with two *or one* leading '-' |
|
365 | 365 | # accepts: --foo |
|
366 | 366 | # -foo-bar-again |
|
367 | 367 | # rejects: --anything=anything |
|
368 | 368 | # --two.word |
|
369 | 369 | |
|
370 | 370 | flag_pattern = re.compile(r'\-\-?\w+[\-\w]*$') |
|
371 | 371 | |
|
372 | 372 | class KeyValueConfigLoader(CommandLineConfigLoader): |
|
373 | 373 | """A config loader that loads key value pairs from the command line. |
|
374 | 374 | |
|
375 | 375 | This allows command line options to be gives in the following form:: |
|
376 | ||
|
376 | ||
|
377 | 377 | ipython --profile="foo" --InteractiveShell.autocall=False |
|
378 | 378 | """ |
|
379 | 379 | |
|
380 | 380 | def __init__(self, argv=None, aliases=None, flags=None): |
|
381 | 381 | """Create a key value pair config loader. |
|
382 | 382 | |
|
383 | 383 | Parameters |
|
384 | 384 | ---------- |
|
385 | 385 | argv : list |
|
386 | 386 | A list that has the form of sys.argv[1:] which has unicode |
|
387 | 387 | elements of the form u"key=value". If this is None (default), |
|
388 | 388 | then sys.argv[1:] will be used. |
|
389 | 389 | aliases : dict |
|
390 | 390 | A dict of aliases for configurable traits. |
|
391 | 391 | Keys are the short aliases, Values are the resolved trait. |
|
392 | 392 | Of the form: `{'alias' : 'Configurable.trait'}` |
|
393 | 393 | flags : dict |
|
394 | 394 | A dict of flags, keyed by str name. Vaues can be Config objects, |
|
395 | 395 | dicts, or "key=value" strings. If Config or dict, when the flag |
|
396 | 396 | is triggered, The flag is loaded as `self.config.update(m)`. |
|
397 | 397 | |
|
398 | 398 | Returns |
|
399 | 399 | ------- |
|
400 | 400 | config : Config |
|
401 | 401 | The resulting Config object. |
|
402 | 402 | |
|
403 | 403 | Examples |
|
404 | 404 | -------- |
|
405 | 405 | |
|
406 | 406 | >>> from IPython.config.loader import KeyValueConfigLoader |
|
407 | 407 | >>> cl = KeyValueConfigLoader() |
|
408 | 408 | >>> cl.load_config(["--A.name='brian'","--B.number=0"]) |
|
409 | 409 | {'A': {'name': 'brian'}, 'B': {'number': 0}} |
|
410 | 410 | """ |
|
411 | 411 | self.clear() |
|
412 | 412 | if argv is None: |
|
413 | 413 | argv = sys.argv[1:] |
|
414 | 414 | self.argv = argv |
|
415 | 415 | self.aliases = aliases or {} |
|
416 | 416 | self.flags = flags or {} |
|
417 | ||
|
418 | ||
|
417 | ||
|
418 | ||
|
419 | 419 | def clear(self): |
|
420 | 420 | super(KeyValueConfigLoader, self).clear() |
|
421 | 421 | self.extra_args = [] |
|
422 | ||
|
423 | ||
|
422 | ||
|
423 | ||
|
424 | 424 | def _decode_argv(self, argv, enc=None): |
|
425 | 425 | """decode argv if bytes, using stin.encoding, falling back on default enc""" |
|
426 | 426 | uargv = [] |
|
427 | 427 | if enc is None: |
|
428 | 428 | enc = text.getdefaultencoding() |
|
429 | 429 | for arg in argv: |
|
430 | 430 | if not isinstance(arg, unicode): |
|
431 | 431 | # only decode if not already decoded |
|
432 | 432 | arg = arg.decode(enc) |
|
433 | 433 | uargv.append(arg) |
|
434 | 434 | return uargv |
|
435 | ||
|
436 | ||
|
435 | ||
|
436 | ||
|
437 | 437 | def load_config(self, argv=None, aliases=None, flags=None): |
|
438 | 438 | """Parse the configuration and generate the Config object. |
|
439 | ||
|
439 | ||
|
440 | 440 | After loading, any arguments that are not key-value or |
|
441 | 441 | flags will be stored in self.extra_args - a list of |
|
442 | 442 | unparsed command-line arguments. This is used for |
|
443 | 443 | arguments such as input files or subcommands. |
|
444 | ||
|
444 | ||
|
445 | 445 | Parameters |
|
446 | 446 | ---------- |
|
447 | 447 | argv : list, optional |
|
448 | 448 | A list that has the form of sys.argv[1:] which has unicode |
|
449 | 449 | elements of the form u"key=value". If this is None (default), |
|
450 | 450 | then self.argv will be used. |
|
451 | 451 | aliases : dict |
|
452 | 452 | A dict of aliases for configurable traits. |
|
453 | 453 | Keys are the short aliases, Values are the resolved trait. |
|
454 | 454 | Of the form: `{'alias' : 'Configurable.trait'}` |
|
455 | 455 | flags : dict |
|
456 | 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 | 458 | `self.config.update(cfg)`. |
|
459 | 459 | """ |
|
460 | 460 | from IPython.config.configurable import Configurable |
|
461 | 461 | |
|
462 | 462 | self.clear() |
|
463 | 463 | if argv is None: |
|
464 | 464 | argv = self.argv |
|
465 | 465 | if aliases is None: |
|
466 | 466 | aliases = self.aliases |
|
467 | 467 | if flags is None: |
|
468 | 468 | flags = self.flags |
|
469 | ||
|
469 | ||
|
470 | 470 | # ensure argv is a list of unicode strings: |
|
471 | 471 | uargv = self._decode_argv(argv) |
|
472 | 472 | for idx,raw in enumerate(uargv): |
|
473 | 473 | # strip leading '-' |
|
474 | 474 | item = raw.lstrip('-') |
|
475 | ||
|
475 | ||
|
476 | 476 | if raw == '--': |
|
477 | 477 | # don't parse arguments after '--' |
|
478 | 478 | # this is useful for relaying arguments to scripts, e.g. |
|
479 | 479 | # ipython -i foo.py --pylab=qt -- args after '--' go-to-foo.py |
|
480 | 480 | self.extra_args.extend(uargv[idx+1:]) |
|
481 | 481 | break |
|
482 | ||
|
482 | ||
|
483 | 483 | if kv_pattern.match(raw): |
|
484 | 484 | lhs,rhs = item.split('=',1) |
|
485 | 485 | # Substitute longnames for aliases. |
|
486 | 486 | if lhs in aliases: |
|
487 | 487 | lhs = aliases[lhs] |
|
488 | 488 | if '.' not in lhs: |
|
489 | 489 | # probably a mistyped alias, but not technically illegal |
|
490 | 490 | warn.warn("Unrecognized alias: '%s', it will probably have no effect."%lhs) |
|
491 | 491 | self._exec_config_str(lhs, rhs) |
|
492 | ||
|
492 | ||
|
493 | 493 | elif flag_pattern.match(raw): |
|
494 | 494 | if item in flags: |
|
495 | 495 | cfg,help = flags[item] |
|
496 | 496 | self._load_flag(cfg) |
|
497 | 497 | else: |
|
498 | 498 | raise ArgumentError("Unrecognized flag: '%s'"%raw) |
|
499 | 499 | elif raw.startswith('-'): |
|
500 | 500 | kv = '--'+item |
|
501 | 501 | if kv_pattern.match(kv): |
|
502 | 502 | raise ArgumentError("Invalid argument: '%s', did you mean '%s'?"%(raw, kv)) |
|
503 | 503 | else: |
|
504 | 504 | raise ArgumentError("Invalid argument: '%s'"%raw) |
|
505 | 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 | 507 | # in case our parent knows what to do with them. |
|
508 | 508 | self.extra_args.append(item) |
|
509 | 509 | return self.config |
|
510 | 510 | |
|
511 | 511 | class ArgParseConfigLoader(CommandLineConfigLoader): |
|
512 | 512 | """A loader that uses the argparse module to load from the command line.""" |
|
513 | 513 | |
|
514 | 514 | def __init__(self, argv=None, aliases=None, flags=None, *parser_args, **parser_kw): |
|
515 | 515 | """Create a config loader for use with argparse. |
|
516 | 516 | |
|
517 | 517 | Parameters |
|
518 | 518 | ---------- |
|
519 | 519 | |
|
520 | 520 | argv : optional, list |
|
521 | 521 | If given, used to read command-line arguments from, otherwise |
|
522 | 522 | sys.argv[1:] is used. |
|
523 | 523 | |
|
524 | 524 | parser_args : tuple |
|
525 | 525 | A tuple of positional arguments that will be passed to the |
|
526 | 526 | constructor of :class:`argparse.ArgumentParser`. |
|
527 | 527 | |
|
528 | 528 | parser_kw : dict |
|
529 | 529 | A tuple of keyword arguments that will be passed to the |
|
530 | 530 | constructor of :class:`argparse.ArgumentParser`. |
|
531 | 531 | |
|
532 | 532 | Returns |
|
533 | 533 | ------- |
|
534 | 534 | config : Config |
|
535 | 535 | The resulting Config object. |
|
536 | 536 | """ |
|
537 | 537 | super(CommandLineConfigLoader, self).__init__() |
|
538 | 538 | self.clear() |
|
539 | 539 | if argv is None: |
|
540 | 540 | argv = sys.argv[1:] |
|
541 | 541 | self.argv = argv |
|
542 | 542 | self.aliases = aliases or {} |
|
543 | 543 | self.flags = flags or {} |
|
544 | ||
|
544 | ||
|
545 | 545 | self.parser_args = parser_args |
|
546 | 546 | self.version = parser_kw.pop("version", None) |
|
547 | 547 | kwargs = dict(argument_default=argparse.SUPPRESS) |
|
548 | 548 | kwargs.update(parser_kw) |
|
549 | 549 | self.parser_kw = kwargs |
|
550 | 550 | |
|
551 | 551 | def load_config(self, argv=None, aliases=None, flags=None): |
|
552 | 552 | """Parse command line arguments and return as a Config object. |
|
553 | 553 | |
|
554 | 554 | Parameters |
|
555 | 555 | ---------- |
|
556 | 556 | |
|
557 | 557 | args : optional, list |
|
558 | 558 | If given, a list with the structure of sys.argv[1:] to parse |
|
559 | 559 | arguments from. If not given, the instance's self.argv attribute |
|
560 | 560 | (given at construction time) is used.""" |
|
561 | 561 | self.clear() |
|
562 | 562 | if argv is None: |
|
563 | 563 | argv = self.argv |
|
564 | 564 | if aliases is None: |
|
565 | 565 | aliases = self.aliases |
|
566 | 566 | if flags is None: |
|
567 | 567 | flags = self.flags |
|
568 | 568 | self._create_parser(aliases, flags) |
|
569 | 569 | self._parse_args(argv) |
|
570 | 570 | self._convert_to_config() |
|
571 | 571 | return self.config |
|
572 | 572 | |
|
573 | 573 | def get_extra_args(self): |
|
574 | 574 | if hasattr(self, 'extra_args'): |
|
575 | 575 | return self.extra_args |
|
576 | 576 | else: |
|
577 | 577 | return [] |
|
578 | 578 | |
|
579 | 579 | def _create_parser(self, aliases=None, flags=None): |
|
580 | 580 | self.parser = ArgumentParser(*self.parser_args, **self.parser_kw) |
|
581 | 581 | self._add_arguments(aliases, flags) |
|
582 | 582 | |
|
583 | 583 | def _add_arguments(self, aliases=None, flags=None): |
|
584 | 584 | raise NotImplementedError("subclasses must implement _add_arguments") |
|
585 | 585 | |
|
586 | 586 | def _parse_args(self, args): |
|
587 | 587 | """self.parser->self.parsed_data""" |
|
588 | 588 | # decode sys.argv to support unicode command-line options |
|
589 | 589 | enc = text.getdefaultencoding() |
|
590 | 590 | uargs = [py3compat.cast_unicode(a, enc) for a in args] |
|
591 | 591 | self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs) |
|
592 | 592 | |
|
593 | 593 | def _convert_to_config(self): |
|
594 | 594 | """self.parsed_data->self.config""" |
|
595 | 595 | for k, v in vars(self.parsed_data).iteritems(): |
|
596 | 596 | exec "self.config.%s = v"%k in locals(), globals() |
|
597 | 597 | |
|
598 | 598 | class KVArgParseConfigLoader(ArgParseConfigLoader): |
|
599 | 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 | 601 | of common args, such as `ipython -c 'print 5'`, but still gets |
|
602 | 602 | arbitrary config with `ipython --InteractiveShell.use_readline=False`""" |
|
603 | ||
|
603 | ||
|
604 | 604 | def _convert_to_config(self): |
|
605 | 605 | """self.parsed_data->self.config""" |
|
606 | 606 | for k, v in vars(self.parsed_data).iteritems(): |
|
607 | 607 | self._exec_config_str(k, v) |
|
608 | 608 | |
|
609 | 609 | def _add_arguments(self, aliases=None, flags=None): |
|
610 | 610 | self.alias_flags = {} |
|
611 | 611 | # print aliases, flags |
|
612 | 612 | if aliases is None: |
|
613 | 613 | aliases = self.aliases |
|
614 | 614 | if flags is None: |
|
615 | 615 | flags = self.flags |
|
616 | 616 | paa = self.parser.add_argument |
|
617 | 617 | for key,value in aliases.iteritems(): |
|
618 | 618 | if key in flags: |
|
619 | 619 | # flags |
|
620 | 620 | nargs = '?' |
|
621 | 621 | else: |
|
622 | 622 | nargs = None |
|
623 | 623 | if len(key) is 1: |
|
624 | 624 | paa('-'+key, '--'+key, type=unicode, dest=value, nargs=nargs) |
|
625 | 625 | else: |
|
626 | 626 | paa('--'+key, type=unicode, dest=value, nargs=nargs) |
|
627 | 627 | for key, (value, help) in flags.iteritems(): |
|
628 | 628 | if key in self.aliases: |
|
629 |
# |
|
|
629 | # | |
|
630 | 630 | self.alias_flags[self.aliases[key]] = value |
|
631 | 631 | continue |
|
632 | 632 | if len(key) is 1: |
|
633 | 633 | paa('-'+key, '--'+key, action='append_const', dest='_flags', const=value) |
|
634 | 634 | else: |
|
635 | 635 | paa('--'+key, action='append_const', dest='_flags', const=value) |
|
636 | ||
|
636 | ||
|
637 | 637 | def _convert_to_config(self): |
|
638 | 638 | """self.parsed_data->self.config, parse unrecognized extra args via KVLoader.""" |
|
639 | 639 | # remove subconfigs list from namespace before transforming the Namespace |
|
640 | 640 | if '_flags' in self.parsed_data: |
|
641 | 641 | subcs = self.parsed_data._flags |
|
642 | 642 | del self.parsed_data._flags |
|
643 | 643 | else: |
|
644 | 644 | subcs = [] |
|
645 | ||
|
645 | ||
|
646 | 646 | for k, v in vars(self.parsed_data).iteritems(): |
|
647 | 647 | if v is None: |
|
648 | 648 | # it was a flag that shares the name of an alias |
|
649 | 649 | subcs.append(self.alias_flags[k]) |
|
650 | 650 | else: |
|
651 | 651 | # eval the KV assignment |
|
652 | 652 | self._exec_config_str(k, v) |
|
653 | ||
|
653 | ||
|
654 | 654 | for subc in subcs: |
|
655 | 655 | self._load_flag(subc) |
|
656 | ||
|
656 | ||
|
657 | 657 | if self.extra_args: |
|
658 | 658 | sub_parser = KeyValueConfigLoader() |
|
659 | 659 | sub_parser.load_config(self.extra_args) |
|
660 | 660 | self.config._merge(sub_parser.config) |
|
661 | 661 | self.extra_args = sub_parser.extra_args |
@@ -1,263 +1,263 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | System command aliases. |
|
4 | 4 | |
|
5 | 5 | Authors: |
|
6 | 6 | |
|
7 | 7 | * Fernando Perez |
|
8 | 8 | * Brian Granger |
|
9 | 9 | """ |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Copyright (C) 2008-2010 The IPython Development Team |
|
13 | 13 | # |
|
14 | 14 | # Distributed under the terms of the BSD License. |
|
15 | 15 | # |
|
16 | 16 | # The full license is in the file COPYING.txt, distributed with this software. |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Imports |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | |
|
23 | 23 | import __builtin__ |
|
24 | 24 | import keyword |
|
25 | 25 | import os |
|
26 | 26 | import re |
|
27 | 27 | import sys |
|
28 | 28 | |
|
29 | 29 | from IPython.config.configurable import Configurable |
|
30 | 30 | from IPython.core.splitinput import split_user_input |
|
31 | 31 | |
|
32 | 32 | from IPython.utils.traitlets import List, Instance |
|
33 | 33 | from IPython.utils.autoattr import auto_attr |
|
34 | 34 | from IPython.utils.warn import warn, error |
|
35 | 35 | |
|
36 | 36 | #----------------------------------------------------------------------------- |
|
37 | 37 | # Utilities |
|
38 | 38 | #----------------------------------------------------------------------------- |
|
39 | 39 | |
|
40 | 40 | # This is used as the pattern for calls to split_user_input. |
|
41 | 41 | shell_line_split = re.compile(r'^(\s*)()(\S+)(.*$)') |
|
42 | 42 | |
|
43 | 43 | def default_aliases(): |
|
44 | 44 | """Return list of shell aliases to auto-define. |
|
45 | 45 | """ |
|
46 | 46 | # Note: the aliases defined here should be safe to use on a kernel |
|
47 | 47 | # regardless of what frontend it is attached to. Frontends that use a |
|
48 | 48 | # kernel in-process can define additional aliases that will only work in |
|
49 | 49 | # their case. For example, things like 'less' or 'clear' that manipulate |
|
50 | 50 | # the terminal should NOT be declared here, as they will only work if the |
|
51 | 51 | # kernel is running inside a true terminal, and not over the network. |
|
52 | ||
|
52 | ||
|
53 | 53 | if os.name == 'posix': |
|
54 | 54 | default_aliases = [('mkdir', 'mkdir'), ('rmdir', 'rmdir'), |
|
55 | 55 | ('mv', 'mv -i'), ('rm', 'rm -i'), ('cp', 'cp -i'), |
|
56 | 56 | ('cat', 'cat'), |
|
57 | 57 | ] |
|
58 | 58 | # Useful set of ls aliases. The GNU and BSD options are a little |
|
59 | 59 | # different, so we make aliases that provide as similar as possible |
|
60 | 60 | # behavior in ipython, by passing the right flags for each platform |
|
61 | 61 | if sys.platform.startswith('linux'): |
|
62 | 62 | ls_aliases = [('ls', 'ls -F --color'), |
|
63 | 63 | # long ls |
|
64 | 64 | ('ll', 'ls -F -o --color'), |
|
65 | 65 | # ls normal files only |
|
66 | 66 | ('lf', 'ls -F -o --color %l | grep ^-'), |
|
67 | 67 | # ls symbolic links |
|
68 | 68 | ('lk', 'ls -F -o --color %l | grep ^l'), |
|
69 | 69 | # directories or links to directories, |
|
70 | 70 | ('ldir', 'ls -F -o --color %l | grep /$'), |
|
71 | 71 | # things which are executable |
|
72 | 72 | ('lx', 'ls -F -o --color %l | grep ^-..x'), |
|
73 | 73 | ] |
|
74 | 74 | else: |
|
75 | 75 | # BSD, OSX, etc. |
|
76 | 76 | ls_aliases = [('ls', 'ls -F'), |
|
77 | 77 | # long ls |
|
78 | 78 | ('ll', 'ls -F -l'), |
|
79 | 79 | # ls normal files only |
|
80 | 80 | ('lf', 'ls -F -l %l | grep ^-'), |
|
81 | 81 | # ls symbolic links |
|
82 | 82 | ('lk', 'ls -F -l %l | grep ^l'), |
|
83 | 83 | # directories or links to directories, |
|
84 | 84 | ('ldir', 'ls -F -l %l | grep /$'), |
|
85 | 85 | # things which are executable |
|
86 | 86 | ('lx', 'ls -F -l %l | grep ^-..x'), |
|
87 | 87 | ] |
|
88 | 88 | default_aliases = default_aliases + ls_aliases |
|
89 | 89 | elif os.name in ['nt', 'dos']: |
|
90 | 90 | default_aliases = [('ls', 'dir /on'), |
|
91 | 91 | ('ddir', 'dir /ad /on'), ('ldir', 'dir /ad /on'), |
|
92 | 92 | ('mkdir', 'mkdir'), ('rmdir', 'rmdir'), |
|
93 | 93 | ('echo', 'echo'), ('ren', 'ren'), ('copy', 'copy'), |
|
94 | 94 | ] |
|
95 | 95 | else: |
|
96 | 96 | default_aliases = [] |
|
97 | ||
|
97 | ||
|
98 | 98 | return default_aliases |
|
99 | 99 | |
|
100 | 100 | |
|
101 | 101 | class AliasError(Exception): |
|
102 | 102 | pass |
|
103 | 103 | |
|
104 | 104 | |
|
105 | 105 | class InvalidAliasError(AliasError): |
|
106 | 106 | pass |
|
107 | 107 | |
|
108 | 108 | #----------------------------------------------------------------------------- |
|
109 | 109 | # Main AliasManager class |
|
110 | 110 | #----------------------------------------------------------------------------- |
|
111 | 111 | |
|
112 | 112 | class AliasManager(Configurable): |
|
113 | 113 | |
|
114 | 114 | default_aliases = List(default_aliases(), config=True) |
|
115 | 115 | user_aliases = List(default_value=[], config=True) |
|
116 | 116 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
117 | 117 | |
|
118 | 118 | def __init__(self, shell=None, config=None): |
|
119 | 119 | super(AliasManager, self).__init__(shell=shell, config=config) |
|
120 | 120 | self.alias_table = {} |
|
121 | 121 | self.exclude_aliases() |
|
122 | 122 | self.init_aliases() |
|
123 | 123 | |
|
124 | 124 | def __contains__(self, name): |
|
125 | 125 | return name in self.alias_table |
|
126 | 126 | |
|
127 | 127 | @property |
|
128 | 128 | def aliases(self): |
|
129 | 129 | return [(item[0], item[1][1]) for item in self.alias_table.iteritems()] |
|
130 | 130 | |
|
131 | 131 | def exclude_aliases(self): |
|
132 | 132 | # set of things NOT to alias (keywords, builtins and some magics) |
|
133 | 133 | no_alias = set(['cd','popd','pushd','dhist','alias','unalias']) |
|
134 | 134 | no_alias.update(set(keyword.kwlist)) |
|
135 | 135 | no_alias.update(set(__builtin__.__dict__.keys())) |
|
136 | 136 | self.no_alias = no_alias |
|
137 | 137 | |
|
138 | 138 | def init_aliases(self): |
|
139 | 139 | # Load default aliases |
|
140 | 140 | for name, cmd in self.default_aliases: |
|
141 | 141 | self.soft_define_alias(name, cmd) |
|
142 | 142 | |
|
143 | 143 | # Load user aliases |
|
144 | 144 | for name, cmd in self.user_aliases: |
|
145 | 145 | self.soft_define_alias(name, cmd) |
|
146 | 146 | |
|
147 | 147 | def clear_aliases(self): |
|
148 | 148 | self.alias_table.clear() |
|
149 | 149 | |
|
150 | 150 | def soft_define_alias(self, name, cmd): |
|
151 | 151 | """Define an alias, but don't raise on an AliasError.""" |
|
152 | 152 | try: |
|
153 | 153 | self.define_alias(name, cmd) |
|
154 | 154 | except AliasError, e: |
|
155 | 155 | error("Invalid alias: %s" % e) |
|
156 | 156 | |
|
157 | 157 | def define_alias(self, name, cmd): |
|
158 | 158 | """Define a new alias after validating it. |
|
159 | 159 | |
|
160 | 160 | This will raise an :exc:`AliasError` if there are validation |
|
161 | 161 | problems. |
|
162 | 162 | """ |
|
163 | 163 | nargs = self.validate_alias(name, cmd) |
|
164 | 164 | self.alias_table[name] = (nargs, cmd) |
|
165 | 165 | |
|
166 | 166 | def undefine_alias(self, name): |
|
167 | 167 | if self.alias_table.has_key(name): |
|
168 | 168 | del self.alias_table[name] |
|
169 | 169 | |
|
170 | 170 | def validate_alias(self, name, cmd): |
|
171 | 171 | """Validate an alias and return the its number of arguments.""" |
|
172 | 172 | if name in self.no_alias: |
|
173 | 173 | raise InvalidAliasError("The name %s can't be aliased " |
|
174 | 174 | "because it is a keyword or builtin." % name) |
|
175 | 175 | if not (isinstance(cmd, basestring)): |
|
176 | 176 | raise InvalidAliasError("An alias command must be a string, " |
|
177 | 177 | "got: %r" % name) |
|
178 | 178 | nargs = cmd.count('%s') |
|
179 | 179 | if nargs>0 and cmd.find('%l')>=0: |
|
180 | 180 | raise InvalidAliasError('The %s and %l specifiers are mutually ' |
|
181 | 181 | 'exclusive in alias definitions.') |
|
182 | 182 | return nargs |
|
183 | 183 | |
|
184 | 184 | def call_alias(self, alias, rest=''): |
|
185 | 185 | """Call an alias given its name and the rest of the line.""" |
|
186 | 186 | cmd = self.transform_alias(alias, rest) |
|
187 | 187 | try: |
|
188 | 188 | self.shell.system(cmd) |
|
189 | 189 | except: |
|
190 | 190 | self.shell.showtraceback() |
|
191 | 191 | |
|
192 | 192 | def transform_alias(self, alias,rest=''): |
|
193 | 193 | """Transform alias to system command string.""" |
|
194 | 194 | nargs, cmd = self.alias_table[alias] |
|
195 | 195 | |
|
196 | 196 | if ' ' in cmd and os.path.isfile(cmd): |
|
197 | 197 | cmd = '"%s"' % cmd |
|
198 | 198 | |
|
199 | 199 | # Expand the %l special to be the user's input line |
|
200 | 200 | if cmd.find('%l') >= 0: |
|
201 | 201 | cmd = cmd.replace('%l', rest) |
|
202 | 202 | rest = '' |
|
203 | 203 | if nargs==0: |
|
204 | 204 | # Simple, argument-less aliases |
|
205 | 205 | cmd = '%s %s' % (cmd, rest) |
|
206 | 206 | else: |
|
207 | 207 | # Handle aliases with positional arguments |
|
208 | 208 | args = rest.split(None, nargs) |
|
209 | 209 | if len(args) < nargs: |
|
210 | 210 | raise AliasError('Alias <%s> requires %s arguments, %s given.' % |
|
211 | 211 | (alias, nargs, len(args))) |
|
212 | 212 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
213 | 213 | return cmd |
|
214 | 214 | |
|
215 | 215 | def expand_alias(self, line): |
|
216 |
""" Expand an alias in the command line |
|
|
217 | ||
|
218 |
Returns the provided command line, possibly with the first word |
|
|
216 | """ Expand an alias in the command line | |
|
217 | ||
|
218 | Returns the provided command line, possibly with the first word | |
|
219 | 219 | (command) translated according to alias expansion rules. |
|
220 | ||
|
220 | ||
|
221 | 221 | [ipython]|16> _ip.expand_aliases("np myfile.txt") |
|
222 | 222 | <16> 'q:/opt/np/notepad++.exe myfile.txt' |
|
223 | 223 | """ |
|
224 | ||
|
224 | ||
|
225 | 225 | pre,_,fn,rest = split_user_input(line) |
|
226 | 226 | res = pre + self.expand_aliases(fn, rest) |
|
227 | 227 | return res |
|
228 | 228 | |
|
229 | 229 | def expand_aliases(self, fn, rest): |
|
230 | 230 | """Expand multiple levels of aliases: |
|
231 | ||
|
231 | ||
|
232 | 232 | if: |
|
233 | ||
|
233 | ||
|
234 | 234 | alias foo bar /tmp |
|
235 | 235 | alias baz foo |
|
236 | ||
|
236 | ||
|
237 | 237 | then: |
|
238 | ||
|
238 | ||
|
239 | 239 | baz huhhahhei -> bar /tmp huhhahhei |
|
240 | 240 | """ |
|
241 | 241 | line = fn + " " + rest |
|
242 | ||
|
242 | ||
|
243 | 243 | done = set() |
|
244 | 244 | while 1: |
|
245 | 245 | pre,_,fn,rest = split_user_input(line, shell_line_split) |
|
246 | 246 | if fn in self.alias_table: |
|
247 | 247 | if fn in done: |
|
248 | 248 | warn("Cyclic alias definition, repeated '%s'" % fn) |
|
249 | 249 | return "" |
|
250 | 250 | done.add(fn) |
|
251 | 251 | |
|
252 | 252 | l2 = self.transform_alias(fn, rest) |
|
253 | 253 | if l2 == line: |
|
254 | 254 | break |
|
255 | 255 | # ls -> ls -F should not recurse forever |
|
256 | 256 | if l2.split(None,1)[0] == line.split(None,1)[0]: |
|
257 | 257 | line = l2 |
|
258 | 258 | break |
|
259 | 259 | line=l2 |
|
260 | 260 | else: |
|
261 | 261 | break |
|
262 | ||
|
262 | ||
|
263 | 263 | return line |
@@ -1,317 +1,317 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | An application for IPython. |
|
4 | 4 | |
|
5 | 5 | All top-level applications should use the classes in this module for |
|
6 | 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 | 9 | object and then create the configurable objects, passing the config to them. |
|
10 | 10 | |
|
11 | 11 | Authors: |
|
12 | 12 | |
|
13 | 13 | * Brian Granger |
|
14 | 14 | * Fernando Perez |
|
15 | 15 | * Min RK |
|
16 | 16 | |
|
17 | 17 | """ |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Copyright (C) 2008-2011 The IPython Development Team |
|
21 | 21 | # |
|
22 | 22 | # Distributed under the terms of the BSD License. The full license is in |
|
23 | 23 | # the file COPYING, distributed as part of this software. |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | # Imports |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | |
|
30 | 30 | import glob |
|
31 | 31 | import logging |
|
32 | 32 | import os |
|
33 | 33 | import shutil |
|
34 | 34 | import sys |
|
35 | 35 | |
|
36 | 36 | from IPython.config.application import Application |
|
37 | 37 | from IPython.config.configurable import Configurable |
|
38 | 38 | from IPython.config.loader import Config |
|
39 | 39 | from IPython.core import release, crashhandler |
|
40 | 40 | from IPython.core.profiledir import ProfileDir, ProfileDirError |
|
41 | 41 | from IPython.utils.path import get_ipython_dir, get_ipython_package_dir |
|
42 | 42 | from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict |
|
43 | 43 | from IPython.utils import py3compat |
|
44 | 44 | |
|
45 | 45 | #----------------------------------------------------------------------------- |
|
46 | 46 | # Classes and functions |
|
47 | 47 | #----------------------------------------------------------------------------- |
|
48 | 48 | |
|
49 | 49 | |
|
50 | 50 | #----------------------------------------------------------------------------- |
|
51 | 51 | # Base Application Class |
|
52 | 52 | #----------------------------------------------------------------------------- |
|
53 | 53 | |
|
54 | 54 | # aliases and flags |
|
55 | 55 | |
|
56 | 56 | base_aliases = { |
|
57 | 57 | 'profile' : 'BaseIPythonApplication.profile', |
|
58 | 58 | 'ipython-dir' : 'BaseIPythonApplication.ipython_dir', |
|
59 | 59 | 'log-level' : 'Application.log_level', |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | base_flags = dict( |
|
63 | 63 | debug = ({'Application' : {'log_level' : logging.DEBUG}}, |
|
64 | 64 | "set log level to logging.DEBUG (maximize logging output)"), |
|
65 | 65 | quiet = ({'Application' : {'log_level' : logging.CRITICAL}}, |
|
66 | 66 | "set log level to logging.CRITICAL (minimize logging output)"), |
|
67 | 67 | init = ({'BaseIPythonApplication' : { |
|
68 | 68 | 'copy_config_files' : True, |
|
69 | 69 | 'auto_create' : True} |
|
70 | 70 | }, """Initialize profile with default config files. This is equivalent |
|
71 | 71 | to running `ipython profile create <profile>` prior to startup. |
|
72 | 72 | """) |
|
73 | 73 | ) |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | class BaseIPythonApplication(Application): |
|
77 | 77 | |
|
78 | 78 | name = Unicode(u'ipython') |
|
79 | 79 | description = Unicode(u'IPython: an enhanced interactive Python shell.') |
|
80 | 80 | version = Unicode(release.version) |
|
81 | ||
|
81 | ||
|
82 | 82 | aliases = Dict(base_aliases) |
|
83 | 83 | flags = Dict(base_flags) |
|
84 | 84 | classes = List([ProfileDir]) |
|
85 | ||
|
85 | ||
|
86 | 86 | # Track whether the config_file has changed, |
|
87 | 87 | # because some logic happens only if we aren't using the default. |
|
88 | 88 | config_file_specified = Bool(False) |
|
89 | ||
|
89 | ||
|
90 | 90 | config_file_name = Unicode(u'ipython_config.py') |
|
91 | 91 | def _config_file_name_default(self): |
|
92 | 92 | return self.name.replace('-','_') + u'_config.py' |
|
93 | 93 | def _config_file_name_changed(self, name, old, new): |
|
94 | 94 | if new != old: |
|
95 | 95 | self.config_file_specified = True |
|
96 | 96 | |
|
97 | 97 | # The directory that contains IPython's builtin profiles. |
|
98 | 98 | builtin_profile_dir = Unicode( |
|
99 | 99 | os.path.join(get_ipython_package_dir(), u'config', u'profile', u'default') |
|
100 | 100 | ) |
|
101 | 101 | |
|
102 | 102 | config_file_paths = List(Unicode) |
|
103 | 103 | def _config_file_paths_default(self): |
|
104 | 104 | return [os.getcwdu()] |
|
105 | 105 | |
|
106 | 106 | profile = Unicode(u'', config=True, |
|
107 | 107 | help="""The IPython profile to use.""" |
|
108 | 108 | ) |
|
109 | 109 | def _profile_default(self): |
|
110 | 110 | return "python3" if py3compat.PY3 else "default" |
|
111 | ||
|
111 | ||
|
112 | 112 | def _profile_changed(self, name, old, new): |
|
113 | 113 | self.builtin_profile_dir = os.path.join( |
|
114 | 114 | get_ipython_package_dir(), u'config', u'profile', new |
|
115 | 115 | ) |
|
116 | ||
|
117 |
ipython_dir = Unicode(get_ipython_dir(), config=True, |
|
|
116 | ||
|
117 | ipython_dir = Unicode(get_ipython_dir(), config=True, | |
|
118 | 118 | help=""" |
|
119 | 119 | The name of the IPython directory. This directory is used for logging |
|
120 | 120 | configuration (through profiles), history storage, etc. The default |
|
121 | 121 | is usually $HOME/.ipython. This options can also be specified through |
|
122 | 122 | the environment variable IPYTHON_DIR. |
|
123 | 123 | """ |
|
124 | 124 | ) |
|
125 | ||
|
125 | ||
|
126 | 126 | overwrite = Bool(False, config=True, |
|
127 | 127 | help="""Whether to overwrite existing config files when copying""") |
|
128 | 128 | auto_create = Bool(False, config=True, |
|
129 | 129 | help="""Whether to create profile dir if it doesn't exist""") |
|
130 | ||
|
130 | ||
|
131 | 131 | config_files = List(Unicode) |
|
132 | 132 | def _config_files_default(self): |
|
133 | 133 | return [u'ipython_config.py'] |
|
134 | ||
|
134 | ||
|
135 | 135 | copy_config_files = Bool(False, config=True, |
|
136 | 136 | help="""Whether to install the default config files into the profile dir. |
|
137 | 137 | If a new profile is being created, and IPython contains config files for that |
|
138 | 138 | profile, then they will be staged into the new directory. Otherwise, |
|
139 | 139 | default config files will be automatically generated. |
|
140 | 140 | """) |
|
141 | 141 | |
|
142 | 142 | # The class to use as the crash handler. |
|
143 | 143 | crash_handler_class = Type(crashhandler.CrashHandler) |
|
144 | 144 | |
|
145 | 145 | def __init__(self, **kwargs): |
|
146 | 146 | super(BaseIPythonApplication, self).__init__(**kwargs) |
|
147 | 147 | # ensure even default IPYTHON_DIR exists |
|
148 | 148 | if not os.path.exists(self.ipython_dir): |
|
149 | 149 | self._ipython_dir_changed('ipython_dir', self.ipython_dir, self.ipython_dir) |
|
150 | ||
|
150 | ||
|
151 | 151 | #------------------------------------------------------------------------- |
|
152 | 152 | # Various stages of Application creation |
|
153 | 153 | #------------------------------------------------------------------------- |
|
154 | 154 | |
|
155 | 155 | def init_crash_handler(self): |
|
156 | 156 | """Create a crash handler, typically setting sys.excepthook to it.""" |
|
157 | 157 | self.crash_handler = self.crash_handler_class(self) |
|
158 | 158 | sys.excepthook = self.crash_handler |
|
159 | 159 | |
|
160 | 160 | def _ipython_dir_changed(self, name, old, new): |
|
161 | 161 | if old in sys.path: |
|
162 | 162 | sys.path.remove(old) |
|
163 | 163 | sys.path.append(os.path.abspath(new)) |
|
164 | 164 | if not os.path.isdir(new): |
|
165 | 165 | os.makedirs(new, mode=0777) |
|
166 | 166 | readme = os.path.join(new, 'README') |
|
167 | 167 | if not os.path.exists(readme): |
|
168 | 168 | path = os.path.join(get_ipython_package_dir(), u'config', u'profile') |
|
169 | 169 | shutil.copy(os.path.join(path, 'README'), readme) |
|
170 | 170 | self.log.debug("IPYTHON_DIR set to: %s" % new) |
|
171 | 171 | |
|
172 | 172 | def load_config_file(self, suppress_errors=True): |
|
173 | 173 | """Load the config file. |
|
174 | 174 | |
|
175 | 175 | By default, errors in loading config are handled, and a warning |
|
176 | 176 | printed on screen. For testing, the suppress_errors option is set |
|
177 | 177 | to False, so errors will make tests fail. |
|
178 | 178 | """ |
|
179 | 179 | self.log.debug("Searching path %s for config files", self.config_file_paths) |
|
180 | 180 | base_config = 'ipython_config.py' |
|
181 | 181 | self.log.debug("Attempting to load config file: %s" % |
|
182 | 182 | base_config) |
|
183 | 183 | try: |
|
184 | 184 | Application.load_config_file( |
|
185 | 185 | self, |
|
186 |
base_config, |
|
|
186 | base_config, | |
|
187 | 187 | path=self.config_file_paths |
|
188 | 188 | ) |
|
189 | 189 | except IOError: |
|
190 | 190 | # ignore errors loading parent |
|
191 | 191 | self.log.debug("Config file %s not found", base_config) |
|
192 | 192 | pass |
|
193 | 193 | if self.config_file_name == base_config: |
|
194 | 194 | # don't load secondary config |
|
195 | 195 | return |
|
196 | 196 | self.log.debug("Attempting to load config file: %s" % |
|
197 | 197 | self.config_file_name) |
|
198 | 198 | try: |
|
199 | 199 | Application.load_config_file( |
|
200 | 200 | self, |
|
201 |
self.config_file_name, |
|
|
201 | self.config_file_name, | |
|
202 | 202 | path=self.config_file_paths |
|
203 | 203 | ) |
|
204 | 204 | except IOError: |
|
205 | 205 | # Only warn if the default config file was NOT being used. |
|
206 | 206 | if self.config_file_specified: |
|
207 | 207 | msg = self.log.warn |
|
208 | 208 | else: |
|
209 | 209 | msg = self.log.debug |
|
210 | 210 | msg("Config file not found, skipping: %s", self.config_file_name) |
|
211 | 211 | except: |
|
212 | 212 | # For testing purposes. |
|
213 | 213 | if not suppress_errors: |
|
214 | 214 | raise |
|
215 | 215 | self.log.warn("Error loading config file: %s" % |
|
216 | 216 | self.config_file_name, exc_info=True) |
|
217 | 217 | |
|
218 | 218 | def init_profile_dir(self): |
|
219 | 219 | """initialize the profile dir""" |
|
220 | 220 | try: |
|
221 | 221 | # location explicitly specified: |
|
222 | 222 | location = self.config.ProfileDir.location |
|
223 | 223 | except AttributeError: |
|
224 | 224 | # location not specified, find by profile name |
|
225 | 225 | try: |
|
226 | 226 | p = ProfileDir.find_profile_dir_by_name(self.ipython_dir, self.profile, self.config) |
|
227 | 227 | except ProfileDirError: |
|
228 | 228 | # not found, maybe create it (always create default profile) |
|
229 | 229 | if self.auto_create or self.profile==self._profile_default(): |
|
230 | 230 | try: |
|
231 | 231 | p = ProfileDir.create_profile_dir_by_name(self.ipython_dir, self.profile, self.config) |
|
232 | 232 | except ProfileDirError: |
|
233 | 233 | self.log.fatal("Could not create profile: %r"%self.profile) |
|
234 | 234 | self.exit(1) |
|
235 | 235 | else: |
|
236 | 236 | self.log.info("Created profile dir: %r"%p.location) |
|
237 | 237 | else: |
|
238 | 238 | self.log.fatal("Profile %r not found."%self.profile) |
|
239 | 239 | self.exit(1) |
|
240 | 240 | else: |
|
241 | 241 | self.log.info("Using existing profile dir: %r"%p.location) |
|
242 | 242 | else: |
|
243 | 243 | # location is fully specified |
|
244 | 244 | try: |
|
245 | 245 | p = ProfileDir.find_profile_dir(location, self.config) |
|
246 | 246 | except ProfileDirError: |
|
247 | 247 | # not found, maybe create it |
|
248 | 248 | if self.auto_create: |
|
249 | 249 | try: |
|
250 | 250 | p = ProfileDir.create_profile_dir(location, self.config) |
|
251 | 251 | except ProfileDirError: |
|
252 | 252 | self.log.fatal("Could not create profile directory: %r"%location) |
|
253 | 253 | self.exit(1) |
|
254 | 254 | else: |
|
255 | 255 | self.log.info("Creating new profile dir: %r"%location) |
|
256 | 256 | else: |
|
257 | 257 | self.log.fatal("Profile directory %r not found."%location) |
|
258 | 258 | self.exit(1) |
|
259 | 259 | else: |
|
260 | 260 | self.log.info("Using existing profile dir: %r"%location) |
|
261 | ||
|
261 | ||
|
262 | 262 | self.profile_dir = p |
|
263 | 263 | self.config_file_paths.append(p.location) |
|
264 | ||
|
264 | ||
|
265 | 265 | def init_config_files(self): |
|
266 | 266 | """[optionally] copy default config files into profile dir.""" |
|
267 | 267 | # copy config files |
|
268 | 268 | path = self.builtin_profile_dir |
|
269 | 269 | if self.copy_config_files: |
|
270 | 270 | src = self.profile |
|
271 | ||
|
271 | ||
|
272 | 272 | cfg = self.config_file_name |
|
273 | 273 | if path and os.path.exists(os.path.join(path, cfg)): |
|
274 | 274 | self.log.warn("Staging %r from %s into %r [overwrite=%s]"%( |
|
275 | 275 | cfg, src, self.profile_dir.location, self.overwrite) |
|
276 | 276 | ) |
|
277 | 277 | self.profile_dir.copy_config_file(cfg, path=path, overwrite=self.overwrite) |
|
278 | 278 | else: |
|
279 | 279 | self.stage_default_config_file() |
|
280 | 280 | else: |
|
281 | 281 | # Still stage *bundled* config files, but not generated ones |
|
282 | 282 | # This is necessary for `ipython profile=sympy` to load the profile |
|
283 | 283 | # on the first go |
|
284 | 284 | files = glob.glob(os.path.join(path, '*.py')) |
|
285 | 285 | for fullpath in files: |
|
286 | 286 | cfg = os.path.basename(fullpath) |
|
287 | 287 | if self.profile_dir.copy_config_file(cfg, path=path, overwrite=False): |
|
288 | 288 | # file was copied |
|
289 | 289 | self.log.warn("Staging bundled %s from %s into %r"%( |
|
290 | 290 | cfg, self.profile, self.profile_dir.location) |
|
291 | 291 | ) |
|
292 | ||
|
293 | ||
|
292 | ||
|
293 | ||
|
294 | 294 | def stage_default_config_file(self): |
|
295 | 295 | """auto generate default config file, and stage it into the profile.""" |
|
296 | 296 | s = self.generate_config_file() |
|
297 | 297 | fname = os.path.join(self.profile_dir.location, self.config_file_name) |
|
298 | 298 | if self.overwrite or not os.path.exists(fname): |
|
299 | 299 | self.log.warn("Generating default config file: %r"%(fname)) |
|
300 | 300 | with open(fname, 'w') as f: |
|
301 | 301 | f.write(s) |
|
302 | ||
|
303 | ||
|
302 | ||
|
303 | ||
|
304 | 304 | def initialize(self, argv=None): |
|
305 | 305 | # don't hook up crash handler before parsing command-line |
|
306 | 306 | self.parse_command_line(argv) |
|
307 | 307 | self.init_crash_handler() |
|
308 | 308 | if self.subapp is not None: |
|
309 | 309 | # stop here if subapp is taking over |
|
310 | 310 | return |
|
311 | 311 | cl_config = self.config |
|
312 | 312 | self.init_profile_dir() |
|
313 | 313 | self.init_config_files() |
|
314 | 314 | self.load_config_file() |
|
315 | 315 | # enforce cl-opts override configfile opts: |
|
316 | 316 | self.update_config(cl_config) |
|
317 | 317 |
@@ -1,903 +1,903 b'' | |||
|
1 | 1 | """Word completion for IPython. |
|
2 | 2 | |
|
3 | 3 | This module is a fork of the rlcompleter module in the Python standard |
|
4 | 4 | library. The original enhancements made to rlcompleter have been sent |
|
5 | 5 | upstream and were accepted as of Python 2.3, but we need a lot more |
|
6 | 6 | functionality specific to IPython, so this module will continue to live as an |
|
7 | 7 | IPython-specific utility. |
|
8 | 8 | |
|
9 | 9 | Original rlcompleter documentation: |
|
10 | 10 | |
|
11 | 11 | This requires the latest extension to the readline module (the |
|
12 | 12 | completes keywords, built-ins and globals in __main__; when completing |
|
13 | 13 | NAME.NAME..., it evaluates (!) the expression up to the last dot and |
|
14 | 14 | completes its attributes. |
|
15 | 15 | |
|
16 | 16 | It's very cool to do "import string" type "string.", hit the |
|
17 | 17 | completion key (twice), and see the list of names defined by the |
|
18 | 18 | string module! |
|
19 | 19 | |
|
20 | 20 | Tip: to use the tab key as the completion key, call |
|
21 | 21 | |
|
22 | 22 | readline.parse_and_bind("tab: complete") |
|
23 | 23 | |
|
24 | 24 | Notes: |
|
25 | 25 | |
|
26 | 26 | - Exceptions raised by the completer function are *ignored* (and |
|
27 | 27 | generally cause the completion to fail). This is a feature -- since |
|
28 | 28 | readline sets the tty device in raw (or cbreak) mode, printing a |
|
29 | 29 | traceback wouldn't work well without some complicated hoopla to save, |
|
30 | 30 | reset and restore the tty state. |
|
31 | 31 | |
|
32 | 32 | - The evaluation of the NAME.NAME... form may cause arbitrary |
|
33 | 33 | application defined code to be executed if an object with a |
|
34 | 34 | __getattr__ hook is found. Since it is the responsibility of the |
|
35 | 35 | application (or the user) to enable this feature, I consider this an |
|
36 | 36 | acceptable risk. More complicated expressions (e.g. function calls or |
|
37 | 37 | indexing operations) are *not* evaluated. |
|
38 | 38 | |
|
39 | 39 | - GNU readline is also used by the built-in functions input() and |
|
40 | 40 | raw_input(), and thus these also benefit/suffer from the completer |
|
41 | 41 | features. Clearly an interactive application can benefit by |
|
42 | 42 | specifying its own completer function and using raw_input() for all |
|
43 | 43 | its input. |
|
44 | 44 | |
|
45 | 45 | - When the original stdin is not a tty device, GNU readline is never |
|
46 | 46 | used, and this module (and the readline module) are silently inactive. |
|
47 | 47 | """ |
|
48 | 48 | |
|
49 | 49 | #***************************************************************************** |
|
50 | 50 | # |
|
51 | 51 | # Since this file is essentially a minimally modified copy of the rlcompleter |
|
52 | 52 | # module which is part of the standard Python distribution, I assume that the |
|
53 | 53 | # proper procedure is to maintain its copyright as belonging to the Python |
|
54 | 54 | # Software Foundation (in addition to my own, for all new code). |
|
55 | 55 | # |
|
56 | 56 | # Copyright (C) 2008-2010 IPython Development Team |
|
57 | 57 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
58 | 58 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
59 | 59 | # |
|
60 | 60 | # Distributed under the terms of the BSD License. The full license is in |
|
61 | 61 | # the file COPYING, distributed as part of this software. |
|
62 | 62 | # |
|
63 | 63 | #***************************************************************************** |
|
64 | 64 | from __future__ import print_function |
|
65 | 65 | |
|
66 | 66 | #----------------------------------------------------------------------------- |
|
67 | 67 | # Imports |
|
68 | 68 | #----------------------------------------------------------------------------- |
|
69 | 69 | |
|
70 | 70 | import __builtin__ |
|
71 | 71 | import __main__ |
|
72 | 72 | import glob |
|
73 | 73 | import inspect |
|
74 | 74 | import itertools |
|
75 | 75 | import keyword |
|
76 | 76 | import os |
|
77 | 77 | import re |
|
78 | 78 | import shlex |
|
79 | 79 | import sys |
|
80 | 80 | |
|
81 | 81 | from IPython.config.configurable import Configurable |
|
82 | 82 | from IPython.core.error import TryNext |
|
83 | 83 | from IPython.core.prefilter import ESC_MAGIC |
|
84 | 84 | from IPython.utils import generics |
|
85 | 85 | from IPython.utils import io |
|
86 | 86 | from IPython.utils.dir2 import dir2 |
|
87 | 87 | from IPython.utils.process import arg_split |
|
88 | 88 | from IPython.utils.traitlets import CBool |
|
89 | 89 | |
|
90 | 90 | #----------------------------------------------------------------------------- |
|
91 | 91 | # Globals |
|
92 | 92 | #----------------------------------------------------------------------------- |
|
93 | 93 | |
|
94 | 94 | # Public API |
|
95 | 95 | __all__ = ['Completer','IPCompleter'] |
|
96 | 96 | |
|
97 | 97 | if sys.platform == 'win32': |
|
98 | 98 | PROTECTABLES = ' ' |
|
99 | 99 | else: |
|
100 | 100 | PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&' |
|
101 | 101 | |
|
102 | 102 | #----------------------------------------------------------------------------- |
|
103 | 103 | # Main functions and classes |
|
104 | 104 | #----------------------------------------------------------------------------- |
|
105 | 105 | |
|
106 | 106 | def has_open_quotes(s): |
|
107 | 107 | """Return whether a string has open quotes. |
|
108 | 108 | |
|
109 | 109 | This simply counts whether the number of quote characters of either type in |
|
110 | 110 | the string is odd. |
|
111 | 111 | |
|
112 | 112 | Returns |
|
113 | 113 | ------- |
|
114 | 114 | If there is an open quote, the quote character is returned. Else, return |
|
115 | 115 | False. |
|
116 | 116 | """ |
|
117 | 117 | # We check " first, then ', so complex cases with nested quotes will get |
|
118 | 118 | # the " to take precedence. |
|
119 | 119 | if s.count('"') % 2: |
|
120 | 120 | return '"' |
|
121 | 121 | elif s.count("'") % 2: |
|
122 | 122 | return "'" |
|
123 | 123 | else: |
|
124 | 124 | return False |
|
125 | 125 | |
|
126 | 126 | |
|
127 | 127 | def protect_filename(s): |
|
128 | 128 | """Escape a string to protect certain characters.""" |
|
129 | ||
|
129 | ||
|
130 | 130 | return "".join([(ch in PROTECTABLES and '\\' + ch or ch) |
|
131 | 131 | for ch in s]) |
|
132 | 132 | |
|
133 | 133 | |
|
134 | 134 | def mark_dirs(matches): |
|
135 | 135 | """Mark directories in input list by appending '/' to their names.""" |
|
136 | 136 | out = [] |
|
137 | 137 | isdir = os.path.isdir |
|
138 | 138 | for x in matches: |
|
139 | 139 | if isdir(x): |
|
140 | 140 | out.append(x+'/') |
|
141 | 141 | else: |
|
142 | 142 | out.append(x) |
|
143 | 143 | return out |
|
144 | 144 | |
|
145 | 145 | |
|
146 | 146 | def expand_user(path): |
|
147 | 147 | """Expand '~'-style usernames in strings. |
|
148 | 148 | |
|
149 | 149 | This is similar to :func:`os.path.expanduser`, but it computes and returns |
|
150 | 150 | extra information that will be useful if the input was being used in |
|
151 | 151 | computing completions, and you wish to return the completions with the |
|
152 | 152 | original '~' instead of its expanded value. |
|
153 | 153 | |
|
154 | 154 | Parameters |
|
155 | 155 | ---------- |
|
156 | 156 | path : str |
|
157 | 157 | String to be expanded. If no ~ is present, the output is the same as the |
|
158 | 158 | input. |
|
159 | ||
|
159 | ||
|
160 | 160 | Returns |
|
161 | 161 | ------- |
|
162 | 162 | newpath : str |
|
163 | 163 | Result of ~ expansion in the input path. |
|
164 | 164 | tilde_expand : bool |
|
165 | 165 | Whether any expansion was performed or not. |
|
166 | 166 | tilde_val : str |
|
167 | 167 | The value that ~ was replaced with. |
|
168 | 168 | """ |
|
169 | 169 | # Default values |
|
170 | 170 | tilde_expand = False |
|
171 | 171 | tilde_val = '' |
|
172 | 172 | newpath = path |
|
173 | ||
|
173 | ||
|
174 | 174 | if path.startswith('~'): |
|
175 | 175 | tilde_expand = True |
|
176 | 176 | rest = path[1:] |
|
177 | 177 | newpath = os.path.expanduser(path) |
|
178 | 178 | tilde_val = newpath.replace(rest, '') |
|
179 | 179 | |
|
180 | 180 | return newpath, tilde_expand, tilde_val |
|
181 | 181 | |
|
182 | 182 | |
|
183 | 183 | def compress_user(path, tilde_expand, tilde_val): |
|
184 | 184 | """Does the opposite of expand_user, with its outputs. |
|
185 | 185 | """ |
|
186 | 186 | if tilde_expand: |
|
187 | 187 | return path.replace(tilde_val, '~') |
|
188 | 188 | else: |
|
189 | 189 | return path |
|
190 | 190 | |
|
191 | 191 | |
|
192 | 192 | def single_dir_expand(matches): |
|
193 | 193 | "Recursively expand match lists containing a single dir." |
|
194 | 194 | |
|
195 | 195 | if len(matches) == 1 and os.path.isdir(matches[0]): |
|
196 | 196 | # Takes care of links to directories also. Use '/' |
|
197 | 197 | # explicitly, even under Windows, so that name completions |
|
198 | 198 | # don't end up escaped. |
|
199 | 199 | d = matches[0] |
|
200 | 200 | if d[-1] in ['/','\\']: |
|
201 | 201 | d = d[:-1] |
|
202 | 202 | |
|
203 | 203 | subdirs = os.listdir(d) |
|
204 | 204 | if subdirs: |
|
205 | 205 | matches = [ (d + '/' + p) for p in subdirs] |
|
206 | 206 | return single_dir_expand(matches) |
|
207 | 207 | else: |
|
208 | 208 | return matches |
|
209 | 209 | else: |
|
210 | 210 | return matches |
|
211 | 211 | |
|
212 | 212 | |
|
213 | 213 | class Bunch(object): pass |
|
214 | 214 | |
|
215 | 215 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' |
|
216 | 216 | GREEDY_DELIMS = ' \r\n' |
|
217 | 217 | |
|
218 | 218 | class CompletionSplitter(object): |
|
219 | 219 | """An object to split an input line in a manner similar to readline. |
|
220 | 220 | |
|
221 | 221 | By having our own implementation, we can expose readline-like completion in |
|
222 | 222 | a uniform manner to all frontends. This object only needs to be given the |
|
223 | 223 | line of text to be split and the cursor position on said line, and it |
|
224 | 224 | returns the 'word' to be completed on at the cursor after splitting the |
|
225 | 225 | entire line. |
|
226 | 226 | |
|
227 | 227 | What characters are used as splitting delimiters can be controlled by |
|
228 | 228 | setting the `delims` attribute (this is a property that internally |
|
229 | 229 | automatically builds the necessary """ |
|
230 | 230 | |
|
231 | 231 | # Private interface |
|
232 | ||
|
232 | ||
|
233 | 233 | # A string of delimiter characters. The default value makes sense for |
|
234 | 234 | # IPython's most typical usage patterns. |
|
235 | 235 | _delims = DELIMS |
|
236 | 236 | |
|
237 | 237 | # The expression (a normal string) to be compiled into a regular expression |
|
238 | 238 | # for actual splitting. We store it as an attribute mostly for ease of |
|
239 | 239 | # debugging, since this type of code can be so tricky to debug. |
|
240 | 240 | _delim_expr = None |
|
241 | 241 | |
|
242 | 242 | # The regular expression that does the actual splitting |
|
243 | 243 | _delim_re = None |
|
244 | 244 | |
|
245 | 245 | def __init__(self, delims=None): |
|
246 | 246 | delims = CompletionSplitter._delims if delims is None else delims |
|
247 | 247 | self.set_delims(delims) |
|
248 | 248 | |
|
249 | 249 | def set_delims(self, delims): |
|
250 | 250 | """Set the delimiters for line splitting.""" |
|
251 | 251 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' |
|
252 | 252 | self._delim_re = re.compile(expr) |
|
253 | 253 | self._delims = delims |
|
254 | 254 | self._delim_expr = expr |
|
255 | 255 | |
|
256 | 256 | def get_delims(self): |
|
257 | 257 | """Return the string of delimiter characters.""" |
|
258 | 258 | return self._delims |
|
259 | 259 | |
|
260 | 260 | def split_line(self, line, cursor_pos=None): |
|
261 | 261 | """Split a line of text with a cursor at the given position. |
|
262 | 262 | """ |
|
263 | 263 | l = line if cursor_pos is None else line[:cursor_pos] |
|
264 | 264 | return self._delim_re.split(l)[-1] |
|
265 | 265 | |
|
266 | 266 | |
|
267 | 267 | class Completer(Configurable): |
|
268 | ||
|
268 | ||
|
269 | 269 | greedy = CBool(False, config=True, |
|
270 | 270 | help="""Activate greedy completion |
|
271 | ||
|
271 | ||
|
272 | 272 | This will enable completion on elements of lists, results of function calls, etc., |
|
273 | 273 | but can be unsafe because the code is actually evaluated on TAB. |
|
274 | 274 | """ |
|
275 | 275 | ) |
|
276 | ||
|
276 | ||
|
277 | 277 | def __init__(self, namespace=None, global_namespace=None, config=None): |
|
278 | 278 | """Create a new completer for the command line. |
|
279 | 279 | |
|
280 | 280 | Completer(namespace=ns,global_namespace=ns2) -> completer instance. |
|
281 | 281 | |
|
282 | 282 | If unspecified, the default namespace where completions are performed |
|
283 | 283 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
284 | 284 | given as dictionaries. |
|
285 | 285 | |
|
286 | 286 | An optional second namespace can be given. This allows the completer |
|
287 | 287 | to handle cases where both the local and global scopes need to be |
|
288 | 288 | distinguished. |
|
289 | 289 | |
|
290 | 290 | Completer instances should be used as the completion mechanism of |
|
291 | 291 | readline via the set_completer() call: |
|
292 | 292 | |
|
293 | 293 | readline.set_completer(Completer(my_namespace).complete) |
|
294 | 294 | """ |
|
295 | 295 | |
|
296 | 296 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
297 | 297 | # specific namespace or to use __main__.__dict__. This will allow us |
|
298 | 298 | # to bind to __main__.__dict__ at completion time, not now. |
|
299 | 299 | if namespace is None: |
|
300 | 300 | self.use_main_ns = 1 |
|
301 | 301 | else: |
|
302 | 302 | self.use_main_ns = 0 |
|
303 | 303 | self.namespace = namespace |
|
304 | 304 | |
|
305 | 305 | # The global namespace, if given, can be bound directly |
|
306 | 306 | if global_namespace is None: |
|
307 | 307 | self.global_namespace = {} |
|
308 | 308 | else: |
|
309 | 309 | self.global_namespace = global_namespace |
|
310 | ||
|
310 | ||
|
311 | 311 | super(Completer, self).__init__(config=config) |
|
312 | 312 | |
|
313 | 313 | def complete(self, text, state): |
|
314 | 314 | """Return the next possible completion for 'text'. |
|
315 | 315 | |
|
316 | 316 | This is called successively with state == 0, 1, 2, ... until it |
|
317 | 317 | returns None. The completion should begin with 'text'. |
|
318 | 318 | |
|
319 | 319 | """ |
|
320 | 320 | if self.use_main_ns: |
|
321 | 321 | self.namespace = __main__.__dict__ |
|
322 | ||
|
322 | ||
|
323 | 323 | if state == 0: |
|
324 | 324 | if "." in text: |
|
325 | 325 | self.matches = self.attr_matches(text) |
|
326 | 326 | else: |
|
327 | 327 | self.matches = self.global_matches(text) |
|
328 | 328 | try: |
|
329 | 329 | return self.matches[state] |
|
330 | 330 | except IndexError: |
|
331 | 331 | return None |
|
332 | 332 | |
|
333 | 333 | def global_matches(self, text): |
|
334 | 334 | """Compute matches when text is a simple name. |
|
335 | 335 | |
|
336 | 336 | Return a list of all keywords, built-in functions and names currently |
|
337 | 337 | defined in self.namespace or self.global_namespace that match. |
|
338 | 338 | |
|
339 | 339 | """ |
|
340 | 340 | #print 'Completer->global_matches, txt=%r' % text # dbg |
|
341 | 341 | matches = [] |
|
342 | 342 | match_append = matches.append |
|
343 | 343 | n = len(text) |
|
344 | 344 | for lst in [keyword.kwlist, |
|
345 | 345 | __builtin__.__dict__.keys(), |
|
346 | 346 | self.namespace.keys(), |
|
347 | 347 | self.global_namespace.keys()]: |
|
348 | 348 | for word in lst: |
|
349 | 349 | if word[:n] == text and word != "__builtins__": |
|
350 | 350 | match_append(word) |
|
351 | 351 | return matches |
|
352 | 352 | |
|
353 | 353 | def attr_matches(self, text): |
|
354 | 354 | """Compute matches when text contains a dot. |
|
355 | 355 | |
|
356 | 356 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
357 | 357 | evaluatable in self.namespace or self.global_namespace, it will be |
|
358 | 358 | evaluated and its attributes (as revealed by dir()) are used as |
|
359 | 359 | possible completions. (For class instances, class members are are |
|
360 | 360 | also considered.) |
|
361 | 361 | |
|
362 | 362 | WARNING: this can still invoke arbitrary C code, if an object |
|
363 | 363 | with a __getattr__ hook is evaluated. |
|
364 | 364 | |
|
365 | 365 | """ |
|
366 | 366 | |
|
367 | 367 | #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg |
|
368 | 368 | # Another option, seems to work great. Catches things like ''.<tab> |
|
369 | 369 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
370 | 370 | |
|
371 | 371 | if m: |
|
372 |
expr, attr = m.group(1, 3) |
|
|
372 | expr, attr = m.group(1, 3) | |
|
373 | 373 | elif self.greedy: |
|
374 | 374 | m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer) |
|
375 | 375 | if not m2: |
|
376 | 376 | return [] |
|
377 | 377 | expr, attr = m2.group(1,2) |
|
378 | 378 | else: |
|
379 | 379 | return [] |
|
380 | ||
|
380 | ||
|
381 | 381 | try: |
|
382 | 382 | obj = eval(expr, self.namespace) |
|
383 | 383 | except: |
|
384 | 384 | try: |
|
385 | 385 | obj = eval(expr, self.global_namespace) |
|
386 | 386 | except: |
|
387 | 387 | return [] |
|
388 | 388 | |
|
389 | 389 | words = dir2(obj) |
|
390 | ||
|
390 | ||
|
391 | 391 | try: |
|
392 | 392 | words = generics.complete_object(obj, words) |
|
393 | 393 | except TryNext: |
|
394 | 394 | pass |
|
395 | 395 | # Build match list to return |
|
396 | 396 | n = len(attr) |
|
397 | 397 | res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ] |
|
398 | 398 | return res |
|
399 | 399 | |
|
400 | 400 | |
|
401 | 401 | class IPCompleter(Completer): |
|
402 | 402 | """Extension of the completer class with IPython-specific features""" |
|
403 | 403 | |
|
404 | 404 | def _greedy_changed(self, name, old, new): |
|
405 | 405 | """update the splitter and readline delims when greedy is changed""" |
|
406 | 406 | if new: |
|
407 | 407 | self.splitter.set_delims(GREEDY_DELIMS) |
|
408 | 408 | else: |
|
409 | 409 | self.splitter.set_delims(DELIMS) |
|
410 | ||
|
410 | ||
|
411 | 411 | if self.readline: |
|
412 | 412 | self.readline.set_completer_delims(self.splitter.get_delims()) |
|
413 | ||
|
413 | ||
|
414 | 414 | def __init__(self, shell=None, namespace=None, global_namespace=None, |
|
415 | 415 | omit__names=True, alias_table=None, use_readline=True, |
|
416 | 416 | config=None): |
|
417 | 417 | """IPCompleter() -> completer |
|
418 | 418 | |
|
419 | 419 | Return a completer object suitable for use by the readline library |
|
420 | 420 | via readline.set_completer(). |
|
421 | 421 | |
|
422 | 422 | Inputs: |
|
423 | 423 | |
|
424 | 424 | - shell: a pointer to the ipython shell itself. This is needed |
|
425 | 425 | because this completer knows about magic functions, and those can |
|
426 | 426 | only be accessed via the ipython instance. |
|
427 | 427 | |
|
428 | 428 | - namespace: an optional dict where completions are performed. |
|
429 | 429 | |
|
430 | 430 | - global_namespace: secondary optional dict for completions, to |
|
431 | 431 | handle cases (such as IPython embedded inside functions) where |
|
432 | 432 | both Python scopes are visible. |
|
433 | 433 | |
|
434 | 434 | - The optional omit__names parameter sets the completer to omit the |
|
435 | 435 | 'magic' names (__magicname__) for python objects unless the text |
|
436 | 436 | to be completed explicitly starts with one or more underscores. |
|
437 | 437 | |
|
438 | 438 | - If alias_table is supplied, it should be a dictionary of aliases |
|
439 | 439 | to complete. |
|
440 | 440 | |
|
441 | 441 | use_readline : bool, optional |
|
442 | 442 | If true, use the readline library. This completer can still function |
|
443 | 443 | without readline, though in that case callers must provide some extra |
|
444 | 444 | information on each call about the current line.""" |
|
445 | 445 | |
|
446 | 446 | self.magic_escape = ESC_MAGIC |
|
447 | 447 | self.splitter = CompletionSplitter() |
|
448 | 448 | |
|
449 | 449 | # Readline configuration, only used by the rlcompleter method. |
|
450 | 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 | 452 | import IPython.utils.rlineimpl as readline |
|
453 | 453 | self.readline = readline |
|
454 | 454 | else: |
|
455 | 455 | self.readline = None |
|
456 | 456 | |
|
457 | 457 | # _greedy_changed() depends on splitter and readline being defined: |
|
458 | 458 | Completer.__init__(self, namespace=namespace, global_namespace=global_namespace, |
|
459 | 459 | config=config) |
|
460 | 460 | |
|
461 | 461 | # List where completion matches will be stored |
|
462 | 462 | self.matches = [] |
|
463 | 463 | self.omit__names = omit__names |
|
464 | 464 | self.merge_completions = shell.readline_merge_completions |
|
465 | 465 | self.shell = shell.shell |
|
466 | 466 | if alias_table is None: |
|
467 | 467 | alias_table = {} |
|
468 | 468 | self.alias_table = alias_table |
|
469 | 469 | # Regexp to split filenames with spaces in them |
|
470 | 470 | self.space_name_re = re.compile(r'([^\\] )') |
|
471 | 471 | # Hold a local ref. to glob.glob for speed |
|
472 | 472 | self.glob = glob.glob |
|
473 | 473 | |
|
474 | 474 | # Determine if we are running on 'dumb' terminals, like (X)Emacs |
|
475 | 475 | # buffers, to avoid completion problems. |
|
476 | 476 | term = os.environ.get('TERM','xterm') |
|
477 | 477 | self.dumb_terminal = term in ['dumb','emacs'] |
|
478 | ||
|
478 | ||
|
479 | 479 | # Special handling of backslashes needed in win32 platforms |
|
480 | 480 | if sys.platform == "win32": |
|
481 | 481 | self.clean_glob = self._clean_glob_win32 |
|
482 | 482 | else: |
|
483 | 483 | self.clean_glob = self._clean_glob |
|
484 | 484 | |
|
485 | 485 | # All active matcher routines for completion |
|
486 | 486 | self.matchers = [self.python_matches, |
|
487 | 487 | self.file_matches, |
|
488 | 488 | self.magic_matches, |
|
489 | 489 | self.alias_matches, |
|
490 | 490 | self.python_func_kw_matches, |
|
491 | 491 | ] |
|
492 | ||
|
492 | ||
|
493 | 493 | def all_completions(self, text): |
|
494 | 494 | """ |
|
495 | 495 | Wrapper around the complete method for the benefit of emacs |
|
496 | 496 | and pydb. |
|
497 | 497 | """ |
|
498 | 498 | return self.complete(text)[1] |
|
499 | 499 | |
|
500 | 500 | def _clean_glob(self,text): |
|
501 | 501 | return self.glob("%s*" % text) |
|
502 | 502 | |
|
503 | 503 | def _clean_glob_win32(self,text): |
|
504 | 504 | return [f.replace("\\","/") |
|
505 |
for f in self.glob("%s*" % text)] |
|
|
505 | for f in self.glob("%s*" % text)] | |
|
506 | 506 | |
|
507 | 507 | def file_matches(self, text): |
|
508 | 508 | """Match filenames, expanding ~USER type strings. |
|
509 | 509 | |
|
510 | 510 | Most of the seemingly convoluted logic in this completer is an |
|
511 | 511 | attempt to handle filenames with spaces in them. And yet it's not |
|
512 | 512 | quite perfect, because Python's readline doesn't expose all of the |
|
513 | 513 | GNU readline details needed for this to be done correctly. |
|
514 | 514 | |
|
515 | 515 | For a filename with a space in it, the printed completions will be |
|
516 | 516 | only the parts after what's already been typed (instead of the |
|
517 | 517 | full completions, as is normally done). I don't think with the |
|
518 | 518 | current (as of Python 2.3) Python readline it's possible to do |
|
519 | 519 | better.""" |
|
520 | 520 | |
|
521 | 521 | #io.rprint('Completer->file_matches: <%r>' % text) # dbg |
|
522 | 522 | |
|
523 | 523 | # chars that require escaping with backslash - i.e. chars |
|
524 | 524 | # that readline treats incorrectly as delimiters, but we |
|
525 | 525 | # don't want to treat as delimiters in filename matching |
|
526 | 526 | # when escaped with backslash |
|
527 | 527 | if text.startswith('!'): |
|
528 | 528 | text = text[1:] |
|
529 | 529 | text_prefix = '!' |
|
530 | 530 | else: |
|
531 | 531 | text_prefix = '' |
|
532 | 532 | |
|
533 | 533 | text_until_cursor = self.text_until_cursor |
|
534 | 534 | # track strings with open quotes |
|
535 | 535 | open_quotes = has_open_quotes(text_until_cursor) |
|
536 | 536 | |
|
537 | 537 | if '(' in text_until_cursor or '[' in text_until_cursor: |
|
538 | 538 | lsplit = text |
|
539 | 539 | else: |
|
540 | 540 | try: |
|
541 | 541 | # arg_split ~ shlex.split, but with unicode bugs fixed by us |
|
542 | 542 | lsplit = arg_split(text_until_cursor)[-1] |
|
543 | 543 | except ValueError: |
|
544 | 544 | # typically an unmatched ", or backslash without escaped char. |
|
545 | 545 | if open_quotes: |
|
546 | 546 | lsplit = text_until_cursor.split(open_quotes)[-1] |
|
547 | 547 | else: |
|
548 | 548 | return [] |
|
549 | 549 | except IndexError: |
|
550 | 550 | # tab pressed on empty line |
|
551 | 551 | lsplit = "" |
|
552 | 552 | |
|
553 | 553 | if not open_quotes and lsplit != protect_filename(lsplit): |
|
554 | 554 | # if protectables are found, do matching on the whole escaped name |
|
555 | 555 | has_protectables = True |
|
556 | 556 | text0,text = text,lsplit |
|
557 | 557 | else: |
|
558 | 558 | has_protectables = False |
|
559 | 559 | text = os.path.expanduser(text) |
|
560 | 560 | |
|
561 | 561 | if text == "": |
|
562 | 562 | return [text_prefix + protect_filename(f) for f in self.glob("*")] |
|
563 | 563 | |
|
564 | 564 | # Compute the matches from the filesystem |
|
565 | 565 | m0 = self.clean_glob(text.replace('\\','')) |
|
566 | 566 | |
|
567 | 567 | if has_protectables: |
|
568 | 568 | # If we had protectables, we need to revert our changes to the |
|
569 | 569 | # beginning of filename so that we don't double-write the part |
|
570 | 570 | # of the filename we have so far |
|
571 | 571 | len_lsplit = len(lsplit) |
|
572 |
matches = [text_prefix + text0 + |
|
|
572 | matches = [text_prefix + text0 + | |
|
573 | 573 | protect_filename(f[len_lsplit:]) for f in m0] |
|
574 | 574 | else: |
|
575 | 575 | if open_quotes: |
|
576 | 576 | # if we have a string with an open quote, we don't need to |
|
577 | 577 | # protect the names at all (and we _shouldn't_, as it |
|
578 | 578 | # would cause bugs when the filesystem call is made). |
|
579 | 579 | matches = m0 |
|
580 | 580 | else: |
|
581 |
matches = [text_prefix + |
|
|
581 | matches = [text_prefix + | |
|
582 | 582 | protect_filename(f) for f in m0] |
|
583 | 583 | |
|
584 | 584 | #io.rprint('mm', matches) # dbg |
|
585 | 585 | return mark_dirs(matches) |
|
586 | 586 | |
|
587 | 587 | def magic_matches(self, text): |
|
588 | 588 | """Match magics""" |
|
589 | 589 | #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg |
|
590 | 590 | # Get all shell magics now rather than statically, so magics loaded at |
|
591 | 591 | # runtime show up too |
|
592 | 592 | magics = self.shell.lsmagic() |
|
593 | 593 | pre = self.magic_escape |
|
594 | 594 | baretext = text.lstrip(pre) |
|
595 | 595 | return [ pre+m for m in magics if m.startswith(baretext)] |
|
596 | 596 | |
|
597 | 597 | def alias_matches(self, text): |
|
598 |
"""Match internal system aliases""" |
|
|
598 | """Match internal system aliases""" | |
|
599 | 599 | #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg |
|
600 | 600 | |
|
601 | 601 | # if we are not in the first 'item', alias matching |
|
602 | 602 | # doesn't make sense - unless we are starting with 'sudo' command. |
|
603 | 603 | main_text = self.text_until_cursor.lstrip() |
|
604 | 604 | if ' ' in main_text and not main_text.startswith('sudo'): |
|
605 | 605 | return [] |
|
606 | 606 | text = os.path.expanduser(text) |
|
607 | 607 | aliases = self.alias_table.keys() |
|
608 | 608 | if text == '': |
|
609 | 609 | return aliases |
|
610 | 610 | else: |
|
611 | 611 | return [a for a in aliases if a.startswith(text)] |
|
612 | 612 | |
|
613 | 613 | def python_matches(self,text): |
|
614 | 614 | """Match attributes or global python names""" |
|
615 | 615 | |
|
616 | 616 | #io.rprint('Completer->python_matches, txt=%r' % text) # dbg |
|
617 | 617 | if "." in text: |
|
618 | 618 | try: |
|
619 | 619 | matches = self.attr_matches(text) |
|
620 | 620 | if text.endswith('.') and self.omit__names: |
|
621 | 621 | if self.omit__names == 1: |
|
622 | 622 | # true if txt is _not_ a __ name, false otherwise: |
|
623 | 623 | no__name = (lambda txt: |
|
624 | 624 | re.match(r'.*\.__.*?__',txt) is None) |
|
625 | 625 | else: |
|
626 | 626 | # true if txt is _not_ a _ name, false otherwise: |
|
627 | 627 | no__name = (lambda txt: |
|
628 | 628 | re.match(r'.*\._.*?',txt) is None) |
|
629 | 629 | matches = filter(no__name, matches) |
|
630 | 630 | except NameError: |
|
631 | 631 | # catches <undefined attributes>.<tab> |
|
632 | 632 | matches = [] |
|
633 | 633 | else: |
|
634 | 634 | matches = self.global_matches(text) |
|
635 | 635 | |
|
636 | 636 | return matches |
|
637 | 637 | |
|
638 | 638 | def _default_arguments(self, obj): |
|
639 | 639 | """Return the list of default arguments of obj if it is callable, |
|
640 | 640 | or empty list otherwise.""" |
|
641 | 641 | |
|
642 | 642 | if not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
643 | 643 | # for classes, check for __init__,__new__ |
|
644 | 644 | if inspect.isclass(obj): |
|
645 | 645 | obj = (getattr(obj,'__init__',None) or |
|
646 | 646 | getattr(obj,'__new__',None)) |
|
647 | 647 | # for all others, check if they are __call__able |
|
648 | 648 | elif hasattr(obj, '__call__'): |
|
649 | 649 | obj = obj.__call__ |
|
650 | 650 | # XXX: is there a way to handle the builtins ? |
|
651 | 651 | try: |
|
652 | 652 | args,_,_1,defaults = inspect.getargspec(obj) |
|
653 | 653 | if defaults: |
|
654 | 654 | return args[-len(defaults):] |
|
655 | 655 | except TypeError: pass |
|
656 | 656 | return [] |
|
657 | 657 | |
|
658 | 658 | def python_func_kw_matches(self,text): |
|
659 | 659 | """Match named parameters (kwargs) of the last open function""" |
|
660 | 660 | |
|
661 | 661 | if "." in text: # a parameter cannot be dotted |
|
662 | 662 | return [] |
|
663 | 663 | try: regexp = self.__funcParamsRegex |
|
664 | 664 | except AttributeError: |
|
665 | 665 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
666 | 666 | '.*?' | # single quoted strings or |
|
667 | 667 | ".*?" | # double quoted strings or |
|
668 | 668 | \w+ | # identifier |
|
669 | 669 | \S # other characters |
|
670 | 670 | ''', re.VERBOSE | re.DOTALL) |
|
671 | 671 | # 1. find the nearest identifier that comes before an unclosed |
|
672 | 672 | # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo" |
|
673 | 673 | tokens = regexp.findall(self.line_buffer) |
|
674 | 674 | tokens.reverse() |
|
675 | 675 | iterTokens = iter(tokens); openPar = 0 |
|
676 | 676 | for token in iterTokens: |
|
677 | 677 | if token == ')': |
|
678 | 678 | openPar -= 1 |
|
679 | 679 | elif token == '(': |
|
680 | 680 | openPar += 1 |
|
681 | 681 | if openPar > 0: |
|
682 | 682 | # found the last unclosed parenthesis |
|
683 | 683 | break |
|
684 | 684 | else: |
|
685 | 685 | return [] |
|
686 | 686 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) |
|
687 | 687 | ids = [] |
|
688 | 688 | isId = re.compile(r'\w+$').match |
|
689 | 689 | while True: |
|
690 | 690 | try: |
|
691 | 691 | ids.append(iterTokens.next()) |
|
692 | 692 | if not isId(ids[-1]): |
|
693 | 693 | ids.pop(); break |
|
694 | 694 | if not iterTokens.next() == '.': |
|
695 | 695 | break |
|
696 | 696 | except StopIteration: |
|
697 | 697 | break |
|
698 | 698 | # lookup the candidate callable matches either using global_matches |
|
699 | 699 | # or attr_matches for dotted names |
|
700 | 700 | if len(ids) == 1: |
|
701 | 701 | callableMatches = self.global_matches(ids[0]) |
|
702 | 702 | else: |
|
703 | 703 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
704 | 704 | argMatches = [] |
|
705 | 705 | for callableMatch in callableMatches: |
|
706 | 706 | try: |
|
707 | 707 | namedArgs = self._default_arguments(eval(callableMatch, |
|
708 | 708 | self.namespace)) |
|
709 | 709 | except: |
|
710 | 710 | continue |
|
711 | 711 | for namedArg in namedArgs: |
|
712 | 712 | if namedArg.startswith(text): |
|
713 | 713 | argMatches.append("%s=" %namedArg) |
|
714 | 714 | return argMatches |
|
715 | 715 | |
|
716 | 716 | def dispatch_custom_completer(self, text): |
|
717 | 717 | #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg |
|
718 |
line = self.line_buffer |
|
|
718 | line = self.line_buffer | |
|
719 | 719 | if not line.strip(): |
|
720 | 720 | return None |
|
721 | ||
|
721 | ||
|
722 | 722 | # Create a little structure to pass all the relevant information about |
|
723 | 723 | # the current completion to any custom completer. |
|
724 | 724 | event = Bunch() |
|
725 | 725 | event.line = line |
|
726 | 726 | event.symbol = text |
|
727 | 727 | cmd = line.split(None,1)[0] |
|
728 | 728 | event.command = cmd |
|
729 | 729 | event.text_until_cursor = self.text_until_cursor |
|
730 | ||
|
730 | ||
|
731 | 731 | #print "\ncustom:{%s]\n" % event # dbg |
|
732 | ||
|
732 | ||
|
733 | 733 | # for foo etc, try also to find completer for %foo |
|
734 | 734 | if not cmd.startswith(self.magic_escape): |
|
735 | 735 | try_magic = self.custom_completers.s_matches( |
|
736 |
self.magic_escape + cmd) |
|
|
736 | self.magic_escape + cmd) | |
|
737 | 737 | else: |
|
738 |
try_magic = [] |
|
|
739 | ||
|
738 | try_magic = [] | |
|
739 | ||
|
740 | 740 | for c in itertools.chain(self.custom_completers.s_matches(cmd), |
|
741 | 741 | try_magic, |
|
742 | 742 | self.custom_completers.flat_matches(self.text_until_cursor)): |
|
743 | 743 | #print "try",c # dbg |
|
744 | 744 | try: |
|
745 | 745 | res = c(event) |
|
746 | 746 | if res: |
|
747 | 747 | # first, try case sensitive match |
|
748 | 748 | withcase = [r for r in res if r.startswith(text)] |
|
749 | 749 | if withcase: |
|
750 | 750 | return withcase |
|
751 | 751 | # if none, then case insensitive ones are ok too |
|
752 | 752 | text_low = text.lower() |
|
753 | 753 | return [r for r in res if r.lower().startswith(text_low)] |
|
754 | 754 | except TryNext: |
|
755 | 755 | pass |
|
756 | ||
|
756 | ||
|
757 | 757 | return None |
|
758 | ||
|
758 | ||
|
759 | 759 | def complete(self, text=None, line_buffer=None, cursor_pos=None): |
|
760 | 760 | """Find completions for the given text and line context. |
|
761 | 761 | |
|
762 | 762 | This is called successively with state == 0, 1, 2, ... until it |
|
763 | 763 | returns None. The completion should begin with 'text'. |
|
764 | 764 | |
|
765 | 765 | Note that both the text and the line_buffer are optional, but at least |
|
766 | 766 | one of them must be given. |
|
767 | 767 | |
|
768 | 768 | Parameters |
|
769 | 769 | ---------- |
|
770 | 770 | text : string, optional |
|
771 | 771 | Text to perform the completion on. If not given, the line buffer |
|
772 | 772 | is split using the instance's CompletionSplitter object. |
|
773 | 773 | |
|
774 | 774 | line_buffer : string, optional |
|
775 | 775 | If not given, the completer attempts to obtain the current line |
|
776 | 776 | buffer via readline. This keyword allows clients which are |
|
777 | 777 | requesting for text completions in non-readline contexts to inform |
|
778 | 778 | the completer of the entire text. |
|
779 | 779 | |
|
780 | 780 | cursor_pos : int, optional |
|
781 | 781 | Index of the cursor in the full line buffer. Should be provided by |
|
782 | 782 | remote frontends where kernel has no access to frontend state. |
|
783 | 783 | |
|
784 | 784 | Returns |
|
785 | 785 | ------- |
|
786 | 786 | text : str |
|
787 | 787 | Text that was actually used in the completion. |
|
788 | ||
|
788 | ||
|
789 | 789 | matches : list |
|
790 | 790 | A list of completion matches. |
|
791 | 791 | """ |
|
792 | 792 | #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg |
|
793 | 793 | |
|
794 | 794 | # if the cursor position isn't given, the only sane assumption we can |
|
795 | 795 | # make is that it's at the end of the line (the common case) |
|
796 | 796 | if cursor_pos is None: |
|
797 | 797 | cursor_pos = len(line_buffer) if text is None else len(text) |
|
798 | 798 | |
|
799 | 799 | # if text is either None or an empty string, rely on the line buffer |
|
800 | 800 | if not text: |
|
801 | 801 | text = self.splitter.split_line(line_buffer, cursor_pos) |
|
802 | 802 | |
|
803 | 803 | # If no line buffer is given, assume the input text is all there was |
|
804 | 804 | if line_buffer is None: |
|
805 | 805 | line_buffer = text |
|
806 | ||
|
806 | ||
|
807 | 807 | self.line_buffer = line_buffer |
|
808 | 808 | self.text_until_cursor = self.line_buffer[:cursor_pos] |
|
809 | 809 | #io.rprint('\nCOMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg |
|
810 | 810 | |
|
811 | 811 | # Start with a clean slate of completions |
|
812 | 812 | self.matches[:] = [] |
|
813 | 813 | custom_res = self.dispatch_custom_completer(text) |
|
814 | 814 | if custom_res is not None: |
|
815 | 815 | # did custom completers produce something? |
|
816 | 816 | self.matches = custom_res |
|
817 | 817 | else: |
|
818 | 818 | # Extend the list of completions with the results of each |
|
819 | 819 | # matcher, so we return results to the user from all |
|
820 | 820 | # namespaces. |
|
821 | 821 | if self.merge_completions: |
|
822 | 822 | self.matches = [] |
|
823 | 823 | for matcher in self.matchers: |
|
824 | 824 | try: |
|
825 | 825 | self.matches.extend(matcher(text)) |
|
826 | 826 | except: |
|
827 | 827 | # Show the ugly traceback if the matcher causes an |
|
828 | 828 | # exception, but do NOT crash the kernel! |
|
829 | 829 | sys.excepthook(*sys.exc_info()) |
|
830 | 830 | else: |
|
831 | 831 | for matcher in self.matchers: |
|
832 | 832 | self.matches = matcher(text) |
|
833 | 833 | if self.matches: |
|
834 | 834 | break |
|
835 | 835 | # FIXME: we should extend our api to return a dict with completions for |
|
836 | 836 | # different types of objects. The rlcomplete() method could then |
|
837 | 837 | # simply collapse the dict into a list for readline, but we'd have |
|
838 | 838 | # richer completion semantics in other evironments. |
|
839 | 839 | self.matches = sorted(set(self.matches)) |
|
840 | 840 | #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg |
|
841 | 841 | return text, self.matches |
|
842 | 842 | |
|
843 | 843 | def rlcomplete(self, text, state): |
|
844 | 844 | """Return the state-th possible completion for 'text'. |
|
845 | 845 | |
|
846 | 846 | This is called successively with state == 0, 1, 2, ... until it |
|
847 | 847 | returns None. The completion should begin with 'text'. |
|
848 | 848 | |
|
849 | 849 | Parameters |
|
850 | 850 | ---------- |
|
851 | 851 | text : string |
|
852 | 852 | Text to perform the completion on. |
|
853 | 853 | |
|
854 | 854 | state : int |
|
855 | 855 | Counter used by readline. |
|
856 | 856 | """ |
|
857 | 857 | if state==0: |
|
858 | 858 | |
|
859 | 859 | self.line_buffer = line_buffer = self.readline.get_line_buffer() |
|
860 | 860 | cursor_pos = self.readline.get_endidx() |
|
861 | 861 | |
|
862 | 862 | #io.rprint("\nRLCOMPLETE: %r %r %r" % |
|
863 | 863 | # (text, line_buffer, cursor_pos) ) # dbg |
|
864 | 864 | |
|
865 | 865 | # if there is only a tab on a line with only whitespace, instead of |
|
866 | 866 | # the mostly useless 'do you want to see all million completions' |
|
867 | 867 | # message, just do the right thing and give the user his tab! |
|
868 | 868 | # Incidentally, this enables pasting of tabbed text from an editor |
|
869 | 869 | # (as long as autoindent is off). |
|
870 | 870 | |
|
871 | 871 | # It should be noted that at least pyreadline still shows file |
|
872 | 872 | # completions - is there a way around it? |
|
873 | 873 | |
|
874 | 874 | # don't apply this on 'dumb' terminals, such as emacs buffers, so |
|
875 | 875 | # we don't interfere with their own tab-completion mechanism. |
|
876 | 876 | if not (self.dumb_terminal or line_buffer.strip()): |
|
877 | 877 | self.readline.insert_text('\t') |
|
878 | 878 | sys.stdout.flush() |
|
879 | 879 | return None |
|
880 | 880 | |
|
881 | 881 | # Note: debugging exceptions that may occur in completion is very |
|
882 | 882 | # tricky, because readline unconditionally silences them. So if |
|
883 | 883 | # during development you suspect a bug in the completion code, turn |
|
884 | 884 | # this flag on temporarily by uncommenting the second form (don't |
|
885 | 885 | # flip the value in the first line, as the '# dbg' marker can be |
|
886 | 886 | # automatically detected and is used elsewhere). |
|
887 | 887 | DEBUG = False |
|
888 | 888 | #DEBUG = True # dbg |
|
889 | 889 | if DEBUG: |
|
890 | 890 | try: |
|
891 | 891 | self.complete(text, line_buffer, cursor_pos) |
|
892 | 892 | except: |
|
893 | 893 | import traceback; traceback.print_exc() |
|
894 | 894 | else: |
|
895 | 895 | # The normal production version is here |
|
896 | ||
|
896 | ||
|
897 | 897 | # This method computes the self.matches array |
|
898 | 898 | self.complete(text, line_buffer, cursor_pos) |
|
899 | 899 | |
|
900 | 900 | try: |
|
901 | 901 | return self.matches[state] |
|
902 | 902 | except IndexError: |
|
903 | 903 | return None |
@@ -1,347 +1,347 b'' | |||
|
1 | 1 | """Implementations for various useful completers. |
|
2 | 2 | |
|
3 | 3 | These are all loaded by default by IPython. |
|
4 | 4 | """ |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Copyright (C) 2010 The IPython Development Team. |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the BSD License. |
|
9 | 9 | # |
|
10 | 10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | from __future__ import print_function |
|
17 | 17 | |
|
18 | 18 | # Stdlib imports |
|
19 | 19 | import glob |
|
20 | 20 | import inspect |
|
21 | 21 | import os |
|
22 | 22 | import re |
|
23 | 23 | import shlex |
|
24 | 24 | import sys |
|
25 | 25 | |
|
26 | 26 | # Third-party imports |
|
27 | 27 | from time import time |
|
28 | 28 | from zipimport import zipimporter |
|
29 | 29 | |
|
30 | 30 | # Our own imports |
|
31 | 31 | from IPython.core.completer import expand_user, compress_user |
|
32 | 32 | from IPython.core.error import TryNext |
|
33 | 33 | from IPython.utils import py3compat |
|
34 | 34 | |
|
35 | 35 | # FIXME: this should be pulled in with the right call via the component system |
|
36 | 36 | from IPython.core.ipapi import get as get_ipython |
|
37 | 37 | |
|
38 | 38 | #----------------------------------------------------------------------------- |
|
39 | 39 | # Globals and constants |
|
40 | 40 | #----------------------------------------------------------------------------- |
|
41 | 41 | |
|
42 | 42 | # Time in seconds after which the rootmodules will be stored permanently in the |
|
43 | 43 | # ipython ip.db database (kept in the user's .ipython dir). |
|
44 | 44 | TIMEOUT_STORAGE = 2 |
|
45 | 45 | |
|
46 | 46 | # Time in seconds after which we give up |
|
47 | 47 | TIMEOUT_GIVEUP = 20 |
|
48 | 48 | |
|
49 | 49 | # Regular expression for the python import statement |
|
50 | 50 | import_re = re.compile(r'.*(\.so|\.py[cod]?)$') |
|
51 | 51 | |
|
52 | 52 | # RE for the ipython %run command (python + ipython scripts) |
|
53 | 53 | magic_run_re = re.compile(r'.*(\.ipy|\.py[w]?)$') |
|
54 | 54 | |
|
55 | 55 | #----------------------------------------------------------------------------- |
|
56 | 56 | # Local utilities |
|
57 | 57 | #----------------------------------------------------------------------------- |
|
58 | 58 | |
|
59 | 59 | def shlex_split(x): |
|
60 | 60 | """Helper function to split lines into segments. |
|
61 | 61 | """ |
|
62 | 62 | # shlex.split raises an exception if there is a syntax error in sh syntax |
|
63 | 63 | # for example if no closing " is found. This function keeps dropping the |
|
64 | 64 | # last character of the line until shlex.split does not raise |
|
65 | 65 | # an exception. It adds end of the line to the result of shlex.split |
|
66 | 66 | # |
|
67 | 67 | # Example: |
|
68 | 68 | # %run "c:/python -> ['%run','"c:/python'] |
|
69 | 69 | |
|
70 | 70 | # shlex.split has unicode bugs in Python 2, so encode first to str |
|
71 | 71 | if not py3compat.PY3: |
|
72 | 72 | x = py3compat.cast_bytes(x) |
|
73 | 73 | |
|
74 | 74 | endofline = [] |
|
75 | 75 | while x != '': |
|
76 | 76 | try: |
|
77 | 77 | comps = shlex.split(x) |
|
78 | 78 | if len(endofline) >= 1: |
|
79 | 79 | comps.append(''.join(endofline)) |
|
80 | 80 | return comps |
|
81 | ||
|
81 | ||
|
82 | 82 | except ValueError: |
|
83 | 83 | endofline = [x[-1:]]+endofline |
|
84 | 84 | x = x[:-1] |
|
85 | 85 | |
|
86 | 86 | return [''.join(endofline)] |
|
87 | 87 | |
|
88 | 88 | def module_list(path): |
|
89 | 89 | """ |
|
90 | 90 | Return the list containing the names of the modules available in the given |
|
91 | 91 | folder. |
|
92 | 92 | """ |
|
93 | 93 | |
|
94 | 94 | if os.path.isdir(path): |
|
95 | 95 | folder_list = os.listdir(path) |
|
96 | 96 | elif path.endswith('.egg'): |
|
97 | 97 | try: |
|
98 | 98 | folder_list = [f for f in zipimporter(path)._files] |
|
99 | 99 | except: |
|
100 | 100 | folder_list = [] |
|
101 | 101 | else: |
|
102 | 102 | folder_list = [] |
|
103 | 103 | |
|
104 | 104 | if not folder_list: |
|
105 | 105 | return [] |
|
106 | 106 | |
|
107 | 107 | # A few local constants to be used in loops below |
|
108 | 108 | isfile = os.path.isfile |
|
109 | 109 | pjoin = os.path.join |
|
110 | 110 | basename = os.path.basename |
|
111 | ||
|
111 | ||
|
112 | 112 | # Now find actual path matches for packages or modules |
|
113 | 113 | folder_list = [p for p in folder_list |
|
114 | 114 | if isfile(pjoin(path, p,'__init__.py')) |
|
115 | 115 | or import_re.match(p) ] |
|
116 | 116 | |
|
117 | 117 | return [basename(p).split('.')[0] for p in folder_list] |
|
118 | 118 | |
|
119 | 119 | def get_root_modules(): |
|
120 | 120 | """ |
|
121 | 121 | Returns a list containing the names of all the modules available in the |
|
122 | 122 | folders of the pythonpath. |
|
123 | 123 | """ |
|
124 | 124 | ip = get_ipython() |
|
125 | 125 | |
|
126 | 126 | if 'rootmodules' in ip.db: |
|
127 | 127 | return ip.db['rootmodules'] |
|
128 | ||
|
128 | ||
|
129 | 129 | t = time() |
|
130 | 130 | store = False |
|
131 | 131 | modules = list(sys.builtin_module_names) |
|
132 | 132 | for path in sys.path: |
|
133 |
modules += module_list(path) |
|
|
133 | modules += module_list(path) | |
|
134 | 134 | if time() - t >= TIMEOUT_STORAGE and not store: |
|
135 | 135 | store = True |
|
136 | 136 | print("\nCaching the list of root modules, please wait!") |
|
137 | 137 | print("(This will only be done once - type '%rehashx' to " |
|
138 | 138 | "reset cache!)\n") |
|
139 | 139 | sys.stdout.flush() |
|
140 | 140 | if time() - t > TIMEOUT_GIVEUP: |
|
141 | 141 | print("This is taking too long, we give up.\n") |
|
142 | 142 | ip.db['rootmodules'] = [] |
|
143 | 143 | return [] |
|
144 | ||
|
144 | ||
|
145 | 145 | modules = set(modules) |
|
146 | 146 | if '__init__' in modules: |
|
147 | 147 | modules.remove('__init__') |
|
148 | 148 | modules = list(modules) |
|
149 | 149 | if store: |
|
150 | 150 | ip.db['rootmodules'] = modules |
|
151 | 151 | return modules |
|
152 | 152 | |
|
153 | 153 | |
|
154 | 154 | def is_importable(module, attr, only_modules): |
|
155 | 155 | if only_modules: |
|
156 | 156 | return inspect.ismodule(getattr(module, attr)) |
|
157 | 157 | else: |
|
158 | 158 | return not(attr[:2] == '__' and attr[-2:] == '__') |
|
159 | 159 | |
|
160 | 160 | |
|
161 | 161 | def try_import(mod, only_modules=False): |
|
162 | 162 | try: |
|
163 | 163 | m = __import__(mod) |
|
164 | 164 | except: |
|
165 | 165 | return [] |
|
166 | 166 | mods = mod.split('.') |
|
167 | 167 | for module in mods[1:]: |
|
168 | 168 | m = getattr(m, module) |
|
169 | 169 | |
|
170 | 170 | m_is_init = hasattr(m, '__file__') and '__init__' in m.__file__ |
|
171 | 171 | |
|
172 | 172 | completions = [] |
|
173 | 173 | if (not hasattr(m, '__file__')) or (not only_modules) or m_is_init: |
|
174 | 174 | completions.extend( [attr for attr in dir(m) if |
|
175 | 175 | is_importable(m, attr, only_modules)]) |
|
176 | ||
|
176 | ||
|
177 | 177 | completions.extend(getattr(m, '__all__', [])) |
|
178 | 178 | if m_is_init: |
|
179 | 179 | completions.extend(module_list(os.path.dirname(m.__file__))) |
|
180 | 180 | completions = set(completions) |
|
181 | 181 | if '__init__' in completions: |
|
182 | 182 | completions.remove('__init__') |
|
183 | 183 | return list(completions) |
|
184 | 184 | |
|
185 | 185 | |
|
186 | 186 | #----------------------------------------------------------------------------- |
|
187 | 187 | # Completion-related functions. |
|
188 | 188 | #----------------------------------------------------------------------------- |
|
189 | 189 | |
|
190 | 190 | def quick_completer(cmd, completions): |
|
191 | 191 | """ Easily create a trivial completer for a command. |
|
192 | 192 | |
|
193 | 193 | Takes either a list of completions, or all completions in string (that will |
|
194 | 194 | be split on whitespace). |
|
195 | ||
|
195 | ||
|
196 | 196 | Example:: |
|
197 | ||
|
197 | ||
|
198 | 198 | [d:\ipython]|1> import ipy_completers |
|
199 | 199 | [d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz']) |
|
200 | 200 | [d:\ipython]|3> foo b<TAB> |
|
201 | 201 | bar baz |
|
202 | 202 | [d:\ipython]|3> foo ba |
|
203 | 203 | """ |
|
204 | ||
|
204 | ||
|
205 | 205 | if isinstance(completions, basestring): |
|
206 | 206 | completions = completions.split() |
|
207 | 207 | |
|
208 | 208 | def do_complete(self, event): |
|
209 | 209 | return completions |
|
210 | ||
|
210 | ||
|
211 | 211 | get_ipython().set_hook('complete_command',do_complete, str_key = cmd) |
|
212 | 212 | |
|
213 | 213 | |
|
214 | 214 | def module_completion(line): |
|
215 | 215 | """ |
|
216 | 216 | Returns a list containing the completion possibilities for an import line. |
|
217 | 217 | |
|
218 | 218 | The line looks like this : |
|
219 | 219 | 'import xml.d' |
|
220 | 220 | 'from xml.dom import' |
|
221 | 221 | """ |
|
222 | 222 | |
|
223 | 223 | words = line.split(' ') |
|
224 | 224 | nwords = len(words) |
|
225 | 225 | |
|
226 | 226 | # from whatever <tab> -> 'import ' |
|
227 | 227 | if nwords == 3 and words[0] == 'from': |
|
228 | 228 | return ['import '] |
|
229 | ||
|
229 | ||
|
230 | 230 | # 'from xy<tab>' or 'import xy<tab>' |
|
231 | 231 | if nwords < 3 and (words[0] in ['import','from']) : |
|
232 | 232 | if nwords == 1: |
|
233 | 233 | return get_root_modules() |
|
234 | 234 | mod = words[1].split('.') |
|
235 | 235 | if len(mod) < 2: |
|
236 | 236 | return get_root_modules() |
|
237 | 237 | completion_list = try_import('.'.join(mod[:-1]), True) |
|
238 | 238 | return ['.'.join(mod[:-1] + [el]) for el in completion_list] |
|
239 | ||
|
239 | ||
|
240 | 240 | # 'from xyz import abc<tab>' |
|
241 | 241 | if nwords >= 3 and words[0] == 'from': |
|
242 | 242 | mod = words[1] |
|
243 | 243 | return try_import(mod) |
|
244 | 244 | |
|
245 | 245 | #----------------------------------------------------------------------------- |
|
246 | 246 | # Completers |
|
247 | 247 | #----------------------------------------------------------------------------- |
|
248 | 248 | # These all have the func(self, event) signature to be used as custom |
|
249 | 249 | # completers |
|
250 | 250 | |
|
251 | 251 | def module_completer(self,event): |
|
252 | 252 | """Give completions after user has typed 'import ...' or 'from ...'""" |
|
253 | 253 | |
|
254 | 254 | # This works in all versions of python. While 2.5 has |
|
255 | 255 | # pkgutil.walk_packages(), that particular routine is fairly dangerous, |
|
256 | 256 | # since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full |
|
257 | 257 | # of possibly problematic side effects. |
|
258 | 258 | # This search the folders in the sys.path for available modules. |
|
259 | 259 | |
|
260 | 260 | return module_completion(event.line) |
|
261 | 261 | |
|
262 | 262 | # FIXME: there's a lot of logic common to the run, cd and builtin file |
|
263 | 263 | # completers, that is currently reimplemented in each. |
|
264 | 264 | |
|
265 | 265 | def magic_run_completer(self, event): |
|
266 | 266 | """Complete files that end in .py or .ipy for the %run command. |
|
267 | 267 | """ |
|
268 | 268 | comps = shlex_split(event.line) |
|
269 | 269 | relpath = (len(comps) > 1 and comps[-1] or '').strip("'\"") |
|
270 | 270 | |
|
271 | 271 | #print("\nev=", event) # dbg |
|
272 | 272 | #print("rp=", relpath) # dbg |
|
273 | 273 | #print('comps=', comps) # dbg |
|
274 | 274 | |
|
275 | 275 | lglob = glob.glob |
|
276 | 276 | isdir = os.path.isdir |
|
277 | 277 | relpath, tilde_expand, tilde_val = expand_user(relpath) |
|
278 | ||
|
278 | ||
|
279 | 279 | dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)] |
|
280 | 280 | |
|
281 | 281 | # Find if the user has already typed the first filename, after which we |
|
282 | 282 | # should complete on all files, since after the first one other files may |
|
283 | 283 | # be arguments to the input script. |
|
284 | 284 | |
|
285 | 285 | if filter(magic_run_re.match, comps): |
|
286 | 286 | pys = [f.replace('\\','/') for f in lglob('*')] |
|
287 | 287 | else: |
|
288 | 288 | pys = [f.replace('\\','/') |
|
289 | 289 | for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') + |
|
290 | 290 | lglob(relpath + '*.pyw')] |
|
291 | 291 | #print('run comp:', dirs+pys) # dbg |
|
292 | 292 | return [compress_user(p, tilde_expand, tilde_val) for p in dirs+pys] |
|
293 | 293 | |
|
294 | 294 | |
|
295 | 295 | def cd_completer(self, event): |
|
296 | 296 | """Completer function for cd, which only returns directories.""" |
|
297 | 297 | ip = get_ipython() |
|
298 | 298 | relpath = event.symbol |
|
299 | 299 | |
|
300 | 300 | #print(event) # dbg |
|
301 | 301 | if event.line.endswith('-b') or ' -b ' in event.line: |
|
302 | 302 | # return only bookmark completions |
|
303 | 303 | bkms = self.db.get('bookmarks', None) |
|
304 | 304 | if bkms: |
|
305 | 305 | return bkms.keys() |
|
306 | 306 | else: |
|
307 | 307 | return [] |
|
308 | ||
|
308 | ||
|
309 | 309 | if event.symbol == '-': |
|
310 | 310 | width_dh = str(len(str(len(ip.user_ns['_dh']) + 1))) |
|
311 | 311 | # jump in directory history by number |
|
312 | 312 | fmt = '-%0' + width_dh +'d [%s]' |
|
313 | 313 | ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])] |
|
314 | 314 | if len(ents) > 1: |
|
315 | 315 | return ents |
|
316 | 316 | return [] |
|
317 | 317 | |
|
318 | 318 | if event.symbol.startswith('--'): |
|
319 | 319 | return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']] |
|
320 | 320 | |
|
321 | 321 | # Expand ~ in path and normalize directory separators. |
|
322 | 322 | relpath, tilde_expand, tilde_val = expand_user(relpath) |
|
323 | 323 | relpath = relpath.replace('\\','/') |
|
324 | 324 | |
|
325 | 325 | found = [] |
|
326 | 326 | for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*') |
|
327 | 327 | if os.path.isdir(f)]: |
|
328 | 328 | if ' ' in d: |
|
329 | 329 | # we don't want to deal with any of that, complex code |
|
330 | 330 | # for this is elsewhere |
|
331 | 331 | raise TryNext |
|
332 | ||
|
332 | ||
|
333 | 333 | found.append(d) |
|
334 | 334 | |
|
335 | 335 | if not found: |
|
336 | 336 | if os.path.isdir(relpath): |
|
337 | 337 | return [compress_user(relpath, tilde_expand, tilde_val)] |
|
338 | 338 | |
|
339 | 339 | # if no completions so far, try bookmarks |
|
340 | 340 | bks = self.db.get('bookmarks',{}).iterkeys() |
|
341 | 341 | bkmatches = [s for s in bks if s.startswith(event.symbol)] |
|
342 | 342 | if bkmatches: |
|
343 | 343 | return bkmatches |
|
344 | ||
|
344 | ||
|
345 | 345 | raise TryNext |
|
346 | 346 | |
|
347 | 347 | return [compress_user(p, tilde_expand, tilde_val) for p in found] |
@@ -1,181 +1,181 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """sys.excepthook for IPython itself, leaves a detailed report on disk. |
|
3 | 3 | |
|
4 | 4 | Authors: |
|
5 | 5 | |
|
6 | 6 | * Fernando Perez |
|
7 | 7 | * Brian E. Granger |
|
8 | 8 | """ |
|
9 | 9 | |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
12 | 12 | # Copyright (C) 2008-2010 The IPython Development Team |
|
13 | 13 | # |
|
14 | 14 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | 15 | # the file COPYING, distributed as part of this software. |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | # Imports |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | |
|
22 | 22 | import os |
|
23 | 23 | import sys |
|
24 | 24 | from pprint import pformat |
|
25 | 25 | |
|
26 | 26 | from IPython.core import ultratb |
|
27 | 27 | from IPython.utils.sysinfo import sys_info |
|
28 | 28 | |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | # Code |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | # Template for the user message. |
|
34 | 34 | _default_message_template = """\ |
|
35 | 35 | Oops, {app_name} crashed. We do our best to make it stable, but... |
|
36 | 36 | |
|
37 | 37 | A crash report was automatically generated with the following information: |
|
38 | 38 | - A verbatim copy of the crash traceback. |
|
39 | 39 | - A copy of your input history during this session. |
|
40 | 40 | - Data on your current {app_name} configuration. |
|
41 | 41 | |
|
42 | 42 | It was left in the file named: |
|
43 | 43 | \t'{crash_report_fname}' |
|
44 | 44 | If you can email this file to the developers, the information in it will help |
|
45 | 45 | them in understanding and correcting the problem. |
|
46 | 46 | |
|
47 | 47 | You can mail it to: {contact_name} at {contact_email} |
|
48 | 48 | with the subject '{app_name} Crash Report'. |
|
49 | 49 | |
|
50 | 50 | If you want to do it now, the following command will work (under Unix): |
|
51 | 51 | mail -s '{app_name} Crash Report' {contact_email} < {crash_report_fname} |
|
52 | 52 | |
|
53 | 53 | To ensure accurate tracking of this issue, please file a report about it at: |
|
54 | 54 | {bug_tracker} |
|
55 | 55 | """ |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | class CrashHandler(object): |
|
59 | 59 | """Customizable crash handlers for IPython applications. |
|
60 | 60 | |
|
61 | 61 | Instances of this class provide a :meth:`__call__` method which can be |
|
62 | 62 | used as a ``sys.excepthook``. The :meth:`__call__` signature is:: |
|
63 | 63 | |
|
64 | 64 | def __call__(self, etype, evalue, etb) |
|
65 | 65 | """ |
|
66 | 66 | |
|
67 | 67 | message_template = _default_message_template |
|
68 | 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 | 71 | bug_tracker=None, show_crash_traceback=True, call_pdb=False): |
|
72 | 72 | """Create a new crash handler |
|
73 | 73 | |
|
74 | 74 | Parameters |
|
75 | 75 | ---------- |
|
76 | 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 | 78 | crash time for internal information. |
|
79 | 79 | |
|
80 | 80 | contact_name : str |
|
81 | 81 | A string with the name of the person to contact. |
|
82 | 82 | |
|
83 | 83 | contact_email : str |
|
84 | 84 | A string with the email address of the contact. |
|
85 | 85 | |
|
86 | 86 | bug_tracker : str |
|
87 | 87 | A string with the URL for your project's bug tracker. |
|
88 | 88 | |
|
89 | 89 | show_crash_traceback : bool |
|
90 | 90 | If false, don't print the crash traceback on stderr, only generate |
|
91 | 91 | the on-disk report |
|
92 | 92 | |
|
93 | 93 | Non-argument instance attributes: |
|
94 | 94 | |
|
95 | 95 | These instances contain some non-argument attributes which allow for |
|
96 | 96 | further customization of the crash handler's behavior. Please see the |
|
97 | 97 | source for further details. |
|
98 | 98 | """ |
|
99 | 99 | self.crash_report_fname = "Crash_report_%s.txt" % app.name |
|
100 | 100 | self.app = app |
|
101 | 101 | self.call_pdb = call_pdb |
|
102 | 102 | #self.call_pdb = True # dbg |
|
103 | 103 | self.show_crash_traceback = show_crash_traceback |
|
104 | 104 | self.info = dict(app_name = app.name, |
|
105 | 105 | contact_name = contact_name, |
|
106 | 106 | contact_email = contact_email, |
|
107 | 107 | bug_tracker = bug_tracker, |
|
108 | 108 | crash_report_fname = self.crash_report_fname) |
|
109 | ||
|
109 | ||
|
110 | 110 | |
|
111 | 111 | def __call__(self, etype, evalue, etb): |
|
112 | 112 | """Handle an exception, call for compatible with sys.excepthook""" |
|
113 | 113 | |
|
114 | 114 | # Report tracebacks shouldn't use color in general (safer for users) |
|
115 | 115 | color_scheme = 'NoColor' |
|
116 | 116 | |
|
117 | 117 | # Use this ONLY for developer debugging (keep commented out for release) |
|
118 | 118 | #color_scheme = 'Linux' # dbg |
|
119 | 119 | try: |
|
120 | 120 | rptdir = self.app.ipython_dir |
|
121 | 121 | except: |
|
122 | 122 | rptdir = os.getcwdu() |
|
123 | 123 | if rptdir is None or not os.path.isdir(rptdir): |
|
124 | 124 | rptdir = os.getcwdu() |
|
125 | 125 | report_name = os.path.join(rptdir,self.crash_report_fname) |
|
126 | 126 | # write the report filename into the instance dict so it can get |
|
127 | 127 | # properly expanded out in the user message template |
|
128 | 128 | self.crash_report_fname = report_name |
|
129 | 129 | self.info['crash_report_fname'] = report_name |
|
130 | 130 | TBhandler = ultratb.VerboseTB( |
|
131 | 131 | color_scheme=color_scheme, |
|
132 | 132 | long_header=1, |
|
133 | 133 | call_pdb=self.call_pdb, |
|
134 | 134 | ) |
|
135 | 135 | if self.call_pdb: |
|
136 | 136 | TBhandler(etype,evalue,etb) |
|
137 | 137 | return |
|
138 | 138 | else: |
|
139 | 139 | traceback = TBhandler.text(etype,evalue,etb,context=31) |
|
140 | 140 | |
|
141 | 141 | # print traceback to screen |
|
142 | 142 | if self.show_crash_traceback: |
|
143 | 143 | print >> sys.stderr, traceback |
|
144 | 144 | |
|
145 | 145 | # and generate a complete report on disk |
|
146 | 146 | try: |
|
147 | 147 | report = open(report_name,'w') |
|
148 | 148 | except: |
|
149 | 149 | print >> sys.stderr, 'Could not create crash report on disk.' |
|
150 | 150 | return |
|
151 | 151 | |
|
152 | 152 | # Inform user on stderr of what happened |
|
153 | 153 | print >> sys.stderr, '\n'+'*'*70+'\n' |
|
154 | 154 | print >> sys.stderr, self.message_template.format(**self.info) |
|
155 | 155 | |
|
156 | 156 | # Construct report on disk |
|
157 | 157 | report.write(self.make_report(traceback)) |
|
158 | 158 | report.close() |
|
159 | 159 | raw_input("Hit <Enter> to quit this message (your terminal may close):") |
|
160 | 160 | |
|
161 | 161 | def make_report(self,traceback): |
|
162 | 162 | """Return a string containing a crash report.""" |
|
163 | ||
|
163 | ||
|
164 | 164 | sec_sep = self.section_sep |
|
165 | ||
|
165 | ||
|
166 | 166 | report = ['*'*75+'\n\n'+'IPython post-mortem report\n\n'] |
|
167 | 167 | rpt_add = report.append |
|
168 | 168 | rpt_add(sys_info()) |
|
169 | ||
|
169 | ||
|
170 | 170 | try: |
|
171 | 171 | config = pformat(self.app.config) |
|
172 | 172 | rpt_add(sec_sep) |
|
173 | 173 | rpt_add('Application name: %s\n\n' % self.app_name) |
|
174 | 174 | rpt_add('Current user configuration structure:\n\n') |
|
175 | 175 | rpt_add(config) |
|
176 | 176 | except: |
|
177 | 177 | pass |
|
178 | 178 | rpt_add(sec_sep+'Crash traceback:\n\n' + traceback) |
|
179 | 179 | |
|
180 | 180 | return ''.join(report) |
|
181 | 181 |
@@ -1,509 +1,509 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | Pdb debugger class. |
|
4 | 4 | |
|
5 | 5 | Modified from the standard pdb.Pdb class to avoid including readline, so that |
|
6 | 6 | the command line completion of other programs which include this isn't |
|
7 | 7 | damaged. |
|
8 | 8 | |
|
9 | 9 | In the future, this class will be expanded with improvements over the standard |
|
10 | 10 | pdb. |
|
11 | 11 | |
|
12 | 12 | The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor |
|
13 | 13 | changes. Licensing should therefore be under the standard Python terms. For |
|
14 | 14 | details on the PSF (Python Software Foundation) standard license, see: |
|
15 | 15 | |
|
16 | 16 | http://www.python.org/2.2.3/license.html""" |
|
17 | 17 | |
|
18 | 18 | #***************************************************************************** |
|
19 | 19 | # |
|
20 | 20 | # This file is licensed under the PSF license. |
|
21 | 21 | # |
|
22 | 22 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
23 | 23 | # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu> |
|
24 | 24 | # |
|
25 | 25 | # |
|
26 | 26 | #***************************************************************************** |
|
27 | 27 | |
|
28 | 28 | import bdb |
|
29 | 29 | import linecache |
|
30 | 30 | import sys |
|
31 | 31 | |
|
32 | 32 | from IPython.utils import PyColorize |
|
33 | 33 | from IPython.core import ipapi |
|
34 | 34 | from IPython.utils import coloransi, io |
|
35 | 35 | from IPython.core.excolors import exception_colors |
|
36 | 36 | |
|
37 | 37 | # See if we can use pydb. |
|
38 | 38 | has_pydb = False |
|
39 | 39 | prompt = 'ipdb> ' |
|
40 | 40 | #We have to check this directly from sys.argv, config struct not yet available |
|
41 | 41 | if '-pydb' in sys.argv: |
|
42 |
try: |
|
|
42 | try: | |
|
43 | 43 | import pydb |
|
44 | 44 | if hasattr(pydb.pydb, "runl") and pydb.version>'1.17': |
|
45 | 45 | # Version 1.17 is broken, and that's what ships with Ubuntu Edgy, so we |
|
46 | 46 | # better protect against it. |
|
47 | 47 | has_pydb = True |
|
48 | 48 | except ImportError: |
|
49 | 49 | print "Pydb (http://bashdb.sourceforge.net/pydb/) does not seem to be available" |
|
50 | 50 | |
|
51 | 51 | if has_pydb: |
|
52 | 52 | from pydb import Pdb as OldPdb |
|
53 | 53 | #print "Using pydb for %run -d and post-mortem" #dbg |
|
54 | 54 | prompt = 'ipydb> ' |
|
55 | 55 | else: |
|
56 | 56 | from pdb import Pdb as OldPdb |
|
57 | 57 | |
|
58 | 58 | # Allow the set_trace code to operate outside of an ipython instance, even if |
|
59 | 59 | # it does so with some limitations. The rest of this support is implemented in |
|
60 | 60 | # the Tracer constructor. |
|
61 | 61 | def BdbQuit_excepthook(et,ev,tb): |
|
62 | 62 | if et==bdb.BdbQuit: |
|
63 | 63 | print 'Exiting Debugger.' |
|
64 | 64 | else: |
|
65 | 65 | BdbQuit_excepthook.excepthook_ori(et,ev,tb) |
|
66 | 66 | |
|
67 | 67 | def BdbQuit_IPython_excepthook(self,et,ev,tb): |
|
68 | 68 | print 'Exiting Debugger.' |
|
69 | 69 | |
|
70 | 70 | |
|
71 | 71 | class Tracer(object): |
|
72 | 72 | """Class for local debugging, similar to pdb.set_trace. |
|
73 | 73 | |
|
74 | 74 | Instances of this class, when called, behave like pdb.set_trace, but |
|
75 | 75 | providing IPython's enhanced capabilities. |
|
76 | 76 | |
|
77 | 77 | This is implemented as a class which must be initialized in your own code |
|
78 | 78 | and not as a standalone function because we need to detect at runtime |
|
79 | 79 | whether IPython is already active or not. That detection is done in the |
|
80 | 80 | constructor, ensuring that this code plays nicely with a running IPython, |
|
81 | 81 | while functioning acceptably (though with limitations) if outside of it. |
|
82 | 82 | """ |
|
83 | 83 | |
|
84 | 84 | def __init__(self,colors=None): |
|
85 | 85 | """Create a local debugger instance. |
|
86 | 86 | |
|
87 | 87 | :Parameters: |
|
88 | 88 | |
|
89 | 89 | - `colors` (None): a string containing the name of the color scheme to |
|
90 | 90 | use, it must be one of IPython's valid color schemes. If not given, the |
|
91 | 91 | function will default to the current IPython scheme when running inside |
|
92 | 92 | IPython, and to 'NoColor' otherwise. |
|
93 | 93 | |
|
94 | 94 | Usage example: |
|
95 | 95 | |
|
96 | 96 | from IPython.core.debugger import Tracer; debug_here = Tracer() |
|
97 | 97 | |
|
98 | 98 | ... later in your code |
|
99 | 99 | debug_here() # -> will open up the debugger at that point. |
|
100 | 100 | |
|
101 | 101 | Once the debugger activates, you can use all of its regular commands to |
|
102 | 102 | step through code, set breakpoints, etc. See the pdb documentation |
|
103 | 103 | from the Python standard library for usage details. |
|
104 | 104 | """ |
|
105 | 105 | |
|
106 | 106 | try: |
|
107 | 107 | ip = ipapi.get() |
|
108 | 108 | except: |
|
109 | 109 | # Outside of ipython, we set our own exception hook manually |
|
110 | 110 | BdbQuit_excepthook.excepthook_ori = sys.excepthook |
|
111 | 111 | sys.excepthook = BdbQuit_excepthook |
|
112 | 112 | def_colors = 'NoColor' |
|
113 | 113 | try: |
|
114 | 114 | # Limited tab completion support |
|
115 | 115 | import readline |
|
116 | 116 | readline.parse_and_bind('tab: complete') |
|
117 | 117 | except ImportError: |
|
118 | 118 | pass |
|
119 | 119 | else: |
|
120 | 120 | # In ipython, we use its custom exception handler mechanism |
|
121 | 121 | def_colors = ip.colors |
|
122 | 122 | ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook) |
|
123 | 123 | |
|
124 | 124 | if colors is None: |
|
125 | 125 | colors = def_colors |
|
126 | 126 | self.debugger = Pdb(colors) |
|
127 | 127 | |
|
128 | 128 | def __call__(self): |
|
129 | 129 | """Starts an interactive debugger at the point where called. |
|
130 | 130 | |
|
131 | 131 | This is similar to the pdb.set_trace() function from the std lib, but |
|
132 | 132 | using IPython's enhanced debugger.""" |
|
133 | ||
|
133 | ||
|
134 | 134 | self.debugger.set_trace(sys._getframe().f_back) |
|
135 | 135 | |
|
136 | 136 | |
|
137 | 137 | def decorate_fn_with_doc(new_fn, old_fn, additional_text=""): |
|
138 | 138 | """Make new_fn have old_fn's doc string. This is particularly useful |
|
139 | 139 | for the do_... commands that hook into the help system. |
|
140 | 140 | Adapted from from a comp.lang.python posting |
|
141 | 141 | by Duncan Booth.""" |
|
142 | 142 | def wrapper(*args, **kw): |
|
143 | 143 | return new_fn(*args, **kw) |
|
144 | 144 | if old_fn.__doc__: |
|
145 | 145 | wrapper.__doc__ = old_fn.__doc__ + additional_text |
|
146 | 146 | return wrapper |
|
147 | 147 | |
|
148 | 148 | |
|
149 | 149 | def _file_lines(fname): |
|
150 | 150 | """Return the contents of a named file as a list of lines. |
|
151 | 151 | |
|
152 | 152 | This function never raises an IOError exception: if the file can't be |
|
153 | 153 | read, it simply returns an empty list.""" |
|
154 | 154 | |
|
155 | 155 | try: |
|
156 | 156 | outfile = open(fname) |
|
157 | 157 | except IOError: |
|
158 | 158 | return [] |
|
159 | 159 | else: |
|
160 | 160 | out = outfile.readlines() |
|
161 | 161 | outfile.close() |
|
162 | 162 | return out |
|
163 | 163 | |
|
164 | 164 | |
|
165 | 165 | class Pdb(OldPdb): |
|
166 | 166 | """Modified Pdb class, does not load readline.""" |
|
167 | 167 | |
|
168 | 168 | def __init__(self,color_scheme='NoColor',completekey=None, |
|
169 | 169 | stdin=None, stdout=None): |
|
170 | 170 | |
|
171 | 171 | # Parent constructor: |
|
172 | 172 | if has_pydb and completekey is None: |
|
173 | 173 | OldPdb.__init__(self,stdin=stdin,stdout=io.stdout) |
|
174 | 174 | else: |
|
175 | 175 | OldPdb.__init__(self,completekey,stdin,stdout) |
|
176 | ||
|
176 | ||
|
177 | 177 | self.prompt = prompt # The default prompt is '(Pdb)' |
|
178 | ||
|
178 | ||
|
179 | 179 | # IPython changes... |
|
180 | 180 | self.is_pydb = has_pydb |
|
181 | 181 | |
|
182 | 182 | self.shell = ipapi.get() |
|
183 | 183 | |
|
184 | 184 | if self.is_pydb: |
|
185 | 185 | |
|
186 | 186 | # interactiveshell.py's ipalias seems to want pdb's checkline |
|
187 | 187 | # which located in pydb.fn |
|
188 | 188 | import pydb.fns |
|
189 | 189 | self.checkline = lambda filename, lineno: \ |
|
190 | 190 | pydb.fns.checkline(self, filename, lineno) |
|
191 | 191 | |
|
192 | 192 | self.curframe = None |
|
193 | 193 | self.do_restart = self.new_do_restart |
|
194 | 194 | |
|
195 | 195 | self.old_all_completions = self.shell.Completer.all_completions |
|
196 | 196 | self.shell.Completer.all_completions=self.all_completions |
|
197 | 197 | |
|
198 | 198 | self.do_list = decorate_fn_with_doc(self.list_command_pydb, |
|
199 | 199 | OldPdb.do_list) |
|
200 | 200 | self.do_l = self.do_list |
|
201 | 201 | self.do_frame = decorate_fn_with_doc(self.new_do_frame, |
|
202 | 202 | OldPdb.do_frame) |
|
203 | 203 | |
|
204 | 204 | self.aliases = {} |
|
205 | 205 | |
|
206 | 206 | # Create color table: we copy the default one from the traceback |
|
207 | 207 | # module and add a few attributes needed for debugging |
|
208 | 208 | self.color_scheme_table = exception_colors() |
|
209 | 209 | |
|
210 |
# shorthands |
|
|
210 | # shorthands | |
|
211 | 211 | C = coloransi.TermColors |
|
212 | 212 | cst = self.color_scheme_table |
|
213 | 213 | |
|
214 | 214 | cst['NoColor'].colors.breakpoint_enabled = C.NoColor |
|
215 | 215 | cst['NoColor'].colors.breakpoint_disabled = C.NoColor |
|
216 | 216 | |
|
217 | 217 | cst['Linux'].colors.breakpoint_enabled = C.LightRed |
|
218 | 218 | cst['Linux'].colors.breakpoint_disabled = C.Red |
|
219 | 219 | |
|
220 | 220 | cst['LightBG'].colors.breakpoint_enabled = C.LightRed |
|
221 | 221 | cst['LightBG'].colors.breakpoint_disabled = C.Red |
|
222 | 222 | |
|
223 | 223 | self.set_colors(color_scheme) |
|
224 | 224 | |
|
225 | 225 | # Add a python parser so we can syntax highlight source while |
|
226 | 226 | # debugging. |
|
227 | 227 | self.parser = PyColorize.Parser() |
|
228 | 228 | |
|
229 | 229 | def set_colors(self, scheme): |
|
230 | 230 | """Shorthand access to the color table scheme selector method.""" |
|
231 | 231 | self.color_scheme_table.set_active_scheme(scheme) |
|
232 | 232 | |
|
233 | 233 | def interaction(self, frame, traceback): |
|
234 | 234 | self.shell.set_completer_frame(frame) |
|
235 | 235 | OldPdb.interaction(self, frame, traceback) |
|
236 | 236 | |
|
237 | 237 | def new_do_up(self, arg): |
|
238 | 238 | OldPdb.do_up(self, arg) |
|
239 | 239 | self.shell.set_completer_frame(self.curframe) |
|
240 | 240 | do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up) |
|
241 | 241 | |
|
242 | 242 | def new_do_down(self, arg): |
|
243 | 243 | OldPdb.do_down(self, arg) |
|
244 | 244 | self.shell.set_completer_frame(self.curframe) |
|
245 | 245 | |
|
246 | 246 | do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down) |
|
247 | 247 | |
|
248 | 248 | def new_do_frame(self, arg): |
|
249 | 249 | OldPdb.do_frame(self, arg) |
|
250 | 250 | self.shell.set_completer_frame(self.curframe) |
|
251 | 251 | |
|
252 | 252 | def new_do_quit(self, arg): |
|
253 | ||
|
253 | ||
|
254 | 254 | if hasattr(self, 'old_all_completions'): |
|
255 | 255 | self.shell.Completer.all_completions=self.old_all_completions |
|
256 | ||
|
257 | ||
|
256 | ||
|
257 | ||
|
258 | 258 | return OldPdb.do_quit(self, arg) |
|
259 | 259 | |
|
260 | 260 | do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit) |
|
261 | 261 | |
|
262 | 262 | def new_do_restart(self, arg): |
|
263 | 263 | """Restart command. In the context of ipython this is exactly the same |
|
264 | 264 | thing as 'quit'.""" |
|
265 | 265 | self.msg("Restart doesn't make sense here. Using 'quit' instead.") |
|
266 | 266 | return self.do_quit(arg) |
|
267 | 267 | |
|
268 | 268 | def postloop(self): |
|
269 | 269 | self.shell.set_completer_frame(None) |
|
270 | 270 | |
|
271 | 271 | def print_stack_trace(self): |
|
272 | 272 | try: |
|
273 | 273 | for frame_lineno in self.stack: |
|
274 | 274 | self.print_stack_entry(frame_lineno, context = 5) |
|
275 | 275 | except KeyboardInterrupt: |
|
276 | 276 | pass |
|
277 | 277 | |
|
278 | 278 | def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ', |
|
279 | 279 | context = 3): |
|
280 | 280 | #frame, lineno = frame_lineno |
|
281 | 281 | print >>io.stdout, self.format_stack_entry(frame_lineno, '', context) |
|
282 | 282 | |
|
283 | 283 | # vds: >> |
|
284 | 284 | frame, lineno = frame_lineno |
|
285 | 285 | filename = frame.f_code.co_filename |
|
286 | 286 | self.shell.hooks.synchronize_with_editor(filename, lineno, 0) |
|
287 | 287 | # vds: << |
|
288 | 288 | |
|
289 | 289 | def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3): |
|
290 | 290 | import linecache, repr |
|
291 | ||
|
291 | ||
|
292 | 292 | ret = [] |
|
293 | ||
|
293 | ||
|
294 | 294 | Colors = self.color_scheme_table.active_colors |
|
295 | 295 | ColorsNormal = Colors.Normal |
|
296 | 296 | tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal) |
|
297 | 297 | tpl_call = '%s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal) |
|
298 | 298 | tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) |
|
299 | 299 | tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, |
|
300 | 300 | ColorsNormal) |
|
301 | ||
|
301 | ||
|
302 | 302 | frame, lineno = frame_lineno |
|
303 | ||
|
303 | ||
|
304 | 304 | return_value = '' |
|
305 | 305 | if '__return__' in frame.f_locals: |
|
306 | 306 | rv = frame.f_locals['__return__'] |
|
307 | 307 | #return_value += '->' |
|
308 | 308 | return_value += repr.repr(rv) + '\n' |
|
309 | 309 | ret.append(return_value) |
|
310 | 310 | |
|
311 | 311 | #s = filename + '(' + `lineno` + ')' |
|
312 | 312 | filename = self.canonic(frame.f_code.co_filename) |
|
313 | 313 | link = tpl_link % filename |
|
314 | ||
|
314 | ||
|
315 | 315 | if frame.f_code.co_name: |
|
316 | 316 | func = frame.f_code.co_name |
|
317 | 317 | else: |
|
318 | 318 | func = "<lambda>" |
|
319 | ||
|
319 | ||
|
320 | 320 | call = '' |
|
321 |
if func != '?': |
|
|
321 | if func != '?': | |
|
322 | 322 | if '__args__' in frame.f_locals: |
|
323 | 323 | args = repr.repr(frame.f_locals['__args__']) |
|
324 | 324 | else: |
|
325 | 325 | args = '()' |
|
326 | 326 | call = tpl_call % (func, args) |
|
327 | 327 | |
|
328 | 328 | # The level info should be generated in the same format pdb uses, to |
|
329 | 329 | # avoid breaking the pdbtrack functionality of python-mode in *emacs. |
|
330 | 330 | if frame is self.curframe: |
|
331 | 331 | ret.append('> ') |
|
332 | 332 | else: |
|
333 | 333 | ret.append(' ') |
|
334 | 334 | ret.append('%s(%s)%s\n' % (link,lineno,call)) |
|
335 | ||
|
335 | ||
|
336 | 336 | start = lineno - 1 - context//2 |
|
337 | 337 | lines = linecache.getlines(filename) |
|
338 | 338 | start = max(start, 0) |
|
339 | 339 | start = min(start, len(lines) - context) |
|
340 | 340 | lines = lines[start : start + context] |
|
341 | ||
|
341 | ||
|
342 | 342 | for i,line in enumerate(lines): |
|
343 | 343 | show_arrow = (start + 1 + i == lineno) |
|
344 | 344 | linetpl = (frame is self.curframe or show_arrow) \ |
|
345 | 345 | and tpl_line_em \ |
|
346 | 346 | or tpl_line |
|
347 | 347 | ret.append(self.__format_line(linetpl, filename, |
|
348 | 348 | start + 1 + i, line, |
|
349 | 349 | arrow = show_arrow) ) |
|
350 | 350 | |
|
351 | 351 | return ''.join(ret) |
|
352 | 352 | |
|
353 | 353 | def __format_line(self, tpl_line, filename, lineno, line, arrow = False): |
|
354 | 354 | bp_mark = "" |
|
355 | 355 | bp_mark_color = "" |
|
356 | 356 | |
|
357 | 357 | scheme = self.color_scheme_table.active_scheme_name |
|
358 | 358 | new_line, err = self.parser.format2(line, 'str', scheme) |
|
359 | 359 | if not err: line = new_line |
|
360 | 360 | |
|
361 | 361 | bp = None |
|
362 | 362 | if lineno in self.get_file_breaks(filename): |
|
363 | 363 | bps = self.get_breaks(filename, lineno) |
|
364 | 364 | bp = bps[-1] |
|
365 | ||
|
365 | ||
|
366 | 366 | if bp: |
|
367 | 367 | Colors = self.color_scheme_table.active_colors |
|
368 | 368 | bp_mark = str(bp.number) |
|
369 | 369 | bp_mark_color = Colors.breakpoint_enabled |
|
370 | 370 | if not bp.enabled: |
|
371 | 371 | bp_mark_color = Colors.breakpoint_disabled |
|
372 | 372 | |
|
373 | 373 | numbers_width = 7 |
|
374 | 374 | if arrow: |
|
375 | 375 | # This is the line with the error |
|
376 | 376 | pad = numbers_width - len(str(lineno)) - len(bp_mark) |
|
377 | 377 | if pad >= 3: |
|
378 | 378 | marker = '-'*(pad-3) + '-> ' |
|
379 | 379 | elif pad == 2: |
|
380 | 380 | marker = '> ' |
|
381 | 381 | elif pad == 1: |
|
382 | 382 | marker = '>' |
|
383 | 383 | else: |
|
384 | 384 | marker = '' |
|
385 | 385 | num = '%s%s' % (marker, str(lineno)) |
|
386 | 386 | line = tpl_line % (bp_mark_color + bp_mark, num, line) |
|
387 | 387 | else: |
|
388 | 388 | num = '%*s' % (numbers_width - len(bp_mark), str(lineno)) |
|
389 | 389 | line = tpl_line % (bp_mark_color + bp_mark, num, line) |
|
390 | ||
|
390 | ||
|
391 | 391 | return line |
|
392 | 392 | |
|
393 | 393 | def list_command_pydb(self, arg): |
|
394 | 394 | """List command to use if we have a newer pydb installed""" |
|
395 | 395 | filename, first, last = OldPdb.parse_list_cmd(self, arg) |
|
396 | 396 | if filename is not None: |
|
397 | 397 | self.print_list_lines(filename, first, last) |
|
398 | ||
|
398 | ||
|
399 | 399 | def print_list_lines(self, filename, first, last): |
|
400 | 400 | """The printing (as opposed to the parsing part of a 'list' |
|
401 | 401 | command.""" |
|
402 | 402 | try: |
|
403 | 403 | Colors = self.color_scheme_table.active_colors |
|
404 | 404 | ColorsNormal = Colors.Normal |
|
405 | 405 | tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) |
|
406 | 406 | tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal) |
|
407 | 407 | src = [] |
|
408 | 408 | for lineno in range(first, last+1): |
|
409 | 409 | line = linecache.getline(filename, lineno) |
|
410 | 410 | if not line: |
|
411 | 411 | break |
|
412 | 412 | |
|
413 | 413 | if lineno == self.curframe.f_lineno: |
|
414 | 414 | line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True) |
|
415 | 415 | else: |
|
416 | 416 | line = self.__format_line(tpl_line, filename, lineno, line, arrow = False) |
|
417 | 417 | |
|
418 | 418 | src.append(line) |
|
419 | 419 | self.lineno = lineno |
|
420 | 420 | |
|
421 | 421 | print >>io.stdout, ''.join(src) |
|
422 | 422 | |
|
423 | 423 | except KeyboardInterrupt: |
|
424 | 424 | pass |
|
425 | 425 | |
|
426 | 426 | def do_list(self, arg): |
|
427 | 427 | self.lastcmd = 'list' |
|
428 | 428 | last = None |
|
429 | 429 | if arg: |
|
430 | 430 | try: |
|
431 | 431 | x = eval(arg, {}, {}) |
|
432 | 432 | if type(x) == type(()): |
|
433 | 433 | first, last = x |
|
434 | 434 | first = int(first) |
|
435 | 435 | last = int(last) |
|
436 | 436 | if last < first: |
|
437 | 437 | # Assume it's a count |
|
438 | 438 | last = first + last |
|
439 | 439 | else: |
|
440 | 440 | first = max(1, int(x) - 5) |
|
441 | 441 | except: |
|
442 | 442 | print '*** Error in argument:', `arg` |
|
443 | 443 | return |
|
444 | 444 | elif self.lineno is None: |
|
445 | 445 | first = max(1, self.curframe.f_lineno - 5) |
|
446 | 446 | else: |
|
447 | 447 | first = self.lineno + 1 |
|
448 | 448 | if last is None: |
|
449 | 449 | last = first + 10 |
|
450 | 450 | self.print_list_lines(self.curframe.f_code.co_filename, first, last) |
|
451 | 451 | |
|
452 | 452 | # vds: >> |
|
453 | 453 | lineno = first |
|
454 | 454 | filename = self.curframe.f_code.co_filename |
|
455 | 455 | self.shell.hooks.synchronize_with_editor(filename, lineno, 0) |
|
456 | 456 | # vds: << |
|
457 | 457 | |
|
458 | 458 | do_l = do_list |
|
459 | 459 | |
|
460 | 460 | def do_pdef(self, arg): |
|
461 | 461 | """The debugger interface to magic_pdef""" |
|
462 | 462 | namespaces = [('Locals', self.curframe.f_locals), |
|
463 | 463 | ('Globals', self.curframe.f_globals)] |
|
464 | 464 | self.shell.magic_pdef(arg, namespaces=namespaces) |
|
465 | 465 | |
|
466 | 466 | def do_pdoc(self, arg): |
|
467 | 467 | """The debugger interface to magic_pdoc""" |
|
468 | 468 | namespaces = [('Locals', self.curframe.f_locals), |
|
469 | 469 | ('Globals', self.curframe.f_globals)] |
|
470 | 470 | self.shell.magic_pdoc(arg, namespaces=namespaces) |
|
471 | 471 | |
|
472 | 472 | def do_pinfo(self, arg): |
|
473 | 473 | """The debugger equivalant of ?obj""" |
|
474 | 474 | namespaces = [('Locals', self.curframe.f_locals), |
|
475 | 475 | ('Globals', self.curframe.f_globals)] |
|
476 | 476 | self.shell.magic_pinfo("pinfo %s" % arg, namespaces=namespaces) |
|
477 | 477 | |
|
478 | 478 | def checkline(self, filename, lineno): |
|
479 | 479 | """Check whether specified line seems to be executable. |
|
480 | 480 | |
|
481 | 481 | Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank |
|
482 | 482 | line or EOF). Warning: testing is not comprehensive. |
|
483 | 483 | """ |
|
484 | 484 | ####################################################################### |
|
485 | 485 | # XXX Hack! Use python-2.5 compatible code for this call, because with |
|
486 | 486 | # all of our changes, we've drifted from the pdb api in 2.6. For now, |
|
487 | 487 | # changing: |
|
488 | 488 | # |
|
489 | 489 | #line = linecache.getline(filename, lineno, self.curframe.f_globals) |
|
490 | 490 | # to: |
|
491 | 491 | # |
|
492 | 492 | line = linecache.getline(filename, lineno) |
|
493 | 493 | # |
|
494 | 494 | # does the trick. But in reality, we need to fix this by reconciling |
|
495 | 495 | # our updates with the new Pdb APIs in Python 2.6. |
|
496 | 496 | # |
|
497 | 497 | # End hack. The rest of this method is copied verbatim from 2.6 pdb.py |
|
498 | 498 | ####################################################################### |
|
499 | ||
|
499 | ||
|
500 | 500 | if not line: |
|
501 | 501 | print >>self.stdout, 'End of file' |
|
502 | 502 | return 0 |
|
503 | 503 | line = line.strip() |
|
504 | 504 | # Don't allow setting breakpoint at a blank line |
|
505 | 505 | if (not line or (line[0] == '#') or |
|
506 | 506 | (line[:3] == '"""') or line[:3] == "'''"): |
|
507 | 507 | print >>self.stdout, '*** Blank or comment' |
|
508 | 508 | return 0 |
|
509 | 509 | return lineno |
@@ -1,383 +1,383 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Top-level display functions for displaying object in different formats. |
|
3 | 3 | |
|
4 | 4 | Authors: |
|
5 | 5 | |
|
6 | 6 | * Brian Granger |
|
7 | 7 | """ |
|
8 | 8 | |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | # Copyright (C) 2008-2010 The IPython Development Team |
|
11 | 11 | # |
|
12 | 12 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | 13 | # the file COPYING, distributed as part of this software. |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | # Imports |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | |
|
20 | 20 | from .displaypub import ( |
|
21 | 21 | publish_pretty, publish_html, |
|
22 | 22 | publish_latex, publish_svg, |
|
23 | 23 | publish_png, publish_json, |
|
24 | 24 | publish_javascript, publish_jpeg |
|
25 | 25 | ) |
|
26 | 26 | |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | # Main functions |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | |
|
31 | 31 | def display(*objs, **kwargs): |
|
32 | 32 | """Display a Python object in all frontends. |
|
33 | 33 | |
|
34 | 34 | By default all representations will be computed and sent to the frontends. |
|
35 | 35 | Frontends can decide which representation is used and how. |
|
36 | 36 | |
|
37 | 37 | Parameters |
|
38 | 38 | ---------- |
|
39 | 39 | objs : tuple of objects |
|
40 | 40 | The Python objects to display. |
|
41 | 41 | include : list or tuple, optional |
|
42 | 42 | A list of format type strings (MIME types) to include in the |
|
43 | 43 | format data dict. If this is set *only* the format types included |
|
44 | 44 | in this list will be computed. |
|
45 | 45 | exclude : list or tuple, optional |
|
46 | 46 | A list of format type string (MIME types) to exclue in the format |
|
47 | 47 | data dict. If this is set all format types will be computed, |
|
48 | 48 | except for those included in this argument. |
|
49 | 49 | """ |
|
50 | 50 | include = kwargs.get('include') |
|
51 | 51 | exclude = kwargs.get('exclude') |
|
52 | ||
|
52 | ||
|
53 | 53 | from IPython.core.interactiveshell import InteractiveShell |
|
54 | 54 | inst = InteractiveShell.instance() |
|
55 | 55 | format = inst.display_formatter.format |
|
56 | 56 | publish = inst.display_pub.publish |
|
57 | 57 | |
|
58 | 58 | for obj in objs: |
|
59 | 59 | format_dict = format(obj, include=include, exclude=exclude) |
|
60 | 60 | publish('IPython.core.display.display', format_dict) |
|
61 | 61 | |
|
62 | 62 | |
|
63 | 63 | def display_pretty(*objs, **kwargs): |
|
64 | 64 | """Display the pretty (default) representation of an object. |
|
65 | 65 | |
|
66 | 66 | Parameters |
|
67 | 67 | ---------- |
|
68 | 68 | objs : tuple of objects |
|
69 | 69 | The Python objects to display, or if raw=True raw text data to |
|
70 | 70 | display. |
|
71 | 71 | raw : bool |
|
72 | 72 | Are the data objects raw data or Python objects that need to be |
|
73 | 73 | formatted before display? [default: False] |
|
74 | 74 | """ |
|
75 | 75 | raw = kwargs.pop('raw',False) |
|
76 | 76 | if raw: |
|
77 | 77 | for obj in objs: |
|
78 | 78 | publish_pretty(obj) |
|
79 | 79 | else: |
|
80 | 80 | display(*objs, include=['text/plain']) |
|
81 | 81 | |
|
82 | 82 | |
|
83 | 83 | def display_html(*objs, **kwargs): |
|
84 | 84 | """Display the HTML representation of an object. |
|
85 | 85 | |
|
86 | 86 | Parameters |
|
87 | 87 | ---------- |
|
88 | 88 | objs : tuple of objects |
|
89 | 89 | The Python objects to display, or if raw=True raw HTML data to |
|
90 | 90 | display. |
|
91 | 91 | raw : bool |
|
92 | 92 | Are the data objects raw data or Python objects that need to be |
|
93 | 93 | formatted before display? [default: False] |
|
94 | 94 | """ |
|
95 | 95 | raw = kwargs.pop('raw',False) |
|
96 | 96 | if raw: |
|
97 | 97 | for obj in objs: |
|
98 | 98 | publish_html(obj) |
|
99 | 99 | else: |
|
100 | 100 | display(*objs, include=['text/plain','text/html']) |
|
101 | 101 | |
|
102 | 102 | |
|
103 | 103 | def display_svg(*objs, **kwargs): |
|
104 | 104 | """Display the SVG representation of an object. |
|
105 | 105 | |
|
106 | 106 | Parameters |
|
107 | 107 | ---------- |
|
108 | 108 | objs : tuple of objects |
|
109 | 109 | The Python objects to display, or if raw=True raw svg data to |
|
110 | 110 | display. |
|
111 | 111 | raw : bool |
|
112 | 112 | Are the data objects raw data or Python objects that need to be |
|
113 | 113 | formatted before display? [default: False] |
|
114 | 114 | """ |
|
115 | 115 | raw = kwargs.pop('raw',False) |
|
116 | 116 | if raw: |
|
117 | 117 | for obj in objs: |
|
118 | 118 | publish_svg(obj) |
|
119 | 119 | else: |
|
120 | 120 | display(*objs, include=['text/plain','image/svg+xml']) |
|
121 | 121 | |
|
122 | 122 | |
|
123 | 123 | def display_png(*objs, **kwargs): |
|
124 | 124 | """Display the PNG representation of an object. |
|
125 | 125 | |
|
126 | 126 | Parameters |
|
127 | 127 | ---------- |
|
128 | 128 | objs : tuple of objects |
|
129 | 129 | The Python objects to display, or if raw=True raw png data to |
|
130 | 130 | display. |
|
131 | 131 | raw : bool |
|
132 | 132 | Are the data objects raw data or Python objects that need to be |
|
133 | 133 | formatted before display? [default: False] |
|
134 | 134 | """ |
|
135 | 135 | raw = kwargs.pop('raw',False) |
|
136 | 136 | if raw: |
|
137 | 137 | for obj in objs: |
|
138 | 138 | publish_png(obj) |
|
139 | 139 | else: |
|
140 | 140 | display(*objs, include=['text/plain','image/png']) |
|
141 | 141 | |
|
142 | 142 | |
|
143 | 143 | def display_jpeg(*objs, **kwargs): |
|
144 | 144 | """Display the JPEG representation of an object. |
|
145 | 145 | |
|
146 | 146 | Parameters |
|
147 | 147 | ---------- |
|
148 | 148 | objs : tuple of objects |
|
149 | 149 | The Python objects to display, or if raw=True raw JPEG data to |
|
150 | 150 | display. |
|
151 | 151 | raw : bool |
|
152 | 152 | Are the data objects raw data or Python objects that need to be |
|
153 | 153 | formatted before display? [default: False] |
|
154 | 154 | """ |
|
155 | 155 | raw = kwargs.pop('raw',False) |
|
156 | 156 | if raw: |
|
157 | 157 | for obj in objs: |
|
158 | 158 | publish_jpeg(obj) |
|
159 | 159 | else: |
|
160 | 160 | display(*objs, include=['text/plain','image/jpeg']) |
|
161 | 161 | |
|
162 | 162 | |
|
163 | 163 | def display_latex(*objs, **kwargs): |
|
164 | 164 | """Display the LaTeX representation of an object. |
|
165 | 165 | |
|
166 | 166 | Parameters |
|
167 | 167 | ---------- |
|
168 | 168 | objs : tuple of objects |
|
169 | 169 | The Python objects to display, or if raw=True raw latex data to |
|
170 | 170 | display. |
|
171 | 171 | raw : bool |
|
172 | 172 | Are the data objects raw data or Python objects that need to be |
|
173 | 173 | formatted before display? [default: False] |
|
174 | 174 | """ |
|
175 | 175 | raw = kwargs.pop('raw',False) |
|
176 | 176 | if raw: |
|
177 | 177 | for obj in objs: |
|
178 | 178 | publish_latex(obj) |
|
179 | 179 | else: |
|
180 | 180 | display(*objs, include=['text/plain','text/latex']) |
|
181 | 181 | |
|
182 | 182 | |
|
183 | 183 | def display_json(*objs, **kwargs): |
|
184 | 184 | """Display the JSON representation of an object. |
|
185 | 185 | |
|
186 | 186 | Parameters |
|
187 | 187 | ---------- |
|
188 | 188 | objs : tuple of objects |
|
189 | 189 | The Python objects to display, or if raw=True raw json data to |
|
190 | 190 | display. |
|
191 | 191 | raw : bool |
|
192 | 192 | Are the data objects raw data or Python objects that need to be |
|
193 | 193 | formatted before display? [default: False] |
|
194 | 194 | """ |
|
195 | 195 | raw = kwargs.pop('raw',False) |
|
196 | 196 | if raw: |
|
197 | 197 | for obj in objs: |
|
198 | 198 | publish_json(obj) |
|
199 | 199 | else: |
|
200 | 200 | display(*objs, include=['text/plain','application/json']) |
|
201 | 201 | |
|
202 | 202 | |
|
203 | 203 | def display_javascript(*objs, **kwargs): |
|
204 | 204 | """Display the Javascript representation of an object. |
|
205 | 205 | |
|
206 | 206 | Parameters |
|
207 | 207 | ---------- |
|
208 | 208 | objs : tuple of objects |
|
209 | 209 | The Python objects to display, or if raw=True raw javascript data to |
|
210 | 210 | display. |
|
211 | 211 | raw : bool |
|
212 | 212 | Are the data objects raw data or Python objects that need to be |
|
213 | 213 | formatted before display? [default: False] |
|
214 | 214 | """ |
|
215 | 215 | raw = kwargs.pop('raw',False) |
|
216 | 216 | if raw: |
|
217 | 217 | for obj in objs: |
|
218 | 218 | publish_javascript(obj) |
|
219 | 219 | else: |
|
220 | 220 | display(*objs, include=['text/plain','application/javascript']) |
|
221 | 221 | |
|
222 | 222 | #----------------------------------------------------------------------------- |
|
223 | 223 | # Smart classes |
|
224 | 224 | #----------------------------------------------------------------------------- |
|
225 | 225 | |
|
226 | 226 | |
|
227 | 227 | class DisplayObject(object): |
|
228 | 228 | """An object that wraps data to be displayed.""" |
|
229 | 229 | |
|
230 | 230 | _read_flags = 'r' |
|
231 | 231 | |
|
232 | 232 | def __init__(self, data=None, url=None, filename=None): |
|
233 | 233 | """Create a display object given raw data. |
|
234 | 234 | |
|
235 | 235 | When this object is returned by an expression or passed to the |
|
236 | 236 | display function, it will result in the data being displayed |
|
237 | 237 | in the frontend. The MIME type of the data should match the |
|
238 | 238 | subclasses used, so the Png subclass should be used for 'image/png' |
|
239 | 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 | 242 | Parameters |
|
243 | 243 | ---------- |
|
244 | 244 | data : unicode, str or bytes |
|
245 | 245 | The raw data or a URL to download the data from. |
|
246 | 246 | url : unicode |
|
247 | 247 | A URL to download the data from. |
|
248 | 248 | filename : unicode |
|
249 | 249 | Path to a local file to load the data from. |
|
250 | 250 | """ |
|
251 | 251 | if data is not None and data.startswith('http'): |
|
252 | 252 | self.url = data |
|
253 | 253 | self.filename = None |
|
254 | 254 | self.data = None |
|
255 | 255 | else: |
|
256 | 256 | self.data = data |
|
257 | 257 | self.url = url |
|
258 | 258 | self.filename = None if filename is None else unicode(filename) |
|
259 | 259 | self.reload() |
|
260 | 260 | |
|
261 | 261 | def reload(self): |
|
262 | 262 | """Reload the raw data from file or URL.""" |
|
263 | 263 | if self.filename is not None: |
|
264 | 264 | with open(self.filename, self._read_flags) as f: |
|
265 | 265 | self.data = f.read() |
|
266 | 266 | elif self.url is not None: |
|
267 | 267 | try: |
|
268 | 268 | import urllib2 |
|
269 | 269 | response = urllib2.urlopen(self.url) |
|
270 | 270 | self.data = response.read() |
|
271 | 271 | # extract encoding from header, if there is one: |
|
272 | 272 | encoding = None |
|
273 | 273 | for sub in response.headers['content-type'].split(';'): |
|
274 | 274 | sub = sub.strip() |
|
275 | 275 | if sub.startswith('charset'): |
|
276 | 276 | encoding = sub.split('=')[-1].strip() |
|
277 | 277 | break |
|
278 | 278 | # decode data, if an encoding was specified |
|
279 | 279 | if encoding: |
|
280 | 280 | self.data = self.data.decode(encoding, 'replace') |
|
281 | 281 | except: |
|
282 | 282 | self.data = None |
|
283 | 283 | |
|
284 | 284 | class Pretty(DisplayObject): |
|
285 | 285 | |
|
286 | 286 | def _repr_pretty_(self): |
|
287 | 287 | return self.data |
|
288 | 288 | |
|
289 | 289 | |
|
290 | 290 | class HTML(DisplayObject): |
|
291 | 291 | |
|
292 | 292 | def _repr_html_(self): |
|
293 | 293 | return self.data |
|
294 | 294 | |
|
295 | 295 | |
|
296 | 296 | class Math(DisplayObject): |
|
297 | 297 | |
|
298 | 298 | def _repr_latex_(self): |
|
299 | 299 | return self.data |
|
300 | 300 | |
|
301 | 301 | |
|
302 | 302 | class SVG(DisplayObject): |
|
303 | 303 | |
|
304 | 304 | def _repr_svg_(self): |
|
305 | 305 | return self.data |
|
306 | 306 | |
|
307 | 307 | |
|
308 | 308 | class JSON(DisplayObject): |
|
309 | 309 | |
|
310 | 310 | def _repr_json_(self): |
|
311 | 311 | return self.data |
|
312 | 312 | |
|
313 | 313 | |
|
314 | 314 | class Javascript(DisplayObject): |
|
315 | 315 | |
|
316 | 316 | def _repr_javascript_(self): |
|
317 | 317 | return self.data |
|
318 | 318 | |
|
319 | 319 | |
|
320 | 320 | class Image(DisplayObject): |
|
321 | 321 | |
|
322 | 322 | _read_flags = 'rb' |
|
323 | 323 | |
|
324 | 324 | def __init__(self, data=None, url=None, filename=None, format=u'png', embed=False): |
|
325 | 325 | """Create a display an PNG/JPEG image given raw data. |
|
326 | 326 | |
|
327 | 327 | When this object is returned by an expression or passed to the |
|
328 | 328 | display function, it will result in the image being displayed |
|
329 | 329 | in the frontend. |
|
330 | 330 | |
|
331 | 331 | Parameters |
|
332 | 332 | ---------- |
|
333 | 333 | data : unicode, str or bytes |
|
334 | 334 | The raw data or a URL to download the data from. |
|
335 | 335 | url : unicode |
|
336 | 336 | A URL to download the data from. |
|
337 | 337 | filename : unicode |
|
338 | 338 | Path to a local file to load the data from. |
|
339 | 339 | format : unicode |
|
340 | 340 | The format of the image data (png/jpeg/jpg). If a filename or URL is given |
|
341 | 341 | for format will be inferred from the filename extension. |
|
342 | 342 | embed : bool |
|
343 | 343 | Should the image data be embedded in the notebook using a data URI (True) |
|
344 | 344 | or be loaded using an <img> tag. Set this to True if you want the image |
|
345 | 345 | to be viewable later with no internet connection. If a filename is given |
|
346 | 346 | embed is always set to True. |
|
347 | 347 | """ |
|
348 | 348 | if filename is not None: |
|
349 | 349 | ext = self._find_ext(filename) |
|
350 | 350 | elif url is not None: |
|
351 | 351 | ext = self._find_ext(url) |
|
352 | 352 | elif data.startswith('http'): |
|
353 | 353 | ext = self._find_ext(data) |
|
354 | 354 | else: |
|
355 | 355 | ext = None |
|
356 | 356 | if ext is not None: |
|
357 | 357 | if ext == u'jpg' or ext == u'jpeg': |
|
358 | 358 | format = u'jpeg' |
|
359 | 359 | if ext == u'png': |
|
360 | 360 | format = u'png' |
|
361 | 361 | self.format = unicode(format).lower() |
|
362 | 362 | self.embed = True if filename is not None else embed |
|
363 | 363 | super(Image, self).__init__(data=data, url=url, filename=filename) |
|
364 | 364 | |
|
365 | 365 | def reload(self): |
|
366 | 366 | """Reload the raw data from file or URL.""" |
|
367 | 367 | if self.embed: |
|
368 | 368 | super(Image,self).reload() |
|
369 | 369 | |
|
370 | 370 | def _repr_html_(self): |
|
371 | 371 | if not self.embed: |
|
372 | 372 | return u'<img src="%s" />' % self.url |
|
373 | 373 | |
|
374 | 374 | def _repr_png_(self): |
|
375 | 375 | if self.embed and self.format == u'png': |
|
376 | 376 | return self.data |
|
377 | 377 | |
|
378 | 378 | def _repr_jpeg_(self): |
|
379 | 379 | if self.embed and (self.format == u'jpeg' or self.format == u'jpg'): |
|
380 | 380 | return self.data |
|
381 | 381 | |
|
382 | 382 | def _find_ext(self, s): |
|
383 | 383 | return unicode(s.split('.')[-1].lower()) |
@@ -1,329 +1,329 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Displayhook for IPython. |
|
3 | 3 | |
|
4 | 4 | This defines a callable class that IPython uses for `sys.displayhook`. |
|
5 | 5 | |
|
6 | 6 | Authors: |
|
7 | 7 | |
|
8 | 8 | * Fernando Perez |
|
9 | 9 | * Brian Granger |
|
10 | 10 | * Robert Kern |
|
11 | 11 | """ |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Copyright (C) 2008-2010 The IPython Development Team |
|
15 | 15 | # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu> |
|
16 | 16 | # |
|
17 | 17 | # Distributed under the terms of the BSD License. The full license is in |
|
18 | 18 | # the file COPYING, distributed as part of this software. |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # Imports |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 | 25 | import __builtin__ |
|
26 | 26 | |
|
27 | 27 | from IPython.config.configurable import Configurable |
|
28 | 28 | from IPython.core import prompts |
|
29 | 29 | from IPython.utils import io |
|
30 | 30 | from IPython.utils.traitlets import Instance, List |
|
31 | 31 | from IPython.utils.warn import warn |
|
32 | 32 | |
|
33 | 33 | #----------------------------------------------------------------------------- |
|
34 | 34 | # Main displayhook class |
|
35 | 35 | #----------------------------------------------------------------------------- |
|
36 | 36 | |
|
37 | 37 | # TODO: The DisplayHook class should be split into two classes, one that |
|
38 | 38 | # manages the prompts and their synchronization and another that just does the |
|
39 | 39 | # displayhook logic and calls into the prompt manager. |
|
40 | 40 | |
|
41 | 41 | # TODO: Move the various attributes (cache_size, colors, input_sep, |
|
42 | 42 | # output_sep, output_sep2, ps1, ps2, ps_out, pad_left). Some of these are also |
|
43 | 43 | # attributes of InteractiveShell. They should be on ONE object only and the |
|
44 | 44 | # other objects should ask that one object for their values. |
|
45 | 45 | |
|
46 | 46 | class DisplayHook(Configurable): |
|
47 | 47 | """The custom IPython displayhook to replace sys.displayhook. |
|
48 | 48 | |
|
49 | 49 | This class does many things, but the basic idea is that it is a callable |
|
50 | 50 | that gets called anytime user code returns a value. |
|
51 | 51 | |
|
52 | 52 | Currently this class does more than just the displayhook logic and that |
|
53 | 53 | extra logic should eventually be moved out of here. |
|
54 | 54 | """ |
|
55 | 55 | |
|
56 | 56 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
57 | 57 | |
|
58 | 58 | def __init__(self, shell=None, cache_size=1000, |
|
59 | 59 | colors='NoColor', input_sep='\n', |
|
60 | 60 | output_sep='\n', output_sep2='', |
|
61 | 61 | ps1 = None, ps2 = None, ps_out = None, pad_left=True, |
|
62 | 62 | config=None): |
|
63 | 63 | super(DisplayHook, self).__init__(shell=shell, config=config) |
|
64 | 64 | |
|
65 | 65 | cache_size_min = 3 |
|
66 | 66 | if cache_size <= 0: |
|
67 | 67 | self.do_full_cache = 0 |
|
68 | 68 | cache_size = 0 |
|
69 | 69 | elif cache_size < cache_size_min: |
|
70 | 70 | self.do_full_cache = 0 |
|
71 | 71 | cache_size = 0 |
|
72 | 72 | warn('caching was disabled (min value for cache size is %s).' % |
|
73 | 73 | cache_size_min,level=3) |
|
74 | 74 | else: |
|
75 | 75 | self.do_full_cache = 1 |
|
76 | 76 | |
|
77 | 77 | self.cache_size = cache_size |
|
78 | 78 | self.input_sep = input_sep |
|
79 | 79 | |
|
80 | 80 | # we need a reference to the user-level namespace |
|
81 | 81 | self.shell = shell |
|
82 | 82 | |
|
83 | 83 | # Set input prompt strings and colors |
|
84 | 84 | if cache_size == 0: |
|
85 | 85 | if ps1.find('%n') > -1 or ps1.find(r'\#') > -1 \ |
|
86 | 86 | or ps1.find(r'\N') > -1: |
|
87 | 87 | ps1 = '>>> ' |
|
88 | 88 | if ps2.find('%n') > -1 or ps2.find(r'\#') > -1 \ |
|
89 | 89 | or ps2.find(r'\N') > -1: |
|
90 | 90 | ps2 = '... ' |
|
91 | 91 | self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ') |
|
92 | 92 | self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ') |
|
93 | 93 | self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','') |
|
94 | 94 | |
|
95 | 95 | self.color_table = prompts.PromptColors |
|
96 | 96 | self.prompt1 = prompts.Prompt1(self,sep=input_sep,prompt=self.ps1_str, |
|
97 | 97 | pad_left=pad_left) |
|
98 | 98 | self.prompt2 = prompts.Prompt2(self,prompt=self.ps2_str,pad_left=pad_left) |
|
99 | 99 | self.prompt_out = prompts.PromptOut(self,sep='',prompt=self.ps_out_str, |
|
100 | 100 | pad_left=pad_left) |
|
101 | 101 | self.set_colors(colors) |
|
102 | 102 | |
|
103 | 103 | # Store the last prompt string each time, we need it for aligning |
|
104 | 104 | # continuation and auto-rewrite prompts |
|
105 | 105 | self.last_prompt = '' |
|
106 | 106 | self.output_sep = output_sep |
|
107 | 107 | self.output_sep2 = output_sep2 |
|
108 | 108 | self._,self.__,self.___ = '','','' |
|
109 | 109 | |
|
110 | 110 | # these are deliberately global: |
|
111 | 111 | to_user_ns = {'_':self._,'__':self.__,'___':self.___} |
|
112 | 112 | self.shell.user_ns.update(to_user_ns) |
|
113 | 113 | |
|
114 | 114 | @property |
|
115 | 115 | def prompt_count(self): |
|
116 | 116 | return self.shell.execution_count |
|
117 | 117 | |
|
118 | 118 | def _set_prompt_str(self,p_str,cache_def,no_cache_def): |
|
119 | 119 | if p_str is None: |
|
120 | 120 | if self.do_full_cache: |
|
121 | 121 | return cache_def |
|
122 | 122 | else: |
|
123 | 123 | return no_cache_def |
|
124 | 124 | else: |
|
125 | 125 | return p_str |
|
126 | ||
|
126 | ||
|
127 | 127 | def set_colors(self, colors): |
|
128 | 128 | """Set the active color scheme and configure colors for the three |
|
129 | 129 | prompt subsystems.""" |
|
130 | 130 | |
|
131 | 131 | # FIXME: This modifying of the global prompts.prompt_specials needs |
|
132 | 132 | # to be fixed. We need to refactor all of the prompts stuff to use |
|
133 | 133 | # proper configuration and traits notifications. |
|
134 | 134 | if colors.lower()=='nocolor': |
|
135 | 135 | prompts.prompt_specials = prompts.prompt_specials_nocolor |
|
136 | 136 | else: |
|
137 | 137 | prompts.prompt_specials = prompts.prompt_specials_color |
|
138 | ||
|
138 | ||
|
139 | 139 | self.color_table.set_active_scheme(colors) |
|
140 | 140 | self.prompt1.set_colors() |
|
141 | 141 | self.prompt2.set_colors() |
|
142 | 142 | self.prompt_out.set_colors() |
|
143 | 143 | |
|
144 | 144 | #------------------------------------------------------------------------- |
|
145 | 145 | # Methods used in __call__. Override these methods to modify the behavior |
|
146 | 146 | # of the displayhook. |
|
147 | 147 | #------------------------------------------------------------------------- |
|
148 | 148 | |
|
149 | 149 | def check_for_underscore(self): |
|
150 | 150 | """Check if the user has set the '_' variable by hand.""" |
|
151 | 151 | # If something injected a '_' variable in __builtin__, delete |
|
152 | 152 | # ipython's automatic one so we don't clobber that. gettext() in |
|
153 | 153 | # particular uses _, so we need to stay away from it. |
|
154 | 154 | if '_' in __builtin__.__dict__: |
|
155 | 155 | try: |
|
156 | 156 | del self.shell.user_ns['_'] |
|
157 | 157 | except KeyError: |
|
158 | 158 | pass |
|
159 | 159 | |
|
160 | 160 | def quiet(self): |
|
161 | 161 | """Should we silence the display hook because of ';'?""" |
|
162 | 162 | # do not print output if input ends in ';' |
|
163 | 163 | try: |
|
164 | 164 | cell = self.shell.history_manager.input_hist_parsed[self.prompt_count] |
|
165 | 165 | if cell.rstrip().endswith(';'): |
|
166 | 166 | return True |
|
167 | 167 | except IndexError: |
|
168 | 168 | # some uses of ipshellembed may fail here |
|
169 | 169 | pass |
|
170 | 170 | return False |
|
171 | 171 | |
|
172 | 172 | def start_displayhook(self): |
|
173 | 173 | """Start the displayhook, initializing resources.""" |
|
174 | 174 | pass |
|
175 | 175 | |
|
176 | 176 | def write_output_prompt(self): |
|
177 | 177 | """Write the output prompt. |
|
178 | 178 | |
|
179 | 179 | The default implementation simply writes the prompt to |
|
180 | 180 | ``io.stdout``. |
|
181 | 181 | """ |
|
182 | 182 | # Use write, not print which adds an extra space. |
|
183 | 183 | io.stdout.write(self.output_sep) |
|
184 | 184 | outprompt = str(self.prompt_out) |
|
185 | 185 | if self.do_full_cache: |
|
186 | 186 | io.stdout.write(outprompt) |
|
187 | 187 | |
|
188 | 188 | def compute_format_data(self, result): |
|
189 | 189 | """Compute format data of the object to be displayed. |
|
190 | 190 | |
|
191 | 191 | The format data is a generalization of the :func:`repr` of an object. |
|
192 | 192 | In the default implementation the format data is a :class:`dict` of |
|
193 | 193 | key value pair where the keys are valid MIME types and the values |
|
194 | 194 | are JSON'able data structure containing the raw data for that MIME |
|
195 | 195 | type. It is up to frontends to determine pick a MIME to to use and |
|
196 | 196 | display that data in an appropriate manner. |
|
197 | 197 | |
|
198 | 198 | This method only computes the format data for the object and should |
|
199 | 199 | NOT actually print or write that to a stream. |
|
200 | 200 | |
|
201 | 201 | Parameters |
|
202 | 202 | ---------- |
|
203 | 203 | result : object |
|
204 | 204 | The Python object passed to the display hook, whose format will be |
|
205 | 205 | computed. |
|
206 | 206 | |
|
207 | 207 | Returns |
|
208 | 208 | ------- |
|
209 | 209 | format_data : dict |
|
210 | 210 | A :class:`dict` whose keys are valid MIME types and values are |
|
211 | 211 | JSON'able raw data for that MIME type. It is recommended that |
|
212 | 212 | all return values of this should always include the "text/plain" |
|
213 | 213 | MIME type representation of the object. |
|
214 | 214 | """ |
|
215 | 215 | return self.shell.display_formatter.format(result) |
|
216 | 216 | |
|
217 | 217 | def write_format_data(self, format_dict): |
|
218 | 218 | """Write the format data dict to the frontend. |
|
219 | 219 | |
|
220 | 220 | This default version of this method simply writes the plain text |
|
221 | 221 | representation of the object to ``io.stdout``. Subclasses should |
|
222 | 222 | override this method to send the entire `format_dict` to the |
|
223 | 223 | frontends. |
|
224 | 224 | |
|
225 | 225 | Parameters |
|
226 | 226 | ---------- |
|
227 | 227 | format_dict : dict |
|
228 | 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 | 231 | # newline, even if all the prompt separators are ''. This is the |
|
232 | 232 | # standard IPython behavior. |
|
233 | 233 | result_repr = format_dict['text/plain'] |
|
234 | 234 | if '\n' in result_repr: |
|
235 | 235 | # So that multi-line strings line up with the left column of |
|
236 | 236 | # the screen, instead of having the output prompt mess up |
|
237 | 237 | # their first line. |
|
238 | 238 | # We use the ps_out_str template instead of the expanded prompt |
|
239 | 239 | # because the expansion may add ANSI escapes that will interfere |
|
240 | 240 | # with our ability to determine whether or not we should add |
|
241 | 241 | # a newline. |
|
242 | 242 | if self.ps_out_str and not self.ps_out_str.endswith('\n'): |
|
243 | 243 | # But avoid extraneous empty lines. |
|
244 | 244 | result_repr = '\n' + result_repr |
|
245 | 245 | |
|
246 | 246 | print >>io.stdout, result_repr |
|
247 | 247 | |
|
248 | 248 | def update_user_ns(self, result): |
|
249 | 249 | """Update user_ns with various things like _, __, _1, etc.""" |
|
250 | 250 | |
|
251 | 251 | # Avoid recursive reference when displaying _oh/Out |
|
252 | 252 | if result is not self.shell.user_ns['_oh']: |
|
253 | 253 | if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache: |
|
254 | 254 | warn('Output cache limit (currently '+ |
|
255 | 255 | `self.cache_size`+' entries) hit.\n' |
|
256 | 256 | 'Flushing cache and resetting history counter...\n' |
|
257 | 257 | 'The only history variables available will be _,__,___ and _1\n' |
|
258 | 258 | 'with the current result.') |
|
259 | 259 | |
|
260 | 260 | self.flush() |
|
261 | 261 | # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise |
|
262 | 262 | # we cause buggy behavior for things like gettext). |
|
263 | 263 | |
|
264 | 264 | if '_' not in __builtin__.__dict__: |
|
265 | 265 | self.___ = self.__ |
|
266 | 266 | self.__ = self._ |
|
267 | 267 | self._ = result |
|
268 | 268 | self.shell.user_ns.update({'_':self._, |
|
269 | 269 | '__':self.__, |
|
270 | 270 | '___':self.___}) |
|
271 | 271 | |
|
272 | 272 | # hackish access to top-level namespace to create _1,_2... dynamically |
|
273 | 273 | to_main = {} |
|
274 | 274 | if self.do_full_cache: |
|
275 | 275 | new_result = '_'+`self.prompt_count` |
|
276 | 276 | to_main[new_result] = result |
|
277 | 277 | self.shell.user_ns.update(to_main) |
|
278 | 278 | self.shell.user_ns['_oh'][self.prompt_count] = result |
|
279 | 279 | |
|
280 | 280 | def log_output(self, format_dict): |
|
281 | 281 | """Log the output.""" |
|
282 | 282 | if self.shell.logger.log_output: |
|
283 | 283 | self.shell.logger.log_write(format_dict['text/plain'], 'output') |
|
284 | 284 | self.shell.history_manager.output_hist_reprs[self.prompt_count] = \ |
|
285 | 285 | format_dict['text/plain'] |
|
286 | 286 | |
|
287 | 287 | def finish_displayhook(self): |
|
288 | 288 | """Finish up all displayhook activities.""" |
|
289 | 289 | io.stdout.write(self.output_sep2) |
|
290 | 290 | io.stdout.flush() |
|
291 | 291 | |
|
292 | 292 | def __call__(self, result=None): |
|
293 | 293 | """Printing with history cache management. |
|
294 | ||
|
294 | ||
|
295 | 295 | This is invoked everytime the interpreter needs to print, and is |
|
296 | 296 | activated by setting the variable sys.displayhook to it. |
|
297 | 297 | """ |
|
298 | 298 | self.check_for_underscore() |
|
299 | 299 | if result is not None and not self.quiet(): |
|
300 | 300 | self.start_displayhook() |
|
301 | 301 | self.write_output_prompt() |
|
302 | 302 | format_dict = self.compute_format_data(result) |
|
303 | 303 | self.write_format_data(format_dict) |
|
304 | 304 | self.update_user_ns(result) |
|
305 | 305 | self.log_output(format_dict) |
|
306 | 306 | self.finish_displayhook() |
|
307 | 307 | |
|
308 | 308 | def flush(self): |
|
309 | 309 | if not self.do_full_cache: |
|
310 | 310 | raise ValueError,"You shouldn't have reached the cache flush "\ |
|
311 | 311 | "if full caching is not enabled!" |
|
312 | 312 | # delete auto-generated vars from global namespace |
|
313 | ||
|
313 | ||
|
314 | 314 | for n in range(1,self.prompt_count + 1): |
|
315 | 315 | key = '_'+`n` |
|
316 | 316 | try: |
|
317 | 317 | del self.shell.user_ns[key] |
|
318 | 318 | except: pass |
|
319 | 319 | self.shell.user_ns['_oh'].clear() |
|
320 | ||
|
320 | ||
|
321 | 321 | # Release our own references to objects: |
|
322 | 322 | self._, self.__, self.___ = '', '', '' |
|
323 | ||
|
323 | ||
|
324 | 324 | if '_' not in __builtin__.__dict__: |
|
325 | 325 | self.shell.user_ns.update({'_':None,'__':None, '___':None}) |
|
326 | 326 | import gc |
|
327 | 327 | # TODO: Is this really needed? |
|
328 | 328 | gc.collect() |
|
329 | 329 |
@@ -1,298 +1,298 b'' | |||
|
1 | 1 | """An interface for publishing rich data to frontends. |
|
2 | 2 | |
|
3 | 3 | There are two components of the display system: |
|
4 | 4 | |
|
5 | 5 | * Display formatters, which take a Python object and compute the |
|
6 | 6 | representation of the object in various formats (text, HTML, SVg, etc.). |
|
7 | 7 | * The display publisher that is used to send the representation data to the |
|
8 | 8 | various frontends. |
|
9 | 9 | |
|
10 | 10 | This module defines the logic display publishing. The display publisher uses |
|
11 | 11 | the ``display_data`` message type that is defined in the IPython messaging |
|
12 | 12 | spec. |
|
13 | 13 | |
|
14 | 14 | Authors: |
|
15 | 15 | |
|
16 | 16 | * Brian Granger |
|
17 | 17 | """ |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Copyright (C) 2008-2010 The IPython Development Team |
|
21 | 21 | # |
|
22 | 22 | # Distributed under the terms of the BSD License. The full license is in |
|
23 | 23 | # the file COPYING, distributed as part of this software. |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | # Imports |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | |
|
30 | 30 | from __future__ import print_function |
|
31 | 31 | |
|
32 | 32 | from IPython.config.configurable import Configurable |
|
33 | 33 | |
|
34 | 34 | #----------------------------------------------------------------------------- |
|
35 | 35 | # Main payload class |
|
36 | 36 | #----------------------------------------------------------------------------- |
|
37 | 37 | |
|
38 | 38 | class DisplayPublisher(Configurable): |
|
39 | 39 | """A traited class that publishes display data to frontends. |
|
40 | 40 | |
|
41 | 41 | Instances of this class are created by the main IPython object and should |
|
42 | 42 | be accessed there. |
|
43 | 43 | """ |
|
44 | 44 | |
|
45 | 45 | def _validate_data(self, source, data, metadata=None): |
|
46 | 46 | """Validate the display data. |
|
47 | 47 | |
|
48 | 48 | Parameters |
|
49 | 49 | ---------- |
|
50 | 50 | source : str |
|
51 | 51 | The fully dotted name of the callable that created the data, like |
|
52 | 52 | :func:`foo.bar.my_formatter`. |
|
53 | 53 | data : dict |
|
54 | 54 | The formata data dictionary. |
|
55 | 55 | metadata : dict |
|
56 | 56 | Any metadata for the data. |
|
57 | 57 | """ |
|
58 | 58 | |
|
59 | 59 | if not isinstance(source, basestring): |
|
60 | 60 | raise TypeError('source must be a str, got: %r' % source) |
|
61 | 61 | if not isinstance(data, dict): |
|
62 | 62 | raise TypeError('data must be a dict, got: %r' % data) |
|
63 | 63 | if metadata is not None: |
|
64 | 64 | if not isinstance(metadata, dict): |
|
65 | 65 | raise TypeError('metadata must be a dict, got: %r' % data) |
|
66 | 66 | |
|
67 | 67 | def publish(self, source, data, metadata=None): |
|
68 | 68 | """Publish data and metadata to all frontends. |
|
69 | 69 | |
|
70 | 70 | See the ``display_data`` message in the messaging documentation for |
|
71 | 71 | more details about this message type. |
|
72 | 72 | |
|
73 | 73 | The following MIME types are currently implemented: |
|
74 | 74 | |
|
75 | 75 | * text/plain |
|
76 | 76 | * text/html |
|
77 | 77 | * text/latex |
|
78 | 78 | * application/json |
|
79 | 79 | * application/javascript |
|
80 | 80 | * image/png |
|
81 | 81 | * image/jpeg |
|
82 | 82 | * image/svg+xml |
|
83 | 83 | |
|
84 | 84 | Parameters |
|
85 | 85 | ---------- |
|
86 | 86 | source : str |
|
87 | 87 | A string that give the function or method that created the data, |
|
88 | 88 | such as 'IPython.core.page'. |
|
89 | 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 | 91 | 'text/plain' or 'image/svg+xml') and values that are the data for |
|
92 | 92 | that MIME type. The data itself must be a JSON'able data |
|
93 | 93 | structure. Minimally all data should have the 'text/plain' data, |
|
94 | 94 | which can be displayed by all frontends. If more than the plain |
|
95 | 95 | text is given, it is up to the frontend to decide which |
|
96 | 96 | representation to use. |
|
97 | 97 | metadata : dict |
|
98 | 98 | A dictionary for metadata related to the data. This can contain |
|
99 | 99 | arbitrary key, value pairs that frontends can use to interpret |
|
100 | 100 | the data. |
|
101 | 101 | """ |
|
102 | 102 | from IPython.utils import io |
|
103 | 103 | # The default is to simply write the plain text data using io.stdout. |
|
104 | 104 | if data.has_key('text/plain'): |
|
105 | 105 | print(data['text/plain'], file=io.stdout) |
|
106 | 106 | |
|
107 | 107 | |
|
108 | 108 | def publish_display_data(source, data, metadata=None): |
|
109 | 109 | """Publish data and metadata to all frontends. |
|
110 | 110 | |
|
111 | 111 | See the ``display_data`` message in the messaging documentation for |
|
112 | 112 | more details about this message type. |
|
113 | 113 | |
|
114 | 114 | The following MIME types are currently implemented: |
|
115 | 115 | |
|
116 | 116 | * text/plain |
|
117 | 117 | * text/html |
|
118 | 118 | * text/latex |
|
119 | 119 | * application/json |
|
120 | 120 | * application/javascript |
|
121 | 121 | * image/png |
|
122 | 122 | * image/jpeg |
|
123 | 123 | * image/svg+xml |
|
124 | 124 | |
|
125 | 125 | Parameters |
|
126 | 126 | ---------- |
|
127 | 127 | source : str |
|
128 | 128 | A string that give the function or method that created the data, |
|
129 | 129 | such as 'IPython.core.page'. |
|
130 | 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 | 132 | 'text/plain' or 'image/svg+xml') and values that are the data for |
|
133 | 133 | that MIME type. The data itself must be a JSON'able data |
|
134 | 134 | structure. Minimally all data should have the 'text/plain' data, |
|
135 | 135 | which can be displayed by all frontends. If more than the plain |
|
136 | 136 | text is given, it is up to the frontend to decide which |
|
137 | 137 | representation to use. |
|
138 | 138 | metadata : dict |
|
139 | 139 | A dictionary for metadata related to the data. This can contain |
|
140 | 140 | arbitrary key, value pairs that frontends can use to interpret |
|
141 | 141 | the data. |
|
142 | 142 | """ |
|
143 | 143 | from IPython.core.interactiveshell import InteractiveShell |
|
144 | 144 | InteractiveShell.instance().display_pub.publish( |
|
145 | 145 | source, |
|
146 | 146 | data, |
|
147 | 147 | metadata |
|
148 | 148 | ) |
|
149 | 149 | |
|
150 | 150 | |
|
151 | 151 | def publish_pretty(data, metadata=None): |
|
152 | 152 | """Publish raw text data to all frontends. |
|
153 | 153 | |
|
154 | 154 | Parameters |
|
155 | 155 | ---------- |
|
156 | 156 | data : unicode |
|
157 | 157 | The raw text data to publish. |
|
158 | 158 | metadata : dict |
|
159 | 159 | A dictionary for metadata related to the data. This can contain |
|
160 | 160 | arbitrary key, value pairs that frontends can use to interpret |
|
161 | 161 | the data. |
|
162 | 162 | """ |
|
163 | 163 | publish_display_data( |
|
164 | 164 | u'IPython.core.displaypub.publish_pretty', |
|
165 | 165 | {'text/plain':data}, |
|
166 | 166 | metadata=metadata |
|
167 | 167 | ) |
|
168 | 168 | |
|
169 | 169 | |
|
170 | 170 | def publish_html(data, metadata=None): |
|
171 | 171 | """Publish raw HTML data to all frontends. |
|
172 | 172 | |
|
173 | 173 | Parameters |
|
174 | 174 | ---------- |
|
175 | 175 | data : unicode |
|
176 | 176 | The raw HTML data to publish. |
|
177 | 177 | metadata : dict |
|
178 | 178 | A dictionary for metadata related to the data. This can contain |
|
179 | 179 | arbitrary key, value pairs that frontends can use to interpret |
|
180 | 180 | the data. |
|
181 | 181 | """ |
|
182 | 182 | publish_display_data( |
|
183 | 183 | u'IPython.core.displaypub.publish_html', |
|
184 | 184 | {'text/html':data}, |
|
185 | 185 | metadata=metadata |
|
186 | 186 | ) |
|
187 | 187 | |
|
188 | 188 | |
|
189 | 189 | def publish_latex(data, metadata=None): |
|
190 | 190 | """Publish raw LaTeX data to all frontends. |
|
191 | 191 | |
|
192 | 192 | Parameters |
|
193 | 193 | ---------- |
|
194 | 194 | data : unicode |
|
195 | 195 | The raw LaTeX data to publish. |
|
196 | 196 | metadata : dict |
|
197 | 197 | A dictionary for metadata related to the data. This can contain |
|
198 | 198 | arbitrary key, value pairs that frontends can use to interpret |
|
199 | 199 | the data. |
|
200 | 200 | """ |
|
201 | 201 | publish_display_data( |
|
202 | 202 | u'IPython.core.displaypub.publish_latex', |
|
203 | 203 | {'text/latex':data}, |
|
204 | 204 | metadata=metadata |
|
205 | 205 | ) |
|
206 | 206 | |
|
207 | 207 | def publish_png(data, metadata=None): |
|
208 | 208 | """Publish raw binary PNG data to all frontends. |
|
209 | 209 | |
|
210 | 210 | Parameters |
|
211 | 211 | ---------- |
|
212 | 212 | data : str/bytes |
|
213 | 213 | The raw binary PNG data to publish. |
|
214 | 214 | metadata : dict |
|
215 | 215 | A dictionary for metadata related to the data. This can contain |
|
216 | 216 | arbitrary key, value pairs that frontends can use to interpret |
|
217 | 217 | the data. |
|
218 | 218 | """ |
|
219 | 219 | publish_display_data( |
|
220 | 220 | u'IPython.core.displaypub.publish_png', |
|
221 | 221 | {'image/png':data}, |
|
222 | 222 | metadata=metadata |
|
223 | 223 | ) |
|
224 | 224 | |
|
225 | 225 | |
|
226 | 226 | def publish_jpeg(data, metadata=None): |
|
227 | 227 | """Publish raw binary JPEG data to all frontends. |
|
228 | 228 | |
|
229 | 229 | Parameters |
|
230 | 230 | ---------- |
|
231 | 231 | data : str/bytes |
|
232 | 232 | The raw binary JPEG data to publish. |
|
233 | 233 | metadata : dict |
|
234 | 234 | A dictionary for metadata related to the data. This can contain |
|
235 | 235 | arbitrary key, value pairs that frontends can use to interpret |
|
236 | 236 | the data. |
|
237 | 237 | """ |
|
238 | 238 | publish_display_data( |
|
239 | 239 | u'IPython.core.displaypub.publish_jpeg', |
|
240 | 240 | {'image/jpeg':data}, |
|
241 | 241 | metadata=metadata |
|
242 | 242 | ) |
|
243 | 243 | |
|
244 | 244 | |
|
245 | 245 | def publish_svg(data, metadata=None): |
|
246 | 246 | """Publish raw SVG data to all frontends. |
|
247 | 247 | |
|
248 | 248 | Parameters |
|
249 | 249 | ---------- |
|
250 | 250 | data : unicode |
|
251 | 251 | The raw SVG data to publish. |
|
252 | 252 | metadata : dict |
|
253 | 253 | A dictionary for metadata related to the data. This can contain |
|
254 | 254 | arbitrary key, value pairs that frontends can use to interpret |
|
255 | 255 | the data. |
|
256 | 256 | """ |
|
257 | 257 | publish_display_data( |
|
258 | 258 | u'IPython.core.displaypub.publish_svg', |
|
259 | 259 | {'image/svg+xml':data}, |
|
260 | 260 | metadata=metadata |
|
261 | 261 | ) |
|
262 | 262 | |
|
263 | 263 | def publish_json(data, metadata=None): |
|
264 | 264 | """Publish raw JSON data to all frontends. |
|
265 | 265 | |
|
266 | 266 | Parameters |
|
267 | 267 | ---------- |
|
268 | 268 | data : unicode |
|
269 | 269 | The raw JSON data to publish. |
|
270 | 270 | metadata : dict |
|
271 | 271 | A dictionary for metadata related to the data. This can contain |
|
272 | 272 | arbitrary key, value pairs that frontends can use to interpret |
|
273 | 273 | the data. |
|
274 | 274 | """ |
|
275 | 275 | publish_display_data( |
|
276 | 276 | u'IPython.core.displaypub.publish_json', |
|
277 | 277 | {'application/json':data}, |
|
278 | 278 | metadata=metadata |
|
279 | 279 | ) |
|
280 | 280 | |
|
281 | 281 | def publish_javascript(data, metadata=None): |
|
282 | 282 | """Publish raw Javascript data to all frontends. |
|
283 | 283 | |
|
284 | 284 | Parameters |
|
285 | 285 | ---------- |
|
286 | 286 | data : unicode |
|
287 | 287 | The raw Javascript data to publish. |
|
288 | 288 | metadata : dict |
|
289 | 289 | A dictionary for metadata related to the data. This can contain |
|
290 | 290 | arbitrary key, value pairs that frontends can use to interpret |
|
291 | 291 | the data. |
|
292 | 292 | """ |
|
293 | 293 | publish_display_data( |
|
294 | 294 | u'IPython.core.displaypub.publish_javascript', |
|
295 | 295 | {'application/javascript':data}, |
|
296 | 296 | metadata=metadata |
|
297 | 297 | ) |
|
298 | 298 |
@@ -1,51 +1,51 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Global exception classes for IPython.core. |
|
4 | 4 | |
|
5 | 5 | Authors: |
|
6 | 6 | |
|
7 | 7 | * Brian Granger |
|
8 | 8 | * Fernando Perez |
|
9 | 9 | |
|
10 | 10 | Notes |
|
11 | 11 | ----- |
|
12 | 12 | """ |
|
13 | 13 | |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | # Copyright (C) 2008-2009 The IPython Development Team |
|
16 | 16 | # |
|
17 | 17 | # Distributed under the terms of the BSD License. The full license is in |
|
18 | 18 | # the file COPYING, distributed as part of this software. |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # Imports |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | # Exception classes |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | |
|
29 | 29 | class IPythonCoreError(Exception): |
|
30 | 30 | pass |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | class TryNext(IPythonCoreError): |
|
34 | 34 | """Try next hook exception. |
|
35 | ||
|
35 | ||
|
36 | 36 | Raise this in your hook function to indicate that the next hook handler |
|
37 | 37 | should be used to handle the operation. If you pass arguments to the |
|
38 | 38 | constructor those arguments will be used by the next hook instead of the |
|
39 | 39 | original ones. |
|
40 | 40 | """ |
|
41 | 41 | |
|
42 | 42 | def __init__(self, *args, **kwargs): |
|
43 | 43 | self.args = args |
|
44 | 44 | self.kwargs = kwargs |
|
45 | 45 | |
|
46 | 46 | class UsageError(IPythonCoreError): |
|
47 | 47 | """Error in magic function arguments, etc. |
|
48 | ||
|
48 | ||
|
49 | 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 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | Color schemes for exception handling code in IPython. |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2005-2006 Fernando Perez <fperez@colorado.edu> |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #***************************************************************************** |
|
12 | 12 | |
|
13 | 13 | from IPython.utils.coloransi import ColorSchemeTable, TermColors, ColorScheme |
|
14 | 14 | |
|
15 | 15 | def exception_colors(): |
|
16 | 16 | """Return a color table with fields for exception reporting. |
|
17 | 17 | |
|
18 | 18 | The table is an instance of ColorSchemeTable with schemes added for |
|
19 | 19 | 'Linux', 'LightBG' and 'NoColor' and fields for exception handling filled |
|
20 | 20 | in. |
|
21 | 21 | |
|
22 | 22 | Examples: |
|
23 | 23 | |
|
24 | 24 | >>> ec = exception_colors() |
|
25 | 25 | >>> ec.active_scheme_name |
|
26 | 26 | '' |
|
27 | 27 | >>> print ec.active_colors |
|
28 | 28 | None |
|
29 | 29 | |
|
30 |
Now we activate a color scheme: |
|
|
30 | Now we activate a color scheme: | |
|
31 | 31 | >>> ec.set_active_scheme('NoColor') |
|
32 | 32 | >>> ec.active_scheme_name |
|
33 | 33 | 'NoColor' |
|
34 | 34 | >>> ec.active_colors.keys() |
|
35 |
['em', 'filenameEm', 'excName', 'valEm', 'nameEm', 'line', 'topline', |
|
|
36 |
'name', 'caret', 'val', 'vName', 'Normal', 'filename', 'linenoEm', |
|
|
35 | ['em', 'filenameEm', 'excName', 'valEm', 'nameEm', 'line', 'topline', | |
|
36 | 'name', 'caret', 'val', 'vName', 'Normal', 'filename', 'linenoEm', | |
|
37 | 37 | 'lineno', 'normalEm'] |
|
38 | 38 | """ |
|
39 | ||
|
39 | ||
|
40 | 40 | ex_colors = ColorSchemeTable() |
|
41 | 41 | |
|
42 | 42 | # Populate it with color schemes |
|
43 | 43 | C = TermColors # shorthand and local lookup |
|
44 | 44 | ex_colors.add_scheme(ColorScheme( |
|
45 | 45 | 'NoColor', |
|
46 | 46 | # The color to be used for the top line |
|
47 | 47 | topline = C.NoColor, |
|
48 | 48 | |
|
49 | 49 | # The colors to be used in the traceback |
|
50 | 50 | filename = C.NoColor, |
|
51 | 51 | lineno = C.NoColor, |
|
52 | 52 | name = C.NoColor, |
|
53 | 53 | vName = C.NoColor, |
|
54 | 54 | val = C.NoColor, |
|
55 | 55 | em = C.NoColor, |
|
56 | 56 | |
|
57 | 57 | # Emphasized colors for the last frame of the traceback |
|
58 | 58 | normalEm = C.NoColor, |
|
59 | 59 | filenameEm = C.NoColor, |
|
60 | 60 | linenoEm = C.NoColor, |
|
61 | 61 | nameEm = C.NoColor, |
|
62 | 62 | valEm = C.NoColor, |
|
63 | 63 | |
|
64 | 64 | # Colors for printing the exception |
|
65 | 65 | excName = C.NoColor, |
|
66 | 66 | line = C.NoColor, |
|
67 | 67 | caret = C.NoColor, |
|
68 | 68 | Normal = C.NoColor |
|
69 | 69 | )) |
|
70 | 70 | |
|
71 | 71 | # make some schemes as instances so we can copy them for modification easily |
|
72 | 72 | ex_colors.add_scheme(ColorScheme( |
|
73 | 73 | 'Linux', |
|
74 | 74 | # The color to be used for the top line |
|
75 | 75 | topline = C.LightRed, |
|
76 | 76 | |
|
77 | 77 | # The colors to be used in the traceback |
|
78 | 78 | filename = C.Green, |
|
79 | 79 | lineno = C.Green, |
|
80 | 80 | name = C.Purple, |
|
81 | 81 | vName = C.Cyan, |
|
82 | 82 | val = C.Green, |
|
83 | 83 | em = C.LightCyan, |
|
84 | 84 | |
|
85 | 85 | # Emphasized colors for the last frame of the traceback |
|
86 | 86 | normalEm = C.LightCyan, |
|
87 | 87 | filenameEm = C.LightGreen, |
|
88 | 88 | linenoEm = C.LightGreen, |
|
89 | 89 | nameEm = C.LightPurple, |
|
90 | 90 | valEm = C.LightBlue, |
|
91 | 91 | |
|
92 | 92 | # Colors for printing the exception |
|
93 | 93 | excName = C.LightRed, |
|
94 | 94 | line = C.Yellow, |
|
95 | 95 | caret = C.White, |
|
96 | 96 | Normal = C.Normal |
|
97 | 97 | )) |
|
98 | 98 | |
|
99 | 99 | # For light backgrounds, swap dark/light colors |
|
100 | 100 | ex_colors.add_scheme(ColorScheme( |
|
101 | 101 | 'LightBG', |
|
102 | 102 | # The color to be used for the top line |
|
103 | 103 | topline = C.Red, |
|
104 | 104 | |
|
105 | 105 | # The colors to be used in the traceback |
|
106 | 106 | filename = C.LightGreen, |
|
107 | 107 | lineno = C.LightGreen, |
|
108 | 108 | name = C.LightPurple, |
|
109 | 109 | vName = C.Cyan, |
|
110 | 110 | val = C.LightGreen, |
|
111 | 111 | em = C.Cyan, |
|
112 | 112 | |
|
113 | 113 | # Emphasized colors for the last frame of the traceback |
|
114 | 114 | normalEm = C.Cyan, |
|
115 | 115 | filenameEm = C.Green, |
|
116 | 116 | linenoEm = C.Green, |
|
117 | 117 | nameEm = C.Purple, |
|
118 | 118 | valEm = C.Blue, |
|
119 | 119 | |
|
120 | 120 | # Colors for printing the exception |
|
121 | 121 | excName = C.Red, |
|
122 | 122 | #line = C.Brown, # brown often is displayed as yellow |
|
123 | 123 | line = C.Red, |
|
124 | 124 | caret = C.Normal, |
|
125 | 125 | Normal = C.Normal, |
|
126 | 126 | )) |
|
127 | 127 | |
|
128 | 128 | return ex_colors |
|
129 | 129 | |
|
130 | 130 | |
|
131 | 131 | # For backwards compatibility, keep around a single global object. Note that |
|
132 | 132 | # this should NOT be used, the factory function should be used instead, since |
|
133 | 133 | # these objects are stateful and it's very easy to get strange bugs if any code |
|
134 | 134 | # modifies the module-level object's state. |
|
135 | 135 | ExceptionColors = exception_colors() |
@@ -1,125 +1,125 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """A class for managing IPython extensions. |
|
3 | 3 | |
|
4 | 4 | Authors: |
|
5 | 5 | |
|
6 | 6 | * Brian Granger |
|
7 | 7 | """ |
|
8 | 8 | |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | # Copyright (C) 2010 The IPython Development Team |
|
11 | 11 | # |
|
12 | 12 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | 13 | # the file COPYING, distributed as part of this software. |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | # Imports |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | |
|
20 | 20 | import os |
|
21 | 21 | import sys |
|
22 | 22 | |
|
23 | 23 | from IPython.config.configurable import Configurable |
|
24 | 24 | from IPython.utils.traitlets import Instance |
|
25 | 25 | |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | # Main class |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | |
|
30 | 30 | class ExtensionManager(Configurable): |
|
31 | 31 | """A class to manage IPython extensions. |
|
32 | 32 | |
|
33 | 33 | An IPython extension is an importable Python module that has |
|
34 | 34 | a function with the signature:: |
|
35 | 35 | |
|
36 | 36 | def load_ipython_extension(ipython): |
|
37 | 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 | 40 | currently active :class:`InteractiveShell` instance is passed as |
|
41 | 41 | the only argument. You can do anything you want with IPython at |
|
42 | 42 | that point, including defining new magic and aliases, adding new |
|
43 | 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 | 46 | load or reload the extension again. It is up to the extension |
|
47 | 47 | author to add code to manage that. |
|
48 | 48 | |
|
49 | 49 | You can put your extension modules anywhere you want, as long as |
|
50 | 50 | they can be imported by Python's standard import mechanism. However, |
|
51 | 51 | to make it easy to write extensions, you can also put your extensions |
|
52 | 52 | in ``os.path.join(self.ipython_dir, 'extensions')``. This directory |
|
53 | 53 | is added to ``sys.path`` automatically. |
|
54 | 54 | """ |
|
55 | 55 | |
|
56 | 56 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
57 | 57 | |
|
58 | 58 | def __init__(self, shell=None, config=None): |
|
59 | 59 | super(ExtensionManager, self).__init__(shell=shell, config=config) |
|
60 | 60 | self.shell.on_trait_change( |
|
61 | 61 | self._on_ipython_dir_changed, 'ipython_dir' |
|
62 | 62 | ) |
|
63 | 63 | |
|
64 | 64 | def __del__(self): |
|
65 | 65 | self.shell.on_trait_change( |
|
66 | 66 | self._on_ipython_dir_changed, 'ipython_dir', remove=True |
|
67 | 67 | ) |
|
68 | 68 | |
|
69 | 69 | @property |
|
70 | 70 | def ipython_extension_dir(self): |
|
71 | 71 | return os.path.join(self.shell.ipython_dir, u'extensions') |
|
72 | 72 | |
|
73 | 73 | def _on_ipython_dir_changed(self): |
|
74 | 74 | if not os.path.isdir(self.ipython_extension_dir): |
|
75 | 75 | os.makedirs(self.ipython_extension_dir, mode = 0777) |
|
76 | 76 | |
|
77 | 77 | def load_extension(self, module_str): |
|
78 | 78 | """Load an IPython extension by its module name. |
|
79 | 79 | |
|
80 | 80 | If :func:`load_ipython_extension` returns anything, this function |
|
81 | 81 | will return that object. |
|
82 | 82 | """ |
|
83 | 83 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
84 | 84 | |
|
85 | 85 | if module_str not in sys.modules: |
|
86 | 86 | with prepended_to_syspath(self.ipython_extension_dir): |
|
87 | 87 | __import__(module_str) |
|
88 | 88 | mod = sys.modules[module_str] |
|
89 | 89 | return self._call_load_ipython_extension(mod) |
|
90 | 90 | |
|
91 | 91 | def unload_extension(self, module_str): |
|
92 | 92 | """Unload an IPython extension by its module name. |
|
93 | 93 | |
|
94 | 94 | This function looks up the extension's name in ``sys.modules`` and |
|
95 | 95 | simply calls ``mod.unload_ipython_extension(self)``. |
|
96 | 96 | """ |
|
97 | 97 | if module_str in sys.modules: |
|
98 | 98 | mod = sys.modules[module_str] |
|
99 | 99 | self._call_unload_ipython_extension(mod) |
|
100 | 100 | |
|
101 | 101 | def reload_extension(self, module_str): |
|
102 | 102 | """Reload an IPython extension by calling reload. |
|
103 | 103 | |
|
104 | 104 | If the module has not been loaded before, |
|
105 | 105 | :meth:`InteractiveShell.load_extension` is called. Otherwise |
|
106 | 106 | :func:`reload` is called and then the :func:`load_ipython_extension` |
|
107 | 107 | function of the module, if it exists is called. |
|
108 | 108 | """ |
|
109 | 109 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
110 | 110 | |
|
111 | 111 | with prepended_to_syspath(self.ipython_extension_dir): |
|
112 | 112 | if module_str in sys.modules: |
|
113 | 113 | mod = sys.modules[module_str] |
|
114 | 114 | reload(mod) |
|
115 | 115 | self._call_load_ipython_extension(mod) |
|
116 | 116 | else: |
|
117 | 117 | self.load_extension(module_str) |
|
118 | 118 | |
|
119 | 119 | def _call_load_ipython_extension(self, mod): |
|
120 | 120 | if hasattr(mod, 'load_ipython_extension'): |
|
121 | 121 | return mod.load_ipython_extension(self.shell) |
|
122 | 122 | |
|
123 | 123 | def _call_unload_ipython_extension(self, mod): |
|
124 | 124 | if hasattr(mod, 'unload_ipython_extension'): |
|
125 | 125 | return mod.unload_ipython_extension(self.shell) |
@@ -1,620 +1,620 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Display formatters. |
|
3 | 3 | |
|
4 | 4 | |
|
5 | 5 | Authors: |
|
6 | 6 | |
|
7 | 7 | * Robert Kern |
|
8 | 8 | * Brian Granger |
|
9 | 9 | """ |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | # Copyright (c) 2010, IPython Development Team. |
|
12 | 12 | # |
|
13 | 13 | # Distributed under the terms of the Modified BSD License. |
|
14 | 14 | # |
|
15 | 15 | # The full license is in the file COPYING.txt, distributed with this software. |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | # Imports |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | |
|
22 | 22 | # Stdlib imports |
|
23 | 23 | import abc |
|
24 | 24 | import sys |
|
25 | 25 | # We must use StringIO, as cStringIO doesn't handle unicode properly. |
|
26 | 26 | from StringIO import StringIO |
|
27 | 27 | |
|
28 | 28 | # Our own imports |
|
29 | 29 | from IPython.config.configurable import Configurable |
|
30 | 30 | from IPython.lib import pretty |
|
31 | 31 | from IPython.utils.traitlets import Bool, Dict, Int, Unicode, CUnicode, ObjectName |
|
32 | 32 | from IPython.utils.py3compat import unicode_to_str |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | #----------------------------------------------------------------------------- |
|
36 | 36 | # The main DisplayFormatter class |
|
37 | 37 | #----------------------------------------------------------------------------- |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | class DisplayFormatter(Configurable): |
|
41 | 41 | |
|
42 | 42 | # When set to true only the default plain text formatter will be used. |
|
43 | 43 | plain_text_only = Bool(False, config=True) |
|
44 | 44 | |
|
45 | 45 | # A dict of formatter whose keys are format types (MIME types) and whose |
|
46 | 46 | # values are subclasses of BaseFormatter. |
|
47 | 47 | formatters = Dict(config=True) |
|
48 | 48 | def _formatters_default(self): |
|
49 | 49 | """Activate the default formatters.""" |
|
50 | 50 | formatter_classes = [ |
|
51 | 51 | PlainTextFormatter, |
|
52 | 52 | HTMLFormatter, |
|
53 | 53 | SVGFormatter, |
|
54 | 54 | PNGFormatter, |
|
55 | 55 | JPEGFormatter, |
|
56 | 56 | LatexFormatter, |
|
57 | 57 | JSONFormatter, |
|
58 | 58 | JavascriptFormatter |
|
59 | 59 | ] |
|
60 | 60 | d = {} |
|
61 | 61 | for cls in formatter_classes: |
|
62 | 62 | f = cls(config=self.config) |
|
63 | 63 | d[f.format_type] = f |
|
64 | 64 | return d |
|
65 | 65 | |
|
66 | 66 | def format(self, obj, include=None, exclude=None): |
|
67 | 67 | """Return a format data dict for an object. |
|
68 | 68 | |
|
69 | 69 | By default all format types will be computed. |
|
70 | 70 | |
|
71 | 71 | The following MIME types are currently implemented: |
|
72 | 72 | |
|
73 | 73 | * text/plain |
|
74 | 74 | * text/html |
|
75 | 75 | * text/latex |
|
76 | 76 | * application/json |
|
77 | 77 | * application/javascript |
|
78 | 78 | * image/png |
|
79 | 79 | * image/jpeg |
|
80 | 80 | * image/svg+xml |
|
81 | 81 | |
|
82 | 82 | Parameters |
|
83 | 83 | ---------- |
|
84 | 84 | obj : object |
|
85 | 85 | The Python object whose format data will be computed. |
|
86 | 86 | include : list or tuple, optional |
|
87 | 87 | A list of format type strings (MIME types) to include in the |
|
88 | 88 | format data dict. If this is set *only* the format types included |
|
89 | 89 | in this list will be computed. |
|
90 | 90 | exclude : list or tuple, optional |
|
91 | 91 | A list of format type string (MIME types) to exclue in the format |
|
92 | 92 | data dict. If this is set all format types will be computed, |
|
93 | 93 | except for those included in this argument. |
|
94 | 94 | |
|
95 | 95 | Returns |
|
96 | 96 | ------- |
|
97 | 97 | format_dict : dict |
|
98 | 98 | A dictionary of key/value pairs, one or each format that was |
|
99 | 99 | generated for the object. The keys are the format types, which |
|
100 | 100 | will usually be MIME type strings and the values and JSON'able |
|
101 | 101 | data structure containing the raw data for the representation in |
|
102 | 102 | that format. |
|
103 | 103 | """ |
|
104 | 104 | format_dict = {} |
|
105 | 105 | |
|
106 | 106 | # If plain text only is active |
|
107 | 107 | if self.plain_text_only: |
|
108 | 108 | formatter = self.formatters['text/plain'] |
|
109 | 109 | try: |
|
110 | 110 | data = formatter(obj) |
|
111 | 111 | except: |
|
112 | 112 | # FIXME: log the exception |
|
113 | 113 | raise |
|
114 | 114 | if data is not None: |
|
115 | 115 | format_dict['text/plain'] = data |
|
116 | 116 | return format_dict |
|
117 | 117 | |
|
118 | 118 | for format_type, formatter in self.formatters.items(): |
|
119 | 119 | if include is not None: |
|
120 | 120 | if format_type not in include: |
|
121 | 121 | continue |
|
122 | 122 | if exclude is not None: |
|
123 | 123 | if format_type in exclude: |
|
124 | 124 | continue |
|
125 | 125 | try: |
|
126 | 126 | data = formatter(obj) |
|
127 | 127 | except: |
|
128 | 128 | # FIXME: log the exception |
|
129 | 129 | raise |
|
130 | 130 | if data is not None: |
|
131 | 131 | format_dict[format_type] = data |
|
132 | 132 | return format_dict |
|
133 | 133 | |
|
134 | 134 | @property |
|
135 | 135 | def format_types(self): |
|
136 | 136 | """Return the format types (MIME types) of the active formatters.""" |
|
137 | 137 | return self.formatters.keys() |
|
138 | 138 | |
|
139 | 139 | |
|
140 | 140 | #----------------------------------------------------------------------------- |
|
141 | 141 | # Formatters for specific format types (text, html, svg, etc.) |
|
142 | 142 | #----------------------------------------------------------------------------- |
|
143 | 143 | |
|
144 | 144 | |
|
145 | 145 | class FormatterABC(object): |
|
146 | 146 | """ Abstract base class for Formatters. |
|
147 | 147 | |
|
148 | 148 | A formatter is a callable class that is responsible for computing the |
|
149 | 149 | raw format data for a particular format type (MIME type). For example, |
|
150 | 150 | an HTML formatter would have a format type of `text/html` and would return |
|
151 | 151 | the HTML representation of the object when called. |
|
152 | 152 | """ |
|
153 | 153 | __metaclass__ = abc.ABCMeta |
|
154 | 154 | |
|
155 | 155 | # The format type of the data returned, usually a MIME type. |
|
156 | 156 | format_type = 'text/plain' |
|
157 | 157 | |
|
158 | 158 | # Is the formatter enabled... |
|
159 | 159 | enabled = True |
|
160 | 160 | |
|
161 | 161 | @abc.abstractmethod |
|
162 | 162 | def __call__(self, obj): |
|
163 | 163 | """Return a JSON'able representation of the object. |
|
164 | 164 | |
|
165 | 165 | If the object cannot be formatted by this formatter, then return None |
|
166 | 166 | """ |
|
167 | 167 | try: |
|
168 | 168 | return repr(obj) |
|
169 | 169 | except TypeError: |
|
170 | 170 | return None |
|
171 | 171 | |
|
172 | 172 | |
|
173 | 173 | class BaseFormatter(Configurable): |
|
174 | 174 | """A base formatter class that is configurable. |
|
175 | 175 | |
|
176 | 176 | This formatter should usually be used as the base class of all formatters. |
|
177 | 177 | It is a traited :class:`Configurable` class and includes an extensible |
|
178 | 178 | API for users to determine how their objects are formatted. The following |
|
179 | 179 | logic is used to find a function to format an given object. |
|
180 | 180 | |
|
181 | 181 | 1. The object is introspected to see if it has a method with the name |
|
182 | 182 | :attr:`print_method`. If is does, that object is passed to that method |
|
183 | 183 | for formatting. |
|
184 | 184 | 2. If no print method is found, three internal dictionaries are consulted |
|
185 | 185 | to find print method: :attr:`singleton_printers`, :attr:`type_printers` |
|
186 | 186 | and :attr:`deferred_printers`. |
|
187 | 187 | |
|
188 | 188 | Users should use these dictionaries to register functions that will be |
|
189 | 189 | used to compute the format data for their objects (if those objects don't |
|
190 | 190 | have the special print methods). The easiest way of using these |
|
191 | 191 | dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name` |
|
192 | 192 | methods. |
|
193 | 193 | |
|
194 | 194 | If no function/callable is found to compute the format data, ``None`` is |
|
195 | 195 | returned and this format type is not used. |
|
196 | 196 | """ |
|
197 | 197 | |
|
198 | 198 | format_type = Unicode('text/plain') |
|
199 | 199 | |
|
200 | 200 | enabled = Bool(True, config=True) |
|
201 | 201 | |
|
202 | 202 | print_method = ObjectName('__repr__') |
|
203 | 203 | |
|
204 | 204 | # The singleton printers. |
|
205 | 205 | # Maps the IDs of the builtin singleton objects to the format functions. |
|
206 | 206 | singleton_printers = Dict(config=True) |
|
207 | 207 | def _singleton_printers_default(self): |
|
208 | 208 | return {} |
|
209 | 209 | |
|
210 | 210 | # The type-specific printers. |
|
211 | 211 | # Map type objects to the format functions. |
|
212 | 212 | type_printers = Dict(config=True) |
|
213 | 213 | def _type_printers_default(self): |
|
214 | 214 | return {} |
|
215 | 215 | |
|
216 | 216 | # The deferred-import type-specific printers. |
|
217 | 217 | # Map (modulename, classname) pairs to the format functions. |
|
218 | 218 | deferred_printers = Dict(config=True) |
|
219 | 219 | def _deferred_printers_default(self): |
|
220 | 220 | return {} |
|
221 | 221 | |
|
222 | 222 | def __call__(self, obj): |
|
223 | 223 | """Compute the format for an object.""" |
|
224 | 224 | if self.enabled: |
|
225 | 225 | obj_id = id(obj) |
|
226 | 226 | try: |
|
227 | 227 | obj_class = getattr(obj, '__class__', None) or type(obj) |
|
228 | 228 | # First try to find registered singleton printers for the type. |
|
229 | 229 | try: |
|
230 | 230 | printer = self.singleton_printers[obj_id] |
|
231 | 231 | except (TypeError, KeyError): |
|
232 | 232 | pass |
|
233 | 233 | else: |
|
234 | 234 | return printer(obj) |
|
235 | 235 | # Next look for type_printers. |
|
236 | 236 | for cls in pretty._get_mro(obj_class): |
|
237 | 237 | if cls in self.type_printers: |
|
238 | 238 | return self.type_printers[cls](obj) |
|
239 | 239 | else: |
|
240 | 240 | printer = self._in_deferred_types(cls) |
|
241 | 241 | if printer is not None: |
|
242 | 242 | return printer(obj) |
|
243 | 243 | # Finally look for special method names. |
|
244 | 244 | if hasattr(obj_class, self.print_method): |
|
245 | 245 | printer = getattr(obj_class, self.print_method) |
|
246 | 246 | return printer(obj) |
|
247 | 247 | return None |
|
248 | 248 | except Exception: |
|
249 | 249 | pass |
|
250 | 250 | else: |
|
251 | 251 | return None |
|
252 | 252 | |
|
253 | 253 | def for_type(self, typ, func): |
|
254 | 254 | """Add a format function for a given type. |
|
255 | 255 | |
|
256 | 256 | Parameters |
|
257 | 257 | ----------- |
|
258 | 258 | typ : class |
|
259 | 259 | The class of the object that will be formatted using `func`. |
|
260 | 260 | func : callable |
|
261 | 261 | The callable that will be called to compute the format data. The |
|
262 | 262 | call signature of this function is simple, it must take the |
|
263 | 263 | object to be formatted and return the raw data for the given |
|
264 | 264 | format. Subclasses may use a different call signature for the |
|
265 | 265 | `func` argument. |
|
266 | 266 | """ |
|
267 | 267 | oldfunc = self.type_printers.get(typ, None) |
|
268 | 268 | if func is not None: |
|
269 | 269 | # To support easy restoration of old printers, we need to ignore |
|
270 | 270 | # Nones. |
|
271 | 271 | self.type_printers[typ] = func |
|
272 | 272 | return oldfunc |
|
273 | 273 | |
|
274 | 274 | def for_type_by_name(self, type_module, type_name, func): |
|
275 | 275 | """Add a format function for a type specified by the full dotted |
|
276 | 276 | module and name of the type, rather than the type of the object. |
|
277 | 277 | |
|
278 | 278 | Parameters |
|
279 | 279 | ---------- |
|
280 | 280 | type_module : str |
|
281 | 281 | The full dotted name of the module the type is defined in, like |
|
282 | 282 | ``numpy``. |
|
283 | 283 | type_name : str |
|
284 | 284 | The name of the type (the class name), like ``dtype`` |
|
285 | 285 | func : callable |
|
286 | 286 | The callable that will be called to compute the format data. The |
|
287 | 287 | call signature of this function is simple, it must take the |
|
288 | 288 | object to be formatted and return the raw data for the given |
|
289 | 289 | format. Subclasses may use a different call signature for the |
|
290 | 290 | `func` argument. |
|
291 | 291 | """ |
|
292 | 292 | key = (type_module, type_name) |
|
293 | 293 | oldfunc = self.deferred_printers.get(key, None) |
|
294 | 294 | if func is not None: |
|
295 | 295 | # To support easy restoration of old printers, we need to ignore |
|
296 | 296 | # Nones. |
|
297 | 297 | self.deferred_printers[key] = func |
|
298 | 298 | return oldfunc |
|
299 | 299 | |
|
300 | 300 | def _in_deferred_types(self, cls): |
|
301 | 301 | """ |
|
302 | 302 | Check if the given class is specified in the deferred type registry. |
|
303 | 303 | |
|
304 | 304 | Returns the printer from the registry if it exists, and None if the |
|
305 | 305 | class is not in the registry. Successful matches will be moved to the |
|
306 | 306 | regular type registry for future use. |
|
307 | 307 | """ |
|
308 | 308 | mod = getattr(cls, '__module__', None) |
|
309 | 309 | name = getattr(cls, '__name__', None) |
|
310 | 310 | key = (mod, name) |
|
311 | 311 | printer = None |
|
312 | 312 | if key in self.deferred_printers: |
|
313 | 313 | # Move the printer over to the regular registry. |
|
314 | 314 | printer = self.deferred_printers.pop(key) |
|
315 | 315 | self.type_printers[cls] = printer |
|
316 | 316 | return printer |
|
317 | 317 | |
|
318 | 318 | |
|
319 | 319 | class PlainTextFormatter(BaseFormatter): |
|
320 | 320 | """The default pretty-printer. |
|
321 | 321 | |
|
322 | 322 | This uses :mod:`IPython.external.pretty` to compute the format data of |
|
323 | 323 | the object. If the object cannot be pretty printed, :func:`repr` is used. |
|
324 | 324 | See the documentation of :mod:`IPython.external.pretty` for details on |
|
325 | 325 | how to write pretty printers. Here is a simple example:: |
|
326 | 326 | |
|
327 | 327 | def dtype_pprinter(obj, p, cycle): |
|
328 | 328 | if cycle: |
|
329 | 329 | return p.text('dtype(...)') |
|
330 | 330 | if hasattr(obj, 'fields'): |
|
331 | 331 | if obj.fields is None: |
|
332 | 332 | p.text(repr(obj)) |
|
333 | 333 | else: |
|
334 | 334 | p.begin_group(7, 'dtype([') |
|
335 | 335 | for i, field in enumerate(obj.descr): |
|
336 | 336 | if i > 0: |
|
337 | 337 | p.text(',') |
|
338 | 338 | p.breakable() |
|
339 | 339 | p.pretty(field) |
|
340 | 340 | p.end_group(7, '])') |
|
341 | 341 | """ |
|
342 | 342 | |
|
343 | 343 | # The format type of data returned. |
|
344 | 344 | format_type = Unicode('text/plain') |
|
345 | 345 | |
|
346 | 346 | # This subclass ignores this attribute as it always need to return |
|
347 | 347 | # something. |
|
348 | 348 | enabled = Bool(True, config=False) |
|
349 | 349 | |
|
350 | 350 | # Look for a _repr_pretty_ methods to use for pretty printing. |
|
351 | 351 | print_method = ObjectName('_repr_pretty_') |
|
352 | 352 | |
|
353 | 353 | # Whether to pretty-print or not. |
|
354 | 354 | pprint = Bool(True, config=True) |
|
355 | 355 | |
|
356 | 356 | # Whether to be verbose or not. |
|
357 | 357 | verbose = Bool(False, config=True) |
|
358 | 358 | |
|
359 | 359 | # The maximum width. |
|
360 | 360 | max_width = Int(79, config=True) |
|
361 | 361 | |
|
362 | 362 | # The newline character. |
|
363 | 363 | newline = Unicode('\n', config=True) |
|
364 | ||
|
364 | ||
|
365 | 365 | # format-string for pprinting floats |
|
366 | 366 | float_format = Unicode('%r') |
|
367 | 367 | # setter for float precision, either int or direct format-string |
|
368 | 368 | float_precision = CUnicode('', config=True) |
|
369 | ||
|
369 | ||
|
370 | 370 | def _float_precision_changed(self, name, old, new): |
|
371 | 371 | """float_precision changed, set float_format accordingly. |
|
372 | ||
|
372 | ||
|
373 | 373 | float_precision can be set by int or str. |
|
374 | 374 | This will set float_format, after interpreting input. |
|
375 | 375 | If numpy has been imported, numpy print precision will also be set. |
|
376 | ||
|
376 | ||
|
377 | 377 | integer `n` sets format to '%.nf', otherwise, format set directly. |
|
378 | ||
|
378 | ||
|
379 | 379 | An empty string returns to defaults (repr for float, 8 for numpy). |
|
380 | ||
|
380 | ||
|
381 | 381 | This parameter can be set via the '%precision' magic. |
|
382 | 382 | """ |
|
383 | ||
|
383 | ||
|
384 | 384 | if '%' in new: |
|
385 | 385 | # got explicit format string |
|
386 | 386 | fmt = new |
|
387 | 387 | try: |
|
388 | 388 | fmt%3.14159 |
|
389 | 389 | except Exception: |
|
390 | 390 | raise ValueError("Precision must be int or format string, not %r"%new) |
|
391 | 391 | elif new: |
|
392 | 392 | # otherwise, should be an int |
|
393 | 393 | try: |
|
394 | 394 | i = int(new) |
|
395 | 395 | assert i >= 0 |
|
396 | 396 | except ValueError: |
|
397 | 397 | raise ValueError("Precision must be int or format string, not %r"%new) |
|
398 | 398 | except AssertionError: |
|
399 | 399 | raise ValueError("int precision must be non-negative, not %r"%i) |
|
400 | ||
|
400 | ||
|
401 | 401 | fmt = '%%.%if'%i |
|
402 | 402 | if 'numpy' in sys.modules: |
|
403 | 403 | # set numpy precision if it has been imported |
|
404 | 404 | import numpy |
|
405 | 405 | numpy.set_printoptions(precision=i) |
|
406 | 406 | else: |
|
407 | 407 | # default back to repr |
|
408 | 408 | fmt = '%r' |
|
409 | 409 | if 'numpy' in sys.modules: |
|
410 | 410 | import numpy |
|
411 | 411 | # numpy default is 8 |
|
412 | 412 | numpy.set_printoptions(precision=8) |
|
413 | 413 | self.float_format = fmt |
|
414 | 414 | |
|
415 | 415 | # Use the default pretty printers from IPython.external.pretty. |
|
416 | 416 | def _singleton_printers_default(self): |
|
417 | 417 | return pretty._singleton_pprinters.copy() |
|
418 | 418 | |
|
419 | 419 | def _type_printers_default(self): |
|
420 | 420 | d = pretty._type_pprinters.copy() |
|
421 | 421 | d[float] = lambda obj,p,cycle: p.text(self.float_format%obj) |
|
422 | 422 | return d |
|
423 | 423 | |
|
424 | 424 | def _deferred_printers_default(self): |
|
425 | 425 | return pretty._deferred_type_pprinters.copy() |
|
426 | 426 | |
|
427 | 427 | #### FormatterABC interface #### |
|
428 | 428 | |
|
429 | 429 | def __call__(self, obj): |
|
430 | 430 | """Compute the pretty representation of the object.""" |
|
431 | 431 | if not self.pprint: |
|
432 | 432 | try: |
|
433 | 433 | return repr(obj) |
|
434 | 434 | except TypeError: |
|
435 | 435 | return '' |
|
436 | 436 | else: |
|
437 | 437 | # This uses use StringIO, as cStringIO doesn't handle unicode. |
|
438 | 438 | stream = StringIO() |
|
439 | 439 | # self.newline.encode() is a quick fix for issue gh-597. We need to |
|
440 | 440 | # ensure that stream does not get a mix of unicode and bytestrings, |
|
441 | 441 | # or it will cause trouble. |
|
442 | 442 | printer = pretty.RepresentationPrinter(stream, self.verbose, |
|
443 | 443 | self.max_width, unicode_to_str(self.newline), |
|
444 | 444 | singleton_pprinters=self.singleton_printers, |
|
445 | 445 | type_pprinters=self.type_printers, |
|
446 | 446 | deferred_pprinters=self.deferred_printers) |
|
447 | 447 | printer.pretty(obj) |
|
448 | 448 | printer.flush() |
|
449 | 449 | return stream.getvalue() |
|
450 | 450 | |
|
451 | 451 | |
|
452 | 452 | class HTMLFormatter(BaseFormatter): |
|
453 | 453 | """An HTML formatter. |
|
454 | 454 | |
|
455 | 455 | To define the callables that compute the HTML representation of your |
|
456 | 456 | objects, define a :meth:`_repr_html_` method or use the :meth:`for_type` |
|
457 | 457 | or :meth:`for_type_by_name` methods to register functions that handle |
|
458 | 458 | this. |
|
459 | 459 | |
|
460 | 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 | 462 | ```<html>`` or ```<body>`` tags. |
|
463 | 463 | """ |
|
464 | 464 | format_type = Unicode('text/html') |
|
465 | 465 | |
|
466 | 466 | print_method = ObjectName('_repr_html_') |
|
467 | 467 | |
|
468 | 468 | |
|
469 | 469 | class SVGFormatter(BaseFormatter): |
|
470 | 470 | """An SVG formatter. |
|
471 | 471 | |
|
472 | 472 | To define the callables that compute the SVG representation of your |
|
473 | 473 | objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type` |
|
474 | 474 | or :meth:`for_type_by_name` methods to register functions that handle |
|
475 | 475 | this. |
|
476 | 476 | |
|
477 | 477 | The return value of this formatter should be valid SVG enclosed in |
|
478 | 478 | ```<svg>``` tags, that could be injected into an existing DOM. It should |
|
479 | 479 | *not* include the ```<html>`` or ```<body>`` tags. |
|
480 | 480 | """ |
|
481 | 481 | format_type = Unicode('image/svg+xml') |
|
482 | 482 | |
|
483 | 483 | print_method = ObjectName('_repr_svg_') |
|
484 | 484 | |
|
485 | 485 | |
|
486 | 486 | class PNGFormatter(BaseFormatter): |
|
487 | 487 | """A PNG formatter. |
|
488 | 488 | |
|
489 | 489 | To define the callables that compute the PNG representation of your |
|
490 | 490 | objects, define a :meth:`_repr_png_` method or use the :meth:`for_type` |
|
491 | 491 | or :meth:`for_type_by_name` methods to register functions that handle |
|
492 | 492 | this. |
|
493 | 493 | |
|
494 | 494 | The return value of this formatter should be raw PNG data, *not* |
|
495 | 495 | base64 encoded. |
|
496 | 496 | """ |
|
497 | 497 | format_type = Unicode('image/png') |
|
498 | 498 | |
|
499 | 499 | print_method = ObjectName('_repr_png_') |
|
500 | 500 | |
|
501 | 501 | |
|
502 | 502 | class JPEGFormatter(BaseFormatter): |
|
503 | 503 | """A JPEG formatter. |
|
504 | 504 | |
|
505 | 505 | To define the callables that compute the JPEG representation of your |
|
506 | 506 | objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type` |
|
507 | 507 | or :meth:`for_type_by_name` methods to register functions that handle |
|
508 | 508 | this. |
|
509 | 509 | |
|
510 | 510 | The return value of this formatter should be raw JPEG data, *not* |
|
511 | 511 | base64 encoded. |
|
512 | 512 | """ |
|
513 | 513 | format_type = Unicode('image/jpeg') |
|
514 | 514 | |
|
515 | 515 | print_method = ObjectName('_repr_jpeg_') |
|
516 | 516 | |
|
517 | 517 | |
|
518 | 518 | class LatexFormatter(BaseFormatter): |
|
519 | 519 | """A LaTeX formatter. |
|
520 | 520 | |
|
521 | 521 | To define the callables that compute the LaTeX representation of your |
|
522 | 522 | objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type` |
|
523 | 523 | or :meth:`for_type_by_name` methods to register functions that handle |
|
524 | 524 | this. |
|
525 | 525 | |
|
526 | 526 | The return value of this formatter should be a valid LaTeX equation, |
|
527 | 527 | enclosed in either ```$``` or ```$$```. |
|
528 | 528 | """ |
|
529 | 529 | format_type = Unicode('text/latex') |
|
530 | 530 | |
|
531 | 531 | print_method = ObjectName('_repr_latex_') |
|
532 | 532 | |
|
533 | 533 | |
|
534 | 534 | class JSONFormatter(BaseFormatter): |
|
535 | 535 | """A JSON string formatter. |
|
536 | 536 | |
|
537 | 537 | To define the callables that compute the JSON string representation of |
|
538 | 538 | your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type` |
|
539 | 539 | or :meth:`for_type_by_name` methods to register functions that handle |
|
540 | 540 | this. |
|
541 | 541 | |
|
542 | 542 | The return value of this formatter should be a valid JSON string. |
|
543 | 543 | """ |
|
544 | 544 | format_type = Unicode('application/json') |
|
545 | 545 | |
|
546 | 546 | print_method = ObjectName('_repr_json_') |
|
547 | 547 | |
|
548 | 548 | |
|
549 | 549 | class JavascriptFormatter(BaseFormatter): |
|
550 | 550 | """A Javascript formatter. |
|
551 | 551 | |
|
552 | 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 | 554 | :meth:`for_type` or :meth:`for_type_by_name` methods to register functions |
|
555 | 555 | that handle this. |
|
556 | 556 | |
|
557 | 557 | The return value of this formatter should be valid Javascript code and |
|
558 | 558 | should *not* be enclosed in ```<script>``` tags. |
|
559 | 559 | """ |
|
560 | 560 | format_type = Unicode('application/javascript') |
|
561 | 561 | |
|
562 | 562 | print_method = ObjectName('_repr_javascript_') |
|
563 | 563 | |
|
564 | 564 | FormatterABC.register(BaseFormatter) |
|
565 | 565 | FormatterABC.register(PlainTextFormatter) |
|
566 | 566 | FormatterABC.register(HTMLFormatter) |
|
567 | 567 | FormatterABC.register(SVGFormatter) |
|
568 | 568 | FormatterABC.register(PNGFormatter) |
|
569 | 569 | FormatterABC.register(JPEGFormatter) |
|
570 | 570 | FormatterABC.register(LatexFormatter) |
|
571 | 571 | FormatterABC.register(JSONFormatter) |
|
572 | 572 | FormatterABC.register(JavascriptFormatter) |
|
573 | 573 | |
|
574 | 574 | |
|
575 | 575 | def format_display_data(obj, include=None, exclude=None): |
|
576 | 576 | """Return a format data dict for an object. |
|
577 | 577 | |
|
578 | 578 | By default all format types will be computed. |
|
579 | 579 | |
|
580 | 580 | The following MIME types are currently implemented: |
|
581 | 581 | |
|
582 | 582 | * text/plain |
|
583 | 583 | * text/html |
|
584 | 584 | * text/latex |
|
585 | 585 | * application/json |
|
586 | 586 | * application/javascript |
|
587 | 587 | * image/png |
|
588 | 588 | * image/jpeg |
|
589 | 589 | * image/svg+xml |
|
590 | 590 | |
|
591 | 591 | Parameters |
|
592 | 592 | ---------- |
|
593 | 593 | obj : object |
|
594 | 594 | The Python object whose format data will be computed. |
|
595 | 595 | |
|
596 | 596 | Returns |
|
597 | 597 | ------- |
|
598 | 598 | format_dict : dict |
|
599 | 599 | A dictionary of key/value pairs, one or each format that was |
|
600 | 600 | generated for the object. The keys are the format types, which |
|
601 | 601 | will usually be MIME type strings and the values and JSON'able |
|
602 | 602 | data structure containing the raw data for the representation in |
|
603 | 603 | that format. |
|
604 | 604 | include : list or tuple, optional |
|
605 | 605 | A list of format type strings (MIME types) to include in the |
|
606 | 606 | format data dict. If this is set *only* the format types included |
|
607 | 607 | in this list will be computed. |
|
608 | 608 | exclude : list or tuple, optional |
|
609 | 609 | A list of format type string (MIME types) to exclue in the format |
|
610 | 610 | data dict. If this is set all format types will be computed, |
|
611 | 611 | except for those included in this argument. |
|
612 | 612 | """ |
|
613 | 613 | from IPython.core.interactiveshell import InteractiveShell |
|
614 | 614 | |
|
615 | 615 | InteractiveShell.instance().display_formatter.format( |
|
616 | 616 | obj, |
|
617 | 617 | include, |
|
618 | 618 | exclude |
|
619 | 619 | ) |
|
620 | 620 |
@@ -1,835 +1,835 b'' | |||
|
1 | 1 | """ History related magics and functionality """ |
|
2 | 2 | #----------------------------------------------------------------------------- |
|
3 | 3 | # Copyright (C) 2010 The IPython Development Team. |
|
4 | 4 | # |
|
5 | 5 | # Distributed under the terms of the BSD License. |
|
6 | 6 | # |
|
7 | 7 | # The full license is in the file COPYING.txt, distributed with this software. |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | # Imports |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | from __future__ import print_function |
|
14 | 14 | |
|
15 | 15 | # Stdlib imports |
|
16 | 16 | import atexit |
|
17 | 17 | import datetime |
|
18 | 18 | import os |
|
19 | 19 | import re |
|
20 | 20 | import sqlite3 |
|
21 | 21 | import threading |
|
22 | 22 | |
|
23 | 23 | # Our own packages |
|
24 | 24 | from IPython.config.configurable import Configurable |
|
25 | 25 | |
|
26 | 26 | from IPython.testing.skipdoctest import skip_doctest |
|
27 | 27 | from IPython.utils import io |
|
28 | 28 | from IPython.utils.traitlets import Bool, Dict, Instance, Int, CInt, List, Unicode |
|
29 | 29 | from IPython.utils.warn import warn |
|
30 | 30 | |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | # Classes and functions |
|
33 | 33 | #----------------------------------------------------------------------------- |
|
34 | 34 | |
|
35 | 35 | class HistoryManager(Configurable): |
|
36 | 36 | """A class to organize all history-related functionality in one place. |
|
37 | 37 | """ |
|
38 | 38 | # Public interface |
|
39 | 39 | |
|
40 | 40 | # An instance of the IPython shell we are attached to |
|
41 | 41 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
42 | 42 | # Lists to hold processed and raw history. These start with a blank entry |
|
43 | 43 | # so that we can index them starting from 1 |
|
44 | 44 | input_hist_parsed = List([""]) |
|
45 | 45 | input_hist_raw = List([""]) |
|
46 | 46 | # A list of directories visited during session |
|
47 | 47 | dir_hist = List() |
|
48 | 48 | def _dir_hist_default(self): |
|
49 | 49 | try: |
|
50 | 50 | return [os.getcwdu()] |
|
51 | 51 | except OSError: |
|
52 | 52 | return [] |
|
53 | 53 | |
|
54 | 54 | # A dict of output history, keyed with ints from the shell's |
|
55 | 55 | # execution count. |
|
56 | 56 | output_hist = Dict() |
|
57 | 57 | # The text/plain repr of outputs. |
|
58 | 58 | output_hist_reprs = Dict() |
|
59 | 59 | |
|
60 | 60 | # String holding the path to the history file |
|
61 | 61 | hist_file = Unicode(config=True) |
|
62 | 62 | |
|
63 | 63 | # The SQLite database |
|
64 | 64 | db = Instance(sqlite3.Connection) |
|
65 | 65 | # The number of the current session in the history database |
|
66 | 66 | session_number = CInt() |
|
67 | 67 | # Should we log output to the database? (default no) |
|
68 | 68 | db_log_output = Bool(False, config=True) |
|
69 | 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 | 71 | db_cache_size = Int(0, config=True) |
|
72 | 72 | # The input and output caches |
|
73 | 73 | db_input_cache = List() |
|
74 | 74 | db_output_cache = List() |
|
75 | ||
|
75 | ||
|
76 | 76 | # History saving in separate thread |
|
77 | 77 | save_thread = Instance('IPython.core.history.HistorySavingThread') |
|
78 | 78 | try: # Event is a function returning an instance of _Event... |
|
79 | 79 | save_flag = Instance(threading._Event) |
|
80 | 80 | except AttributeError: # ...until Python 3.3, when it's a class. |
|
81 | 81 | save_flag = Instance(threading.Event) |
|
82 | ||
|
82 | ||
|
83 | 83 | # Private interface |
|
84 | 84 | # Variables used to store the three last inputs from the user. On each new |
|
85 | 85 | # history update, we populate the user's namespace with these, shifted as |
|
86 | 86 | # necessary. |
|
87 | 87 | _i00 = Unicode(u'') |
|
88 | 88 | _i = Unicode(u'') |
|
89 | 89 | _ii = Unicode(u'') |
|
90 | 90 | _iii = Unicode(u'') |
|
91 | 91 | |
|
92 | 92 | # A regex matching all forms of the exit command, so that we don't store |
|
93 | 93 | # them in the history (it's annoying to rewind the first entry and land on |
|
94 | 94 | # an exit call). |
|
95 | 95 | _exit_re = re.compile(r"(exit|quit)(\s*\(.*\))?$") |
|
96 | 96 | |
|
97 | 97 | def __init__(self, shell, config=None, **traits): |
|
98 | 98 | """Create a new history manager associated with a shell instance. |
|
99 | 99 | """ |
|
100 | 100 | # We need a pointer back to the shell for various tasks. |
|
101 | 101 | super(HistoryManager, self).__init__(shell=shell, config=config, |
|
102 | 102 | **traits) |
|
103 | 103 | |
|
104 | 104 | if self.hist_file == u'': |
|
105 | 105 | # No one has set the hist_file, yet. |
|
106 | 106 | histfname = 'history' |
|
107 | 107 | self.hist_file = os.path.join(shell.profile_dir.location, histfname + '.sqlite') |
|
108 | 108 | |
|
109 | 109 | try: |
|
110 | 110 | self.init_db() |
|
111 | 111 | except sqlite3.DatabaseError: |
|
112 | 112 | if os.path.isfile(self.hist_file): |
|
113 | 113 | # Try to move the file out of the way. |
|
114 | 114 | newpath = os.path.join(self.shell.profile_dir.location, "hist-corrupt.sqlite") |
|
115 | 115 | os.rename(self.hist_file, newpath) |
|
116 | 116 | print("ERROR! History file wasn't a valid SQLite database.", |
|
117 | 117 | "It was moved to %s" % newpath, "and a new file created.") |
|
118 | 118 | self.init_db() |
|
119 | 119 | else: |
|
120 | 120 | # The hist_file is probably :memory: or something else. |
|
121 | 121 | raise |
|
122 | ||
|
122 | ||
|
123 | 123 | self.save_flag = threading.Event() |
|
124 | 124 | self.db_input_cache_lock = threading.Lock() |
|
125 | 125 | self.db_output_cache_lock = threading.Lock() |
|
126 | 126 | self.save_thread = HistorySavingThread(self) |
|
127 | 127 | self.save_thread.start() |
|
128 | 128 | |
|
129 | 129 | self.new_session() |
|
130 | 130 | |
|
131 | ||
|
131 | ||
|
132 | 132 | def init_db(self): |
|
133 | 133 | """Connect to the database, and create tables if necessary.""" |
|
134 | 134 | # use detect_types so that timestamps return datetime objects |
|
135 | 135 | self.db = sqlite3.connect(self.hist_file, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) |
|
136 | 136 | self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer |
|
137 | 137 | primary key autoincrement, start timestamp, |
|
138 | 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 | 140 | (session integer, line integer, source text, source_raw text, |
|
141 | 141 | PRIMARY KEY (session, line))""") |
|
142 | 142 | # Output history is optional, but ensure the table's there so it can be |
|
143 | 143 | # enabled later. |
|
144 | 144 | self.db.execute("""CREATE TABLE IF NOT EXISTS output_history |
|
145 | 145 | (session integer, line integer, output text, |
|
146 | 146 | PRIMARY KEY (session, line))""") |
|
147 | 147 | self.db.commit() |
|
148 | ||
|
148 | ||
|
149 | 149 | def new_session(self, conn=None): |
|
150 | 150 | """Get a new session number.""" |
|
151 | 151 | if conn is None: |
|
152 | 152 | conn = self.db |
|
153 | ||
|
153 | ||
|
154 | 154 | with conn: |
|
155 | 155 | # N.B. 'insert into' here is lower case because of a bug in the |
|
156 | 156 | # sqlite3 module that affects the Turkish locale. This should be |
|
157 | 157 | # fixed for Python 2.7.3 and 3.2.3, as well as 3.3 onwards. |
|
158 | 158 | # http://bugs.python.org/issue13099 |
|
159 | 159 | cur = conn.execute("""insert into sessions VALUES (NULL, ?, NULL, |
|
160 | 160 | NULL, "") """, (datetime.datetime.now(),)) |
|
161 | 161 | self.session_number = cur.lastrowid |
|
162 | ||
|
162 | ||
|
163 | 163 | def end_session(self): |
|
164 | 164 | """Close the database session, filling in the end time and line count.""" |
|
165 | 165 | self.writeout_cache() |
|
166 | 166 | with self.db: |
|
167 | 167 | self.db.execute("""UPDATE sessions SET end=?, num_cmds=? WHERE |
|
168 | 168 | session==?""", (datetime.datetime.now(), |
|
169 | 169 | len(self.input_hist_parsed)-1, self.session_number)) |
|
170 | 170 | self.session_number = 0 |
|
171 | ||
|
171 | ||
|
172 | 172 | def name_session(self, name): |
|
173 | 173 | """Give the current session a name in the history database.""" |
|
174 | 174 | with self.db: |
|
175 | 175 | self.db.execute("UPDATE sessions SET remark=? WHERE session==?", |
|
176 | 176 | (name, self.session_number)) |
|
177 | ||
|
177 | ||
|
178 | 178 | def reset(self, new_session=True): |
|
179 | 179 | """Clear the session history, releasing all object references, and |
|
180 | 180 | optionally open a new session.""" |
|
181 | 181 | self.output_hist.clear() |
|
182 | 182 | # The directory history can't be completely empty |
|
183 | 183 | self.dir_hist[:] = [os.getcwdu()] |
|
184 | ||
|
184 | ||
|
185 | 185 | if new_session: |
|
186 | 186 | if self.session_number: |
|
187 | 187 | self.end_session() |
|
188 | 188 | self.input_hist_parsed[:] = [""] |
|
189 | 189 | self.input_hist_raw[:] = [""] |
|
190 | 190 | self.new_session() |
|
191 | ||
|
191 | ||
|
192 | 192 | ## ------------------------------- |
|
193 | 193 | ## Methods for retrieving history: |
|
194 | 194 | ## ------------------------------- |
|
195 | 195 | def _run_sql(self, sql, params, raw=True, output=False): |
|
196 | 196 | """Prepares and runs an SQL query for the history database. |
|
197 | ||
|
197 | ||
|
198 | 198 | Parameters |
|
199 | 199 | ---------- |
|
200 | 200 | sql : str |
|
201 | 201 | Any filtering expressions to go after SELECT ... FROM ... |
|
202 | 202 | params : tuple |
|
203 | 203 | Parameters passed to the SQL query (to replace "?") |
|
204 | 204 | raw, output : bool |
|
205 | 205 | See :meth:`get_range` |
|
206 | ||
|
206 | ||
|
207 | 207 | Returns |
|
208 | 208 | ------- |
|
209 | 209 | Tuples as :meth:`get_range` |
|
210 | 210 | """ |
|
211 | 211 | toget = 'source_raw' if raw else 'source' |
|
212 | 212 | sqlfrom = "history" |
|
213 | 213 | if output: |
|
214 | 214 | sqlfrom = "history LEFT JOIN output_history USING (session, line)" |
|
215 | 215 | toget = "history.%s, output_history.output" % toget |
|
216 | 216 | cur = self.db.execute("SELECT session, line, %s FROM %s " %\ |
|
217 | 217 | (toget, sqlfrom) + sql, params) |
|
218 | 218 | if output: # Regroup into 3-tuples, and parse JSON |
|
219 | 219 | return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur) |
|
220 | 220 | return cur |
|
221 | ||
|
222 | ||
|
221 | ||
|
222 | ||
|
223 | 223 | def get_session_info(self, session=0): |
|
224 | 224 | """get info about a session |
|
225 | ||
|
225 | ||
|
226 | 226 | Parameters |
|
227 | 227 | ---------- |
|
228 | ||
|
228 | ||
|
229 | 229 | session : int |
|
230 | 230 | Session number to retrieve. The current session is 0, and negative |
|
231 | 231 | numbers count back from current session, so -1 is previous session. |
|
232 | ||
|
232 | ||
|
233 | 233 | Returns |
|
234 | 234 | ------- |
|
235 | ||
|
235 | ||
|
236 | 236 | (session_id [int], start [datetime], end [datetime], num_cmds [int], remark [unicode]) |
|
237 | ||
|
237 | ||
|
238 | 238 | Sessions that are running or did not exit cleanly will have `end=None` |
|
239 | 239 | and `num_cmds=None`. |
|
240 | ||
|
240 | ||
|
241 | 241 | """ |
|
242 | ||
|
242 | ||
|
243 | 243 | if session <= 0: |
|
244 | 244 | session += self.session_number |
|
245 | ||
|
245 | ||
|
246 | 246 | query = "SELECT * from sessions where session == ?" |
|
247 | 247 | return self.db.execute(query, (session,)).fetchone() |
|
248 | ||
|
249 | ||
|
248 | ||
|
249 | ||
|
250 | 250 | def get_tail(self, n=10, raw=True, output=False, include_latest=False): |
|
251 | 251 | """Get the last n lines from the history database. |
|
252 | ||
|
252 | ||
|
253 | 253 | Parameters |
|
254 | 254 | ---------- |
|
255 | 255 | n : int |
|
256 | 256 | The number of lines to get |
|
257 | 257 | raw, output : bool |
|
258 | 258 | See :meth:`get_range` |
|
259 | 259 | include_latest : bool |
|
260 | 260 | If False (default), n+1 lines are fetched, and the latest one |
|
261 | 261 | is discarded. This is intended to be used where the function |
|
262 | 262 | is called by a user command, which it should not return. |
|
263 | ||
|
263 | ||
|
264 | 264 | Returns |
|
265 | 265 | ------- |
|
266 | 266 | Tuples as :meth:`get_range` |
|
267 | 267 | """ |
|
268 | 268 | self.writeout_cache() |
|
269 | 269 | if not include_latest: |
|
270 | 270 | n += 1 |
|
271 | 271 | cur = self._run_sql("ORDER BY session DESC, line DESC LIMIT ?", |
|
272 | 272 | (n,), raw=raw, output=output) |
|
273 | 273 | if not include_latest: |
|
274 | 274 | return reversed(list(cur)[1:]) |
|
275 | 275 | return reversed(list(cur)) |
|
276 | ||
|
276 | ||
|
277 | 277 | def search(self, pattern="*", raw=True, search_raw=True, |
|
278 | 278 | output=False): |
|
279 | 279 | """Search the database using unix glob-style matching (wildcards |
|
280 | 280 | * and ?). |
|
281 | ||
|
281 | ||
|
282 | 282 | Parameters |
|
283 | 283 | ---------- |
|
284 | 284 | pattern : str |
|
285 | 285 | The wildcarded pattern to match when searching |
|
286 | 286 | search_raw : bool |
|
287 | 287 | If True, search the raw input, otherwise, the parsed input |
|
288 | 288 | raw, output : bool |
|
289 | 289 | See :meth:`get_range` |
|
290 | ||
|
290 | ||
|
291 | 291 | Returns |
|
292 | 292 | ------- |
|
293 | 293 | Tuples as :meth:`get_range` |
|
294 | 294 | """ |
|
295 | 295 | tosearch = "source_raw" if search_raw else "source" |
|
296 | 296 | if output: |
|
297 | 297 | tosearch = "history." + tosearch |
|
298 | 298 | self.writeout_cache() |
|
299 | 299 | return self._run_sql("WHERE %s GLOB ?" % tosearch, (pattern,), |
|
300 | 300 | raw=raw, output=output) |
|
301 | ||
|
301 | ||
|
302 | 302 | def _get_range_session(self, start=1, stop=None, raw=True, output=False): |
|
303 | 303 | """Get input and output history from the current session. Called by |
|
304 | 304 | get_range, and takes similar parameters.""" |
|
305 | 305 | input_hist = self.input_hist_raw if raw else self.input_hist_parsed |
|
306 | ||
|
306 | ||
|
307 | 307 | n = len(input_hist) |
|
308 | 308 | if start < 0: |
|
309 | 309 | start += n |
|
310 | 310 | if not stop or (stop > n): |
|
311 | 311 | stop = n |
|
312 | 312 | elif stop < 0: |
|
313 | 313 | stop += n |
|
314 | ||
|
314 | ||
|
315 | 315 | for i in range(start, stop): |
|
316 | 316 | if output: |
|
317 | 317 | line = (input_hist[i], self.output_hist_reprs.get(i)) |
|
318 | 318 | else: |
|
319 | 319 | line = input_hist[i] |
|
320 | 320 | yield (0, i, line) |
|
321 | ||
|
321 | ||
|
322 | 322 | def get_range(self, session=0, start=1, stop=None, raw=True,output=False): |
|
323 | 323 | """Retrieve input by session. |
|
324 | ||
|
324 | ||
|
325 | 325 | Parameters |
|
326 | 326 | ---------- |
|
327 | 327 | session : int |
|
328 | 328 | Session number to retrieve. The current session is 0, and negative |
|
329 | 329 | numbers count back from current session, so -1 is previous session. |
|
330 | 330 | start : int |
|
331 | 331 | First line to retrieve. |
|
332 | 332 | stop : int |
|
333 | 333 | End of line range (excluded from output itself). If None, retrieve |
|
334 | 334 | to the end of the session. |
|
335 | 335 | raw : bool |
|
336 | 336 | If True, return untranslated input |
|
337 | 337 | output : bool |
|
338 | 338 | If True, attempt to include output. This will be 'real' Python |
|
339 | 339 | objects for the current session, or text reprs from previous |
|
340 | 340 | sessions if db_log_output was enabled at the time. Where no output |
|
341 | 341 | is found, None is used. |
|
342 | ||
|
342 | ||
|
343 | 343 | Returns |
|
344 | 344 | ------- |
|
345 | 345 | An iterator over the desired lines. Each line is a 3-tuple, either |
|
346 | 346 | (session, line, input) if output is False, or |
|
347 | 347 | (session, line, (input, output)) if output is True. |
|
348 | 348 | """ |
|
349 | 349 | if session == 0 or session==self.session_number: # Current session |
|
350 | 350 | return self._get_range_session(start, stop, raw, output) |
|
351 | 351 | if session < 0: |
|
352 | 352 | session += self.session_number |
|
353 | ||
|
353 | ||
|
354 | 354 | if stop: |
|
355 | 355 | lineclause = "line >= ? AND line < ?" |
|
356 | 356 | params = (session, start, stop) |
|
357 | 357 | else: |
|
358 | 358 | lineclause = "line>=?" |
|
359 | 359 | params = (session, start) |
|
360 | ||
|
360 | ||
|
361 | 361 | return self._run_sql("WHERE session==? AND %s""" % lineclause, |
|
362 | 362 | params, raw=raw, output=output) |
|
363 | ||
|
363 | ||
|
364 | 364 | def get_range_by_str(self, rangestr, raw=True, output=False): |
|
365 | 365 | """Get lines of history from a string of ranges, as used by magic |
|
366 | 366 | commands %hist, %save, %macro, etc. |
|
367 | ||
|
367 | ||
|
368 | 368 | Parameters |
|
369 | 369 | ---------- |
|
370 | 370 | rangestr : str |
|
371 | 371 | A string specifying ranges, e.g. "5 ~2/1-4". See |
|
372 | 372 | :func:`magic_history` for full details. |
|
373 | 373 | raw, output : bool |
|
374 | 374 | As :meth:`get_range` |
|
375 | ||
|
375 | ||
|
376 | 376 | Returns |
|
377 | 377 | ------- |
|
378 | 378 | Tuples as :meth:`get_range` |
|
379 | 379 | """ |
|
380 | 380 | for sess, s, e in extract_hist_ranges(rangestr): |
|
381 | 381 | for line in self.get_range(sess, s, e, raw=raw, output=output): |
|
382 | 382 | yield line |
|
383 | ||
|
383 | ||
|
384 | 384 | ## ---------------------------- |
|
385 | 385 | ## Methods for storing history: |
|
386 | 386 | ## ---------------------------- |
|
387 | 387 | def store_inputs(self, line_num, source, source_raw=None): |
|
388 | 388 | """Store source and raw input in history and create input cache |
|
389 | 389 | variables _i*. |
|
390 | ||
|
390 | ||
|
391 | 391 | Parameters |
|
392 | 392 | ---------- |
|
393 | 393 | line_num : int |
|
394 | 394 | The prompt number of this input. |
|
395 | ||
|
395 | ||
|
396 | 396 | source : str |
|
397 | 397 | Python input. |
|
398 | 398 | |
|
399 | 399 | source_raw : str, optional |
|
400 | 400 | If given, this is the raw input without any IPython transformations |
|
401 | 401 | applied to it. If not given, ``source`` is used. |
|
402 | 402 | """ |
|
403 | 403 | if source_raw is None: |
|
404 | 404 | source_raw = source |
|
405 | 405 | source = source.rstrip('\n') |
|
406 | 406 | source_raw = source_raw.rstrip('\n') |
|
407 | ||
|
407 | ||
|
408 | 408 | # do not store exit/quit commands |
|
409 | 409 | if self._exit_re.match(source_raw.strip()): |
|
410 | 410 | return |
|
411 | ||
|
411 | ||
|
412 | 412 | self.input_hist_parsed.append(source) |
|
413 | 413 | self.input_hist_raw.append(source_raw) |
|
414 | ||
|
414 | ||
|
415 | 415 | with self.db_input_cache_lock: |
|
416 | 416 | self.db_input_cache.append((line_num, source, source_raw)) |
|
417 | 417 | # Trigger to flush cache and write to DB. |
|
418 | 418 | if len(self.db_input_cache) >= self.db_cache_size: |
|
419 | 419 | self.save_flag.set() |
|
420 | 420 | |
|
421 | 421 | # update the auto _i variables |
|
422 | 422 | self._iii = self._ii |
|
423 | 423 | self._ii = self._i |
|
424 | 424 | self._i = self._i00 |
|
425 | 425 | self._i00 = source_raw |
|
426 | 426 | |
|
427 | 427 | # hackish access to user namespace to create _i1,_i2... dynamically |
|
428 | 428 | new_i = '_i%s' % line_num |
|
429 | 429 | to_main = {'_i': self._i, |
|
430 | 430 | '_ii': self._ii, |
|
431 | 431 | '_iii': self._iii, |
|
432 | 432 | new_i : self._i00 } |
|
433 | 433 | self.shell.user_ns.update(to_main) |
|
434 | ||
|
434 | ||
|
435 | 435 | def store_output(self, line_num): |
|
436 | 436 | """If database output logging is enabled, this saves all the |
|
437 | 437 | outputs from the indicated prompt number to the database. It's |
|
438 | 438 | called by run_cell after code has been executed. |
|
439 | ||
|
439 | ||
|
440 | 440 | Parameters |
|
441 | 441 | ---------- |
|
442 | 442 | line_num : int |
|
443 | 443 | The line number from which to save outputs |
|
444 | 444 | """ |
|
445 | 445 | if (not self.db_log_output) or (line_num not in self.output_hist_reprs): |
|
446 | 446 | return |
|
447 | 447 | output = self.output_hist_reprs[line_num] |
|
448 | ||
|
448 | ||
|
449 | 449 | with self.db_output_cache_lock: |
|
450 | 450 | self.db_output_cache.append((line_num, output)) |
|
451 | 451 | if self.db_cache_size <= 1: |
|
452 | 452 | self.save_flag.set() |
|
453 | ||
|
453 | ||
|
454 | 454 | def _writeout_input_cache(self, conn): |
|
455 | 455 | with conn: |
|
456 | 456 | for line in self.db_input_cache: |
|
457 | 457 | conn.execute("INSERT INTO history VALUES (?, ?, ?, ?)", |
|
458 | 458 | (self.session_number,)+line) |
|
459 | ||
|
459 | ||
|
460 | 460 | def _writeout_output_cache(self, conn): |
|
461 | 461 | with conn: |
|
462 | 462 | for line in self.db_output_cache: |
|
463 | 463 | conn.execute("INSERT INTO output_history VALUES (?, ?, ?)", |
|
464 | 464 | (self.session_number,)+line) |
|
465 | ||
|
465 | ||
|
466 | 466 | def writeout_cache(self, conn=None): |
|
467 | 467 | """Write any entries in the cache to the database.""" |
|
468 | 468 | if conn is None: |
|
469 | 469 | conn = self.db |
|
470 | ||
|
470 | ||
|
471 | 471 | with self.db_input_cache_lock: |
|
472 | 472 | try: |
|
473 | 473 | self._writeout_input_cache(conn) |
|
474 | 474 | except sqlite3.IntegrityError: |
|
475 | 475 | self.new_session(conn) |
|
476 | 476 | print("ERROR! Session/line number was not unique in", |
|
477 | 477 | "database. History logging moved to new session", |
|
478 | 478 | self.session_number) |
|
479 | 479 | try: # Try writing to the new session. If this fails, don't recurse |
|
480 | 480 | self._writeout_input_cache(conn) |
|
481 | 481 | except sqlite3.IntegrityError: |
|
482 | 482 | pass |
|
483 | 483 | finally: |
|
484 | 484 | self.db_input_cache = [] |
|
485 | 485 | |
|
486 | 486 | with self.db_output_cache_lock: |
|
487 | 487 | try: |
|
488 | 488 | self._writeout_output_cache(conn) |
|
489 | 489 | except sqlite3.IntegrityError: |
|
490 | 490 | print("!! Session/line number for output was not unique", |
|
491 | 491 | "in database. Output will not be stored.") |
|
492 | 492 | finally: |
|
493 | 493 | self.db_output_cache = [] |
|
494 | 494 | |
|
495 | 495 | |
|
496 | 496 | class HistorySavingThread(threading.Thread): |
|
497 | 497 | """This thread takes care of writing history to the database, so that |
|
498 | 498 | the UI isn't held up while that happens. |
|
499 | ||
|
499 | ||
|
500 | 500 | It waits for the HistoryManager's save_flag to be set, then writes out |
|
501 | 501 | the history cache. The main thread is responsible for setting the flag when |
|
502 | 502 | the cache size reaches a defined threshold.""" |
|
503 | 503 | daemon = True |
|
504 | 504 | stop_now = False |
|
505 | 505 | def __init__(self, history_manager): |
|
506 | 506 | super(HistorySavingThread, self).__init__() |
|
507 | 507 | self.history_manager = history_manager |
|
508 | 508 | atexit.register(self.stop) |
|
509 | ||
|
509 | ||
|
510 | 510 | def run(self): |
|
511 | 511 | # We need a separate db connection per thread: |
|
512 | 512 | try: |
|
513 | 513 | self.db = sqlite3.connect(self.history_manager.hist_file) |
|
514 | 514 | while True: |
|
515 | 515 | self.history_manager.save_flag.wait() |
|
516 | 516 | if self.stop_now: |
|
517 | 517 | return |
|
518 | 518 | self.history_manager.save_flag.clear() |
|
519 | 519 | self.history_manager.writeout_cache(self.db) |
|
520 | 520 | except Exception as e: |
|
521 | 521 | print(("The history saving thread hit an unexpected error (%s)." |
|
522 | 522 | "History will not be written to the database.") % repr(e)) |
|
523 | ||
|
523 | ||
|
524 | 524 | def stop(self): |
|
525 | 525 | """This can be called from the main thread to safely stop this thread. |
|
526 | ||
|
526 | ||
|
527 | 527 | Note that it does not attempt to write out remaining history before |
|
528 | 528 | exiting. That should be done by calling the HistoryManager's |
|
529 | 529 | end_session method.""" |
|
530 | 530 | self.stop_now = True |
|
531 | 531 | self.history_manager.save_flag.set() |
|
532 | 532 | self.join() |
|
533 | 533 | |
|
534 | ||
|
534 | ||
|
535 | 535 | # To match, e.g. ~5/8-~2/3 |
|
536 | 536 | range_re = re.compile(r""" |
|
537 | 537 | ((?P<startsess>~?\d+)/)? |
|
538 | 538 | (?P<start>\d+) # Only the start line num is compulsory |
|
539 | 539 | ((?P<sep>[\-:]) |
|
540 | 540 | ((?P<endsess>~?\d+)/)? |
|
541 | 541 | (?P<end>\d+))? |
|
542 | 542 | $""", re.VERBOSE) |
|
543 | 543 | |
|
544 | 544 | def extract_hist_ranges(ranges_str): |
|
545 | 545 | """Turn a string of history ranges into 3-tuples of (session, start, stop). |
|
546 | ||
|
546 | ||
|
547 | 547 | Examples |
|
548 | 548 | -------- |
|
549 | 549 | list(extract_input_ranges("~8/5-~7/4 2")) |
|
550 | 550 | [(-8, 5, None), (-7, 1, 4), (0, 2, 3)] |
|
551 | 551 | """ |
|
552 | 552 | for range_str in ranges_str.split(): |
|
553 | 553 | rmatch = range_re.match(range_str) |
|
554 | 554 | if not rmatch: |
|
555 | 555 | continue |
|
556 | 556 | start = int(rmatch.group("start")) |
|
557 | 557 | end = rmatch.group("end") |
|
558 | 558 | end = int(end) if end else start+1 # If no end specified, get (a, a+1) |
|
559 | 559 | if rmatch.group("sep") == "-": # 1-3 == 1:4 --> [1, 2, 3] |
|
560 | 560 | end += 1 |
|
561 | 561 | startsess = rmatch.group("startsess") or "0" |
|
562 | 562 | endsess = rmatch.group("endsess") or startsess |
|
563 | 563 | startsess = int(startsess.replace("~","-")) |
|
564 | 564 | endsess = int(endsess.replace("~","-")) |
|
565 | 565 | assert endsess >= startsess |
|
566 | 566 | |
|
567 | 567 | if endsess == startsess: |
|
568 | 568 | yield (startsess, start, end) |
|
569 | 569 | continue |
|
570 | 570 | # Multiple sessions in one range: |
|
571 | 571 | yield (startsess, start, None) |
|
572 | 572 | for sess in range(startsess+1, endsess): |
|
573 | 573 | yield (sess, 1, None) |
|
574 | 574 | yield (endsess, 1, end) |
|
575 | 575 | |
|
576 | 576 | def _format_lineno(session, line): |
|
577 | 577 | """Helper function to format line numbers properly.""" |
|
578 | 578 | if session == 0: |
|
579 | 579 | return str(line) |
|
580 | 580 | return "%s#%s" % (session, line) |
|
581 | 581 | |
|
582 | 582 | @skip_doctest |
|
583 | 583 | def magic_history(self, parameter_s = ''): |
|
584 | 584 | """Print input history (_i<n> variables), with most recent last. |
|
585 | ||
|
585 | ||
|
586 | 586 | %history -> print at most 40 inputs (some may be multi-line)\\ |
|
587 | 587 | %history n -> print at most n inputs\\ |
|
588 | 588 | %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\ |
|
589 | 589 | |
|
590 | 590 | By default, input history is printed without line numbers so it can be |
|
591 | 591 | directly pasted into an editor. Use -n to show them. |
|
592 | 592 | |
|
593 | 593 | Ranges of history can be indicated using the syntax: |
|
594 | 594 | 4 : Line 4, current session |
|
595 | 595 | 4-6 : Lines 4-6, current session |
|
596 | 596 | 243/1-5: Lines 1-5, session 243 |
|
597 | 597 | ~2/7 : Line 7, session 2 before current |
|
598 | 598 | ~8/1-~6/5 : From the first line of 8 sessions ago, to the fifth line |
|
599 | 599 | of 6 sessions ago. |
|
600 | 600 | Multiple ranges can be entered, separated by spaces |
|
601 | ||
|
601 | ||
|
602 | 602 | The same syntax is used by %macro, %save, %edit, %rerun |
|
603 | 603 | |
|
604 | 604 | Options: |
|
605 | 605 | |
|
606 | 606 | -n: print line numbers for each input. |
|
607 | 607 | This feature is only available if numbered prompts are in use. |
|
608 | 608 | |
|
609 | 609 | -o: also print outputs for each input. |
|
610 | 610 | |
|
611 | 611 | -p: print classic '>>>' python prompts before each input. This is useful |
|
612 | 612 | for making documentation, and in conjunction with -o, for producing |
|
613 | 613 | doctest-ready output. |
|
614 | 614 | |
|
615 | 615 | -r: (default) print the 'raw' history, i.e. the actual commands you typed. |
|
616 | ||
|
616 | ||
|
617 | 617 | -t: print the 'translated' history, as IPython understands it. IPython |
|
618 | 618 | filters your input and converts it all into valid Python source before |
|
619 | 619 | executing it (things like magics or aliases are turned into function |
|
620 | 620 | calls, for example). With this option, you'll see the native history |
|
621 | 621 | instead of the user-entered version: '%cd /' will be seen as |
|
622 | 622 | 'get_ipython().magic("%cd /")' instead of '%cd /'. |
|
623 | ||
|
623 | ||
|
624 | 624 | -g: treat the arg as a pattern to grep for in (full) history. |
|
625 | 625 | This includes the saved history (almost all commands ever written). |
|
626 | 626 | Use '%hist -g' to show full saved history (may be very long). |
|
627 | ||
|
627 | ||
|
628 | 628 | -l: get the last n lines from all sessions. Specify n as a single arg, or |
|
629 | 629 | the default is the last 10 lines. |
|
630 | 630 | |
|
631 | 631 | -f FILENAME: instead of printing the output to the screen, redirect it to |
|
632 | 632 | the given file. The file is always overwritten, though IPython asks for |
|
633 | 633 | confirmation first if it already exists. |
|
634 | ||
|
634 | ||
|
635 | 635 | Examples |
|
636 | 636 | -------- |
|
637 | 637 | :: |
|
638 | ||
|
638 | ||
|
639 | 639 | In [6]: %hist -n 4 6 |
|
640 | 640 | 4:a = 12 |
|
641 | 641 | 5:print a**2 |
|
642 | 642 | |
|
643 | 643 | """ |
|
644 | 644 | |
|
645 | 645 | if not self.shell.displayhook.do_full_cache: |
|
646 | 646 | print('This feature is only available if numbered prompts are in use.') |
|
647 | 647 | return |
|
648 | 648 | opts,args = self.parse_options(parameter_s,'noprtglf:',mode='string') |
|
649 | ||
|
649 | ||
|
650 | 650 | # For brevity |
|
651 | 651 | history_manager = self.shell.history_manager |
|
652 | ||
|
652 | ||
|
653 | 653 | def _format_lineno(session, line): |
|
654 | 654 | """Helper function to format line numbers properly.""" |
|
655 | 655 | if session in (0, history_manager.session_number): |
|
656 | 656 | return str(line) |
|
657 | 657 | return "%s/%s" % (session, line) |
|
658 | 658 | |
|
659 | 659 | # Check if output to specific file was requested. |
|
660 | 660 | try: |
|
661 | 661 | outfname = opts['f'] |
|
662 | 662 | except KeyError: |
|
663 | 663 | outfile = io.stdout # default |
|
664 | 664 | # We don't want to close stdout at the end! |
|
665 | 665 | close_at_end = False |
|
666 | 666 | else: |
|
667 | 667 | if os.path.exists(outfname): |
|
668 |
if not io.ask_yes_no("File %r exists. Overwrite?" % outfname): |
|
|
668 | if not io.ask_yes_no("File %r exists. Overwrite?" % outfname): | |
|
669 | 669 | print('Aborting.') |
|
670 | 670 | return |
|
671 | 671 | |
|
672 | 672 | outfile = open(outfname,'w') |
|
673 | 673 | close_at_end = True |
|
674 | ||
|
674 | ||
|
675 | 675 | print_nums = 'n' in opts |
|
676 | 676 | get_output = 'o' in opts |
|
677 | 677 | pyprompts = 'p' in opts |
|
678 | 678 | # Raw history is the default |
|
679 | 679 | raw = not('t' in opts) |
|
680 | ||
|
680 | ||
|
681 | 681 | default_length = 40 |
|
682 | 682 | pattern = None |
|
683 | ||
|
683 | ||
|
684 | 684 | if 'g' in opts: # Glob search |
|
685 | 685 | pattern = "*" + args + "*" if args else "*" |
|
686 | 686 | hist = history_manager.search(pattern, raw=raw, output=get_output) |
|
687 | 687 | print_nums = True |
|
688 | 688 | elif 'l' in opts: # Get 'tail' |
|
689 | 689 | try: |
|
690 | 690 | n = int(args) |
|
691 | 691 | except ValueError, IndexError: |
|
692 | 692 | n = 10 |
|
693 | 693 | hist = history_manager.get_tail(n, raw=raw, output=get_output) |
|
694 | 694 | else: |
|
695 | 695 | if args: # Get history by ranges |
|
696 | 696 | hist = history_manager.get_range_by_str(args, raw, get_output) |
|
697 | 697 | else: # Just get history for the current session |
|
698 | 698 | hist = history_manager.get_range(raw=raw, output=get_output) |
|
699 | ||
|
700 |
# We could be displaying the entire history, so let's not try to pull it |
|
|
699 | ||
|
700 | # We could be displaying the entire history, so let's not try to pull it | |
|
701 | 701 | # into a list in memory. Anything that needs more space will just misalign. |
|
702 | 702 | width = 4 |
|
703 | ||
|
703 | ||
|
704 | 704 | for session, lineno, inline in hist: |
|
705 | 705 | # Print user history with tabs expanded to 4 spaces. The GUI clients |
|
706 | 706 | # use hard tabs for easier usability in auto-indented code, but we want |
|
707 | 707 | # to produce PEP-8 compliant history for safe pasting into an editor. |
|
708 | 708 | if get_output: |
|
709 | 709 | inline, output = inline |
|
710 | 710 | inline = inline.expandtabs(4).rstrip() |
|
711 | ||
|
711 | ||
|
712 | 712 | multiline = "\n" in inline |
|
713 | 713 | line_sep = '\n' if multiline else ' ' |
|
714 | 714 | if print_nums: |
|
715 | 715 | print('%s:%s' % (_format_lineno(session, lineno).rjust(width), |
|
716 | 716 | line_sep), file=outfile, end='') |
|
717 | 717 | if pyprompts: |
|
718 | 718 | print(">>> ", end="", file=outfile) |
|
719 | 719 | if multiline: |
|
720 | 720 | inline = "\n... ".join(inline.splitlines()) + "\n..." |
|
721 | 721 | print(inline, file=outfile) |
|
722 | 722 | if get_output and output: |
|
723 | 723 | print(output, file=outfile) |
|
724 | 724 | |
|
725 | 725 | if close_at_end: |
|
726 | 726 | outfile.close() |
|
727 | 727 | |
|
728 | 728 | |
|
729 | 729 | def magic_rep(self, arg): |
|
730 | 730 | r"""Repeat a command, or get command to input line for editing. %recall and |
|
731 | 731 | %rep are equivalent. |
|
732 | 732 | |
|
733 | 733 | - %recall (no arguments): |
|
734 | ||
|
734 | ||
|
735 | 735 | Place a string version of last computation result (stored in the special '_' |
|
736 | 736 | variable) to the next input prompt. Allows you to create elaborate command |
|
737 | 737 | lines without using copy-paste:: |
|
738 | ||
|
738 | ||
|
739 | 739 | In[1]: l = ["hei", "vaan"] |
|
740 | 740 | In[2]: "".join(l) |
|
741 | 741 | Out[2]: heivaan |
|
742 | 742 | In[3]: %rep |
|
743 | 743 | In[4]: heivaan_ <== cursor blinking |
|
744 | ||
|
744 | ||
|
745 | 745 | %recall 45 |
|
746 | ||
|
746 | ||
|
747 | 747 | Place history line 45 on the next input prompt. Use %hist to find |
|
748 | 748 | out the number. |
|
749 | ||
|
749 | ||
|
750 | 750 | %recall 1-4 |
|
751 | ||
|
751 | ||
|
752 | 752 | Combine the specified lines into one cell, and place it on the next |
|
753 | 753 | input prompt. See %history for the slice syntax. |
|
754 | ||
|
754 | ||
|
755 | 755 | %recall foo+bar |
|
756 | ||
|
756 | ||
|
757 | 757 | If foo+bar can be evaluated in the user namespace, the result is |
|
758 | 758 | placed at the next input prompt. Otherwise, the history is searched |
|
759 | 759 | for lines which contain that substring, and the most recent one is |
|
760 | 760 | placed at the next input prompt. |
|
761 | 761 | """ |
|
762 | 762 | if not arg: # Last output |
|
763 | 763 | self.set_next_input(str(self.shell.user_ns["_"])) |
|
764 | 764 | return |
|
765 | 765 | # Get history range |
|
766 | 766 | histlines = self.history_manager.get_range_by_str(arg) |
|
767 | 767 | cmd = "\n".join(x[2] for x in histlines) |
|
768 | 768 | if cmd: |
|
769 | 769 | self.set_next_input(cmd.rstrip()) |
|
770 | 770 | return |
|
771 | 771 | |
|
772 | 772 | try: # Variable in user namespace |
|
773 | 773 | cmd = str(eval(arg, self.shell.user_ns)) |
|
774 | 774 | except Exception: # Search for term in history |
|
775 | 775 | histlines = self.history_manager.search("*"+arg+"*") |
|
776 | 776 | for h in reversed([x[2] for x in histlines]): |
|
777 | 777 | if 'rep' in h: |
|
778 | 778 | continue |
|
779 | 779 | self.set_next_input(h.rstrip()) |
|
780 | 780 | return |
|
781 | 781 | else: |
|
782 | 782 | self.set_next_input(cmd.rstrip()) |
|
783 | 783 | print("Couldn't evaluate or find in history:", arg) |
|
784 | ||
|
784 | ||
|
785 | 785 | def magic_rerun(self, parameter_s=''): |
|
786 | 786 | """Re-run previous input |
|
787 | ||
|
787 | ||
|
788 | 788 | By default, you can specify ranges of input history to be repeated |
|
789 | 789 | (as with %history). With no arguments, it will repeat the last line. |
|
790 | ||
|
790 | ||
|
791 | 791 | Options: |
|
792 | ||
|
792 | ||
|
793 | 793 | -l <n> : Repeat the last n lines of input, not including the |
|
794 | 794 | current command. |
|
795 | ||
|
795 | ||
|
796 | 796 | -g foo : Repeat the most recent line which contains foo |
|
797 | 797 | """ |
|
798 | 798 | opts, args = self.parse_options(parameter_s, 'l:g:', mode='string') |
|
799 | 799 | if "l" in opts: # Last n lines |
|
800 | 800 | n = int(opts['l']) |
|
801 | 801 | hist = self.history_manager.get_tail(n) |
|
802 | 802 | elif "g" in opts: # Search |
|
803 | 803 | p = "*"+opts['g']+"*" |
|
804 | 804 | hist = list(self.history_manager.search(p)) |
|
805 | 805 | for l in reversed(hist): |
|
806 | 806 | if "rerun" not in l[2]: |
|
807 | 807 | hist = [l] # The last match which isn't a %rerun |
|
808 | 808 | break |
|
809 | 809 | else: |
|
810 | 810 | hist = [] # No matches except %rerun |
|
811 | 811 | elif args: # Specify history ranges |
|
812 | 812 | hist = self.history_manager.get_range_by_str(args) |
|
813 | 813 | else: # Last line |
|
814 | 814 | hist = self.history_manager.get_tail(1) |
|
815 | 815 | hist = [x[2] for x in hist] |
|
816 | 816 | if not hist: |
|
817 | 817 | print("No lines in history match specification") |
|
818 | 818 | return |
|
819 | 819 | histlines = "\n".join(hist) |
|
820 | 820 | print("=== Executing: ===") |
|
821 | 821 | print(histlines) |
|
822 | 822 | print("=== Output: ===") |
|
823 | 823 | self.run_cell("\n".join(hist), store_history=False) |
|
824 | 824 | |
|
825 | 825 | |
|
826 | 826 | def init_ipython(ip): |
|
827 |
ip.define_magic("rep", magic_rep) |
|
|
827 | ip.define_magic("rep", magic_rep) | |
|
828 | 828 | ip.define_magic("recall", magic_rep) |
|
829 | 829 | ip.define_magic("rerun", magic_rerun) |
|
830 | 830 | ip.define_magic("hist",magic_history) # Alternative name |
|
831 | 831 | ip.define_magic("history",magic_history) |
|
832 | 832 | |
|
833 | 833 | # XXX - ipy_completers are in quarantine, need to be updated to new apis |
|
834 | 834 | #import ipy_completers |
|
835 | 835 | #ipy_completers.quick_completer('%hist' ,'-g -t -r -n') |
@@ -1,235 +1,235 b'' | |||
|
1 | 1 | """hooks for IPython. |
|
2 | 2 | |
|
3 | 3 | In Python, it is possible to overwrite any method of any object if you really |
|
4 | 4 | want to. But IPython exposes a few 'hooks', methods which are _designed_ to |
|
5 | 5 | be overwritten by users for customization purposes. This module defines the |
|
6 | 6 | default versions of all such hooks, which get used by IPython if not |
|
7 | 7 | overridden by the user. |
|
8 | 8 | |
|
9 | 9 | hooks are simple functions, but they should be declared with 'self' as their |
|
10 | 10 | first argument, because when activated they are registered into IPython as |
|
11 | 11 | instance methods. The self argument will be the IPython running instance |
|
12 | 12 | itself, so hooks have full access to the entire IPython object. |
|
13 | 13 | |
|
14 | 14 | If you wish to define a new hook and activate it, you need to put the |
|
15 | 15 | necessary code into a python file which can be either imported or execfile()'d |
|
16 | 16 | from within your profile's ipython_config.py configuration. |
|
17 | 17 | |
|
18 | 18 | For example, suppose that you have a module called 'myiphooks' in your |
|
19 | 19 | PYTHONPATH, which contains the following definition: |
|
20 | 20 | |
|
21 | 21 | import os |
|
22 | 22 | from IPython.core import ipapi |
|
23 | 23 | ip = ipapi.get() |
|
24 | 24 | |
|
25 | 25 | def calljed(self,filename, linenum): |
|
26 | 26 | "My editor hook calls the jed editor directly." |
|
27 | 27 | print "Calling my own editor, jed ..." |
|
28 | 28 | if os.system('jed +%d %s' % (linenum,filename)) != 0: |
|
29 | 29 | raise TryNext() |
|
30 | 30 | |
|
31 | 31 | ip.set_hook('editor', calljed) |
|
32 | 32 | |
|
33 | 33 | You can then enable the functionality by doing 'import myiphooks' |
|
34 | 34 | somewhere in your configuration files or ipython command line. |
|
35 | 35 | """ |
|
36 | 36 | |
|
37 | 37 | #***************************************************************************** |
|
38 | 38 | # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu> |
|
39 | 39 | # |
|
40 | 40 | # Distributed under the terms of the BSD License. The full license is in |
|
41 | 41 | # the file COPYING, distributed as part of this software. |
|
42 | 42 | #***************************************************************************** |
|
43 | 43 | |
|
44 | 44 | import os, bisect |
|
45 | 45 | import sys |
|
46 | 46 | |
|
47 | 47 | from IPython.core.error import TryNext |
|
48 | 48 | |
|
49 | 49 | # List here all the default hooks. For now it's just the editor functions |
|
50 | 50 | # but over time we'll move here all the public API for user-accessible things. |
|
51 | 51 | |
|
52 | 52 | __all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor', |
|
53 | 53 | 'input_prefilter', 'shutdown_hook', 'late_startup_hook', |
|
54 | 54 | 'generate_prompt', 'show_in_pager','pre_prompt_hook', |
|
55 | 55 | 'pre_run_code_hook', 'clipboard_get'] |
|
56 | 56 | |
|
57 | 57 | def editor(self,filename, linenum=None): |
|
58 | 58 | """Open the default editor at the given filename and linenumber. |
|
59 | 59 | |
|
60 | 60 | This is IPython's default editor hook, you can use it as an example to |
|
61 | 61 | write your own modified one. To set your own editor function as the |
|
62 | 62 | new editor hook, call ip.set_hook('editor',yourfunc).""" |
|
63 | 63 | |
|
64 | 64 | # IPython configures a default editor at startup by reading $EDITOR from |
|
65 | 65 | # the environment, and falling back on vi (unix) or notepad (win32). |
|
66 | 66 | editor = self.editor |
|
67 | ||
|
67 | ||
|
68 | 68 | # marker for at which line to open the file (for existing objects) |
|
69 | 69 | if linenum is None or editor=='notepad': |
|
70 | 70 | linemark = '' |
|
71 | 71 | else: |
|
72 | 72 | linemark = '+%d' % int(linenum) |
|
73 | ||
|
73 | ||
|
74 | 74 | # Enclose in quotes if necessary and legal |
|
75 | 75 | if ' ' in editor and os.path.isfile(editor) and editor[0] != '"': |
|
76 | 76 | editor = '"%s"' % editor |
|
77 | ||
|
77 | ||
|
78 | 78 | # Call the actual editor |
|
79 | 79 | if os.system('%s %s %s' % (editor,linemark,filename)) != 0: |
|
80 | 80 | raise TryNext() |
|
81 | 81 | |
|
82 | 82 | import tempfile |
|
83 | 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 | 85 | show an error message. This is used for correcting syntax errors. |
|
86 | 86 | The current implementation only has special support for the VIM editor, |
|
87 | 87 | and falls back on the 'editor' hook if VIM is not used. |
|
88 | 88 | |
|
89 | 89 | Call ip.set_hook('fix_error_editor',youfunc) to use your own function, |
|
90 | 90 | """ |
|
91 | 91 | def vim_quickfix_file(): |
|
92 | 92 | t = tempfile.NamedTemporaryFile() |
|
93 | 93 | t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg)) |
|
94 | 94 | t.flush() |
|
95 | 95 | return t |
|
96 | 96 | if os.path.basename(self.editor) != 'vim': |
|
97 | 97 | self.hooks.editor(filename,linenum) |
|
98 | 98 | return |
|
99 | 99 | t = vim_quickfix_file() |
|
100 | 100 | try: |
|
101 | 101 | if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name): |
|
102 | 102 | raise TryNext() |
|
103 | 103 | finally: |
|
104 | 104 | t.close() |
|
105 | 105 | |
|
106 | 106 | |
|
107 | 107 | def synchronize_with_editor(self, filename, linenum, column): |
|
108 | 108 | pass |
|
109 | 109 | |
|
110 | 110 | |
|
111 | 111 | class CommandChainDispatcher: |
|
112 | 112 | """ Dispatch calls to a chain of commands until some func can handle it |
|
113 | ||
|
113 | ||
|
114 | 114 | Usage: instantiate, execute "add" to add commands (with optional |
|
115 | 115 | priority), execute normally via f() calling mechanism. |
|
116 | ||
|
116 | ||
|
117 | 117 | """ |
|
118 | 118 | def __init__(self,commands=None): |
|
119 | 119 | if commands is None: |
|
120 | 120 | self.chain = [] |
|
121 | 121 | else: |
|
122 | 122 | self.chain = commands |
|
123 | ||
|
124 | ||
|
123 | ||
|
124 | ||
|
125 | 125 | def __call__(self,*args, **kw): |
|
126 |
""" Command chain is called just like normal func. |
|
|
127 | ||
|
126 | """ Command chain is called just like normal func. | |
|
127 | ||
|
128 | 128 | This will call all funcs in chain with the same args as were given to this |
|
129 | 129 | function, and return the result of first func that didn't raise |
|
130 | 130 | TryNext """ |
|
131 | ||
|
131 | ||
|
132 | 132 | for prio,cmd in self.chain: |
|
133 | 133 | #print "prio",prio,"cmd",cmd #dbg |
|
134 | 134 | try: |
|
135 | 135 | return cmd(*args, **kw) |
|
136 | 136 | except TryNext, exc: |
|
137 | 137 | if exc.args or exc.kwargs: |
|
138 | 138 | args = exc.args |
|
139 | 139 | kw = exc.kwargs |
|
140 | 140 | # if no function will accept it, raise TryNext up to the caller |
|
141 | 141 | raise TryNext |
|
142 | ||
|
142 | ||
|
143 | 143 | def __str__(self): |
|
144 | 144 | return str(self.chain) |
|
145 | ||
|
145 | ||
|
146 | 146 | def add(self, func, priority=0): |
|
147 | 147 | """ Add a func to the cmd chain with given priority """ |
|
148 | 148 | bisect.insort(self.chain,(priority,func)) |
|
149 | 149 | |
|
150 | 150 | def __iter__(self): |
|
151 | 151 | """ Return all objects in chain. |
|
152 | ||
|
152 | ||
|
153 | 153 | Handy if the objects are not callable. |
|
154 | 154 | """ |
|
155 | 155 | return iter(self.chain) |
|
156 | 156 | |
|
157 | 157 | |
|
158 |
def input_prefilter(self,line): |
|
|
158 | def input_prefilter(self,line): | |
|
159 | 159 | """ Default input prefilter |
|
160 | ||
|
160 | ||
|
161 | 161 | This returns the line as unchanged, so that the interpreter |
|
162 | 162 | knows that nothing was done and proceeds with "classic" prefiltering |
|
163 |
(%magics, !shell commands etc.). |
|
|
164 | ||
|
163 | (%magics, !shell commands etc.). | |
|
164 | ||
|
165 | 165 | Note that leading whitespace is not passed to this hook. Prefilter |
|
166 | 166 | can't alter indentation. |
|
167 | ||
|
167 | ||
|
168 | 168 | """ |
|
169 | 169 | #print "attempt to rewrite",line #dbg |
|
170 | 170 | return line |
|
171 | 171 | |
|
172 | 172 | |
|
173 | 173 | def shutdown_hook(self): |
|
174 | 174 | """ default shutdown hook |
|
175 | ||
|
175 | ||
|
176 | 176 | Typically, shotdown hooks should raise TryNext so all shutdown ops are done |
|
177 | 177 | """ |
|
178 | ||
|
178 | ||
|
179 | 179 | #print "default shutdown hook ok" # dbg |
|
180 | 180 | return |
|
181 | 181 | |
|
182 | 182 | |
|
183 | 183 | def late_startup_hook(self): |
|
184 |
""" Executed after ipython has been constructed and configured |
|
|
185 | ||
|
184 | """ Executed after ipython has been constructed and configured | |
|
185 | ||
|
186 | 186 | """ |
|
187 | 187 | #print "default startup hook ok" # dbg |
|
188 | 188 | |
|
189 | 189 | |
|
190 | 190 | def generate_prompt(self, is_continuation): |
|
191 | 191 | """ calculate and return a string with the prompt to display """ |
|
192 | 192 | if is_continuation: |
|
193 | 193 | return str(self.displayhook.prompt2) |
|
194 | 194 | return str(self.displayhook.prompt1) |
|
195 | 195 | |
|
196 | 196 | |
|
197 | 197 | def show_in_pager(self,s): |
|
198 | 198 | """ Run a string through pager """ |
|
199 | 199 | # raising TryNext here will use the default paging functionality |
|
200 | 200 | raise TryNext |
|
201 | 201 | |
|
202 | 202 | |
|
203 | 203 | def pre_prompt_hook(self): |
|
204 | 204 | """ Run before displaying the next prompt |
|
205 | ||
|
206 |
Use this e.g. to display output from asynchronous operations (in order |
|
|
207 |
to not mess up text entry) |
|
|
205 | ||
|
206 | Use this e.g. to display output from asynchronous operations (in order | |
|
207 | to not mess up text entry) | |
|
208 | 208 | """ |
|
209 | ||
|
209 | ||
|
210 | 210 | return None |
|
211 | 211 | |
|
212 | 212 | |
|
213 | 213 | def pre_run_code_hook(self): |
|
214 | 214 | """ Executed before running the (prefiltered) code in IPython """ |
|
215 | 215 | return None |
|
216 | 216 | |
|
217 | 217 | |
|
218 | 218 | def clipboard_get(self): |
|
219 | 219 | """ Get text from the clipboard. |
|
220 | 220 | """ |
|
221 | 221 | from IPython.lib.clipboard import ( |
|
222 |
osx_clipboard_get, tkinter_clipboard_get, |
|
|
222 | osx_clipboard_get, tkinter_clipboard_get, | |
|
223 | 223 | win32_clipboard_get |
|
224 | 224 | ) |
|
225 | 225 | if sys.platform == 'win32': |
|
226 | 226 | chain = [win32_clipboard_get, tkinter_clipboard_get] |
|
227 | 227 | elif sys.platform == 'darwin': |
|
228 | 228 | chain = [osx_clipboard_get, tkinter_clipboard_get] |
|
229 | 229 | else: |
|
230 | 230 | chain = [tkinter_clipboard_get] |
|
231 | 231 | dispatcher = CommandChainDispatcher() |
|
232 | 232 | for func in chain: |
|
233 | 233 | dispatcher.add(func) |
|
234 | 234 | text = dispatcher() |
|
235 | 235 | return text |
@@ -1,2598 +1,2598 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Main IPython class.""" |
|
3 | 3 | |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
6 | 6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
7 | 7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | from __future__ import with_statement |
|
18 | 18 | from __future__ import absolute_import |
|
19 | 19 | |
|
20 | 20 | import __builtin__ as builtin_mod |
|
21 | 21 | import __future__ |
|
22 | 22 | import abc |
|
23 | 23 | import ast |
|
24 | 24 | import atexit |
|
25 | 25 | import codeop |
|
26 | 26 | import inspect |
|
27 | 27 | import os |
|
28 | 28 | import re |
|
29 | 29 | import sys |
|
30 | 30 | import tempfile |
|
31 | 31 | import types |
|
32 | 32 | try: |
|
33 | 33 | from contextlib import nested |
|
34 | 34 | except: |
|
35 | 35 | from IPython.utils.nested_context import nested |
|
36 | 36 | |
|
37 | 37 | from IPython.config.configurable import SingletonConfigurable |
|
38 | 38 | from IPython.core import debugger, oinspect |
|
39 | 39 | from IPython.core import history as ipcorehist |
|
40 | 40 | from IPython.core import page |
|
41 | 41 | from IPython.core import prefilter |
|
42 | 42 | from IPython.core import shadowns |
|
43 | 43 | from IPython.core import ultratb |
|
44 | 44 | from IPython.core.alias import AliasManager, AliasError |
|
45 | 45 | from IPython.core.autocall import ExitAutocall |
|
46 | 46 | from IPython.core.builtin_trap import BuiltinTrap |
|
47 | 47 | from IPython.core.compilerop import CachingCompiler |
|
48 | 48 | from IPython.core.display_trap import DisplayTrap |
|
49 | 49 | from IPython.core.displayhook import DisplayHook |
|
50 | 50 | from IPython.core.displaypub import DisplayPublisher |
|
51 | 51 | from IPython.core.error import TryNext, UsageError |
|
52 | 52 | from IPython.core.extensions import ExtensionManager |
|
53 | 53 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict |
|
54 | 54 | from IPython.core.formatters import DisplayFormatter |
|
55 | 55 | from IPython.core.history import HistoryManager |
|
56 | 56 | from IPython.core.inputsplitter import IPythonInputSplitter |
|
57 | 57 | from IPython.core.logger import Logger |
|
58 | 58 | from IPython.core.macro import Macro |
|
59 | 59 | from IPython.core.magic import Magic |
|
60 | 60 | from IPython.core.payload import PayloadManager |
|
61 | 61 | from IPython.core.plugin import PluginManager |
|
62 | 62 | from IPython.core.prefilter import PrefilterManager, ESC_MAGIC |
|
63 | 63 | from IPython.core.profiledir import ProfileDir |
|
64 | 64 | from IPython.external.Itpl import ItplNS |
|
65 | 65 | from IPython.utils import PyColorize |
|
66 | 66 | from IPython.utils import io |
|
67 | 67 | from IPython.utils import py3compat |
|
68 | 68 | from IPython.utils.doctestreload import doctest_reload |
|
69 | 69 | from IPython.utils.io import ask_yes_no, rprint |
|
70 | 70 | from IPython.utils.ipstruct import Struct |
|
71 | 71 | from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError |
|
72 | 72 | from IPython.utils.pickleshare import PickleShareDB |
|
73 | 73 | from IPython.utils.process import system, getoutput |
|
74 | 74 | from IPython.utils.strdispatch import StrDispatch |
|
75 | 75 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
76 | 76 | from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList |
|
77 | 77 | from IPython.utils.traitlets import (Int, CBool, CaselessStrEnum, Enum, |
|
78 | 78 | List, Unicode, Instance, Type) |
|
79 | 79 | from IPython.utils.warn import warn, error, fatal |
|
80 | 80 | import IPython.core.hooks |
|
81 | 81 | |
|
82 | 82 | #----------------------------------------------------------------------------- |
|
83 | 83 | # Globals |
|
84 | 84 | #----------------------------------------------------------------------------- |
|
85 | 85 | |
|
86 | 86 | # compiled regexps for autoindent management |
|
87 | 87 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
88 | 88 | |
|
89 | 89 | #----------------------------------------------------------------------------- |
|
90 | 90 | # Utilities |
|
91 | 91 | #----------------------------------------------------------------------------- |
|
92 | 92 | |
|
93 | 93 | def softspace(file, newvalue): |
|
94 | 94 | """Copied from code.py, to remove the dependency""" |
|
95 | 95 | |
|
96 | 96 | oldvalue = 0 |
|
97 | 97 | try: |
|
98 | 98 | oldvalue = file.softspace |
|
99 | 99 | except AttributeError: |
|
100 | 100 | pass |
|
101 | 101 | try: |
|
102 | 102 | file.softspace = newvalue |
|
103 | 103 | except (AttributeError, TypeError): |
|
104 | 104 | # "attribute-less object" or "read-only attributes" |
|
105 | 105 | pass |
|
106 | 106 | return oldvalue |
|
107 | 107 | |
|
108 | 108 | |
|
109 | 109 | def no_op(*a, **kw): pass |
|
110 | 110 | |
|
111 | 111 | class NoOpContext(object): |
|
112 | 112 | def __enter__(self): pass |
|
113 | 113 | def __exit__(self, type, value, traceback): pass |
|
114 | 114 | no_op_context = NoOpContext() |
|
115 | 115 | |
|
116 | 116 | class SpaceInInput(Exception): pass |
|
117 | 117 | |
|
118 | 118 | class Bunch: pass |
|
119 | 119 | |
|
120 | 120 | |
|
121 | 121 | def get_default_colors(): |
|
122 | 122 | if sys.platform=='darwin': |
|
123 | 123 | return "LightBG" |
|
124 | 124 | elif os.name=='nt': |
|
125 | 125 | return 'Linux' |
|
126 | 126 | else: |
|
127 | 127 | return 'Linux' |
|
128 | 128 | |
|
129 | 129 | |
|
130 | 130 | class SeparateUnicode(Unicode): |
|
131 | 131 | """A Unicode subclass to validate separate_in, separate_out, etc. |
|
132 | 132 | |
|
133 | 133 | This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'. |
|
134 | 134 | """ |
|
135 | 135 | |
|
136 | 136 | def validate(self, obj, value): |
|
137 | 137 | if value == '0': value = '' |
|
138 | 138 | value = value.replace('\\n','\n') |
|
139 | 139 | return super(SeparateUnicode, self).validate(obj, value) |
|
140 | 140 | |
|
141 | 141 | |
|
142 | 142 | class ReadlineNoRecord(object): |
|
143 | 143 | """Context manager to execute some code, then reload readline history |
|
144 | 144 | so that interactive input to the code doesn't appear when pressing up.""" |
|
145 | 145 | def __init__(self, shell): |
|
146 | 146 | self.shell = shell |
|
147 | 147 | self._nested_level = 0 |
|
148 | ||
|
148 | ||
|
149 | 149 | def __enter__(self): |
|
150 | 150 | if self._nested_level == 0: |
|
151 | 151 | try: |
|
152 | 152 | self.orig_length = self.current_length() |
|
153 | 153 | self.readline_tail = self.get_readline_tail() |
|
154 | 154 | except (AttributeError, IndexError): # Can fail with pyreadline |
|
155 | 155 | self.orig_length, self.readline_tail = 999999, [] |
|
156 | 156 | self._nested_level += 1 |
|
157 | ||
|
157 | ||
|
158 | 158 | def __exit__(self, type, value, traceback): |
|
159 | 159 | self._nested_level -= 1 |
|
160 | 160 | if self._nested_level == 0: |
|
161 | 161 | # Try clipping the end if it's got longer |
|
162 | 162 | try: |
|
163 | 163 | e = self.current_length() - self.orig_length |
|
164 | 164 | if e > 0: |
|
165 | 165 | for _ in range(e): |
|
166 | 166 | self.shell.readline.remove_history_item(self.orig_length) |
|
167 | ||
|
167 | ||
|
168 | 168 | # If it still doesn't match, just reload readline history. |
|
169 | 169 | if self.current_length() != self.orig_length \ |
|
170 | 170 | or self.get_readline_tail() != self.readline_tail: |
|
171 | 171 | self.shell.refill_readline_hist() |
|
172 | 172 | except (AttributeError, IndexError): |
|
173 | 173 | pass |
|
174 | 174 | # Returning False will cause exceptions to propagate |
|
175 | 175 | return False |
|
176 | ||
|
176 | ||
|
177 | 177 | def current_length(self): |
|
178 | 178 | return self.shell.readline.get_current_history_length() |
|
179 | ||
|
179 | ||
|
180 | 180 | def get_readline_tail(self, n=10): |
|
181 | 181 | """Get the last n items in readline history.""" |
|
182 | 182 | end = self.shell.readline.get_current_history_length() + 1 |
|
183 | 183 | start = max(end-n, 1) |
|
184 | 184 | ghi = self.shell.readline.get_history_item |
|
185 | 185 | return [ghi(x) for x in range(start, end)] |
|
186 | 186 | |
|
187 | 187 | |
|
188 | 188 | _autocall_help = """ |
|
189 | 189 | Make IPython automatically call any callable object even if |
|
190 | 190 | you didn't type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
191 | 191 | automatically. The value can be '0' to disable the feature, '1' for 'smart' |
|
192 | 192 | autocall, where it is not applied if there are no more arguments on the line, |
|
193 | 193 | and '2' for 'full' autocall, where all callable objects are automatically |
|
194 | 194 | called (even if no arguments are present). The default is '1'. |
|
195 | 195 | """ |
|
196 | 196 | |
|
197 | 197 | #----------------------------------------------------------------------------- |
|
198 | 198 | # Main IPython class |
|
199 | 199 | #----------------------------------------------------------------------------- |
|
200 | 200 | |
|
201 | 201 | class InteractiveShell(SingletonConfigurable, Magic): |
|
202 | 202 | """An enhanced, interactive shell for Python.""" |
|
203 | 203 | |
|
204 | 204 | _instance = None |
|
205 | 205 | |
|
206 | 206 | autocall = Enum((0,1,2), default_value=1, config=True, help= |
|
207 | 207 | """ |
|
208 | 208 | Make IPython automatically call any callable object even if you didn't |
|
209 | 209 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
210 | 210 | automatically. The value can be '0' to disable the feature, '1' for |
|
211 | 211 | 'smart' autocall, where it is not applied if there are no more |
|
212 | 212 | arguments on the line, and '2' for 'full' autocall, where all callable |
|
213 | 213 | objects are automatically called (even if no arguments are present). |
|
214 | 214 | The default is '1'. |
|
215 | 215 | """ |
|
216 | 216 | ) |
|
217 | 217 | # TODO: remove all autoindent logic and put into frontends. |
|
218 | 218 | # We can't do this yet because even runlines uses the autoindent. |
|
219 | 219 | autoindent = CBool(True, config=True, help= |
|
220 | 220 | """ |
|
221 | 221 | Autoindent IPython code entered interactively. |
|
222 | 222 | """ |
|
223 | 223 | ) |
|
224 | 224 | automagic = CBool(True, config=True, help= |
|
225 | 225 | """ |
|
226 | 226 | Enable magic commands to be called without the leading %. |
|
227 | 227 | """ |
|
228 | 228 | ) |
|
229 | 229 | cache_size = Int(1000, config=True, help= |
|
230 | 230 | """ |
|
231 | 231 | Set the size of the output cache. The default is 1000, you can |
|
232 | 232 | change it permanently in your config file. Setting it to 0 completely |
|
233 | 233 | disables the caching system, and the minimum value accepted is 20 (if |
|
234 | 234 | you provide a value less than 20, it is reset to 0 and a warning is |
|
235 | 235 | issued). This limit is defined because otherwise you'll spend more |
|
236 | 236 | time re-flushing a too small cache than working |
|
237 | 237 | """ |
|
238 | 238 | ) |
|
239 | 239 | color_info = CBool(True, config=True, help= |
|
240 | 240 | """ |
|
241 | 241 | Use colors for displaying information about objects. Because this |
|
242 | 242 | information is passed through a pager (like 'less'), and some pagers |
|
243 | 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 | 247 | default_value=get_default_colors(), config=True, |
|
248 | 248 | help="Set the color scheme (NoColor, Linux, or LightBG)." |
|
249 | 249 | ) |
|
250 | 250 | colors_force = CBool(False, help= |
|
251 | 251 | """ |
|
252 | 252 | Force use of ANSI color codes, regardless of OS and readline |
|
253 | 253 | availability. |
|
254 | 254 | """ |
|
255 | 255 | # FIXME: This is essentially a hack to allow ZMQShell to show colors |
|
256 | 256 | # without readline on Win32. When the ZMQ formatting system is |
|
257 | 257 | # refactored, this should be removed. |
|
258 | 258 | ) |
|
259 | 259 | debug = CBool(False, config=True) |
|
260 | 260 | deep_reload = CBool(False, config=True, help= |
|
261 | 261 | """ |
|
262 | 262 | Enable deep (recursive) reloading by default. IPython can use the |
|
263 | 263 | deep_reload module which reloads changes in modules recursively (it |
|
264 | 264 | replaces the reload() function, so you don't need to change anything to |
|
265 | 265 | use it). deep_reload() forces a full reload of modules whose code may |
|
266 | 266 | have changed, which the default reload() function does not. When |
|
267 | 267 | deep_reload is off, IPython will use the normal reload(), but |
|
268 | 268 | deep_reload will still be available as dreload(). |
|
269 | 269 | """ |
|
270 | 270 | ) |
|
271 | 271 | display_formatter = Instance(DisplayFormatter) |
|
272 | 272 | displayhook_class = Type(DisplayHook) |
|
273 | 273 | display_pub_class = Type(DisplayPublisher) |
|
274 | 274 | |
|
275 | 275 | exit_now = CBool(False) |
|
276 | 276 | exiter = Instance(ExitAutocall) |
|
277 | 277 | def _exiter_default(self): |
|
278 | 278 | return ExitAutocall(self) |
|
279 | 279 | # Monotonically increasing execution counter |
|
280 | 280 | execution_count = Int(1) |
|
281 | 281 | filename = Unicode("<ipython console>") |
|
282 | 282 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ |
|
283 | 283 | |
|
284 | 284 | # Input splitter, to split entire cells of input into either individual |
|
285 | 285 | # interactive statements or whole blocks. |
|
286 | 286 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
287 | 287 | (), {}) |
|
288 | 288 | logstart = CBool(False, config=True, help= |
|
289 | 289 | """ |
|
290 | 290 | Start logging to the default log file. |
|
291 | 291 | """ |
|
292 | 292 | ) |
|
293 | 293 | logfile = Unicode('', config=True, help= |
|
294 | 294 | """ |
|
295 | 295 | The name of the logfile to use. |
|
296 | 296 | """ |
|
297 | 297 | ) |
|
298 | 298 | logappend = Unicode('', config=True, help= |
|
299 | 299 | """ |
|
300 | 300 | Start logging to the given file in append mode. |
|
301 | 301 | """ |
|
302 | 302 | ) |
|
303 | 303 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
304 | 304 | config=True) |
|
305 | 305 | pdb = CBool(False, config=True, help= |
|
306 | 306 | """ |
|
307 | 307 | Automatically call the pdb debugger after every exception. |
|
308 | 308 | """ |
|
309 | 309 | ) |
|
310 | 310 | |
|
311 | 311 | prompt_in1 = Unicode('In [\\#]: ', config=True) |
|
312 | 312 | prompt_in2 = Unicode(' .\\D.: ', config=True) |
|
313 | 313 | prompt_out = Unicode('Out[\\#]: ', config=True) |
|
314 | 314 | prompts_pad_left = CBool(True, config=True) |
|
315 | 315 | quiet = CBool(False, config=True) |
|
316 | 316 | |
|
317 | 317 | history_length = Int(10000, config=True) |
|
318 | ||
|
318 | ||
|
319 | 319 | # The readline stuff will eventually be moved to the terminal subclass |
|
320 | 320 | # but for now, we can't do that as readline is welded in everywhere. |
|
321 | 321 | readline_use = CBool(True, config=True) |
|
322 | 322 | readline_merge_completions = CBool(True, config=True) |
|
323 | 323 | readline_omit__names = Enum((0,1,2), default_value=2, config=True) |
|
324 | 324 | readline_remove_delims = Unicode('-/~', config=True) |
|
325 | 325 | # don't use \M- bindings by default, because they |
|
326 | 326 | # conflict with 8-bit encodings. See gh-58,gh-88 |
|
327 | 327 | readline_parse_and_bind = List([ |
|
328 | 328 | 'tab: complete', |
|
329 | 329 | '"\C-l": clear-screen', |
|
330 | 330 | 'set show-all-if-ambiguous on', |
|
331 | 331 | '"\C-o": tab-insert', |
|
332 | 332 | '"\C-r": reverse-search-history', |
|
333 | 333 | '"\C-s": forward-search-history', |
|
334 | 334 | '"\C-p": history-search-backward', |
|
335 | 335 | '"\C-n": history-search-forward', |
|
336 | 336 | '"\e[A": history-search-backward', |
|
337 | 337 | '"\e[B": history-search-forward', |
|
338 | 338 | '"\C-k": kill-line', |
|
339 | 339 | '"\C-u": unix-line-discard', |
|
340 | 340 | ], allow_none=False, config=True) |
|
341 | 341 | |
|
342 | 342 | # TODO: this part of prompt management should be moved to the frontends. |
|
343 | 343 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
344 | 344 | separate_in = SeparateUnicode('\n', config=True) |
|
345 | 345 | separate_out = SeparateUnicode('', config=True) |
|
346 | 346 | separate_out2 = SeparateUnicode('', config=True) |
|
347 | 347 | wildcards_case_sensitive = CBool(True, config=True) |
|
348 |
xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
|
348 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), | |
|
349 | 349 | default_value='Context', config=True) |
|
350 | 350 | |
|
351 | 351 | # Subcomponents of InteractiveShell |
|
352 | 352 | alias_manager = Instance('IPython.core.alias.AliasManager') |
|
353 | 353 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') |
|
354 | 354 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') |
|
355 | 355 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') |
|
356 | 356 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') |
|
357 | 357 | plugin_manager = Instance('IPython.core.plugin.PluginManager') |
|
358 | 358 | payload_manager = Instance('IPython.core.payload.PayloadManager') |
|
359 | 359 | history_manager = Instance('IPython.core.history.HistoryManager') |
|
360 | 360 | |
|
361 | 361 | profile_dir = Instance('IPython.core.application.ProfileDir') |
|
362 | 362 | @property |
|
363 | 363 | def profile(self): |
|
364 | 364 | if self.profile_dir is not None: |
|
365 | 365 | name = os.path.basename(self.profile_dir.location) |
|
366 | 366 | return name.replace('profile_','') |
|
367 | 367 | |
|
368 | 368 | |
|
369 | 369 | # Private interface |
|
370 | 370 | _post_execute = Instance(dict) |
|
371 | 371 | |
|
372 | 372 | def __init__(self, config=None, ipython_dir=None, profile_dir=None, |
|
373 | 373 | user_ns=None, user_global_ns=None, |
|
374 | 374 | custom_exceptions=((), None)): |
|
375 | 375 | |
|
376 | 376 | # This is where traits with a config_key argument are updated |
|
377 | 377 | # from the values on config. |
|
378 | 378 | super(InteractiveShell, self).__init__(config=config) |
|
379 | 379 | |
|
380 | 380 | # These are relatively independent and stateless |
|
381 | 381 | self.init_ipython_dir(ipython_dir) |
|
382 | 382 | self.init_profile_dir(profile_dir) |
|
383 | 383 | self.init_instance_attrs() |
|
384 | 384 | self.init_environment() |
|
385 | 385 | |
|
386 | 386 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
387 | 387 | self.init_create_namespaces(user_ns, user_global_ns) |
|
388 | 388 | # This has to be done after init_create_namespaces because it uses |
|
389 | 389 | # something in self.user_ns, but before init_sys_modules, which |
|
390 | 390 | # is the first thing to modify sys. |
|
391 | 391 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
392 | 392 | # is created, we are saving the overridden ones here. Not sure if this |
|
393 | 393 | # is what we want to do. |
|
394 | 394 | self.save_sys_module_state() |
|
395 | 395 | self.init_sys_modules() |
|
396 | ||
|
396 | ||
|
397 | 397 | # While we're trying to have each part of the code directly access what |
|
398 | 398 | # it needs without keeping redundant references to objects, we have too |
|
399 | 399 | # much legacy code that expects ip.db to exist. |
|
400 | 400 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) |
|
401 | 401 | |
|
402 | 402 | self.init_history() |
|
403 | 403 | self.init_encoding() |
|
404 | 404 | self.init_prefilter() |
|
405 | 405 | |
|
406 | 406 | Magic.__init__(self, self) |
|
407 | 407 | |
|
408 | 408 | self.init_syntax_highlighting() |
|
409 | 409 | self.init_hooks() |
|
410 | 410 | self.init_pushd_popd_magic() |
|
411 | 411 | # self.init_traceback_handlers use to be here, but we moved it below |
|
412 | 412 | # because it and init_io have to come after init_readline. |
|
413 | 413 | self.init_user_ns() |
|
414 | 414 | self.init_logger() |
|
415 | 415 | self.init_alias() |
|
416 | 416 | self.init_builtins() |
|
417 | 417 | |
|
418 | 418 | # pre_config_initialization |
|
419 | 419 | |
|
420 | 420 | # The next section should contain everything that was in ipmaker. |
|
421 | 421 | self.init_logstart() |
|
422 | 422 | |
|
423 | 423 | # The following was in post_config_initialization |
|
424 | 424 | self.init_inspector() |
|
425 | 425 | # init_readline() must come before init_io(), because init_io uses |
|
426 | 426 | # readline related things. |
|
427 | 427 | self.init_readline() |
|
428 | 428 | # We save this here in case user code replaces raw_input, but it needs |
|
429 | 429 | # to be after init_readline(), because PyPy's readline works by replacing |
|
430 | 430 | # raw_input. |
|
431 | 431 | if py3compat.PY3: |
|
432 | 432 | self.raw_input_original = input |
|
433 | 433 | else: |
|
434 | 434 | self.raw_input_original = raw_input |
|
435 | 435 | # init_completer must come after init_readline, because it needs to |
|
436 | 436 | # know whether readline is present or not system-wide to configure the |
|
437 | 437 | # completers, since the completion machinery can now operate |
|
438 | 438 | # independently of readline (e.g. over the network) |
|
439 | 439 | self.init_completer() |
|
440 | 440 | # TODO: init_io() needs to happen before init_traceback handlers |
|
441 | 441 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
442 | 442 | # This logic in in debugger.Pdb and should eventually be changed. |
|
443 | 443 | self.init_io() |
|
444 | 444 | self.init_traceback_handlers(custom_exceptions) |
|
445 | 445 | self.init_prompts() |
|
446 | 446 | self.init_display_formatter() |
|
447 | 447 | self.init_display_pub() |
|
448 | 448 | self.init_displayhook() |
|
449 | 449 | self.init_reload_doctest() |
|
450 | 450 | self.init_magics() |
|
451 | 451 | self.init_pdb() |
|
452 | 452 | self.init_extension_manager() |
|
453 | 453 | self.init_plugin_manager() |
|
454 | 454 | self.init_payload() |
|
455 | 455 | self.hooks.late_startup_hook() |
|
456 | 456 | atexit.register(self.atexit_operations) |
|
457 | 457 | |
|
458 | 458 | def get_ipython(self): |
|
459 | 459 | """Return the currently running IPython instance.""" |
|
460 | 460 | return self |
|
461 | 461 | |
|
462 | 462 | #------------------------------------------------------------------------- |
|
463 | 463 | # Trait changed handlers |
|
464 | 464 | #------------------------------------------------------------------------- |
|
465 | 465 | |
|
466 | 466 | def _ipython_dir_changed(self, name, new): |
|
467 | 467 | if not os.path.isdir(new): |
|
468 | 468 | os.makedirs(new, mode = 0777) |
|
469 | 469 | |
|
470 | 470 | def set_autoindent(self,value=None): |
|
471 | 471 | """Set the autoindent flag, checking for readline support. |
|
472 | 472 | |
|
473 | 473 | If called with no arguments, it acts as a toggle.""" |
|
474 | 474 | |
|
475 | 475 | if value != 0 and not self.has_readline: |
|
476 | 476 | if os.name == 'posix': |
|
477 | 477 | warn("The auto-indent feature requires the readline library") |
|
478 | 478 | self.autoindent = 0 |
|
479 | 479 | return |
|
480 | 480 | if value is None: |
|
481 | 481 | self.autoindent = not self.autoindent |
|
482 | 482 | else: |
|
483 | 483 | self.autoindent = value |
|
484 | 484 | |
|
485 | 485 | #------------------------------------------------------------------------- |
|
486 | 486 | # init_* methods called by __init__ |
|
487 | 487 | #------------------------------------------------------------------------- |
|
488 | 488 | |
|
489 | 489 | def init_ipython_dir(self, ipython_dir): |
|
490 | 490 | if ipython_dir is not None: |
|
491 | 491 | self.ipython_dir = ipython_dir |
|
492 | 492 | return |
|
493 | 493 | |
|
494 | 494 | self.ipython_dir = get_ipython_dir() |
|
495 | 495 | |
|
496 | 496 | def init_profile_dir(self, profile_dir): |
|
497 | 497 | if profile_dir is not None: |
|
498 | 498 | self.profile_dir = profile_dir |
|
499 | 499 | return |
|
500 | 500 | self.profile_dir =\ |
|
501 | 501 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') |
|
502 | 502 | |
|
503 | 503 | def init_instance_attrs(self): |
|
504 | 504 | self.more = False |
|
505 | 505 | |
|
506 | 506 | # command compiler |
|
507 | 507 | self.compile = CachingCompiler() |
|
508 | 508 | |
|
509 | 509 | # Make an empty namespace, which extension writers can rely on both |
|
510 | 510 | # existing and NEVER being used by ipython itself. This gives them a |
|
511 | 511 | # convenient location for storing additional information and state |
|
512 | 512 | # their extensions may require, without fear of collisions with other |
|
513 | 513 | # ipython names that may develop later. |
|
514 | 514 | self.meta = Struct() |
|
515 | 515 | |
|
516 | 516 | # Temporary files used for various purposes. Deleted at exit. |
|
517 | 517 | self.tempfiles = [] |
|
518 | 518 | |
|
519 | 519 | # Keep track of readline usage (later set by init_readline) |
|
520 | 520 | self.has_readline = False |
|
521 | 521 | |
|
522 | 522 | # keep track of where we started running (mainly for crash post-mortem) |
|
523 | 523 | # This is not being used anywhere currently. |
|
524 | 524 | self.starting_dir = os.getcwdu() |
|
525 | 525 | |
|
526 | 526 | # Indentation management |
|
527 | 527 | self.indent_current_nsp = 0 |
|
528 | 528 | |
|
529 | 529 | # Dict to track post-execution functions that have been registered |
|
530 | 530 | self._post_execute = {} |
|
531 | 531 | |
|
532 | 532 | def init_environment(self): |
|
533 | 533 | """Any changes we need to make to the user's environment.""" |
|
534 | 534 | pass |
|
535 | 535 | |
|
536 | 536 | def init_encoding(self): |
|
537 | 537 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
538 | 538 | # under Win32 have it set to None, and we need to have a known valid |
|
539 | 539 | # encoding to use in the raw_input() method |
|
540 | 540 | try: |
|
541 | 541 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
542 | 542 | except AttributeError: |
|
543 | 543 | self.stdin_encoding = 'ascii' |
|
544 | 544 | |
|
545 | 545 | def init_syntax_highlighting(self): |
|
546 | 546 | # Python source parser/formatter for syntax highlighting |
|
547 | 547 | pyformat = PyColorize.Parser().format |
|
548 | 548 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
549 | 549 | |
|
550 | 550 | def init_pushd_popd_magic(self): |
|
551 | 551 | # for pushd/popd management |
|
552 | 552 | try: |
|
553 | 553 | self.home_dir = get_home_dir() |
|
554 | 554 | except HomeDirError, msg: |
|
555 | 555 | fatal(msg) |
|
556 | 556 | |
|
557 | 557 | self.dir_stack = [] |
|
558 | 558 | |
|
559 | 559 | def init_logger(self): |
|
560 | 560 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', |
|
561 | 561 | logmode='rotate') |
|
562 | 562 | |
|
563 | 563 | def init_logstart(self): |
|
564 | 564 | """Initialize logging in case it was requested at the command line. |
|
565 | 565 | """ |
|
566 | 566 | if self.logappend: |
|
567 | 567 | self.magic_logstart(self.logappend + ' append') |
|
568 | 568 | elif self.logfile: |
|
569 | 569 | self.magic_logstart(self.logfile) |
|
570 | 570 | elif self.logstart: |
|
571 | 571 | self.magic_logstart() |
|
572 | 572 | |
|
573 | 573 | def init_builtins(self): |
|
574 | 574 | self.builtin_trap = BuiltinTrap(shell=self) |
|
575 | 575 | |
|
576 | 576 | def init_inspector(self): |
|
577 | 577 | # Object inspector |
|
578 | 578 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
579 | 579 | PyColorize.ANSICodeColors, |
|
580 | 580 | 'NoColor', |
|
581 | 581 | self.object_info_string_level) |
|
582 | 582 | |
|
583 | 583 | def init_io(self): |
|
584 | 584 | # This will just use sys.stdout and sys.stderr. If you want to |
|
585 | 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 | 587 | # references to the underlying streams. |
|
588 | 588 | if sys.platform == 'win32' and self.has_readline: |
|
589 | 589 | io.stdout = io.stderr = io.IOStream(self.readline._outputfile) |
|
590 | 590 | else: |
|
591 | 591 | io.stdout = io.IOStream(sys.stdout) |
|
592 | 592 | io.stderr = io.IOStream(sys.stderr) |
|
593 | 593 | |
|
594 | 594 | def init_prompts(self): |
|
595 | 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 | 597 | # will initialize that object and all prompt related information. |
|
598 | 598 | pass |
|
599 | 599 | |
|
600 | 600 | def init_display_formatter(self): |
|
601 | 601 | self.display_formatter = DisplayFormatter(config=self.config) |
|
602 | 602 | |
|
603 | 603 | def init_display_pub(self): |
|
604 | 604 | self.display_pub = self.display_pub_class(config=self.config) |
|
605 | 605 | |
|
606 | 606 | def init_displayhook(self): |
|
607 | 607 | # Initialize displayhook, set in/out prompts and printing system |
|
608 | 608 | self.displayhook = self.displayhook_class( |
|
609 | 609 | config=self.config, |
|
610 | 610 | shell=self, |
|
611 | 611 | cache_size=self.cache_size, |
|
612 | 612 | input_sep = self.separate_in, |
|
613 | 613 | output_sep = self.separate_out, |
|
614 | 614 | output_sep2 = self.separate_out2, |
|
615 | 615 | ps1 = self.prompt_in1, |
|
616 | 616 | ps2 = self.prompt_in2, |
|
617 | 617 | ps_out = self.prompt_out, |
|
618 | 618 | pad_left = self.prompts_pad_left |
|
619 | 619 | ) |
|
620 | 620 | # This is a context manager that installs/revmoes the displayhook at |
|
621 | 621 | # the appropriate time. |
|
622 | 622 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
623 | 623 | |
|
624 | 624 | def init_reload_doctest(self): |
|
625 | 625 | # Do a proper resetting of doctest, including the necessary displayhook |
|
626 | 626 | # monkeypatching |
|
627 | 627 | try: |
|
628 | 628 | doctest_reload() |
|
629 | 629 | except ImportError: |
|
630 | 630 | warn("doctest module does not exist.") |
|
631 | 631 | |
|
632 | 632 | #------------------------------------------------------------------------- |
|
633 | 633 | # Things related to injections into the sys module |
|
634 | 634 | #------------------------------------------------------------------------- |
|
635 | 635 | |
|
636 | 636 | def save_sys_module_state(self): |
|
637 | 637 | """Save the state of hooks in the sys module. |
|
638 | 638 | |
|
639 | 639 | This has to be called after self.user_ns is created. |
|
640 | 640 | """ |
|
641 | 641 | self._orig_sys_module_state = {} |
|
642 | 642 | self._orig_sys_module_state['stdin'] = sys.stdin |
|
643 | 643 | self._orig_sys_module_state['stdout'] = sys.stdout |
|
644 | 644 | self._orig_sys_module_state['stderr'] = sys.stderr |
|
645 | 645 | self._orig_sys_module_state['excepthook'] = sys.excepthook |
|
646 | 646 | try: |
|
647 | 647 | self._orig_sys_modules_main_name = self.user_ns['__name__'] |
|
648 | 648 | except KeyError: |
|
649 | 649 | pass |
|
650 | 650 | |
|
651 | 651 | def restore_sys_module_state(self): |
|
652 | 652 | """Restore the state of the sys module.""" |
|
653 | 653 | try: |
|
654 | 654 | for k, v in self._orig_sys_module_state.iteritems(): |
|
655 | 655 | setattr(sys, k, v) |
|
656 | 656 | except AttributeError: |
|
657 | 657 | pass |
|
658 | 658 | # Reset what what done in self.init_sys_modules |
|
659 | 659 | try: |
|
660 | 660 | sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name |
|
661 | 661 | except (AttributeError, KeyError): |
|
662 | 662 | pass |
|
663 | 663 | |
|
664 | 664 | #------------------------------------------------------------------------- |
|
665 | 665 | # Things related to hooks |
|
666 | 666 | #------------------------------------------------------------------------- |
|
667 | 667 | |
|
668 | 668 | def init_hooks(self): |
|
669 | 669 | # hooks holds pointers used for user-side customizations |
|
670 | 670 | self.hooks = Struct() |
|
671 | 671 | |
|
672 | 672 | self.strdispatchers = {} |
|
673 | 673 | |
|
674 | 674 | # Set all default hooks, defined in the IPython.hooks module. |
|
675 | 675 | hooks = IPython.core.hooks |
|
676 | 676 | for hook_name in hooks.__all__: |
|
677 | 677 | # default hooks have priority 100, i.e. low; user hooks should have |
|
678 | 678 | # 0-100 priority |
|
679 | 679 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
680 | 680 | |
|
681 | 681 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
682 | 682 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
683 | 683 | |
|
684 | 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 | 686 | behavior to call at runtime your own routines.""" |
|
687 | 687 | |
|
688 | 688 | # At some point in the future, this should validate the hook before it |
|
689 | 689 | # accepts it. Probably at least check that the hook takes the number |
|
690 | 690 | # of args it's supposed to. |
|
691 | ||
|
691 | ||
|
692 | 692 | f = types.MethodType(hook,self) |
|
693 | 693 | |
|
694 | 694 | # check if the hook is for strdispatcher first |
|
695 | 695 | if str_key is not None: |
|
696 | 696 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
697 | 697 | sdp.add_s(str_key, f, priority ) |
|
698 | 698 | self.strdispatchers[name] = sdp |
|
699 | 699 | return |
|
700 | 700 | if re_key is not None: |
|
701 | 701 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
702 | 702 | sdp.add_re(re.compile(re_key), f, priority ) |
|
703 | 703 | self.strdispatchers[name] = sdp |
|
704 | 704 | return |
|
705 | ||
|
705 | ||
|
706 | 706 | dp = getattr(self.hooks, name, None) |
|
707 | 707 | if name not in IPython.core.hooks.__all__: |
|
708 | 708 | print "Warning! Hook '%s' is not one of %s" % \ |
|
709 | 709 | (name, IPython.core.hooks.__all__ ) |
|
710 | 710 | if not dp: |
|
711 | 711 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
712 | ||
|
712 | ||
|
713 | 713 | try: |
|
714 | 714 | dp.add(f,priority) |
|
715 | 715 | except AttributeError: |
|
716 | 716 | # it was not commandchain, plain old func - replace |
|
717 | 717 | dp = f |
|
718 | 718 | |
|
719 | 719 | setattr(self.hooks,name, dp) |
|
720 | 720 | |
|
721 | 721 | def register_post_execute(self, func): |
|
722 | 722 | """Register a function for calling after code execution. |
|
723 | 723 | """ |
|
724 | 724 | if not callable(func): |
|
725 | 725 | raise ValueError('argument %s must be callable' % func) |
|
726 | 726 | self._post_execute[func] = True |
|
727 | 727 | |
|
728 | 728 | #------------------------------------------------------------------------- |
|
729 | 729 | # Things related to the "main" module |
|
730 | 730 | #------------------------------------------------------------------------- |
|
731 | 731 | |
|
732 | 732 | def new_main_mod(self,ns=None): |
|
733 | 733 | """Return a new 'main' module object for user code execution. |
|
734 | 734 | """ |
|
735 | 735 | main_mod = self._user_main_module |
|
736 | 736 | init_fakemod_dict(main_mod,ns) |
|
737 | 737 | return main_mod |
|
738 | 738 | |
|
739 | 739 | def cache_main_mod(self,ns,fname): |
|
740 | 740 | """Cache a main module's namespace. |
|
741 | 741 | |
|
742 | 742 | When scripts are executed via %run, we must keep a reference to the |
|
743 | 743 | namespace of their __main__ module (a FakeModule instance) around so |
|
744 | 744 | that Python doesn't clear it, rendering objects defined therein |
|
745 | 745 | useless. |
|
746 | 746 | |
|
747 | 747 | This method keeps said reference in a private dict, keyed by the |
|
748 | 748 | absolute path of the module object (which corresponds to the script |
|
749 | 749 | path). This way, for multiple executions of the same script we only |
|
750 | 750 | keep one copy of the namespace (the last one), thus preventing memory |
|
751 | 751 | leaks from old references while allowing the objects from the last |
|
752 | 752 | execution to be accessible. |
|
753 | 753 | |
|
754 | 754 | Note: we can not allow the actual FakeModule instances to be deleted, |
|
755 | 755 | because of how Python tears down modules (it hard-sets all their |
|
756 | 756 | references to None without regard for reference counts). This method |
|
757 | 757 | must therefore make a *copy* of the given namespace, to allow the |
|
758 | 758 | original module's __dict__ to be cleared and reused. |
|
759 | 759 | |
|
760 | ||
|
760 | ||
|
761 | 761 | Parameters |
|
762 | 762 | ---------- |
|
763 | 763 | ns : a namespace (a dict, typically) |
|
764 | 764 | |
|
765 | 765 | fname : str |
|
766 | 766 | Filename associated with the namespace. |
|
767 | 767 | |
|
768 | 768 | Examples |
|
769 | 769 | -------- |
|
770 | 770 | |
|
771 | 771 | In [10]: import IPython |
|
772 | 772 | |
|
773 | 773 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
774 | 774 | |
|
775 | 775 | In [12]: IPython.__file__ in _ip._main_ns_cache |
|
776 | 776 | Out[12]: True |
|
777 | 777 | """ |
|
778 | 778 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() |
|
779 | 779 | |
|
780 | 780 | def clear_main_mod_cache(self): |
|
781 | 781 | """Clear the cache of main modules. |
|
782 | 782 | |
|
783 | 783 | Mainly for use by utilities like %reset. |
|
784 | 784 | |
|
785 | 785 | Examples |
|
786 | 786 | -------- |
|
787 | 787 | |
|
788 | 788 | In [15]: import IPython |
|
789 | 789 | |
|
790 | 790 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
791 | 791 | |
|
792 | 792 | In [17]: len(_ip._main_ns_cache) > 0 |
|
793 | 793 | Out[17]: True |
|
794 | 794 | |
|
795 | 795 | In [18]: _ip.clear_main_mod_cache() |
|
796 | 796 | |
|
797 | 797 | In [19]: len(_ip._main_ns_cache) == 0 |
|
798 | 798 | Out[19]: True |
|
799 | 799 | """ |
|
800 | 800 | self._main_ns_cache.clear() |
|
801 | 801 | |
|
802 | 802 | #------------------------------------------------------------------------- |
|
803 | 803 | # Things related to debugging |
|
804 | 804 | #------------------------------------------------------------------------- |
|
805 | 805 | |
|
806 | 806 | def init_pdb(self): |
|
807 | 807 | # Set calling of pdb on exceptions |
|
808 | 808 | # self.call_pdb is a property |
|
809 | 809 | self.call_pdb = self.pdb |
|
810 | 810 | |
|
811 | 811 | def _get_call_pdb(self): |
|
812 | 812 | return self._call_pdb |
|
813 | 813 | |
|
814 | 814 | def _set_call_pdb(self,val): |
|
815 | 815 | |
|
816 | 816 | if val not in (0,1,False,True): |
|
817 | 817 | raise ValueError,'new call_pdb value must be boolean' |
|
818 | 818 | |
|
819 | 819 | # store value in instance |
|
820 | 820 | self._call_pdb = val |
|
821 | 821 | |
|
822 | 822 | # notify the actual exception handlers |
|
823 | 823 | self.InteractiveTB.call_pdb = val |
|
824 | 824 | |
|
825 | 825 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
826 | 826 | 'Control auto-activation of pdb at exceptions') |
|
827 | 827 | |
|
828 | 828 | def debugger(self,force=False): |
|
829 | 829 | """Call the pydb/pdb debugger. |
|
830 | 830 | |
|
831 | 831 | Keywords: |
|
832 | 832 | |
|
833 | 833 | - force(False): by default, this routine checks the instance call_pdb |
|
834 | 834 | flag and does not actually invoke the debugger if the flag is false. |
|
835 | 835 | The 'force' option forces the debugger to activate even if the flag |
|
836 | 836 | is false. |
|
837 | 837 | """ |
|
838 | 838 | |
|
839 | 839 | if not (force or self.call_pdb): |
|
840 | 840 | return |
|
841 | 841 | |
|
842 | 842 | if not hasattr(sys,'last_traceback'): |
|
843 | 843 | error('No traceback has been produced, nothing to debug.') |
|
844 | 844 | return |
|
845 | 845 | |
|
846 | 846 | # use pydb if available |
|
847 | 847 | if debugger.has_pydb: |
|
848 | 848 | from pydb import pm |
|
849 | 849 | else: |
|
850 | 850 | # fallback to our internal debugger |
|
851 | 851 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
852 | ||
|
852 | ||
|
853 | 853 | with self.readline_no_record: |
|
854 | 854 | pm() |
|
855 | 855 | |
|
856 | 856 | #------------------------------------------------------------------------- |
|
857 | 857 | # Things related to IPython's various namespaces |
|
858 | 858 | #------------------------------------------------------------------------- |
|
859 | 859 | |
|
860 | 860 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): |
|
861 | 861 | # Create the namespace where the user will operate. user_ns is |
|
862 | 862 | # normally the only one used, and it is passed to the exec calls as |
|
863 | 863 | # the locals argument. But we do carry a user_global_ns namespace |
|
864 | 864 | # given as the exec 'globals' argument, This is useful in embedding |
|
865 | 865 | # situations where the ipython shell opens in a context where the |
|
866 | 866 | # distinction between locals and globals is meaningful. For |
|
867 | 867 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
868 | 868 | |
|
869 | 869 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
870 | 870 | # level as a dict instead of a module. This is a manual fix, but I |
|
871 | 871 | # should really track down where the problem is coming from. Alex |
|
872 | 872 | # Schmolck reported this problem first. |
|
873 | 873 | |
|
874 | 874 | # A useful post by Alex Martelli on this topic: |
|
875 | 875 | # Re: inconsistent value from __builtins__ |
|
876 | 876 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
877 | 877 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
878 | 878 | # Gruppen: comp.lang.python |
|
879 | 879 | |
|
880 | 880 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
881 | 881 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
882 | 882 | # > <type 'dict'> |
|
883 | 883 | # > >>> print type(__builtins__) |
|
884 | 884 | # > <type 'module'> |
|
885 | 885 | # > Is this difference in return value intentional? |
|
886 | 886 | |
|
887 | 887 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
888 | 888 | # or a module, and it's been that way for a long time. Whether it's |
|
889 | 889 | # intentional (or sensible), I don't know. In any case, the idea is |
|
890 | 890 | # that if you need to access the built-in namespace directly, you |
|
891 | 891 | # should start with "import __builtin__" (note, no 's') which will |
|
892 | 892 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
893 | 893 | |
|
894 | 894 | # These routines return properly built dicts as needed by the rest of |
|
895 | 895 | # the code, and can also be used by extension writers to generate |
|
896 | 896 | # properly initialized namespaces. |
|
897 | 897 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, |
|
898 | 898 | user_global_ns) |
|
899 | 899 | |
|
900 | 900 | # Assign namespaces |
|
901 | 901 | # This is the namespace where all normal user variables live |
|
902 | 902 | self.user_ns = user_ns |
|
903 | 903 | self.user_global_ns = user_global_ns |
|
904 | 904 | |
|
905 | 905 | # An auxiliary namespace that checks what parts of the user_ns were |
|
906 | 906 | # loaded at startup, so we can list later only variables defined in |
|
907 | 907 | # actual interactive use. Since it is always a subset of user_ns, it |
|
908 | 908 | # doesn't need to be separately tracked in the ns_table. |
|
909 | 909 | self.user_ns_hidden = {} |
|
910 | 910 | |
|
911 | 911 | # A namespace to keep track of internal data structures to prevent |
|
912 | 912 | # them from cluttering user-visible stuff. Will be updated later |
|
913 | 913 | self.internal_ns = {} |
|
914 | 914 | |
|
915 | 915 | # Now that FakeModule produces a real module, we've run into a nasty |
|
916 | 916 | # problem: after script execution (via %run), the module where the user |
|
917 | 917 | # code ran is deleted. Now that this object is a true module (needed |
|
918 | 918 | # so docetst and other tools work correctly), the Python module |
|
919 | 919 | # teardown mechanism runs over it, and sets to None every variable |
|
920 | 920 | # present in that module. Top-level references to objects from the |
|
921 | 921 | # script survive, because the user_ns is updated with them. However, |
|
922 | 922 | # calling functions defined in the script that use other things from |
|
923 | 923 | # the script will fail, because the function's closure had references |
|
924 | 924 | # to the original objects, which are now all None. So we must protect |
|
925 | 925 | # these modules from deletion by keeping a cache. |
|
926 |
# |
|
|
926 | # | |
|
927 | 927 | # To avoid keeping stale modules around (we only need the one from the |
|
928 | 928 | # last run), we use a dict keyed with the full path to the script, so |
|
929 | 929 | # only the last version of the module is held in the cache. Note, |
|
930 | 930 | # however, that we must cache the module *namespace contents* (their |
|
931 | 931 | # __dict__). Because if we try to cache the actual modules, old ones |
|
932 | 932 | # (uncached) could be destroyed while still holding references (such as |
|
933 | 933 | # those held by GUI objects that tend to be long-lived)> |
|
934 |
# |
|
|
934 | # | |
|
935 | 935 | # The %reset command will flush this cache. See the cache_main_mod() |
|
936 | 936 | # and clear_main_mod_cache() methods for details on use. |
|
937 | 937 | |
|
938 | 938 | # This is the cache used for 'main' namespaces |
|
939 | 939 | self._main_ns_cache = {} |
|
940 | 940 | # And this is the single instance of FakeModule whose __dict__ we keep |
|
941 | 941 | # copying and clearing for reuse on each %run |
|
942 | 942 | self._user_main_module = FakeModule() |
|
943 | 943 | |
|
944 | 944 | # A table holding all the namespaces IPython deals with, so that |
|
945 | 945 | # introspection facilities can search easily. |
|
946 | 946 | self.ns_table = {'user':user_ns, |
|
947 | 947 | 'user_global':user_global_ns, |
|
948 | 948 | 'internal':self.internal_ns, |
|
949 | 949 | 'builtin':builtin_mod.__dict__ |
|
950 | 950 | } |
|
951 | 951 | |
|
952 | 952 | # Similarly, track all namespaces where references can be held and that |
|
953 | 953 | # we can safely clear (so it can NOT include builtin). This one can be |
|
954 | 954 | # a simple list. Note that the main execution namespaces, user_ns and |
|
955 | 955 | # user_global_ns, can NOT be listed here, as clearing them blindly |
|
956 | 956 | # causes errors in object __del__ methods. Instead, the reset() method |
|
957 | 957 | # clears them manually and carefully. |
|
958 | 958 | self.ns_refs_table = [ self.user_ns_hidden, |
|
959 | 959 | self.internal_ns, self._main_ns_cache ] |
|
960 | 960 | |
|
961 | 961 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): |
|
962 | 962 | """Return a valid local and global user interactive namespaces. |
|
963 | 963 | |
|
964 | 964 | This builds a dict with the minimal information needed to operate as a |
|
965 | 965 | valid IPython user namespace, which you can pass to the various |
|
966 | 966 | embedding classes in ipython. The default implementation returns the |
|
967 | 967 | same dict for both the locals and the globals to allow functions to |
|
968 | 968 | refer to variables in the namespace. Customized implementations can |
|
969 | 969 | return different dicts. The locals dictionary can actually be anything |
|
970 | 970 | following the basic mapping protocol of a dict, but the globals dict |
|
971 | 971 | must be a true dict, not even a subclass. It is recommended that any |
|
972 | 972 | custom object for the locals namespace synchronize with the globals |
|
973 | 973 | dict somehow. |
|
974 | 974 | |
|
975 | 975 | Raises TypeError if the provided globals namespace is not a true dict. |
|
976 | 976 | |
|
977 | 977 | Parameters |
|
978 | 978 | ---------- |
|
979 | 979 | user_ns : dict-like, optional |
|
980 | 980 | The current user namespace. The items in this namespace should |
|
981 | 981 | be included in the output. If None, an appropriate blank |
|
982 | 982 | namespace should be created. |
|
983 | 983 | user_global_ns : dict, optional |
|
984 | 984 | The current user global namespace. The items in this namespace |
|
985 | 985 | should be included in the output. If None, an appropriate |
|
986 | 986 | blank namespace should be created. |
|
987 | 987 | |
|
988 | 988 | Returns |
|
989 | 989 | ------- |
|
990 | 990 | A pair of dictionary-like object to be used as the local namespace |
|
991 | 991 | of the interpreter and a dict to be used as the global namespace. |
|
992 | 992 | """ |
|
993 | 993 | |
|
994 | 994 | |
|
995 | 995 | # We must ensure that __builtin__ (without the final 's') is always |
|
996 | 996 | # available and pointing to the __builtin__ *module*. For more details: |
|
997 | 997 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
998 | 998 | |
|
999 | 999 | if user_ns is None: |
|
1000 | 1000 | # Set __name__ to __main__ to better match the behavior of the |
|
1001 | 1001 | # normal interpreter. |
|
1002 | 1002 | user_ns = {'__name__' :'__main__', |
|
1003 | 1003 | py3compat.builtin_mod_name: builtin_mod, |
|
1004 | 1004 | '__builtins__' : builtin_mod, |
|
1005 | 1005 | } |
|
1006 | 1006 | else: |
|
1007 | 1007 | user_ns.setdefault('__name__','__main__') |
|
1008 | 1008 | user_ns.setdefault(py3compat.builtin_mod_name,builtin_mod) |
|
1009 | 1009 | user_ns.setdefault('__builtins__',builtin_mod) |
|
1010 | 1010 | |
|
1011 | 1011 | if user_global_ns is None: |
|
1012 | 1012 | user_global_ns = user_ns |
|
1013 | 1013 | if type(user_global_ns) is not dict: |
|
1014 | 1014 | raise TypeError("user_global_ns must be a true dict; got %r" |
|
1015 | 1015 | % type(user_global_ns)) |
|
1016 | 1016 | |
|
1017 | 1017 | return user_ns, user_global_ns |
|
1018 | 1018 | |
|
1019 | 1019 | def init_sys_modules(self): |
|
1020 | 1020 | # We need to insert into sys.modules something that looks like a |
|
1021 | 1021 | # module but which accesses the IPython namespace, for shelve and |
|
1022 | 1022 | # pickle to work interactively. Normally they rely on getting |
|
1023 | 1023 | # everything out of __main__, but for embedding purposes each IPython |
|
1024 | 1024 | # instance has its own private namespace, so we can't go shoving |
|
1025 | 1025 | # everything into __main__. |
|
1026 | 1026 | |
|
1027 | 1027 | # note, however, that we should only do this for non-embedded |
|
1028 | 1028 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
1029 | 1029 | # namespace. Embedded instances, on the other hand, should not do |
|
1030 | 1030 | # this because they need to manage the user local/global namespaces |
|
1031 | 1031 | # only, but they live within a 'normal' __main__ (meaning, they |
|
1032 | 1032 | # shouldn't overtake the execution environment of the script they're |
|
1033 | 1033 | # embedded in). |
|
1034 | 1034 | |
|
1035 | 1035 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
1036 | 1036 | |
|
1037 | 1037 | try: |
|
1038 | 1038 | main_name = self.user_ns['__name__'] |
|
1039 | 1039 | except KeyError: |
|
1040 | 1040 | raise KeyError('user_ns dictionary MUST have a "__name__" key') |
|
1041 | 1041 | else: |
|
1042 | 1042 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
1043 | 1043 | |
|
1044 | 1044 | def init_user_ns(self): |
|
1045 | 1045 | """Initialize all user-visible namespaces to their minimum defaults. |
|
1046 | 1046 | |
|
1047 | 1047 | Certain history lists are also initialized here, as they effectively |
|
1048 | 1048 | act as user namespaces. |
|
1049 | 1049 | |
|
1050 | 1050 | Notes |
|
1051 | 1051 | ----- |
|
1052 | 1052 | All data structures here are only filled in, they are NOT reset by this |
|
1053 | 1053 | method. If they were not empty before, data will simply be added to |
|
1054 | 1054 | therm. |
|
1055 | 1055 | """ |
|
1056 | 1056 | # This function works in two parts: first we put a few things in |
|
1057 | 1057 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
1058 | 1058 | # initial variables aren't shown by %who. After the sync, we add the |
|
1059 | 1059 | # rest of what we *do* want the user to see with %who even on a new |
|
1060 | 1060 | # session (probably nothing, so theye really only see their own stuff) |
|
1061 | 1061 | |
|
1062 | 1062 | # The user dict must *always* have a __builtin__ reference to the |
|
1063 | 1063 | # Python standard __builtin__ namespace, which must be imported. |
|
1064 | 1064 | # This is so that certain operations in prompt evaluation can be |
|
1065 | 1065 | # reliably executed with builtins. Note that we can NOT use |
|
1066 | 1066 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
1067 | 1067 | # module, and can even mutate at runtime, depending on the context |
|
1068 | 1068 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
1069 | 1069 | # always a module object, though it must be explicitly imported. |
|
1070 | ||
|
1070 | ||
|
1071 | 1071 | # For more details: |
|
1072 | 1072 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1073 | 1073 | ns = dict(__builtin__ = builtin_mod) |
|
1074 | ||
|
1074 | ||
|
1075 | 1075 | # Put 'help' in the user namespace |
|
1076 | 1076 | try: |
|
1077 | 1077 | from site import _Helper |
|
1078 | 1078 | ns['help'] = _Helper() |
|
1079 | 1079 | except ImportError: |
|
1080 | 1080 | warn('help() not available - check site.py') |
|
1081 | 1081 | |
|
1082 | 1082 | # make global variables for user access to the histories |
|
1083 | 1083 | ns['_ih'] = self.history_manager.input_hist_parsed |
|
1084 | 1084 | ns['_oh'] = self.history_manager.output_hist |
|
1085 | 1085 | ns['_dh'] = self.history_manager.dir_hist |
|
1086 | 1086 | |
|
1087 | 1087 | ns['_sh'] = shadowns |
|
1088 | 1088 | |
|
1089 | 1089 | # user aliases to input and output histories. These shouldn't show up |
|
1090 | 1090 | # in %who, as they can have very large reprs. |
|
1091 | 1091 | ns['In'] = self.history_manager.input_hist_parsed |
|
1092 | 1092 | ns['Out'] = self.history_manager.output_hist |
|
1093 | 1093 | |
|
1094 | 1094 | # Store myself as the public api!!! |
|
1095 | 1095 | ns['get_ipython'] = self.get_ipython |
|
1096 | ||
|
1096 | ||
|
1097 | 1097 | ns['exit'] = self.exiter |
|
1098 | 1098 | ns['quit'] = self.exiter |
|
1099 | 1099 | |
|
1100 | 1100 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
1101 | 1101 | # by %who |
|
1102 | 1102 | self.user_ns_hidden.update(ns) |
|
1103 | 1103 | |
|
1104 | 1104 | # Anything put into ns now would show up in %who. Think twice before |
|
1105 | 1105 | # putting anything here, as we really want %who to show the user their |
|
1106 | 1106 | # stuff, not our variables. |
|
1107 | ||
|
1107 | ||
|
1108 | 1108 | # Finally, update the real user's namespace |
|
1109 | 1109 | self.user_ns.update(ns) |
|
1110 | 1110 | |
|
1111 | 1111 | def reset(self, new_session=True): |
|
1112 | 1112 | """Clear all internal namespaces, and attempt to release references to |
|
1113 | 1113 | user objects. |
|
1114 | ||
|
1114 | ||
|
1115 | 1115 | If new_session is True, a new history session will be opened. |
|
1116 | 1116 | """ |
|
1117 | 1117 | # Clear histories |
|
1118 | 1118 | self.history_manager.reset(new_session) |
|
1119 | 1119 | # Reset counter used to index all histories |
|
1120 | 1120 | if new_session: |
|
1121 | 1121 | self.execution_count = 1 |
|
1122 | ||
|
1122 | ||
|
1123 | 1123 | # Flush cached output items |
|
1124 | 1124 | if self.displayhook.do_full_cache: |
|
1125 | 1125 | self.displayhook.flush() |
|
1126 | ||
|
1126 | ||
|
1127 | 1127 | # Restore the user namespaces to minimal usability |
|
1128 | 1128 | for ns in self.ns_refs_table: |
|
1129 | 1129 | ns.clear() |
|
1130 | 1130 | |
|
1131 | 1131 | # The main execution namespaces must be cleared very carefully, |
|
1132 | 1132 | # skipping the deletion of the builtin-related keys, because doing so |
|
1133 | 1133 | # would cause errors in many object's __del__ methods. |
|
1134 | 1134 | for ns in [self.user_ns, self.user_global_ns]: |
|
1135 | 1135 | drop_keys = set(ns.keys()) |
|
1136 | 1136 | drop_keys.discard('__builtin__') |
|
1137 | 1137 | drop_keys.discard('__builtins__') |
|
1138 | 1138 | for k in drop_keys: |
|
1139 | 1139 | del ns[k] |
|
1140 | ||
|
1140 | ||
|
1141 | 1141 | # Restore the user namespaces to minimal usability |
|
1142 | 1142 | self.init_user_ns() |
|
1143 | 1143 | |
|
1144 | 1144 | # Restore the default and user aliases |
|
1145 | 1145 | self.alias_manager.clear_aliases() |
|
1146 | 1146 | self.alias_manager.init_aliases() |
|
1147 | ||
|
1147 | ||
|
1148 | 1148 | # Flush the private list of module references kept for script |
|
1149 | 1149 | # execution protection |
|
1150 | 1150 | self.clear_main_mod_cache() |
|
1151 | ||
|
1151 | ||
|
1152 | 1152 | # Clear out the namespace from the last %run |
|
1153 | 1153 | self.new_main_mod() |
|
1154 | ||
|
1154 | ||
|
1155 | 1155 | def del_var(self, varname, by_name=False): |
|
1156 | 1156 | """Delete a variable from the various namespaces, so that, as |
|
1157 | 1157 | far as possible, we're not keeping any hidden references to it. |
|
1158 | ||
|
1158 | ||
|
1159 | 1159 | Parameters |
|
1160 | 1160 | ---------- |
|
1161 | 1161 | varname : str |
|
1162 | 1162 | The name of the variable to delete. |
|
1163 | 1163 | by_name : bool |
|
1164 | 1164 | If True, delete variables with the given name in each |
|
1165 | 1165 | namespace. If False (default), find the variable in the user |
|
1166 | 1166 | namespace, and delete references to it. |
|
1167 | 1167 | """ |
|
1168 | 1168 | if varname in ('__builtin__', '__builtins__'): |
|
1169 | 1169 | raise ValueError("Refusing to delete %s" % varname) |
|
1170 | 1170 | ns_refs = self.ns_refs_table + [self.user_ns, |
|
1171 | 1171 | self.user_global_ns, self._user_main_module.__dict__] +\ |
|
1172 | 1172 | self._main_ns_cache.values() |
|
1173 | ||
|
1173 | ||
|
1174 | 1174 | if by_name: # Delete by name |
|
1175 | 1175 | for ns in ns_refs: |
|
1176 | 1176 | try: |
|
1177 | 1177 | del ns[varname] |
|
1178 | 1178 | except KeyError: |
|
1179 | 1179 | pass |
|
1180 | 1180 | else: # Delete by object |
|
1181 | 1181 | try: |
|
1182 | 1182 | obj = self.user_ns[varname] |
|
1183 | 1183 | except KeyError: |
|
1184 | 1184 | raise NameError("name '%s' is not defined" % varname) |
|
1185 | 1185 | # Also check in output history |
|
1186 | 1186 | ns_refs.append(self.history_manager.output_hist) |
|
1187 | 1187 | for ns in ns_refs: |
|
1188 | 1188 | to_delete = [n for n, o in ns.iteritems() if o is obj] |
|
1189 | 1189 | for name in to_delete: |
|
1190 | 1190 | del ns[name] |
|
1191 | ||
|
1191 | ||
|
1192 | 1192 | # displayhook keeps extra references, but not in a dictionary |
|
1193 | 1193 | for name in ('_', '__', '___'): |
|
1194 | 1194 | if getattr(self.displayhook, name) is obj: |
|
1195 | 1195 | setattr(self.displayhook, name, None) |
|
1196 | ||
|
1196 | ||
|
1197 | 1197 | def reset_selective(self, regex=None): |
|
1198 | 1198 | """Clear selective variables from internal namespaces based on a |
|
1199 | 1199 | specified regular expression. |
|
1200 | 1200 | |
|
1201 | 1201 | Parameters |
|
1202 | 1202 | ---------- |
|
1203 | 1203 | regex : string or compiled pattern, optional |
|
1204 | 1204 | A regular expression pattern that will be used in searching |
|
1205 | 1205 | variable names in the users namespaces. |
|
1206 | 1206 | """ |
|
1207 | 1207 | if regex is not None: |
|
1208 | 1208 | try: |
|
1209 | 1209 | m = re.compile(regex) |
|
1210 | 1210 | except TypeError: |
|
1211 | 1211 | raise TypeError('regex must be a string or compiled pattern') |
|
1212 | 1212 | # Search for keys in each namespace that match the given regex |
|
1213 | 1213 | # If a match is found, delete the key/value pair. |
|
1214 | 1214 | for ns in self.ns_refs_table: |
|
1215 | 1215 | for var in ns: |
|
1216 | 1216 | if m.search(var): |
|
1217 |
del ns[var] |
|
|
1218 | ||
|
1217 | del ns[var] | |
|
1218 | ||
|
1219 | 1219 | def push(self, variables, interactive=True): |
|
1220 | 1220 | """Inject a group of variables into the IPython user namespace. |
|
1221 | 1221 | |
|
1222 | 1222 | Parameters |
|
1223 | 1223 | ---------- |
|
1224 | 1224 | variables : dict, str or list/tuple of str |
|
1225 | 1225 | The variables to inject into the user's namespace. If a dict, a |
|
1226 | 1226 | simple update is done. If a str, the string is assumed to have |
|
1227 | 1227 | variable names separated by spaces. A list/tuple of str can also |
|
1228 | 1228 | be used to give the variable names. If just the variable names are |
|
1229 | 1229 | give (list/tuple/str) then the variable values looked up in the |
|
1230 | 1230 | callers frame. |
|
1231 | 1231 | interactive : bool |
|
1232 | 1232 | If True (default), the variables will be listed with the ``who`` |
|
1233 | 1233 | magic. |
|
1234 | 1234 | """ |
|
1235 | 1235 | vdict = None |
|
1236 | 1236 | |
|
1237 | 1237 | # We need a dict of name/value pairs to do namespace updates. |
|
1238 | 1238 | if isinstance(variables, dict): |
|
1239 | 1239 | vdict = variables |
|
1240 | 1240 | elif isinstance(variables, (basestring, list, tuple)): |
|
1241 | 1241 | if isinstance(variables, basestring): |
|
1242 | 1242 | vlist = variables.split() |
|
1243 | 1243 | else: |
|
1244 | 1244 | vlist = variables |
|
1245 | 1245 | vdict = {} |
|
1246 | 1246 | cf = sys._getframe(1) |
|
1247 | 1247 | for name in vlist: |
|
1248 | 1248 | try: |
|
1249 | 1249 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1250 | 1250 | except: |
|
1251 | 1251 | print ('Could not get variable %s from %s' % |
|
1252 | 1252 | (name,cf.f_code.co_name)) |
|
1253 | 1253 | else: |
|
1254 | 1254 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1255 | ||
|
1255 | ||
|
1256 | 1256 | # Propagate variables to user namespace |
|
1257 | 1257 | self.user_ns.update(vdict) |
|
1258 | 1258 | |
|
1259 | 1259 | # And configure interactive visibility |
|
1260 | 1260 | config_ns = self.user_ns_hidden |
|
1261 | 1261 | if interactive: |
|
1262 | 1262 | for name, val in vdict.iteritems(): |
|
1263 | 1263 | config_ns.pop(name, None) |
|
1264 | 1264 | else: |
|
1265 | 1265 | for name,val in vdict.iteritems(): |
|
1266 | 1266 | config_ns[name] = val |
|
1267 | 1267 | |
|
1268 | 1268 | #------------------------------------------------------------------------- |
|
1269 | 1269 | # Things related to object introspection |
|
1270 | 1270 | #------------------------------------------------------------------------- |
|
1271 | 1271 | |
|
1272 | 1272 | def _ofind(self, oname, namespaces=None): |
|
1273 | 1273 | """Find an object in the available namespaces. |
|
1274 | 1274 | |
|
1275 | 1275 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1276 | 1276 | |
|
1277 | 1277 | Has special code to detect magic functions. |
|
1278 | 1278 | """ |
|
1279 | 1279 | oname = oname.strip() |
|
1280 | 1280 | #print '1- oname: <%r>' % oname # dbg |
|
1281 | 1281 | if not py3compat.isidentifier(oname.lstrip(ESC_MAGIC), dotted=True): |
|
1282 | 1282 | return dict(found=False) |
|
1283 | 1283 | |
|
1284 | 1284 | alias_ns = None |
|
1285 | 1285 | if namespaces is None: |
|
1286 | 1286 | # Namespaces to search in: |
|
1287 | 1287 | # Put them in a list. The order is important so that we |
|
1288 | 1288 | # find things in the same order that Python finds them. |
|
1289 | 1289 | namespaces = [ ('Interactive', self.user_ns), |
|
1290 | 1290 | ('IPython internal', self.internal_ns), |
|
1291 | 1291 | ('Python builtin', builtin_mod.__dict__), |
|
1292 | 1292 | ('Alias', self.alias_manager.alias_table), |
|
1293 | 1293 | ] |
|
1294 | 1294 | alias_ns = self.alias_manager.alias_table |
|
1295 | 1295 | |
|
1296 | 1296 | # initialize results to 'null' |
|
1297 | 1297 | found = False; obj = None; ospace = None; ds = None; |
|
1298 | 1298 | ismagic = False; isalias = False; parent = None |
|
1299 | 1299 | |
|
1300 | 1300 | # We need to special-case 'print', which as of python2.6 registers as a |
|
1301 | 1301 | # function but should only be treated as one if print_function was |
|
1302 | 1302 | # loaded with a future import. In this case, just bail. |
|
1303 | 1303 | if (oname == 'print' and not py3compat.PY3 and not \ |
|
1304 | 1304 | (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): |
|
1305 | 1305 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1306 | 1306 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1307 | 1307 | |
|
1308 | 1308 | # Look for the given name by splitting it in parts. If the head is |
|
1309 | 1309 | # found, then we look for all the remaining parts as members, and only |
|
1310 | 1310 | # declare success if we can find them all. |
|
1311 | 1311 | oname_parts = oname.split('.') |
|
1312 | 1312 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
1313 | 1313 | for nsname,ns in namespaces: |
|
1314 | 1314 | try: |
|
1315 | 1315 | obj = ns[oname_head] |
|
1316 | 1316 | except KeyError: |
|
1317 | 1317 | continue |
|
1318 | 1318 | else: |
|
1319 | 1319 | #print 'oname_rest:', oname_rest # dbg |
|
1320 | 1320 | for part in oname_rest: |
|
1321 | 1321 | try: |
|
1322 | 1322 | parent = obj |
|
1323 | 1323 | obj = getattr(obj,part) |
|
1324 | 1324 | except: |
|
1325 | 1325 | # Blanket except b/c some badly implemented objects |
|
1326 | 1326 | # allow __getattr__ to raise exceptions other than |
|
1327 | 1327 | # AttributeError, which then crashes IPython. |
|
1328 | 1328 | break |
|
1329 | 1329 | else: |
|
1330 | 1330 | # If we finish the for loop (no break), we got all members |
|
1331 | 1331 | found = True |
|
1332 | 1332 | ospace = nsname |
|
1333 | 1333 | if ns == alias_ns: |
|
1334 | 1334 | isalias = True |
|
1335 | 1335 | break # namespace loop |
|
1336 | 1336 | |
|
1337 | 1337 | # Try to see if it's magic |
|
1338 | 1338 | if not found: |
|
1339 | 1339 | if oname.startswith(ESC_MAGIC): |
|
1340 | 1340 | oname = oname[1:] |
|
1341 | 1341 | obj = getattr(self,'magic_'+oname,None) |
|
1342 | 1342 | if obj is not None: |
|
1343 | 1343 | found = True |
|
1344 | 1344 | ospace = 'IPython internal' |
|
1345 | 1345 | ismagic = True |
|
1346 | 1346 | |
|
1347 | 1347 | # Last try: special-case some literals like '', [], {}, etc: |
|
1348 | 1348 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
1349 | 1349 | obj = eval(oname_head) |
|
1350 | 1350 | found = True |
|
1351 | 1351 | ospace = 'Interactive' |
|
1352 | 1352 | |
|
1353 | 1353 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1354 | 1354 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1355 | 1355 | |
|
1356 | 1356 | def _ofind_property(self, oname, info): |
|
1357 | 1357 | """Second part of object finding, to look for property details.""" |
|
1358 | 1358 | if info.found: |
|
1359 | 1359 | # Get the docstring of the class property if it exists. |
|
1360 | 1360 | path = oname.split('.') |
|
1361 | 1361 | root = '.'.join(path[:-1]) |
|
1362 | 1362 | if info.parent is not None: |
|
1363 | 1363 | try: |
|
1364 |
target = getattr(info.parent, '__class__') |
|
|
1365 |
# The object belongs to a class instance. |
|
|
1366 |
try: |
|
|
1364 | target = getattr(info.parent, '__class__') | |
|
1365 | # The object belongs to a class instance. | |
|
1366 | try: | |
|
1367 | 1367 | target = getattr(target, path[-1]) |
|
1368 |
# The class defines the object. |
|
|
1368 | # The class defines the object. | |
|
1369 | 1369 | if isinstance(target, property): |
|
1370 | 1370 | oname = root + '.__class__.' + path[-1] |
|
1371 | 1371 | info = Struct(self._ofind(oname)) |
|
1372 | 1372 | except AttributeError: pass |
|
1373 | 1373 | except AttributeError: pass |
|
1374 | 1374 | |
|
1375 | 1375 | # We return either the new info or the unmodified input if the object |
|
1376 | 1376 | # hadn't been found |
|
1377 | 1377 | return info |
|
1378 | 1378 | |
|
1379 | 1379 | def _object_find(self, oname, namespaces=None): |
|
1380 | 1380 | """Find an object and return a struct with info about it.""" |
|
1381 | 1381 | inf = Struct(self._ofind(oname, namespaces)) |
|
1382 | 1382 | return Struct(self._ofind_property(oname, inf)) |
|
1383 | ||
|
1383 | ||
|
1384 | 1384 | def _inspect(self, meth, oname, namespaces=None, **kw): |
|
1385 | 1385 | """Generic interface to the inspector system. |
|
1386 | 1386 | |
|
1387 | 1387 | This function is meant to be called by pdef, pdoc & friends.""" |
|
1388 | 1388 | info = self._object_find(oname) |
|
1389 | 1389 | if info.found: |
|
1390 | 1390 | pmethod = getattr(self.inspector, meth) |
|
1391 | 1391 | formatter = format_screen if info.ismagic else None |
|
1392 | 1392 | if meth == 'pdoc': |
|
1393 | 1393 | pmethod(info.obj, oname, formatter) |
|
1394 | 1394 | elif meth == 'pinfo': |
|
1395 | 1395 | pmethod(info.obj, oname, formatter, info, **kw) |
|
1396 | 1396 | else: |
|
1397 | 1397 | pmethod(info.obj, oname) |
|
1398 | 1398 | else: |
|
1399 | 1399 | print 'Object `%s` not found.' % oname |
|
1400 | 1400 | return 'not found' # so callers can take other action |
|
1401 | 1401 | |
|
1402 | 1402 | def object_inspect(self, oname): |
|
1403 | 1403 | with self.builtin_trap: |
|
1404 | 1404 | info = self._object_find(oname) |
|
1405 | 1405 | if info.found: |
|
1406 | 1406 | return self.inspector.info(info.obj, oname, info=info) |
|
1407 | 1407 | else: |
|
1408 | 1408 | return oinspect.object_info(name=oname, found=False) |
|
1409 | 1409 | |
|
1410 | 1410 | #------------------------------------------------------------------------- |
|
1411 | 1411 | # Things related to history management |
|
1412 | 1412 | #------------------------------------------------------------------------- |
|
1413 | 1413 | |
|
1414 | 1414 | def init_history(self): |
|
1415 | 1415 | """Sets up the command history, and starts regular autosaves.""" |
|
1416 | 1416 | self.history_manager = HistoryManager(shell=self, config=self.config) |
|
1417 | 1417 | |
|
1418 | 1418 | #------------------------------------------------------------------------- |
|
1419 | 1419 | # Things related to exception handling and tracebacks (not debugging) |
|
1420 | 1420 | #------------------------------------------------------------------------- |
|
1421 | 1421 | |
|
1422 | 1422 | def init_traceback_handlers(self, custom_exceptions): |
|
1423 | 1423 | # Syntax error handler. |
|
1424 | 1424 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') |
|
1425 | ||
|
1425 | ||
|
1426 | 1426 | # The interactive one is initialized with an offset, meaning we always |
|
1427 | 1427 | # want to remove the topmost item in the traceback, which is our own |
|
1428 | 1428 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1429 | 1429 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1430 | 1430 | color_scheme='NoColor', |
|
1431 | 1431 | tb_offset = 1, |
|
1432 | 1432 | check_cache=self.compile.check_cache) |
|
1433 | 1433 | |
|
1434 | 1434 | # The instance will store a pointer to the system-wide exception hook, |
|
1435 | 1435 | # so that runtime code (such as magics) can access it. This is because |
|
1436 | 1436 | # during the read-eval loop, it may get temporarily overwritten. |
|
1437 | 1437 | self.sys_excepthook = sys.excepthook |
|
1438 | 1438 | |
|
1439 | 1439 | # and add any custom exception handlers the user may have specified |
|
1440 | 1440 | self.set_custom_exc(*custom_exceptions) |
|
1441 | 1441 | |
|
1442 | 1442 | # Set the exception mode |
|
1443 | 1443 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1444 | 1444 | |
|
1445 | 1445 | def set_custom_exc(self, exc_tuple, handler): |
|
1446 | 1446 | """set_custom_exc(exc_tuple,handler) |
|
1447 | 1447 | |
|
1448 | 1448 | Set a custom exception handler, which will be called if any of the |
|
1449 | 1449 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1450 | 1450 | run_code() method. |
|
1451 | 1451 | |
|
1452 | 1452 | Inputs: |
|
1453 | 1453 | |
|
1454 | 1454 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
1455 | 1455 | handler for. It is very important that you use a tuple, and NOT A |
|
1456 | 1456 | LIST here, because of the way Python's except statement works. If |
|
1457 | 1457 | you only want to trap a single exception, use a singleton tuple: |
|
1458 | 1458 | |
|
1459 | 1459 | exc_tuple == (MyCustomException,) |
|
1460 | 1460 | |
|
1461 | 1461 | - handler: this must be defined as a function with the following |
|
1462 | 1462 | basic interface:: |
|
1463 | 1463 | |
|
1464 | 1464 | def my_handler(self, etype, value, tb, tb_offset=None) |
|
1465 | 1465 | ... |
|
1466 | 1466 | # The return value must be |
|
1467 | 1467 | return structured_traceback |
|
1468 | 1468 | |
|
1469 | 1469 | This will be made into an instance method (via types.MethodType) |
|
1470 | 1470 | of IPython itself, and it will be called if any of the exceptions |
|
1471 | 1471 | listed in the exc_tuple are caught. If the handler is None, an |
|
1472 | 1472 | internal basic one is used, which just prints basic info. |
|
1473 | 1473 | |
|
1474 | 1474 | WARNING: by putting in your own exception handler into IPython's main |
|
1475 | 1475 | execution loop, you run a very good chance of nasty crashes. This |
|
1476 | 1476 | facility should only be used if you really know what you are doing.""" |
|
1477 | 1477 | |
|
1478 | 1478 | assert type(exc_tuple)==type(()) , \ |
|
1479 | 1479 | "The custom exceptions must be given AS A TUPLE." |
|
1480 | 1480 | |
|
1481 | 1481 | def dummy_handler(self,etype,value,tb): |
|
1482 | 1482 | print '*** Simple custom exception handler ***' |
|
1483 | 1483 | print 'Exception type :',etype |
|
1484 | 1484 | print 'Exception value:',value |
|
1485 | 1485 | print 'Traceback :',tb |
|
1486 | 1486 | #print 'Source code :','\n'.join(self.buffer) |
|
1487 | 1487 | |
|
1488 | 1488 | if handler is None: handler = dummy_handler |
|
1489 | 1489 | |
|
1490 | 1490 | self.CustomTB = types.MethodType(handler,self) |
|
1491 | 1491 | self.custom_exceptions = exc_tuple |
|
1492 | 1492 | |
|
1493 | 1493 | def excepthook(self, etype, value, tb): |
|
1494 | 1494 | """One more defense for GUI apps that call sys.excepthook. |
|
1495 | 1495 | |
|
1496 | 1496 | GUI frameworks like wxPython trap exceptions and call |
|
1497 | 1497 | sys.excepthook themselves. I guess this is a feature that |
|
1498 | 1498 | enables them to keep running after exceptions that would |
|
1499 | 1499 | otherwise kill their mainloop. This is a bother for IPython |
|
1500 | 1500 | which excepts to catch all of the program exceptions with a try: |
|
1501 | 1501 | except: statement. |
|
1502 | 1502 | |
|
1503 | 1503 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1504 | 1504 | any app directly invokes sys.excepthook, it will look to the user like |
|
1505 | 1505 | IPython crashed. In order to work around this, we can disable the |
|
1506 | 1506 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1507 | 1507 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1508 | 1508 | call sys.excepthook will generate a regular-looking exception from |
|
1509 | 1509 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1510 | 1510 | crashes. |
|
1511 | 1511 | |
|
1512 | 1512 | This hook should be used sparingly, only in places which are not likely |
|
1513 | 1513 | to be true IPython errors. |
|
1514 | 1514 | """ |
|
1515 | 1515 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1516 | 1516 | |
|
1517 | 1517 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, |
|
1518 | 1518 | exception_only=False): |
|
1519 | 1519 | """Display the exception that just occurred. |
|
1520 | 1520 | |
|
1521 | 1521 | If nothing is known about the exception, this is the method which |
|
1522 | 1522 | should be used throughout the code for presenting user tracebacks, |
|
1523 | 1523 | rather than directly invoking the InteractiveTB object. |
|
1524 | 1524 | |
|
1525 | 1525 | A specific showsyntaxerror() also exists, but this method can take |
|
1526 | 1526 | care of calling it if needed, so unless you are explicitly catching a |
|
1527 | 1527 | SyntaxError exception, don't try to analyze the stack manually and |
|
1528 | 1528 | simply call this method.""" |
|
1529 | ||
|
1529 | ||
|
1530 | 1530 | try: |
|
1531 | 1531 | if exc_tuple is None: |
|
1532 | 1532 | etype, value, tb = sys.exc_info() |
|
1533 | 1533 | else: |
|
1534 | 1534 | etype, value, tb = exc_tuple |
|
1535 | 1535 | |
|
1536 | 1536 | if etype is None: |
|
1537 | 1537 | if hasattr(sys, 'last_type'): |
|
1538 | 1538 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1539 | 1539 | sys.last_traceback |
|
1540 | 1540 | else: |
|
1541 | 1541 | self.write_err('No traceback available to show.\n') |
|
1542 | 1542 | return |
|
1543 | ||
|
1543 | ||
|
1544 | 1544 | if etype is SyntaxError: |
|
1545 | 1545 | # Though this won't be called by syntax errors in the input |
|
1546 | 1546 | # line, there may be SyntaxError cases with imported code. |
|
1547 | 1547 | self.showsyntaxerror(filename) |
|
1548 | 1548 | elif etype is UsageError: |
|
1549 | 1549 | print "UsageError:", value |
|
1550 | 1550 | else: |
|
1551 | 1551 | # WARNING: these variables are somewhat deprecated and not |
|
1552 | 1552 | # necessarily safe to use in a threaded environment, but tools |
|
1553 | 1553 | # like pdb depend on their existence, so let's set them. If we |
|
1554 | 1554 | # find problems in the field, we'll need to revisit their use. |
|
1555 | 1555 | sys.last_type = etype |
|
1556 | 1556 | sys.last_value = value |
|
1557 | 1557 | sys.last_traceback = tb |
|
1558 | 1558 | if etype in self.custom_exceptions: |
|
1559 | 1559 | # FIXME: Old custom traceback objects may just return a |
|
1560 | 1560 | # string, in that case we just put it into a list |
|
1561 | 1561 | stb = self.CustomTB(etype, value, tb, tb_offset) |
|
1562 | 1562 | if isinstance(ctb, basestring): |
|
1563 | 1563 | stb = [stb] |
|
1564 | 1564 | else: |
|
1565 | 1565 | if exception_only: |
|
1566 | 1566 | stb = ['An exception has occurred, use %tb to see ' |
|
1567 | 1567 | 'the full traceback.\n'] |
|
1568 | 1568 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
1569 | 1569 | value)) |
|
1570 | 1570 | else: |
|
1571 | 1571 | stb = self.InteractiveTB.structured_traceback(etype, |
|
1572 | 1572 | value, tb, tb_offset=tb_offset) |
|
1573 | ||
|
1573 | ||
|
1574 | 1574 | if self.call_pdb: |
|
1575 | 1575 | # drop into debugger |
|
1576 | 1576 | self.debugger(force=True) |
|
1577 | 1577 | |
|
1578 | 1578 | # Actually show the traceback |
|
1579 | 1579 | self._showtraceback(etype, value, stb) |
|
1580 | ||
|
1580 | ||
|
1581 | 1581 | except KeyboardInterrupt: |
|
1582 | 1582 | self.write_err("\nKeyboardInterrupt\n") |
|
1583 | 1583 | |
|
1584 | 1584 | def _showtraceback(self, etype, evalue, stb): |
|
1585 | 1585 | """Actually show a traceback. |
|
1586 | 1586 | |
|
1587 | 1587 | Subclasses may override this method to put the traceback on a different |
|
1588 | 1588 | place, like a side channel. |
|
1589 | 1589 | """ |
|
1590 | 1590 | print >> io.stdout, self.InteractiveTB.stb2text(stb) |
|
1591 | 1591 | |
|
1592 | 1592 | def showsyntaxerror(self, filename=None): |
|
1593 | 1593 | """Display the syntax error that just occurred. |
|
1594 | 1594 | |
|
1595 | 1595 | This doesn't display a stack trace because there isn't one. |
|
1596 | 1596 | |
|
1597 | 1597 | If a filename is given, it is stuffed in the exception instead |
|
1598 | 1598 | of what was there before (because Python's parser always uses |
|
1599 | 1599 | "<string>" when reading from a string). |
|
1600 | 1600 | """ |
|
1601 | 1601 | etype, value, last_traceback = sys.exc_info() |
|
1602 | 1602 | |
|
1603 | 1603 | # See note about these variables in showtraceback() above |
|
1604 | 1604 | sys.last_type = etype |
|
1605 | 1605 | sys.last_value = value |
|
1606 | 1606 | sys.last_traceback = last_traceback |
|
1607 | ||
|
1607 | ||
|
1608 | 1608 | if filename and etype is SyntaxError: |
|
1609 | 1609 | # Work hard to stuff the correct filename in the exception |
|
1610 | 1610 | try: |
|
1611 | 1611 | msg, (dummy_filename, lineno, offset, line) = value |
|
1612 | 1612 | except: |
|
1613 | 1613 | # Not the format we expect; leave it alone |
|
1614 | 1614 | pass |
|
1615 | 1615 | else: |
|
1616 | 1616 | # Stuff in the right filename |
|
1617 | 1617 | try: |
|
1618 | 1618 | # Assume SyntaxError is a class exception |
|
1619 | 1619 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1620 | 1620 | except: |
|
1621 | 1621 | # If that failed, assume SyntaxError is a string |
|
1622 | 1622 | value = msg, (filename, lineno, offset, line) |
|
1623 | 1623 | stb = self.SyntaxTB.structured_traceback(etype, value, []) |
|
1624 | 1624 | self._showtraceback(etype, value, stb) |
|
1625 | ||
|
1625 | ||
|
1626 | 1626 | # This is overridden in TerminalInteractiveShell to show a message about |
|
1627 | 1627 | # the %paste magic. |
|
1628 | 1628 | def showindentationerror(self): |
|
1629 | 1629 | """Called by run_cell when there's an IndentationError in code entered |
|
1630 | 1630 | at the prompt. |
|
1631 | ||
|
1631 | ||
|
1632 | 1632 | This is overridden in TerminalInteractiveShell to show a message about |
|
1633 | 1633 | the %paste magic.""" |
|
1634 | 1634 | self.showsyntaxerror() |
|
1635 | 1635 | |
|
1636 | 1636 | #------------------------------------------------------------------------- |
|
1637 | 1637 | # Things related to readline |
|
1638 | 1638 | #------------------------------------------------------------------------- |
|
1639 | 1639 | |
|
1640 | 1640 | def init_readline(self): |
|
1641 | 1641 | """Command history completion/saving/reloading.""" |
|
1642 | 1642 | |
|
1643 | 1643 | if self.readline_use: |
|
1644 | 1644 | import IPython.utils.rlineimpl as readline |
|
1645 | 1645 | |
|
1646 | 1646 | self.rl_next_input = None |
|
1647 | 1647 | self.rl_do_indent = False |
|
1648 | 1648 | |
|
1649 | 1649 | if not self.readline_use or not readline.have_readline: |
|
1650 | 1650 | self.has_readline = False |
|
1651 | 1651 | self.readline = None |
|
1652 | 1652 | # Set a number of methods that depend on readline to be no-op |
|
1653 | 1653 | self.readline_no_record = no_op_context |
|
1654 | 1654 | self.set_readline_completer = no_op |
|
1655 | 1655 | self.set_custom_completer = no_op |
|
1656 | 1656 | self.set_completer_frame = no_op |
|
1657 | 1657 | if self.readline_use: |
|
1658 | 1658 | warn('Readline services not available or not loaded.') |
|
1659 | 1659 | else: |
|
1660 | 1660 | self.has_readline = True |
|
1661 | 1661 | self.readline = readline |
|
1662 | 1662 | sys.modules['readline'] = readline |
|
1663 | ||
|
1663 | ||
|
1664 | 1664 | # Platform-specific configuration |
|
1665 | 1665 | if os.name == 'nt': |
|
1666 | 1666 | # FIXME - check with Frederick to see if we can harmonize |
|
1667 | 1667 | # naming conventions with pyreadline to avoid this |
|
1668 | 1668 | # platform-dependent check |
|
1669 | 1669 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1670 | 1670 | else: |
|
1671 | 1671 | self.readline_startup_hook = readline.set_startup_hook |
|
1672 | 1672 | |
|
1673 | 1673 | # Load user's initrc file (readline config) |
|
1674 | 1674 | # Or if libedit is used, load editrc. |
|
1675 | 1675 | inputrc_name = os.environ.get('INPUTRC') |
|
1676 | 1676 | if inputrc_name is None: |
|
1677 | 1677 | home_dir = get_home_dir() |
|
1678 | 1678 | if home_dir is not None: |
|
1679 | 1679 | inputrc_name = '.inputrc' |
|
1680 | 1680 | if readline.uses_libedit: |
|
1681 | 1681 | inputrc_name = '.editrc' |
|
1682 | 1682 | inputrc_name = os.path.join(home_dir, inputrc_name) |
|
1683 | 1683 | if os.path.isfile(inputrc_name): |
|
1684 | 1684 | try: |
|
1685 | 1685 | readline.read_init_file(inputrc_name) |
|
1686 | 1686 | except: |
|
1687 | 1687 | warn('Problems reading readline initialization file <%s>' |
|
1688 | 1688 | % inputrc_name) |
|
1689 | ||
|
1689 | ||
|
1690 | 1690 | # Configure readline according to user's prefs |
|
1691 | 1691 | # This is only done if GNU readline is being used. If libedit |
|
1692 | 1692 | # is being used (as on Leopard) the readline config is |
|
1693 | 1693 | # not run as the syntax for libedit is different. |
|
1694 | 1694 | if not readline.uses_libedit: |
|
1695 | 1695 | for rlcommand in self.readline_parse_and_bind: |
|
1696 | 1696 | #print "loading rl:",rlcommand # dbg |
|
1697 | 1697 | readline.parse_and_bind(rlcommand) |
|
1698 | 1698 | |
|
1699 | 1699 | # Remove some chars from the delimiters list. If we encounter |
|
1700 | 1700 | # unicode chars, discard them. |
|
1701 | 1701 | delims = readline.get_completer_delims() |
|
1702 | 1702 | if not py3compat.PY3: |
|
1703 | 1703 | delims = delims.encode("ascii", "ignore") |
|
1704 | 1704 | for d in self.readline_remove_delims: |
|
1705 | 1705 | delims = delims.replace(d, "") |
|
1706 | 1706 | delims = delims.replace(ESC_MAGIC, '') |
|
1707 | 1707 | readline.set_completer_delims(delims) |
|
1708 | 1708 | # otherwise we end up with a monster history after a while: |
|
1709 | 1709 | readline.set_history_length(self.history_length) |
|
1710 | ||
|
1710 | ||
|
1711 | 1711 | self.refill_readline_hist() |
|
1712 | 1712 | self.readline_no_record = ReadlineNoRecord(self) |
|
1713 | 1713 | |
|
1714 | 1714 | # Configure auto-indent for all platforms |
|
1715 | 1715 | self.set_autoindent(self.autoindent) |
|
1716 | ||
|
1716 | ||
|
1717 | 1717 | def refill_readline_hist(self): |
|
1718 | 1718 | # Load the last 1000 lines from history |
|
1719 | 1719 | self.readline.clear_history() |
|
1720 | 1720 | stdin_encoding = sys.stdin.encoding or "utf-8" |
|
1721 | 1721 | for _, _, cell in self.history_manager.get_tail(1000, |
|
1722 | 1722 | include_latest=True): |
|
1723 | 1723 | if cell.strip(): # Ignore blank lines |
|
1724 | 1724 | for line in cell.splitlines(): |
|
1725 | 1725 | self.readline.add_history(py3compat.unicode_to_str(line, |
|
1726 | 1726 | stdin_encoding)) |
|
1727 | 1727 | |
|
1728 | 1728 | def set_next_input(self, s): |
|
1729 | 1729 | """ Sets the 'default' input string for the next command line. |
|
1730 | ||
|
1730 | ||
|
1731 | 1731 | Requires readline. |
|
1732 | ||
|
1732 | ||
|
1733 | 1733 | Example: |
|
1734 | ||
|
1734 | ||
|
1735 | 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 | 1738 | if isinstance(s, unicode): |
|
1739 | 1739 | s = s.encode(self.stdin_encoding, 'replace') |
|
1740 | 1740 | self.rl_next_input = s |
|
1741 | 1741 | |
|
1742 | 1742 | # Maybe move this to the terminal subclass? |
|
1743 | 1743 | def pre_readline(self): |
|
1744 | 1744 | """readline hook to be used at the start of each line. |
|
1745 | 1745 | |
|
1746 | 1746 | Currently it handles auto-indent only.""" |
|
1747 | 1747 | |
|
1748 | 1748 | if self.rl_do_indent: |
|
1749 | 1749 | self.readline.insert_text(self._indent_current_str()) |
|
1750 | 1750 | if self.rl_next_input is not None: |
|
1751 | 1751 | self.readline.insert_text(self.rl_next_input) |
|
1752 | 1752 | self.rl_next_input = None |
|
1753 | 1753 | |
|
1754 | 1754 | def _indent_current_str(self): |
|
1755 | 1755 | """return the current level of indentation as a string""" |
|
1756 | 1756 | return self.input_splitter.indent_spaces * ' ' |
|
1757 | 1757 | |
|
1758 | 1758 | #------------------------------------------------------------------------- |
|
1759 | 1759 | # Things related to text completion |
|
1760 | 1760 | #------------------------------------------------------------------------- |
|
1761 | 1761 | |
|
1762 | 1762 | def init_completer(self): |
|
1763 | 1763 | """Initialize the completion machinery. |
|
1764 | 1764 | |
|
1765 | 1765 | This creates completion machinery that can be used by client code, |
|
1766 | 1766 | either interactively in-process (typically triggered by the readline |
|
1767 | 1767 | library), programatically (such as in test suites) or out-of-prcess |
|
1768 | 1768 | (typically over the network by remote frontends). |
|
1769 | 1769 | """ |
|
1770 | 1770 | from IPython.core.completer import IPCompleter |
|
1771 | 1771 | from IPython.core.completerlib import (module_completer, |
|
1772 | 1772 | magic_run_completer, cd_completer) |
|
1773 | ||
|
1773 | ||
|
1774 | 1774 | self.Completer = IPCompleter(shell=self, |
|
1775 | 1775 | namespace=self.user_ns, |
|
1776 | 1776 | global_namespace=self.user_global_ns, |
|
1777 | 1777 | omit__names=self.readline_omit__names, |
|
1778 | 1778 | alias_table=self.alias_manager.alias_table, |
|
1779 | 1779 | use_readline=self.has_readline, |
|
1780 | 1780 | config=self.config, |
|
1781 | 1781 | ) |
|
1782 | ||
|
1782 | ||
|
1783 | 1783 | # Add custom completers to the basic ones built into IPCompleter |
|
1784 | 1784 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1785 | 1785 | self.strdispatchers['complete_command'] = sdisp |
|
1786 | 1786 | self.Completer.custom_completers = sdisp |
|
1787 | 1787 | |
|
1788 | 1788 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
1789 | 1789 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
1790 | 1790 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
1791 | 1791 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
1792 | 1792 | |
|
1793 | 1793 | # Only configure readline if we truly are using readline. IPython can |
|
1794 | 1794 | # do tab-completion over the network, in GUIs, etc, where readline |
|
1795 | 1795 | # itself may be absent |
|
1796 | 1796 | if self.has_readline: |
|
1797 | 1797 | self.set_readline_completer() |
|
1798 | 1798 | |
|
1799 | 1799 | def complete(self, text, line=None, cursor_pos=None): |
|
1800 | 1800 | """Return the completed text and a list of completions. |
|
1801 | 1801 | |
|
1802 | 1802 | Parameters |
|
1803 | 1803 | ---------- |
|
1804 | 1804 | |
|
1805 | 1805 | text : string |
|
1806 | 1806 | A string of text to be completed on. It can be given as empty and |
|
1807 | 1807 | instead a line/position pair are given. In this case, the |
|
1808 | 1808 | completer itself will split the line like readline does. |
|
1809 | 1809 | |
|
1810 | 1810 | line : string, optional |
|
1811 | 1811 | The complete line that text is part of. |
|
1812 | 1812 | |
|
1813 | 1813 | cursor_pos : int, optional |
|
1814 | 1814 | The position of the cursor on the input line. |
|
1815 | 1815 | |
|
1816 | 1816 | Returns |
|
1817 | 1817 | ------- |
|
1818 | 1818 | text : string |
|
1819 | 1819 | The actual text that was completed. |
|
1820 | 1820 | |
|
1821 | 1821 | matches : list |
|
1822 | 1822 | A sorted list with all possible completions. |
|
1823 | 1823 | |
|
1824 | 1824 | The optional arguments allow the completion to take more context into |
|
1825 | 1825 | account, and are part of the low-level completion API. |
|
1826 | ||
|
1826 | ||
|
1827 | 1827 | This is a wrapper around the completion mechanism, similar to what |
|
1828 | 1828 | readline does at the command line when the TAB key is hit. By |
|
1829 | 1829 | exposing it as a method, it can be used by other non-readline |
|
1830 | 1830 | environments (such as GUIs) for text completion. |
|
1831 | 1831 | |
|
1832 | 1832 | Simple usage example: |
|
1833 | 1833 | |
|
1834 | 1834 | In [1]: x = 'hello' |
|
1835 | 1835 | |
|
1836 | 1836 | In [2]: _ip.complete('x.l') |
|
1837 | 1837 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
1838 | 1838 | """ |
|
1839 | 1839 | |
|
1840 | 1840 | # Inject names into __builtin__ so we can complete on the added names. |
|
1841 | 1841 | with self.builtin_trap: |
|
1842 | 1842 | return self.Completer.complete(text, line, cursor_pos) |
|
1843 | 1843 | |
|
1844 | 1844 | def set_custom_completer(self, completer, pos=0): |
|
1845 | 1845 | """Adds a new custom completer function. |
|
1846 | 1846 | |
|
1847 | 1847 | The position argument (defaults to 0) is the index in the completers |
|
1848 | 1848 | list where you want the completer to be inserted.""" |
|
1849 | 1849 | |
|
1850 | 1850 | newcomp = types.MethodType(completer,self.Completer) |
|
1851 | 1851 | self.Completer.matchers.insert(pos,newcomp) |
|
1852 | 1852 | |
|
1853 | 1853 | def set_readline_completer(self): |
|
1854 | 1854 | """Reset readline's completer to be our own.""" |
|
1855 | 1855 | self.readline.set_completer(self.Completer.rlcomplete) |
|
1856 | 1856 | |
|
1857 | 1857 | def set_completer_frame(self, frame=None): |
|
1858 | 1858 | """Set the frame of the completer.""" |
|
1859 | 1859 | if frame: |
|
1860 | 1860 | self.Completer.namespace = frame.f_locals |
|
1861 | 1861 | self.Completer.global_namespace = frame.f_globals |
|
1862 | 1862 | else: |
|
1863 | 1863 | self.Completer.namespace = self.user_ns |
|
1864 | 1864 | self.Completer.global_namespace = self.user_global_ns |
|
1865 | 1865 | |
|
1866 | 1866 | #------------------------------------------------------------------------- |
|
1867 | 1867 | # Things related to magics |
|
1868 | 1868 | #------------------------------------------------------------------------- |
|
1869 | 1869 | |
|
1870 | 1870 | def init_magics(self): |
|
1871 | 1871 | # FIXME: Move the color initialization to the DisplayHook, which |
|
1872 | 1872 | # should be split into a prompt manager and displayhook. We probably |
|
1873 | 1873 | # even need a centralize colors management object. |
|
1874 | 1874 | self.magic_colors(self.colors) |
|
1875 | 1875 | # History was moved to a separate module |
|
1876 | 1876 | from . import history |
|
1877 | 1877 | history.init_ipython(self) |
|
1878 | 1878 | |
|
1879 | 1879 | def magic(self, arg_s, next_input=None): |
|
1880 | 1880 | """Call a magic function by name. |
|
1881 | 1881 | |
|
1882 | 1882 | Input: a string containing the name of the magic function to call and |
|
1883 | 1883 | any additional arguments to be passed to the magic. |
|
1884 | 1884 | |
|
1885 | 1885 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
1886 | 1886 | prompt: |
|
1887 | 1887 | |
|
1888 | 1888 | In[1]: %name -opt foo bar |
|
1889 | 1889 | |
|
1890 | 1890 | To call a magic without arguments, simply use magic('name'). |
|
1891 | 1891 | |
|
1892 | 1892 | This provides a proper Python function to call IPython's magics in any |
|
1893 | 1893 | valid Python code you can type at the interpreter, including loops and |
|
1894 | 1894 | compound statements. |
|
1895 | 1895 | """ |
|
1896 | 1896 | # Allow setting the next input - this is used if the user does `a=abs?`. |
|
1897 | 1897 | # We do this first so that magic functions can override it. |
|
1898 | 1898 | if next_input: |
|
1899 | 1899 | self.set_next_input(next_input) |
|
1900 | ||
|
1900 | ||
|
1901 | 1901 | args = arg_s.split(' ',1) |
|
1902 | 1902 | magic_name = args[0] |
|
1903 | 1903 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
1904 | ||
|
1904 | ||
|
1905 | 1905 | try: |
|
1906 | 1906 | magic_args = args[1] |
|
1907 | 1907 | except IndexError: |
|
1908 | 1908 | magic_args = '' |
|
1909 | 1909 | fn = getattr(self,'magic_'+magic_name,None) |
|
1910 | 1910 | if fn is None: |
|
1911 | 1911 | error("Magic function `%s` not found." % magic_name) |
|
1912 | 1912 | else: |
|
1913 | 1913 | magic_args = self.var_expand(magic_args,1) |
|
1914 | 1914 | # Grab local namespace if we need it: |
|
1915 | 1915 | if getattr(fn, "needs_local_scope", False): |
|
1916 | 1916 | self._magic_locals = sys._getframe(1).f_locals |
|
1917 | 1917 | with self.builtin_trap: |
|
1918 | 1918 | result = fn(magic_args) |
|
1919 | 1919 | # Ensure we're not keeping object references around: |
|
1920 | 1920 | self._magic_locals = {} |
|
1921 | 1921 | return result |
|
1922 | 1922 | |
|
1923 | 1923 | def define_magic(self, magicname, func): |
|
1924 |
"""Expose own function as magic function for ipython |
|
|
1925 | ||
|
1924 | """Expose own function as magic function for ipython | |
|
1925 | ||
|
1926 | 1926 | def foo_impl(self,parameter_s=''): |
|
1927 | 1927 | 'My very own magic!. (Use docstrings, IPython reads them).' |
|
1928 | 1928 | print 'Magic function. Passed parameter is between < >:' |
|
1929 | 1929 | print '<%s>' % parameter_s |
|
1930 | 1930 | print 'The self object is:',self |
|
1931 | ||
|
1931 | ||
|
1932 | 1932 | self.define_magic('foo',foo_impl) |
|
1933 | 1933 | """ |
|
1934 | 1934 | im = types.MethodType(func,self) |
|
1935 | 1935 | old = getattr(self, "magic_" + magicname, None) |
|
1936 | 1936 | setattr(self, "magic_" + magicname, im) |
|
1937 | 1937 | return old |
|
1938 | 1938 | |
|
1939 | 1939 | #------------------------------------------------------------------------- |
|
1940 | 1940 | # Things related to macros |
|
1941 | 1941 | #------------------------------------------------------------------------- |
|
1942 | 1942 | |
|
1943 | 1943 | def define_macro(self, name, themacro): |
|
1944 | 1944 | """Define a new macro |
|
1945 | 1945 | |
|
1946 | 1946 | Parameters |
|
1947 | 1947 | ---------- |
|
1948 | 1948 | name : str |
|
1949 | 1949 | The name of the macro. |
|
1950 | 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 | 1952 | Macro object is created by passing the string to it. |
|
1953 | 1953 | """ |
|
1954 | ||
|
1954 | ||
|
1955 | 1955 | from IPython.core import macro |
|
1956 | 1956 | |
|
1957 | 1957 | if isinstance(themacro, basestring): |
|
1958 | 1958 | themacro = macro.Macro(themacro) |
|
1959 | 1959 | if not isinstance(themacro, macro.Macro): |
|
1960 | 1960 | raise ValueError('A macro must be a string or a Macro instance.') |
|
1961 | 1961 | self.user_ns[name] = themacro |
|
1962 | 1962 | |
|
1963 | 1963 | #------------------------------------------------------------------------- |
|
1964 | 1964 | # Things related to the running of system commands |
|
1965 | 1965 | #------------------------------------------------------------------------- |
|
1966 | 1966 | |
|
1967 | 1967 | def system_piped(self, cmd): |
|
1968 | 1968 | """Call the given cmd in a subprocess, piping stdout/err |
|
1969 | 1969 | |
|
1970 | 1970 | Parameters |
|
1971 | 1971 | ---------- |
|
1972 | 1972 | cmd : str |
|
1973 | 1973 | Command to execute (can not end in '&', as background processes are |
|
1974 | 1974 | not supported. Should not be a command that expects input |
|
1975 | 1975 | other than simple text. |
|
1976 | 1976 | """ |
|
1977 | 1977 | if cmd.rstrip().endswith('&'): |
|
1978 | 1978 | # this is *far* from a rigorous test |
|
1979 | 1979 | # We do not support backgrounding processes because we either use |
|
1980 | 1980 | # pexpect or pipes to read from. Users can always just call |
|
1981 | 1981 | # os.system() or use ip.system=ip.system_raw |
|
1982 | 1982 | # if they really want a background process. |
|
1983 | 1983 | raise OSError("Background processes not supported.") |
|
1984 | ||
|
1984 | ||
|
1985 | 1985 | # we explicitly do NOT return the subprocess status code, because |
|
1986 | 1986 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
1987 | 1987 | # Instead, we store the exit_code in user_ns. |
|
1988 | 1988 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=2)) |
|
1989 | ||
|
1989 | ||
|
1990 | 1990 | def system_raw(self, cmd): |
|
1991 | 1991 | """Call the given cmd in a subprocess using os.system |
|
1992 | ||
|
1992 | ||
|
1993 | 1993 | Parameters |
|
1994 | 1994 | ---------- |
|
1995 | 1995 | cmd : str |
|
1996 | 1996 | Command to execute. |
|
1997 | 1997 | """ |
|
1998 | 1998 | # We explicitly do NOT return the subprocess status code, because |
|
1999 | 1999 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2000 | 2000 | # Instead, we store the exit_code in user_ns. |
|
2001 | 2001 | self.user_ns['_exit_code'] = os.system(self.var_expand(cmd, depth=2)) |
|
2002 | ||
|
2002 | ||
|
2003 | 2003 | # use piped system by default, because it is better behaved |
|
2004 | 2004 | system = system_piped |
|
2005 | 2005 | |
|
2006 | 2006 | def getoutput(self, cmd, split=True): |
|
2007 | 2007 | """Get output (possibly including stderr) from a subprocess. |
|
2008 | 2008 | |
|
2009 | 2009 | Parameters |
|
2010 | 2010 | ---------- |
|
2011 | 2011 | cmd : str |
|
2012 | 2012 | Command to execute (can not end in '&', as background processes are |
|
2013 | 2013 | not supported. |
|
2014 | 2014 | split : bool, optional |
|
2015 | ||
|
2015 | ||
|
2016 | 2016 | If True, split the output into an IPython SList. Otherwise, an |
|
2017 | 2017 | IPython LSString is returned. These are objects similar to normal |
|
2018 | 2018 | lists and strings, with a few convenience attributes for easier |
|
2019 | 2019 | manipulation of line-based output. You can use '?' on them for |
|
2020 | 2020 | details. |
|
2021 | 2021 | """ |
|
2022 | 2022 | if cmd.rstrip().endswith('&'): |
|
2023 | 2023 | # this is *far* from a rigorous test |
|
2024 | 2024 | raise OSError("Background processes not supported.") |
|
2025 | 2025 | out = getoutput(self.var_expand(cmd, depth=2)) |
|
2026 | 2026 | if split: |
|
2027 | 2027 | out = SList(out.splitlines()) |
|
2028 | 2028 | else: |
|
2029 | 2029 | out = LSString(out) |
|
2030 | 2030 | return out |
|
2031 | 2031 | |
|
2032 | 2032 | #------------------------------------------------------------------------- |
|
2033 | 2033 | # Things related to aliases |
|
2034 | 2034 | #------------------------------------------------------------------------- |
|
2035 | 2035 | |
|
2036 | 2036 | def init_alias(self): |
|
2037 | 2037 | self.alias_manager = AliasManager(shell=self, config=self.config) |
|
2038 | 2038 | self.ns_table['alias'] = self.alias_manager.alias_table, |
|
2039 | 2039 | |
|
2040 | 2040 | #------------------------------------------------------------------------- |
|
2041 | 2041 | # Things related to extensions and plugins |
|
2042 | 2042 | #------------------------------------------------------------------------- |
|
2043 | 2043 | |
|
2044 | 2044 | def init_extension_manager(self): |
|
2045 | 2045 | self.extension_manager = ExtensionManager(shell=self, config=self.config) |
|
2046 | 2046 | |
|
2047 | 2047 | def init_plugin_manager(self): |
|
2048 | 2048 | self.plugin_manager = PluginManager(config=self.config) |
|
2049 | 2049 | |
|
2050 | 2050 | #------------------------------------------------------------------------- |
|
2051 | 2051 | # Things related to payloads |
|
2052 | 2052 | #------------------------------------------------------------------------- |
|
2053 | 2053 | |
|
2054 | 2054 | def init_payload(self): |
|
2055 | 2055 | self.payload_manager = PayloadManager(config=self.config) |
|
2056 | 2056 | |
|
2057 | 2057 | #------------------------------------------------------------------------- |
|
2058 | 2058 | # Things related to the prefilter |
|
2059 | 2059 | #------------------------------------------------------------------------- |
|
2060 | 2060 | |
|
2061 | 2061 | def init_prefilter(self): |
|
2062 | 2062 | self.prefilter_manager = PrefilterManager(shell=self, config=self.config) |
|
2063 | 2063 | # Ultimately this will be refactored in the new interpreter code, but |
|
2064 | 2064 | # for now, we should expose the main prefilter method (there's legacy |
|
2065 | 2065 | # code out there that may rely on this). |
|
2066 | 2066 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
2067 | 2067 | |
|
2068 | 2068 | def auto_rewrite_input(self, cmd): |
|
2069 | 2069 | """Print to the screen the rewritten form of the user's command. |
|
2070 | 2070 | |
|
2071 | 2071 | This shows visual feedback by rewriting input lines that cause |
|
2072 | 2072 | automatic calling to kick in, like:: |
|
2073 | 2073 | |
|
2074 | 2074 | /f x |
|
2075 | 2075 | |
|
2076 | 2076 | into:: |
|
2077 | 2077 | |
|
2078 | 2078 | ------> f(x) |
|
2079 | ||
|
2079 | ||
|
2080 | 2080 | after the user's input prompt. This helps the user understand that the |
|
2081 | 2081 | input line was transformed automatically by IPython. |
|
2082 | 2082 | """ |
|
2083 | 2083 | rw = self.displayhook.prompt1.auto_rewrite() + cmd |
|
2084 | 2084 | |
|
2085 | 2085 | try: |
|
2086 | 2086 | # plain ascii works better w/ pyreadline, on some machines, so |
|
2087 | 2087 | # we use it and only print uncolored rewrite if we have unicode |
|
2088 | 2088 | rw = str(rw) |
|
2089 | 2089 | print >> io.stdout, rw |
|
2090 | 2090 | except UnicodeEncodeError: |
|
2091 | 2091 | print "------> " + cmd |
|
2092 | ||
|
2092 | ||
|
2093 | 2093 | #------------------------------------------------------------------------- |
|
2094 | 2094 | # Things related to extracting values/expressions from kernel and user_ns |
|
2095 | 2095 | #------------------------------------------------------------------------- |
|
2096 | 2096 | |
|
2097 | 2097 | def _simple_error(self): |
|
2098 | 2098 | etype, value = sys.exc_info()[:2] |
|
2099 | 2099 | return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value) |
|
2100 | 2100 | |
|
2101 | 2101 | def user_variables(self, names): |
|
2102 | 2102 | """Get a list of variable names from the user's namespace. |
|
2103 | 2103 | |
|
2104 | 2104 | Parameters |
|
2105 | 2105 | ---------- |
|
2106 | 2106 | names : list of strings |
|
2107 | 2107 | A list of names of variables to be read from the user namespace. |
|
2108 | 2108 | |
|
2109 | 2109 | Returns |
|
2110 | 2110 | ------- |
|
2111 | 2111 | A dict, keyed by the input names and with the repr() of each value. |
|
2112 | 2112 | """ |
|
2113 | 2113 | out = {} |
|
2114 | 2114 | user_ns = self.user_ns |
|
2115 | 2115 | for varname in names: |
|
2116 | 2116 | try: |
|
2117 | 2117 | value = repr(user_ns[varname]) |
|
2118 | 2118 | except: |
|
2119 | 2119 | value = self._simple_error() |
|
2120 | 2120 | out[varname] = value |
|
2121 | 2121 | return out |
|
2122 | ||
|
2122 | ||
|
2123 | 2123 | def user_expressions(self, expressions): |
|
2124 | 2124 | """Evaluate a dict of expressions in the user's namespace. |
|
2125 | 2125 | |
|
2126 | 2126 | Parameters |
|
2127 | 2127 | ---------- |
|
2128 | 2128 | expressions : dict |
|
2129 | 2129 | A dict with string keys and string values. The expression values |
|
2130 | 2130 | should be valid Python expressions, each of which will be evaluated |
|
2131 | 2131 | in the user namespace. |
|
2132 | ||
|
2132 | ||
|
2133 | 2133 | Returns |
|
2134 | 2134 | ------- |
|
2135 | 2135 | A dict, keyed like the input expressions dict, with the repr() of each |
|
2136 | 2136 | value. |
|
2137 | 2137 | """ |
|
2138 | 2138 | out = {} |
|
2139 | 2139 | user_ns = self.user_ns |
|
2140 | 2140 | global_ns = self.user_global_ns |
|
2141 | 2141 | for key, expr in expressions.iteritems(): |
|
2142 | 2142 | try: |
|
2143 | 2143 | value = repr(eval(expr, global_ns, user_ns)) |
|
2144 | 2144 | except: |
|
2145 | 2145 | value = self._simple_error() |
|
2146 | 2146 | out[key] = value |
|
2147 | 2147 | return out |
|
2148 | 2148 | |
|
2149 | 2149 | #------------------------------------------------------------------------- |
|
2150 | 2150 | # Things related to the running of code |
|
2151 | 2151 | #------------------------------------------------------------------------- |
|
2152 | 2152 | |
|
2153 | 2153 | def ex(self, cmd): |
|
2154 | 2154 | """Execute a normal python statement in user namespace.""" |
|
2155 | 2155 | with self.builtin_trap: |
|
2156 | 2156 | exec cmd in self.user_global_ns, self.user_ns |
|
2157 | 2157 | |
|
2158 | 2158 | def ev(self, expr): |
|
2159 | 2159 | """Evaluate python expression expr in user namespace. |
|
2160 | 2160 | |
|
2161 | 2161 | Returns the result of evaluation |
|
2162 | 2162 | """ |
|
2163 | 2163 | with self.builtin_trap: |
|
2164 | 2164 | return eval(expr, self.user_global_ns, self.user_ns) |
|
2165 | 2165 | |
|
2166 | 2166 | def safe_execfile(self, fname, *where, **kw): |
|
2167 | 2167 | """A safe version of the builtin execfile(). |
|
2168 | 2168 | |
|
2169 | 2169 | This version will never throw an exception, but instead print |
|
2170 | 2170 | helpful error messages to the screen. This only works on pure |
|
2171 | 2171 | Python files with the .py extension. |
|
2172 | 2172 | |
|
2173 | 2173 | Parameters |
|
2174 | 2174 | ---------- |
|
2175 | 2175 | fname : string |
|
2176 | 2176 | The name of the file to be executed. |
|
2177 | 2177 | where : tuple |
|
2178 | 2178 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2179 | 2179 | If only one is given, it is passed as both. |
|
2180 | 2180 | exit_ignore : bool (False) |
|
2181 | 2181 | If True, then silence SystemExit for non-zero status (it is always |
|
2182 | 2182 | silenced for zero status, as it is so common). |
|
2183 | 2183 | """ |
|
2184 | 2184 | kw.setdefault('exit_ignore', False) |
|
2185 | 2185 | |
|
2186 | 2186 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2187 | 2187 | |
|
2188 | 2188 | # Make sure we can open the file |
|
2189 | 2189 | try: |
|
2190 | 2190 | with open(fname) as thefile: |
|
2191 | 2191 | pass |
|
2192 | 2192 | except: |
|
2193 | 2193 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2194 | 2194 | return |
|
2195 | 2195 | |
|
2196 | 2196 | # Find things also in current directory. This is needed to mimic the |
|
2197 | 2197 | # behavior of running a script from the system command line, where |
|
2198 | 2198 | # Python inserts the script's directory into sys.path |
|
2199 | 2199 | dname = os.path.dirname(fname) |
|
2200 | 2200 | |
|
2201 | 2201 | with prepended_to_syspath(dname): |
|
2202 | 2202 | try: |
|
2203 | 2203 | py3compat.execfile(fname,*where) |
|
2204 | 2204 | except SystemExit, status: |
|
2205 | 2205 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2206 | 2206 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2207 | 2207 | # these are considered normal by the OS: |
|
2208 | 2208 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
2209 | 2209 | # 0 |
|
2210 | 2210 | # > python -c'import sys;sys.exit()'; echo $? |
|
2211 | 2211 | # 0 |
|
2212 | 2212 | # For other exit status, we show the exception unless |
|
2213 | 2213 | # explicitly silenced, but only in short form. |
|
2214 | 2214 | if status.code not in (0, None) and not kw['exit_ignore']: |
|
2215 | 2215 | self.showtraceback(exception_only=True) |
|
2216 | 2216 | except: |
|
2217 | 2217 | self.showtraceback() |
|
2218 | 2218 | |
|
2219 | 2219 | def safe_execfile_ipy(self, fname): |
|
2220 | 2220 | """Like safe_execfile, but for .ipy files with IPython syntax. |
|
2221 | 2221 | |
|
2222 | 2222 | Parameters |
|
2223 | 2223 | ---------- |
|
2224 | 2224 | fname : str |
|
2225 | 2225 | The name of the file to execute. The filename must have a |
|
2226 | 2226 | .ipy extension. |
|
2227 | 2227 | """ |
|
2228 | 2228 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2229 | 2229 | |
|
2230 | 2230 | # Make sure we can open the file |
|
2231 | 2231 | try: |
|
2232 | 2232 | with open(fname) as thefile: |
|
2233 | 2233 | pass |
|
2234 | 2234 | except: |
|
2235 | 2235 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2236 | 2236 | return |
|
2237 | 2237 | |
|
2238 | 2238 | # Find things also in current directory. This is needed to mimic the |
|
2239 | 2239 | # behavior of running a script from the system command line, where |
|
2240 | 2240 | # Python inserts the script's directory into sys.path |
|
2241 | 2241 | dname = os.path.dirname(fname) |
|
2242 | 2242 | |
|
2243 | 2243 | with prepended_to_syspath(dname): |
|
2244 | 2244 | try: |
|
2245 | 2245 | with open(fname) as thefile: |
|
2246 | 2246 | # self.run_cell currently captures all exceptions |
|
2247 | 2247 | # raised in user code. It would be nice if there were |
|
2248 | 2248 | # versions of runlines, execfile that did raise, so |
|
2249 | 2249 | # we could catch the errors. |
|
2250 | 2250 | self.run_cell(thefile.read(), store_history=False) |
|
2251 | 2251 | except: |
|
2252 | 2252 | self.showtraceback() |
|
2253 | 2253 | warn('Unknown failure executing file: <%s>' % fname) |
|
2254 | ||
|
2254 | ||
|
2255 | 2255 | def run_cell(self, raw_cell, store_history=True): |
|
2256 | 2256 | """Run a complete IPython cell. |
|
2257 | ||
|
2257 | ||
|
2258 | 2258 | Parameters |
|
2259 | 2259 | ---------- |
|
2260 | 2260 | raw_cell : str |
|
2261 | 2261 | The code (including IPython code such as %magic functions) to run. |
|
2262 | 2262 | store_history : bool |
|
2263 | 2263 | If True, the raw and translated cell will be stored in IPython's |
|
2264 | 2264 | history. For user code calling back into IPython's machinery, this |
|
2265 | 2265 | should be set to False. |
|
2266 | 2266 | """ |
|
2267 | 2267 | if (not raw_cell) or raw_cell.isspace(): |
|
2268 | 2268 | return |
|
2269 | ||
|
2269 | ||
|
2270 | 2270 | for line in raw_cell.splitlines(): |
|
2271 | 2271 | self.input_splitter.push(line) |
|
2272 | 2272 | cell = self.input_splitter.source_reset() |
|
2273 | ||
|
2273 | ||
|
2274 | 2274 | with self.builtin_trap: |
|
2275 | 2275 | prefilter_failed = False |
|
2276 | 2276 | if len(cell.splitlines()) == 1: |
|
2277 | 2277 | try: |
|
2278 | 2278 | # use prefilter_lines to handle trailing newlines |
|
2279 | 2279 | # restore trailing newline for ast.parse |
|
2280 | 2280 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' |
|
2281 | 2281 | except AliasError as e: |
|
2282 | 2282 | error(e) |
|
2283 | 2283 | prefilter_failed = True |
|
2284 | 2284 | except Exception: |
|
2285 | 2285 | # don't allow prefilter errors to crash IPython |
|
2286 | 2286 | self.showtraceback() |
|
2287 | 2287 | prefilter_failed = True |
|
2288 | ||
|
2288 | ||
|
2289 | 2289 | # Store raw and processed history |
|
2290 | 2290 | if store_history: |
|
2291 |
self.history_manager.store_inputs(self.execution_count, |
|
|
2291 | self.history_manager.store_inputs(self.execution_count, | |
|
2292 | 2292 | cell, raw_cell) |
|
2293 | 2293 | |
|
2294 | 2294 | self.logger.log(cell, raw_cell) |
|
2295 | ||
|
2295 | ||
|
2296 | 2296 | if not prefilter_failed: |
|
2297 | 2297 | # don't run if prefilter failed |
|
2298 | 2298 | cell_name = self.compile.cache(cell, self.execution_count) |
|
2299 | ||
|
2299 | ||
|
2300 | 2300 | with self.display_trap: |
|
2301 | 2301 | try: |
|
2302 | 2302 | code_ast = self.compile.ast_parse(cell, filename=cell_name) |
|
2303 | 2303 | except IndentationError: |
|
2304 | 2304 | self.showindentationerror() |
|
2305 | 2305 | self.execution_count += 1 |
|
2306 | 2306 | return None |
|
2307 | 2307 | except (OverflowError, SyntaxError, ValueError, TypeError, |
|
2308 | 2308 | MemoryError): |
|
2309 | 2309 | self.showsyntaxerror() |
|
2310 | 2310 | self.execution_count += 1 |
|
2311 | 2311 | return None |
|
2312 | ||
|
2312 | ||
|
2313 | 2313 | self.run_ast_nodes(code_ast.body, cell_name, |
|
2314 | 2314 | interactivity="last_expr") |
|
2315 | ||
|
2315 | ||
|
2316 | 2316 | # Execute any registered post-execution functions. |
|
2317 | 2317 | for func, status in self._post_execute.iteritems(): |
|
2318 | 2318 | if not status: |
|
2319 | 2319 | continue |
|
2320 | 2320 | try: |
|
2321 | 2321 | func() |
|
2322 | 2322 | except: |
|
2323 | 2323 | self.showtraceback() |
|
2324 | 2324 | # Deactivate failing function |
|
2325 | 2325 | self._post_execute[func] = False |
|
2326 | ||
|
2326 | ||
|
2327 | 2327 | if store_history: |
|
2328 | 2328 | # Write output to the database. Does nothing unless |
|
2329 | 2329 | # history output logging is enabled. |
|
2330 | 2330 | self.history_manager.store_output(self.execution_count) |
|
2331 | 2331 | # Each cell is a *single* input, regardless of how many lines it has |
|
2332 | 2332 | self.execution_count += 1 |
|
2333 | ||
|
2333 | ||
|
2334 | 2334 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'): |
|
2335 | 2335 | """Run a sequence of AST nodes. The execution mode depends on the |
|
2336 | 2336 | interactivity parameter. |
|
2337 | ||
|
2337 | ||
|
2338 | 2338 | Parameters |
|
2339 | 2339 | ---------- |
|
2340 | 2340 | nodelist : list |
|
2341 | 2341 | A sequence of AST nodes to run. |
|
2342 | 2342 | cell_name : str |
|
2343 | 2343 | Will be passed to the compiler as the filename of the cell. Typically |
|
2344 | 2344 | the value returned by ip.compile.cache(cell). |
|
2345 | 2345 | interactivity : str |
|
2346 | 2346 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
2347 | 2347 | run interactively (displaying output from expressions). 'last_expr' |
|
2348 | 2348 | will run the last node interactively only if it is an expression (i.e. |
|
2349 | 2349 | expressions in loops or other blocks are not displayed. Other values |
|
2350 | 2350 | for this parameter will raise a ValueError. |
|
2351 | 2351 | """ |
|
2352 | 2352 | if not nodelist: |
|
2353 | 2353 | return |
|
2354 | ||
|
2354 | ||
|
2355 | 2355 | if interactivity == 'last_expr': |
|
2356 | 2356 | if isinstance(nodelist[-1], ast.Expr): |
|
2357 | 2357 | interactivity = "last" |
|
2358 | 2358 | else: |
|
2359 | 2359 | interactivity = "none" |
|
2360 | ||
|
2360 | ||
|
2361 | 2361 | if interactivity == 'none': |
|
2362 | 2362 | to_run_exec, to_run_interactive = nodelist, [] |
|
2363 | 2363 | elif interactivity == 'last': |
|
2364 | 2364 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] |
|
2365 | 2365 | elif interactivity == 'all': |
|
2366 | 2366 | to_run_exec, to_run_interactive = [], nodelist |
|
2367 | 2367 | else: |
|
2368 | 2368 | raise ValueError("Interactivity was %r" % interactivity) |
|
2369 | ||
|
2369 | ||
|
2370 | 2370 | exec_count = self.execution_count |
|
2371 | 2371 | |
|
2372 | 2372 | try: |
|
2373 | 2373 | for i, node in enumerate(to_run_exec): |
|
2374 | 2374 | mod = ast.Module([node]) |
|
2375 | 2375 | code = self.compile(mod, cell_name, "exec") |
|
2376 | 2376 | if self.run_code(code): |
|
2377 | 2377 | return True |
|
2378 | 2378 | |
|
2379 | 2379 | for i, node in enumerate(to_run_interactive): |
|
2380 | 2380 | mod = ast.Interactive([node]) |
|
2381 | 2381 | code = self.compile(mod, cell_name, "single") |
|
2382 | 2382 | if self.run_code(code): |
|
2383 | 2383 | return True |
|
2384 | 2384 | except: |
|
2385 | 2385 | # It's possible to have exceptions raised here, typically by |
|
2386 | 2386 | # compilation of odd code (such as a naked 'return' outside a |
|
2387 | 2387 | # function) that did parse but isn't valid. Typically the exception |
|
2388 | 2388 | # is a SyntaxError, but it's safest just to catch anything and show |
|
2389 | 2389 | # the user a traceback. |
|
2390 | 2390 | |
|
2391 | 2391 | # We do only one try/except outside the loop to minimize the impact |
|
2392 | 2392 | # on runtime, and also because if any node in the node list is |
|
2393 | 2393 | # broken, we should stop execution completely. |
|
2394 | 2394 | self.showtraceback() |
|
2395 | 2395 | |
|
2396 | 2396 | return False |
|
2397 | ||
|
2397 | ||
|
2398 | 2398 | def run_code(self, code_obj): |
|
2399 | 2399 | """Execute a code object. |
|
2400 | 2400 | |
|
2401 | 2401 | When an exception occurs, self.showtraceback() is called to display a |
|
2402 | 2402 | traceback. |
|
2403 | 2403 | |
|
2404 | 2404 | Parameters |
|
2405 | 2405 | ---------- |
|
2406 | 2406 | code_obj : code object |
|
2407 | 2407 | A compiled code object, to be executed |
|
2408 | 2408 | post_execute : bool [default: True] |
|
2409 | 2409 | whether to call post_execute hooks after this particular execution. |
|
2410 | 2410 | |
|
2411 | 2411 | Returns |
|
2412 | 2412 | ------- |
|
2413 | 2413 | False : successful execution. |
|
2414 | 2414 | True : an error occurred. |
|
2415 | 2415 | """ |
|
2416 | 2416 | |
|
2417 | 2417 | # Set our own excepthook in case the user code tries to call it |
|
2418 | 2418 | # directly, so that the IPython crash handler doesn't get triggered |
|
2419 | 2419 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
2420 | 2420 | |
|
2421 | 2421 | # we save the original sys.excepthook in the instance, in case config |
|
2422 | 2422 | # code (such as magics) needs access to it. |
|
2423 | 2423 | self.sys_excepthook = old_excepthook |
|
2424 | 2424 | outflag = 1 # happens in more places, so it's easier as default |
|
2425 | 2425 | try: |
|
2426 | 2426 | try: |
|
2427 | 2427 | self.hooks.pre_run_code_hook() |
|
2428 | 2428 | #rprint('Running code', repr(code_obj)) # dbg |
|
2429 | 2429 | exec code_obj in self.user_global_ns, self.user_ns |
|
2430 | 2430 | finally: |
|
2431 | 2431 | # Reset our crash handler in place |
|
2432 | 2432 | sys.excepthook = old_excepthook |
|
2433 | 2433 | except SystemExit: |
|
2434 | 2434 | self.showtraceback(exception_only=True) |
|
2435 | 2435 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1) |
|
2436 | 2436 | except self.custom_exceptions: |
|
2437 | 2437 | etype,value,tb = sys.exc_info() |
|
2438 | 2438 | self.CustomTB(etype,value,tb) |
|
2439 | 2439 | except: |
|
2440 | 2440 | self.showtraceback() |
|
2441 | 2441 | else: |
|
2442 | 2442 | outflag = 0 |
|
2443 | 2443 | if softspace(sys.stdout, 0): |
|
2444 | 2444 | |
|
2445 | 2445 | |
|
2446 | 2446 | return outflag |
|
2447 | ||
|
2447 | ||
|
2448 | 2448 | # For backwards compatibility |
|
2449 | 2449 | runcode = run_code |
|
2450 | 2450 | |
|
2451 | 2451 | #------------------------------------------------------------------------- |
|
2452 | 2452 | # Things related to GUI support and pylab |
|
2453 | 2453 | #------------------------------------------------------------------------- |
|
2454 | 2454 | |
|
2455 | 2455 | def enable_pylab(self, gui=None, import_all=True): |
|
2456 | 2456 | raise NotImplementedError('Implement enable_pylab in a subclass') |
|
2457 | 2457 | |
|
2458 | 2458 | #------------------------------------------------------------------------- |
|
2459 | 2459 | # Utilities |
|
2460 | 2460 | #------------------------------------------------------------------------- |
|
2461 | 2461 | |
|
2462 | 2462 | def var_expand(self,cmd,depth=0): |
|
2463 | 2463 | """Expand python variables in a string. |
|
2464 | 2464 | |
|
2465 | 2465 | The depth argument indicates how many frames above the caller should |
|
2466 | 2466 | be walked to look for the local namespace where to expand variables. |
|
2467 | 2467 | |
|
2468 | 2468 | The global namespace for expansion is always the user's interactive |
|
2469 | 2469 | namespace. |
|
2470 | 2470 | """ |
|
2471 | 2471 | res = ItplNS(cmd, self.user_ns, # globals |
|
2472 | 2472 | # Skip our own frame in searching for locals: |
|
2473 | 2473 | sys._getframe(depth+1).f_locals # locals |
|
2474 | 2474 | ) |
|
2475 | 2475 | return py3compat.str_to_unicode(str(res), res.codec) |
|
2476 | 2476 | |
|
2477 | 2477 | def mktempfile(self, data=None, prefix='ipython_edit_'): |
|
2478 | 2478 | """Make a new tempfile and return its filename. |
|
2479 | 2479 | |
|
2480 | 2480 | This makes a call to tempfile.mktemp, but it registers the created |
|
2481 | 2481 | filename internally so ipython cleans it up at exit time. |
|
2482 | 2482 | |
|
2483 | 2483 | Optional inputs: |
|
2484 | 2484 | |
|
2485 | 2485 | - data(None): if data is given, it gets written out to the temp file |
|
2486 | 2486 | immediately, and the file is closed again.""" |
|
2487 | 2487 | |
|
2488 | 2488 | filename = tempfile.mktemp('.py', prefix) |
|
2489 | 2489 | self.tempfiles.append(filename) |
|
2490 | ||
|
2490 | ||
|
2491 | 2491 | if data: |
|
2492 | 2492 | tmp_file = open(filename,'w') |
|
2493 | 2493 | tmp_file.write(data) |
|
2494 | 2494 | tmp_file.close() |
|
2495 | 2495 | return filename |
|
2496 | 2496 | |
|
2497 | 2497 | # TODO: This should be removed when Term is refactored. |
|
2498 | 2498 | def write(self,data): |
|
2499 | 2499 | """Write a string to the default output""" |
|
2500 | 2500 | io.stdout.write(data) |
|
2501 | 2501 | |
|
2502 | 2502 | # TODO: This should be removed when Term is refactored. |
|
2503 | 2503 | def write_err(self,data): |
|
2504 | 2504 | """Write a string to the default error output""" |
|
2505 | 2505 | io.stderr.write(data) |
|
2506 | 2506 | |
|
2507 | 2507 | def ask_yes_no(self,prompt,default=True): |
|
2508 | 2508 | if self.quiet: |
|
2509 | 2509 | return True |
|
2510 | 2510 | return ask_yes_no(prompt,default) |
|
2511 | ||
|
2511 | ||
|
2512 | 2512 | def show_usage(self): |
|
2513 | 2513 | """Show a usage message""" |
|
2514 | 2514 | page.page(IPython.core.usage.interactive_usage) |
|
2515 | ||
|
2515 | ||
|
2516 | 2516 | def find_user_code(self, target, raw=True): |
|
2517 | 2517 | """Get a code string from history, file, or a string or macro. |
|
2518 | ||
|
2519 |
This is mainly used by magic functions. |
|
|
2520 | ||
|
2518 | ||
|
2519 | This is mainly used by magic functions. | |
|
2520 | ||
|
2521 | 2521 | Parameters |
|
2522 | 2522 | ---------- |
|
2523 | 2523 | target : str |
|
2524 | 2524 | A string specifying code to retrieve. This will be tried respectively |
|
2525 | 2525 | as: ranges of input history (see %history for syntax), a filename, or |
|
2526 | 2526 | an expression evaluating to a string or Macro in the user namespace. |
|
2527 | 2527 | raw : bool |
|
2528 | 2528 | If true (default), retrieve raw history. Has no effect on the other |
|
2529 | 2529 | retrieval mechanisms. |
|
2530 | ||
|
2530 | ||
|
2531 | 2531 | Returns |
|
2532 | 2532 | ------- |
|
2533 | 2533 | A string of code. |
|
2534 | ||
|
2534 | ||
|
2535 | 2535 | ValueError is raised if nothing is found, and TypeError if it evaluates |
|
2536 | 2536 | to an object of another type. In each case, .args[0] is a printable |
|
2537 | 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 | 2540 | if code: |
|
2541 | 2541 | return code |
|
2542 | 2542 | if os.path.isfile(target): # Read file |
|
2543 | 2543 | return open(target, "r").read() |
|
2544 | ||
|
2544 | ||
|
2545 | 2545 | try: # User namespace |
|
2546 | 2546 | codeobj = eval(target, self.user_ns) |
|
2547 | 2547 | except Exception: |
|
2548 | 2548 | raise ValueError(("'%s' was not found in history, as a file, nor in" |
|
2549 | 2549 | " the user namespace.") % target) |
|
2550 | 2550 | if isinstance(codeobj, basestring): |
|
2551 | 2551 | return codeobj |
|
2552 | 2552 | elif isinstance(codeobj, Macro): |
|
2553 | 2553 | return codeobj.value |
|
2554 | ||
|
2554 | ||
|
2555 | 2555 | raise TypeError("%s is neither a string nor a macro." % target, |
|
2556 | 2556 | codeobj) |
|
2557 | 2557 | |
|
2558 | 2558 | #------------------------------------------------------------------------- |
|
2559 | 2559 | # Things related to IPython exiting |
|
2560 | 2560 | #------------------------------------------------------------------------- |
|
2561 | 2561 | def atexit_operations(self): |
|
2562 | 2562 | """This will be executed at the time of exit. |
|
2563 | 2563 | |
|
2564 | 2564 | Cleanup operations and saving of persistent data that is done |
|
2565 | 2565 | unconditionally by IPython should be performed here. |
|
2566 | 2566 | |
|
2567 | 2567 | For things that may depend on startup flags or platform specifics (such |
|
2568 | 2568 | as having readline or not), register a separate atexit function in the |
|
2569 | 2569 | code that has the appropriate information, rather than trying to |
|
2570 |
clutter |
|
|
2570 | clutter | |
|
2571 | 2571 | """ |
|
2572 | 2572 | # Close the history session (this stores the end time and line count) |
|
2573 | 2573 | # this must be *before* the tempfile cleanup, in case of temporary |
|
2574 | 2574 | # history db |
|
2575 | 2575 | self.history_manager.end_session() |
|
2576 | ||
|
2576 | ||
|
2577 | 2577 | # Cleanup all tempfiles left around |
|
2578 | 2578 | for tfile in self.tempfiles: |
|
2579 | 2579 | try: |
|
2580 | 2580 | os.unlink(tfile) |
|
2581 | 2581 | except OSError: |
|
2582 | 2582 | pass |
|
2583 | ||
|
2583 | ||
|
2584 | 2584 | # Clear all user namespaces to release all references cleanly. |
|
2585 | 2585 | self.reset(new_session=False) |
|
2586 | 2586 | |
|
2587 | 2587 | # Run user hooks |
|
2588 | 2588 | self.hooks.shutdown_hook() |
|
2589 | 2589 | |
|
2590 | 2590 | def cleanup(self): |
|
2591 | 2591 | self.restore_sys_module_state() |
|
2592 | 2592 | |
|
2593 | 2593 | |
|
2594 | 2594 | class InteractiveShellABC(object): |
|
2595 | 2595 | """An abstract base class for InteractiveShell.""" |
|
2596 | 2596 | __metaclass__ = abc.ABCMeta |
|
2597 | 2597 | |
|
2598 | 2598 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,217 +1,217 b'' | |||
|
1 | 1 | """Logger class for IPython's logging facilities. |
|
2 | 2 | """ |
|
3 | 3 | |
|
4 | 4 | #***************************************************************************** |
|
5 | 5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
6 | 6 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | 9 | # the file COPYING, distributed as part of this software. |
|
10 | 10 | #***************************************************************************** |
|
11 | 11 | |
|
12 | 12 | #**************************************************************************** |
|
13 | 13 | # Modules and globals |
|
14 | 14 | |
|
15 | 15 | # Python standard modules |
|
16 | 16 | import glob |
|
17 | 17 | import os |
|
18 | 18 | import time |
|
19 | 19 | |
|
20 | 20 | #**************************************************************************** |
|
21 | 21 | # FIXME: This class isn't a mixin anymore, but it still needs attributes from |
|
22 | 22 | # ipython and does input cache management. Finish cleanup later... |
|
23 | 23 | |
|
24 | 24 | class Logger(object): |
|
25 | 25 | """A Logfile class with different policies for file creation""" |
|
26 | 26 | |
|
27 | 27 | def __init__(self, home_dir, logfname='Logger.log', loghead='', |
|
28 | 28 | logmode='over'): |
|
29 | 29 | |
|
30 | 30 | # this is the full ipython instance, we need some attributes from it |
|
31 | 31 | # which won't exist until later. What a mess, clean up later... |
|
32 | 32 | self.home_dir = home_dir |
|
33 | 33 | |
|
34 | 34 | self.logfname = logfname |
|
35 | 35 | self.loghead = loghead |
|
36 | 36 | self.logmode = logmode |
|
37 | 37 | self.logfile = None |
|
38 | 38 | |
|
39 | 39 | # Whether to log raw or processed input |
|
40 | 40 | self.log_raw_input = False |
|
41 | 41 | |
|
42 | 42 | # whether to also log output |
|
43 | 43 | self.log_output = False |
|
44 | 44 | |
|
45 | 45 | # whether to put timestamps before each log entry |
|
46 | 46 | self.timestamp = False |
|
47 | 47 | |
|
48 | 48 | # activity control flags |
|
49 | 49 | self.log_active = False |
|
50 | 50 | |
|
51 | 51 | # logmode is a validated property |
|
52 | 52 | def _set_mode(self,mode): |
|
53 | 53 | if mode not in ['append','backup','global','over','rotate']: |
|
54 | 54 | raise ValueError,'invalid log mode %s given' % mode |
|
55 | 55 | self._logmode = mode |
|
56 | 56 | |
|
57 | 57 | def _get_mode(self): |
|
58 | 58 | return self._logmode |
|
59 | 59 | |
|
60 | 60 | logmode = property(_get_mode,_set_mode) |
|
61 | ||
|
61 | ||
|
62 | 62 | def logstart(self,logfname=None,loghead=None,logmode=None, |
|
63 | 63 | log_output=False,timestamp=False,log_raw_input=False): |
|
64 | 64 | """Generate a new log-file with a default header. |
|
65 | 65 | |
|
66 | 66 | Raises RuntimeError if the log has already been started""" |
|
67 | 67 | |
|
68 | 68 | if self.logfile is not None: |
|
69 | 69 | raise RuntimeError('Log file is already active: %s' % |
|
70 | 70 | self.logfname) |
|
71 | ||
|
71 | ||
|
72 | 72 | # The parameters can override constructor defaults |
|
73 | 73 | if logfname is not None: self.logfname = logfname |
|
74 | 74 | if loghead is not None: self.loghead = loghead |
|
75 | 75 | if logmode is not None: self.logmode = logmode |
|
76 | 76 | |
|
77 | 77 | # Parameters not part of the constructor |
|
78 | 78 | self.timestamp = timestamp |
|
79 | 79 | self.log_output = log_output |
|
80 | 80 | self.log_raw_input = log_raw_input |
|
81 | ||
|
81 | ||
|
82 | 82 | # init depending on the log mode requested |
|
83 | 83 | isfile = os.path.isfile |
|
84 | 84 | logmode = self.logmode |
|
85 | 85 | |
|
86 | 86 | if logmode == 'append': |
|
87 | 87 | self.logfile = open(self.logfname,'a') |
|
88 | 88 | |
|
89 | 89 | elif logmode == 'backup': |
|
90 | 90 | if isfile(self.logfname): |
|
91 | 91 | backup_logname = self.logfname+'~' |
|
92 | 92 | # Manually remove any old backup, since os.rename may fail |
|
93 | 93 | # under Windows. |
|
94 | 94 | if isfile(backup_logname): |
|
95 | 95 | os.remove(backup_logname) |
|
96 | 96 | os.rename(self.logfname,backup_logname) |
|
97 | 97 | self.logfile = open(self.logfname,'w') |
|
98 | 98 | |
|
99 | 99 | elif logmode == 'global': |
|
100 | 100 | self.logfname = os.path.join(self.home_dir,self.logfname) |
|
101 | 101 | self.logfile = open(self.logfname, 'a') |
|
102 | 102 | |
|
103 | 103 | elif logmode == 'over': |
|
104 | 104 | if isfile(self.logfname): |
|
105 |
os.remove(self.logfname) |
|
|
105 | os.remove(self.logfname) | |
|
106 | 106 | self.logfile = open(self.logfname,'w') |
|
107 | 107 | |
|
108 | 108 | elif logmode == 'rotate': |
|
109 | 109 | if isfile(self.logfname): |
|
110 |
if isfile(self.logfname+'.001~'): |
|
|
110 | if isfile(self.logfname+'.001~'): | |
|
111 | 111 | old = glob.glob(self.logfname+'.*~') |
|
112 | 112 | old.sort() |
|
113 | 113 | old.reverse() |
|
114 | 114 | for f in old: |
|
115 | 115 | root, ext = os.path.splitext(f) |
|
116 | 116 | num = int(ext[1:-1])+1 |
|
117 | 117 | os.rename(f, root+'.'+`num`.zfill(3)+'~') |
|
118 | 118 | os.rename(self.logfname, self.logfname+'.001~') |
|
119 | 119 | self.logfile = open(self.logfname,'w') |
|
120 | ||
|
120 | ||
|
121 | 121 | if logmode != 'append': |
|
122 | 122 | self.logfile.write(self.loghead) |
|
123 | 123 | |
|
124 | 124 | self.logfile.flush() |
|
125 | 125 | self.log_active = True |
|
126 | 126 | |
|
127 | 127 | def switch_log(self,val): |
|
128 | 128 | """Switch logging on/off. val should be ONLY a boolean.""" |
|
129 | 129 | |
|
130 | 130 | if val not in [False,True,0,1]: |
|
131 | 131 | raise ValueError, \ |
|
132 | 132 | 'Call switch_log ONLY with a boolean argument, not with:',val |
|
133 | ||
|
133 | ||
|
134 | 134 | label = {0:'OFF',1:'ON',False:'OFF',True:'ON'} |
|
135 | 135 | |
|
136 | 136 | if self.logfile is None: |
|
137 | 137 | print """ |
|
138 | 138 | Logging hasn't been started yet (use logstart for that). |
|
139 | 139 | |
|
140 | 140 | %logon/%logoff are for temporarily starting and stopping logging for a logfile |
|
141 | 141 | which already exists. But you must first start the logging process with |
|
142 | 142 | %logstart (optionally giving a logfile name).""" |
|
143 | ||
|
143 | ||
|
144 | 144 | else: |
|
145 | 145 | if self.log_active == val: |
|
146 | 146 | print 'Logging is already',label[val] |
|
147 | 147 | else: |
|
148 | 148 | print 'Switching logging',label[val] |
|
149 | 149 | self.log_active = not self.log_active |
|
150 | 150 | self.log_active_out = self.log_active |
|
151 | 151 | |
|
152 | 152 | def logstate(self): |
|
153 | 153 | """Print a status message about the logger.""" |
|
154 | 154 | if self.logfile is None: |
|
155 | 155 | print 'Logging has not been activated.' |
|
156 | 156 | else: |
|
157 | 157 | state = self.log_active and 'active' or 'temporarily suspended' |
|
158 | 158 | print 'Filename :',self.logfname |
|
159 | 159 | print 'Mode :',self.logmode |
|
160 | 160 | print 'Output logging :',self.log_output |
|
161 | 161 | print 'Raw input log :',self.log_raw_input |
|
162 | 162 | print 'Timestamping :',self.timestamp |
|
163 | 163 | print 'State :',state |
|
164 | 164 | |
|
165 | 165 | def log(self, line_mod, line_ori): |
|
166 | 166 | """Write the sources to a log. |
|
167 | 167 | |
|
168 | 168 | Inputs: |
|
169 | 169 | |
|
170 | 170 | - line_mod: possibly modified input, such as the transformations made |
|
171 | 171 | by input prefilters or input handlers of various kinds. This should |
|
172 | 172 | always be valid Python. |
|
173 | 173 | |
|
174 | 174 | - line_ori: unmodified input line from the user. This is not |
|
175 | 175 | necessarily valid Python. |
|
176 | 176 | """ |
|
177 | 177 | |
|
178 | 178 | # Write the log line, but decide which one according to the |
|
179 | 179 | # log_raw_input flag, set when the log is started. |
|
180 | 180 | if self.log_raw_input: |
|
181 | 181 | self.log_write(line_ori) |
|
182 | 182 | else: |
|
183 | 183 | self.log_write(line_mod) |
|
184 | 184 | |
|
185 | 185 | def log_write(self, data, kind='input'): |
|
186 | 186 | """Write data to the log file, if active""" |
|
187 | 187 | |
|
188 | 188 | #print 'data: %r' % data # dbg |
|
189 | 189 | if self.log_active and data: |
|
190 | 190 | write = self.logfile.write |
|
191 | 191 | if kind=='input': |
|
192 | 192 | if self.timestamp: |
|
193 | 193 | write(time.strftime('# %a, %d %b %Y %H:%M:%S\n', |
|
194 | 194 | time.localtime())) |
|
195 | 195 | write(data) |
|
196 | 196 | elif kind=='output' and self.log_output: |
|
197 | 197 | odata = '\n'.join(['#[Out]# %s' % s |
|
198 | 198 | for s in data.splitlines()]) |
|
199 | 199 | write('%s\n' % odata) |
|
200 | 200 | self.logfile.flush() |
|
201 | 201 | |
|
202 | 202 | def logstop(self): |
|
203 | 203 | """Fully stop logging and close log file. |
|
204 | 204 | |
|
205 | 205 | In order to start logging again, a new logstart() call needs to be |
|
206 | 206 | made, possibly (though not necessarily) with a new filename, mode and |
|
207 | 207 | other options.""" |
|
208 | ||
|
208 | ||
|
209 | 209 | if self.logfile is not None: |
|
210 | 210 | self.logfile.close() |
|
211 | 211 | self.logfile = None |
|
212 | 212 | else: |
|
213 | 213 | print "Logging hadn't been started." |
|
214 | 214 | self.log_active = False |
|
215 | 215 | |
|
216 | 216 | # For backwards compatibility, in case anyone was using this. |
|
217 | 217 | close_log = logstop |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now