##// END OF EJS Templates
Restore pspersistence, including %store magic, as an extension.
Thomas Kluyver -
r5378:26afef67
parent child
Show More
@@ -174,7 +174,7 class AliasManager(Configurable):
174 "because it is a keyword or builtin." % name)
174 "because it is a keyword or builtin." % name)
175 if not (isinstance(cmd, basestring)):
175 if not (isinstance(cmd, basestring)):
176 raise InvalidAliasError("An alias command must be a string, "
176 raise InvalidAliasError("An alias command must be a string, "
177 "got: %r" % name)
177 "got: %r" % cmd)
178 nargs = cmd.count('%s')
178 nargs = cmd.count('%s')
179 if nargs>0 and cmd.find('%l')>=0:
179 if nargs>0 and cmd.find('%l')>=0:
180 raise InvalidAliasError('The %s and %l specifiers are mutually '
180 raise InvalidAliasError('The %s and %l specifiers are mutually '
@@ -5,22 +5,18
5 Stores variables, aliases etc. in PickleShare database.
5 Stores variables, aliases etc. in PickleShare database.
6 """
6 """
7
7
8 from IPython.core import ipapi
9 from IPython.core.error import TryNext, UsageError
8 from IPython.core.error import TryNext, UsageError
10 ip = ipapi.get()
9 from IPython.utils import pickleshare
11
12 import pickleshare
13
10
14 import inspect,pickle,os,sys,textwrap
11 import inspect,pickle,os,sys,textwrap
15 from IPython.core.fakemodule import FakeModule
12 from IPython.core.fakemodule import FakeModule
16
13
17 def restore_aliases(self):
14 def restore_aliases(ip):
18 ip = self.getapi()
19 staliases = ip.db.get('stored_aliases', {})
15 staliases = ip.db.get('stored_aliases', {})
20 for k,v in staliases.items():
16 for k,v in staliases.items():
21 #print "restore alias",k,v # dbg
17 #print "restore alias",k,v # dbg
22 #self.alias_table[k] = v
18 #self.alias_table[k] = v
23 ip.define_alias(k,v)
19 ip.alias_manager.define_alias(k,v)
24
20
25
21
26 def refresh_variables(ip):
22 def refresh_variables(ip):
@@ -39,17 +35,12 def refresh_variables(ip):
39
35
40
36
41 def restore_dhist(ip):
37 def restore_dhist(ip):
42 db = ip.db
38 ip.user_ns['_dh'] = ip.db.get('dhist',[])
43 ip.user_ns['_dh'] = db.get('dhist',[])
44
39
45 def restore_data(self):
40 def restore_data(ip):
46 ip = self.getapi()
47 refresh_variables(ip)
41 refresh_variables(ip)
48 restore_aliases(self)
42 restore_aliases(ip)
49 restore_dhist(self)
43 restore_dhist(ip)
50 raise TryNext
51
52 ip.set_hook('late_startup_hook', restore_data)
53
44
54 def magic_store(self, parameter_s=''):
45 def magic_store(self, parameter_s=''):
55 """Lightweight persistence for python variables.
46 """Lightweight persistence for python variables.
@@ -88,7 +79,7 def magic_store(self, parameter_s=''):
88
79
89 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
80 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
90 args = argsl.split(None,1)
81 args = argsl.split(None,1)
91 ip = self.getapi()
82 ip = self.shell
92 db = ip.db
83 db = ip.db
93 # delete
84 # delete
94 if opts.has_key('d'):
85 if opts.has_key('d'):
@@ -158,11 +149,13 def magic_store(self, parameter_s=''):
158 except KeyError:
149 except KeyError:
159 # it might be an alias
150 # it might be an alias
160 # This needs to be refactored to use the new AliasManager stuff.
151 # This needs to be refactored to use the new AliasManager stuff.
161 if args[0] in self.alias_table:
152 if args[0] in self.alias_manager:
153 name = args[0]
154 nargs, cmd = self.alias_manager.alias_table[ name ]
162 staliases = db.get('stored_aliases',{})
155 staliases = db.get('stored_aliases',{})
163 staliases[ args[0] ] = self.alias_table[ args[0] ]
156 staliases[ name ] = cmd
164 db['stored_aliases'] = staliases
157 db['stored_aliases'] = staliases
165 print "Alias stored:", args[0], self.alias_table[ args[0] ]
158 print "Alias stored: %s (%s)" % (name, cmd)
166 return
159 return
167 else:
160 else:
168 raise UsageError("Unknown variable '%s'" % args[0])
161 raise UsageError("Unknown variable '%s'" % args[0])
@@ -180,4 +173,6 def magic_store(self, parameter_s=''):
180 self.db[ 'autorestore/' + args[0] ] = obj
173 self.db[ 'autorestore/' + args[0] ] = obj
181 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
174 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
182
175
183 ip.define_magic('store',magic_store)
176 def load_ipython_extension(ip):
177 ip.define_magic('store', magic_store)
178 restore_data(ip)
General Comments 0
You need to be logged in to leave comments. Login now