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