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