Show More
@@ -1,227 +1,243 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | %store magic for lightweight persistence. |
|
3 | %store magic for lightweight persistence. | |
4 |
|
4 | |||
5 | Stores variables, aliases and macros in IPython's database. |
|
5 | Stores variables, aliases and macros in IPython's database. | |
6 |
|
6 | |||
7 | To automatically restore stored variables at startup, add this to your |
|
7 | To automatically restore stored variables at startup, add this to your | |
8 | :file:`ipython_config.py` file:: |
|
8 | :file:`ipython_config.py` file:: | |
9 |
|
9 | |||
10 | c.StoreMagic.autorestore = True |
|
10 | c.StoreMagic.autorestore = True | |
11 | """ |
|
11 | """ | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Copyright (c) 2012, The IPython Development Team. |
|
13 | # Copyright (c) 2012, The IPython Development Team. | |
14 | # |
|
14 | # | |
15 | # Distributed under the terms of the Modified BSD License. |
|
15 | # Distributed under the terms of the Modified BSD License. | |
16 | # |
|
16 | # | |
17 | # The full license is in the file COPYING.txt, distributed with this software. |
|
17 | # The full license is in the file COPYING.txt, distributed with this software. | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | # Imports |
|
21 | # Imports | |
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 |
|
23 | |||
24 | # Stdlib |
|
24 | # Stdlib | |
25 | import inspect, os, sys, textwrap |
|
25 | import inspect, os, sys, textwrap | |
26 |
|
26 | |||
27 | # Our own |
|
27 | # Our own | |
|
28 | from IPython.config.configurable import Configurable | |||
28 | from IPython.core.error import UsageError |
|
29 | from IPython.core.error import UsageError | |
29 | from IPython.core.fakemodule import FakeModule |
|
30 | from IPython.core.fakemodule import FakeModule | |
30 | from IPython.core.magic import Magics, magics_class, line_magic |
|
31 | from IPython.core.magic import Magics, magics_class, line_magic | |
31 | from IPython.testing.skipdoctest import skip_doctest |
|
32 | from IPython.testing.skipdoctest import skip_doctest | |
|
33 | from IPython.utils.traitlets import Bool | |||
32 |
|
34 | |||
33 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
34 | # Functions and classes |
|
36 | # Functions and classes | |
35 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
36 |
|
38 | |||
37 | def restore_aliases(ip): |
|
39 | def restore_aliases(ip): | |
38 | staliases = ip.db.get('stored_aliases', {}) |
|
40 | staliases = ip.db.get('stored_aliases', {}) | |
39 | for k,v in staliases.items(): |
|
41 | for k,v in staliases.items(): | |
40 | #print "restore alias",k,v # dbg |
|
42 | #print "restore alias",k,v # dbg | |
41 | #self.alias_table[k] = v |
|
43 | #self.alias_table[k] = v | |
42 | ip.alias_manager.define_alias(k,v) |
|
44 | ip.alias_manager.define_alias(k,v) | |
43 |
|
45 | |||
44 |
|
46 | |||
45 | def refresh_variables(ip): |
|
47 | def refresh_variables(ip): | |
46 | db = ip.db |
|
48 | db = ip.db | |
47 | for key in db.keys('autorestore/*'): |
|
49 | for key in db.keys('autorestore/*'): | |
48 | # strip autorestore |
|
50 | # strip autorestore | |
49 | justkey = os.path.basename(key) |
|
51 | justkey = os.path.basename(key) | |
50 | try: |
|
52 | try: | |
51 | obj = db[key] |
|
53 | obj = db[key] | |
52 | except KeyError: |
|
54 | except KeyError: | |
53 | print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey |
|
55 | print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey | |
54 | print "The error was:", sys.exc_info()[0] |
|
56 | print "The error was:", sys.exc_info()[0] | |
55 | else: |
|
57 | else: | |
56 | #print "restored",justkey,"=",obj #dbg |
|
58 | #print "restored",justkey,"=",obj #dbg | |
57 | ip.user_ns[justkey] = obj |
|
59 | ip.user_ns[justkey] = obj | |
58 |
|
60 | |||
59 |
|
61 | |||
60 | def restore_dhist(ip): |
|
62 | def restore_dhist(ip): | |
61 | ip.user_ns['_dh'] = ip.db.get('dhist',[]) |
|
63 | ip.user_ns['_dh'] = ip.db.get('dhist',[]) | |
62 |
|
64 | |||
63 |
|
65 | |||
64 | def restore_data(ip): |
|
66 | def restore_data(ip): | |
65 | refresh_variables(ip) |
|
67 | refresh_variables(ip) | |
66 | restore_aliases(ip) |
|
68 | restore_aliases(ip) | |
67 | restore_dhist(ip) |
|
69 | restore_dhist(ip) | |
68 |
|
70 | |||
69 |
|
71 | |||
70 | @magics_class |
|
72 | @magics_class | |
71 | class StoreMagics(Magics): |
|
73 | class StoreMagics(Magics, Configurable): | |
72 | """Lightweight persistence for python variables. |
|
74 | """Lightweight persistence for python variables. | |
73 |
|
75 | |||
74 | Provides the %store magic.""" |
|
76 | Provides the %store magic.""" | |
|
77 | ||||
|
78 | autorestore = Bool(False, config=True, help= | |||
|
79 | """If True, any %store-d variables will be automatically restored | |||
|
80 | when IPython starts. | |||
|
81 | """ | |||
|
82 | ) | |||
|
83 | ||||
|
84 | def __init__(self, shell): | |||
|
85 | Configurable.__init__(self, config=shell.config) | |||
|
86 | Magics.__init__(self, shell=shell) | |||
|
87 | self.shell.configurables.append(self) | |||
|
88 | if self.autorestore: | |||
|
89 | restore_data(self.shell) | |||
75 |
|
90 | |||
76 | @skip_doctest |
|
91 | @skip_doctest | |
77 | @line_magic |
|
92 | @line_magic | |
78 | def store(self, parameter_s=''): |
|
93 | def store(self, parameter_s=''): | |
79 | """Lightweight persistence for python variables. |
|
94 | """Lightweight persistence for python variables. | |
80 |
|
95 | |||
81 | Example:: |
|
96 | Example:: | |
82 |
|
97 | |||
83 | In [1]: l = ['hello',10,'world'] |
|
98 | In [1]: l = ['hello',10,'world'] | |
84 | In [2]: %store l |
|
99 | In [2]: %store l | |
85 | In [3]: exit |
|
100 | In [3]: exit | |
86 |
|
101 | |||
87 | (IPython session is closed and started again...) |
|
102 | (IPython session is closed and started again...) | |
88 |
|
103 | |||
89 | ville@badger:~$ ipython |
|
104 | ville@badger:~$ ipython | |
90 | In [1]: l |
|
105 | In [1]: l | |
91 | NameError: name 'l' is not defined |
|
106 | NameError: name 'l' is not defined | |
92 | In [2]: %store -r |
|
107 | In [2]: %store -r | |
93 | In [3]: l |
|
108 | In [3]: l | |
94 | Out[3]: ['hello', 10, 'world'] |
|
109 | Out[3]: ['hello', 10, 'world'] | |
95 |
|
110 | |||
96 | Usage: |
|
111 | Usage: | |
97 |
|
112 | |||
98 | * ``%store`` - Show list of all variables and their current |
|
113 | * ``%store`` - Show list of all variables and their current | |
99 | values |
|
114 | values | |
100 | * ``%store spam`` - Store the *current* value of the variable spam |
|
115 | * ``%store spam`` - Store the *current* value of the variable spam | |
101 | to disk |
|
116 | to disk | |
102 | * ``%store -d spam`` - Remove the variable and its value from storage |
|
117 | * ``%store -d spam`` - Remove the variable and its value from storage | |
103 | * ``%store -z`` - Remove all variables from storage |
|
118 | * ``%store -z`` - Remove all variables from storage | |
104 | * ``%store -r`` - Refresh all variables from store (overwrite |
|
119 | * ``%store -r`` - Refresh all variables from store (overwrite | |
105 | current vals) |
|
120 | current vals) | |
106 | * ``%store -r spam bar`` - Refresh specified variables from store |
|
121 | * ``%store -r spam bar`` - Refresh specified variables from store | |
107 | (delete current val) |
|
122 | (delete current val) | |
108 | * ``%store foo >a.txt`` - Store value of foo to new file a.txt |
|
123 | * ``%store foo >a.txt`` - Store value of foo to new file a.txt | |
109 | * ``%store foo >>a.txt`` - Append value of foo to file a.txt |
|
124 | * ``%store foo >>a.txt`` - Append value of foo to file a.txt | |
110 |
|
125 | |||
111 | It should be noted that if you change the value of a variable, you |
|
126 | It should be noted that if you change the value of a variable, you | |
112 | need to %store it again if you want to persist the new value. |
|
127 | need to %store it again if you want to persist the new value. | |
113 |
|
128 | |||
114 | Note also that the variables will need to be pickleable; most basic |
|
129 | Note also that the variables will need to be pickleable; most basic | |
115 | python types can be safely %store'd. |
|
130 | python types can be safely %store'd. | |
116 |
|
131 | |||
117 | Also aliases can be %store'd across sessions. |
|
132 | Also aliases can be %store'd across sessions. | |
118 | """ |
|
133 | """ | |
119 |
|
134 | |||
120 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') |
|
135 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') | |
121 | args = argsl.split(None,1) |
|
136 | args = argsl.split(None,1) | |
122 | ip = self.shell |
|
137 | ip = self.shell | |
123 | db = ip.db |
|
138 | db = ip.db | |
124 | # delete |
|
139 | # delete | |
125 | if 'd' in opts: |
|
140 | if 'd' in opts: | |
126 | try: |
|
141 | try: | |
127 | todel = args[0] |
|
142 | todel = args[0] | |
128 | except IndexError: |
|
143 | except IndexError: | |
129 | raise UsageError('You must provide the variable to forget') |
|
144 | raise UsageError('You must provide the variable to forget') | |
130 | else: |
|
145 | else: | |
131 | try: |
|
146 | try: | |
132 | del db['autorestore/' + todel] |
|
147 | del db['autorestore/' + todel] | |
133 | except: |
|
148 | except: | |
134 | raise UsageError("Can't delete variable '%s'" % todel) |
|
149 | raise UsageError("Can't delete variable '%s'" % todel) | |
135 | # reset |
|
150 | # reset | |
136 | elif 'z' in opts: |
|
151 | elif 'z' in opts: | |
137 | for k in db.keys('autorestore/*'): |
|
152 | for k in db.keys('autorestore/*'): | |
138 | del db[k] |
|
153 | del db[k] | |
139 |
|
154 | |||
140 | elif 'r' in opts: |
|
155 | elif 'r' in opts: | |
141 | if args: |
|
156 | if args: | |
142 | for arg in args: |
|
157 | for arg in args: | |
143 | try: |
|
158 | try: | |
144 | obj = db['autorestore/' + arg] |
|
159 | obj = db['autorestore/' + arg] | |
145 | except KeyError: |
|
160 | except KeyError: | |
146 | print "no stored variable %s" % arg |
|
161 | print "no stored variable %s" % arg | |
147 | else: |
|
162 | else: | |
148 | ip.user_ns[arg] = obj |
|
163 | ip.user_ns[arg] = obj | |
149 | else: |
|
164 | else: | |
150 | restore_data(ip) |
|
165 | restore_data(ip) | |
151 |
|
166 | |||
152 | # run without arguments -> list variables & values |
|
167 | # run without arguments -> list variables & values | |
153 | elif not args: |
|
168 | elif not args: | |
154 | vars = db.keys('autorestore/*') |
|
169 | vars = db.keys('autorestore/*') | |
155 | vars.sort() |
|
170 | vars.sort() | |
156 | if vars: |
|
171 | if vars: | |
157 | size = max(map(len, vars)) |
|
172 | size = max(map(len, vars)) | |
158 | else: |
|
173 | else: | |
159 | size = 0 |
|
174 | size = 0 | |
160 |
|
175 | |||
161 | print 'Stored variables and their in-db values:' |
|
176 | print 'Stored variables and their in-db values:' | |
162 | fmt = '%-'+str(size)+'s -> %s' |
|
177 | fmt = '%-'+str(size)+'s -> %s' | |
163 | get = db.get |
|
178 | get = db.get | |
164 | for var in vars: |
|
179 | for var in vars: | |
165 | justkey = os.path.basename(var) |
|
180 | justkey = os.path.basename(var) | |
166 | # print 30 first characters from every var |
|
181 | # print 30 first characters from every var | |
167 | print fmt % (justkey, repr(get(var, '<unavailable>'))[:50]) |
|
182 | print fmt % (justkey, repr(get(var, '<unavailable>'))[:50]) | |
168 |
|
183 | |||
169 | # default action - store the variable |
|
184 | # default action - store the variable | |
170 | else: |
|
185 | else: | |
171 | # %store foo >file.txt or >>file.txt |
|
186 | # %store foo >file.txt or >>file.txt | |
172 | if len(args) > 1 and args[1].startswith('>'): |
|
187 | if len(args) > 1 and args[1].startswith('>'): | |
173 | fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) |
|
188 | fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) | |
174 | if args[1].startswith('>>'): |
|
189 | if args[1].startswith('>>'): | |
175 | fil = open(fnam, 'a') |
|
190 | fil = open(fnam, 'a') | |
176 | else: |
|
191 | else: | |
177 | fil = open(fnam, 'w') |
|
192 | fil = open(fnam, 'w') | |
178 | obj = ip.ev(args[0]) |
|
193 | obj = ip.ev(args[0]) | |
179 | print "Writing '%s' (%s) to file '%s'." % (args[0], |
|
194 | print "Writing '%s' (%s) to file '%s'." % (args[0], | |
180 | obj.__class__.__name__, fnam) |
|
195 | obj.__class__.__name__, fnam) | |
181 |
|
196 | |||
182 |
|
197 | |||
183 | if not isinstance (obj, basestring): |
|
198 | if not isinstance (obj, basestring): | |
184 | from pprint import pprint |
|
199 | from pprint import pprint | |
185 | pprint(obj, fil) |
|
200 | pprint(obj, fil) | |
186 | else: |
|
201 | else: | |
187 | fil.write(obj) |
|
202 | fil.write(obj) | |
188 | if not obj.endswith('\n'): |
|
203 | if not obj.endswith('\n'): | |
189 | fil.write('\n') |
|
204 | fil.write('\n') | |
190 |
|
205 | |||
191 | fil.close() |
|
206 | fil.close() | |
192 | return |
|
207 | return | |
193 |
|
208 | |||
194 | # %store foo |
|
209 | # %store foo | |
195 | try: |
|
210 | try: | |
196 | obj = ip.user_ns[args[0]] |
|
211 | obj = ip.user_ns[args[0]] | |
197 | except KeyError: |
|
212 | except KeyError: | |
198 | # it might be an alias |
|
213 | # it might be an alias | |
199 | # This needs to be refactored to use the new AliasManager stuff. |
|
214 | # This needs to be refactored to use the new AliasManager stuff. | |
200 | if args[0] in ip.alias_manager: |
|
215 | if args[0] in ip.alias_manager: | |
201 | name = args[0] |
|
216 | name = args[0] | |
202 | nargs, cmd = ip.alias_manager.alias_table[ name ] |
|
217 | nargs, cmd = ip.alias_manager.alias_table[ name ] | |
203 | staliases = db.get('stored_aliases',{}) |
|
218 | staliases = db.get('stored_aliases',{}) | |
204 | staliases[ name ] = cmd |
|
219 | staliases[ name ] = cmd | |
205 | db['stored_aliases'] = staliases |
|
220 | db['stored_aliases'] = staliases | |
206 | print "Alias stored: %s (%s)" % (name, cmd) |
|
221 | print "Alias stored: %s (%s)" % (name, cmd) | |
207 | return |
|
222 | return | |
208 | else: |
|
223 | else: | |
209 | raise UsageError("Unknown variable '%s'" % args[0]) |
|
224 | raise UsageError("Unknown variable '%s'" % args[0]) | |
210 |
|
225 | |||
211 | else: |
|
226 | else: | |
212 | if isinstance(inspect.getmodule(obj), FakeModule): |
|
227 | if isinstance(inspect.getmodule(obj), FakeModule): | |
213 | print textwrap.dedent("""\ |
|
228 | print textwrap.dedent("""\ | |
214 | Warning:%s is %s |
|
229 | Warning:%s is %s | |
215 | Proper storage of interactively declared classes (or instances |
|
230 | Proper storage of interactively declared classes (or instances | |
216 | of those classes) is not possible! Only instances |
|
231 | of those classes) is not possible! Only instances | |
217 | of classes in real modules on file system can be %%store'd. |
|
232 | of classes in real modules on file system can be %%store'd. | |
218 | """ % (args[0], obj) ) |
|
233 | """ % (args[0], obj) ) | |
219 | return |
|
234 | return | |
220 | #pickled = pickle.dumps(obj) |
|
235 | #pickled = pickle.dumps(obj) | |
221 | db[ 'autorestore/' + args[0] ] = obj |
|
236 | db[ 'autorestore/' + args[0] ] = obj | |
222 | print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__) |
|
237 | print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__) | |
223 |
|
238 | |||
224 |
|
239 | |||
225 | def load_ipython_extension(ip): |
|
240 | def load_ipython_extension(ip): | |
226 | """Load the extension in IPython.""" |
|
241 | """Load the extension in IPython.""" | |
227 | ip.register_magics(StoreMagics) |
|
242 | ip.register_magics(StoreMagics) | |
|
243 |
@@ -1,32 +1,50 b'' | |||||
1 | import tempfile, os |
|
1 | import tempfile, os | |
2 |
|
2 | |||
|
3 | from IPython.config.loader import Config | |||
3 | import nose.tools as nt |
|
4 | import nose.tools as nt | |
4 |
|
5 | |||
5 | ip = get_ipython() |
|
6 | ip = get_ipython() | |
6 | ip.magic('load_ext storemagic') |
|
7 | ip.magic('load_ext storemagic') | |
7 |
|
8 | |||
8 | def test_store_restore(): |
|
9 | def test_store_restore(): | |
9 | ip.user_ns['foo'] = 78 |
|
10 | ip.user_ns['foo'] = 78 | |
10 | ip.magic('alias bar echo "hello"') |
|
11 | ip.magic('alias bar echo "hello"') | |
11 | tmpd = tempfile.mkdtemp() |
|
12 | tmpd = tempfile.mkdtemp() | |
12 | ip.magic('cd ' + tmpd) |
|
13 | ip.magic('cd ' + tmpd) | |
13 | ip.magic('store foo') |
|
14 | ip.magic('store foo') | |
14 | ip.magic('store bar') |
|
15 | ip.magic('store bar') | |
15 |
|
16 | |||
16 | # Check storing |
|
17 | # Check storing | |
17 | nt.assert_equal(ip.db['autorestore/foo'], 78) |
|
18 | nt.assert_equal(ip.db['autorestore/foo'], 78) | |
18 | nt.assert_in('bar', ip.db['stored_aliases']) |
|
19 | nt.assert_in('bar', ip.db['stored_aliases']) | |
19 |
|
20 | |||
20 | # Remove those items |
|
21 | # Remove those items | |
21 | ip.user_ns.pop('foo', None) |
|
22 | ip.user_ns.pop('foo', None) | |
22 | ip.alias_manager.undefine_alias('bar') |
|
23 | ip.alias_manager.undefine_alias('bar') | |
23 | ip.magic('cd -') |
|
24 | ip.magic('cd -') | |
24 | ip.user_ns['_dh'][:] = [] |
|
25 | ip.user_ns['_dh'][:] = [] | |
25 |
|
26 | |||
26 | # Check restoring |
|
27 | # Check restoring | |
27 | ip.magic('store -r') |
|
28 | ip.magic('store -r') | |
28 | nt.assert_equal(ip.user_ns['foo'], 78) |
|
29 | nt.assert_equal(ip.user_ns['foo'], 78) | |
29 | nt.assert_in('bar', ip.alias_manager.alias_table) |
|
30 | nt.assert_in('bar', ip.alias_manager.alias_table) | |
30 | nt.assert_in(os.path.realpath(tmpd), ip.user_ns['_dh']) |
|
31 | nt.assert_in(os.path.realpath(tmpd), ip.user_ns['_dh']) | |
31 |
|
32 | |||
32 | os.rmdir(tmpd) |
|
33 | os.rmdir(tmpd) | |
|
34 | ||||
|
35 | def test_autorestore(): | |||
|
36 | ip.user_ns['foo'] = 95 | |||
|
37 | ip.magic('store foo') | |||
|
38 | del ip.user_ns['foo'] | |||
|
39 | c = Config() | |||
|
40 | c.StoreMagics.autorestore = False | |||
|
41 | orig_config = ip.config | |||
|
42 | try: | |||
|
43 | ip.config = c | |||
|
44 | ip.extension_manager.reload_extension('storemagic') | |||
|
45 | nt.assert_not_in('foo', ip.user_ns) | |||
|
46 | c.StoreMagics.autorestore = True | |||
|
47 | ip.extension_manager.reload_extension('storemagic') | |||
|
48 | nt.assert_equal(ip.user_ns['foo'], 95) | |||
|
49 | finally: | |||
|
50 | ip.config = orig_config |
@@ -1,386 +1,388 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.extensions.storemagic import StoreMagics | |||
50 | from IPython.terminal.interactiveshell import TerminalInteractiveShell |
|
51 | from IPython.terminal.interactiveshell import TerminalInteractiveShell | |
51 | from IPython.utils import warn |
|
52 | from IPython.utils import warn | |
52 | from IPython.utils.path import get_ipython_dir, check_for_old_config |
|
53 | from IPython.utils.path import get_ipython_dir, check_for_old_config | |
53 | from IPython.utils.traitlets import ( |
|
54 | from IPython.utils.traitlets import ( | |
54 | Bool, List, Dict, |
|
55 | Bool, List, Dict, | |
55 | ) |
|
56 | ) | |
56 |
|
57 | |||
57 | #----------------------------------------------------------------------------- |
|
58 | #----------------------------------------------------------------------------- | |
58 | # Globals, utilities and helpers |
|
59 | # Globals, utilities and helpers | |
59 | #----------------------------------------------------------------------------- |
|
60 | #----------------------------------------------------------------------------- | |
60 |
|
61 | |||
61 | _examples = """ |
|
62 | _examples = """ | |
62 | ipython --matplotlib # enable matplotlib integration |
|
63 | ipython --matplotlib # enable matplotlib integration | |
63 | ipython --matplotlib=qt # enable matplotlib integration with qt4 backend |
|
64 | ipython --matplotlib=qt # enable matplotlib integration with qt4 backend | |
64 |
|
65 | |||
65 | ipython --log-level=DEBUG # set logging to DEBUG |
|
66 | ipython --log-level=DEBUG # set logging to DEBUG | |
66 | ipython --profile=foo # start with profile foo |
|
67 | ipython --profile=foo # start with profile foo | |
67 |
|
68 | |||
68 | ipython qtconsole # start the qtconsole GUI application |
|
69 | ipython qtconsole # start the qtconsole GUI application | |
69 | ipython help qtconsole # show the help for the qtconsole subcmd |
|
70 | ipython help qtconsole # show the help for the qtconsole subcmd | |
70 |
|
71 | |||
71 | ipython console # start the terminal-based console application |
|
72 | ipython console # start the terminal-based console application | |
72 | ipython help console # show the help for the console subcmd |
|
73 | ipython help console # show the help for the console subcmd | |
73 |
|
74 | |||
74 | ipython notebook # start the IPython notebook |
|
75 | ipython notebook # start the IPython notebook | |
75 | ipython help notebook # show the help for the notebook subcmd |
|
76 | ipython help notebook # show the help for the notebook subcmd | |
76 |
|
77 | |||
77 | ipython profile create foo # create profile foo w/ default config files |
|
78 | ipython profile create foo # create profile foo w/ default config files | |
78 | ipython help profile # show the help for the profile subcmd |
|
79 | ipython help profile # show the help for the profile subcmd | |
79 |
|
80 | |||
80 | ipython locate # print the path to the IPython directory |
|
81 | ipython locate # print the path to the IPython directory | |
81 | ipython locate profile foo # print the path to the directory for profile `foo` |
|
82 | ipython locate profile foo # print the path to the directory for profile `foo` | |
82 |
|
83 | |||
83 | ipython nbconvert # convert notebooks to/from other formats |
|
84 | ipython nbconvert # convert notebooks to/from other formats | |
84 | """ |
|
85 | """ | |
85 |
|
86 | |||
86 | #----------------------------------------------------------------------------- |
|
87 | #----------------------------------------------------------------------------- | |
87 | # Crash handler for this application |
|
88 | # Crash handler for this application | |
88 | #----------------------------------------------------------------------------- |
|
89 | #----------------------------------------------------------------------------- | |
89 |
|
90 | |||
90 | class IPAppCrashHandler(CrashHandler): |
|
91 | class IPAppCrashHandler(CrashHandler): | |
91 | """sys.excepthook for IPython itself, leaves a detailed report on disk.""" |
|
92 | """sys.excepthook for IPython itself, leaves a detailed report on disk.""" | |
92 |
|
93 | |||
93 | def __init__(self, app): |
|
94 | def __init__(self, app): | |
94 | contact_name = release.author |
|
95 | contact_name = release.author | |
95 | contact_email = release.author_email |
|
96 | contact_email = release.author_email | |
96 | bug_tracker = 'https://github.com/ipython/ipython/issues' |
|
97 | bug_tracker = 'https://github.com/ipython/ipython/issues' | |
97 | super(IPAppCrashHandler,self).__init__( |
|
98 | super(IPAppCrashHandler,self).__init__( | |
98 | app, contact_name, contact_email, bug_tracker |
|
99 | app, contact_name, contact_email, bug_tracker | |
99 | ) |
|
100 | ) | |
100 |
|
101 | |||
101 | def make_report(self,traceback): |
|
102 | def make_report(self,traceback): | |
102 | """Return a string containing a crash report.""" |
|
103 | """Return a string containing a crash report.""" | |
103 |
|
104 | |||
104 | sec_sep = self.section_sep |
|
105 | sec_sep = self.section_sep | |
105 | # Start with parent report |
|
106 | # Start with parent report | |
106 | report = [super(IPAppCrashHandler, self).make_report(traceback)] |
|
107 | report = [super(IPAppCrashHandler, self).make_report(traceback)] | |
107 | # Add interactive-specific info we may have |
|
108 | # Add interactive-specific info we may have | |
108 | rpt_add = report.append |
|
109 | rpt_add = report.append | |
109 | try: |
|
110 | try: | |
110 | rpt_add(sec_sep+"History of session input:") |
|
111 | rpt_add(sec_sep+"History of session input:") | |
111 | for line in self.app.shell.user_ns['_ih']: |
|
112 | for line in self.app.shell.user_ns['_ih']: | |
112 | rpt_add(line) |
|
113 | rpt_add(line) | |
113 | rpt_add('\n*** Last line of input (may not be in above history):\n') |
|
114 | rpt_add('\n*** Last line of input (may not be in above history):\n') | |
114 | rpt_add(self.app.shell._last_input_line+'\n') |
|
115 | rpt_add(self.app.shell._last_input_line+'\n') | |
115 | except: |
|
116 | except: | |
116 | pass |
|
117 | pass | |
117 |
|
118 | |||
118 | return ''.join(report) |
|
119 | return ''.join(report) | |
119 |
|
120 | |||
120 | #----------------------------------------------------------------------------- |
|
121 | #----------------------------------------------------------------------------- | |
121 | # Aliases and Flags |
|
122 | # Aliases and Flags | |
122 | #----------------------------------------------------------------------------- |
|
123 | #----------------------------------------------------------------------------- | |
123 | flags = dict(base_flags) |
|
124 | flags = dict(base_flags) | |
124 | flags.update(shell_flags) |
|
125 | flags.update(shell_flags) | |
125 | frontend_flags = {} |
|
126 | frontend_flags = {} | |
126 | addflag = lambda *args: frontend_flags.update(boolean_flag(*args)) |
|
127 | addflag = lambda *args: frontend_flags.update(boolean_flag(*args)) | |
127 | addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax', |
|
128 | addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax', | |
128 | 'Turn on auto editing of files with syntax errors.', |
|
129 | 'Turn on auto editing of files with syntax errors.', | |
129 | 'Turn off auto editing of files with syntax errors.' |
|
130 | 'Turn off auto editing of files with syntax errors.' | |
130 | ) |
|
131 | ) | |
131 | addflag('banner', 'TerminalIPythonApp.display_banner', |
|
132 | addflag('banner', 'TerminalIPythonApp.display_banner', | |
132 | "Display a banner upon starting IPython.", |
|
133 | "Display a banner upon starting IPython.", | |
133 | "Don't display a banner upon starting IPython." |
|
134 | "Don't display a banner upon starting IPython." | |
134 | ) |
|
135 | ) | |
135 | addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit', |
|
136 | addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit', | |
136 | """Set to confirm when you try to exit IPython with an EOF (Control-D |
|
137 | """Set to confirm when you try to exit IPython with an EOF (Control-D | |
137 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', |
|
138 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', | |
138 | you can force a direct exit without any confirmation.""", |
|
139 | you can force a direct exit without any confirmation.""", | |
139 | "Don't prompt the user when exiting." |
|
140 | "Don't prompt the user when exiting." | |
140 | ) |
|
141 | ) | |
141 | addflag('term-title', 'TerminalInteractiveShell.term_title', |
|
142 | addflag('term-title', 'TerminalInteractiveShell.term_title', | |
142 | "Enable auto setting the terminal title.", |
|
143 | "Enable auto setting the terminal title.", | |
143 | "Disable auto setting the terminal title." |
|
144 | "Disable auto setting the terminal title." | |
144 | ) |
|
145 | ) | |
145 | classic_config = Config() |
|
146 | classic_config = Config() | |
146 | classic_config.InteractiveShell.cache_size = 0 |
|
147 | classic_config.InteractiveShell.cache_size = 0 | |
147 | classic_config.PlainTextFormatter.pprint = False |
|
148 | classic_config.PlainTextFormatter.pprint = False | |
148 | classic_config.PromptManager.in_template = '>>> ' |
|
149 | classic_config.PromptManager.in_template = '>>> ' | |
149 | classic_config.PromptManager.in2_template = '... ' |
|
150 | classic_config.PromptManager.in2_template = '... ' | |
150 | classic_config.PromptManager.out_template = '' |
|
151 | classic_config.PromptManager.out_template = '' | |
151 | classic_config.InteractiveShell.separate_in = '' |
|
152 | classic_config.InteractiveShell.separate_in = '' | |
152 | classic_config.InteractiveShell.separate_out = '' |
|
153 | classic_config.InteractiveShell.separate_out = '' | |
153 | classic_config.InteractiveShell.separate_out2 = '' |
|
154 | classic_config.InteractiveShell.separate_out2 = '' | |
154 | classic_config.InteractiveShell.colors = 'NoColor' |
|
155 | classic_config.InteractiveShell.colors = 'NoColor' | |
155 | classic_config.InteractiveShell.xmode = 'Plain' |
|
156 | classic_config.InteractiveShell.xmode = 'Plain' | |
156 |
|
157 | |||
157 | frontend_flags['classic']=( |
|
158 | frontend_flags['classic']=( | |
158 | classic_config, |
|
159 | classic_config, | |
159 | "Gives IPython a similar feel to the classic Python prompt." |
|
160 | "Gives IPython a similar feel to the classic Python prompt." | |
160 | ) |
|
161 | ) | |
161 | # # log doesn't make so much sense this way anymore |
|
162 | # # log doesn't make so much sense this way anymore | |
162 | # paa('--log','-l', |
|
163 | # paa('--log','-l', | |
163 | # action='store_true', dest='InteractiveShell.logstart', |
|
164 | # action='store_true', dest='InteractiveShell.logstart', | |
164 | # help="Start logging to the default log file (./ipython_log.py).") |
|
165 | # help="Start logging to the default log file (./ipython_log.py).") | |
165 | # |
|
166 | # | |
166 | # # quick is harder to implement |
|
167 | # # quick is harder to implement | |
167 | frontend_flags['quick']=( |
|
168 | frontend_flags['quick']=( | |
168 | {'TerminalIPythonApp' : {'quick' : True}}, |
|
169 | {'TerminalIPythonApp' : {'quick' : True}}, | |
169 | "Enable quick startup with no config files." |
|
170 | "Enable quick startup with no config files." | |
170 | ) |
|
171 | ) | |
171 |
|
172 | |||
172 | frontend_flags['i'] = ( |
|
173 | frontend_flags['i'] = ( | |
173 | {'TerminalIPythonApp' : {'force_interact' : True}}, |
|
174 | {'TerminalIPythonApp' : {'force_interact' : True}}, | |
174 | """If running code from the command line, become interactive afterwards. |
|
175 | """If running code from the command line, become interactive afterwards. | |
175 | Note: can also be given simply as '-i.'""" |
|
176 | Note: can also be given simply as '-i.'""" | |
176 | ) |
|
177 | ) | |
177 | flags.update(frontend_flags) |
|
178 | flags.update(frontend_flags) | |
178 |
|
179 | |||
179 | aliases = dict(base_aliases) |
|
180 | aliases = dict(base_aliases) | |
180 | aliases.update(shell_aliases) |
|
181 | aliases.update(shell_aliases) | |
181 |
|
182 | |||
182 | #----------------------------------------------------------------------------- |
|
183 | #----------------------------------------------------------------------------- | |
183 | # Main classes and functions |
|
184 | # Main classes and functions | |
184 | #----------------------------------------------------------------------------- |
|
185 | #----------------------------------------------------------------------------- | |
185 |
|
186 | |||
186 |
|
187 | |||
187 | class LocateIPythonApp(BaseIPythonApplication): |
|
188 | class LocateIPythonApp(BaseIPythonApplication): | |
188 | description = """print the path to the IPython dir""" |
|
189 | description = """print the path to the IPython dir""" | |
189 | subcommands = Dict(dict( |
|
190 | subcommands = Dict(dict( | |
190 | profile=('IPython.core.profileapp.ProfileLocate', |
|
191 | profile=('IPython.core.profileapp.ProfileLocate', | |
191 | "print the path to an IPython profile directory", |
|
192 | "print the path to an IPython profile directory", | |
192 | ), |
|
193 | ), | |
193 | )) |
|
194 | )) | |
194 | def start(self): |
|
195 | def start(self): | |
195 | if self.subapp is not None: |
|
196 | if self.subapp is not None: | |
196 | return self.subapp.start() |
|
197 | return self.subapp.start() | |
197 | else: |
|
198 | else: | |
198 | print self.ipython_dir |
|
199 | print self.ipython_dir | |
199 |
|
200 | |||
200 |
|
201 | |||
201 | class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp): |
|
202 | class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp): | |
202 | name = u'ipython' |
|
203 | name = u'ipython' | |
203 | description = usage.cl_usage |
|
204 | description = usage.cl_usage | |
204 | crash_handler_class = IPAppCrashHandler |
|
205 | crash_handler_class = IPAppCrashHandler | |
205 | examples = _examples |
|
206 | examples = _examples | |
206 |
|
207 | |||
207 | flags = Dict(flags) |
|
208 | flags = Dict(flags) | |
208 | aliases = Dict(aliases) |
|
209 | aliases = Dict(aliases) | |
209 | classes = List() |
|
210 | classes = List() | |
210 | def _classes_default(self): |
|
211 | def _classes_default(self): | |
211 | """This has to be in a method, for TerminalIPythonApp to be available.""" |
|
212 | """This has to be in a method, for TerminalIPythonApp to be available.""" | |
212 | return [ |
|
213 | return [ | |
213 | InteractiveShellApp, # ShellApp comes before TerminalApp, because |
|
214 | InteractiveShellApp, # ShellApp comes before TerminalApp, because | |
214 | self.__class__, # it will also affect subclasses (e.g. QtConsole) |
|
215 | self.__class__, # it will also affect subclasses (e.g. QtConsole) | |
215 | TerminalInteractiveShell, |
|
216 | TerminalInteractiveShell, | |
216 | PromptManager, |
|
217 | PromptManager, | |
217 | HistoryManager, |
|
218 | HistoryManager, | |
218 | ProfileDir, |
|
219 | ProfileDir, | |
219 | PlainTextFormatter, |
|
220 | PlainTextFormatter, | |
220 | IPCompleter, |
|
221 | IPCompleter, | |
221 | ScriptMagics, |
|
222 | ScriptMagics, | |
|
223 | StoreMagics, | |||
222 | ] |
|
224 | ] | |
223 |
|
225 | |||
224 | subcommands = Dict(dict( |
|
226 | subcommands = Dict(dict( | |
225 | qtconsole=('IPython.qt.console.qtconsoleapp.IPythonQtConsoleApp', |
|
227 | qtconsole=('IPython.qt.console.qtconsoleapp.IPythonQtConsoleApp', | |
226 | """Launch the IPython Qt Console.""" |
|
228 | """Launch the IPython Qt Console.""" | |
227 | ), |
|
229 | ), | |
228 | notebook=('IPython.html.notebookapp.NotebookApp', |
|
230 | notebook=('IPython.html.notebookapp.NotebookApp', | |
229 | """Launch the IPython HTML Notebook Server.""" |
|
231 | """Launch the IPython HTML Notebook Server.""" | |
230 | ), |
|
232 | ), | |
231 | profile = ("IPython.core.profileapp.ProfileApp", |
|
233 | profile = ("IPython.core.profileapp.ProfileApp", | |
232 | "Create and manage IPython profiles." |
|
234 | "Create and manage IPython profiles." | |
233 | ), |
|
235 | ), | |
234 | kernel = ("IPython.kernel.zmq.kernelapp.IPKernelApp", |
|
236 | kernel = ("IPython.kernel.zmq.kernelapp.IPKernelApp", | |
235 | "Start a kernel without an attached frontend." |
|
237 | "Start a kernel without an attached frontend." | |
236 | ), |
|
238 | ), | |
237 | console=('IPython.terminal.console.app.ZMQTerminalIPythonApp', |
|
239 | console=('IPython.terminal.console.app.ZMQTerminalIPythonApp', | |
238 | """Launch the IPython terminal-based Console.""" |
|
240 | """Launch the IPython terminal-based Console.""" | |
239 | ), |
|
241 | ), | |
240 | locate=('IPython.terminal.ipapp.LocateIPythonApp', |
|
242 | locate=('IPython.terminal.ipapp.LocateIPythonApp', | |
241 | LocateIPythonApp.description |
|
243 | LocateIPythonApp.description | |
242 | ), |
|
244 | ), | |
243 | history=('IPython.core.historyapp.HistoryApp', |
|
245 | history=('IPython.core.historyapp.HistoryApp', | |
244 | "Manage the IPython history database." |
|
246 | "Manage the IPython history database." | |
245 | ), |
|
247 | ), | |
246 | nbconvert=('IPython.nbconvert.nbconvertapp.NbConvertApp', |
|
248 | nbconvert=('IPython.nbconvert.nbconvertapp.NbConvertApp', | |
247 | "Convert notebooks to/from other formats." |
|
249 | "Convert notebooks to/from other formats." | |
248 | ), |
|
250 | ), | |
249 | )) |
|
251 | )) | |
250 |
|
252 | |||
251 | # *do* autocreate requested profile, but don't create the config file. |
|
253 | # *do* autocreate requested profile, but don't create the config file. | |
252 | auto_create=Bool(True) |
|
254 | auto_create=Bool(True) | |
253 | # configurables |
|
255 | # configurables | |
254 | ignore_old_config=Bool(False, config=True, |
|
256 | ignore_old_config=Bool(False, config=True, | |
255 | help="Suppress warning messages about legacy config files" |
|
257 | help="Suppress warning messages about legacy config files" | |
256 | ) |
|
258 | ) | |
257 | quick = Bool(False, config=True, |
|
259 | quick = Bool(False, config=True, | |
258 | help="""Start IPython quickly by skipping the loading of config files.""" |
|
260 | help="""Start IPython quickly by skipping the loading of config files.""" | |
259 | ) |
|
261 | ) | |
260 | def _quick_changed(self, name, old, new): |
|
262 | def _quick_changed(self, name, old, new): | |
261 | if new: |
|
263 | if new: | |
262 | self.load_config_file = lambda *a, **kw: None |
|
264 | self.load_config_file = lambda *a, **kw: None | |
263 | self.ignore_old_config=True |
|
265 | self.ignore_old_config=True | |
264 |
|
266 | |||
265 | display_banner = Bool(True, config=True, |
|
267 | display_banner = Bool(True, config=True, | |
266 | help="Whether to display a banner upon starting IPython." |
|
268 | help="Whether to display a banner upon starting IPython." | |
267 | ) |
|
269 | ) | |
268 |
|
270 | |||
269 | # if there is code of files to run from the cmd line, don't interact |
|
271 | # if there is code of files to run from the cmd line, don't interact | |
270 | # unless the --i flag (App.force_interact) is true. |
|
272 | # unless the --i flag (App.force_interact) is true. | |
271 | force_interact = Bool(False, config=True, |
|
273 | force_interact = Bool(False, config=True, | |
272 | help="""If a command or file is given via the command-line, |
|
274 | help="""If a command or file is given via the command-line, | |
273 | e.g. 'ipython foo.py""" |
|
275 | e.g. 'ipython foo.py""" | |
274 | ) |
|
276 | ) | |
275 | def _force_interact_changed(self, name, old, new): |
|
277 | def _force_interact_changed(self, name, old, new): | |
276 | if new: |
|
278 | if new: | |
277 | self.interact = True |
|
279 | self.interact = True | |
278 |
|
280 | |||
279 | def _file_to_run_changed(self, name, old, new): |
|
281 | def _file_to_run_changed(self, name, old, new): | |
280 | if new: |
|
282 | if new: | |
281 | self.something_to_run = True |
|
283 | self.something_to_run = True | |
282 | if new and not self.force_interact: |
|
284 | if new and not self.force_interact: | |
283 | self.interact = False |
|
285 | self.interact = False | |
284 | _code_to_run_changed = _file_to_run_changed |
|
286 | _code_to_run_changed = _file_to_run_changed | |
285 | _module_to_run_changed = _file_to_run_changed |
|
287 | _module_to_run_changed = _file_to_run_changed | |
286 |
|
288 | |||
287 | # internal, not-configurable |
|
289 | # internal, not-configurable | |
288 | interact=Bool(True) |
|
290 | interact=Bool(True) | |
289 | something_to_run=Bool(False) |
|
291 | something_to_run=Bool(False) | |
290 |
|
292 | |||
291 | def parse_command_line(self, argv=None): |
|
293 | def parse_command_line(self, argv=None): | |
292 | """override to allow old '-pylab' flag with deprecation warning""" |
|
294 | """override to allow old '-pylab' flag with deprecation warning""" | |
293 |
|
295 | |||
294 | argv = sys.argv[1:] if argv is None else argv |
|
296 | argv = sys.argv[1:] if argv is None else argv | |
295 |
|
297 | |||
296 | if '-pylab' in argv: |
|
298 | if '-pylab' in argv: | |
297 | # deprecated `-pylab` given, |
|
299 | # deprecated `-pylab` given, | |
298 | # warn and transform into current syntax |
|
300 | # warn and transform into current syntax | |
299 | argv = argv[:] # copy, don't clobber |
|
301 | argv = argv[:] # copy, don't clobber | |
300 | idx = argv.index('-pylab') |
|
302 | idx = argv.index('-pylab') | |
301 | warn.warn("`-pylab` flag has been deprecated.\n" |
|
303 | warn.warn("`-pylab` flag has been deprecated.\n" | |
302 | " Use `--matplotlib <backend>` and import pylab manually.") |
|
304 | " Use `--matplotlib <backend>` and import pylab manually.") | |
303 | argv[idx] = '--pylab' |
|
305 | argv[idx] = '--pylab' | |
304 |
|
306 | |||
305 | return super(TerminalIPythonApp, self).parse_command_line(argv) |
|
307 | return super(TerminalIPythonApp, self).parse_command_line(argv) | |
306 |
|
308 | |||
307 | @catch_config_error |
|
309 | @catch_config_error | |
308 | def initialize(self, argv=None): |
|
310 | def initialize(self, argv=None): | |
309 | """Do actions after construct, but before starting the app.""" |
|
311 | """Do actions after construct, but before starting the app.""" | |
310 | super(TerminalIPythonApp, self).initialize(argv) |
|
312 | super(TerminalIPythonApp, self).initialize(argv) | |
311 | if self.subapp is not None: |
|
313 | if self.subapp is not None: | |
312 | # don't bother initializing further, starting subapp |
|
314 | # don't bother initializing further, starting subapp | |
313 | return |
|
315 | return | |
314 | if not self.ignore_old_config: |
|
316 | if not self.ignore_old_config: | |
315 | check_for_old_config(self.ipython_dir) |
|
317 | check_for_old_config(self.ipython_dir) | |
316 | # print self.extra_args |
|
318 | # print self.extra_args | |
317 | if self.extra_args and not self.something_to_run: |
|
319 | if self.extra_args and not self.something_to_run: | |
318 | self.file_to_run = self.extra_args[0] |
|
320 | self.file_to_run = self.extra_args[0] | |
319 | self.init_path() |
|
321 | self.init_path() | |
320 | # create the shell |
|
322 | # create the shell | |
321 | self.init_shell() |
|
323 | self.init_shell() | |
322 | # and draw the banner |
|
324 | # and draw the banner | |
323 | self.init_banner() |
|
325 | self.init_banner() | |
324 | # Now a variety of things that happen after the banner is printed. |
|
326 | # Now a variety of things that happen after the banner is printed. | |
325 | self.init_gui_pylab() |
|
327 | self.init_gui_pylab() | |
326 | self.init_extensions() |
|
328 | self.init_extensions() | |
327 | self.init_code() |
|
329 | self.init_code() | |
328 |
|
330 | |||
329 | def init_shell(self): |
|
331 | def init_shell(self): | |
330 | """initialize the InteractiveShell instance""" |
|
332 | """initialize the InteractiveShell instance""" | |
331 | # Create an InteractiveShell instance. |
|
333 | # Create an InteractiveShell instance. | |
332 | # shell.display_banner should always be False for the terminal |
|
334 | # shell.display_banner should always be False for the terminal | |
333 | # based app, because we call shell.show_banner() by hand below |
|
335 | # based app, because we call shell.show_banner() by hand below | |
334 | # so the banner shows *before* all extension loading stuff. |
|
336 | # so the banner shows *before* all extension loading stuff. | |
335 | self.shell = TerminalInteractiveShell.instance(parent=self, |
|
337 | self.shell = TerminalInteractiveShell.instance(parent=self, | |
336 | display_banner=False, profile_dir=self.profile_dir, |
|
338 | display_banner=False, profile_dir=self.profile_dir, | |
337 | ipython_dir=self.ipython_dir, user_ns=self.user_ns) |
|
339 | ipython_dir=self.ipython_dir, user_ns=self.user_ns) | |
338 | self.shell.configurables.append(self) |
|
340 | self.shell.configurables.append(self) | |
339 |
|
341 | |||
340 | def init_banner(self): |
|
342 | def init_banner(self): | |
341 | """optionally display the banner""" |
|
343 | """optionally display the banner""" | |
342 | if self.display_banner and self.interact: |
|
344 | if self.display_banner and self.interact: | |
343 | self.shell.show_banner() |
|
345 | self.shell.show_banner() | |
344 | # Make sure there is a space below the banner. |
|
346 | # Make sure there is a space below the banner. | |
345 | if self.log_level <= logging.INFO: print |
|
347 | if self.log_level <= logging.INFO: print | |
346 |
|
348 | |||
347 | def _pylab_changed(self, name, old, new): |
|
349 | def _pylab_changed(self, name, old, new): | |
348 | """Replace --pylab='inline' with --pylab='auto'""" |
|
350 | """Replace --pylab='inline' with --pylab='auto'""" | |
349 | if new == 'inline': |
|
351 | if new == 'inline': | |
350 | warn.warn("'inline' not available as pylab backend, " |
|
352 | warn.warn("'inline' not available as pylab backend, " | |
351 | "using 'auto' instead.") |
|
353 | "using 'auto' instead.") | |
352 | self.pylab = 'auto' |
|
354 | self.pylab = 'auto' | |
353 |
|
355 | |||
354 | def start(self): |
|
356 | def start(self): | |
355 | if self.subapp is not None: |
|
357 | if self.subapp is not None: | |
356 | return self.subapp.start() |
|
358 | return self.subapp.start() | |
357 | # perform any prexec steps: |
|
359 | # perform any prexec steps: | |
358 | if self.interact: |
|
360 | if self.interact: | |
359 | self.log.debug("Starting IPython's mainloop...") |
|
361 | self.log.debug("Starting IPython's mainloop...") | |
360 | self.shell.mainloop() |
|
362 | self.shell.mainloop() | |
361 | else: |
|
363 | else: | |
362 | self.log.debug("IPython not interactive...") |
|
364 | self.log.debug("IPython not interactive...") | |
363 |
|
365 | |||
364 |
|
366 | |||
365 | def load_default_config(ipython_dir=None): |
|
367 | def load_default_config(ipython_dir=None): | |
366 | """Load the default config file from the default ipython_dir. |
|
368 | """Load the default config file from the default ipython_dir. | |
367 |
|
369 | |||
368 | This is useful for embedded shells. |
|
370 | This is useful for embedded shells. | |
369 | """ |
|
371 | """ | |
370 | if ipython_dir is None: |
|
372 | if ipython_dir is None: | |
371 | ipython_dir = get_ipython_dir() |
|
373 | ipython_dir = get_ipython_dir() | |
372 | profile_dir = os.path.join(ipython_dir, 'profile_default') |
|
374 | profile_dir = os.path.join(ipython_dir, 'profile_default') | |
373 | cl = PyFileConfigLoader("ipython_config.py", profile_dir) |
|
375 | cl = PyFileConfigLoader("ipython_config.py", profile_dir) | |
374 | try: |
|
376 | try: | |
375 | config = cl.load_config() |
|
377 | config = cl.load_config() | |
376 | except ConfigFileNotFound: |
|
378 | except ConfigFileNotFound: | |
377 | # no config found |
|
379 | # no config found | |
378 | config = Config() |
|
380 | config = Config() | |
379 | return config |
|
381 | return config | |
380 |
|
382 | |||
381 |
|
383 | |||
382 | launch_new_instance = TerminalIPythonApp.launch_instance |
|
384 | launch_new_instance = TerminalIPythonApp.launch_instance | |
383 |
|
385 | |||
384 |
|
386 | |||
385 | if __name__ == '__main__': |
|
387 | if __name__ == '__main__': | |
386 | launch_new_instance() |
|
388 | launch_new_instance() |
General Comments 0
You need to be logged in to leave comments.
Login now