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 |
|
|
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 |
|
|
87 |
del |
|
|
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 = |
|
|
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,18 +146,27 b" def magic_store(self, parameter_s=''):" | |||
|
132 | 146 | return |
|
133 | 147 | |
|
134 | 148 | # %store foo |
|
135 | obj = ip.ev(args[0]) | |
|
136 | if isinstance(inspect.getmodule(obj), FakeModule): | |
|
137 | print textwrap.dedent("""\ | |
|
138 | Warning:%s is %s | |
|
139 | Proper storage of interactively declared classes (or instances | |
|
140 | of those classes) is not possible! Only instances | |
|
141 | of classes in real modules on file system can be %%store'd. | |
|
142 | """ % (args[0], obj) ) | |
|
143 | return | |
|
144 | #pickled = pickle.dumps(obj) | |
|
145 | self.db[ 'autorestore/' + args[0] ] = obj | |
|
146 | print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__) | |
|
149 | try: | |
|
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: | |
|
160 | if isinstance(inspect.getmodule(obj), FakeModule): | |
|
161 | print textwrap.dedent("""\ | |
|
162 | Warning:%s is %s | |
|
163 | Proper storage of interactively declared classes (or instances | |
|
164 | of those classes) is not possible! Only instances | |
|
165 | of classes in real modules on file system can be %%store'd. | |
|
166 | """ % (args[0], obj) ) | |
|
167 | return | |
|
168 | #pickled = pickle.dumps(obj) | |
|
169 | self.db[ 'autorestore/' + args[0] ] = obj | |
|
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 |
General Comments 0
You need to be logged in to leave comments.
Login now