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