我想添加一个装饰器函数,将 Bottle 请求装饰函数包装在一个类中,我尝试使用检查来执行此操作,但当我调用此请求时,我无法看到装饰器所做的任何更改。
这是我的代码:
import inspect
from bottle import run, route, template, get, post, request, delete, redirect
def decorator_jwt(func):
def authenticate_jwt(*args):
if(Afunction()):
func(*args)
else:
return False
return authenticate_jwt
def decorator_for_class(cls):
for name, method in inspect.getmembers(cls):
if (not inspect.ismethod(method) and not inspect.isfunction(method)) or inspect.isbuiltin(method):
continue
print("Decorating function %s" % name)
setattr(cls, name, decorator_jwt(method))
return cls
@decorator_for_class
class python_functions:
@route('/')
def show_index():
#something0
@get('/api/V1/something')
def Reboot():
#something
@get('/api/V1/something2')
def umountDevice():
#something2
@post('/api/V1/random/something3')
def LogOut():
#something3
route
装饰器将回调函数添加到存储在当前应用程序的Bottle
对象中的路由列表中,因此之后应用
decorator_jwt
装饰器对于已存储在列表中的回调函数没有任何影响。相反,您可以迭代当前应用程序的
Bottle
对象中存储的路由,以使用应用的
decorator_jwt
装饰器来更改存储的回调:
from bottle import app
def decorator_for_class(cls):
callbacks = {
func for func in inspect.getmembers(cls)
if inspect.isfunction(func) and not inspect.isbuiltin(func)
}
for route in app().routes:
if route.callback in callbacks:
route.callback = decorator_jwt(route.callback)
return cls