From 91ee2b2f105ee1bda3669be48ea766f889d1a413 2013-08-29 21:58:34 From: Min RK Date: 2013-08-29 21:58:34 Subject: [PATCH] Merge pull request #4137 from takluyver/restore-autorestore Restore autorestore option for storemagic It is now StoreMagics.autorestore rather than StoreMagic.autorestore (note the extra s). Closes #4078 --- diff --git a/IPython/extensions/storemagic.py b/IPython/extensions/storemagic.py index 8759b2a..4323b66 100644 --- a/IPython/extensions/storemagic.py +++ b/IPython/extensions/storemagic.py @@ -25,10 +25,12 @@ To automatically restore stored variables at startup, add this to your import inspect, os, sys, textwrap # Our own +from IPython.config.configurable import Configurable from IPython.core.error import UsageError from IPython.core.fakemodule import FakeModule from IPython.core.magic import Magics, magics_class, line_magic from IPython.testing.skipdoctest import skip_doctest +from IPython.utils.traitlets import Bool #----------------------------------------------------------------------------- # Functions and classes @@ -68,10 +70,23 @@ def restore_data(ip): @magics_class -class StoreMagics(Magics): +class StoreMagics(Magics, Configurable): """Lightweight persistence for python variables. Provides the %store magic.""" + + autorestore = Bool(False, config=True, help= + """If True, any %store-d variables will be automatically restored + when IPython starts. + """ + ) + + def __init__(self, shell): + Configurable.__init__(self, config=shell.config) + Magics.__init__(self, shell=shell) + self.shell.configurables.append(self) + if self.autorestore: + restore_data(self.shell) @skip_doctest @line_magic @@ -225,3 +240,4 @@ class StoreMagics(Magics): def load_ipython_extension(ip): """Load the extension in IPython.""" ip.register_magics(StoreMagics) + diff --git a/IPython/extensions/tests/test_storemagic.py b/IPython/extensions/tests/test_storemagic.py index ac6fb97..203ffd4 100644 --- a/IPython/extensions/tests/test_storemagic.py +++ b/IPython/extensions/tests/test_storemagic.py @@ -1,5 +1,6 @@ import tempfile, os +from IPython.config.loader import Config import nose.tools as nt ip = get_ipython() @@ -30,3 +31,20 @@ def test_store_restore(): nt.assert_in(os.path.realpath(tmpd), ip.user_ns['_dh']) os.rmdir(tmpd) + +def test_autorestore(): + ip.user_ns['foo'] = 95 + ip.magic('store foo') + del ip.user_ns['foo'] + c = Config() + c.StoreMagics.autorestore = False + orig_config = ip.config + try: + ip.config = c + ip.extension_manager.reload_extension('storemagic') + nt.assert_not_in('foo', ip.user_ns) + c.StoreMagics.autorestore = True + ip.extension_manager.reload_extension('storemagic') + nt.assert_equal(ip.user_ns['foo'], 95) + finally: + ip.config = orig_config diff --git a/IPython/terminal/ipapp.py b/IPython/terminal/ipapp.py index 02ba278..3a0c314 100755 --- a/IPython/terminal/ipapp.py +++ b/IPython/terminal/ipapp.py @@ -47,6 +47,7 @@ from IPython.core.magics import ScriptMagics from IPython.core.shellapp import ( InteractiveShellApp, shell_flags, shell_aliases ) +from IPython.extensions.storemagic import StoreMagics from IPython.terminal.interactiveshell import TerminalInteractiveShell from IPython.utils import warn from IPython.utils.path import get_ipython_dir, check_for_old_config @@ -219,6 +220,7 @@ class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp): PlainTextFormatter, IPCompleter, ScriptMagics, + StoreMagics, ] subcommands = Dict(dict(