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