Show More
@@ -1,224 +1,227 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | %store magic for lightweight persistence. |
|
4 | 4 | |
|
5 | 5 | Stores variables, aliases and macros in IPython's database. |
|
6 | 6 | |
|
7 | 7 | To automatically restore stored variables at startup, add this to your |
|
8 | 8 | :file:`ipython_config.py` file:: |
|
9 | 9 | |
|
10 | 10 | c.StoreMagic.autorestore = True |
|
11 | 11 | """ |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Copyright (c) 2012, The IPython Development Team. |
|
14 | 14 | # |
|
15 | 15 | # Distributed under the terms of the Modified BSD License. |
|
16 | 16 | # |
|
17 | 17 | # The full license is in the file COPYING.txt, distributed with this software. |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Imports |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | |
|
24 | 24 | # Stdlib |
|
25 | 25 | import inspect, os, sys, textwrap |
|
26 | 26 | |
|
27 | 27 | # Our own |
|
28 | 28 | from IPython.core.error import UsageError |
|
29 | 29 | from IPython.core.fakemodule import FakeModule |
|
30 | 30 | from IPython.core.magic import Magics, magics_class, line_magic |
|
31 | 31 | from IPython.testing.skipdoctest import skip_doctest |
|
32 | 32 | |
|
33 | 33 | #----------------------------------------------------------------------------- |
|
34 | 34 | # Functions and classes |
|
35 | 35 | #----------------------------------------------------------------------------- |
|
36 | 36 | |
|
37 | 37 | def restore_aliases(ip): |
|
38 | 38 | staliases = ip.db.get('stored_aliases', {}) |
|
39 | 39 | for k,v in staliases.items(): |
|
40 | 40 | #print "restore alias",k,v # dbg |
|
41 | 41 | #self.alias_table[k] = v |
|
42 | 42 | ip.alias_manager.define_alias(k,v) |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | def refresh_variables(ip): |
|
46 | 46 | db = ip.db |
|
47 | 47 | for key in db.keys('autorestore/*'): |
|
48 | 48 | # strip autorestore |
|
49 | 49 | justkey = os.path.basename(key) |
|
50 | 50 | try: |
|
51 | 51 | obj = db[key] |
|
52 | 52 | except KeyError: |
|
53 | 53 | print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey |
|
54 | 54 | print "The error was:", sys.exc_info()[0] |
|
55 | 55 | else: |
|
56 | 56 | #print "restored",justkey,"=",obj #dbg |
|
57 | 57 | ip.user_ns[justkey] = obj |
|
58 | 58 | |
|
59 | 59 | |
|
60 | 60 | def restore_dhist(ip): |
|
61 | 61 | ip.user_ns['_dh'] = ip.db.get('dhist',[]) |
|
62 | 62 | |
|
63 | 63 | |
|
64 | 64 | def restore_data(ip): |
|
65 | 65 | refresh_variables(ip) |
|
66 | 66 | restore_aliases(ip) |
|
67 | 67 | restore_dhist(ip) |
|
68 | 68 | |
|
69 | 69 | |
|
70 | 70 | @magics_class |
|
71 | 71 | class StoreMagics(Magics): |
|
72 | 72 | """Lightweight persistence for python variables. |
|
73 | 73 | |
|
74 | 74 | Provides the %store magic.""" |
|
75 | 75 | |
|
76 | 76 | @skip_doctest |
|
77 | 77 | @line_magic |
|
78 | 78 | def store(self, parameter_s=''): |
|
79 | 79 | """Lightweight persistence for python variables. |
|
80 | 80 | |
|
81 | 81 | Example:: |
|
82 | 82 | |
|
83 | 83 | In [1]: l = ['hello',10,'world'] |
|
84 | 84 | In [2]: %store l |
|
85 | 85 | In [3]: exit |
|
86 | 86 | |
|
87 | 87 | (IPython session is closed and started again...) |
|
88 | 88 | |
|
89 | 89 | ville@badger:~$ ipython |
|
90 | 90 | In [1]: l |
|
91 | Out[1]: ['hello', 10, 'world'] | |
|
91 | NameError: name 'l' is not defined | |
|
92 | In [2]: %store -r | |
|
93 | In [3]: l | |
|
94 | Out[3]: ['hello', 10, 'world'] | |
|
92 | 95 | |
|
93 | 96 | Usage: |
|
94 | 97 | |
|
95 | 98 | * ``%store`` - Show list of all variables and their current |
|
96 | 99 | values |
|
97 | 100 | * ``%store spam`` - Store the *current* value of the variable spam |
|
98 | 101 | to disk |
|
99 | 102 | * ``%store -d spam`` - Remove the variable and its value from storage |
|
100 | 103 | * ``%store -z`` - Remove all variables from storage |
|
101 |
* ``%store -r`` - Refresh all variables from store ( |
|
|
104 | * ``%store -r`` - Refresh all variables from store (overwrite | |
|
102 | 105 | current vals) |
|
103 | 106 | * ``%store -r spam bar`` - Refresh specified variables from store |
|
104 | 107 | (delete current val) |
|
105 | 108 | * ``%store foo >a.txt`` - Store value of foo to new file a.txt |
|
106 | 109 | * ``%store foo >>a.txt`` - Append value of foo to file a.txt |
|
107 | 110 | |
|
108 | 111 | It should be noted that if you change the value of a variable, you |
|
109 | 112 | need to %store it again if you want to persist the new value. |
|
110 | 113 | |
|
111 | 114 | Note also that the variables will need to be pickleable; most basic |
|
112 | 115 | python types can be safely %store'd. |
|
113 | 116 | |
|
114 | 117 | Also aliases can be %store'd across sessions. |
|
115 | 118 | """ |
|
116 | 119 | |
|
117 | 120 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') |
|
118 | 121 | args = argsl.split(None,1) |
|
119 | 122 | ip = self.shell |
|
120 | 123 | db = ip.db |
|
121 | 124 | # delete |
|
122 | 125 | if 'd' in opts: |
|
123 | 126 | try: |
|
124 | 127 | todel = args[0] |
|
125 | 128 | except IndexError: |
|
126 | 129 | raise UsageError('You must provide the variable to forget') |
|
127 | 130 | else: |
|
128 | 131 | try: |
|
129 | 132 | del db['autorestore/' + todel] |
|
130 | 133 | except: |
|
131 | 134 | raise UsageError("Can't delete variable '%s'" % todel) |
|
132 | 135 | # reset |
|
133 | 136 | elif 'z' in opts: |
|
134 | 137 | for k in db.keys('autorestore/*'): |
|
135 | 138 | del db[k] |
|
136 | 139 | |
|
137 | 140 | elif 'r' in opts: |
|
138 | 141 | if args: |
|
139 | 142 | for arg in args: |
|
140 | 143 | try: |
|
141 | 144 | obj = db['autorestore/' + arg] |
|
142 | 145 | except KeyError: |
|
143 | 146 | print "no stored variable %s" % arg |
|
144 | 147 | else: |
|
145 | 148 | ip.user_ns[arg] = obj |
|
146 | 149 | else: |
|
147 | 150 | refresh_variables(ip) |
|
148 | 151 | |
|
149 | 152 | # run without arguments -> list variables & values |
|
150 | 153 | elif not args: |
|
151 | 154 | vars = db.keys('autorestore/*') |
|
152 | 155 | vars.sort() |
|
153 | 156 | if vars: |
|
154 | 157 | size = max(map(len, vars)) |
|
155 | 158 | else: |
|
156 | 159 | size = 0 |
|
157 | 160 | |
|
158 | 161 | print 'Stored variables and their in-db values:' |
|
159 | 162 | fmt = '%-'+str(size)+'s -> %s' |
|
160 | 163 | get = db.get |
|
161 | 164 | for var in vars: |
|
162 | 165 | justkey = os.path.basename(var) |
|
163 | 166 | # print 30 first characters from every var |
|
164 | 167 | print fmt % (justkey, repr(get(var, '<unavailable>'))[:50]) |
|
165 | 168 | |
|
166 | 169 | # default action - store the variable |
|
167 | 170 | else: |
|
168 | 171 | # %store foo >file.txt or >>file.txt |
|
169 | 172 | if len(args) > 1 and args[1].startswith('>'): |
|
170 | 173 | fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) |
|
171 | 174 | if args[1].startswith('>>'): |
|
172 | 175 | fil = open(fnam, 'a') |
|
173 | 176 | else: |
|
174 | 177 | fil = open(fnam, 'w') |
|
175 | 178 | obj = ip.ev(args[0]) |
|
176 | 179 | print "Writing '%s' (%s) to file '%s'." % (args[0], |
|
177 | 180 | obj.__class__.__name__, fnam) |
|
178 | 181 | |
|
179 | 182 | |
|
180 | 183 | if not isinstance (obj, basestring): |
|
181 | 184 | from pprint import pprint |
|
182 | 185 | pprint(obj, fil) |
|
183 | 186 | else: |
|
184 | 187 | fil.write(obj) |
|
185 | 188 | if not obj.endswith('\n'): |
|
186 | 189 | fil.write('\n') |
|
187 | 190 | |
|
188 | 191 | fil.close() |
|
189 | 192 | return |
|
190 | 193 | |
|
191 | 194 | # %store foo |
|
192 | 195 | try: |
|
193 | 196 | obj = ip.user_ns[args[0]] |
|
194 | 197 | except KeyError: |
|
195 | 198 | # it might be an alias |
|
196 | 199 | # This needs to be refactored to use the new AliasManager stuff. |
|
197 | 200 | if args[0] in ip.alias_manager: |
|
198 | 201 | name = args[0] |
|
199 | 202 | nargs, cmd = ip.alias_manager.alias_table[ name ] |
|
200 | 203 | staliases = db.get('stored_aliases',{}) |
|
201 | 204 | staliases[ name ] = cmd |
|
202 | 205 | db['stored_aliases'] = staliases |
|
203 | 206 | print "Alias stored: %s (%s)" % (name, cmd) |
|
204 | 207 | return |
|
205 | 208 | else: |
|
206 | 209 | raise UsageError("Unknown variable '%s'" % args[0]) |
|
207 | 210 | |
|
208 | 211 | else: |
|
209 | 212 | if isinstance(inspect.getmodule(obj), FakeModule): |
|
210 | 213 | print textwrap.dedent("""\ |
|
211 | 214 | Warning:%s is %s |
|
212 | 215 | Proper storage of interactively declared classes (or instances |
|
213 | 216 | of those classes) is not possible! Only instances |
|
214 | 217 | of classes in real modules on file system can be %%store'd. |
|
215 | 218 | """ % (args[0], obj) ) |
|
216 | 219 | return |
|
217 | 220 | #pickled = pickle.dumps(obj) |
|
218 | 221 | db[ 'autorestore/' + args[0] ] = obj |
|
219 | 222 | print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__) |
|
220 | 223 | |
|
221 | 224 | |
|
222 | 225 | def load_ipython_extension(ip): |
|
223 | 226 | """Load the extension in IPython.""" |
|
224 | 227 | ip.register_magics(StoreMagics) |
General Comments 0
You need to be logged in to leave comments.
Login now