From 52528707336a98330bf3bddaa756d3f510528be4 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Mon, 3 Mar 2025 15:17:42 +0800 Subject: [PATCH] style: cleanup --- .pre-commit-config.yaml | 19 +++++++++------ astrbot/core/message/components.py | 2 +- .../aiocqhttp/aiocqhttp_platform_adapter.py | 3 ++- .../core/platform/sources/gewechat/client.py | 8 ++++--- .../qqofficial_webhook/qo_webhook_server.py | 2 +- .../platform/sources/wecom/wecom_adapter.py | 2 +- .../core/provider/sources/edge_tts_source.py | 3 ++- astrbot/dashboard/server.py | 2 +- main.py | 8 +++---- packages/python_interpreter/main.py | 8 +++---- pyproject.toml | 23 +++++++++++++++---- 11 files changed, 52 insertions(+), 28 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e7c53aecf..6ecab6bd2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,8 +1,13 @@ +default_install_hook_types: [pre-commit, prepare-commit-msg] +ci: + autofix_commit_msg: ":balloon: auto fixes by pre-commit hooks" + autofix_prs: true + autoupdate_branch: master + autoupdate_schedule: weekly + autoupdate_commit_msg: ":balloon: pre-commit autoupdate" repos: -- repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.9.9 - hooks: - # Run the linter. - - id: ruff - # Run the formatter. - - id: ruff-format \ No newline at end of file + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.9.9 + hooks: + - id: ruff + - id: ruff-format diff --git a/astrbot/core/message/components.py b/astrbot/core/message/components.py index d2f48d388..44caa2075 100644 --- a/astrbot/core/message/components.py +++ b/astrbot/core/message/components.py @@ -84,7 +84,7 @@ class BaseMessageComponent(BaseModel): return output def toDict(self): - data = dict() + data = {} for k, v in self.__dict__.items(): if k == "type" or v is None: continue diff --git a/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py b/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py index 9287044d9..f21fda066 100644 --- a/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py +++ b/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py @@ -278,7 +278,8 @@ class AiocqhttpAdapter(Platform): return self.metadata async def shutdown_trigger_placeholder(self): - while not self._event_queue.closed and not self.stop: + # TODO: use asyncio.Event + while not self._event_queue.closed and not self.stop: # noqa: ASYNC110 await asyncio.sleep(1) logger.info("aiocqhttp 适配器已关闭。") diff --git a/astrbot/core/platform/sources/gewechat/client.py b/astrbot/core/platform/sources/gewechat/client.py index 8b41abc97..05e915dc1 100644 --- a/astrbot/core/platform/sources/gewechat/client.py +++ b/astrbot/core/platform/sources/gewechat/client.py @@ -6,6 +6,7 @@ import base64 import datetime import re import os +import anyio from astrbot.api.platform import AstrBotMessage, MessageMember, MessageType from astrbot.api.message_components import Plain, Image, At, Record from astrbot.api import logger, sp @@ -189,8 +190,8 @@ class SimpleGewechatClient: if "ImgBuf" in d and "buffer" in d["ImgBuf"]: voice_data = base64.b64decode(d["ImgBuf"]["buffer"]) file_path = f"data/temp/gewe_voice_{abm.message_id}.silk" - with open(file_path, "wb") as f: - f.write(voice_data) + async with await anyio.open_file(file_path, "wb") as f: + await f.write(voice_data) abm.message.append(Record(file=file_path, url=file_path)) case _: logger.info(f"未实现的消息类型: {d['MsgType']}") @@ -251,7 +252,8 @@ class SimpleGewechatClient: ) async def shutdown_trigger_placeholder(self): - while not self.event_queue.closed and not self.stop: + # TODO: use asyncio.Event + while not self.event_queue.closed and not self.stop: # noqa: ASYNC110 await asyncio.sleep(1) logger.info("gewechat 适配器已关闭。") diff --git a/astrbot/core/platform/sources/qqofficial_webhook/qo_webhook_server.py b/astrbot/core/platform/sources/qqofficial_webhook/qo_webhook_server.py index f863c87be..562574204 100644 --- a/astrbot/core/platform/sources/qqofficial_webhook/qo_webhook_server.py +++ b/astrbot/core/platform/sources/qqofficial_webhook/qo_webhook_server.py @@ -102,6 +102,6 @@ class QQOfficialWebhook: ) async def shutdown_trigger_placeholder(self): - while not self.event_queue.closed: + while not self.event_queue.closed: # noqa: ASYNC110 await asyncio.sleep(1) logger.info("qq_official_webhook 适配器已关闭。") diff --git a/astrbot/core/platform/sources/wecom/wecom_adapter.py b/astrbot/core/platform/sources/wecom/wecom_adapter.py index 2e3f5fb19..16f3fdd13 100644 --- a/astrbot/core/platform/sources/wecom/wecom_adapter.py +++ b/astrbot/core/platform/sources/wecom/wecom_adapter.py @@ -94,7 +94,7 @@ class WecomServer: ) async def shutdown_trigger_placeholder(self): - while not self.event_queue.closed: + while not self.event_queue.closed: # noqa: ASYNC110 await asyncio.sleep(1) logger.info("企业微信 适配器已关闭。") diff --git a/astrbot/core/provider/sources/edge_tts_source.py b/astrbot/core/provider/sources/edge_tts_source.py index db28244c4..56b42a0ca 100644 --- a/astrbot/core/provider/sources/edge_tts_source.py +++ b/astrbot/core/provider/sources/edge_tts_source.py @@ -2,6 +2,7 @@ import uuid import os import edge_tts import subprocess +import asyncio from ..provider import TTSProvider from ..entites import ProviderType from ..register import register_provider_adapter @@ -55,7 +56,7 @@ class ProviderEdgeTTS(TTSProvider): await communicate.save(mp3_path) # 使用ffmpeg将MP3转换为标准WAV格式 - _ = subprocess.run( + _ = await asyncio.create_subprocess_exec( [ "ffmpeg", "-y", # 覆盖输出文件 diff --git a/astrbot/dashboard/server.py b/astrbot/dashboard/server.py index 0a5839533..0ba0f35e0 100644 --- a/astrbot/dashboard/server.py +++ b/astrbot/dashboard/server.py @@ -71,7 +71,7 @@ class AstrBotDashboard: return r async def shutdown_trigger_placeholder(self): - while not self.core_lifecycle.event_queue.closed: + while not self.core_lifecycle.event_queue.closed: # noqa: ASYNC110 await asyncio.sleep(1) logger.info("管理面板已关闭。") diff --git a/main.py b/main.py index 55f785d86..e66d33fef 100644 --- a/main.py +++ b/main.py @@ -15,10 +15,10 @@ logo_tmpl = r""" ___ _______.___________..______ .______ ______ .___________. / \ / | || _ \ | _ \ / __ \ | | / ^ \ | (----`---| |----`| |_) | | |_) | | | | | `---| |----` - / /_\ \ \ \ | | | / | _ < | | | | | | - / _____ \ .----) | | | | |\ \----.| |_) | | `--' | | | -/__/ \__\ |_______/ |__| | _| `._____||______/ \______/ |__| - + / /_\ \ \ \ | | | / | _ < | | | | | | + / _____ \ .----) | | | | |\ \----.| |_) | | `--' | | | +/__/ \__\ |_______/ |__| | _| `._____||______/ \______/ |__| + """ diff --git a/packages/python_interpreter/main.py b/packages/python_interpreter/main.py index aaeba473f..73492bb1f 100644 --- a/packages/python_interpreter/main.py +++ b/packages/python_interpreter/main.py @@ -23,7 +23,7 @@ You need to generate python codes to solve user's problem: {prompt} {extra_input} ## Limit -1. Available libraries: +1. Available libraries: - standard libs - `Pillow` - `requests` @@ -40,8 +40,8 @@ You need to generate python codes to solve user's problem: {prompt} - `mplfonts` You can only use these libraries and the libraries that they depend on. 2. Do not generate malicious code. -3. Use given `shared.api` package to output the result. - It has 3 functions: `send_text(text: str)`, `send_image(image_path: str)`, `send_file(file_path: str)`. +3. Use given `shared.api` package to output the result. + It has 3 functions: `send_text(text: str)`, `send_image(image_path: str)`, `send_file(file_path: str)`. For Image and file, you must save it to `output` folder. 4. You must only output the code, do not output the result of the code and any other information. 5. The output language is same as user's input language. @@ -58,7 +58,7 @@ def fabonacci(n): return n else: return fabonacci(n-1) + fabonacci(n-2) - + result = fabonacci(10) send_text("The fabonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, starting from 0 and 1.") send_text("Let's calculate the fabonacci sequence of 10: " + result) # send_text is a function to send pure text to user diff --git a/pyproject.toml b/pyproject.toml index e61f1a087..15adfaca2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,21 @@ [tool.ruff] -lint.ignore = ["F403", "F405"] exclude = [ - "astrbot/core/utils/t2i/local_strategy.py", - "astrbot/api/all.py" -] \ No newline at end of file + "astrbot/core/utils/t2i/local_strategy.py", + "astrbot/api/all.py", +] +line-length = 88 +lint.ignore = [ + "F403", + "F405", + "E501", + "ASYNC230" # TODO: handle ASYNC230 in AstrBot +] +lint.select = [ + "F", # Pyflakes + "W", # pycodestyle warnings + "E", # pycodestyle errors + "ASYNC", # flake8-async + "C4", # flake8-comprehensions + "Q", # flake8-quotes +] +target-version = "py310"