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