Skip to content

Commit 41da9b6

Browse files
committed
fix: refresh custom placeholders without restart
Closes #5770
1 parent 9128955 commit 41da9b6

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

app/core/meta/customization.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,32 @@ def __init__(self):
1515
self.customization = None
1616
self.custom_separator = None
1717

18+
@staticmethod
19+
def _normalize_customization(customization):
20+
"""
21+
规范化自定义占位符配置,兼容历史字符串与列表两种保存格式。
22+
"""
23+
if isinstance(customization, str):
24+
customization = customization.replace("\n", ";").replace("|", ";").strip(";").split(";")
25+
if not customization:
26+
return []
27+
return list(filter(None, customization))
28+
1829
def match(self, title=None):
1930
"""
2031
:param title: 资源标题或文件名
2132
:return: 匹配结果
2233
"""
2334
if not title:
2435
return ""
25-
if not self.customization:
26-
# 自定义占位符
27-
customization = self.systemconfig.get(SystemConfigKey.Customization)
28-
if not customization:
29-
return ""
30-
if isinstance(customization, str):
31-
customization = customization.replace("\n", ";").replace("|", ";").strip(";").split(";")
32-
self.customization = "|".join([f"({item})" for item in customization])
36+
# 自定义占位符需要跟随系统配置实时生效,避免单例缓存导致保存后仍沿用旧规则。
37+
customization = self._normalize_customization(
38+
self.systemconfig.get(SystemConfigKey.Customization)
39+
)
40+
if not customization:
41+
self.customization = None
42+
return ""
43+
self.customization = "|".join([f"({item})" for item in customization])
3344

3445
customization_re = re.compile(r"%s" % self.customization)
3546
# 处理重复多次的情况,保留先后顺序(按添加自定义占位符的顺序)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch
3+
4+
from app.core.meta.customization import CustomizationMatcher
5+
6+
7+
class CustomizationMatcherTest(TestCase):
8+
def test_match_uses_latest_customization_setting(self):
9+
"""自定义占位符修改后,下一次识别应直接使用新配置。"""
10+
matcher = CustomizationMatcher()
11+
values = [["GROUP"], ["TEAM"]]
12+
13+
with patch.object(
14+
matcher.systemconfig,
15+
"get",
16+
side_effect=lambda _: values[0],
17+
):
18+
self.assertEqual(matcher.match("[GROUP][TEAM] Movie"), "GROUP")
19+
values[0] = ["TEAM"]
20+
self.assertEqual(matcher.match("[GROUP][TEAM] Movie"), "TEAM")

0 commit comments

Comments
 (0)