Skip to content

Commit f055a1b

Browse files
authored
feat: 商店存放 json5 格式的数据 (#259)
* chore(deps): 添加 pyjson5 * feat: 商店存放 json5 格式的数据 避免合并冲突 * fix: 模块名应该为 pyjson5 * refactor: 使用推荐的 decode 方法 * test: 加上 json5 的测试 * fix: 后缀都改成 json5 * fix: 空列表末尾也应该有换行符
1 parent b285af6 commit f055a1b

File tree

23 files changed

+235
-98
lines changed

23 files changed

+235
-98
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/
1010
### Added
1111

1212
- 通过名称加主页避免机器人重复
13+
- 商店存放 json5 格式的数据
1314

1415
### Fixed
1516

examples/noneflow.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ jobs:
4343
config: >
4444
{
4545
"base": "master",
46-
"plugin_path": "assets/plugins.json",
47-
"bot_path": "assets/bots.json",
48-
"adapter_path": "assets/adapters.json",
46+
"plugin_path": "assets/plugins.json5",
47+
"bot_path": "assets/bots.json5",
48+
"adapter_path": "assets/adapters.json5",
4949
"registry_repository": "nonebot/registry"
5050
}
5151
env:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies = [
1414
"nonebot2>=2.4.0",
1515
"pre-commit>=4.0.1",
1616
"pydantic-extra-types>=2.9.0",
17+
"pyjson5>=1.6.7",
1718
]
1819

1920
[project.urls]

src/plugins/github/plugins/publish/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
from src.plugins.github.models import IssueHandler
1111
from src.plugins.github.models.github import GithubHandler
1212
from src.plugins.github.utils import commit_message as _commit_message
13-
from src.plugins.github.utils import dump_json, load_json, run_shell_command
13+
from src.plugins.github.utils import run_shell_command
1414
from src.providers.models import RegistryUpdatePayload, to_store
15+
from src.providers.utils import dump_json5, load_json5_from_file
1516
from src.providers.validation import PublishType, ValidationDict
1617

1718
from .constants import (
@@ -149,9 +150,9 @@ def update_file(result: ValidationDict) -> None:
149150

150151
logger.info(f"正在更新文件: {path}")
151152

152-
data = load_json(path)
153+
data = load_json5_from_file(path)
153154
data.append(new_data)
154-
dump_json(path, data, 2)
155+
dump_json5(path, data)
155156

156157
logger.info("文件更新完成")
157158

src/plugins/github/plugins/remove/utils.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99
get_type_by_labels,
1010
)
1111
from src.plugins.github.models import GithubHandler, IssueHandler
12-
from src.plugins.github.utils import (
13-
commit_message,
14-
dump_json,
15-
run_shell_command,
16-
)
12+
from src.plugins.github.utils import commit_message, run_shell_command
13+
from src.providers.utils import dump_json5
1714
from src.providers.validation.models import PublishType
1815

1916
from .constants import COMMIT_MESSAGE_PREFIX, REMOVE_LABEL
@@ -40,7 +37,7 @@ def update_file(type: PublishType, key: str):
4037
data = load_publish_data(type)
4138
# 删除对应的数据项
4239
data.pop(key)
43-
dump_json(path, list(data.values()))
40+
dump_json5(path, list(data.values()))
4441
logger.info(f"已更新 {path.name} 文件")
4542

4643

src/plugins/github/plugins/remove/validation.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
from src.plugins.github import plugin_config
66
from src.plugins.github.models import AuthorInfo
7-
from src.plugins.github.utils import extract_issue_info_from_issue, load_json
7+
from src.plugins.github.utils import extract_issue_info_from_issue
88
from src.providers.constants import BOT_KEY_TEMPLATE, PYPI_KEY_TEMPLATE
9+
from src.providers.utils import load_json5_from_file
910
from src.providers.validation.models import PublishType
1011

1112
from .constants import (
@@ -25,23 +26,27 @@ def load_publish_data(publish_type: PublishType):
2526
project_link=adapter["project_link"],
2627
module_name=adapter["module_name"],
2728
): adapter
28-
for adapter in load_json(plugin_config.input_config.adapter_path)
29+
for adapter in load_json5_from_file(
30+
plugin_config.input_config.adapter_path
31+
)
2932
}
3033
case PublishType.BOT:
3134
return {
3235
BOT_KEY_TEMPLATE.format(
3336
name=bot["name"],
3437
homepage=bot["homepage"],
3538
): bot
36-
for bot in load_json(plugin_config.input_config.bot_path)
39+
for bot in load_json5_from_file(plugin_config.input_config.bot_path)
3740
}
3841
case PublishType.PLUGIN:
3942
return {
4043
PYPI_KEY_TEMPLATE.format(
4144
project_link=plugin["project_link"],
4245
module_name=plugin["module_name"],
4346
): plugin
44-
for plugin in load_json(plugin_config.input_config.plugin_path)
47+
for plugin in load_json5_from_file(
48+
plugin_config.input_config.plugin_path
49+
)
4550
}
4651
case PublishType.DRIVER:
4752
raise ValueError("不支持的删除类型")

