mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-04 21:06:03 +00:00
style: introduce ruff as linter and formatter (#1356)
* style: remove necessary imports * style: fix F841 * style: fix F401 * style: fix F811 * style: fix E402 * style: fix E721 * style: fix E722 * style: fix E722 * style: fix F541 * style: ruff format * style: all passed * style: add ruff in deps * style: more ignores in ruff.toml * style: add pre-commit
This commit is contained in:
committed by
GitHub
parent
09e70d70e9
commit
209f16af76
@@ -1,10 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import typing
|
||||
import traceback
|
||||
|
||||
import sqlalchemy
|
||||
import logging
|
||||
|
||||
from ..core import app, taskmgr
|
||||
from . import context, loader, events, installer, models
|
||||
@@ -28,28 +26,26 @@ class PluginManager:
|
||||
|
||||
def plugins(
|
||||
self,
|
||||
enabled: bool=None,
|
||||
status: context.RuntimeContainerStatus=None,
|
||||
enabled: bool = None,
|
||||
status: context.RuntimeContainerStatus = None,
|
||||
) -> list[context.RuntimeContainer]:
|
||||
"""获取插件列表
|
||||
"""
|
||||
"""获取插件列表"""
|
||||
plugins = self.plugin_containers
|
||||
|
||||
if enabled is not None:
|
||||
plugins = [plugin for plugin in plugins if plugin.enabled == enabled]
|
||||
|
||||
|
||||
if status is not None:
|
||||
plugins = [plugin for plugin in plugins if plugin.status == status]
|
||||
|
||||
return plugins
|
||||
|
||||
|
||||
def get_plugin(
|
||||
self,
|
||||
author: str,
|
||||
plugin_name: str,
|
||||
) -> context.RuntimeContainer:
|
||||
"""通过作者和插件名获取插件
|
||||
"""
|
||||
"""通过作者和插件名获取插件"""
|
||||
for plugin in self.plugins():
|
||||
if plugin.plugin_author == author and plugin.plugin_name == plugin_name:
|
||||
return plugin
|
||||
@@ -88,20 +84,24 @@ class PluginManager:
|
||||
self.ap.logger.debug(f'优先级排序后的插件列表 {self.plugin_containers}')
|
||||
|
||||
async def load_plugin_settings(
|
||||
self,
|
||||
plugin_containers: list[context.RuntimeContainer]
|
||||
self, plugin_containers: list[context.RuntimeContainer]
|
||||
):
|
||||
for plugin_container in plugin_containers:
|
||||
result = await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.select(persistence_plugin.PluginSetting) \
|
||||
.where(persistence_plugin.PluginSetting.plugin_author == plugin_container.plugin_author)
|
||||
.where(persistence_plugin.PluginSetting.plugin_name == plugin_container.plugin_name)
|
||||
sqlalchemy.select(persistence_plugin.PluginSetting)
|
||||
.where(
|
||||
persistence_plugin.PluginSetting.plugin_author
|
||||
== plugin_container.plugin_author
|
||||
)
|
||||
.where(
|
||||
persistence_plugin.PluginSetting.plugin_name
|
||||
== plugin_container.plugin_name
|
||||
)
|
||||
)
|
||||
|
||||
setting = result.first()
|
||||
|
||||
if setting is None:
|
||||
|
||||
new_setting_data = {
|
||||
'plugin_author': plugin_container.plugin_author,
|
||||
'plugin_name': plugin_container.plugin_name,
|
||||
@@ -111,7 +111,9 @@ class PluginManager:
|
||||
}
|
||||
|
||||
await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.insert(persistence_plugin.PluginSetting).values(**new_setting_data)
|
||||
sqlalchemy.insert(persistence_plugin.PluginSetting).values(
|
||||
**new_setting_data
|
||||
)
|
||||
)
|
||||
continue
|
||||
else:
|
||||
@@ -120,19 +122,23 @@ class PluginManager:
|
||||
plugin_container.plugin_config = setting.config
|
||||
|
||||
async def dump_plugin_container_setting(
|
||||
self,
|
||||
plugin_container: context.RuntimeContainer
|
||||
self, plugin_container: context.RuntimeContainer
|
||||
):
|
||||
"""保存单个插件容器的设置到数据库
|
||||
"""
|
||||
"""保存单个插件容器的设置到数据库"""
|
||||
await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.update(persistence_plugin.PluginSetting)
|
||||
.where(persistence_plugin.PluginSetting.plugin_author == plugin_container.plugin_author)
|
||||
.where(persistence_plugin.PluginSetting.plugin_name == plugin_container.plugin_name)
|
||||
.where(
|
||||
persistence_plugin.PluginSetting.plugin_author
|
||||
== plugin_container.plugin_author
|
||||
)
|
||||
.where(
|
||||
persistence_plugin.PluginSetting.plugin_name
|
||||
== plugin_container.plugin_name
|
||||
)
|
||||
.values(
|
||||
enabled=plugin_container.enabled,
|
||||
priority=plugin_container.priority,
|
||||
config=plugin_container.plugin_config
|
||||
config=plugin_container.plugin_config,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -160,13 +166,13 @@ class PluginManager:
|
||||
async def destroy_plugin(self, plugin: context.RuntimeContainer):
|
||||
if plugin.status != context.RuntimeContainerStatus.INITIALIZED:
|
||||
return
|
||||
|
||||
|
||||
self.ap.logger.debug(f'释放插件 {plugin.plugin_name}')
|
||||
plugin.plugin_inst.__del__()
|
||||
await plugin.plugin_inst.destroy()
|
||||
plugin.plugin_inst = None
|
||||
plugin.status = context.RuntimeContainerStatus.MOUNTED
|
||||
|
||||
|
||||
async def destroy_plugins(self):
|
||||
for plugin in self.plugins():
|
||||
if plugin.status != context.RuntimeContainerStatus.INITIALIZED:
|
||||
@@ -185,16 +191,15 @@ class PluginManager:
|
||||
plugin_source: str,
|
||||
task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder(),
|
||||
):
|
||||
"""安装插件
|
||||
"""
|
||||
"""安装插件"""
|
||||
await self.installer.install_plugin(plugin_source, task_context)
|
||||
|
||||
await self.ap.ctr_mgr.plugin.post_install_record(
|
||||
{
|
||||
"name": "unknown",
|
||||
"remote": plugin_source,
|
||||
"author": "unknown",
|
||||
"version": "HEAD"
|
||||
'name': 'unknown',
|
||||
'remote': plugin_source,
|
||||
'author': 'unknown',
|
||||
'version': 'HEAD',
|
||||
}
|
||||
)
|
||||
|
||||
@@ -206,8 +211,7 @@ class PluginManager:
|
||||
plugin_name: str,
|
||||
task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder(),
|
||||
):
|
||||
"""卸载插件
|
||||
"""
|
||||
"""卸载插件"""
|
||||
|
||||
plugin_container = self.get_plugin_by_name(plugin_name)
|
||||
|
||||
@@ -219,10 +223,10 @@ class PluginManager:
|
||||
|
||||
await self.ap.ctr_mgr.plugin.post_remove_record(
|
||||
{
|
||||
"name": plugin_name,
|
||||
"remote": plugin_container.plugin_repository,
|
||||
"author": plugin_container.plugin_author,
|
||||
"version": plugin_container.plugin_version
|
||||
'name': plugin_name,
|
||||
'remote': plugin_container.plugin_repository,
|
||||
'author': plugin_container.plugin_author,
|
||||
'version': plugin_container.plugin_version,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -232,80 +236,82 @@ class PluginManager:
|
||||
async def update_plugin(
|
||||
self,
|
||||
plugin_name: str,
|
||||
plugin_source: str=None,
|
||||
plugin_source: str = None,
|
||||
task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder(),
|
||||
):
|
||||
"""更新插件
|
||||
"""
|
||||
"""更新插件"""
|
||||
await self.installer.update_plugin(plugin_name, plugin_source, task_context)
|
||||
|
||||
|
||||
plugin_container = self.get_plugin_by_name(plugin_name)
|
||||
|
||||
await self.ap.ctr_mgr.plugin.post_update_record(
|
||||
plugin={
|
||||
"name": plugin_name,
|
||||
"remote": plugin_container.plugin_repository,
|
||||
"author": plugin_container.plugin_author,
|
||||
"version": plugin_container.plugin_version
|
||||
'name': plugin_name,
|
||||
'remote': plugin_container.plugin_repository,
|
||||
'author': plugin_container.plugin_author,
|
||||
'version': plugin_container.plugin_version,
|
||||
},
|
||||
old_version=plugin_container.plugin_version,
|
||||
new_version="HEAD"
|
||||
new_version='HEAD',
|
||||
)
|
||||
|
||||
task_context.trace('重载插件..', 'reload-plugin')
|
||||
await self.ap.reload(scope='plugin')
|
||||
|
||||
def get_plugin_by_name(self, plugin_name: str) -> context.RuntimeContainer:
|
||||
"""通过插件名获取插件
|
||||
"""
|
||||
"""通过插件名获取插件"""
|
||||
for plugin in self.plugins():
|
||||
if plugin.plugin_name == plugin_name:
|
||||
return plugin
|
||||
return None
|
||||
|
||||
async def emit_event(self, event: events.BaseEventModel) -> context.EventContext:
|
||||
"""触发事件
|
||||
"""
|
||||
"""触发事件"""
|
||||
|
||||
ctx = context.EventContext(host=self.api_host, event=event)
|
||||
|
||||
ctx = context.EventContext(
|
||||
host=self.api_host,
|
||||
event=event
|
||||
)
|
||||
|
||||
emitted_plugins: list[context.RuntimeContainer] = []
|
||||
|
||||
for plugin in self.plugins(
|
||||
enabled=True,
|
||||
status=context.RuntimeContainerStatus.INITIALIZED
|
||||
enabled=True, status=context.RuntimeContainerStatus.INITIALIZED
|
||||
):
|
||||
if event.__class__ in plugin.event_handlers:
|
||||
self.ap.logger.debug(f'插件 {plugin.plugin_name} 处理事件 {event.__class__.__name__}')
|
||||
|
||||
self.ap.logger.debug(
|
||||
f'插件 {plugin.plugin_name} 处理事件 {event.__class__.__name__}'
|
||||
)
|
||||
|
||||
is_prevented_default_before_call = ctx.is_prevented_default()
|
||||
|
||||
try:
|
||||
await plugin.event_handlers[event.__class__](
|
||||
plugin.plugin_inst,
|
||||
ctx
|
||||
plugin.plugin_inst, ctx
|
||||
)
|
||||
except Exception as e:
|
||||
self.ap.logger.error(f'插件 {plugin.plugin_name} 处理事件 {event.__class__.__name__} 时发生错误: {e}')
|
||||
self.ap.logger.debug(f"Traceback: {traceback.format_exc()}")
|
||||
|
||||
self.ap.logger.error(
|
||||
f'插件 {plugin.plugin_name} 处理事件 {event.__class__.__name__} 时发生错误: {e}'
|
||||
)
|
||||
self.ap.logger.debug(f'Traceback: {traceback.format_exc()}')
|
||||
|
||||
emitted_plugins.append(plugin)
|
||||
|
||||
if not is_prevented_default_before_call and ctx.is_prevented_default():
|
||||
self.ap.logger.debug(f'插件 {plugin.plugin_name} 阻止了默认行为执行')
|
||||
self.ap.logger.debug(
|
||||
f'插件 {plugin.plugin_name} 阻止了默认行为执行'
|
||||
)
|
||||
|
||||
if ctx.is_prevented_postorder():
|
||||
self.ap.logger.debug(f'插件 {plugin.plugin_name} 阻止了后序插件的执行')
|
||||
self.ap.logger.debug(
|
||||
f'插件 {plugin.plugin_name} 阻止了后序插件的执行'
|
||||
)
|
||||
break
|
||||
|
||||
for key in ctx.__return_value__.keys():
|
||||
if hasattr(ctx.event, key):
|
||||
setattr(ctx.event, key, ctx.__return_value__[key][0])
|
||||
|
||||
self.ap.logger.debug(f'事件 {event.__class__.__name__}({ctx.eid}) 处理完成,返回值 {ctx.__return_value__}')
|
||||
|
||||
self.ap.logger.debug(
|
||||
f'事件 {event.__class__.__name__}({ctx.eid}) 处理完成,返回值 {ctx.__return_value__}'
|
||||
)
|
||||
|
||||
if emitted_plugins:
|
||||
plugins_info: list[dict] = [
|
||||
@@ -313,13 +319,13 @@ class PluginManager:
|
||||
'name': plugin.plugin_name,
|
||||
'remote': plugin.plugin_repository,
|
||||
'version': plugin.plugin_version,
|
||||
'author': plugin.plugin_author
|
||||
} for plugin in emitted_plugins
|
||||
'author': plugin.plugin_author,
|
||||
}
|
||||
for plugin in emitted_plugins
|
||||
]
|
||||
|
||||
await self.ap.ctr_mgr.usage.post_event_record(
|
||||
plugins=plugins_info,
|
||||
event_name=event.__class__.__name__
|
||||
plugins=plugins_info, event_name=event.__class__.__name__
|
||||
)
|
||||
|
||||
return ctx
|
||||
@@ -330,7 +336,7 @@ class PluginManager:
|
||||
if plugin.plugin_name == plugin_name:
|
||||
if plugin.enabled == new_status:
|
||||
return False
|
||||
|
||||
|
||||
# 初始化/释放插件
|
||||
if new_status:
|
||||
await self.initialize_plugin(plugin)
|
||||
@@ -338,7 +344,7 @@ class PluginManager:
|
||||
await self.destroy_plugin(plugin)
|
||||
|
||||
plugin.enabled = new_status
|
||||
|
||||
|
||||
await self.dump_plugin_container_setting(plugin)
|
||||
|
||||
break
|
||||
@@ -348,7 +354,6 @@ class PluginManager:
|
||||
return False
|
||||
|
||||
async def reorder_plugins(self, plugins: list[dict]):
|
||||
|
||||
for plugin in plugins:
|
||||
plugin_name = plugin.get('name')
|
||||
plugin_priority = plugin.get('priority')
|
||||
@@ -363,7 +368,9 @@ class PluginManager:
|
||||
for plugin in self.plugin_containers:
|
||||
await self.dump_plugin_container_setting(plugin)
|
||||
|
||||
async def set_plugin_config(self, plugin_container: context.RuntimeContainer, new_config: dict):
|
||||
async def set_plugin_config(
|
||||
self, plugin_container: context.RuntimeContainer, new_config: dict
|
||||
):
|
||||
plugin_container.plugin_config = new_config
|
||||
|
||||
plugin_container.plugin_inst.config = new_config
|
||||
|
||||
Reference in New Issue
Block a user