Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
lgc2333 committed Mar 5, 2024
1 parent 0c08c03 commit 75363df
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 28 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ _✨ NoneBot2 更实用的初始项目新建工具 ✨_

<br />

<a href="https://pydantic.dev">
<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" >
</a>
<a href="./LICENSE">
<img src="https://img.shields.io/github/license/lgc-NB2Dev/nb-cli-plugin-bootstrap.svg" alt="license">
</a>
Expand Down Expand Up @@ -104,6 +107,11 @@ Telegram:[@lgc2333](https://t.me/lgc2333)

## 📝 更新日志

### 0.2.0

- 适配 Pydantic V1 & V2
- 可能再次修复了更新所有插件后统计归类不正确的问题

### 0.1.6

- 修复由于下划线横线不统一导致的显示错误
Expand Down
2 changes: 1 addition & 1 deletion nb_cli_plugin_bootstrap/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.6"
__version__ = "0.2.0"
29 changes: 12 additions & 17 deletions nb_cli_plugin_bootstrap/handlers/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
project_name_validator,
)
from nb_cli.cli.utils import CLI_DEFAULT_STYLE
from nb_cli.compat import type_validate_python
from nb_cli.config.parser import ConfigManager
from nb_cli.handlers.adapter import list_adapters
from nb_cli.handlers.plugin import list_builtin_plugins
from nb_cli.handlers.process import create_process
from nb_cli.handlers.venv import create_virtualenv
from noneprompt import CheckboxPrompt, Choice, ConfirmPrompt, InputPrompt
from noneprompt.prompts.list import ListPrompt
from pydantic.config import BaseConfig
from pydantic.errors import IPvAnyAddressError
from pydantic.fields import ModelField
from pydantic.networks import AnyHttpUrl, IPvAnyAddress
from pydantic import AnyHttpUrl, BaseModel, IPvAnyAddress, ValidationError

from ..utils import call_pip_no_output, call_pip_update_no_output

Expand All @@ -45,26 +43,23 @@


def validate_ip_v_any_addr(addr: str) -> bool:
class ValidateModel(BaseModel):
addr: IPvAnyAddress

try:
IPvAnyAddress.validate(addr)
except IPvAnyAddressError:
type_validate_python(ValidateModel, {"addr": addr})
except ValidationError:
return False
return True


def validate_http_url(url: str) -> bool:
class ValidateModel(BaseModel):
url: AnyHttpUrl

try:
AnyHttpUrl.validate(
url,
ModelField(
name="",
type_=AnyHttpUrl,
class_validators=None,
model_config=BaseConfig,
),
BaseConfig(),
)
except Exception:
type_validate_python(ValidateModel, {"url": url})
except ValidationError:
return False
return True

Expand Down
11 changes: 4 additions & 7 deletions nb_cli_plugin_bootstrap/handlers/update_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
InstallInfoType,
SuccessInstallInfo,
list_all_packages,
normalize_pkg_name,
update_package,
)

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


def style_change(*change: Optional[str]) -> str:
Expand Down Expand Up @@ -73,11 +74,7 @@ async def summary_infos(
for k, v in changed_pkgs.items()
if any(True for x in success_infos if x.name == k)
}
changed_others = {
k: v
for k, v in changed_pkgs.items()
if k.replace("_", "-") not in changed_targets
}
changed_others = {k: v for k, v in changed_pkgs.items() if k not in changed_targets}

info_li: List[str] = []
if unchanged_infos:
Expand Down Expand Up @@ -163,7 +160,7 @@ async def update_project_handler(

pkgs = [
*guess_adapter_pkg_name([x.module_name for x in bot_config.adapters]),
*(x.replace("_", "-") for x in bot_config.plugins),
*(normalize_pkg_name(x) for x in bot_config.plugins),
]
if not pkgs:
click.secho("你还没有安装过商店插件或适配器,没有需要更新的包", fg="green")
Expand Down
6 changes: 5 additions & 1 deletion nb_cli_plugin_bootstrap/plugin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from typing import cast

import click
from nb_cli.cli import ClickAliasedGroup, cli, run_async
from nb_cli.cli import ClickAliasedGroup, cli as cli_, run_async

from .handlers.bootstrap import bootstrap_handler
from .handlers.update_project import update_project_handler

cli = cast(ClickAliasedGroup, cli_)


@click.group(
cls=ClickAliasedGroup,
Expand Down
6 changes: 5 additions & 1 deletion nb_cli_plugin_bootstrap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,18 @@ async def call_pip_update_no_output(
)


def normalize_pkg_name(name: str) -> str:
return name.replace("_", "-").lower()


async def list_all_packages(python_path: Optional[str] = None) -> Dict[str, str]:
proc = await call_pip_no_output(["list", "--format=json"], python_path=python_path)
return_code = await proc.wait()
if not return_code == 0:
raise RuntimeError("Failed to execute command `pip list`")
assert proc.stdout
stdout = decode(await proc.stdout.read())
return {x["name"]: x["version"] for x in json.loads(stdout)}
return {normalize_pkg_name(x["name"]): x["version"] for x in json.loads(stdout)}


async def update_package(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "nb-cli-plugin-bootstrap"
dynamic = ["version"]
description = "A nb-cli plugin for quickly create and bootstrap a NoneBot2 project."
authors = [{ name = "student_2333", email = "[email protected]" }]
dependencies = ["nb-cli>=1.2.7"]
dependencies = ["nb-cli>=1.4.0"]
requires-python = ">=3.9,<4.0"
readme = "README.md"
license = { text = "MIT" }
Expand Down

0 comments on commit 75363df

Please sign in to comment.