Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Storemagic plugin #1073

Merged
merged 4 commits into from

3 participants

@takluyver
Owner

This makes the storemagic extension a plugin, so there's a configurable option for autorestoring variables on startup (default False).

It also adds a non-configurable default_extensions trait to InteractiveShellApp, for extensions we want to always load as part of IPython. I considered just having a default value for the extensions trait, but I think that's too easy to clobber unintentionally in config, e.g. c.InteractiveShellApp.extensions = ['sympyprinting'].

I think this is a shortcoming of the extension system, that there isn't a simple way to add values to a container - you have to do the little dance with hasattr, and then assign or use extend.

@takluyver
Owner

Actually, I'm not completely happy about this. Because extensions are loaded by the application, %store won't be available when it's embedded. I think the always-on extensions should be loaded by the shell.

@fperez
Owner

Agreed, those should be shell-level. Do you want to do that here or in a different PR?

@minrk
Owner

Related: should extensions just be attached to the InteractiveShell in general, rather than the app? That seems to make more sense.

@fperez
Owner

I agree, @minrk.

@minrk
Owner

Okay, then if we do move extensions to InteractiveShell from App, there should be a deprecation similar to the one we did for InteractiveShell.prompt_x.

@minrk
Owner

opened #1144 for moving extensions to Shell, merging this.

@minrk minrk merged commit d3ded1a into ipython:master
@ellisonbg ellisonbg referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
7 IPython/core/shellapp.py
@@ -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)
View
41 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
View
5 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`.
Something went wrong with that request. Please try again.