Show More
@@ -20,12 +20,15 b' from IPython.core.magic import Magics, magics_class, line_magic' | |||||
20 | from traitlets import Bool |
|
20 | from traitlets import Bool | |
21 |
|
21 | |||
22 |
|
22 | |||
23 | def restore_aliases(ip): |
|
23 | def restore_aliases(ip, alias=None): | |
24 | staliases = ip.db.get('stored_aliases', {}) |
|
24 | staliases = ip.db.get('stored_aliases', {}) | |
25 | for k,v in staliases.items(): |
|
25 | if alias is None: | |
26 | #print "restore alias",k,v # dbg |
|
26 | for k,v in staliases.items(): | |
27 | #self.alias_table[k] = v |
|
27 | #print "restore alias",k,v # dbg | |
28 | ip.alias_manager.define_alias(k,v) |
|
28 | #self.alias_table[k] = v | |
|
29 | ip.alias_manager.define_alias(k,v) | |||
|
30 | else: | |||
|
31 | ip.alias_manager.define_alias(alias, staliases[alias]) | |||
29 |
|
32 | |||
30 |
|
33 | |||
31 | def refresh_variables(ip): |
|
34 | def refresh_variables(ip): | |
@@ -58,13 +61,13 b' class StoreMagics(Magics):' | |||||
58 | """Lightweight persistence for python variables. |
|
61 | """Lightweight persistence for python variables. | |
59 |
|
62 | |||
60 | Provides the %store magic.""" |
|
63 | Provides the %store magic.""" | |
61 |
|
64 | |||
62 | autorestore = Bool(False, help= |
|
65 | autorestore = Bool(False, help= | |
63 | """If True, any %store-d variables will be automatically restored |
|
66 | """If True, any %store-d variables will be automatically restored | |
64 | when IPython starts. |
|
67 | when IPython starts. | |
65 | """ |
|
68 | """ | |
66 | ).tag(config=True) |
|
69 | ).tag(config=True) | |
67 |
|
70 | |||
68 | def __init__(self, shell): |
|
71 | def __init__(self, shell): | |
69 | super(StoreMagics, self).__init__(shell=shell) |
|
72 | super(StoreMagics, self).__init__(shell=shell) | |
70 | self.shell.configurables.append(self) |
|
73 | self.shell.configurables.append(self) | |
@@ -94,13 +97,13 b' class StoreMagics(Magics):' | |||||
94 |
|
97 | |||
95 | * ``%store`` - Show list of all variables and their current |
|
98 | * ``%store`` - Show list of all variables and their current | |
96 | values |
|
99 | values | |
97 |
* ``%store spam`` |
|
100 | * ``%store spam bar`` - Store the *current* value of the variables spam | |
98 | to disk |
|
101 | and bar to disk | |
99 | * ``%store -d spam`` - Remove the variable and its value from storage |
|
102 | * ``%store -d spam`` - Remove the variable and its value from storage | |
100 | * ``%store -z`` - Remove all variables from storage |
|
103 | * ``%store -z`` - Remove all variables from storage | |
101 |
* ``%store -r`` - Refresh all variables |
|
104 | * ``%store -r`` - Refresh all variables, aliases and directory history | |
102 | current vals) |
|
105 | from store (overwrite current vals) | |
103 | * ``%store -r spam bar`` - Refresh specified variables from store |
|
106 | * ``%store -r spam bar`` - Refresh specified variables and aliases from store | |
104 | (delete current val) |
|
107 | (delete current val) | |
105 | * ``%store foo >a.txt`` - Store value of foo to new file a.txt |
|
108 | * ``%store foo >a.txt`` - Store value of foo to new file a.txt | |
106 | * ``%store foo >>a.txt`` - Append value of foo to file a.txt |
|
109 | * ``%store foo >>a.txt`` - Append value of foo to file a.txt | |
@@ -116,7 +119,7 b' class StoreMagics(Magics):' | |||||
116 | """ |
|
119 | """ | |
117 |
|
120 | |||
118 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') |
|
121 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') | |
119 |
args = argsl.split( |
|
122 | args = argsl.split() | |
120 | ip = self.shell |
|
123 | ip = self.shell | |
121 | db = ip.db |
|
124 | db = ip.db | |
122 | # delete |
|
125 | # delete | |
@@ -141,7 +144,10 b' class StoreMagics(Magics):' | |||||
141 | try: |
|
144 | try: | |
142 | obj = db['autorestore/' + arg] |
|
145 | obj = db['autorestore/' + arg] | |
143 | except KeyError: |
|
146 | except KeyError: | |
144 | print("no stored variable %s" % arg) |
|
147 | try: | |
|
148 | restore_aliases(ip, alias=arg) | |||
|
149 | except KeyError: | |||
|
150 | print("no stored variable or alias %s" % arg) | |||
145 | else: |
|
151 | else: | |
146 | ip.user_ns[arg] = obj |
|
152 | ip.user_ns[arg] = obj | |
147 | else: |
|
153 | else: | |
@@ -189,38 +195,39 b' class StoreMagics(Magics):' | |||||
189 | return |
|
195 | return | |
190 |
|
196 | |||
191 | # %store foo |
|
197 | # %store foo | |
192 |
|
|
198 | for arg in args: | |
193 | obj = ip.user_ns[args[0]] |
|
|||
194 | except KeyError: |
|
|||
195 | # it might be an alias |
|
|||
196 | name = args[0] |
|
|||
197 | try: |
|
199 | try: | |
198 | cmd = ip.alias_manager.retrieve_alias(name) |
|
200 | obj = ip.user_ns[arg] | |
199 |
except |
|
201 | except KeyError: | |
200 | raise UsageError("Unknown variable '%s'" % name) |
|
202 | # it might be an alias | |
201 |
|
203 | name = arg | ||
202 | staliases = db.get('stored_aliases',{}) |
|
204 | try: | |
203 | staliases[name] = cmd |
|
205 | cmd = ip.alias_manager.retrieve_alias(name) | |
204 | db['stored_aliases'] = staliases |
|
206 | except ValueError: | |
205 | print("Alias stored: %s (%s)" % (name, cmd)) |
|
207 | raise UsageError("Unknown variable '%s'" % name) | |
206 | return |
|
208 | ||
207 |
|
209 | staliases = db.get('stored_aliases',{}) | ||
208 | else: |
|
210 | staliases[name] = cmd | |
209 | modname = getattr(inspect.getmodule(obj), '__name__', '') |
|
211 | db['stored_aliases'] = staliases | |
210 | if modname == '__main__': |
|
212 | print("Alias stored: %s (%s)" % (name, cmd)) | |
211 | print(textwrap.dedent("""\ |
|
|||
212 | Warning:%s is %s |
|
|||
213 | Proper storage of interactively declared classes (or instances |
|
|||
214 | of those classes) is not possible! Only instances |
|
|||
215 | of classes in real modules on file system can be %%store'd. |
|
|||
216 | """ % (args[0], obj) )) |
|
|||
217 | return |
|
213 | return | |
218 | #pickled = pickle.dumps(obj) |
|
214 | ||
219 | db[ 'autorestore/' + args[0] ] = obj |
|
215 | else: | |
220 | print("Stored '%s' (%s)" % (args[0], obj.__class__.__name__)) |
|
216 | modname = getattr(inspect.getmodule(obj), '__name__', '') | |
|
217 | if modname == '__main__': | |||
|
218 | print(textwrap.dedent("""\ | |||
|
219 | Warning:%s is %s | |||
|
220 | Proper storage of interactively declared classes (or instances | |||
|
221 | of those classes) is not possible! Only instances | |||
|
222 | of classes in real modules on file system can be %%store'd. | |||
|
223 | """ % (arg, obj) )) | |||
|
224 | return | |||
|
225 | #pickled = pickle.dumps(obj) | |||
|
226 | db[ 'autorestore/' + arg ] = obj | |||
|
227 | print("Stored '%s' (%s)" % (arg, obj.__class__.__name__)) | |||
221 |
|
228 | |||
222 |
|
229 | |||
223 | def load_ipython_extension(ip): |
|
230 | def load_ipython_extension(ip): | |
224 | """Load the extension in IPython.""" |
|
231 | """Load the extension in IPython.""" | |
225 | ip.register_magics(StoreMagics) |
|
232 | ip.register_magics(StoreMagics) | |
226 |
|
233 |
@@ -10,29 +10,42 b' def setup_module():' | |||||
10 | def test_store_restore(): |
|
10 | def test_store_restore(): | |
11 | assert 'bar' not in ip.user_ns, "Error: some other test leaked `bar` in user_ns" |
|
11 | assert 'bar' not in ip.user_ns, "Error: some other test leaked `bar` in user_ns" | |
12 | assert 'foo' not in ip.user_ns, "Error: some other test leaked `foo` in user_ns" |
|
12 | assert 'foo' not in ip.user_ns, "Error: some other test leaked `foo` in user_ns" | |
|
13 | assert 'foobar' not in ip.user_ns, "Error: some other test leaked `foobar` in user_ns" | |||
|
14 | assert 'foobaz' not in ip.user_ns, "Error: some other test leaked `foobaz` in user_ns" | |||
13 | ip.user_ns['foo'] = 78 |
|
15 | ip.user_ns['foo'] = 78 | |
14 | ip.magic('alias bar echo "hello"') |
|
16 | ip.magic('alias bar echo "hello"') | |
|
17 | ip.user_ns['foobar'] = 79 | |||
|
18 | ip.user_ns['foobaz'] = '80' | |||
15 | tmpd = tempfile.mkdtemp() |
|
19 | tmpd = tempfile.mkdtemp() | |
16 | ip.magic('cd ' + tmpd) |
|
20 | ip.magic('cd ' + tmpd) | |
17 | ip.magic('store foo') |
|
21 | ip.magic('store foo') | |
18 | ip.magic('store bar') |
|
22 | ip.magic('store bar') | |
19 |
|
23 | ip.magic('store foobar foobaz') | ||
|
24 | ||||
20 | # Check storing |
|
25 | # Check storing | |
21 | nt.assert_equal(ip.db['autorestore/foo'], 78) |
|
26 | nt.assert_equal(ip.db['autorestore/foo'], 78) | |
22 | nt.assert_in('bar', ip.db['stored_aliases']) |
|
27 | nt.assert_in('bar', ip.db['stored_aliases']) | |
23 |
|
28 | nt.assert_equal(ip.db['autorestore/foobar'], 79) | ||
|
29 | nt.assert_equal(ip.db['autorestore/foobaz'], '80') | |||
|
30 | ||||
24 | # Remove those items |
|
31 | # Remove those items | |
25 | ip.user_ns.pop('foo', None) |
|
32 | ip.user_ns.pop('foo', None) | |
|
33 | ip.user_ns.pop('foobar', None) | |||
|
34 | ip.user_ns.pop('foobaz', None) | |||
26 | ip.alias_manager.undefine_alias('bar') |
|
35 | ip.alias_manager.undefine_alias('bar') | |
27 | ip.magic('cd -') |
|
36 | ip.magic('cd -') | |
28 | ip.user_ns['_dh'][:] = [] |
|
37 | ip.user_ns['_dh'][:] = [] | |
29 |
|
38 | |||
30 | # Check restoring |
|
39 | # Check restoring | |
31 | ip.magic('store -r') |
|
40 | ip.magic('store -r foo bar foobar foobaz') | |
32 | nt.assert_equal(ip.user_ns['foo'], 78) |
|
41 | nt.assert_equal(ip.user_ns['foo'], 78) | |
33 | assert ip.alias_manager.is_alias('bar') |
|
42 | assert ip.alias_manager.is_alias('bar') | |
|
43 | nt.assert_equal(ip.user_ns['foobar'], 79) | |||
|
44 | nt.assert_equal(ip.user_ns['foobaz'], '80') | |||
|
45 | ||||
|
46 | ip.magic('store -r') # restores _dh too | |||
34 | nt.assert_in(os.path.realpath(tmpd), ip.user_ns['_dh']) |
|
47 | nt.assert_in(os.path.realpath(tmpd), ip.user_ns['_dh']) | |
35 |
|
48 | |||
36 | os.rmdir(tmpd) |
|
49 | os.rmdir(tmpd) | |
37 |
|
50 | |||
38 | def test_autorestore(): |
|
51 | def test_autorestore(): |
General Comments 0
You need to be logged in to leave comments.
Login now