##// END OF EJS Templates
Merge pull request #1073 from takluyver/storemagic-plugin...
Min RK -
r5683:d3ded1a8 merge
parent child Browse files
Show More
@@ -130,6 +130,9 b' class InteractiveShellApp(Configurable):'
130 130 if new:
131 131 # add to self.extensions
132 132 self.extensions.append(new)
133
134 # Extensions that are always loaded (not configurable)
135 default_extensions = List(Unicode, [u'storemagic'], config=False)
133 136
134 137 exec_files = List(Unicode, config=True,
135 138 help="""List of files to run at IPython startup."""
@@ -158,11 +161,9 b' class InteractiveShellApp(Configurable):'
158 161 This uses the :meth:`ExtensionManager.load_extensions` to load all
159 162 the extensions listed in ``self.extensions``.
160 163 """
161 if not self.extensions:
162 return
163 164 try:
164 165 self.log.debug("Loading IPython extensions...")
165 extensions = self.extensions
166 extensions = self.default_extensions + self.extensions
166 167 for ext in extensions:
167 168 try:
168 169 self.log.info("Loading IPython extension: %s" % ext)
@@ -2,26 +2,24 b''
2 2 """
3 3 %store magic for lightweight persistence.
4 4
5 Stores variables, aliases and macros in IPython's database. Stored values will
6 be automatically restored whenever the extension is loaded.
5 Stores variables, aliases and macros in IPython's database.
7 6
8 To enable this functionality, list it in your default profile
9 `ipython_config.py` file::
7 To automatically restore stored variables at startup, add this to your
8 :file:`ipython_config.py` file::
10 9
11 c.InteractiveShellApp.extensions = ['storemagic']
12
13 Or to use it temporarily, run this in your IPython session::
14
15 %load_ext storemagic
10 c.StoreMagic.autorestore = True
16 11
17 12 """
18 13
19 14 from IPython.core.error import TryNext, UsageError
15 from IPython.core.plugin import Plugin
16 from IPython.testing.skipdoctest import skip_doctest
20 17 from IPython.utils import pickleshare
18 from IPython.utils.traitlets import Bool, Instance
21 19
22 20 import inspect,pickle,os,sys,textwrap
23 21 from IPython.core.fakemodule import FakeModule
24
22
25 23 def restore_aliases(ip):
26 24 staliases = ip.db.get('stored_aliases', {})
27 25 for k,v in staliases.items():
@@ -53,6 +51,7 b' def restore_data(ip):'
53 51 restore_aliases(ip)
54 52 restore_dhist(ip)
55 53
54 @skip_doctest
56 55 def magic_store(self, parameter_s=''):
57 56 """Lightweight persistence for python variables.
58 57
@@ -183,6 +182,24 b" def magic_store(self, parameter_s=''):"
183 182 self.db[ 'autorestore/' + args[0] ] = obj
184 183 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
185 184
185
186 class StoreMagic(Plugin):
187 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
188 autorestore = Bool(False, config=True)
189
190 def __init__(self, shell, config):
191 super(StoreMagic, self).__init__(shell=shell, config=config)
192 shell.define_magic('store', magic_store)
193
194 if self.autorestore:
195 restore_data(shell)
196
197 _loaded = False
198
186 199 def load_ipython_extension(ip):
187 ip.define_magic('store', magic_store)
188 restore_data(ip)
200 """Load the extension in IPython."""
201 global _loaded
202 if not _loaded:
203 plugin = StoreMagic(shell=ip, config=ip.config)
204 ip.plugin_manager.register_plugin('storemagic', plugin)
205 _loaded = True
@@ -62,8 +62,9 b' New features'
62 62 Terminal frontend by default (:ghpull:`838`).
63 63
64 64 * **%store**: The ``%store`` magic from earlier versions has been updated and
65 placed in an extension, :ref:`extensions_storemagic`. Add 'storemagic' to ``c.InteractiveShellApp.extensions``
66 in ipython_config.py to enable it (:ghpull:`1029`).
65 re-enabled (:ref:`extensions_storemagic`; :ghpull:`1029`). To autorestore
66 stored variables on startup, specify ``c.StoreMagic.autorestore = True`` in
67 :file:`ipython_config.py`.
67 68
68 69
69 70
General Comments 0
You need to be logged in to leave comments. Login now