Show More
@@ -1,176 +1,176 | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | %store magic for lightweight persistence. |
|
3 | %store magic for lightweight persistence. | |
4 |
|
4 | |||
5 | Stores variables, aliases etc. in PickleShare database. |
|
5 | Stores variables, aliases etc. in PickleShare database. | |
6 |
|
6 | |||
7 | $Id: iplib.py 1107 2006-01-30 19:02:20Z vivainio $ |
|
7 | $Id: iplib.py 1107 2006-01-30 19:02:20Z vivainio $ | |
8 | """ |
|
8 | """ | |
9 |
|
9 | |||
10 | import IPython.ipapi |
|
10 | import IPython.ipapi | |
11 | ip = IPython.ipapi.get() |
|
11 | ip = IPython.ipapi.get() | |
12 |
|
12 | |||
13 | import pickleshare |
|
13 | import pickleshare | |
14 |
|
14 | |||
15 | import inspect,pickle,os,textwrap |
|
15 | import inspect,pickle,os,sys,textwrap | |
16 | from IPython.FakeModule import FakeModule |
|
16 | from IPython.FakeModule import FakeModule | |
17 |
|
17 | |||
18 | def restore_aliases(self): |
|
18 | def restore_aliases(self): | |
19 | ip = self.getapi() |
|
19 | ip = self.getapi() | |
20 | staliases = ip.getdb().get('stored_aliases', {}) |
|
20 | staliases = ip.getdb().get('stored_aliases', {}) | |
21 | for k,v in staliases.items(): |
|
21 | for k,v in staliases.items(): | |
22 | #print "restore alias",k,v # dbg |
|
22 | #print "restore alias",k,v # dbg | |
23 | self.alias_table[k] = v |
|
23 | self.alias_table[k] = v | |
24 |
|
24 | |||
25 |
|
25 | |||
26 | def refresh_variables(ip): |
|
26 | def refresh_variables(ip): | |
27 | db = ip.getdb() |
|
27 | db = ip.getdb() | |
28 | for key in db.keys('autorestore/*'): |
|
28 | for key in db.keys('autorestore/*'): | |
29 | # strip autorestore |
|
29 | # strip autorestore | |
30 | justkey = os.path.basename(key) |
|
30 | justkey = os.path.basename(key) | |
31 | try: |
|
31 | try: | |
32 | obj = db[key] |
|
32 | obj = db[key] | |
33 | except KeyError: |
|
33 | except KeyError: | |
34 | print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey |
|
34 | print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey | |
35 | print "The error was:",sys.exc_info()[0] |
|
35 | print "The error was:",sys.exc_info()[0] | |
36 | else: |
|
36 | else: | |
37 | #print "restored",justkey,"=",obj #dbg |
|
37 | #print "restored",justkey,"=",obj #dbg | |
38 | ip.user_ns()[justkey] = obj |
|
38 | ip.user_ns()[justkey] = obj | |
39 |
|
39 | |||
40 |
|
40 | |||
41 |
|
41 | |||
42 | def restore_data(self): |
|
42 | def restore_data(self): | |
43 | ip = self.getapi() |
|
43 | ip = self.getapi() | |
44 | refresh_variables(ip) |
|
44 | refresh_variables(ip) | |
45 | restore_aliases(self) |
|
45 | restore_aliases(self) | |
46 | raise IPython.ipapi.TryNext |
|
46 | raise IPython.ipapi.TryNext | |
47 |
|
47 | |||
48 | ip.set_hook('late_startup_hook', restore_data) |
|
48 | ip.set_hook('late_startup_hook', restore_data) | |
49 |
|
49 | |||
50 | def magic_store(self, parameter_s=''): |
|
50 | def magic_store(self, parameter_s=''): | |
51 | """Lightweight persistence for python variables. |
|
51 | """Lightweight persistence for python variables. | |
52 |
|
52 | |||
53 | Example: |
|
53 | Example: | |
54 |
|
54 | |||
55 | ville@badger[~]|1> A = ['hello',10,'world']\\ |
|
55 | ville@badger[~]|1> A = ['hello',10,'world']\\ | |
56 | ville@badger[~]|2> %store A\\ |
|
56 | ville@badger[~]|2> %store A\\ | |
57 | ville@badger[~]|3> Exit |
|
57 | ville@badger[~]|3> Exit | |
58 |
|
58 | |||
59 | (IPython session is closed and started again...) |
|
59 | (IPython session is closed and started again...) | |
60 |
|
60 | |||
61 | ville@badger:~$ ipython -p pysh\\ |
|
61 | ville@badger:~$ ipython -p pysh\\ | |
62 | ville@badger[~]|1> print A |
|
62 | ville@badger[~]|1> print A | |
63 |
|
63 | |||
64 | ['hello', 10, 'world'] |
|
64 | ['hello', 10, 'world'] | |
65 |
|
65 | |||
66 | Usage: |
|
66 | Usage: | |
67 |
|
67 | |||
68 | %store - Show list of all variables and their current values\\ |
|
68 | %store - Show list of all variables and their current values\\ | |
69 | %store <var> - Store the *current* value of the variable to disk\\ |
|
69 | %store <var> - Store the *current* value of the variable to disk\\ | |
70 | %store -d <var> - Remove the variable and its value from storage\\ |
|
70 | %store -d <var> - Remove the variable and its value from storage\\ | |
71 | %store -z - Remove all variables from storage\\ |
|
71 | %store -z - Remove all variables from storage\\ | |
72 | %store -r - Refresh all variables from store (delete current vals)\\ |
|
72 | %store -r - Refresh all variables from store (delete current vals)\\ | |
73 | %store foo >a.txt - Store value of foo to new file a.txt\\ |
|
73 | %store foo >a.txt - Store value of foo to new file a.txt\\ | |
74 | %store foo >>a.txt - Append value of foo to file a.txt\\ |
|
74 | %store foo >>a.txt - Append value of foo to file a.txt\\ | |
75 |
|
75 | |||
76 | It should be noted that if you change the value of a variable, you |
|
76 | It should be noted that if you change the value of a variable, you | |
77 | need to %store it again if you want to persist the new value. |
|
77 | need to %store it again if you want to persist the new value. | |
78 |
|
78 | |||
79 | Note also that the variables will need to be pickleable; most basic |
|
79 | Note also that the variables will need to be pickleable; most basic | |
80 | python types can be safely %stored. |
|
80 | python types can be safely %stored. | |
81 | """ |
|
81 | """ | |
82 |
|
82 | |||
83 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') |
|
83 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') | |
84 | args = argsl.split(None,1) |
|
84 | args = argsl.split(None,1) | |
85 | ip = self.getapi() |
|
85 | ip = self.getapi() | |
86 | db = ip.getdb() |
|
86 | db = ip.getdb() | |
87 | # delete |
|
87 | # delete | |
88 | if opts.has_key('d'): |
|
88 | if opts.has_key('d'): | |
89 | try: |
|
89 | try: | |
90 | todel = args[0] |
|
90 | todel = args[0] | |
91 | except IndexError: |
|
91 | except IndexError: | |
92 | error('You must provide the variable to forget') |
|
92 | error('You must provide the variable to forget') | |
93 | else: |
|
93 | else: | |
94 | try: |
|
94 | try: | |
95 | del db['autorestore/' + todel] |
|
95 | del db['autorestore/' + todel] | |
96 | except: |
|
96 | except: | |
97 | error("Can't delete variable '%s'" % todel) |
|
97 | error("Can't delete variable '%s'" % todel) | |
98 | # reset |
|
98 | # reset | |
99 | elif opts.has_key('z'): |
|
99 | elif opts.has_key('z'): | |
100 | for k in db.keys('autorestore/*'): |
|
100 | for k in db.keys('autorestore/*'): | |
101 | del db[k] |
|
101 | del db[k] | |
102 |
|
102 | |||
103 | elif opts.has_key('r'): |
|
103 | elif opts.has_key('r'): | |
104 | refresh_variables(ip) |
|
104 | refresh_variables(ip) | |
105 |
|
105 | |||
106 |
|
106 | |||
107 | # run without arguments -> list variables & values |
|
107 | # run without arguments -> list variables & values | |
108 | elif not args: |
|
108 | elif not args: | |
109 | vars = self.db.keys('autorestore/*') |
|
109 | vars = self.db.keys('autorestore/*') | |
110 | vars.sort() |
|
110 | vars.sort() | |
111 | if vars: |
|
111 | if vars: | |
112 | size = max(map(len,vars)) |
|
112 | size = max(map(len,vars)) | |
113 | else: |
|
113 | else: | |
114 | size = 0 |
|
114 | size = 0 | |
115 |
|
115 | |||
116 | print 'Stored variables and their in-db values:' |
|
116 | print 'Stored variables and their in-db values:' | |
117 | fmt = '%-'+str(size)+'s -> %s' |
|
117 | fmt = '%-'+str(size)+'s -> %s' | |
118 | get = db.get |
|
118 | get = db.get | |
119 | for var in vars: |
|
119 | for var in vars: | |
120 | justkey = os.path.basename(var) |
|
120 | justkey = os.path.basename(var) | |
121 | # print 30 first characters from every var |
|
121 | # print 30 first characters from every var | |
122 | print fmt % (justkey,repr(get(var,'<unavailable>'))[:50]) |
|
122 | print fmt % (justkey,repr(get(var,'<unavailable>'))[:50]) | |
123 |
|
123 | |||
124 | # default action - store the variable |
|
124 | # default action - store the variable | |
125 | else: |
|
125 | else: | |
126 | # %store foo >file.txt or >>file.txt |
|
126 | # %store foo >file.txt or >>file.txt | |
127 | if len(args) > 1 and args[1].startswith('>'): |
|
127 | if len(args) > 1 and args[1].startswith('>'): | |
128 | fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) |
|
128 | fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) | |
129 | if args[1].startswith('>>'): |
|
129 | if args[1].startswith('>>'): | |
130 | fil = open(fnam,'a') |
|
130 | fil = open(fnam,'a') | |
131 | else: |
|
131 | else: | |
132 | fil = open(fnam,'w') |
|
132 | fil = open(fnam,'w') | |
133 | obj = ip.ev(args[0]) |
|
133 | obj = ip.ev(args[0]) | |
134 | print "Writing '%s' (%s) to file '%s'." % (args[0], |
|
134 | print "Writing '%s' (%s) to file '%s'." % (args[0], | |
135 | obj.__class__.__name__, fnam) |
|
135 | obj.__class__.__name__, fnam) | |
136 |
|
136 | |||
137 |
|
137 | |||
138 | if not isinstance (obj,basestring): |
|
138 | if not isinstance (obj,basestring): | |
139 | from pprint import pprint |
|
139 | from pprint import pprint | |
140 | pprint(obj,fil) |
|
140 | pprint(obj,fil) | |
141 | else: |
|
141 | else: | |
142 | fil.write(obj) |
|
142 | fil.write(obj) | |
143 | if not obj.endswith('\n'): |
|
143 | if not obj.endswith('\n'): | |
144 | fil.write('\n') |
|
144 | fil.write('\n') | |
145 |
|
145 | |||
146 | fil.close() |
|
146 | fil.close() | |
147 | return |
|
147 | return | |
148 |
|
148 | |||
149 | # %store foo |
|
149 | # %store foo | |
150 | try: |
|
150 | try: | |
151 | obj = ip.user_ns()[args[0]] |
|
151 | obj = ip.user_ns()[args[0]] | |
152 | except KeyError: |
|
152 | except KeyError: | |
153 | # it might be an alias |
|
153 | # it might be an alias | |
154 | if args[0] in self.alias_table: |
|
154 | if args[0] in self.alias_table: | |
155 | staliases = db.get('stored_aliases',{}) |
|
155 | staliases = db.get('stored_aliases',{}) | |
156 | staliases[ args[0] ] = self.alias_table[ args[0] ] |
|
156 | staliases[ args[0] ] = self.alias_table[ args[0] ] | |
157 | db['stored_aliases'] = staliases |
|
157 | db['stored_aliases'] = staliases | |
158 | print "Alias stored:", args[0], self.alias_table[ args[0] ] |
|
158 | print "Alias stored:", args[0], self.alias_table[ args[0] ] | |
159 | return |
|
159 | return | |
160 | else: |
|
160 | else: | |
161 | print "Error: unknown variable '%s'" % args[0] |
|
161 | print "Error: unknown variable '%s'" % args[0] | |
162 |
|
162 | |||
163 | else: |
|
163 | else: | |
164 | if isinstance(inspect.getmodule(obj), FakeModule): |
|
164 | if isinstance(inspect.getmodule(obj), FakeModule): | |
165 | print textwrap.dedent("""\ |
|
165 | print textwrap.dedent("""\ | |
166 | Warning:%s is %s |
|
166 | Warning:%s is %s | |
167 | Proper storage of interactively declared classes (or instances |
|
167 | Proper storage of interactively declared classes (or instances | |
168 | of those classes) is not possible! Only instances |
|
168 | of those classes) is not possible! Only instances | |
169 | of classes in real modules on file system can be %%store'd. |
|
169 | of classes in real modules on file system can be %%store'd. | |
170 | """ % (args[0], obj) ) |
|
170 | """ % (args[0], obj) ) | |
171 | return |
|
171 | return | |
172 | #pickled = pickle.dumps(obj) |
|
172 | #pickled = pickle.dumps(obj) | |
173 | self.db[ 'autorestore/' + args[0] ] = obj |
|
173 | self.db[ 'autorestore/' + args[0] ] = obj | |
174 | print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__) |
|
174 | print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__) | |
175 |
|
175 | |||
176 | ip.expose_magic('store',magic_store) |
|
176 | ip.expose_magic('store',magic_store) |
General Comments 0
You need to be logged in to leave comments.
Login now