diff --git a/IPython/core/shellapp.py b/IPython/core/shellapp.py index f4cd644..0e0bb20 100644 --- a/IPython/core/shellapp.py +++ b/IPython/core/shellapp.py @@ -130,6 +130,9 @@ class InteractiveShellApp(Configurable): if new: # add to self.extensions self.extensions.append(new) + + # Extensions that are always loaded (not configurable) + default_extensions = List(Unicode, [u'storemagic'], config=False) exec_files = List(Unicode, config=True, help="""List of files to run at IPython startup.""" @@ -158,11 +161,9 @@ class InteractiveShellApp(Configurable): This uses the :meth:`ExtensionManager.load_extensions` to load all the extensions listed in ``self.extensions``. """ - if not self.extensions: - return try: self.log.debug("Loading IPython extensions...") - extensions = self.extensions + extensions = self.default_extensions + self.extensions for ext in extensions: try: self.log.info("Loading IPython extension: %s" % ext) diff --git a/IPython/extensions/storemagic.py b/IPython/extensions/storemagic.py index 92f94c9..09e63fc 100644 --- a/IPython/extensions/storemagic.py +++ b/IPython/extensions/storemagic.py @@ -2,26 +2,24 @@ """ %store magic for lightweight persistence. -Stores variables, aliases and macros in IPython's database. Stored values will -be automatically restored whenever the extension is loaded. +Stores variables, aliases and macros in IPython's database. -To enable this functionality, list it in your default profile -`ipython_config.py` file:: +To automatically restore stored variables at startup, add this to your +:file:`ipython_config.py` file:: - c.InteractiveShellApp.extensions = ['storemagic'] - -Or to use it temporarily, run this in your IPython session:: - - %load_ext storemagic + c.StoreMagic.autorestore = True """ from IPython.core.error import TryNext, UsageError +from IPython.core.plugin import Plugin +from IPython.testing.skipdoctest import skip_doctest from IPython.utils import pickleshare +from IPython.utils.traitlets import Bool, Instance import inspect,pickle,os,sys,textwrap from IPython.core.fakemodule import FakeModule - + def restore_aliases(ip): staliases = ip.db.get('stored_aliases', {}) for k,v in staliases.items(): @@ -53,6 +51,7 @@ def restore_data(ip): restore_aliases(ip) restore_dhist(ip) +@skip_doctest def magic_store(self, parameter_s=''): """Lightweight persistence for python variables. @@ -183,6 +182,24 @@ def magic_store(self, parameter_s=''): self.db[ 'autorestore/' + args[0] ] = obj print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__) + +class StoreMagic(Plugin): + shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') + autorestore = Bool(False, config=True) + + def __init__(self, shell, config): + super(StoreMagic, self).__init__(shell=shell, config=config) + shell.define_magic('store', magic_store) + + if self.autorestore: + restore_data(shell) + +_loaded = False + def load_ipython_extension(ip): - ip.define_magic('store', magic_store) - restore_data(ip) + """Load the extension in IPython.""" + global _loaded + if not _loaded: + plugin = StoreMagic(shell=ip, config=ip.config) + ip.plugin_manager.register_plugin('storemagic', plugin) + _loaded = True diff --git a/docs/source/whatsnew/development.txt b/docs/source/whatsnew/development.txt index 749f359..d32ba8d 100644 --- a/docs/source/whatsnew/development.txt +++ b/docs/source/whatsnew/development.txt @@ -62,8 +62,9 @@ New features Terminal frontend by default (:ghpull:`838`). * **%store**: The ``%store`` magic from earlier versions has been updated and - placed in an extension, :ref:`extensions_storemagic`. Add 'storemagic' to ``c.InteractiveShellApp.extensions`` - in ipython_config.py to enable it (:ghpull:`1029`). + re-enabled (:ref:`extensions_storemagic`; :ghpull:`1029`). To autorestore + stored variables on startup, specify ``c.StoreMagic.autorestore = True`` in + :file:`ipython_config.py`.