Files
songforge/plugins/hooks.py
T
swlandCursor b1afe68f3c
selftest / selftest (push) Canceled after 0s
Fix opening filler via lean tags and intro QC gate.
Stop naming filler syllables in tags (they conditioned the model); default prefer_lean_tags; hard-fail intro_filler in QC; add plugins/ hook bus as thin microkernel extension point.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-10 12:49:25 +08:00

95 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""薄钩子总线:内核不知道插件细节,只按名字广播。
钩子约定(均可选):
before_build_tags kwargs: args, cfg, style, chill, instrumental, lean
after_assemble kwargs: tags, lyrics, args, cfg, style, lean
-> 可返回 {"tags":..., "lyrics":...} 覆盖
before_qc_judge kwargs: result, expect, soft -> 可追加 why 列表
after_qc_judge kwargs: verdict, why, result
插件目录:
plugins/builtin/*.py 内置,默认加载
plugins/<name>/plugin.py 第三方包式
plugins/<name>.py 单文件(非 hooks/__init__)
"""
import importlib
import os
import pkgutil
import sys
import traceback
_HERE = os.path.dirname(os.path.abspath(__file__))
_LOADED = False
class HookBus(object):
def __init__(self):
self._handlers = {}
def on(self, name):
def deco(fn):
self._handlers.setdefault(name, []).append(fn)
return fn
return deco
def emit(self, name, **kwargs):
last = None
for fn in self._handlers.get(name, []):
try:
ret = fn(**kwargs)
if ret is not None:
last = ret
except Exception:
traceback.print_exc()
return last
bus = HookBus()
def load_plugins(extra_dirs=None):
"""发现并 import 插件(幂等)。"""
global _LOADED
if _LOADED:
return
_LOADED = True
roots = [_HERE]
if extra_dirs:
roots.extend(extra_dirs)
# 保证 songforge.plugins 可被 import
parent = os.path.dirname(_HERE)
if parent not in sys.path:
sys.path.insert(0, parent)
# builtin
builtin = os.path.join(_HERE, "builtin")
if os.path.isdir(builtin):
for info in pkgutil.iter_modules([builtin]):
if info.name.startswith("_"):
continue
try:
importlib.import_module(
"songforge.plugins.builtin.%s" % info.name)
except Exception:
traceback.print_exc()
# 顶层单文件 / 子包 plugin.py
for name in sorted(os.listdir(_HERE)):
if name.startswith("_") or name in ("builtin", "hooks.py",
"__init__.py", "README.md"):
continue
path = os.path.join(_HERE, name)
try:
if name.endswith(".py"):
mod = "songforge.plugins.%s" % name[:-3]
importlib.import_module(mod)
elif os.path.isdir(path) and os.path.isfile(
os.path.join(path, "plugin.py")):
importlib.import_module(
"songforge.plugins.%s.plugin" % name)
except Exception:
traceback.print_exc()
# 首次 import hooks 时加载内置插件
load_plugins()