src/plugins/github/utils.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import json
21
import subprocess
3-
from pathlib import Path
42
from re import Pattern
5-
from typing import Any
63

74
from nonebot import logger
8-
from pydantic_core import to_jsonable_python
95

106

117
def run_shell_command(command: list[str]):
@@ -30,20 +26,6 @@ def commit_message(prefix: str, message: str, issue_number: int):
3026
return f"{prefix} {message} (#{issue_number})"
3127

3228

33-
def load_json(path: Path) -> list[dict[str, str]]:
34-
"""加载 JSON 文件"""
35-
with path.open("r", encoding="utf-8") as f:
36-
return json.load(f)
37-
38-
39-
def dump_json(path: Path, data: Any, indent: int = 4):
40-
"""保存 JSON 文件"""
41-
with path.open("w", encoding="utf-8") as f:
42-
# 结尾加上换行符,不然会被 pre-commit fix
43-
json.dump(to_jsonable_python(data), f, ensure_ascii=False, indent=indent)
44-
f.write("\n")
45-
46-
4729
def extract_issue_info_from_issue(
4830
patterns: dict[str, Pattern[str]], body: str
4931
) -> dict[str, str | None]:

src/providers/constants.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
os.environ.get("STORE_BASE_URL")
2626
or "https://raw.githubusercontent.com/nonebot/nonebot2/master/assets"
2727
)
28-
STORE_ADAPTERS_URL = f"{STORE_BASE_URL}/adapters.json"
29-
STORE_BOTS_URL = f"{STORE_BASE_URL}/bots.json"
30-
STORE_DRIVERS_URL = f"{STORE_BASE_URL}/drivers.json"
31-
STORE_PLUGINS_URL = f"{STORE_BASE_URL}/plugins.json"
28+
STORE_ADAPTERS_URL = f"{STORE_BASE_URL}/adapters.json5"
29+
STORE_BOTS_URL = f"{STORE_BASE_URL}/bots.json5"
30+
STORE_DRIVERS_URL = f"{STORE_BASE_URL}/drivers.json5"
31+
STORE_PLUGINS_URL = f"{STORE_BASE_URL}/plugins.json5"
3232
"""plugin_test.py 中也有一个常量,需要同时修改"""
3333

3434
# 商店测试镜像

src/providers/docker_test/plugin_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
from urllib.request import urlopen
2020

2121
# NoneBot Store
22-
STORE_PLUGINS_URL = (
23-
"https://raw.githubusercontent.com/nonebot/nonebot2/master/assets/plugins.json"
22+
STORE_BASE_URL = (
23+
os.environ.get("STORE_BASE_URL")
24+
or "https://raw.githubusercontent.com/nonebot/nonebot2/master/assets"
2425
)
26+
STORE_PLUGINS_URL = f"{STORE_BASE_URL}/plugins.json5"
2527
# 匹配信息的正则表达式
2628
ISSUE_PATTERN = r"### {}\s+([^\s#].*?)(?=(?:\s+###|$))"
2729

src/providers/store_test/store.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
StorePlugin,
2727
StoreTestResult,
2828
)
29+
from src.providers.utils import dump_json, load_json5_from_web, load_json_from_web
2930
from src.providers.validation.utils import get_author_name
3031

