From 41da9b62c2e99896504f5925e106ecf68dbe8bbb Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 14 May 2026 23:04:14 +0800 Subject: [PATCH] fix: refresh custom placeholders without restart Closes #5770 --- app/core/meta/customization.py | 27 +++++++++++++++++++-------- tests/test_customization_matcher.py | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 tests/test_customization_matcher.py diff --git a/app/core/meta/customization.py b/app/core/meta/customization.py index 15abbba3..9cdacc8d 100644 --- a/app/core/meta/customization.py +++ b/app/core/meta/customization.py @@ -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) # 处理重复多次的情况,保留先后顺序(按添加自定义占位符的顺序) diff --git a/tests/test_customization_matcher.py b/tests/test_customization_matcher.py new file mode 100644 index 00000000..19e97301 --- /dev/null +++ b/tests/test_customization_matcher.py @@ -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")