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