Show More
@@ -25,10 +25,12 b' To automatically restore stored variables at startup, add this to your' | |||||
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 | |
@@ -68,11 +70,24 b' def restore_data(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.""" | |
75 |
|
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) | |||
|
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=''): | |
@@ -225,3 +240,4 b' class StoreMagics(Magics):' | |||||
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,5 +1,6 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() | |
@@ -30,3 +31,20 b' def test_store_restore():' | |||||
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 |
@@ -47,6 +47,7 b' 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 | |
@@ -219,6 +220,7 b' class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):' | |||||
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( |
General Comments 0
You need to be logged in to leave comments.
Login now