##// END OF EJS Templates
aliases can be %store'd
vivainio -
Show More
@@ -1,3 +1,12 b''
1 # -*- coding: utf-8 -*-
2 """
3 %store magic for lightweight persistence.
4
5 Stores variables, aliases etc. in PickleShare database.
6
7 $Id: iplib.py 1107 2006-01-30 19:02:20Z vivainio $
8 """
9
1 10 import IPython.ipapi
2 11 ip = IPython.ipapi.get()
3 12
@@ -6,6 +15,14 b' import pickleshare'
6 15 import inspect,pickle,os,textwrap
7 16 from IPython.FakeModule import FakeModule
8 17
18 def restore_aliases(self):
19 ip = self.getapi()
20 staliases = ip.getdb().get('stored_aliases', {})
21 for k,v in staliases.items():
22 #print "restore alias",k,v # dbg
23 self.alias_table[k] = v
24
25
9 26 def refresh_variables(ip):
10 27 db = ip.getdb()
11 28 for key in db.keys('autorestore/*'):
@@ -23,15 +40,11 b' def refresh_variables(ip):'
23 40
24 41
25 42 def restore_data(self):
26 #o = ip.options()
27 #self.db = pickleshare.PickleShareDB(o.ipythondir + "/db")
28 #print "restoring ps data" # dbg
29
30 43 ip = self.getapi()
31 44 refresh_variables(ip)
45 restore_aliases(self)
32 46 raise IPython.ipapi.TryNext
33 47
34
35 48 ip.set_hook('late_startup_hook', restore_data)
36 49
37 50 def magic_store(self, parameter_s=''):
@@ -70,6 +83,7 b" def magic_store(self, parameter_s=''):"
70 83 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
71 84 args = argsl.split(None,1)
72 85 ip = self.getapi()
86 db = ip.getdb()
73 87 # delete
74 88 if opts.has_key('d'):
75 89 try:
@@ -78,13 +92,13 b" def magic_store(self, parameter_s=''):"
78 92 error('You must provide the variable to forget')
79 93 else:
80 94 try:
81 del self.db['autorestore/' + todel]
95 del db['autorestore/' + todel]
82 96 except:
83 97 error("Can't delete variable '%s'" % todel)
84 98 # reset
85 99 elif opts.has_key('z'):
86 for k in self.db.keys('autorestore/*'):
87 del self.db[k]
100 for k in db.keys('autorestore/*'):
101 del db[k]
88 102
89 103 elif opts.has_key('r'):
90 104 refresh_variables(ip)
@@ -101,7 +115,7 b" def magic_store(self, parameter_s=''):"
101 115
102 116 print 'Stored variables and their in-db values:'
103 117 fmt = '%-'+str(size)+'s -> %s'
104 get = self.db.get
118 get = db.get
105 119 for var in vars:
106 120 justkey = os.path.basename(var)
107 121 # print 30 first characters from every var
@@ -132,7 +146,17 b" def magic_store(self, parameter_s=''):"
132 146 return
133 147
134 148 # %store foo
149 try:
135 150 obj = ip.ev(args[0])
151 except NameError:
152 # it might be an alias
153 if args[0] in self.alias_table:
154 staliases = db.get('stored_aliases',{})
155 staliases[ args[0] ] = self.alias_table[ args[0] ]
156 db['stored_aliases'] = staliases
157 print "Alias stored:", args[0], self.alias_table[ args[0] ]
158 return
159 else:
136 160 if isinstance(inspect.getmodule(obj), FakeModule):
137 161 print textwrap.dedent("""\
138 162 Warning:%s is %s
@@ -146,4 +170,3 b" def magic_store(self, parameter_s=''):"
146 170 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
147 171
148 172 ip.expose_magic('store',magic_store)
149 No newline at end of file
@@ -11,6 +11,8 b''
11 11
12 12 * hooks.py: Two new hooks, 'shutdown_hook' and 'late_startup_hook'.
13 13
14 * aliases can now be %store'd
15
14 16 2006-01-29 Fernando Perez <Fernando.Perez@colorado.edu>
15 17
16 18 * IPython/iplib.py (interact): Fix that we were not catching
General Comments 0
You need to be logged in to leave comments. Login now