2026-07-07 00:38:01 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
import types
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def install_runtime_stubs() -> None:
|
|
|
|
|
"""Install tiny stubs for optional runtime modules used by service imports."""
|
|
|
|
|
|
|
|
|
|
if "astrbot.api" not in sys.modules:
|
|
|
|
|
logger = types.SimpleNamespace(
|
|
|
|
|
info=lambda *args, **kwargs: None,
|
|
|
|
|
warning=lambda *args, **kwargs: None,
|
|
|
|
|
error=lambda *args, **kwargs: None,
|
|
|
|
|
exception=lambda *args, **kwargs: None,
|
|
|
|
|
)
|
|
|
|
|
astrbot = types.ModuleType("astrbot")
|
|
|
|
|
api = types.ModuleType("astrbot.api")
|
|
|
|
|
api.logger = logger
|
|
|
|
|
sys.modules["astrbot"] = astrbot
|
|
|
|
|
sys.modules["astrbot.api"] = api
|
|
|
|
|
|
|
|
|
|
if "yaml" not in sys.modules:
|
|
|
|
|
yaml = types.ModuleType("yaml")
|
|
|
|
|
yaml.safe_dump = lambda *args, **kwargs: ""
|
|
|
|
|
sys.modules["yaml"] = yaml
|
2026-07-07 00:40:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def install_httpx_stub(status_code: int = 202, text: str = "") -> types.ModuleType:
|
|
|
|
|
httpx = types.ModuleType("httpx")
|
|
|
|
|
httpx.calls = []
|
|
|
|
|
|
|
|
|
|
class Response:
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
|
self.status_code = status_code
|
|
|
|
|
self.text = text
|
|
|
|
|
|
|
|
|
|
class AsyncClient:
|
|
|
|
|
def __init__(self, *args, **kwargs) -> None:
|
|
|
|
|
self.args = args
|
|
|
|
|
self.kwargs = kwargs
|
|
|
|
|
|
|
|
|
|
async def __aenter__(self):
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
async def __aexit__(self, exc_type, exc, tb) -> None:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
async def post(self, url, headers=None, auth=None):
|
|
|
|
|
httpx.calls.append({"url": url, "headers": headers or {}, "auth": auth})
|
|
|
|
|
return Response()
|
|
|
|
|
|
|
|
|
|
httpx.AsyncClient = AsyncClient
|
|
|
|
|
sys.modules["httpx"] = httpx
|
|
|
|
|
return httpx
|