| 1 |
from pecan.compat import getargspec as _getargspec |
| 2 |
|
| 3 |
|
| 4 |
def iscontroller(obj): |
| 5 |
return getattr(obj, 'exposed', False) |
| 6 |
|
| 7 |
|
| 8 |
def getargspec(method): |
| 9 |
""" |
| 10 |
Drill through layers of decorators attempting to locate the actual argspec |
| 11 |
for a method. |
| 12 |
""" |
| 13 |
|
| 14 |
argspec = _getargspec(method) |
| 15 |
args = argspec[0] |
| 16 |
if args and args[0] == 'self': |
| 17 |
return argspec |
| 18 |
if hasattr(method, '__func__'): |
| 19 |
method = method.__func__ |
| 20 |
|
| 21 |
func_closure = method.__closure__ |
| 22 |
|
| 23 |
# NOTE(sileht): if the closure is None we cannot look deeper, |
| 24 |
# so return actual argspec, this occurs when the method |
| 25 |
# is static for example. |
| 26 |
if not func_closure: |
| 27 |
return argspec |
| 28 |
|
| 29 |
closure = None |
| 30 |
# In the case of deeply nested decorators (with arguments), it's possible |
| 31 |
# that there are several callables in scope; Take a best guess and go |
| 32 |
# with the one that looks most like a pecan controller function |
| 33 |
# (has a __code__ object, and 'self' is the first argument) |
| 34 |
func_closure = filter( |
| 35 |
lambda c: ( |
| 36 |
callable(c.cell_contents) and |
| 37 |
hasattr(c.cell_contents, '__code__') |
| 38 |
), |
| 39 |
func_closure |
| 40 |
) |
| 41 |
func_closure = sorted( |
| 42 |
func_closure, |
| 43 |
key=lambda c: 'self' in c.cell_contents.__code__.co_varnames, |
| 44 |
reverse=True |
| 45 |
) |
| 46 |
|
| 47 |
closure = func_closure[0] |
| 48 |
|
| 49 |
method = closure.cell_contents |
| 50 |
return getargspec(method) |
| 51 |
|
| 52 |
|
| 53 |
def _cfg(f): |
| 54 |
if not hasattr(f, '_pecan'): |
| 55 |
f._pecan = {} |
| 56 |
return f._pecan |