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