mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-12-02 00:46:27 +08:00
fix(build): preserve Field description when generating PLUGIN_MARKET default
This commit is contained in:
@@ -72,9 +72,25 @@ def _extract_plugin_market_repos_from_wiki(markdown: str) -> list[str]:
|
||||
return repos
|
||||
|
||||
|
||||
def _find_plugin_market_assignment(source: str) -> tuple[int, int, str]:
|
||||
def _extract_field_meta(value: Optional[ast.expr]) -> Optional[tuple[str, str]]:
|
||||
"""
|
||||
定位 ConfigModel 中插件市场默认值对应的源码范围。
|
||||
从现有的 Field(...) / pydantic.Field(...) 声明中提取调用表达式与 description 文本,普通字面量声明返回 None。
|
||||
"""
|
||||
if not isinstance(value, ast.Call):
|
||||
return None
|
||||
func = value.func
|
||||
func_name = func.id if isinstance(func, ast.Name) else func.attr if isinstance(func, ast.Attribute) else None
|
||||
if func_name != "Field":
|
||||
return None
|
||||
for kw in value.keywords:
|
||||
if kw.arg == "description" and isinstance(kw.value, ast.Constant) and isinstance(kw.value.value, str):
|
||||
return ast.unparse(func), kw.value.value
|
||||
return None
|
||||
|
||||
|
||||
def _find_plugin_market_assignment(source: str) -> tuple[int, int, str, Optional[tuple[str, str]]]:
|
||||
"""
|
||||
定位 ConfigModel 中插件市场默认值对应的源码范围与既有 Field 调用信息。
|
||||
"""
|
||||
tree = ast.parse(source)
|
||||
source_lines = source.splitlines(keepends=True)
|
||||
@@ -91,14 +107,31 @@ def _find_plugin_market_assignment(source: str) -> tuple[int, int, str]:
|
||||
start = item.lineno - 1
|
||||
indent_size = len(source_lines[start]) - len(source_lines[start].lstrip())
|
||||
indent = source_lines[start][:indent_size]
|
||||
return start, item.end_lineno, indent
|
||||
return start, item.end_lineno, indent, _extract_field_meta(item.value)
|
||||
raise ValueError("未在 ConfigModel 中找到 PLUGIN_MARKET 默认值")
|
||||
|
||||
|
||||
def _format_plugin_market_assignment(repos: list[str], indent: str) -> str:
|
||||
def _format_plugin_market_assignment(
|
||||
repos: list[str],
|
||||
indent: str,
|
||||
field_meta: Optional[tuple[str, str]] = None,
|
||||
) -> str:
|
||||
"""
|
||||
按配置文件现有缩进格式生成插件市场默认值源码。
|
||||
按配置文件现有缩进格式生成插件市场默认值源码;原有 Field description 时保留原调用形式与描述。
|
||||
"""
|
||||
if field_meta is not None:
|
||||
field_callable, description = field_meta
|
||||
lines = [
|
||||
f"{indent}PLUGIN_MARKET: str = {field_callable}(\n",
|
||||
f"{indent} default=(\n",
|
||||
]
|
||||
for index, repo in enumerate(repos):
|
||||
suffix = "," if index < len(repos) - 1 else ""
|
||||
lines.append(f'{indent} "{repo}{suffix}"\n')
|
||||
lines.append(f"{indent} ),\n")
|
||||
lines.append(f"{indent} description={description!r},\n")
|
||||
lines.append(f"{indent})\n")
|
||||
return "".join(lines)
|
||||
lines = [f"{indent}PLUGIN_MARKET: str = (\n"]
|
||||
for index, repo in enumerate(repos):
|
||||
suffix = "," if index < len(repos) - 1 else ""
|
||||
@@ -119,9 +152,9 @@ def _generate_plugin_market_default(wiki_file: Path, config_file: Path) -> list[
|
||||
raise ValueError("Wiki 插件仓库清单缺少 MoviePilot 官方插件仓库")
|
||||
|
||||
source = config_file.read_text(encoding="utf-8")
|
||||
start, end, indent = _find_plugin_market_assignment(source)
|
||||
start, end, indent, field_meta = _find_plugin_market_assignment(source)
|
||||
source_lines = source.splitlines(keepends=True)
|
||||
replacement = _format_plugin_market_assignment(repos, indent)
|
||||
replacement = _format_plugin_market_assignment(repos, indent, field_meta=field_meta)
|
||||
updated_source = (
|
||||
"".join(source_lines[:start])
|
||||
+ replacement
|
||||
|
||||
@@ -192,3 +192,69 @@ def test_release_workflows_use_canonical_config_path() -> None:
|
||||
assert "--config-file app/runtime/config.py" in beta_workflow
|
||||
assert "app/core/config.py" not in build_workflow
|
||||
assert "app/core/config.py" not in beta_workflow
|
||||
|
||||
|
||||
def test_generate_plugin_market_default_preserves_field_description_and_settings_contract(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""
|
||||
ConfigModel 使用 Field(description=...) 声明时,生成默认值应保留 Field 描述并满足系统设置目录契约。
|
||||
"""
|
||||
wiki_file = tmp_path / "plugin.md"
|
||||
wiki_file.write_text(
|
||||
f"""
|
||||
<!-- plugin-market-repos:start -->
|
||||
- {OFFICIAL_PLUGIN_MARKET}
|
||||
- https://github.com/demo/MoviePilot-Plugins
|
||||
<!-- plugin-market-repos:end -->
|
||||
""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
config_file = tmp_path / "config.py"
|
||||
source_config = (ROOT_DIR / "app" / "runtime" / "config.py").read_text(encoding="utf-8")
|
||||
config_file.write_text(source_config, encoding="utf-8")
|
||||
|
||||
repos = _generate_plugin_market_default(wiki_file, config_file)
|
||||
first_result = config_file.read_text(encoding="utf-8")
|
||||
_generate_plugin_market_default(wiki_file, config_file)
|
||||
assert config_file.read_text(encoding="utf-8") == first_result
|
||||
|
||||
tree = ast.parse(first_result)
|
||||
plugin_market_value = None
|
||||
for node in tree.body:
|
||||
if isinstance(node, ast.ClassDef) and node.name == "ConfigModel":
|
||||
for item in node.body:
|
||||
if isinstance(item, ast.AnnAssign) and isinstance(item.target, ast.Name) and item.target.id == "PLUGIN_MARKET":
|
||||
plugin_market_value = item.value
|
||||
break
|
||||
assert isinstance(plugin_market_value, ast.Call)
|
||||
kwargs = {kw.arg: ast.literal_eval(kw.value) for kw in plugin_market_value.keywords if kw.arg}
|
||||
assert kwargs["default"] == ",".join(repos)
|
||||
assert kwargs["description"] == "插件市场仓库地址,多个地址使用,分隔,地址以/结尾"
|
||||
|
||||
|
||||
def test_generate_plugin_market_default_preserves_qualified_field_callable(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""
|
||||
ConfigModel 使用 pydantic.Field(...) 限定调用时,生成结果应保留原限定调用形式。
|
||||
"""
|
||||
wiki_file = tmp_path / "plugin.md"
|
||||
wiki_file.write_text(
|
||||
f"""
|
||||
<!-- plugin-market-repos:start -->
|
||||
- {OFFICIAL_PLUGIN_MARKET}
|
||||
<!-- plugin-market-repos:end -->
|
||||
""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
config_file = tmp_path / "config.py"
|
||||
config_file.write_text(
|
||||
'import pydantic\n\nclass ConfigModel:\n PLUGIN_MARKET: str = pydantic.Field(default="old", description="插件市场")\n',
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
_generate_plugin_market_default(wiki_file, config_file)
|
||||
rendered = config_file.read_text(encoding="utf-8")
|
||||
assert "PLUGIN_MARKET: str = pydantic.Field(" in rendered
|
||||
assert "description='插件市场'" in rendered
|
||||
|
||||
Reference in New Issue
Block a user