Decorator factory for methods in Magics subclasses.
(magic_kind)
| 176 | # and make a single one with convoluted logic. |
| 177 | |
| 178 | def _method_magic_marker(magic_kind): |
| 179 | """Decorator factory for methods in Magics subclasses. |
| 180 | """ |
| 181 | |
| 182 | validate_type(magic_kind) |
| 183 | |
| 184 | # This is a closure to capture the magic_kind. We could also use a class, |
| 185 | # but it's overkill for just that one bit of state. |
| 186 | def magic_deco(arg): |
| 187 | call = lambda f, *a, **k: f(*a, **k) |
| 188 | |
| 189 | if callable(arg): |
| 190 | # "Naked" decorator call (just @foo, no args) |
| 191 | func = arg |
| 192 | name = func.__name__ |
| 193 | retval = decorator(call, func) |
| 194 | record_magic(magics, magic_kind, name, name) |
| 195 | elif isinstance(arg, str): |
| 196 | # Decorator called with arguments (@foo('bar')) |
| 197 | name = arg |
| 198 | def mark(func, *a, **kw): |
| 199 | record_magic(magics, magic_kind, name, func.__name__) |
| 200 | return decorator(call, func) |
| 201 | retval = mark |
| 202 | else: |
| 203 | raise TypeError("Decorator can only be called with " |
| 204 | "string or function") |
| 205 | return retval |
| 206 | |
| 207 | # Ensure the resulting decorator has a usable docstring |
| 208 | magic_deco.__doc__ = _docstring_template.format('method', magic_kind) |
| 209 | return magic_deco |
| 210 | |
| 211 | |
| 212 | def _function_magic_marker(magic_kind): |
no test coverage detected