Skip to content

Commit

Permalink
🐛 修复一些细节小问题
Browse files Browse the repository at this point in the history
  • Loading branch information
snowykami committed Oct 20, 2024
1 parent ef58663 commit 13b95c2
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 124 deletions.
Empty file removed liteyuki/bot/iobus.py
Empty file.
5 changes: 0 additions & 5 deletions liteyuki/comm/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ def __init__(self, name: str, type_check: Optional[bool] = None):
if name in _channel:
raise ValueError(f"Channel {name} already exists")
_channel[name] = self
logger.debug(f"Channel {name} initialized in main process")
else:
logger.debug(
f"Channel {name} initialized in sub process, should manually set in main process"
)

def _get_generic_type(self) -> Optional[type]:
"""
Expand Down
2 changes: 2 additions & 0 deletions liteyuki/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def load_config_in_default(no_waring: bool = False) -> dict[str, Any]:
从一个标准的轻雪项目加载配置文件
项目目录下的config.*和config目录下的所有配置文件
项目目录下的配置文件优先
Args:
no_waring: 是否关闭警告
"""
config = load_configs_from_dirs("config", no_waring=no_waring)
config.update(
Expand Down
57 changes: 35 additions & 22 deletions liteyuki/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,34 @@

import loguru

logger = loguru.logger
logger = loguru.logger.bind()

# DEBUG日志格式
debug_format: str = (
"<c>{time:YYYY-MM-DD HH:mm:ss}</c> "
"<lvl>[{level.icon}]</lvl> "
"<c><{name}.{module}.{function}:{line}></c> "
"{message}"
"<c>{time:YYYY-MM-DD HH:mm:ss}</c> "
"<lvl>[{level.icon}{level}]</lvl> "
"<c><{name}.{module}.{function}:{line}></c> "
"{message}"
)

# 默认日志格式
default_format: str = (
"<c>{time:MM-DD HH:mm:ss}</c> "
"<lvl>[{level.icon}]</lvl> "
"<c><{name}></c> "
"{message}"
"<c>{time:MM-DD HH:mm:ss}</c> "
"<lvl>[{level.icon}{level}]</lvl> "
"<c><{name}></c> "
"{message}"
)


def get_format(level: str) -> str:
"""
获取日志格式
Args:
level: 日志等级
Returns: 日志格式
"""
# DEBUG日志格式

if level == "DEBUG":
return debug_format
else:
Expand All @@ -41,23 +49,28 @@ def get_format(level: str) -> str:
def init_log(config: dict):
"""
在语言加载完成后执行
Returns:
Args:
config: 配置
"""

global logger
level = config.get("log_level", "DEBUG")
print("初始化日志系统", level)
logger.remove()
logger.add(
sys.stdout,
level=0,
level=level,
diagnose=False,
format=get_format(config.get("log_level", "INFO")),
format=get_format(level),
)
show_icon = config.get("log_icon", True)
logger.level("DEBUG", color="<blue>", icon=f"{'🐛' if show_icon else ''}DEBUG")
logger.level("INFO", color="<normal>", icon=f"{'ℹ️' if show_icon else ''}INFO")
logger.level("SUCCESS", color="<green>", icon=f"{'✅' if show_icon else ''}SUCCESS")
logger.level("WARNING", color="<yellow>", icon=f"{'⚠️' if show_icon else ''}WARNING")
logger.level("ERROR", color="<red>", icon=f"{'⭕' if show_icon else ''}ERROR")
logger.level("DEBUG", color="<blue>", icon=f"{'🐛' if show_icon else ''}")
logger.level("INFO", color="<normal>", icon=f"{'ℹ️' if show_icon else ''}")
logger.level("SUCCESS", color="<green>", icon=f"{'✅' if show_icon else ''}")
logger.level("WARNING", color="<yellow>", icon=f"{'⚠️' if show_icon else ''}")
logger.level("ERROR", color="<red>", icon=f"{'⭕' if show_icon else ''}")
logger.level("CRITICAL", color="<red>", icon=f"{'❌' if show_icon else ''}")
logger.level("TRACE", color="<cyan>", icon=f"{'🔍' if show_icon else ''}")

logger.bind()

init_log(config={})
init_log(config={"log_level": "DEBUG", "log_icon": True})
5 changes: 4 additions & 1 deletion liteyuki/session/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from magicoca import Chan, select

from liteyuki.log import logger


def message_handler_thread(i_chans: Iterable[Chan[Any]]):
"""
Expand All @@ -15,4 +17,5 @@ def message_handler_thread(i_chans: Iterable[Chan[Any]]):
Returns:
"""
for msg in select(*i_chans):
print("Recv from anybot", msg)
logger.debug(f"Recv from anybot {msg}")
logger.info(f"Recv from anybot {msg}")
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ httpx>=0.27.0
nonebot-plugin-htmlrender>=0.1.0
nonebot2[fastapi,httpx,websockets]>=2.3.3
nonebot-adapter-onebot>=2.4.3
nonebot-plugin-alconna>=0.46.3
nonebot-plugin-alconna>=0.53.0
nonebot_plugin_apscheduler>=0.4.0
nonebot-adapter-satori>=0.11.5
mysql-connector-python>=9.1.0
pyppeteer>=2.0.0
markdown>=3.3.6
zhDateTime>=1.0.3
Expand All @@ -34,4 +35,5 @@ fastapi~=0.115.0

# liteyuki dependencies
croterline>=1.0.7
magicoca>=1.0.5
magicoca>=1.0.5
mysql-connector-python~=9.1.0
6 changes: 5 additions & 1 deletion src/liteyuki_plugins/nonebot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os.path
from pathlib import Path

import nonebot

from croterline.utils import IsMainProcess

from liteyuki.core import sub_process_manager
Expand All @@ -14,9 +14,12 @@


def nb_run(*args, **kwargs):
import nonebot

nonebot.init(**kwargs)

from .nb_utils import driver_manager, adapter_manager

driver_manager.init(config=kwargs)
adapter_manager.init(kwargs)
adapter_manager.register()
Expand All @@ -26,6 +29,7 @@ def nb_run(*args, **kwargs):

if IsMainProcess:
from .dev_reloader import *

bot = get_bot()

sub_process_manager.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def start_push_thread():
)