3132
from .constants import (
@@ -36,7 +37,7 @@
3637
PLUGINS_PATH,
3738
RESULTS_PATH,
3839
)
39-
from .utils import dump_json, get_latest_version, load_json
40+
from .utils import get_latest_version
4041
from .validation import validate_plugin
4142

4243
print = click.echo
@@ -52,63 +53,65 @@ def __init__(self) -> None:
5253
project_link=adapter["project_link"],
5354
module_name=adapter["module_name"],
5455
): StoreAdapter(**adapter)
55-
for adapter in load_json(STORE_ADAPTERS_URL)
56+
for adapter in load_json5_from_web(STORE_ADAPTERS_URL)
5657
}
5758
self._store_bots: dict[str, StoreBot] = {
5859
BOT_KEY_TEMPLATE.format(
5960
name=bot["name"],
6061
homepage=bot["homepage"],
6162
): StoreBot(**bot)
62-
for bot in load_json(STORE_BOTS_URL)
63+
for bot in load_json5_from_web(STORE_BOTS_URL)
6364
}
6465
self._store_drivers: dict[str, StoreDriver] = {
6566
PYPI_KEY_TEMPLATE.format(
6667
project_link=driver["project_link"],
6768
module_name=driver["module_name"],
6869
): StoreDriver(**driver)
69-
for driver in load_json(STORE_DRIVERS_URL)
70+
for driver in load_json5_from_web(STORE_DRIVERS_URL)
7071
}
7172
self._store_plugins: dict[str, StorePlugin] = {
7273
PYPI_KEY_TEMPLATE.format(
7374
project_link=plugin["project_link"],
7475
module_name=plugin["module_name"],
7576
): StorePlugin(**plugin)
76-
for plugin in load_json(STORE_PLUGINS_URL)
77+
for plugin in load_json5_from_web(STORE_PLUGINS_URL)
7778
}
7879
# 上次测试的结果
7980
self._previous_results: dict[str, StoreTestResult] = {
8081
key: StoreTestResult(**value)
81-
for key, value in load_json(REGISTRY_RESULTS_URL).items()
82+
for key, value in load_json_from_web(REGISTRY_RESULTS_URL).items()
8283
}
8384
self._previous_adapters: dict[str, RegistryAdapter] = {
8485
PYPI_KEY_TEMPLATE.format(
8586
project_link=adapter["project_link"],
8687
module_name=adapter["module_name"],
8788
): RegistryAdapter(**adapter)
88-
for adapter in load_json(REGISTRY_ADAPTERS_URL)
89+
for adapter in load_json_from_web(REGISTRY_ADAPTERS_URL)
8990
}
9091
self._previous_bots: dict[str, RegistryBot] = {
9192
BOT_KEY_TEMPLATE.format(
9293
name=bot["name"],
9394
homepage=bot["homepage"],
9495
): RegistryBot(**bot)
95-
for bot in load_json(url=REGISTRY_BOTS_URL)
96+
for bot in load_json_from_web(url=REGISTRY_BOTS_URL)
9697
}
9798
self._previous_drivers: dict[str, RegistryDriver] = {
9899
PYPI_KEY_TEMPLATE.format(
99100
project_link=driver["project_link"],
100101
module_name=driver["module_name"],
101102
): RegistryDriver(**driver)
102-
for driver in load_json(REGISTRY_DRIVERS_URL)
103+
for driver in load_json_from_web(REGISTRY_DRIVERS_URL)
103104
}
104105
self._previous_plugins: dict[str, RegistryPlugin] = {
105106
PYPI_KEY_TEMPLATE.format(
106107
project_link=plugin["project_link"], module_name=plugin["module_name"]
107108
): RegistryPlugin(**plugin)
108-
for plugin in load_json(REGISTRY_PLUGINS_URL)
109+
for plugin in load_json_from_web(REGISTRY_PLUGINS_URL)
109110
}
110111
# 插件配置文件
111-
self._plugin_configs: dict[str, str] = load_json(REGISTRY_PLUGIN_CONFIG_URL)
112+
self._plugin_configs: dict[str, str] = load_json_from_web(
113+
REGISTRY_PLUGIN_CONFIG_URL
114+
)
112115

113116
def should_skip(self, key: str, force: bool = False) -> bool:
114117
"""是否跳过测试"""

0 commit comments

Comments
 (0)