Skip to content

Commit

Permalink
添加格式化工具配置 (#5)
Browse files Browse the repository at this point in the history
* 添加 .pre-commit-config.yaml

* 💄 auto fix by pre-commit hooks

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
AzideCupric and pre-commit-ci[bot] authored Apr 9, 2024
1 parent 419e3ca commit 65a2c57
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 102 deletions.
28 changes: 28 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
default_install_hook_types: [pre-commit, prepare-commit-msg]
ci:
autofix_commit_msg: ":lipstick: auto fix by pre-commit hooks"
autofix_prs: true
autoupdate_branch: main
autoupdate_schedule: monthly
autoupdate_commit_msg: ":arrow_up: auto update by pre-commit hooks"
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.5
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
stages: [commit]

- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
stages: [commit]

- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black
stages: [commit]

exclude: "CHANGELOG.md"
16 changes: 8 additions & 8 deletions src/nonebot_plugin_auto_bot_selector/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from nonebot.plugin import PluginMetadata

from importlib.metadata import version

from nonebot.plugin import PluginMetadata

try:
__version__ = version("nonebot_plugin_auto_bot_selector")
except Exception:
Expand All @@ -28,18 +28,18 @@
},
)

from .compat import get_bot, get_bots, NoBotFoundError
from .compat import NoBotFoundError, get_bot, get_bots
from .target import (
TargetQQGroup,
PlatformTarget,
TargetQQPrivate,
TargetOB12Unknow,
TargetDoDoChannel,
TargetDoDoPrivate,
TargetQQGuildDirect,
TargetQQGuildChannel,
TargetKaiheilaChannel,
TargetKaiheilaPrivate,
TargetOB12Unknow,
TargetQQGroup,
TargetQQGuildChannel,
TargetQQGuildDirect,
TargetQQPrivate,
)

