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