first commit

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
kunkunxiaomaoandClaude committed 2026-11-16 20:45:22 +08:00
commit 9c5fef1a4d
412 files changed
+94582

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
"""Plugin system: base types, manager, generator, optional market client."""
from pixiv_app.core.plugin.base import (
BasePlugin,
PluginAuthError,
PluginDownloadError,
PluginError,
PluginManifest,
PluginParseError,
Resource,
)
from pixiv_app.core.plugin.manager import PluginEvent, PluginManager
__all__ = [
"BasePlugin",
"PluginAuthError",
"PluginDownloadError",
"PluginError",
"PluginEvent",
"PluginManager",
"PluginManifest",
"PluginParseError",
"Resource",
]
+119
View File
@@ -0,0 +1,119 @@
"""
Multi-site plugin core abstractions.
解析与下载采用同步接口,便于与现有 requests / 线程模型集成;
若站点 SDK 仅提供 asyncio,可在插件内部使用 asyncio.run 包装。
"""
from __future__ import annotations
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
@dataclass
class Resource:
"""Unified resource model returned by plugins."""
id: str
url: str
title: str
author: str
author_id: str
files: list[dict[str, Any]]
metadata: dict[str, Any] = field(default_factory=dict)
thumbnail: str | None = None
created_at: str | None = None
@dataclass
class PluginManifest:
"""Optional manifest shipped as manifest.json next to plugin.py."""
name: str
version: str
domain: str
author: str = ""
description: str = ""
requires_auth: bool = False
min_platform_version: str = "1.0.0"
tags: list[str] = field(default_factory=list)
dependencies: list[str] = field(default_factory=list)
class PluginError(Exception):
"""Base class for plugin failures."""
class PluginAuthError(PluginError):
"""Authentication or authorization failed."""
class PluginParseError(PluginError):
"""URL or API response could not be parsed."""
class PluginDownloadError(PluginError):
"""Download or filesystem error."""
class BasePlugin(ABC):
"""Interface implemented by every site plugin."""
@property
@abstractmethod
def domain(self) -> str:
"""Primary domain, e.g. pixiv.net."""
@property
@abstractmethod
def name(self) -> str:
"""Human-readable plugin name."""
@property
@abstractmethod
def version(self) -> str:
"""Semantic version string."""
@abstractmethod
def can_handle(self, url: str) -> bool:
"""Return True if this plugin should process the URL or raw token."""
@abstractmethod
def parse(self, url: str) -> list[Resource]:
"""Resolve URL into one or more downloadable resources."""
@abstractmethod
def download(self, resource: Resource, save_path: Path) -> list[Path]:
"""Download files for ``resource`` under ``save_path``."""
@abstractmethod
def get_headers(self) -> dict[str, str]:
"""HTTP headers for requests (Cookie, Bearer token, etc.)."""
def validate(self) -> bool:
"""Optional self-check (credentials present, deps installed)."""
return True
def get_config_schema(self) -> dict[str, Any]:
"""Optional JSON-schema-like dict for dynamic GUI forms."""
return {}
def load_config(self) -> dict[str, Any]:
"""Override to load persisted plugin settings."""
return {}
def save_config(self, data: dict[str, Any]) -> None:
"""Override to persist plugin settings."""
__all__ = [
"BasePlugin",
"PluginAuthError",
"PluginDownloadError",
"PluginError",
"PluginManifest",
"PluginParseError",
"Resource",
]
+69
View File
@@ -0,0 +1,69 @@
from __future__ import annotations
import re
from datetime import datetime
from pathlib import Path
from typing import Any
from jinja2 import Environment, FileSystemLoader, select_autoescape
def _class_name_from_plugin_name(name: str) -> str:
parts = re.split(r"[\s_]+", name.strip())
stem = "".join(p[:1].upper() + p[1:].lower() if p else "" for p in parts if p)
return f"{stem}Plugin" if stem else "GeneratedPlugin"
class PluginGenerator:
"""Generate a skeleton plugin directory from Jinja2 templates."""
def __init__(self, template_dir: str | Path | None = None) -> None:
base = Path(__file__).resolve().parent / "templates"
self.template_dir = Path(template_dir) if template_dir else base
self.env = Environment(
loader=FileSystemLoader(str(self.template_dir)),
autoescape=select_autoescape(enabled_extensions=()),
)
def generate(
self,
name: str,
domain: str,
author: str,
description: str,
*,
output_root: str | Path = "plugins",
) -> Path:
slug = name.strip().lower().replace(" ", "_")
plugin_dir = Path(output_root) / slug
plugin_dir.mkdir(parents=True, exist_ok=True)
context: dict[str, Any] = {
"name": name.strip(),
"domain": domain.strip(),
"author": author.strip(),
"description": description.strip(),
"generated_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"class_name": _class_name_from_plugin_name(name),
}
py_template = self.env.get_template("base_plugin.py.jinja")
(plugin_dir / "plugin.py").write_text(py_template.render(**context), encoding="utf-8")
manifest_template = self.env.get_template("manifest.json.jinja")
(plugin_dir / "manifest.json").write_text(manifest_template.render(**context), encoding="utf-8")
init_file = plugin_dir / "__init__.py"
if not init_file.exists():
init_file.write_text('"""Generated plugin."""\n', encoding="utf-8")
return plugin_dir.resolve()
def generate_from_gui(self, form_data: dict[str, str], *, output_root: str | Path = "plugins") -> Path:
return self.generate(
name=form_data["name"],
domain=form_data["domain"],
author=form_data["author"],
description=form_data["description"],
output_root=output_root,
)
+194
View File
@@ -0,0 +1,194 @@
from __future__ import annotations
import importlib.util
import json
import logging
import sys
from collections import defaultdict
from enum import Enum
from pathlib import Path
from typing import Any, Callable
from pixiv_app.core.plugin.base import BasePlugin, PluginManifest
logger = logging.getLogger(__name__)
class PluginEvent(str, Enum):
BEFORE_LOAD = "before_load"
AFTER_LOAD = "after_load"
BEFORE_UNLOAD = "before_unload"
AFTER_UNLOAD = "after_unload"
ERROR = "error"
class PluginManager:
"""Discover and load plugins from filesystem directories."""
def __init__(self, plugin_dirs: list[str | Path]) -> None:
self.plugin_dirs = [Path(d).resolve() for d in plugin_dirs]
self.plugins: dict[str, BasePlugin] = {}
self.manifests: dict[str, PluginManifest] = {}
self.loaded_modules: dict[str, Any] = {}
self._paths: dict[str, Path] = {}
self.load_errors: dict[Path, str] = {}
self.event_handlers: dict[PluginEvent, list[Callable[..., None]]] = defaultdict(list)
def on_event(self, event: PluginEvent, callback: Callable[..., None]) -> None:
self.event_handlers[event].append(callback)
def _emit(self, event: PluginEvent, *args: Any, **kwargs: Any) -> None:
for cb in self.event_handlers.get(event, []):
try:
cb(*args, **kwargs)
except Exception as exc:
logger.exception("Plugin event handler failed: %s", exc)
def discover(self) -> list[Path]:
"""Return paths to plugin.py files."""
found: list[Path] = []
for root in self.plugin_dirs:
if not root.is_dir():
continue
for subdir in sorted(root.iterdir()):
if not subdir.is_dir():
continue
candidate = subdir / "plugin.py"
if candidate.is_file():
found.append(candidate)
return found
def _load_manifest(self, plugin_dir: Path) -> PluginManifest | None:
manifest_path = plugin_dir / "manifest.json"
if not manifest_path.is_file():
return None
try:
data = json.loads(manifest_path.read_text(encoding="utf-8"))
return PluginManifest(
name=str(data.get("name", plugin_dir.name)),
version=str(data.get("version", "0.0.0")),
domain=str(data.get("domain", "")),
author=str(data.get("author", "")),
description=str(data.get("description", "")),
requires_auth=bool(data.get("requires_auth", False)),
min_platform_version=str(data.get("min_platform_version", "1.0.0")),
tags=list(data.get("tags") or []),
dependencies=list(data.get("dependencies") or []),
)
except Exception as exc:
logger.warning("Invalid manifest %s: %s", manifest_path, exc)
return None
def load_plugin(self, plugin_path: Path) -> BasePlugin | None:
"""Load a single plugin from ``.../plugin.py``."""
plugin_path = plugin_path.resolve()
plugin_dir = plugin_path.parent
module_name = f"sakura_dynamic_plugin_{plugin_dir.name}"
module_stem = module_name.rsplit(".", 1)[0]
for loaded_name in list(sys.modules):
if loaded_name == module_name or loaded_name.startswith(f"{module_name}."):
del sys.modules[loaded_name]
self.load_errors.pop(plugin_path, None)
self._emit(PluginEvent.BEFORE_LOAD, plugin_path)
spec = importlib.util.spec_from_file_location(
module_name,
plugin_path,
submodule_search_locations=[str(plugin_dir)],
)
if spec is None or spec.loader is None:
self._emit(PluginEvent.ERROR, plugin_path, "invalid spec")
self.load_errors[plugin_path] = "invalid spec"
return None
module = importlib.util.module_from_spec(spec)
module.__package__ = module_stem
module.__file__ = str(plugin_path)
sys.modules[module_name] = module
try:
spec.loader.exec_module(module)
except Exception as exc:
logger.exception("Failed to load %s", plugin_path)
self._emit(PluginEvent.ERROR, plugin_path, exc)
self.load_errors[plugin_path] = f"{type(exc).__name__}: {exc}"
return None
plugin_class = getattr(module, "plugin_class", None)
if plugin_class is None:
logger.error("Module %s has no plugin_class export", plugin_path)
self._emit(PluginEvent.ERROR, plugin_path, "missing plugin_class")
self.load_errors[plugin_path] = "missing plugin_class"
return None
try:
plugin = plugin_class()
except Exception as exc:
logger.exception("Failed to instantiate plugin from %s", plugin_path)
self._emit(PluginEvent.ERROR, plugin_path, exc)
self.load_errors[plugin_path] = f"{type(exc).__name__}: {exc}"
return None
if not isinstance(plugin, BasePlugin):
logger.error("plugin_class is not BasePlugin: %s", plugin_path)
self.load_errors[plugin_path] = "plugin_class is not BasePlugin"
return None
key = plugin.name
manifest = self._load_manifest(plugin_dir)
if manifest:
self.manifests[key] = manifest
self.plugins[key] = plugin
self.loaded_modules[key] = module
self._paths[key] = plugin_path
self.load_errors.pop(plugin_path, None)
self._emit(PluginEvent.AFTER_LOAD, plugin)
return plugin
def load_all(self) -> dict[str, BasePlugin]:
for path in self.discover():
try:
self.load_plugin(path)
except Exception as exc:
logger.exception("Failed during load_all %s: %s", path, exc)
return self.plugins
def reload_all_from_disk(self) -> None:
"""Unload every plugin entry then scan directories again."""
for name in list(self.plugins.keys()):
self.unload_plugin(name)
self.load_errors.clear()
self.load_all()
def reload_plugin(self, name: str) -> bool:
path = self._paths.get(name)
if path is None:
return False
self.unload_plugin(name)
return self.load_plugin(path) is not None
def get_plugin_path(self, name: str) -> Path | None:
return self._paths.get(name)
def unload_plugin(self, name: str) -> None:
self._emit(PluginEvent.BEFORE_UNLOAD, name)
self.plugins.pop(name, None)
mod = self.loaded_modules.pop(name, None)
self.manifests.pop(name, None)
self._paths.pop(name, None)
if mod is not None and getattr(mod, "__name__", None) in sys.modules:
del sys.modules[mod.__name__]
self._emit(PluginEvent.AFTER_UNLOAD, name)
def get_plugin_for_url(self, url: str) -> BasePlugin | None:
for plugin in self.plugins.values():
if plugin.can_handle(url):
return plugin
return None
def check_dependencies(self, manifest: PluginManifest) -> bool:
for dep in manifest.dependencies:
if dep not in self.plugins:
return False
return True
+29
View File
@@ -0,0 +1,29 @@
"""
Optional plugin market client — stub for future HTTP catalog integration.
"""
from __future__ import annotations
from typing import Any
import requests
class PluginMarketClient:
def __init__(self, api_url: str = "") -> None:
self.api_url = api_url.rstrip("/")
def list_plugins(self) -> list[dict[str, Any]]:
if not self.api_url:
return []
response = requests.get(f"{self.api_url}/plugins", timeout=15)
response.raise_for_status()
data = response.json()
return list(data.get("plugins", []))
def install_plugin(self, _plugin_name: str) -> bool:
"""Download zip and extract into plugins/ — not implemented."""
return False
def check_updates(self) -> list[dict[str, Any]]:
return []
@@ -0,0 +1,31 @@
"""
{{ name }} plugin — auto-generated at {{ generated_at }}.
"""
from __future__ import annotations
from pathlib import Path
from typing import Any
from pixiv_app.core.plugin.base import BasePlugin, Resource
class {{ class_name }}(BasePlugin):
domain = "{{ domain }}"
name = "{{ name }}"
version = "1.0.0"
def can_handle(self, url: str) -> bool:
return "{{ domain }}" in url.lower()
def parse(self, url: str) -> list[Resource]:
raise NotImplementedError("Implement URL parsing and build Resource list.")
def download(self, resource: Resource, save_path: Path) -> list[Path]:
raise NotImplementedError("Download files for resource into save_path.")
def get_headers(self) -> dict[str, str]:
return {"User-Agent": "Mozilla/5.0", "Accept": "*/*"}
plugin_class = {{ class_name }}
@@ -0,0 +1,11 @@
{
"name": "{{ name }}",
"version": "1.0.0",
"domain": "{{ domain }}",
"author": "{{ author }}",
"description": "{{ description }}",
"requires_auth": false,
"tags": ["general"],
"min_platform_version": "1.0.0",
"dependencies": []
}