将函数传递给 cherrypy 中的装饰器

passing function to a decorator in cherrypy

如何将 index() 传递给名为 uppercase() 的装饰函数。我希望从 index() 传递的值被大写。

我收到这个错误:

500 Internal Server Error
The server encountered an unexpected condition which prevented it from fulfilling the request.

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 638, in respond
    self._do_respond(path_info)
  File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 701, in _do_respond
    self.hooks.run('before_finalize')
  File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 95, in run
    self.run_hooks(iter(sorted(self[point])))
  File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 117, in run_hooks
    hook()
  File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 65, in __call__
    return self.callback(**self.kwargs)
TypeError: uppercase() missing 1 required positional argument: 'func'

import cherrypy
from cherrypy import tools

@cherrypy.tools.register('before_finalize')
def uppercase(func):
    def wrapper():
        original_result = func()
        modified_result = original_result.upper()
        return modified_result
    return wrapper

class HelloWorld(object):
    @cherrypy.expose
    @cherrypy.tools.uppercase()
    def index(self):
        return 'Hello!'


if __name__ == '__main__':
    cherrypy.tools.uppercase = cherrypy.Tool('before_finalize', uppercase)
    cherrypy.quickstart(HelloWorld())

你应该像这样使用装饰器:

@cherrypy.tools.uppercase
def index(self):
    return 'Hello!'

请注意 uppercase 之后缺少 () - 装饰器隐式采用第一个参数(装饰函数)并且不需要括号。

编辑:

根据the documentation,可以使用cherrypy.tools.register装饰器或cherrypy.Tool构造器来定义工具。在您的代码中,您定义了两次 uppercase 工具。

但是,在您的情况下,uppercase 装饰器不需要定义为 Tool,因为它不是每次都需要 运行 作为钩子的东西( before_finalize).

因此,将它用作普通 Python 装饰器会更好,如下所示:

from functools import wraps

# just a plain Python decorator
def uppercase(func):
    @wraps(func) #preserve function attributes, such as its name
    def wrapper(*args):
        original_result = func(*args)
        modified_result = original_result.upper()
        return modified_result
    return wrapper

class HelloWorld(object):
    @cherrypy.expose
    @uppercase # decorated once, the exposed function is now uppercase(index) 
    def index(self):
        return 'Hello!'