mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-08-28 19:47:41 +08:00
@@ -15,6 +15,17 @@ class CustomizationMatcher(metaclass=Singleton):
|
|||||||
self.customization = None
|
self.customization = None
|
||||||
self.custom_separator = 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):
|
def match(self, title=None):
|
||||||
"""
|
"""
|
||||||
:param title: 资源标题或文件名
|
:param title: 资源标题或文件名
|
||||||
@@ -22,14 +33,14 @@ class CustomizationMatcher(metaclass=Singleton):
|
|||||||
"""
|
"""
|
||||||
if not title:
|
if not title:
|
||||||
return ""
|
return ""
|
||||||
if not self.customization:
|
# 自定义占位符需要跟随系统配置实时生效,避免单例缓存导致保存后仍沿用旧规则。
|
||||||
# 自定义占位符
|
customization = self._normalize_customization(
|
||||||
customization = self.systemconfig.get(SystemConfigKey.Customization)
|
self.systemconfig.get(SystemConfigKey.Customization)
|
||||||
if not customization:
|
)
|
||||||
return ""
|
if not customization:
|
||||||
if isinstance(customization, str):
|
self.customization = None
|
||||||
customization = customization.replace("\n", ";").replace("|", ";").strip(";").split(";")
|
return ""
|
||||||
self.customization = "|".join([f"({item})" for item in customization])
|
self.customization = "|".join([f"({item})" for item in customization])
|
||||||
|
|
||||||
customization_re = re.compile(r"%s" % self.customization)
|
customization_re = re.compile(r"%s" % self.customization)
|
||||||
# 处理重复多次的情况,保留先后顺序(按添加自定义占位符的顺序)
|
# 处理重复多次的情况,保留先后顺序(按添加自定义占位符的顺序)
|
||||||
|
|||||||
@@ -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")
|
||||||
Reference in New Issue
Block a user