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