Skip to content

Commit 75363df

Browse files
committed
u
1 parent 0c08c03 commit 75363df

File tree

7 files changed

+36
-28
lines changed

7 files changed

+36
-28
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ _✨ NoneBot2 更实用的初始项目新建工具 ✨_
2424

2525
<br />
2626

27+
<a href="https://pydantic.dev">
28+
<img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/lgc-NB2Dev/readme/main/template/pyd-v1-or-v2.json" alt="Pydantic Version 1 Or 2" >
29+
</a>
2730
<a href="./LICENSE">
2831
<img src="https://img.shields.io/github/license/lgc-NB2Dev/nb-cli-plugin-bootstrap.svg" alt="license">
2932
</a>
@@ -104,6 +107,11 @@ Telegram:[@lgc2333](https://t.me/lgc2333)
104107

105108
## 📝 更新日志
106109

110+
### 0.2.0
111+
112+
- 适配 Pydantic V1 & V2
113+
- 可能再次修复了更新所有插件后统计归类不正确的问题
114+
107115
### 0.1.6
108116

109117
- 修复由于下划线横线不统一导致的显示错误

nb_cli_plugin_bootstrap/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.1.6"
1+
__version__ = "0.2.0"

nb_cli_plugin_bootstrap/handlers/bootstrap.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@
1414
project_name_validator,
1515
)
1616
from nb_cli.cli.utils import CLI_DEFAULT_STYLE
17+
from nb_cli.compat import type_validate_python
1718
from nb_cli.config.parser import ConfigManager
1819
from nb_cli.handlers.adapter import list_adapters
1920
from nb_cli.handlers.plugin import list_builtin_plugins
2021
from nb_cli.handlers.process import create_process
2122
from nb_cli.handlers.venv import create_virtualenv
2223
from noneprompt import CheckboxPrompt, Choice, ConfirmPrompt, InputPrompt
2324
from noneprompt.prompts.list import ListPrompt
24-
from pydantic.config import BaseConfig
25-
from pydantic.errors import IPvAnyAddressError
26-
from pydantic.fields import ModelField
27-
from pydantic.networks import AnyHttpUrl, IPvAnyAddress
25+
from pydantic import AnyHttpUrl, BaseModel, IPvAnyAddress, ValidationError
2826

2927
from ..utils import call_pip_no_output, call_pip_update_no_output
3028

@@ -45,26 +43,23 @@
4543

4644

4745
def validate_ip_v_any_addr(addr: str) -> bool:
46+
class ValidateModel(BaseModel):
47+
addr: IPvAnyAddress
48+
4849
try:
49-
IPvAnyAddress.validate(addr)
50-
except IPvAnyAddressError:
50+
type_validate_python(ValidateModel, {"addr": addr})
51+
except ValidationError:
5152
return False
5253
return True
5354

5455

5556
def validate_http_url(url: str) -> bool:
57+
class ValidateModel(BaseModel):
58+
url: AnyHttpUrl
59+
5660
try:
57-
AnyHttpUrl.validate(
58-
url,
59-
ModelField(
60-
name="",
61-
type_=AnyHttpUrl,
62-
class_validators=None,
63-
model_config=BaseConfig,
64-
),
65-
BaseConfig(),
66-
)
67-
except Exception:
61+
type_validate_python(ValidateModel, {"url": url})
62+
except ValidationError:
6863
return False
6964
return True
7065

nb_cli_plugin_bootstrap/handlers/update_project.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
InstallInfoType,
1717
SuccessInstallInfo,
1818
list_all_packages,
19+
normalize_pkg_name,
1920
update_package,
2021
)
2122

@@ -30,7 +31,7 @@ def guess_adapter_pkg_name(module_names: List[str]) -> List[str]:
3031
name = name[LEN_ADAPTER_PKG_PFX:]
3132
name = name.split(".", maxsplit=1)[0]
3233
pkg_names.append(f"nonebot-adapter-{name}")
33-
return pkg_names
34+
return [normalize_pkg_name(x) for x in pkg_names]
3435

3536

3637
def style_change(*change: Optional[str]) -> str:
@@ -73,11 +74,7 @@ async def summary_infos(
7374
for k, v in changed_pkgs.items()
7475
if any(True for x in success_infos if x.name == k)
7576
}
76-
changed_others = {
77-
k: v
78-
for k, v in changed_pkgs.items()
79-
if k.replace("_", "-") not in changed_targets
80-
}
77+
changed_others = {k: v for k, v in changed_pkgs.items() if k not in changed_targets}
8178

8279
info_li: List[str] = []
8380
if unchanged_infos:
@@ -163,7 +160,7 @@ async def update_project_handler(
163160

164161
pkgs = [
165162
*guess_adapter_pkg_name([x.module_name for x in bot_config.adapters]),
166-
*(x.replace("_", "-") for x in bot_config.plugins),
163+
*(normalize_pkg_name(x) for x in bot_config.plugins),
167164
]
168165
if not pkgs:
169166
click.secho("你还没有安装过商店插件或适配器,没有需要更新的包", fg="green")

nb_cli_plugin_bootstrap/plugin.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
from typing import cast
2+
13
import click
2-
from nb_cli.cli import ClickAliasedGroup, cli, run_async
4+
from nb_cli.cli import ClickAliasedGroup, cli as cli_, run_async
35

46
from .handlers.bootstrap import bootstrap_handler
57
from .handlers.update_project import update_project_handler
68

9+
cli = cast(ClickAliasedGroup, cli_)
10+
711

812
@click.group(
913
cls=ClickAliasedGroup,

nb_cli_plugin_bootstrap/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,18 @@ async def call_pip_update_no_output(
9393
)
9494

9595

96+
def normalize_pkg_name(name: str) -> str:
97+
return name.replace("_", "-").lower()
98+
99+
96100
async def list_all_packages(python_path: Optional[str] = None) -> Dict[str, str]:
97101
proc = await call_pip_no_output(["list", "--format=json"], python_path=python_path)
98102
return_code = await proc.wait()
99103
if not return_code == 0:
100104
raise RuntimeError("Failed to execute command `pip list`")
101105
assert proc.stdout
102106
stdout = decode(await proc.stdout.read())
103-
return {x["name"]: x["version"] for x in json.loads(stdout)}
107+
return {normalize_pkg_name(x["name"]): x["version"] for x in json.loads(stdout)}
104108

105109

106110
async def update_package(

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "nb-cli-plugin-bootstrap"
33
dynamic = ["version"]
44
description = "A nb-cli plugin for quickly create and bootstrap a NoneBot2 project."
55
authors = [{ name = "student_2333", email = "lgc2333@126.com" }]
6-
dependencies = ["nb-cli>=1.2.7"]
6+
dependencies = ["nb-cli>=1.4.0"]
77
requires-python = ">=3.9,<4.0"
88
readme = "README.md"
99
license = { text = "MIT" }

0 commit comments

Comments
 (0)