fix: refresh custom placeholders without restart

Closes #5770
This commit is contained in:
jxxghp
2026-05-14 23:04:14 +08:00
parent 9128955bf9
commit 41da9b62c2
2 changed files with 39 additions and 8 deletions

View File

@@ -15,6 +15,17 @@ class CustomizationMatcher(metaclass=Singleton):
self.customization = None
self.custom_separator = None
@staticmethod
def _normalize_customization(customization):
"""
规范化自定义占位符配置,兼容历史字符串与列表两种保存格式。
"""
if isinstance(customization, str):
customization = customization.replace("\n", ";").replace("|", ";").strip(";").split(";")
if not customization:
return []
return list(filter(None, customization))
def match(self, title=None):
"""
:param title: 资源标题或文件名
@@ -22,14 +33,14 @@ class CustomizationMatcher(metaclass=Singleton):
"""
if not title:
return ""
if not self.customization:
# 自定义占位符
customization = self.systemconfig.get(SystemConfigKey.Customization)
if not customization:
return ""
if isinstance(customization, str):
customization = customization.replace("\n", ";").replace("|", ";").strip(";").split(";")
self.customization = "|".join([f"({item})" for item in customization])
# 自定义占位符需要跟随系统配置实时生效,避免单例缓存导致保存后仍沿用旧规则。
customization = self._normalize_customization(
self.systemconfig.get(SystemConfigKey.Customization)
)
if not customization:
self.customization = None
return ""
self.customization = "|".join([f"({item})" for item in customization])
customization_re = re.compile(r"%s" % self.customization)
# 处理重复多次的情况,保留先后顺序(按添加自定义占位符的顺序)

View File

@@ -0,0 +1,20 @@
from unittest import TestCase
from unittest.mock import patch
from app.core.meta.customization import CustomizationMatcher
class CustomizationMatcherTest(TestCase):
def test_match_uses_latest_customization_setting(self):
"""自定义占位符修改后,下一次识别应直接使用新配置。"""
matcher = CustomizationMatcher()
values = [["GROUP"], ["TEAM"]]
with patch.object(
matcher.systemconfig,
"get",
side_effect=lambda _: values[0],
):
self.assertEqual(matcher.match("[GROUP][TEAM] Movie"), "GROUP")
values[0] = ["TEAM"]
self.assertEqual(matcher.match("[GROUP][TEAM] Movie"), "TEAM")