Show More
@@ -1,396 +1,395 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # encoding: utf-8 |
|
3 | 3 | """ |
|
4 | 4 | The :class:`~IPython.core.application.Application` object for the command |
|
5 | 5 | line :command:`ipython` program. |
|
6 | 6 | |
|
7 | 7 | Authors |
|
8 | 8 | ------- |
|
9 | 9 | |
|
10 | 10 | * Brian Granger |
|
11 | 11 | * Fernando Perez |
|
12 | 12 | * Min Ragan-Kelley |
|
13 | 13 | """ |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Copyright (C) 2008-2011 The IPython Development Team |
|
17 | 17 | # |
|
18 | 18 | # Distributed under the terms of the BSD License. The full license is in |
|
19 | 19 | # the file COPYING, distributed as part of this software. |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | # Imports |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | |
|
26 | 26 | from __future__ import absolute_import |
|
27 | 27 | from __future__ import print_function |
|
28 | 28 | |
|
29 | 29 | import logging |
|
30 | 30 | import os |
|
31 | 31 | import sys |
|
32 | 32 | |
|
33 | 33 | from IPython.config.loader import Config |
|
34 | 34 | from IPython.config.application import boolean_flag, catch_config_error, Application |
|
35 | 35 | from IPython.core import release |
|
36 | 36 | from IPython.core import usage |
|
37 | 37 | from IPython.core.completer import IPCompleter |
|
38 | 38 | from IPython.core.crashhandler import CrashHandler |
|
39 | 39 | from IPython.core.formatters import PlainTextFormatter |
|
40 | 40 | from IPython.core.history import HistoryManager |
|
41 | 41 | from IPython.core.prompts import PromptManager |
|
42 | 42 | from IPython.core.application import ( |
|
43 | 43 | ProfileDir, BaseIPythonApplication, base_flags, base_aliases |
|
44 | 44 | ) |
|
45 | 45 | from IPython.core.magics import ScriptMagics |
|
46 | 46 | from IPython.core.shellapp import ( |
|
47 | 47 | InteractiveShellApp, shell_flags, shell_aliases |
|
48 | 48 | ) |
|
49 | 49 | from IPython.extensions.storemagic import StoreMagics |
|
50 | 50 | from IPython.terminal.interactiveshell import TerminalInteractiveShell |
|
51 | 51 | from IPython.utils import warn |
|
52 | 52 | from IPython.utils.path import get_ipython_dir, check_for_old_config |
|
53 | 53 | from IPython.utils.traitlets import ( |
|
54 | 54 | Bool, List, Dict, |
|
55 | 55 | ) |
|
56 | 56 | |
|
57 | 57 | #----------------------------------------------------------------------------- |
|
58 | 58 | # Globals, utilities and helpers |
|
59 | 59 | #----------------------------------------------------------------------------- |
|
60 | 60 | |
|
61 | 61 | _examples = """ |
|
62 | 62 | ipython --matplotlib # enable matplotlib integration |
|
63 | 63 | ipython --matplotlib=qt # enable matplotlib integration with qt4 backend |
|
64 | 64 | |
|
65 | 65 | ipython --log-level=DEBUG # set logging to DEBUG |
|
66 | 66 | ipython --profile=foo # start with profile foo |
|
67 | 67 | |
|
68 | 68 | ipython qtconsole # start the qtconsole GUI application |
|
69 | 69 | ipython help qtconsole # show the help for the qtconsole subcmd |
|
70 | 70 | |
|
71 | 71 | ipython console # start the terminal-based console application |
|
72 | 72 | ipython help console # show the help for the console subcmd |
|
73 | 73 | |
|
74 | 74 | ipython notebook # start the IPython notebook |
|
75 | 75 | ipython help notebook # show the help for the notebook subcmd |
|
76 | 76 | |
|
77 | 77 | ipython profile create foo # create profile foo w/ default config files |
|
78 | 78 | ipython help profile # show the help for the profile subcmd |
|
79 | 79 | |
|
80 | 80 | ipython locate # print the path to the IPython directory |
|
81 | 81 | ipython locate profile foo # print the path to the directory for profile `foo` |
|
82 | 82 | |
|
83 | 83 | ipython nbconvert # convert notebooks to/from other formats |
|
84 | 84 | """ |
|
85 | 85 | |
|
86 | 86 | #----------------------------------------------------------------------------- |
|
87 | 87 | # Crash handler for this application |
|
88 | 88 | #----------------------------------------------------------------------------- |
|
89 | 89 | |
|
90 | 90 | class IPAppCrashHandler(CrashHandler): |
|
91 | 91 | """sys.excepthook for IPython itself, leaves a detailed report on disk.""" |
|
92 | 92 | |
|
93 | 93 | def __init__(self, app): |
|
94 | 94 | contact_name = release.author |
|
95 | 95 | contact_email = release.author_email |
|
96 | 96 | bug_tracker = 'https://github.com/ipython/ipython/issues' |
|
97 | 97 | super(IPAppCrashHandler,self).__init__( |
|
98 | 98 | app, contact_name, contact_email, bug_tracker |
|
99 | 99 | ) |
|
100 | 100 | |
|
101 | 101 | def make_report(self,traceback): |
|
102 | 102 | """Return a string containing a crash report.""" |
|
103 | 103 | |
|
104 | 104 | sec_sep = self.section_sep |
|
105 | 105 | # Start with parent report |
|
106 | 106 | report = [super(IPAppCrashHandler, self).make_report(traceback)] |
|
107 | 107 | # Add interactive-specific info we may have |
|
108 | 108 | rpt_add = report.append |
|
109 | 109 | try: |
|
110 | 110 | rpt_add(sec_sep+"History of session input:") |
|
111 | 111 | for line in self.app.shell.user_ns['_ih']: |
|
112 | 112 | rpt_add(line) |
|
113 | 113 | rpt_add('\n*** Last line of input (may not be in above history):\n') |
|
114 | 114 | rpt_add(self.app.shell._last_input_line+'\n') |
|
115 | 115 | except: |
|
116 | 116 | pass |
|
117 | 117 | |
|
118 | 118 | return ''.join(report) |
|
119 | 119 | |
|
120 | 120 | #----------------------------------------------------------------------------- |
|
121 | 121 | # Aliases and Flags |
|
122 | 122 | #----------------------------------------------------------------------------- |
|
123 | 123 | flags = dict(base_flags) |
|
124 | 124 | flags.update(shell_flags) |
|
125 | 125 | frontend_flags = {} |
|
126 | 126 | addflag = lambda *args: frontend_flags.update(boolean_flag(*args)) |
|
127 | 127 | addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax', |
|
128 | 128 | 'Turn on auto editing of files with syntax errors.', |
|
129 | 129 | 'Turn off auto editing of files with syntax errors.' |
|
130 | 130 | ) |
|
131 | 131 | addflag('banner', 'TerminalIPythonApp.display_banner', |
|
132 | 132 | "Display a banner upon starting IPython.", |
|
133 | 133 | "Don't display a banner upon starting IPython." |
|
134 | 134 | ) |
|
135 | 135 | addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit', |
|
136 | 136 | """Set to confirm when you try to exit IPython with an EOF (Control-D |
|
137 | 137 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', |
|
138 | 138 | you can force a direct exit without any confirmation.""", |
|
139 | 139 | "Don't prompt the user when exiting." |
|
140 | 140 | ) |
|
141 | 141 | addflag('term-title', 'TerminalInteractiveShell.term_title', |
|
142 | 142 | "Enable auto setting the terminal title.", |
|
143 | 143 | "Disable auto setting the terminal title." |
|
144 | 144 | ) |
|
145 | 145 | classic_config = Config() |
|
146 | 146 | classic_config.InteractiveShell.cache_size = 0 |
|
147 | 147 | classic_config.PlainTextFormatter.pprint = False |
|
148 | 148 | classic_config.PromptManager.in_template = '>>> ' |
|
149 | 149 | classic_config.PromptManager.in2_template = '... ' |
|
150 | 150 | classic_config.PromptManager.out_template = '' |
|
151 | 151 | classic_config.InteractiveShell.separate_in = '' |
|
152 | 152 | classic_config.InteractiveShell.separate_out = '' |
|
153 | 153 | classic_config.InteractiveShell.separate_out2 = '' |
|
154 | 154 | classic_config.InteractiveShell.colors = 'NoColor' |
|
155 | 155 | classic_config.InteractiveShell.xmode = 'Plain' |
|
156 | 156 | |
|
157 | 157 | frontend_flags['classic']=( |
|
158 | 158 | classic_config, |
|
159 | 159 | "Gives IPython a similar feel to the classic Python prompt." |
|
160 | 160 | ) |
|
161 | 161 | # # log doesn't make so much sense this way anymore |
|
162 | 162 | # paa('--log','-l', |
|
163 | 163 | # action='store_true', dest='InteractiveShell.logstart', |
|
164 | 164 | # help="Start logging to the default log file (./ipython_log.py).") |
|
165 | 165 | # |
|
166 | 166 | # # quick is harder to implement |
|
167 | 167 | frontend_flags['quick']=( |
|
168 | 168 | {'TerminalIPythonApp' : {'quick' : True}}, |
|
169 | 169 | "Enable quick startup with no config files." |
|
170 | 170 | ) |
|
171 | 171 | |
|
172 | 172 | frontend_flags['i'] = ( |
|
173 | 173 | {'TerminalIPythonApp' : {'force_interact' : True}}, |
|
174 | """If running code from the command line, become interactive afterwards. | |
|
175 | Note: can also be given simply as '-i'.""" | |
|
174 | """If running code from the command line, become interactive afterwards.""" | |
|
176 | 175 | ) |
|
177 | 176 | flags.update(frontend_flags) |
|
178 | 177 | |
|
179 | 178 | aliases = dict(base_aliases) |
|
180 | 179 | aliases.update(shell_aliases) |
|
181 | 180 | |
|
182 | 181 | #----------------------------------------------------------------------------- |
|
183 | 182 | # Main classes and functions |
|
184 | 183 | #----------------------------------------------------------------------------- |
|
185 | 184 | |
|
186 | 185 | |
|
187 | 186 | class LocateIPythonApp(BaseIPythonApplication): |
|
188 | 187 | description = """print the path to the IPython dir""" |
|
189 | 188 | subcommands = Dict(dict( |
|
190 | 189 | profile=('IPython.core.profileapp.ProfileLocate', |
|
191 | 190 | "print the path to an IPython profile directory", |
|
192 | 191 | ), |
|
193 | 192 | )) |
|
194 | 193 | def start(self): |
|
195 | 194 | if self.subapp is not None: |
|
196 | 195 | return self.subapp.start() |
|
197 | 196 | else: |
|
198 | 197 | print(self.ipython_dir) |
|
199 | 198 | |
|
200 | 199 | |
|
201 | 200 | class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp): |
|
202 | 201 | name = u'ipython' |
|
203 | 202 | description = usage.cl_usage |
|
204 | 203 | crash_handler_class = IPAppCrashHandler |
|
205 | 204 | examples = _examples |
|
206 | 205 | |
|
207 | 206 | flags = Dict(flags) |
|
208 | 207 | aliases = Dict(aliases) |
|
209 | 208 | classes = List() |
|
210 | 209 | def _classes_default(self): |
|
211 | 210 | """This has to be in a method, for TerminalIPythonApp to be available.""" |
|
212 | 211 | return [ |
|
213 | 212 | InteractiveShellApp, # ShellApp comes before TerminalApp, because |
|
214 | 213 | self.__class__, # it will also affect subclasses (e.g. QtConsole) |
|
215 | 214 | TerminalInteractiveShell, |
|
216 | 215 | PromptManager, |
|
217 | 216 | HistoryManager, |
|
218 | 217 | ProfileDir, |
|
219 | 218 | PlainTextFormatter, |
|
220 | 219 | IPCompleter, |
|
221 | 220 | ScriptMagics, |
|
222 | 221 | StoreMagics, |
|
223 | 222 | ] |
|
224 | 223 | |
|
225 | 224 | subcommands = dict( |
|
226 | 225 | qtconsole=('IPython.qt.console.qtconsoleapp.IPythonQtConsoleApp', |
|
227 | 226 | """Launch the IPython Qt Console.""" |
|
228 | 227 | ), |
|
229 | 228 | notebook=('IPython.html.notebookapp.NotebookApp', |
|
230 | 229 | """Launch the IPython HTML Notebook Server.""" |
|
231 | 230 | ), |
|
232 | 231 | profile = ("IPython.core.profileapp.ProfileApp", |
|
233 | 232 | "Create and manage IPython profiles." |
|
234 | 233 | ), |
|
235 | 234 | kernel = ("IPython.kernel.zmq.kernelapp.IPKernelApp", |
|
236 | 235 | "Start a kernel without an attached frontend." |
|
237 | 236 | ), |
|
238 | 237 | console=('IPython.terminal.console.app.ZMQTerminalIPythonApp', |
|
239 | 238 | """Launch the IPython terminal-based Console.""" |
|
240 | 239 | ), |
|
241 | 240 | locate=('IPython.terminal.ipapp.LocateIPythonApp', |
|
242 | 241 | LocateIPythonApp.description |
|
243 | 242 | ), |
|
244 | 243 | history=('IPython.core.historyapp.HistoryApp', |
|
245 | 244 | "Manage the IPython history database." |
|
246 | 245 | ), |
|
247 | 246 | nbconvert=('IPython.nbconvert.nbconvertapp.NbConvertApp', |
|
248 | 247 | "Convert notebooks to/from other formats." |
|
249 | 248 | ), |
|
250 | 249 | trust=('IPython.nbformat.sign.TrustNotebookApp', |
|
251 | 250 | "Sign notebooks to trust their potentially unsafe contents at load." |
|
252 | 251 | ), |
|
253 | 252 | kernelspec=('IPython.kernel.kernelspecapp.KernelSpecApp', |
|
254 | 253 | "Manage IPython kernel specifications." |
|
255 | 254 | ), |
|
256 | 255 | ) |
|
257 | 256 | subcommands['install-nbextension'] = ( |
|
258 | 257 | "IPython.html.nbextensions.NBExtensionApp", |
|
259 | 258 | "Install IPython notebook extension files" |
|
260 | 259 | ) |
|
261 | 260 | |
|
262 | 261 | # *do* autocreate requested profile, but don't create the config file. |
|
263 | 262 | auto_create=Bool(True) |
|
264 | 263 | # configurables |
|
265 | 264 | ignore_old_config=Bool(False, config=True, |
|
266 | 265 | help="Suppress warning messages about legacy config files" |
|
267 | 266 | ) |
|
268 | 267 | quick = Bool(False, config=True, |
|
269 | 268 | help="""Start IPython quickly by skipping the loading of config files.""" |
|
270 | 269 | ) |
|
271 | 270 | def _quick_changed(self, name, old, new): |
|
272 | 271 | if new: |
|
273 | 272 | self.load_config_file = lambda *a, **kw: None |
|
274 | 273 | self.ignore_old_config=True |
|
275 | 274 | |
|
276 | 275 | display_banner = Bool(True, config=True, |
|
277 | 276 | help="Whether to display a banner upon starting IPython." |
|
278 | 277 | ) |
|
279 | 278 | |
|
280 | 279 | # if there is code of files to run from the cmd line, don't interact |
|
281 | 280 | # unless the --i flag (App.force_interact) is true. |
|
282 | 281 | force_interact = Bool(False, config=True, |
|
283 | 282 | help="""If a command or file is given via the command-line, |
|
284 | 283 | e.g. 'ipython foo.py', start an interactive shell after executing the |
|
285 | 284 | file or command.""" |
|
286 | 285 | ) |
|
287 | 286 | def _force_interact_changed(self, name, old, new): |
|
288 | 287 | if new: |
|
289 | 288 | self.interact = True |
|
290 | 289 | |
|
291 | 290 | def _file_to_run_changed(self, name, old, new): |
|
292 | 291 | if new: |
|
293 | 292 | self.something_to_run = True |
|
294 | 293 | if new and not self.force_interact: |
|
295 | 294 | self.interact = False |
|
296 | 295 | _code_to_run_changed = _file_to_run_changed |
|
297 | 296 | _module_to_run_changed = _file_to_run_changed |
|
298 | 297 | |
|
299 | 298 | # internal, not-configurable |
|
300 | 299 | interact=Bool(True) |
|
301 | 300 | something_to_run=Bool(False) |
|
302 | 301 | |
|
303 | 302 | def parse_command_line(self, argv=None): |
|
304 | 303 | """override to allow old '-pylab' flag with deprecation warning""" |
|
305 | 304 | |
|
306 | 305 | argv = sys.argv[1:] if argv is None else argv |
|
307 | 306 | |
|
308 | 307 | if '-pylab' in argv: |
|
309 | 308 | # deprecated `-pylab` given, |
|
310 | 309 | # warn and transform into current syntax |
|
311 | 310 | argv = argv[:] # copy, don't clobber |
|
312 | 311 | idx = argv.index('-pylab') |
|
313 | 312 | warn.warn("`-pylab` flag has been deprecated.\n" |
|
314 | 313 | " Use `--matplotlib <backend>` and import pylab manually.") |
|
315 | 314 | argv[idx] = '--pylab' |
|
316 | 315 | |
|
317 | 316 | return super(TerminalIPythonApp, self).parse_command_line(argv) |
|
318 | 317 | |
|
319 | 318 | @catch_config_error |
|
320 | 319 | def initialize(self, argv=None): |
|
321 | 320 | """Do actions after construct, but before starting the app.""" |
|
322 | 321 | super(TerminalIPythonApp, self).initialize(argv) |
|
323 | 322 | if self.subapp is not None: |
|
324 | 323 | # don't bother initializing further, starting subapp |
|
325 | 324 | return |
|
326 | 325 | if not self.ignore_old_config: |
|
327 | 326 | check_for_old_config(self.ipython_dir) |
|
328 | 327 | # print self.extra_args |
|
329 | 328 | if self.extra_args and not self.something_to_run: |
|
330 | 329 | self.file_to_run = self.extra_args[0] |
|
331 | 330 | self.init_path() |
|
332 | 331 | # create the shell |
|
333 | 332 | self.init_shell() |
|
334 | 333 | # and draw the banner |
|
335 | 334 | self.init_banner() |
|
336 | 335 | # Now a variety of things that happen after the banner is printed. |
|
337 | 336 | self.init_gui_pylab() |
|
338 | 337 | self.init_extensions() |
|
339 | 338 | self.init_code() |
|
340 | 339 | |
|
341 | 340 | def init_shell(self): |
|
342 | 341 | """initialize the InteractiveShell instance""" |
|
343 | 342 | # Create an InteractiveShell instance. |
|
344 | 343 | # shell.display_banner should always be False for the terminal |
|
345 | 344 | # based app, because we call shell.show_banner() by hand below |
|
346 | 345 | # so the banner shows *before* all extension loading stuff. |
|
347 | 346 | self.shell = TerminalInteractiveShell.instance(parent=self, |
|
348 | 347 | display_banner=False, profile_dir=self.profile_dir, |
|
349 | 348 | ipython_dir=self.ipython_dir, user_ns=self.user_ns) |
|
350 | 349 | self.shell.configurables.append(self) |
|
351 | 350 | |
|
352 | 351 | def init_banner(self): |
|
353 | 352 | """optionally display the banner""" |
|
354 | 353 | if self.display_banner and self.interact: |
|
355 | 354 | self.shell.show_banner() |
|
356 | 355 | # Make sure there is a space below the banner. |
|
357 | 356 | if self.log_level <= logging.INFO: print() |
|
358 | 357 | |
|
359 | 358 | def _pylab_changed(self, name, old, new): |
|
360 | 359 | """Replace --pylab='inline' with --pylab='auto'""" |
|
361 | 360 | if new == 'inline': |
|
362 | 361 | warn.warn("'inline' not available as pylab backend, " |
|
363 | 362 | "using 'auto' instead.") |
|
364 | 363 | self.pylab = 'auto' |
|
365 | 364 | |
|
366 | 365 | def start(self): |
|
367 | 366 | if self.subapp is not None: |
|
368 | 367 | return self.subapp.start() |
|
369 | 368 | # perform any prexec steps: |
|
370 | 369 | if self.interact: |
|
371 | 370 | self.log.debug("Starting IPython's mainloop...") |
|
372 | 371 | self.shell.mainloop() |
|
373 | 372 | else: |
|
374 | 373 | self.log.debug("IPython not interactive...") |
|
375 | 374 | |
|
376 | 375 | def load_default_config(ipython_dir=None): |
|
377 | 376 | """Load the default config file from the default ipython_dir. |
|
378 | 377 | |
|
379 | 378 | This is useful for embedded shells. |
|
380 | 379 | """ |
|
381 | 380 | if ipython_dir is None: |
|
382 | 381 | ipython_dir = get_ipython_dir() |
|
383 | 382 | |
|
384 | 383 | profile_dir = os.path.join(ipython_dir, 'profile_default') |
|
385 | 384 | |
|
386 | 385 | config = Config() |
|
387 | 386 | for cf in Application._load_config_files("ipython_config", path=profile_dir): |
|
388 | 387 | config.update(cf) |
|
389 | 388 | |
|
390 | 389 | return config |
|
391 | 390 | |
|
392 | 391 | launch_new_instance = TerminalIPythonApp.launch_instance |
|
393 | 392 | |
|
394 | 393 | |
|
395 | 394 | if __name__ == '__main__': |
|
396 | 395 | launch_new_instance() |
General Comments 0
You need to be logged in to leave comments.
Login now