if MESSAGE in plugin_config.gotify_includes:
@on_message().handle()
@on_message(block=False, priority=100).handle()
async def _(event: Event):
ctx = Context(
user_id=event.get_user_id(),
Expand Down
9 changes: 6 additions & 3 deletions src/liteyuki_plugins/nonebot/nonebot_plugins/to_liteyuki.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from croterline.process import get_ctx
from nonebot.adapters.onebot.v11 import MessageEvent
from nonebot.plugin import PluginMetadata
from nonebot.log import logger
from nonebot import on_message

__plugin_meta__ = PluginMetadata(
Expand All @@ -20,11 +21,13 @@
usage="用户无需使用",
)



ctx = get_ctx()

@on_message().handle()
@on_message(block=False, priority=100).handle()
async def _(event: MessageEvent):
print("Push message to Liteyuki")

logger.debug("Pushing message to Liteyuki")
ctx.sub_chan << event.raw_message
logger.debug("Pushed message to Liteyuki")

9 changes: 3 additions & 6 deletions src/liteyuki_plugins/nonebot/np_main/core.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import time
from typing import AnyStr

import time
from typing import AnyStr

import nonebot
import pip
from nonebot import get_driver, require
Expand All @@ -14,15 +11,15 @@

# from src.liteyuki.core import Reloader
from src.utils import event as event_utils, satori_utils
from src.utils.base import reload # type: ignore
from src.utils.base.config import get_config
from src.utils.base.data_manager import TempConfig, common_db
from src.utils.base.language import get_user_lang
from src.utils.base.ly_function import get_function # type: ignore
from src.utils.base.ly_typing import T_Bot, T_MessageEvent
from src.utils.message.html_tool import md_to_pic
from src.utils.message.message import MarkdownMessage as md, broadcast_to_superusers
from .api import update_liteyuki # type: ignore
from src.utils.base import reload # type: ignore
from src.utils.base.ly_function import get_function # type: ignore
from src.utils.message.html_tool import md_to_pic

require("nonebot_plugin_alconna")
require("nonebot_plugin_apscheduler")
Expand Down
2 changes: 0 additions & 2 deletions src/liteyuki_plugins/nonebot/np_main/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

import nonebot.plugin
from nonebot import get_driver
from src.utils import init_log
from src.utils.base.config import get_config
from src.utils.base.data_manager import InstalledPlugin, plugin_db
from src.utils.base.resource import load_resources
from src.utils.message.tools import check_for_package

load_resources()
init_log()

driver = get_driver()

Expand Down
2 changes: 0 additions & 2 deletions src/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
__VERSION__ = "6.3.2" # 60201

from src.utils.base.config import load_from_yaml, config
from src.utils.base.log import init_log
from git import Repo

major, minor, patch = map(int, __VERSION__.split("."))
Expand All @@ -20,7 +19,6 @@ def init():
"""
# 检测python版本是否高于3.10
init_log()
if sys.version_info < (3, 10):
nonebot.logger.error("Requires Python3.10+ to run, please upgrade your Python Environment.")
exit(1)
Expand Down
Loading

0 comments on commit 13b95c2

Please sign in to comment.