请记住,Python 中的装饰器语法是糖:
@decorator
def decorated():
pass
相当于:
def decorated():
pass
decorated = decorator(decorated)
因此,您可以print_howdy
在您的蓝图中定义并将其与您的蓝图一起导入。为了保持干净,您甚至可以在蓝图中定义一个信号侦听器列表,并一次性将它们全部导入:
mysignal.connect_via(app)(print_howdy)
甚至:
# blueprint.py
def print_howdy(): pass
def print_seeya(): pass
MYSIGNAL_LISTENERS = (print_howdy, print_seeya)
# __init__.py
from blueprint import MYSIGNAL_LISTENERS
for listener in MYSIGNAL_LISTENERS:
mysignal.connect_via(app)(listener)
您甚至可以将此模式抽象为一个函数:
from werkzeug.utils import import_string
DEFAULT_SIGNALS = ('MYSIGNAL', 'MYOTHERSIGNAL')
def register_blueprint_and_signals(app, bp_path, signals=DEFAULT_SIGNALS):
bp = import_string(bp_path)
app.register_blueprint(bp)
signal_path = bp_path.rsplit(".", 1)[0] + "."
for signal in signals:
try:
listeners = import_string(signal_path + signal + '_LISTENERS')
except ImportError:
continue
for listener in listeners:
signal.connect_via(app)(listener)
然后你可以像这样使用它:
app = Flask(__name__)
register_blueprint_and_signals(app, "my_module.my_blueprint")
register_blueprint_and_signals(app, "another.blueprint", ['ANOTHERSIGNAL'])