feat: 新的资源包加载逻辑

feat: 主题商店支持
This commit is contained in:
2024-04-06 08:48:21 +08:00
parent be0b5e6de1
commit a76bc3de92
14 changed files with 153 additions and 31 deletions

View File

@ -1,6 +1,5 @@
from nonebot.plugin import PluginMetadata
from liteyuki.utils.language import get_default_lang
from .core import *
from .loader import *
from .runtime import *

View File

@ -14,6 +14,7 @@ from liteyuki.utils.language import get_user_lang
from liteyuki.utils.ly_typing import T_Bot, T_MessageEvent
from liteyuki.utils.message import Markdown as md
from liteyuki.utils.reloader import Reloader
from liteyuki.utils.resource import get_loaded_resource_packs, load_resources
require("nonebot_plugin_alconna"), require("nonebot_plugin_htmlrender")
from nonebot_plugin_alconna import on_alconna, Alconna, Args, Subcommand, Arparma
@ -45,6 +46,14 @@ reload_liteyuki = on_alconna(
permission=SUPERUSER
)
reload_resources = on_alconna(
aliases={"重载资源"},
command=Alconna(
"reload-resources"
),
permission=SUPERUSER
)
cmd_config = on_alconna(
aliases={"配置"},
command=Alconna(
@ -153,6 +162,17 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
await md.send_md(reply, bot, event=event)
@reload_resources.handle()
async def _(event: T_MessageEvent):
ulang = get_user_lang(str(event.user_id))
load_resources()
await reload_resources.finish(
ulang.get("liteyuki.reload_resources_success",
NUM=len(get_loaded_resource_packs())
)
)
@switch_image_mode.handle()
async def _(bot: T_Bot, event: T_MessageEvent):
global markdown_image

View File

@ -1,20 +1,15 @@
import os.path
import shutil
import nonebot.plugin
from liteyuki.utils import init_log
from liteyuki.utils.data_manager import InstalledPlugin, plugin_db
from liteyuki.utils.resource import load_resource_from_dir
from liteyuki.utils.resource import load_resource_from_dir, load_resources
from liteyuki.utils.tools import check_for_package
# 加载默认资源和语言
RESOURCE_PATH = "liteyuki/resources"
load_resource_from_dir(RESOURCE_PATH)
load_resources()
init_log()
# 加载其他资源包
if os.path.exists("resources"):
for resource in os.listdir("resources"):
load_resource_from_dir(os.path.join("resources", resource))
nonebot.plugin.load_plugins("liteyuki/plugins")
nonebot.plugin.load_plugins("plugins")

View File

@ -144,10 +144,13 @@ async def get_stats_data(self_id: str = None, lang: str = None) -> dict:
if disk_usage.total > 0:
disk_data.append(
{
"name" : disk.device,
"total" : disk_total_show,
"free" : disk_free_show,
"percent": disk_usage.percent,
"name" : disk.device,
"total" : disk_total_show,
"free" : disk_free_show,
"percent" : disk_usage.percent,
"usedValue" : disk_usage.used,
"freeValue" : disk_usage.free,
"totalValue": disk_usage.total,
}
)
@ -164,14 +167,17 @@ async def get_stats_data(self_id: str = None, lang: str = None) -> dict:
cpu_info = get_cpu_info()
templ = {
"plugin" : len(nonebot.get_loaded_plugins()),
"version" : __VERSION__,
"system" : platform.platform(),
"cpu" : [
{
"name" : "USED",
"value": psutil.cpu_percent(interval=1)
"value": psutil.cpu_percent()
},
{
"name" : "FREE",
"value": 100 - psutil.cpu_percent(interval=1)
"value": 100 - psutil.cpu_percent()
}
],
"mem" : [
@ -221,6 +227,7 @@ async def get_stats_data(self_id: str = None, lang: str = None) -> dict:
"cpu_trans" : ulang.get("main.monitor.cpu"),
"mem_trans" : ulang.get("main.monitor.memory"),
"swap_trans" : ulang.get("main.monitor.swap"),
"disk_trans" : ulang.get("main.monitor.disk"),
"used_trans" : ulang.get("main.monitor.used"),
"free_trans" : ulang.get("main.monitor.free"),
"total_trans": ulang.get("main.monitor.total"),

View File

@ -2,6 +2,7 @@ from nonebot.plugin import PluginMetadata
from .manager import *
from .installer import *
from .helper import *
from .rpm import *
__author__ = "snowykami"
__plugin_meta__ = PluginMetadata(

View File

@ -0,0 +1,18 @@
# 轻雪资源包管理器
from nonebot.permission import SUPERUSER
from nonebot_plugin_alconna import on_alconna, Alconna, Args, Subcommand, Arparma
from liteyuki.utils.ly_typing import T_Bot
list_rp = on_alconna(
aliases={"列出资源包", "资源包列表"},
command=Alconna(
"list",
Args["page", int, 1]["num", int, 10],
),
permission=SUPERUSER
)
@list_rp.handle()
async def _(bot: T_Bot):
pass

View File

@ -98,4 +98,6 @@ liteyuki.image_mode_on=开启Markdown图片模式
liteyuki.image_mode_off=关闭Markdown图片模式
npm.page=第{PAGE}/{TOTAL}页
main.monitor.free=空闲
main.monitor.free=空闲
liteyuki.reload_resources_success=资源重载成功,共计{NUM}个资源包

View File

@ -7,7 +7,6 @@
<title>Liteyuki Stats</title>
<link rel="stylesheet" href="css/fonts.css">
<link rel="stylesheet" href="css/style.css">
<style>
.pie-chart {
height: 240px;

View File

@ -1,4 +1,5 @@
import os
import shutil
import nonebot
import yaml
@ -6,8 +7,8 @@ from typing import Any
from liteyuki.utils.data import LiteModel
_resource_data = {}
_loaded_resource_packs = [] # 按照加载顺序排序
_loaded_resource_packs: list["ResourceMetadata"] = [] # 按照加载顺序排序
temp_resource_root = "data/liteyuki/resources"
class ResourceMetadata(LiteModel):
@ -19,24 +20,21 @@ class ResourceMetadata(LiteModel):
def load_resource_from_dir(path: str):
"""
把资源包按照文件相对路径加载到资源包中,后加载的优先级更高,顺便加载语言
把资源包按照文件相对路径复制到运行临时文件夹data/liteyuki/resources
Args:
path: 资源文件夹
Returns:
"""
for root, dirs, files in os.walk(path):
for file in files:
relative_path = os.path.relpath(os.path.join(root, file), path).replace("\\", "/")
abs_path = os.path.join(root, file).replace("\\", "/")
_resource_data[relative_path] = abs_path
if os.path.exists(os.path.join(path, "metadata.yml")):
with open(os.path.join(path, "metadata.yml"), "r", encoding="utf-8") as f:
metadata = yaml.safe_load(f)
else:
# 没有metadata.yml文件不是一个资源包
return
for root, dirs, files in os.walk(path):
for file in files:
relative_path = os.path.relpath(os.path.join(root, file), path)
copy_file(os.path.join(root, file), os.path.join(temp_resource_root, relative_path))
metadata["path"] = path
if os.path.exists(os.path.join(path, "lang")):
from liteyuki.utils.language import load_from_dir
@ -53,7 +51,11 @@ def get_path(path: str, abs_path: bool = False, default: Any = None) -> str | An
path: 文件相对路径
Returns: 文件绝对路径
"""
return _resource_data.get(path, default) if not abs_path else os.path.abspath(_resource_data.get(path, default))
resource_relative_path = os.path.join(temp_resource_root, path)
if os.path.exists(resource_relative_path):
return os.path.abspath(resource_relative_path) if abs_path else resource_relative_path
else:
return default
def get_files(path: str, abs_path: bool = False) -> list[str]:
@ -64,5 +66,46 @@ def get_files(path: str, abs_path: bool = False) -> list[str]:
path: 文件夹相对路径
Returns: 文件绝对路径
"""
return [os.path.abspath(file) for file in _resource_data if file.startswith(path)] if abs_path else [
file for file in _resource_data if file.startswith(path)]
resource_relative_path = os.path.join(temp_resource_root, path)
if os.path.exists(resource_relative_path):
return [os.path.abspath(os.path.join(resource_relative_path, file)) if abs_path else os.path.join(resource_relative_path, file) for file in
os.listdir(resource_relative_path)]
else:
return []
def get_loaded_resource_packs() -> list[ResourceMetadata]:
"""
获取已加载的资源包
Returns: 资源包列表
"""
return _loaded_resource_packs
def copy_file(src, dst):
# 获取目标文件的目录
dst_dir = os.path.dirname(dst)
# 如果目标目录不存在,创建它
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
# 复制文件
shutil.copy(src, dst)
def load_resources():
"""用于外部主程序调用的资源加载函数
Returns:
"""
# 加载默认资源和语言
# 清空临时资源包路径data/liteyuki/resources
_loaded_resource_packs.clear()
if os.path.exists(temp_resource_root):
shutil.rmtree(temp_resource_root)
os.makedirs(temp_resource_root, exist_ok=True)
standard_resource_path = "liteyuki/resources"
load_resource_from_dir(standard_resource_path)
# 加载其他资源包
if os.path.exists("resources"):
for resource in os.listdir("resources"):
load_resource_from_dir(os.path.join("resources", resource))