Files
37duAutomation/desktop/electron-shell/python/webhost/backend/plugins/base.py
T
puddy 008f6f5998 refactor: 重构执行机架构为 Electron 壳内置 Python 运行时子集
将原 webhost 底座的后端、前端、工具等代码迁移至 electron-shell 目录,构建自包含的 Python 环境包,移除冗余的前端代码、旧的后端模块及相关依赖,更新版本号至 1.0.6,优化平台登录态预检逻辑,统一代码结构为单一真源,避免多副本维护问题。
2026-10-05 14:13:47 +08:00

74 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""场景插件抽象:后端 `run` 承载真实逻辑(浏览器自动化/登录态等),
前端按 `fields` 渲染通用表单,执行过程经 `RunContext.log/progress` 推送到 WebSocket。
"""
from __future__ import annotations
from typing import List
class RunContext:
"""传给插件 `run` 的执行上下文:把日志/进度实时推给前端,并提供共享登录态访问。"""
def __init__(self, hub, run_id: str, plugin_id: str, auth=None):
self._hub = hub
self._auth = auth # webhost.backend.auth 模块(宿主注入的共享登录态)
self.run_id = run_id
self.plugin_id = plugin_id
def log(self, msg: str, level: str = "INFO"):
self._hub.publish({
"t": "log", "run_id": self.run_id, "plugin_id": self.plugin_id,
"level": level, "msg": msg,
})
def progress(self, pct: int):
self._hub.publish({
"t": "progress", "run_id": self.run_id,
"plugin_id": self.plugin_id, "pct": int(max(0, min(100, pct))),
})
# ---------- 共享登录态 ----------
def get_session(self, platform: str) -> "requests.Session":
"""获取指定平台的共享登录 session,可直接传给 shared 数据模块。"""
if not self._auth:
raise RuntimeError("宿主未注入登录态服务,无法构建 session")
return self._auth.build_session(platform)
def get_user_id(self, platform: str) -> str:
"""获取指定平台的用户 ID(HR 数据接口大多需要 user_id 参数)。"""
if not self._auth:
raise RuntimeError("宿主未注入登录态服务,无法获取 user_id")
return self._auth.get_user_id(platform)
class ScenePlugin:
"""场景插件基类。新增场景:子类填 id/name/fields/run 并注册到 plugins 列表。"""
id: str = ""
name: str = ""
version: str = "1.0.0"
description: str = ""
platform: str = "" # 所需平台登录态:hr / oa / ""(无需登录)
uses_month: bool = False # 是否需要底座页眉的“执行月份”(前端据此注入 ref_month 字段)
# 表单字段定义:[{key,label,type,default,options}]
# type ∈ int | text | select
fields: List[dict] = []
# 声明式能力/权限(manifest 驱动):与插件包 plugin.json 保持一致;
# 缺省场景可用本地类补充,plugin.json 存在时以其为权威。
capabilities: List[str] = []
permissions: dict = {}
def run(self, fields: dict, ctx: RunContext) -> dict:
raise NotImplementedError(f"插件 {self.id} 未实现 run")
class _Registry(list):
def find(self, pid: str) -> ScenePlugin:
for p in self:
if p.id == pid:
return p
raise KeyError(pid)
PLUGINS: _Registry = _Registry()