Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

适配session 0.2.0与access-control 1.1.0 #6

Merged
merged 8 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
custom: ['https://afdian.net/a/ssttkkl']
4 changes: 4 additions & 0 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
template: |
## What’s Changed

$CHANGES
31 changes: 31 additions & 0 deletions .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: PR Check

on:
pull_request:

jobs:
test:

strategy:
fail-fast: false
matrix:
os: [ macos-latest, windows-latest, ubuntu-latest ]
python-version: ["3.9", "3.10", "3.11", "3.12"]

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install requirements
run: |
pip install poetry
poetry export --with=dev --without-hashes -o requirements.txt
pip install -r requirements.txt
- name: Test with pytest
run: pytest
54 changes: 54 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Release

on:
push:
tags:
- v*

jobs:
publish-pypi-github:
runs-on: ubuntu-latest
environment:
name: release
url: https://pypi.org/p/nonebot-plugin-access-control
permissions:
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
# write permission is required to create a github release
contents: write
# write permission is required for autolabeler
# otherwise, read permission is required at least
pull-requests: write

steps:
- uses: actions/checkout@v3

- name: Setup Python environment
uses: actions/setup-python@v3
with:
python-version: "3.11"

- name: Setup Poetry
run: |
pip install poetry

- run: |
echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV

- uses: release-drafter/release-drafter@v5
with:
name: Release ${{ env.TAG_NAME }}
tag: ${{ env.TAG_NAME }}
publish: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Publish PyPI and Github
run: |
poetry build
ls dist/
gh release upload --clobber ${{ env.TAG_NAME }} dist/*.tar.gz dist/*.whl
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Publish pypi
uses: pypa/gh-action-pypi-publish@release/v1
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
default_install_hook_types: [pre-commit, prepare-commit-msg]
ci:
autofix_commit_msg: ":rotating_light: auto fix by pre-commit hooks"
autofix_prs: true
autoupdate_branch: master
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.0.292
hooks:
- id: ruff
args: [--fix]
stages: [commit]

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

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

20 changes: 13 additions & 7 deletions nonebot_plugin_nagabus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

from nonebot import require

require("nonebot_plugin_access_control")
require("nonebot_plugin_access_control_api")
require("nonebot_plugin_datastore")
require("nonebot_plugin_majsoul")
require("nonebot_plugin_saa")
require("nonebot_plugin_session")
require("nonebot_plugin_session_orm")
require("ssttkkl_nonebot_utils")

from nonebot import logger
from nonebot.plugin import PluginMetadata
from ssttkkl_nonebot_utils.nonebot import default_command_start

from .config import Config
from .errors import ConfigError

from ssttkkl_nonebot_utils.nonebot import default_command_start

__usage__ = f"""
牌谱分析:
{default_command_start}naga <雀魂牌谱链接> <东/南x局x本场>:消耗10NP解析雀魂小局
Expand All @@ -38,16 +38,22 @@
""".strip()

__plugin_meta__ = PluginMetadata(
name='NAGA公交车',
description='为群友提供NAGA拼车服务',
name="NAGA公交车",
description="为群友提供NAGA拼车服务",
usage=__usage__,
type="application",
homepage="https://github.com/bot-ssttkkl/nonebot-plugin-nagabus",
config=Config,
supported_adapters={"~onebot.v11", "~onebot.v12", "~qqguild", "~kaiheila", "~telegram"}
supported_adapters={
"~onebot.v11",
"~onebot.v12",
"~qqguild",
"~kaiheila",
"~telegram",
},
)

try:
from . import matchers
from . import matchers # noqa
except ConfigError as e:
logger.exception(e)
2 changes: 1 addition & 1 deletion nonebot_plugin_nagabus/ac.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from nonebot_plugin_access_control.service import create_plugin_service
from nonebot_plugin_access_control_api.service import create_plugin_service

ac = create_plugin_service("nonebot_plugin_nagabus")
4 changes: 2 additions & 2 deletions nonebot_plugin_nagabus/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Config(BaseSettings):
def detect_sql_dialect(cls, values):
if "datastore_database_url" in values:
url = urlparse(values["datastore_database_url"])
if '+' in url.scheme:
sql_dialect = url.scheme.split('+')[0]
if "+" in url.scheme:
sql_dialect = url.scheme.split("+")[0]
else:
sql_dialect = url.scheme
else:
Expand Down
1 change: 0 additions & 1 deletion nonebot_plugin_nagabus/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@


49 changes: 48 additions & 1 deletion nonebot_plugin_nagabus/data/mjs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@
import json
from collections.abc import Awaitable
from typing import Any, Callable, Optional

from nonebot import logger
from sqlalchemy import select
from sqlalchemy.orm import Mapped, mapped_column
from nonebot_plugin_majsoul.paipu import download_paipu

from .base import SqlModel
from ..data.utils import insert
from .utils.session import _use_session


class MajsoulPaipuOrm(SqlModel):
__tablename__ = 'nonebot_plugin_nagabus_majsoul_paipu'
__tablename__ = "nonebot_plugin_nagabus_majsoul_paipu"
__table_args__ = {"extend_existing": True}

paipu_uuid: Mapped[str] = mapped_column(primary_key=True)
content: Mapped[str]


# 为了方便单测时mock实现
_get_majsoul_paipu_delegate: Optional[Callable[[str], Awaitable[Any]]] = None


def _set_get_majsoul_paipu_delegate(get_majsoul_paipu_delegate):
global _get_majsoul_paipu_delegate
_get_majsoul_paipu_delegate = get_majsoul_paipu_delegate


async def get_majsoul_paipu(uuid: str):
if _get_majsoul_paipu_delegate is not None:
return await _get_majsoul_paipu_delegate(uuid)

async with _use_session() as sess:
stmt = (
select(MajsoulPaipuOrm).where(MajsoulPaipuOrm.paipu_uuid == uuid).limit(1)
)
res = (await sess.execute(stmt)).scalar_one_or_none()

if res is not None:
logger.opt(colors=True).info(f"Use cached majsoul paipu <y>{uuid}</y>")
return json.loads(res.content)

logger.opt(colors=True).info(f"Downloading majsoul paipu <y>{uuid}</y> ...")
data = await download_paipu(uuid)

stmt = (
insert(MajsoulPaipuOrm)
.values(paipu_uuid=uuid, content=json.dumps(data))
.on_conflict_do_nothing(index_elements=[MajsoulPaipuOrm.paipu_uuid])
)

await sess.execute(stmt)
await sess.commit()

return data
Loading