mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-10 15:56:03 +00:00
refactor: 重构插件系统
This commit is contained in:
83
pkg/plugin/setting.py
Normal file
83
pkg/plugin/setting.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from ..core import app
|
||||
from ..config import manager as cfg_mgr
|
||||
from . import context
|
||||
|
||||
|
||||
class SettingManager:
|
||||
|
||||
ap: app.Application
|
||||
|
||||
settings: cfg_mgr.ConfigManager
|
||||
|
||||
def __init__(self, ap: app.Application):
|
||||
self.ap = ap
|
||||
|
||||
async def initialize(self):
|
||||
self.settings = await cfg_mgr.load_json_config(
|
||||
'plugins/plugins.json',
|
||||
'res/templates/plugin-setting-template.json'
|
||||
)
|
||||
|
||||
async def sync_setting(
|
||||
self,
|
||||
plugin_containers: list[context.RuntimeContainer],
|
||||
):
|
||||
"""同步设置
|
||||
"""
|
||||
|
||||
not_matched_source_record = []
|
||||
|
||||
for value in self.settings.data['plugins']:
|
||||
|
||||
if 'name' not in value: # 只有远程地址的,应用到pkg_path相同的插件容器上
|
||||
matched = False
|
||||
|
||||
for plugin_container in plugin_containers:
|
||||
if plugin_container.pkg_path == value['pkg_path']:
|
||||
matched = True
|
||||
|
||||
plugin_container.plugin_source = value['source']
|
||||
break
|
||||
|
||||
if not matched:
|
||||
not_matched_source_record.append(value)
|
||||
else: # 正常的插件设置
|
||||
for plugin_container in plugin_containers:
|
||||
if plugin_container.plugin_name == value['name']:
|
||||
plugin_container.set_from_setting_dict(value)
|
||||
|
||||
self.settings.data = {
|
||||
'plugins': [
|
||||
p.to_setting_dict()
|
||||
for p in plugin_containers
|
||||
]
|
||||
}
|
||||
|
||||
self.settings.data['plugins'].extend(not_matched_source_record)
|
||||
|
||||
await self.settings.dump_config()
|
||||
|
||||
async def record_installed_plugin_source(
|
||||
self,
|
||||
pkg_path: str,
|
||||
source: str
|
||||
):
|
||||
found = False
|
||||
|
||||
for value in self.settings.data['plugins']:
|
||||
if value['pkg_path'] == pkg_path:
|
||||
value['source'] = source
|
||||
found = True
|
||||
break
|
||||
|
||||
if not found:
|
||||
|
||||
self.settings.data['plugins'].append(
|
||||
{
|
||||
'pkg_path': pkg_path,
|
||||
'source': source
|
||||
}
|
||||
)
|
||||
await self.settings.dump_config()
|
||||
Reference in New Issue
Block a user