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