Show More
@@ -130,16 +130,66 b' def _magic_marker(magic_type):' | |||
|
130 | 130 | else: |
|
131 | 131 | raise ValueError("Decorator can only be called with " |
|
132 | 132 | "string or function") |
|
133 | return retval | |
|
134 | ||
|
135 | return magic_deco | |
|
136 | ||
|
133 | 137 | |
|
138 | def _function_magic_marker(magic_type): | |
|
139 | validate_type(magic_type) | |
|
140 | ||
|
141 | # This is a closure to capture the magic_type. We could also use a class, | |
|
142 | # but it's overkill for just that one bit of state. | |
|
143 | def magic_deco(arg): | |
|
144 | call = lambda f, *a, **k: f(*a, **k) | |
|
145 | ||
|
146 | # Find get_ipython() in the caller's namespace | |
|
147 | caller = sys._getframe(1) | |
|
148 | for ns in ['f_locals', 'f_globals', 'f_builtins']: | |
|
149 | get_ipython = getattr(caller, ns).get('get_ipython') | |
|
150 | if get_ipython is not None: | |
|
151 | break | |
|
152 | else: | |
|
153 | raise('Decorator can only run in context where `get_ipython` exists') | |
|
154 | ||
|
155 | ip = get_ipython() | |
|
156 | ||
|
157 | if callable(arg): | |
|
158 | # "Naked" decorator call (just @foo, no args) | |
|
159 | func = arg | |
|
160 | #name = func.func_name | |
|
161 | #func.magic_name = name | |
|
162 | ip.register_magic_function(func) | |
|
163 | retval = decorator(call, func) | |
|
164 | elif isinstance(arg, basestring): | |
|
165 | # Decorator called with arguments (@foo('bar')) | |
|
166 | name = arg | |
|
167 | def mark(func, *a, **kw): | |
|
168 | #func.magic_name = name | |
|
169 | ip.register_magic_function(func) | |
|
170 | return decorator(call, func) | |
|
171 | retval = mark | |
|
172 | else: | |
|
173 | raise ValueError("Decorator can only be called with " | |
|
174 | "string or function") | |
|
134 | 175 | return retval |
|
135 | 176 | |
|
136 | 177 | return magic_deco |
|
137 | 178 | |
|
138 | 179 | |
|
180 | # Create the actual decorators for public use | |
|
181 | ||
|
182 | # These three are used to decorate methods in class definitions | |
|
139 | 183 | line_magic = _magic_marker('line') |
|
140 | 184 | cell_magic = _magic_marker('cell') |
|
141 | 185 | line_cell_magic = _magic_marker('line_cell') |
|
142 | 186 | |
|
187 | # These three decorate standalone functions and perform the decoration | |
|
188 | # immediately. They can only run where get_ipython() works | |
|
189 | register_line_magic = _function_magic_marker('line') | |
|
190 | register_cell_magic = _function_magic_marker('cell') | |
|
191 | register_line_cell_magic = _function_magic_marker('line_cell') | |
|
192 | ||
|
143 | 193 | #----------------------------------------------------------------------------- |
|
144 | 194 | # Core Magic classes |
|
145 | 195 | #----------------------------------------------------------------------------- |
@@ -1,7 +1,7 b'' | |||
|
1 | 1 | """Implementation of all the magic functions built into IPython. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 |
# Copyright (c) 2012 |
|
|
4 | # Copyright (c) 2012 The IPython Development Team. | |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
General Comments 0
You need to be logged in to leave comments.
Login now