__all__ = [
Expand Down
6 changes: 3 additions & 3 deletions src/nonebot_plugin_auto_bot_selector/adapters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from . import qq as qq
from . import red as red
from . import dodo as dodo
from . import satori as satori
from . import kaiheila as kaiheila
from . import onebot_v11 as onebot_v11
from . import onebot_v12 as onebot_v12
from . import qq as qq
from . import red as red
from . import satori as satori
7 changes: 2 additions & 5 deletions src/nonebot_plugin_auto_bot_selector/adapters/dodo.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from contextlib import suppress
from typing import List
from contextlib import suppress

from nonebot.adapters import Bot as BaseBot

from ..registries import register_list_targets
from ..target import (
PlatformTarget,
TargetDoDoChannel,
)
from ..target import PlatformTarget, TargetDoDoChannel

with suppress(ImportError):
from nonebot.adapters.dodo import Adapter
Expand Down
16 changes: 4 additions & 12 deletions src/nonebot_plugin_auto_bot_selector/adapters/kaiheila.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
from contextlib import suppress
from typing import List
from contextlib import suppress

from nonebot.adapters import Bot as BaseBot

from ..registries import register_list_targets
from ..target import (
PlatformTarget,
TargetKaiheilaChannel,
TargetKaiheilaPrivate,
)
from ..target import PlatformTarget, TargetKaiheilaChannel, TargetKaiheilaPrivate

with suppress(ImportError):
from nonebot.adapters.kaiheila import Adapter, Bot
from nonebot.adapters.kaiheila.api import (
Channel,
Guild,
UserChat,
)
from nonebot.adapters.kaiheila import Bot, Adapter
from nonebot.adapters.kaiheila.api import Guild, Channel, UserChat

adapter_name = Adapter.get_name()

Expand Down
6 changes: 3 additions & 3 deletions src/nonebot_plugin_auto_bot_selector/adapters/onebot_v11.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from contextlib import suppress
from typing import List
from contextlib import suppress

from nonebot.adapters import Bot

from ..registries import PlatformTarget, add_cache, register_list_targets, remove_cache
from ..target import TargetQQGroup, TargetQQPrivate, target_changed
from ..registries import PlatformTarget, add_cache, remove_cache, register_list_targets

with suppress(ImportError):
from nonebot.adapters.onebot.v11 import Bot as BotOB11
from nonebot.adapters.onebot.v11 import (
Adapter,
FriendAddNoticeEvent,
GroupDecreaseNoticeEvent,
GroupIncreaseNoticeEvent,
)
from nonebot.adapters.onebot.v11 import Bot as BotOB11

adapter_name = Adapter.get_name()

Expand Down
12 changes: 6 additions & 6 deletions src/nonebot_plugin_auto_bot_selector/adapters/onebot_v12.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
from contextlib import suppress
from typing import List
from contextlib import suppress

from nonebot.adapters import Bot as BaseBot

from ..registries import PlatformTarget, add_cache, register_list_targets, remove_cache
from ..registries import PlatformTarget, add_cache, remove_cache, register_list_targets
from ..target import (
TargetOB12Unknow,
TargetQQGroup,
TargetQQGuildChannel,
TargetQQPrivate,
TargetOB12Unknow,
TargetQQGuildChannel,
target_changed,
)

with suppress(ImportError):
from nonebot.adapters.onebot.v12.exception import UnsupportedAction
from nonebot.adapters.onebot.v12 import (
Adapter,
Bot,
Adapter,
FriendDecreaseEvent,
FriendIncreaseEvent,
GroupMemberDecreaseEvent,
GroupMemberIncreaseEvent,
)
from nonebot.adapters.onebot.v12.exception import UnsupportedAction

adapter_name = Adapter.get_name()

Expand Down
6 changes: 3 additions & 3 deletions src/nonebot_plugin_auto_bot_selector/adapters/qq.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from contextlib import suppress
from typing import List
from contextlib import suppress

from nonebot.adapters import Bot as BaseBot

from ..registries import PlatformTarget, register_list_targets
from ..target import TargetQQGuildChannel
from ..registries import PlatformTarget, register_list_targets

with suppress(ImportError):
from nonebot.adapters.qq import Adapter, Bot
from nonebot.adapters.qq import Bot, Adapter

adapter_name = Adapter.get_name()

Expand Down
8 changes: 3 additions & 5 deletions src/nonebot_plugin_auto_bot_selector/adapters/red.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from contextlib import suppress
from typing import List
from contextlib import suppress

from nonebot.adapters import Bot

from ..registries import PlatformTarget, register_list_targets
from ..target import TargetQQGroup, TargetQQPrivate
from ..registries import PlatformTarget, register_list_targets

with suppress(ImportError):
from nonebot.adapters.red import (
Adapter,
)
from nonebot.adapters.red import Adapter
from nonebot.adapters.red import Bot as BotRed

adapter_name = Adapter.get_name()
Expand Down
28 changes: 8 additions & 20 deletions src/nonebot_plugin_auto_bot_selector/adapters/satori.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
from contextlib import suppress
from functools import partial
from typing import (
Awaitable,
Generic,
List,
Optional,
Protocol,
TypeVar,
)
from contextlib import suppress
from typing import List, Generic, TypeVar, Optional, Protocol, Awaitable

from nonebot import logger
from pydantic import BaseModel
from nonebot.adapters import Bot
from nonebot.compat import PYDANTIC_V2, ConfigDict
from nonebot.exception import ActionFailed
from pydantic import BaseModel
from nonebot.compat import PYDANTIC_V2, ConfigDict

from ..registries import register_list_targets
from ..target import (
PlatformTarget,
TargetQQGroup,
TargetQQPrivate,
TargetSatoriUnknown,
)
from ..target import TargetQQGroup, PlatformTarget, TargetQQPrivate, TargetSatoriUnknown

with suppress(ImportError):
from nonebot.adapters.satori import Adapter
Expand Down Expand Up @@ -76,12 +64,12 @@ async def list_targets(bot: Bot) -> List[PlatformTarget]:
targets = []
# 获取群组列表
try:
guilds = await _fetch_all(bot.guild_list) # type: ignore
guilds = await _fetch_all(bot.guild_list) # type: ignore
logger.debug(f"Found {len(guilds)} guilds(groups)")
for guild in guilds:
logger.debug(f"featching -> {guild}")
channels = await _fetch_all(
partial(bot.channel_list, guild_id=guild.id) # type: ignore
partial(bot.channel_list, guild_id=guild.id) # type: ignore
)
for channel in channels:
if bot.platform in ["qq", "red", "chronocat"]:
Expand All @@ -98,7 +86,7 @@ async def list_targets(bot: Bot) -> List[PlatformTarget]:

# 获取好友列表
try:
users = await _fetch_all(bot.friend_list) # type: ignore
users = await _fetch_all(bot.friend_list) # type: ignore
for user in users:
if bot.platform in ["qq", "red", "chronocat"]:
target = TargetQQPrivate(user_id=int(user.id))
Expand Down
25 changes: 15 additions & 10 deletions src/nonebot_plugin_auto_bot_selector/compat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from random import random
from typing import Dict, List, Set, cast
from nonebot import require, get_driver, get_bots as nonebot_get_bots, logger
from typing import Set, Dict, List, cast

from nonebot import get_driver
from nonebot.adapters import Bot
from nonebot import logger, require
from nonebot import get_bots as nonebot_get_bots

from .registries import PlatformTarget

Expand All @@ -13,18 +16,17 @@
USE_SAA = True

if USE_SAA:
from nonebot_plugin_saa.auto_select_bot import (
enable_auto_select_bot,
BOT_CACHE as BOT_CACHE,
_info_current as info_current,
NoBotFound as NoBotFoundError,
refresh_bots as refresh_bots,
)
from nonebot_plugin_saa.auto_select_bot import BOT_CACHE as BOT_CACHE
from nonebot_plugin_saa.auto_select_bot import enable_auto_select_bot
from nonebot_plugin_saa.auto_select_bot import refresh_bots as refresh_bots
from nonebot_plugin_saa.auto_select_bot import NoBotFound as NoBotFoundError
from nonebot_plugin_saa.auto_select_bot import _info_current as info_current

enable_auto_select_bot()
else:
from .registries import BOT_CACHE as BOT_CACHE, BOT_CACHE_LOCK, info_current, refresh_bot, hook_register
from .expection import NoBotFoundError
from .registries import BOT_CACHE as BOT_CACHE
from .registries import BOT_CACHE_LOCK, refresh_bot, info_current, hook_register

hook_register()

Expand All @@ -39,12 +41,15 @@ async def refresh_bots():
await refresh_bot(bot)
logger.debug("refresh bot cache complete")


BOT_CACHE = cast(Dict[Bot, Set[PlatformTarget]], BOT_CACHE)


def get_bot(target: PlatformTarget) -> Bot:
"""获取 Bot"""
return random.choice(get_bots(target=target))


def get_bots(target: PlatformTarget) -> List[Bot]:
"""获取 Bot 列表"""
bots = []
Expand Down
7 changes: 4 additions & 3 deletions src/nonebot_plugin_auto_bot_selector/registries.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import asyncio
import json
from typing import Awaitable, Callable, Dict, List, Set
import asyncio
from typing import Set, Dict, List, Callable, Awaitable

from nonebot import logger, get_driver
from nonebot.adapters import Bot
from nonebot.compat import model_dump
from nonebot import logger, get_driver

from .target import PlatformTarget

Expand Down Expand Up @@ -61,6 +61,7 @@ def info_current():
log_info[str(bot)] = [model_dump(target) for target in platform_target_set]
logger.trace(f"current bot-platform_target: {json.dumps(log_info)}")


def hook_register():
driver = get_driver()

Expand Down
Loading

0 comments on commit 65a2c57

Please sign in to comment.