Permalink
Browse files

Merge pull request #1073 from takluyver/storemagic-plugin

* %store is now a plugin
* adds a 'default_extensions' list for extensions that should always be loaded.
  • Loading branch information...
2 parents 5047149 + ac8ff5c commit d3ded1a804d6f756986ee0cd2ee8c99d3cacd1ef @minrk minrk committed Dec 12, 2011
Showing with 36 additions and 17 deletions.
  1. +4 −3 IPython/core/shellapp.py
  2. +29 −12 IPython/extensions/storemagic.py
  3. +3 −2 docs/source/whatsnew/development.txt
View
@@ -130,6 +130,9 @@ def _extra_extension_changed(self, name, old, new):
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 @@ def init_extensions(self):
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)
@@ -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
@@ -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`.

0 comments on commit d3ded1a

Please sign in to comment.