-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathproject.py
More file actions
197 lines (162 loc) · 5.81 KB
/
project.py
File metadata and controls
197 lines (162 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import asyncio
from pathlib import Path
from typing import IO, Any, TypeVar
from collections.abc import Iterable
import click
from cookiecutter.main import cookiecutter
from nb_cli import _
from nb_cli.config import SimpleInfo, PackageInfo, NoneBotConfig, LegacyNoneBotConfig
from . import templates
from .driver import list_drivers
from .plugin import list_plugins
from .adapter import list_adapters
from .process import create_process
from .meta import (
get_project_root,
requires_nonebot,
get_config_manager,
get_default_python,
get_nonebot_config,
requires_project_root,
)
TEMPLATE_ROOT = Path(__file__).parent.parent / "template" / "project"
T_info = TypeVar("T_info", bound=PackageInfo)
def list_project_templates() -> list[str]:
return sorted(t.name for t in (TEMPLATE_ROOT).iterdir())
def create_project(
project_template: str,
context: dict[str, Any] | None = None,
output_dir: str | None = None,
no_input: bool = True,
) -> None:
path = TEMPLATE_ROOT / project_template
path = str(path.resolve()) if path.exists() else project_template
cookiecutter(
path,
no_input=no_input,
extra_context=context,
output_dir=output_dir or ".",
overwrite_if_exists=True,
)
async def generate_run_script(
adapters: list[SimpleInfo] | None = None,
builtin_plugins: list[str] | None = None,
) -> str:
# only read global config when no data provided
if adapters is None or builtin_plugins is None:
bot_config = get_nonebot_config()
if adapters is None:
adapters = bot_config.get_adapters()
if builtin_plugins is None:
builtin_plugins = bot_config.builtin_plugins
t = templates.get_template("project/run_project.py.jinja")
return await t.render_async(adapters=adapters, builtin_plugins=builtin_plugins)
@requires_project_root
@requires_nonebot
async def run_project(
adapters: list[SimpleInfo] | None = None,
builtin_plugins: list[str] | None = None,
exist_bot: Path = Path("bot.py"),
*,
python_path: str | None = None,
cwd: Path | None = None,
stdin: IO[Any] | int | None = None,
stdout: IO[Any] | int | None = None,
stderr: IO[Any] | int | None = None,
) -> asyncio.subprocess.Process:
# only read global config when no data provided
if adapters is None or builtin_plugins is None:
bot_config = get_nonebot_config()
if adapters is None:
adapters = bot_config.get_adapters()
if builtin_plugins is None:
builtin_plugins = bot_config.builtin_plugins
if python_path is None:
python_path = await get_default_python()
if cwd is None:
cwd = get_project_root()
if cwd.joinpath(exist_bot).exists():
return await create_process(
python_path,
exist_bot,
cwd=cwd,
stdin=stdin,
stdout=stdout,
stderr=stderr,
)
return await create_process(
python_path,
"-c",
await generate_run_script(adapters=adapters, builtin_plugins=builtin_plugins),
cwd=cwd,
stdin=stdin,
stdout=stdout,
stderr=stderr,
)
def _index_by_module_name(data: Iterable[T_info]) -> dict[str, T_info]:
res: dict[str, T_info] = {}
for d in data:
res[d.module_name] = d
return res
@requires_project_root
async def upgrade_project_format(*, cwd: Path | None = None) -> None:
bot_config = get_nonebot_config(cwd)
if isinstance(bot_config, NoneBotConfig):
click.echo(_("Current format is already the new format."))
return
all_adapters = _index_by_module_name(await list_adapters())
all_plugins = _index_by_module_name(await list_plugins())
nonebot_pkg = next(iter(await list_drivers("~none"))).model_copy()
nonebot_pkg.project_link = "nonebot2"
new_adapters: dict[str, list[SimpleInfo]] = {"@local": []}
new_plugins: dict[str, list[str]] = {"@local": []}
packages: list[PackageInfo] = []
for a in bot_config.adapters:
if a.module_name in all_adapters:
adapter = all_adapters[a.module_name]
packages.append(adapter)
info = SimpleInfo(name=adapter.name, module_name=adapter.module_name)
if adapter.name != a.name:
click.secho(
_("WARNING: Inconsistent adapter name info: {old!r} -> {new!r}"),
fg="yellow",
)
else:
info = a
new_adapters.setdefault(
(
all_adapters[a.module_name].project_link
if a.module_name in all_adapters
else "@local"
),
[],
).append(info)
for p in bot_config.plugins:
if p in all_plugins:
packages.append(all_plugins[p])
new_plugins.setdefault(
(all_plugins[p].project_link if p in all_plugins else "@local"), []
).append(p)
new_config = NoneBotConfig(
adapters=new_adapters,
plugins=new_plugins,
plugin_dirs=bot_config.plugin_dirs,
builtin_plugins=bot_config.builtin_plugins,
)
manager = get_config_manager(cwd)
manager.update_nonebot_config(new_config)
manager.update_dependency(nonebot_pkg, *packages)
@requires_project_root
async def downgrade_project_format(*, cwd: Path | None = None) -> None:
bot_config = get_nonebot_config(cwd)
if isinstance(bot_config, LegacyNoneBotConfig):
click.echo(_("Current format is already the old format."))
return
old_config = LegacyNoneBotConfig(
adapters=bot_config.get_adapters(),
plugins=bot_config.get_plugins(),
plugin_dirs=bot_config.plugin_dirs,
builtin_plugins=bot_config.builtin_plugins,
)
manager = get_config_manager(cwd)
manager.update_nonebot_config(old_config)