##// END OF EJS Templates
Update storemagic extension to new API
Fernando Perez -
Show More
@@ -191,17 +191,8 b' class MagicsManager(Configurable):'
191 191 self.magics[mtype].update(m.magics[mtype])
192 192
193 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 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 8 :file:`ipython_config.py` file::
9 9
10 10 c.StoreMagic.autorestore = True
11
12 11 """
13 12
14 13 from IPython.core.error import TryNext, UsageError
14 from IPython.core.magic import Magics, register_magics, line_magic
15 15 from IPython.core.plugin import Plugin
16 16 from IPython.testing.skipdoctest import skip_doctest
17 17 from IPython.utils import pickleshare
@@ -19,6 +19,7 b' from IPython.utils.traitlets import Bool, Instance'
19 19
20 20 import inspect,pickle,os,sys,textwrap
21 21 from IPython.core.fakemodule import FakeModule
22
22 23
23 24 def restore_aliases(ip):
24 25 staliases = ip.db.get('stored_aliases', {})
@@ -37,7 +38,7 b' def refresh_variables(ip):'
37 38 obj = db[key]
38 39 except KeyError:
39 40 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey
40 print "The error was:",sys.exc_info()[0]
41 print "The error was:", sys.exc_info()[0]
41 42 else:
42 43 #print "restored",justkey,"=",obj #dbg
43 44 ip.user_ns[justkey] = obj
@@ -46,141 +47,153 b' def refresh_variables(ip):'
46 47 def restore_dhist(ip):
47 48 ip.user_ns['_dh'] = ip.db.get('dhist',[])
48 49
50
49 51 def restore_data(ip):
50 52 refresh_variables(ip)
51 53 restore_aliases(ip)
52 54 restore_dhist(ip)
53 55
54 @skip_doctest
55 def magic_store(self, parameter_s=''):
56
57 @register_magics
58 class StoreMagics(Magics):
56 59 """Lightweight persistence for python variables.
57 60
58 Example::
61 Provides the %store magic."""
59 62
60 In [1]: l = ['hello',10,'world']
61 In [2]: %store l
62 In [3]: exit
63 @skip_doctest
64 @line_magic
65 def store(self, parameter_s=''):
66 """Lightweight persistence for python variables.
63 67
64 (IPython session is closed and started again...)
68 Example::
65 69
66 ville@badger:~$ ipython
67 In [1]: l
68 Out[1]: ['hello', 10, 'world']
70 In [1]: l = ['hello',10,'world']
71 In [2]: %store l
72 In [3]: exit
69 73
70 Usage:
74 (IPython session is closed and started again...)
71 75
72 * ``%store`` - Show list of all variables and their current values
73 * ``%store spam`` - Store the *current* value of the variable spam to disk
74 * ``%store -d spam`` - Remove the variable and its value from storage
75 * ``%store -z`` - Remove all variables from storage
76 * ``%store -r`` - Refresh all variables from store (delete current vals)
77 * ``%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
76 ville@badger:~$ ipython
77 In [1]: l
78 Out[1]: ['hello', 10, 'world']
79 79
80 It should be noted that if you change the value of a variable, you
81 need to %store it again if you want to persist the new value.
80 Usage:
82 81
83 Note also that the variables will need to be pickleable; most basic
84 python types can be safely %store'd.
82 * ``%store`` - Show list of all variables and their current
83 values
84 * ``%store spam`` - Store the *current* value of the variable spam
85 to disk
86 * ``%store -d spam`` - Remove the variable and its value from storage
87 * ``%store -z`` - Remove all variables from storage
88 * ``%store -r`` - Refresh all variables from store (delete
89 current vals)
90 * ``%store foo >a.txt`` - Store value of foo to new file a.txt
91 * ``%store foo >>a.txt`` - Append value of foo to file a.txt
85 92
86 Also aliases can be %store'd across sessions.
87 """
93 It should be noted that if you change the value of a variable, you
94 need to %store it again if you want to persist the new value.
88 95
89 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
90 args = argsl.split(None,1)
91 ip = self.shell
92 db = ip.db
93 # delete
94 if opts.has_key('d'):
95 try:
96 todel = args[0]
97 except IndexError:
98 raise UsageError('You must provide the variable to forget')
99 else:
100 try:
101 del db['autorestore/' + todel]
102 except:
103 raise UsageError("Can't delete variable '%s'" % todel)
104 # reset
105 elif opts.has_key('z'):
106 for k in db.keys('autorestore/*'):
107 del db[k]
108
109 elif opts.has_key('r'):
110 refresh_variables(ip)
111
112
113 # run without arguments -> list variables & values
114 elif not args:
115 vars = self.db.keys('autorestore/*')
116 vars.sort()
117 if vars:
118 size = max(map(len,vars))
119 else:
120 size = 0
121
122 print 'Stored variables and their in-db values:'
123 fmt = '%-'+str(size)+'s -> %s'
124 get = db.get
125 for var in vars:
126 justkey = os.path.basename(var)
127 # print 30 first characters from every var
128 print fmt % (justkey,repr(get(var,'<unavailable>'))[:50])
129
130 # default action - store the variable
131 else:
132 # %store foo >file.txt or >>file.txt
133 if len(args) > 1 and args[1].startswith('>'):
134 fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
135 if args[1].startswith('>>'):
136 fil = open(fnam,'a')
137 else:
138 fil = open(fnam,'w')
139 obj = ip.ev(args[0])
140 print "Writing '%s' (%s) to file '%s'." % (args[0],
141 obj.__class__.__name__, fnam)
96 Note also that the variables will need to be pickleable; most basic
97 python types can be safely %store'd.
142 98
99 Also aliases can be %store'd across sessions.
100 """
143 101
144 if not isinstance (obj,basestring):
145 from pprint import pprint
146 pprint(obj,fil)
102 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
103 args = argsl.split(None,1)
104 ip = self.shell
105 db = ip.db
106 # delete
107 if opts.has_key('d'):
108 try:
109 todel = args[0]
110 except IndexError:
111 raise UsageError('You must provide the variable to forget')
147 112 else:
148 fil.write(obj)
149 if not obj.endswith('\n'):
150 fil.write('\n')
151
152 fil.close()
153 return
154
155 # %store foo
156 try:
157 obj = ip.user_ns[args[0]]
158 except KeyError:
159 # it might be an alias
160 # This needs to be refactored to use the new AliasManager stuff.
161 if args[0] in self.alias_manager:
162 name = args[0]
163 nargs, cmd = self.alias_manager.alias_table[ name ]
164 staliases = db.get('stored_aliases',{})
165 staliases[ name ] = cmd
166 db['stored_aliases'] = staliases
167 print "Alias stored: %s (%s)" % (name, cmd)
168 return
113 try:
114 del db['autorestore/' + todel]
115 except:
116 raise UsageError("Can't delete variable '%s'" % todel)
117 # reset
118 elif opts.has_key('z'):
119 for k in db.keys('autorestore/*'):
120 del db[k]
121
122 elif opts.has_key('r'):
123 refresh_variables(ip)
124
125
126 # run without arguments -> list variables & values
127 elif not args:
128 vars = self.db.keys('autorestore/*')
129 vars.sort()
130 if vars:
131 size = max(map(len,vars))
169 132 else:
170 raise UsageError("Unknown variable '%s'" % args[0])
133 size = 0
134
135 print 'Stored variables and their in-db values:'
136 fmt = '%-'+str(size)+'s -> %s'
137 get = db.get
138 for var in vars:
139 justkey = os.path.basename(var)
140 # print 30 first characters from every var
141 print fmt % (justkey,repr(get(var,'<unavailable>'))[:50])
171 142
143 # default action - store the variable
172 144 else:
173 if isinstance(inspect.getmodule(obj), FakeModule):
174 print textwrap.dedent("""\
175 Warning:%s is %s
176 Proper storage of interactively declared classes (or instances
177 of those classes) is not possible! Only instances
178 of classes in real modules on file system can be %%store'd.
179 """ % (args[0], obj) )
145 # %store foo >file.txt or >>file.txt
146 if len(args) > 1 and args[1].startswith('>'):
147 fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
148 if args[1].startswith('>>'):
149 fil = open(fnam,'a')
150 else:
151 fil = open(fnam,'w')
152 obj = ip.ev(args[0])
153 print "Writing '%s' (%s) to file '%s'." % (args[0],
154 obj.__class__.__name__, fnam)
155
156
157 if not isinstance (obj,basestring):
158 from pprint import pprint
159 pprint(obj,fil)
160 else:
161 fil.write(obj)
162 if not obj.endswith('\n'):
163 fil.write('\n')
164
165 fil.close()
180 166 return
181 #pickled = pickle.dumps(obj)
182 self.db[ 'autorestore/' + args[0] ] = obj
183 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
167
168 # %store foo
169 try:
170 obj = ip.user_ns[args[0]]
171 except KeyError:
172 # it might be an alias
173 # This needs to be refactored to use the new AliasManager stuff.
174 if args[0] in self.alias_manager:
175 name = args[0]
176 nargs, cmd = self.alias_manager.alias_table[ name ]
177 staliases = db.get('stored_aliases',{})
178 staliases[ name ] = cmd
179 db['stored_aliases'] = staliases
180 print "Alias stored: %s (%s)" % (name, cmd)
181 return
182 else:
183 raise UsageError("Unknown variable '%s'" % args[0])
184
185 else:
186 if isinstance(inspect.getmodule(obj), FakeModule):
187 print textwrap.dedent("""\
188 Warning:%s is %s
189 Proper storage of interactively declared classes (or instances
190 of those classes) is not possible! Only instances
191 of classes in real modules on file system can be %%store'd.
192 """ % (args[0], obj) )
193 return
194 #pickled = pickle.dumps(obj)
195 self.db[ 'autorestore/' + args[0] ] = obj
196 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
184 197
185 198
186 199 class StoreMagic(Plugin):
@@ -189,11 +202,12 b' class StoreMagic(Plugin):'
189 202
190 203 def __init__(self, shell, config):
191 204 super(StoreMagic, self).__init__(shell=shell, config=config)
192 shell.define_magic('store', magic_store)
205 shell.register_magics(StoreMagics)
193 206
194 207 if self.autorestore:
195 208 restore_data(shell)
196 209
210
197 211 _loaded = False
198 212
199 213 def load_ipython_extension(ip):
General Comments 0
You need to be logged in to leave comments. Login now