##// END OF EJS Templates
Update storemagic extension to new API
Fernando Perez -
Show More
@@ -1,378 +1,369 b''
1 1 # encoding: utf-8
2 2 """Magic functions for InteractiveShell.
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 7 # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu>
8 8 # Copyright (C) 2008 The IPython Development Team
9 9
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17 # Stdlib
18 18 import os
19 19 import re
20 20 import sys
21 21 import types
22 22 from getopt import getopt, GetoptError
23 23
24 24 # Our own
25 25 from IPython.config.configurable import Configurable
26 26 from IPython.core import oinspect
27 27 from IPython.core.error import UsageError
28 28 from IPython.core.prefilter import ESC_MAGIC
29 29 from IPython.external.decorator import decorator
30 30 from IPython.utils.ipstruct import Struct
31 31 from IPython.utils.process import arg_split
32 32 from IPython.utils.traitlets import Bool, Dict, Instance
33 33 from IPython.utils.warn import error
34 34
35 35 #-----------------------------------------------------------------------------
36 36 # Globals
37 37 #-----------------------------------------------------------------------------
38 38
39 39 # A dict we'll use for each class that has magics, used as temporary storage to
40 40 # pass information between the @line/cell_magic method decorators and the
41 41 # @register_magics class decorator, because the method decorators have no
42 42 # access to the class when they run. See for more details:
43 43 # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class
44 44
45 45 magics = dict(line={}, cell={})
46 46
47 47 magic_types = ('line', 'cell')
48 48
49 49 #-----------------------------------------------------------------------------
50 50 # Utility classes and functions
51 51 #-----------------------------------------------------------------------------
52 52
53 53 class Bunch: pass
54 54
55 55
56 56 def on_off(tag):
57 57 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
58 58 return ['OFF','ON'][tag]
59 59
60 60
61 61 def compress_dhist(dh):
62 62 head, tail = dh[:-10], dh[-10:]
63 63
64 64 newhead = []
65 65 done = set()
66 66 for h in head:
67 67 if h in done:
68 68 continue
69 69 newhead.append(h)
70 70 done.add(h)
71 71
72 72 return newhead + tail
73 73
74 74
75 75 def needs_local_scope(func):
76 76 """Decorator to mark magic functions which need to local scope to run."""
77 77 func.needs_local_scope = True
78 78 return func
79 79
80 80 #-----------------------------------------------------------------------------
81 81 # Class and method decorators for registering magics
82 82 #-----------------------------------------------------------------------------
83 83
84 84 def register_magics(cls):
85 85 cls.registered = True
86 86 cls.magics = dict(line = magics['line'],
87 87 cell = magics['cell'])
88 88 magics['line'] = {}
89 89 magics['cell'] = {}
90 90 return cls
91 91
92 92 def _record_magic(dct, mtype, mname, func):
93 93 if mtype == 'line_cell':
94 94 dct['line'][mname] = dct['cell'][mname] = func
95 95 else:
96 96 dct[mtype][mname] = func
97 97
98 98 def validate_type(magic_type):
99 99 if magic_type not in magic_types:
100 100 raise ValueError('magic_type must be one of %s, %s given' %
101 101 magic_types, magic_type)
102 102
103 103
104 104 def _magic_marker(magic_type):
105 105 validate_type(magic_type)
106 106
107 107 # This is a closure to capture the magic_type. We could also use a class,
108 108 # but it's overkill for just that one bit of state.
109 109 def magic_deco(arg):
110 110 call = lambda f, *a, **k: f(*a, **k)
111 111
112 112 if callable(arg):
113 113 # "Naked" decorator call (just @foo, no args)
114 114 func = arg
115 115 name = func.func_name
116 116 func.magic_name = name
117 117 retval = decorator(call, func)
118 118 magics[magic_type][name] = name
119 119 elif isinstance(arg, basestring):
120 120 # Decorator called with arguments (@foo('bar'))
121 121 name = arg
122 122 def mark(func, *a, **kw):
123 123 func.magic_name = name
124 124 magics[magic_type][name] = func.func_name
125 125 return decorator(call, func)
126 126 retval = mark
127 127 else:
128 128 raise ValueError("Decorator can only be called with "
129 129 "string or function")
130 130
131 131 return retval
132 132
133 133 return magic_deco
134 134
135 135
136 136 line_magic = _magic_marker('line')
137 137 cell_magic = _magic_marker('cell')
138 138
139 139 #-----------------------------------------------------------------------------
140 140 # Core Magic classes
141 141 #-----------------------------------------------------------------------------
142 142
143 143 class MagicsManager(Configurable):
144 144 """Object that handles all magic-related functionality for IPython.
145 145 """
146 146 # Non-configurable class attributes
147 147 magics = Dict
148 148
149 149 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
150 150
151 151 auto_magic = Bool
152 152
153 153 _auto_status = [
154 154 'Automagic is OFF, % prefix IS needed for magic functions.',
155 155 'Automagic is ON, % prefix IS NOT needed for magic functions.']
156 156
157 157 user_magics = Instance('IPython.core.magic_functions.UserMagics')
158 158
159 159 def __init__(self, shell=None, config=None, user_magics=None, **traits):
160 160
161 161 super(MagicsManager, self).__init__(shell=shell, config=config,
162 162 user_magics=user_magics, **traits)
163 163 self.magics = dict(line={}, cell={})
164 164
165 165 def auto_status(self):
166 166 """Return descriptive string with automagic status."""
167 167 return self._auto_status[self.auto_magic]
168 168
169 169 def lsmagic(self):
170 170 """Return a dict of currently available magic functions.
171 171
172 172 The return dict has the keys 'line' and 'cell', corresponding to the
173 173 two types of magics we support. Each value is a list of names.
174 174 """
175 175 return self.magics
176 176
177 177 def register(self, *magic_objects):
178 178 """Register one or more instances of Magics.
179 179 """
180 180 # Start by validating them to ensure they have all had their magic
181 181 # methods registered at the instance level
182 182 for m in magic_objects:
183 183 if not m.registered:
184 184 raise ValueError("Class of magics %r was constructed without "
185 185 "the @register_macics class decorator")
186 186 if type(m) is type:
187 187 # If we're given an uninstantiated class
188 188 m = m(self.shell)
189 189
190 190 for mtype in magic_types:
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
208 199 # global table
209 200 newm, name = self.user_magics.new_magic(func, magic_type, magic_name)
210 201 _record_magic(self.magics, magic_type, name, newm)
211 202
212 203 # Key base class that provides the central functionality for magics.
213 204
214 205 class Magics(object):
215 206 """Base class for implementing magic functions.
216 207
217 208 Shell functions which can be reached as %function_name. All magic
218 209 functions should accept a string, which they can parse for their own
219 210 needs. This can make some functions easier to type, eg `%cd ../`
220 211 vs. `%cd("../")`
221 212
222 213 Classes providing magic functions need to subclass this class, and they
223 214 MUST:
224 215
225 216 - Use the method decorators `@line_magic` and `@cell_magic` to decorate
226 217 individual methods as magic functions, AND
227 218
228 219 - Use the class decorator `@register_magics` to ensure that the magic
229 220 methods are properly registered at the instance level upon instance
230 221 initialization.
231 222
232 223 See :mod:`magic_functions` for examples of actual implementation classes.
233 224 """
234 225 # Dict holding all command-line options for each magic.
235 226 options_table = None
236 227 # Dict for the mapping of magic names to methods, set by class decorator
237 228 magics = None
238 229 # Flag to check that the class decorator was properly applied
239 230 registered = False
240 231 # Instance of IPython shell
241 232 shell = None
242 233
243 234 def __init__(self, shell):
244 235 if not(self.__class__.registered):
245 236 raise ValueError('Magics subclass without registration - '
246 237 'did you forget to apply @register_magics?')
247 238 self.shell = shell
248 239 self.options_table = {}
249 240 # The method decorators are run when the instance doesn't exist yet, so
250 241 # they can only record the names of the methods they are supposed to
251 242 # grab. Only now, that the instance exists, can we create the proper
252 243 # mapping to bound methods. So we read the info off the original names
253 244 # table and replace each method name by the actual bound method.
254 245 for mtype in magic_types:
255 246 tab = self.magics[mtype]
256 247 # must explicitly use keys, as we're mutating this puppy
257 248 for magic_name in tab.keys():
258 249 meth_name = tab[magic_name]
259 250 if isinstance(meth_name, basestring):
260 251 tab[magic_name] = getattr(self, meth_name)
261 252
262 253 def arg_err(self,func):
263 254 """Print docstring if incorrect arguments were passed"""
264 255 print 'Error in arguments:'
265 256 print oinspect.getdoc(func)
266 257
267 258 def format_latex(self, strng):
268 259 """Format a string for latex inclusion."""
269 260
270 261 # Characters that need to be escaped for latex:
271 262 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
272 263 # Magic command names as headers:
273 264 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
274 265 re.MULTILINE)
275 266 # Magic commands
276 267 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
277 268 re.MULTILINE)
278 269 # Paragraph continue
279 270 par_re = re.compile(r'\\$',re.MULTILINE)
280 271
281 272 # The "\n" symbol
282 273 newline_re = re.compile(r'\\n')
283 274
284 275 # Now build the string for output:
285 276 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
286 277 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
287 278 strng)
288 279 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
289 280 strng = par_re.sub(r'\\\\',strng)
290 281 strng = escape_re.sub(r'\\\1',strng)
291 282 strng = newline_re.sub(r'\\textbackslash{}n',strng)
292 283 return strng
293 284
294 285 def parse_options(self, arg_str, opt_str, *long_opts, **kw):
295 286 """Parse options passed to an argument string.
296 287
297 288 The interface is similar to that of getopt(), but it returns back a
298 289 Struct with the options as keys and the stripped argument string still
299 290 as a string.
300 291
301 292 arg_str is quoted as a true sys.argv vector by using shlex.split.
302 293 This allows us to easily expand variables, glob files, quote
303 294 arguments, etc.
304 295
305 296 Options:
306 297 -mode: default 'string'. If given as 'list', the argument string is
307 298 returned as a list (split on whitespace) instead of a string.
308 299
309 300 -list_all: put all option values in lists. Normally only options
310 301 appearing more than once are put in a list.
311 302
312 303 -posix (True): whether to split the input line in POSIX mode or not,
313 304 as per the conventions outlined in the shlex module from the
314 305 standard library."""
315 306
316 307 # inject default options at the beginning of the input line
317 308 caller = sys._getframe(1).f_code.co_name
318 309 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
319 310
320 311 mode = kw.get('mode','string')
321 312 if mode not in ['string','list']:
322 313 raise ValueError,'incorrect mode given: %s' % mode
323 314 # Get options
324 315 list_all = kw.get('list_all',0)
325 316 posix = kw.get('posix', os.name == 'posix')
326 317 strict = kw.get('strict', True)
327 318
328 319 # Check if we have more than one argument to warrant extra processing:
329 320 odict = {} # Dictionary with options
330 321 args = arg_str.split()
331 322 if len(args) >= 1:
332 323 # If the list of inputs only has 0 or 1 thing in it, there's no
333 324 # need to look for options
334 325 argv = arg_split(arg_str, posix, strict)
335 326 # Do regular option processing
336 327 try:
337 328 opts,args = getopt(argv,opt_str,*long_opts)
338 329 except GetoptError,e:
339 330 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
340 331 " ".join(long_opts)))
341 332 for o,a in opts:
342 333 if o.startswith('--'):
343 334 o = o[2:]
344 335 else:
345 336 o = o[1:]
346 337 try:
347 338 odict[o].append(a)
348 339 except AttributeError:
349 340 odict[o] = [odict[o],a]
350 341 except KeyError:
351 342 if list_all:
352 343 odict[o] = [a]
353 344 else:
354 345 odict[o] = a
355 346
356 347 # Prepare opts,args for return
357 348 opts = Struct(odict)
358 349 if mode == 'string':
359 350 args = ' '.join(args)
360 351
361 352 return opts,args
362 353
363 354 def default_option(self, fn, optstr):
364 355 """Make an entry in the options_table for fn, with value optstr"""
365 356
366 357 if fn not in self.lsmagic():
367 358 error("%s is not a magic function" % fn)
368 359 self.options_table[fn] = optstr
369 360
370 361 def new_magic(self, func, magic_type='line', magic_name=None):
371 362 """TODO
372 363 """
373 364 magic_name = func.func_name if magic_name is None else magic_name
374 365 validate_type(magic_type)
375 366 meth = types.MethodType(func, self)
376 367 setattr(self, magic_name, meth)
377 368 _record_magic(self.magics, magic_type, magic_name, meth)
378 369 return meth, magic_name
@@ -1,205 +1,219 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 %store magic for lightweight persistence.
4 4
5 5 Stores variables, aliases and macros in IPython's database.
6 6
7 7 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
18 18 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', {})
25 26 for k,v in staliases.items():
26 27 #print "restore alias",k,v # dbg
27 28 #self.alias_table[k] = v
28 29 ip.alias_manager.define_alias(k,v)
29 30
30 31
31 32 def refresh_variables(ip):
32 33 db = ip.db
33 34 for key in db.keys('autorestore/*'):
34 35 # strip autorestore
35 36 justkey = os.path.basename(key)
36 37 try:
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
44 45
45 46
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):
187 200 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
188 201 autorestore = Bool(False, config=True)
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):
200 214 """Load the extension in IPython."""
201 215 global _loaded
202 216 if not _loaded:
203 217 plugin = StoreMagic(shell=ip, config=ip.config)
204 218 ip.plugin_manager.register_plugin('storemagic', plugin)
205 219 _loaded = True
General Comments 0
You need to be logged in to leave comments. Login now