##// END OF EJS Templates
Update storemagic extension to new API
Fernando Perez -
Show More
@@ -191,17 +191,8 b' class MagicsManager(Configurable):'
191 self.magics[mtype].update(m.magics[mtype])
191 self.magics[mtype].update(m.magics[mtype])
192
192
193 def new_magic(self, func, magic_name=None, magic_type='line'):
193 def new_magic(self, func, magic_name=None, magic_type='line'):
194 """Expose own function as magic function for ipython
194 """Expose a standalone function as magic function for ipython.
195
195
196 Example::
197
198 def foo_impl(self, parameter_s=''):
199 'My very own magic!. (Use docstrings, IPython reads them).'
200 print 'Magic function. Passed parameter is between < >:'
201 print '<%s>' % parameter_s
202 print 'The self object is:', self
203
204 ip.define_magic('foo', foo_impl)
205 """
196 """
206
197
207 # Create the new method in the user_magics and register it in the
198 # Create the new method in the user_magics and register it in the
@@ -8,10 +8,10 b' To automatically restore stored variables at startup, add this to your'
8 :file:`ipython_config.py` file::
8 :file:`ipython_config.py` file::
9
9
10 c.StoreMagic.autorestore = True
10 c.StoreMagic.autorestore = True
11
12 """
11 """
13
12
14 from IPython.core.error import TryNext, UsageError
13 from IPython.core.error import TryNext, UsageError
14 from IPython.core.magic import Magics, register_magics, line_magic
15 from IPython.core.plugin import Plugin
15 from IPython.core.plugin import Plugin
16 from IPython.testing.skipdoctest import skip_doctest
16 from IPython.testing.skipdoctest import skip_doctest
17 from IPython.utils import pickleshare
17 from IPython.utils import pickleshare
@@ -20,6 +20,7 b' from IPython.utils.traitlets import Bool, Instance'
20 import inspect,pickle,os,sys,textwrap
20 import inspect,pickle,os,sys,textwrap
21 from IPython.core.fakemodule import FakeModule
21 from IPython.core.fakemodule import FakeModule
22
22
23
23 def restore_aliases(ip):
24 def restore_aliases(ip):
24 staliases = ip.db.get('stored_aliases', {})
25 staliases = ip.db.get('stored_aliases', {})
25 for k,v in staliases.items():
26 for k,v in staliases.items():
@@ -46,13 +47,22 b' def refresh_variables(ip):'
46 def restore_dhist(ip):
47 def restore_dhist(ip):
47 ip.user_ns['_dh'] = ip.db.get('dhist',[])
48 ip.user_ns['_dh'] = ip.db.get('dhist',[])
48
49
50
49 def restore_data(ip):
51 def restore_data(ip):
50 refresh_variables(ip)
52 refresh_variables(ip)
51 restore_aliases(ip)
53 restore_aliases(ip)
52 restore_dhist(ip)
54 restore_dhist(ip)
53
55
56
57 @register_magics
58 class StoreMagics(Magics):
59 """Lightweight persistence for python variables.
60
61 Provides the %store magic."""
62
54 @skip_doctest
63 @skip_doctest
55 def magic_store(self, parameter_s=''):
64 @line_magic
65 def store(self, parameter_s=''):
56 """Lightweight persistence for python variables.
66 """Lightweight persistence for python variables.
57
67
58 Example::
68 Example::
@@ -69,11 +79,14 b" def magic_store(self, parameter_s=''):"
69
79
70 Usage:
80 Usage:
71
81
72 * ``%store`` - Show list of all variables and their current values
82 * ``%store`` - Show list of all variables and their current
73 * ``%store spam`` - Store the *current* value of the variable spam to disk
83 values
84 * ``%store spam`` - Store the *current* value of the variable spam
85 to disk
74 * ``%store -d spam`` - Remove the variable and its value from storage
86 * ``%store -d spam`` - Remove the variable and its value from storage
75 * ``%store -z`` - Remove all variables from storage
87 * ``%store -z`` - Remove all variables from storage
76 * ``%store -r`` - Refresh all variables from store (delete current vals)
88 * ``%store -r`` - Refresh all variables from store (delete
89 current vals)
77 * ``%store foo >a.txt`` - Store value of foo to new file a.txt
90 * ``%store foo >a.txt`` - Store value of foo to new file a.txt
78 * ``%store foo >>a.txt`` - Append value of foo to file a.txt
91 * ``%store foo >>a.txt`` - Append value of foo to file a.txt
79
92
@@ -189,11 +202,12 b' class StoreMagic(Plugin):'
189
202
190 def __init__(self, shell, config):
203 def __init__(self, shell, config):
191 super(StoreMagic, self).__init__(shell=shell, config=config)
204 super(StoreMagic, self).__init__(shell=shell, config=config)
192 shell.define_magic('store', magic_store)
205 shell.register_magics(StoreMagics)
193
206
194 if self.autorestore:
207 if self.autorestore:
195 restore_data(shell)
208 restore_data(shell)
196
209
210
197 _loaded = False
211 _loaded = False
198
212
199 def load_ipython_extension(ip):
213 def load_ipython_extension(ip):
General Comments 0
You need to be logged in to leave comments. Login now