Show More
@@ -0,0 +1,243 b'' | |||||
|
1 | #!/usr/bin/env python | |||
|
2 | # encoding: utf-8 | |||
|
3 | """ | |||
|
4 | A mixin for :class:`~IPython.core.newapplication.Application` classes that | |||
|
5 | launch InteractiveShell instances, load extensions, etc. | |||
|
6 | ||||
|
7 | Authors | |||
|
8 | ------- | |||
|
9 | ||||
|
10 | * Min Ragan-Kelley | |||
|
11 | """ | |||
|
12 | ||||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | # Copyright (C) 2008-2011 The IPython Development Team | |||
|
15 | # | |||
|
16 | # Distributed under the terms of the BSD License. The full license is in | |||
|
17 | # the file COPYING, distributed as part of this software. | |||
|
18 | #----------------------------------------------------------------------------- | |||
|
19 | ||||
|
20 | #----------------------------------------------------------------------------- | |||
|
21 | # Imports | |||
|
22 | #----------------------------------------------------------------------------- | |||
|
23 | ||||
|
24 | from __future__ import absolute_import | |||
|
25 | ||||
|
26 | import os | |||
|
27 | import sys | |||
|
28 | ||||
|
29 | from IPython.config.application import boolean_flag | |||
|
30 | from IPython.config.configurable import Configurable | |||
|
31 | from IPython.utils.path import filefind | |||
|
32 | from IPython.utils.traitlets import Unicode, Instance, List | |||
|
33 | ||||
|
34 | #----------------------------------------------------------------------------- | |||
|
35 | # Aliases and Flags | |||
|
36 | #----------------------------------------------------------------------------- | |||
|
37 | ||||
|
38 | shell_flags = {} | |||
|
39 | ||||
|
40 | addflag = lambda *args: shell_flags.update(boolean_flag(*args)) | |||
|
41 | addflag('autoindent', 'InteractiveShell.autoindent', | |||
|
42 | 'Turn on autoindenting.', 'Turn off autoindenting.' | |||
|
43 | ) | |||
|
44 | addflag('automagic', 'InteractiveShell.automagic', | |||
|
45 | """Turn on the auto calling of magic commands. Type %%magic at the | |||
|
46 | IPython prompt for more information.""", | |||
|
47 | 'Turn off the auto calling of magic commands.' | |||
|
48 | ) | |||
|
49 | addflag('pdb', 'InteractiveShell.pdb', | |||
|
50 | "Enable auto calling the pdb debugger after every exception.", | |||
|
51 | "Disable auto calling the pdb debugger after every exception." | |||
|
52 | ) | |||
|
53 | addflag('pprint', 'PlainTextFormatter.pprint', | |||
|
54 | "Enable auto pretty printing of results.", | |||
|
55 | "Disable auto auto pretty printing of results." | |||
|
56 | ) | |||
|
57 | addflag('color-info', 'InteractiveShell.color_info', | |||
|
58 | """IPython can display information about objects via a set of func- | |||
|
59 | tions, and optionally can use colors for this, syntax highlighting | |||
|
60 | source code and various other elements. However, because this | |||
|
61 | information is passed through a pager (like 'less') and many pagers get | |||
|
62 | confused with color codes, this option is off by default. You can test | |||
|
63 | it and turn it on permanently in your ipython_config.py file if it | |||
|
64 | works for you. Test it and turn it on permanently if it works with | |||
|
65 | your system. The magic function %%color_info allows you to toggle this | |||
|
66 | inter- actively for testing.""", | |||
|
67 | "Disable using colors for info related things." | |||
|
68 | ) | |||
|
69 | addflag('deep-reload', 'InteractiveShell.deep_reload', | |||
|
70 | """Enable deep (recursive) reloading by default. IPython can use the | |||
|
71 | deep_reload module which reloads changes in modules recursively (it | |||
|
72 | replaces the reload() function, so you don't need to change anything to | |||
|
73 | use it). deep_reload() forces a full reload of modules whose code may | |||
|
74 | have changed, which the default reload() function does not. When | |||
|
75 | deep_reload is off, IPython will use the normal reload(), but | |||
|
76 | deep_reload will still be available as dreload(). This feature is off | |||
|
77 | by default [which means that you have both normal reload() and | |||
|
78 | dreload()].""", | |||
|
79 | "Disable deep (recursive) reloading by default." | |||
|
80 | ) | |||
|
81 | ||||
|
82 | # it's possible we don't want short aliases for *all* of these: | |||
|
83 | shell_aliases = dict( | |||
|
84 | autocall='InteractiveShell.autocall', | |||
|
85 | cache_size='InteractiveShell.cache_size', | |||
|
86 | colors='InteractiveShell.colors', | |||
|
87 | logfile='InteractiveShell.logfile', | |||
|
88 | log_append='InteractiveShell.logappend', | |||
|
89 | pi1='InteractiveShell.prompt_in1', | |||
|
90 | pi2='InteractiveShell.prompt_in1', | |||
|
91 | po='InteractiveShell.prompt_out', | |||
|
92 | si='InteractiveShell.separate_in', | |||
|
93 | so='InteractiveShell.separate_out', | |||
|
94 | so2='InteractiveShell.separate_out2', | |||
|
95 | xmode='InteractiveShell.xmode', | |||
|
96 | c='InteractiveShellApp.code_to_run', | |||
|
97 | ext='InteractiveShellApp.extra_extension', | |||
|
98 | ) | |||
|
99 | ||||
|
100 | #----------------------------------------------------------------------------- | |||
|
101 | # Main classes and functions | |||
|
102 | #----------------------------------------------------------------------------- | |||
|
103 | ||||
|
104 | class InteractiveShellApp(Configurable): | |||
|
105 | """A Mixin for applications that start InteractiveShell instances. | |||
|
106 | ||||
|
107 | Provides configurables for loading extensions and executing files | |||
|
108 | as part of configuring a Shell environment. | |||
|
109 | ||||
|
110 | Provides init_extensions() and init_code() methods, to be called | |||
|
111 | after init_shell(), which must be implemented by subclasses. | |||
|
112 | """ | |||
|
113 | extensions = List(Unicode, config=True, | |||
|
114 | help="A list of dotted module names of IPython extensions to load." | |||
|
115 | ) | |||
|
116 | extra_extension = Unicode('', config=True, | |||
|
117 | help="dotted module name of an IPython extension to load." | |||
|
118 | ) | |||
|
119 | def _extra_extension_changed(self, name, old, new): | |||
|
120 | if new: | |||
|
121 | # add to self.extensions | |||
|
122 | self.extensions.append(new) | |||
|
123 | ||||
|
124 | exec_files = List(Unicode, config=True, | |||
|
125 | help="""List of files to run at IPython startup.""" | |||
|
126 | ) | |||
|
127 | file_to_run = Unicode('', config=True, | |||
|
128 | help="""A file to be run""") | |||
|
129 | ||||
|
130 | exec_lines = List(Unicode, config=True, | |||
|
131 | help="""lines of code to run at IPython startup.""" | |||
|
132 | ) | |||
|
133 | code_to_run = Unicode('', config=True, | |||
|
134 | help="Execute the given command string." | |||
|
135 | ) | |||
|
136 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') | |||
|
137 | ||||
|
138 | def init_shell(self): | |||
|
139 | raise NotImplementedError("Override in subclasses") | |||
|
140 | ||||
|
141 | def init_extensions(self): | |||
|
142 | """Load all IPython extensions in IPythonApp.extensions. | |||
|
143 | ||||
|
144 | This uses the :meth:`ExtensionManager.load_extensions` to load all | |||
|
145 | the extensions listed in ``self.extensions``. | |||
|
146 | """ | |||
|
147 | if not self.extensions: | |||
|
148 | return | |||
|
149 | try: | |||
|
150 | self.log.debug("Loading IPython extensions...") | |||
|
151 | extensions = self.extensions | |||
|
152 | for ext in extensions: | |||
|
153 | try: | |||
|
154 | self.log.info("Loading IPython extension: %s" % ext) | |||
|
155 | self.shell.extension_manager.load_extension(ext) | |||
|
156 | except: | |||
|
157 | self.log.warn("Error in loading extension: %s" % ext) | |||
|
158 | self.shell.showtraceback() | |||
|
159 | except: | |||
|
160 | self.log.warn("Unknown error in loading extensions:") | |||
|
161 | self.shell.showtraceback() | |||
|
162 | ||||
|
163 | def init_code(self): | |||
|
164 | """run the pre-flight code, specified via exec_lines""" | |||
|
165 | self._run_exec_lines() | |||
|
166 | self._run_exec_files() | |||
|
167 | self._run_cmd_line_code() | |||
|
168 | ||||
|
169 | def _run_exec_lines(self): | |||
|
170 | """Run lines of code in IPythonApp.exec_lines in the user's namespace.""" | |||
|
171 | if not self.exec_lines: | |||
|
172 | return | |||
|
173 | try: | |||
|
174 | self.log.debug("Running code from IPythonApp.exec_lines...") | |||
|
175 | for line in self.exec_lines: | |||
|
176 | try: | |||
|
177 | self.log.info("Running code in user namespace: %s" % | |||
|
178 | line) | |||
|
179 | self.shell.run_cell(line, store_history=False) | |||
|
180 | except: | |||
|
181 | self.log.warn("Error in executing line in user " | |||
|
182 | "namespace: %s" % line) | |||
|
183 | self.shell.showtraceback() | |||
|
184 | except: | |||
|
185 | self.log.warn("Unknown error in handling IPythonApp.exec_lines:") | |||
|
186 | self.shell.showtraceback() | |||
|
187 | ||||
|
188 | def _exec_file(self, fname): | |||
|
189 | full_filename = filefind(fname, [u'.', self.ipython_dir]) | |||
|
190 | if os.path.isfile(full_filename): | |||
|
191 | if full_filename.endswith(u'.py'): | |||
|
192 | self.log.info("Running file in user namespace: %s" % | |||
|
193 | full_filename) | |||
|
194 | # Ensure that __file__ is always defined to match Python behavior | |||
|
195 | self.shell.user_ns['__file__'] = fname | |||
|
196 | try: | |||
|
197 | self.shell.safe_execfile(full_filename, self.shell.user_ns) | |||
|
198 | finally: | |||
|
199 | del self.shell.user_ns['__file__'] | |||
|
200 | elif full_filename.endswith('.ipy'): | |||
|
201 | self.log.info("Running file in user namespace: %s" % | |||
|
202 | full_filename) | |||
|
203 | self.shell.safe_execfile_ipy(full_filename) | |||
|
204 | else: | |||
|
205 | self.log.warn("File does not have a .py or .ipy extension: <%s>" | |||
|
206 | % full_filename) | |||
|
207 | ||||
|
208 | def _run_exec_files(self): | |||
|
209 | """Run files from IPythonApp.exec_files""" | |||
|
210 | if not self.exec_files: | |||
|
211 | return | |||
|
212 | ||||
|
213 | self.log.debug("Running files in IPythonApp.exec_files...") | |||
|
214 | try: | |||
|
215 | for fname in self.exec_files: | |||
|
216 | self._exec_file(fname) | |||
|
217 | except: | |||
|
218 | self.log.warn("Unknown error in handling IPythonApp.exec_files:") | |||
|
219 | self.shell.showtraceback() | |||
|
220 | ||||
|
221 | def _run_cmd_line_code(self): | |||
|
222 | """Run code or file specified at the command-line""" | |||
|
223 | if self.code_to_run: | |||
|
224 | line = self.code_to_run | |||
|
225 | try: | |||
|
226 | self.log.info("Running code given at command line (c=): %s" % | |||
|
227 | line) | |||
|
228 | self.shell.run_cell(line, store_history=False) | |||
|
229 | except: | |||
|
230 | self.log.warn("Error in executing line in user namespace: %s" % | |||
|
231 | line) | |||
|
232 | self.shell.showtraceback() | |||
|
233 | ||||
|
234 | # Like Python itself, ignore the second if the first of these is present | |||
|
235 | elif self.file_to_run: | |||
|
236 | fname = self.file_to_run | |||
|
237 | try: | |||
|
238 | self._exec_file(fname) | |||
|
239 | except: | |||
|
240 | self.log.warn("Error in executing file in user namespace: %s" % | |||
|
241 | fname) | |||
|
242 | self.shell.showtraceback() | |||
|
243 |
@@ -4,34 +4,33 b' c = get_config()' | |||||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Application-level options |
|
5 | # Application-level options | |
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | app = c.IPythonApp |
|
|||
8 |
|
7 | |||
9 | # app.display_banner = True |
|
8 | # c.TerminalIPythonApp.display_banner = True | |
10 |
|
9 | |||
11 | # app.classic = False |
|
10 | # c.TerminalIPythonApp.classic = False | |
12 |
|
11 | |||
13 | # app.nosep = True |
|
12 | # c.TerminalIPythonApp.nosep = True | |
14 |
|
13 | |||
15 | # If you still use multiple versions of IPytho on the same machine, |
|
14 | # If you still use multiple versions of IPytho on the same machine, | |
16 | # set this to True to suppress warnings about old configuration files |
|
15 | # set this to True to suppress warnings about old configuration files | |
17 | # app.ignore_old_config = False |
|
16 | # c.TerminalIPythonApp.ignore_old_config = False | |
18 |
|
17 | |||
19 | # Set this to determine the detail of what is logged at startup. |
|
18 | # Set this to determine the detail of what is logged at startup. | |
20 | # The default is 30 and possible values are 0,10,20,30,40,50. |
|
19 | # The default is 30 and possible values are 0,10,20,30,40,50. | |
21 |
# |
|
20 | # c.Application.log_level = 20 | |
22 |
|
21 | |||
23 | # This should be a list of importable Python modules that have an |
|
22 | # This should be a list of importable Python modules that have an | |
24 | # load_ipython_extension(ip) method. This method gets called when the extension |
|
23 | # load_ipython_extension(ip) method. This method gets called when the extension | |
25 | # is loaded. You can put your extensions anywhere they can be imported |
|
24 | # is loaded. You can put your extensions anywhere they can be imported | |
26 | # but we add the extensions subdir of the ipython directory to sys.path |
|
25 | # but we add the extensions subdir of the ipython directory to sys.path | |
27 | # during extension loading, so you can put them there as well. |
|
26 | # during extension loading, so you can put them there as well. | |
28 | # app.extensions = [ |
|
27 | # c.InteractiveShellApp.extensions = [ | |
29 | # 'myextension' |
|
28 | # 'myextension' | |
30 | # ] |
|
29 | # ] | |
31 |
|
30 | |||
32 | # These lines are run in IPython in the user's namespace after extensions |
|
31 | # These lines are run in IPython in the user's namespace after extensions | |
33 | # are loaded. They can contain full IPython syntax with magics etc. |
|
32 | # are loaded. They can contain full IPython syntax with magics etc. | |
34 | # app.exec_lines = [ |
|
33 | # c.InteractiveShellApp.exec_lines = [ | |
35 | # 'import numpy', |
|
34 | # 'import numpy', | |
36 | # 'a = 10; b = 20', |
|
35 | # 'a = 10; b = 20', | |
37 | # '1/0' |
|
36 | # '1/0' | |
@@ -41,7 +40,7 b' app = c.IPythonApp' | |||||
41 | # extension need to be pure Python. Files with a .ipy extension can have |
|
40 | # extension need to be pure Python. Files with a .ipy extension can have | |
42 | # custom IPython syntax (like magics, etc.). |
|
41 | # custom IPython syntax (like magics, etc.). | |
43 | # These files need to be in the cwd, the ipython_dir or be absolute paths. |
|
42 | # These files need to be in the cwd, the ipython_dir or be absolute paths. | |
44 | # app.exec_files = [ |
|
43 | # c.InteractiveShellApp.exec_files = [ | |
45 | # 'mycode.py', |
|
44 | # 'mycode.py', | |
46 | # 'fancy.ipy' |
|
45 | # 'fancy.ipy' | |
47 | # ] |
|
46 | # ] | |
@@ -58,9 +57,9 b' app = c.IPythonApp' | |||||
58 |
|
57 | |||
59 | # c.InteractiveShell.automagic = False |
|
58 | # c.InteractiveShell.automagic = False | |
60 |
|
59 | |||
61 |
# c.Terminal |
|
60 | # c.TerminalInteractiveShell.banner1 = 'This if for overriding the default IPython banner' | |
62 |
|
61 | |||
63 |
# c.Terminal |
|
62 | # c.TerminalInteractiveShell.banner2 = "This is for extra banner text" | |
64 |
|
63 | |||
65 | # c.InteractiveShell.cache_size = 1000 |
|
64 | # c.InteractiveShell.cache_size = 1000 | |
66 |
|
65 |
@@ -40,11 +40,14 b' from IPython.core.formatters import PlainTextFormatter' | |||||
40 | from IPython.core.newapplication import ( |
|
40 | from IPython.core.newapplication import ( | |
41 | ProfileDir, BaseIPythonApplication, base_flags, base_aliases |
|
41 | ProfileDir, BaseIPythonApplication, base_flags, base_aliases | |
42 | ) |
|
42 | ) | |
|
43 | from IPython.core.shellapp import ( | |||
|
44 | InteractiveShellApp, shell_flags, shell_aliases | |||
|
45 | ) | |||
43 | from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell |
|
46 | from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell | |
44 | from IPython.lib import inputhook |
|
47 | from IPython.lib import inputhook | |
45 |
from IPython.utils.path import |
|
48 | from IPython.utils.path import get_ipython_dir, check_for_old_config | |
46 | from IPython.utils.traitlets import ( |
|
49 | from IPython.utils.traitlets import ( | |
47 |
Bool, |
|
50 | Bool, Dict, CaselessStrEnum | |
48 | ) |
|
51 | ) | |
49 |
|
52 | |||
50 | #----------------------------------------------------------------------------- |
|
53 | #----------------------------------------------------------------------------- | |
@@ -55,7 +58,6 b' from IPython.utils.traitlets import (' | |||||
55 | default_config_file_name = u'ipython_config.py' |
|
58 | default_config_file_name = u'ipython_config.py' | |
56 |
|
59 | |||
57 |
|
60 | |||
58 |
|
||||
59 | #----------------------------------------------------------------------------- |
|
61 | #----------------------------------------------------------------------------- | |
60 | # Crash handler for this application |
|
62 | # Crash handler for this application | |
61 | #----------------------------------------------------------------------------- |
|
63 | #----------------------------------------------------------------------------- | |
@@ -119,69 +121,22 b' class IPAppCrashHandler(CrashHandler):' | |||||
119 | # Aliases and Flags |
|
121 | # Aliases and Flags | |
120 | #----------------------------------------------------------------------------- |
|
122 | #----------------------------------------------------------------------------- | |
121 | flags = dict(base_flags) |
|
123 | flags = dict(base_flags) | |
122 |
flags.update( |
|
124 | flags.update(shell_flags) | |
123 |
|
||||
124 |
|
||||
125 | }) |
|
|||
126 | addflag = lambda *args: flags.update(boolean_flag(*args)) |
|
125 | addflag = lambda *args: flags.update(boolean_flag(*args)) | |
127 | addflag('autoindent', 'InteractiveShell.autoindent', |
|
|||
128 | 'Turn on autoindenting.', 'Turn off autoindenting.' |
|
|||
129 | ) |
|
|||
130 | addflag('automagic', 'InteractiveShell.automagic', |
|
|||
131 | """Turn on the auto calling of magic commands. Type %%magic at the |
|
|||
132 | IPython prompt for more information.""", |
|
|||
133 | 'Turn off the auto calling of magic commands.' |
|
|||
134 | ) |
|
|||
135 | addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax', |
|
126 | addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax', | |
136 | 'Turn on auto editing of files with syntax errors.', |
|
127 | 'Turn on auto editing of files with syntax errors.', | |
137 | 'Turn off auto editing of files with syntax errors.' |
|
128 | 'Turn off auto editing of files with syntax errors.' | |
138 | ) |
|
129 | ) | |
139 | addflag('banner', 'IPythonApp.display_banner', |
|
130 | addflag('banner', 'TerminalIPythonApp.display_banner', | |
140 | "Display a banner upon starting IPython.", |
|
131 | "Display a banner upon starting IPython.", | |
141 | "Don't display a banner upon starting IPython." |
|
132 | "Don't display a banner upon starting IPython." | |
142 | ) |
|
133 | ) | |
143 | addflag('pdb', 'InteractiveShell.pdb', |
|
|||
144 | "Enable auto calling the pdb debugger after every exception.", |
|
|||
145 | "Disable auto calling the pdb debugger after every exception." |
|
|||
146 | ) |
|
|||
147 | addflag('pprint', 'PlainTextFormatter.pprint', |
|
|||
148 | "Enable auto pretty printing of results.", |
|
|||
149 | "Disable auto auto pretty printing of results." |
|
|||
150 | ) |
|
|||
151 | addflag('color-info', 'InteractiveShell.color_info', |
|
|||
152 | """IPython can display information about objects via a set of func- |
|
|||
153 | tions, and optionally can use colors for this, syntax highlighting |
|
|||
154 | source code and various other elements. However, because this |
|
|||
155 | information is passed through a pager (like 'less') and many pagers get |
|
|||
156 | confused with color codes, this option is off by default. You can test |
|
|||
157 | it and turn it on permanently in your ipython_config.py file if it |
|
|||
158 | works for you. Test it and turn it on permanently if it works with |
|
|||
159 | your system. The magic function %%color_info allows you to toggle this |
|
|||
160 | inter- actively for testing.""", |
|
|||
161 | "Disable using colors for info related things." |
|
|||
162 | ) |
|
|||
163 | addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit', |
|
134 | addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit', | |
164 | """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 | |
165 | 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', | |
166 | you can force a direct exit without any confirmation.""", |
|
137 | you can force a direct exit without any confirmation.""", | |
167 | "Don't prompt the user when exiting." |
|
138 | "Don't prompt the user when exiting." | |
168 | ) |
|
139 | ) | |
169 | addflag('deep-reload', 'InteractiveShell.deep_reload', |
|
|||
170 | """Enable deep (recursive) reloading by default. IPython can use the |
|
|||
171 | deep_reload module which reloads changes in modules recursively (it |
|
|||
172 | replaces the reload() function, so you don't need to change anything to |
|
|||
173 | use it). deep_reload() forces a full reload of modules whose code may |
|
|||
174 | have changed, which the default reload() function does not. When |
|
|||
175 | deep_reload is off, IPython will use the normal reload(), but |
|
|||
176 | deep_reload will still be available as dreload(). This feature is off |
|
|||
177 | by default [which means that you have both normal reload() and |
|
|||
178 | dreload()].""", |
|
|||
179 | "Disable deep (recursive) reloading by default." |
|
|||
180 | ) |
|
|||
181 | addflag('readline', 'InteractiveShell.readline_use', |
|
|||
182 | "Enable readline for command line usage.", |
|
|||
183 | "Disable readline for command line usage." |
|
|||
184 | ) |
|
|||
185 | addflag('term-title', 'TerminalInteractiveShell.term_title', |
|
140 | addflag('term-title', 'TerminalInteractiveShell.term_title', | |
186 | "Enable auto setting the terminal title.", |
|
141 | "Enable auto setting the terminal title.", | |
187 | "Disable auto setting the terminal title." |
|
142 | "Disable auto setting the terminal title." | |
@@ -209,7 +164,7 b" flags['classic']=(" | |||||
209 | # |
|
164 | # | |
210 | # # quick is harder to implement |
|
165 | # # quick is harder to implement | |
211 | flags['quick']=( |
|
166 | flags['quick']=( | |
212 | {'IPythonApp' : {'quick' : True}}, |
|
167 | {'TerminalIPythonApp' : {'quick' : True}}, | |
213 | "Enable quick startup with no config files." |
|
168 | "Enable quick startup with no config files." | |
214 | ) |
|
169 | ) | |
215 |
|
170 | |||
@@ -221,44 +176,31 b" nosep_config.InteractiveShell.separate_out2 = ''" | |||||
221 | flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.") |
|
176 | flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.") | |
222 |
|
177 | |||
223 | flags['i'] = ( |
|
178 | flags['i'] = ( | |
224 | {'IPythonApp' : {'force_interact' : True}}, |
|
179 | {'TerminalIPythonApp' : {'force_interact' : True}}, | |
225 | "If running code from the command line, become interactive afterwards." |
|
180 | "If running code from the command line, become interactive afterwards." | |
226 | ) |
|
181 | ) | |
227 | flags['pylab'] = ( |
|
182 | flags['pylab'] = ( | |
228 | {'IPythonApp' : {'pylab' : 'auto'}}, |
|
183 | {'TerminalIPythonApp' : {'pylab' : 'auto'}}, | |
229 | """Pre-load matplotlib and numpy for interactive use with |
|
184 | """Pre-load matplotlib and numpy for interactive use with | |
230 | the default matplotlib backend.""" |
|
185 | the default matplotlib backend.""" | |
231 | ) |
|
186 | ) | |
232 |
|
187 | |||
233 | aliases = dict(base_aliases) |
|
188 | aliases = dict(base_aliases) | |
|
189 | aliases.update(shell_aliases) | |||
234 |
|
190 | |||
235 | # it's possible we don't want short aliases for *all* of these: |
|
191 | # it's possible we don't want short aliases for *all* of these: | |
236 | aliases.update(dict( |
|
192 | aliases.update(dict( | |
237 | autocall='InteractiveShell.autocall', |
|
|||
238 | cache_size='InteractiveShell.cache_size', |
|
|||
239 | colors='InteractiveShell.colors', |
|
|||
240 | editor='TerminalInteractiveShell.editor', |
|
193 | editor='TerminalInteractiveShell.editor', | |
241 | logfile='InteractiveShell.logfile', |
|
|||
242 | log_append='InteractiveShell.logappend', |
|
|||
243 | pi1='InteractiveShell.prompt_in1', |
|
|||
244 | pi2='InteractiveShell.prompt_in1', |
|
|||
245 | po='InteractiveShell.prompt_out', |
|
|||
246 | sl='TerminalInteractiveShell.screen_length', |
|
194 | sl='TerminalInteractiveShell.screen_length', | |
247 | si='InteractiveShell.separate_in', |
|
195 | gui='TerminalIPythonApp.gui', | |
248 | so='InteractiveShell.separate_out', |
|
196 | pylab='TerminalIPythonApp.pylab', | |
249 | so2='InteractiveShell.separate_out2', |
|
|||
250 | xmode='InteractiveShell.xmode', |
|
|||
251 | c='IPythonApp.code_to_run', |
|
|||
252 | ext='IPythonApp.extra_extension', |
|
|||
253 | gui='IPythonApp.gui', |
|
|||
254 | pylab='IPythonApp.pylab', |
|
|||
255 | )) |
|
197 | )) | |
256 |
|
198 | |||
257 | #----------------------------------------------------------------------------- |
|
199 | #----------------------------------------------------------------------------- | |
258 | # Main classes and functions |
|
200 | # Main classes and functions | |
259 | #----------------------------------------------------------------------------- |
|
201 | #----------------------------------------------------------------------------- | |
260 |
|
202 | |||
261 | class IPythonApp(BaseIPythonApplication): |
|
203 | class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp): | |
262 | name = u'ipython' |
|
204 | name = u'ipython' | |
263 | description = usage.cl_usage |
|
205 | description = usage.cl_usage | |
264 | # command_line_loader = IPAppConfigLoader |
|
206 | # command_line_loader = IPAppConfigLoader | |
@@ -266,7 +208,7 b' class IPythonApp(BaseIPythonApplication):' | |||||
266 | crash_handler_class = IPAppCrashHandler |
|
208 | crash_handler_class = IPAppCrashHandler | |
267 | flags = Dict(flags) |
|
209 | flags = Dict(flags) | |
268 | aliases = Dict(aliases) |
|
210 | aliases = Dict(aliases) | |
269 | classes = [TerminalInteractiveShell, ProfileDir, PlainTextFormatter] |
|
211 | classes = [InteractiveShellApp, TerminalInteractiveShell, ProfileDir, PlainTextFormatter] | |
270 | # *do* autocreate requested profile |
|
212 | # *do* autocreate requested profile | |
271 | auto_create=Bool(True) |
|
213 | auto_create=Bool(True) | |
272 | copy_config_files=Bool(True) |
|
214 | copy_config_files=Bool(True) | |
@@ -294,16 +236,6 b' class IPythonApp(BaseIPythonApplication):' | |||||
294 | display_banner = Bool(True, config=True, |
|
236 | display_banner = Bool(True, config=True, | |
295 | help="Whether to display a banner upon starting IPython." |
|
237 | help="Whether to display a banner upon starting IPython." | |
296 | ) |
|
238 | ) | |
297 | extensions = List(Unicode, config=True, |
|
|||
298 | help="A list of dotted module names of IPython extensions to load." |
|
|||
299 | ) |
|
|||
300 | extra_extension = Unicode('', config=True, |
|
|||
301 | help="dotted module name of an IPython extension to load." |
|
|||
302 | ) |
|
|||
303 | def _extra_extension_changed(self, name, old, new): |
|
|||
304 | if new: |
|
|||
305 | # add to self.extensions |
|
|||
306 | self.extensions.append(new) |
|
|||
307 |
|
239 | |||
308 | # if there is code of files to run from the cmd line, don't interact |
|
240 | # if there is code of files to run from the cmd line, don't interact | |
309 | # unless the --i flag (App.force_interact) is true. |
|
241 | # unless the --i flag (App.force_interact) is true. | |
@@ -314,22 +246,10 b' class IPythonApp(BaseIPythonApplication):' | |||||
314 | def _force_interact_changed(self, name, old, new): |
|
246 | def _force_interact_changed(self, name, old, new): | |
315 | if new: |
|
247 | if new: | |
316 | self.interact = True |
|
248 | self.interact = True | |
317 |
|
249 | |||
318 | exec_files = List(Unicode, config=True, |
|
|||
319 | help="""List of files to run at IPython startup.""" |
|
|||
320 | ) |
|
|||
321 | file_to_run = Unicode('', config=True, |
|
|||
322 | help="""A file to be run""") |
|
|||
323 | def _file_to_run_changed(self, name, old, new): |
|
250 | def _file_to_run_changed(self, name, old, new): | |
324 | if new and not self.force_interact: |
|
251 | if new and not self.force_interact: | |
325 | self.interact = False |
|
252 | self.interact = False | |
326 |
|
||||
327 | exec_lines = List(Unicode, config=True, |
|
|||
328 | help="""lines of code to run at IPython startup.""" |
|
|||
329 | ) |
|
|||
330 | code_to_run = Unicode('', config=True, |
|
|||
331 | help="Execute the given command string." |
|
|||
332 | ) |
|
|||
333 | _code_to_run_changed = _file_to_run_changed |
|
253 | _code_to_run_changed = _file_to_run_changed | |
334 |
|
254 | |||
335 | # internal, not-configurable |
|
255 | # internal, not-configurable | |
@@ -338,7 +258,7 b' class IPythonApp(BaseIPythonApplication):' | |||||
338 |
|
258 | |||
339 | def initialize(self, argv=None): |
|
259 | def initialize(self, argv=None): | |
340 | """Do actions after construct, but before starting the app.""" |
|
260 | """Do actions after construct, but before starting the app.""" | |
341 | super(IPythonApp, self).initialize(argv) |
|
261 | super(TerminalIPythonApp, self).initialize(argv) | |
342 | if not self.ignore_old_config: |
|
262 | if not self.ignore_old_config: | |
343 | check_for_old_config(self.ipython_dir) |
|
263 | check_for_old_config(self.ipython_dir) | |
344 |
|
264 | |||
@@ -401,110 +321,6 b' class IPythonApp(BaseIPythonApplication):' | |||||
401 | self.log.warn("Error in enabling GUI event loop integration:") |
|
321 | self.log.warn("Error in enabling GUI event loop integration:") | |
402 | self.shell.showtraceback() |
|
322 | self.shell.showtraceback() | |
403 |
|
323 | |||
404 | def init_extensions(self): |
|
|||
405 | """Load all IPython extensions in IPythonApp.extensions. |
|
|||
406 |
|
||||
407 | This uses the :meth:`ExtensionManager.load_extensions` to load all |
|
|||
408 | the extensions listed in ``self.extensions``. |
|
|||
409 | """ |
|
|||
410 | if not self.extensions: |
|
|||
411 | return |
|
|||
412 | try: |
|
|||
413 | self.log.debug("Loading IPython extensions...") |
|
|||
414 | extensions = self.extensions |
|
|||
415 | for ext in extensions: |
|
|||
416 | try: |
|
|||
417 | self.log.info("Loading IPython extension: %s" % ext) |
|
|||
418 | self.shell.extension_manager.load_extension(ext) |
|
|||
419 | except: |
|
|||
420 | self.log.warn("Error in loading extension: %s" % ext) |
|
|||
421 | self.shell.showtraceback() |
|
|||
422 | except: |
|
|||
423 | self.log.warn("Unknown error in loading extensions:") |
|
|||
424 | self.shell.showtraceback() |
|
|||
425 |
|
||||
426 | def init_code(self): |
|
|||
427 | """run the pre-flight code, specified via exec_lines""" |
|
|||
428 | self._run_exec_lines() |
|
|||
429 | self._run_exec_files() |
|
|||
430 | self._run_cmd_line_code() |
|
|||
431 |
|
||||
432 | def _run_exec_lines(self): |
|
|||
433 | """Run lines of code in IPythonApp.exec_lines in the user's namespace.""" |
|
|||
434 | if not self.exec_lines: |
|
|||
435 | return |
|
|||
436 | try: |
|
|||
437 | self.log.debug("Running code from IPythonApp.exec_lines...") |
|
|||
438 | for line in self.exec_lines: |
|
|||
439 | try: |
|
|||
440 | self.log.info("Running code in user namespace: %s" % |
|
|||
441 | line) |
|
|||
442 | self.shell.run_cell(line, store_history=False) |
|
|||
443 | except: |
|
|||
444 | self.log.warn("Error in executing line in user " |
|
|||
445 | "namespace: %s" % line) |
|
|||
446 | self.shell.showtraceback() |
|
|||
447 | except: |
|
|||
448 | self.log.warn("Unknown error in handling IPythonApp.exec_lines:") |
|
|||
449 | self.shell.showtraceback() |
|
|||
450 |
|
||||
451 | def _exec_file(self, fname): |
|
|||
452 | full_filename = filefind(fname, [u'.', self.ipython_dir]) |
|
|||
453 | if os.path.isfile(full_filename): |
|
|||
454 | if full_filename.endswith(u'.py'): |
|
|||
455 | self.log.info("Running file in user namespace: %s" % |
|
|||
456 | full_filename) |
|
|||
457 | # Ensure that __file__ is always defined to match Python behavior |
|
|||
458 | self.shell.user_ns['__file__'] = fname |
|
|||
459 | try: |
|
|||
460 | self.shell.safe_execfile(full_filename, self.shell.user_ns) |
|
|||
461 | finally: |
|
|||
462 | del self.shell.user_ns['__file__'] |
|
|||
463 | elif full_filename.endswith('.ipy'): |
|
|||
464 | self.log.info("Running file in user namespace: %s" % |
|
|||
465 | full_filename) |
|
|||
466 | self.shell.safe_execfile_ipy(full_filename) |
|
|||
467 | else: |
|
|||
468 | self.log.warn("File does not have a .py or .ipy extension: <%s>" |
|
|||
469 | % full_filename) |
|
|||
470 |
|
||||
471 | def _run_exec_files(self): |
|
|||
472 | """Run files from IPythonApp.exec_files""" |
|
|||
473 | if not self.exec_files: |
|
|||
474 | return |
|
|||
475 |
|
||||
476 | self.log.debug("Running files in IPythonApp.exec_files...") |
|
|||
477 | try: |
|
|||
478 | for fname in self.exec_files: |
|
|||
479 | self._exec_file(fname) |
|
|||
480 | except: |
|
|||
481 | self.log.warn("Unknown error in handling IPythonApp.exec_files:") |
|
|||
482 | self.shell.showtraceback() |
|
|||
483 |
|
||||
484 | def _run_cmd_line_code(self): |
|
|||
485 | """Run code or file specified at the command-line""" |
|
|||
486 | if self.code_to_run: |
|
|||
487 | line = self.code_to_run |
|
|||
488 | try: |
|
|||
489 | self.log.info("Running code given at command line (c=): %s" % |
|
|||
490 | line) |
|
|||
491 | self.shell.run_cell(line, store_history=False) |
|
|||
492 | except: |
|
|||
493 | self.log.warn("Error in executing line in user namespace: %s" % |
|
|||
494 | line) |
|
|||
495 | self.shell.showtraceback() |
|
|||
496 |
|
||||
497 | # Like Python itself, ignore the second if the first of these is present |
|
|||
498 | elif self.file_to_run: |
|
|||
499 | fname = self.file_to_run |
|
|||
500 | try: |
|
|||
501 | self._exec_file(fname) |
|
|||
502 | except: |
|
|||
503 | self.log.warn("Error in executing file in user namespace: %s" % |
|
|||
504 | fname) |
|
|||
505 | self.shell.showtraceback() |
|
|||
506 |
|
||||
507 |
|
||||
508 | def start(self): |
|
324 | def start(self): | |
509 | # perform any prexec steps: |
|
325 | # perform any prexec steps: | |
510 | if self.interact: |
|
326 | if self.interact: | |
@@ -529,7 +345,7 b' def load_default_config(ipython_dir=None):' | |||||
529 |
|
345 | |||
530 | def launch_new_instance(): |
|
346 | def launch_new_instance(): | |
531 | """Create and run a full blown IPython instance""" |
|
347 | """Create and run a full blown IPython instance""" | |
532 | app = IPythonApp() |
|
348 | app = TerminalIPythonApp() | |
533 | app.initialize() |
|
349 | app.initialize() | |
534 | # print app.config |
|
350 | # print app.config | |
535 | # print app.profile_dir.location |
|
351 | # print app.profile_dir.location |
General Comments 0
You need to be logged in to leave comments.
Login now