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 | import IPython.ipapi |
|
10 | import IPython.ipapi | |
2 | ip = IPython.ipapi.get() |
|
11 | ip = IPython.ipapi.get() | |
3 |
|
12 | |||
@@ -6,6 +15,14 b' import pickleshare' | |||||
6 | import inspect,pickle,os,textwrap |
|
15 | import inspect,pickle,os,textwrap | |
7 | from IPython.FakeModule import FakeModule |
|
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 | def refresh_variables(ip): |
|
26 | def refresh_variables(ip): | |
10 | db = ip.getdb() |
|
27 | db = ip.getdb() | |
11 | for key in db.keys('autorestore/*'): |
|
28 | for key in db.keys('autorestore/*'): | |
@@ -23,15 +40,11 b' def refresh_variables(ip):' | |||||
23 |
|
40 | |||
24 |
|
41 | |||
25 | def restore_data(self): |
|
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 | ip = self.getapi() |
|
43 | ip = self.getapi() | |
31 | refresh_variables(ip) |
|
44 | refresh_variables(ip) | |
|
45 | restore_aliases(self) | |||
32 | raise IPython.ipapi.TryNext |
|
46 | raise IPython.ipapi.TryNext | |
33 |
|
47 | |||
34 |
|
||||
35 | ip.set_hook('late_startup_hook', restore_data) |
|
48 | ip.set_hook('late_startup_hook', restore_data) | |
36 |
|
49 | |||
37 | def magic_store(self, parameter_s=''): |
|
50 | def magic_store(self, parameter_s=''): | |
@@ -70,6 +83,7 b" def magic_store(self, parameter_s=''):" | |||||
70 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') |
|
83 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') | |
71 | args = argsl.split(None,1) |
|
84 | args = argsl.split(None,1) | |
72 | ip = self.getapi() |
|
85 | ip = self.getapi() | |
|
86 | db = ip.getdb() | |||
73 | # delete |
|
87 | # delete | |
74 | if opts.has_key('d'): |
|
88 | if opts.has_key('d'): | |
75 | try: |
|
89 | try: | |
@@ -78,13 +92,13 b" def magic_store(self, parameter_s=''):" | |||||
78 | error('You must provide the variable to forget') |
|
92 | error('You must provide the variable to forget') | |
79 | else: |
|
93 | else: | |
80 | try: |
|
94 | try: | |
81 |
del |
|
95 | del db['autorestore/' + todel] | |
82 | except: |
|
96 | except: | |
83 | error("Can't delete variable '%s'" % todel) |
|
97 | error("Can't delete variable '%s'" % todel) | |
84 | # reset |
|
98 | # reset | |
85 | elif opts.has_key('z'): |
|
99 | elif opts.has_key('z'): | |
86 |
for k in |
|
100 | for k in db.keys('autorestore/*'): | |
87 |
del |
|
101 | del db[k] | |
88 |
|
102 | |||
89 | elif opts.has_key('r'): |
|
103 | elif opts.has_key('r'): | |
90 | refresh_variables(ip) |
|
104 | refresh_variables(ip) | |
@@ -101,7 +115,7 b" def magic_store(self, parameter_s=''):" | |||||
101 |
|
115 | |||
102 | print 'Stored variables and their in-db values:' |
|
116 | print 'Stored variables and their in-db values:' | |
103 | fmt = '%-'+str(size)+'s -> %s' |
|
117 | fmt = '%-'+str(size)+'s -> %s' | |
104 |
get = |
|
118 | get = db.get | |
105 | for var in vars: |
|
119 | for var in vars: | |
106 | justkey = os.path.basename(var) |
|
120 | justkey = os.path.basename(var) | |
107 | # print 30 first characters from every var |
|
121 | # print 30 first characters from every var | |
@@ -132,18 +146,27 b" def magic_store(self, parameter_s=''):" | |||||
132 | return |
|
146 | return | |
133 |
|
147 | |||
134 | # %store foo |
|
148 | # %store foo | |
135 | obj = ip.ev(args[0]) |
|
149 | try: | |
136 | if isinstance(inspect.getmodule(obj), FakeModule): |
|
150 | obj = ip.ev(args[0]) | |
137 | print textwrap.dedent("""\ |
|
151 | except NameError: | |
138 | Warning:%s is %s |
|
152 | # it might be an alias | |
139 | Proper storage of interactively declared classes (or instances |
|
153 | if args[0] in self.alias_table: | |
140 | of those classes) is not possible! Only instances |
|
154 | staliases = db.get('stored_aliases',{}) | |
141 | of classes in real modules on file system can be %%store'd. |
|
155 | staliases[ args[0] ] = self.alias_table[ args[0] ] | |
142 | """ % (args[0], obj) ) |
|
156 | db['stored_aliases'] = staliases | |
143 | return |
|
157 | print "Alias stored:", args[0], self.alias_table[ args[0] ] | |
144 | #pickled = pickle.dumps(obj) |
|
158 | return | |
145 | self.db[ 'autorestore/' + args[0] ] = obj |
|
159 | else: | |
146 | print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__) |
|
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 | ip.expose_magic('store',magic_store) |
|
172 | ip.expose_magic('store',magic_store) | |
149 | No newline at end of file |
|
@@ -10,6 +10,8 b'' | |||||
10 | http://www.python.org/pypi/pickleshare |
|
10 | http://www.python.org/pypi/pickleshare | |
11 |
|
11 | |||
12 | * hooks.py: Two new hooks, 'shutdown_hook' and 'late_startup_hook'. |
|
12 | * hooks.py: Two new hooks, 'shutdown_hook' and 'late_startup_hook'. | |
|
13 | ||||
|
14 | * aliases can now be %store'd | |||
13 |
|
15 | |||
14 | 2006-01-29 Fernando Perez <Fernando.Perez@colorado.edu> |
|
16 | 2006-01-29 Fernando Perez <Fernando.Perez@colorado.edu> | |
15 |
|
17 |
General Comments 0
You need to be logged in to leave comments.
Login now