-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathdriver.py
More file actions
245 lines (219 loc) · 7.15 KB
/
driver.py
File metadata and controls
245 lines (219 loc) · 7.15 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from typing import cast
import click
from packaging.requirements import Requirement
from noneprompt import Choice, ListPrompt, InputPrompt, CancelledError
from nb_cli import _
from nb_cli.exceptions import ProcessExecutionError
from nb_cli.handlers import EnvironmentExecutor, list_drivers
from nb_cli.cli.utils import find_exact_package, format_package_results
from nb_cli.cli import (
CLI_DEFAULT_STYLE,
ClickAliasedGroup,
back_,
exit_,
run_sync,
run_async,
)
@click.group(
cls=ClickAliasedGroup, invoke_without_command=True, help=_("Manage bot driver.")
)
@click.pass_context
@run_async
async def driver(ctx: click.Context):
if ctx.invoked_subcommand is not None:
return
command = cast(ClickAliasedGroup, ctx.command)
choices: list[Choice[click.Command]] = []
for sub_cmd_name in await run_sync(command.list_commands)(ctx):
if sub_cmd := await run_sync(command.get_command)(ctx, sub_cmd_name):
choices.append(
Choice(
sub_cmd.help
or _("Run subcommand {sub_cmd.name!r}").format(sub_cmd=sub_cmd),
sub_cmd,
)
)
if ctx.parent and ctx.parent.params.get("can_back_to_parent", False):
_exit_choice = Choice(_("Back to top level."), back_)
else:
_exit_choice = Choice(_("Exit NB CLI."), exit_)
choices.append(_exit_choice)
while True:
try:
result = await ListPrompt(
_("What do you want to do?"), choices=choices
).prompt_async(style=CLI_DEFAULT_STYLE)
except CancelledError:
result = _exit_choice
sub_cmd = result.data
if sub_cmd == back_:
return
ctx.params["can_back_to_parent"] = True
await run_sync(ctx.invoke)(sub_cmd)
@driver.command(help=_("Open nonebot driver store."))
@run_async
async def store():
from nb_cli.tui import Gallery
driver_store = Gallery()
driver_store.datasource = await list_drivers()
driver_store.title = _("NB-CLI - NoneBot Driver Store")
await driver_store.run_async()
@driver.command(
name="list", help=_("List nonebot drivers published on nonebot homepage.")
)
@click.option(
"--include-unpublished",
is_flag=True,
default=False,
flag_value=True,
help=_("Whether to include unpublished drivers."),
)
@run_async
async def list_(include_unpublished: bool = False):
drivers = await list_drivers(include_unpublished=include_unpublished)
if include_unpublished:
click.secho(_("WARNING: Unpublished drivers may be included."), fg="yellow")
click.echo(format_package_results(drivers))
@driver.command(help=_("Search for nonebot drivers published on nonebot homepage."))
@click.option(
"--include-unpublished",
is_flag=True,
default=False,
flag_value=True,
help=_("Whether to include unpublished drivers."),
)
@click.argument("name", nargs=1, default=None)
@run_async
async def search(name: str | None, include_unpublished: bool = False):
if name is None:
name = await InputPrompt(_("Driver name to search:")).prompt_async(
style=CLI_DEFAULT_STYLE
)
drivers = await list_drivers(name, include_unpublished=include_unpublished)
if include_unpublished:
click.secho(_("WARNING: Unpublished drivers may be included."), fg="yellow")
click.echo(format_package_results(drivers))
@driver.command(
aliases=["add"],
context_settings={"ignore_unknown_options": True},
help=_("Install nonebot driver to current project."),
)
@click.option(
"--include-unpublished",
is_flag=True,
default=False,
flag_value=True,
help=_("Whether to include unpublished drivers."),
)
@click.argument("name", nargs=1, default=None)
@click.argument("pip_args", nargs=-1, default=None)
@run_async
async def install(
name: str | None,
pip_args: list[str] | None,
include_unpublished: bool = False,
):
try:
driver = await find_exact_package(
_("Driver name to install:"),
name,
await list_drivers(include_unpublished=include_unpublished),
no_extras=True,
)
except CancelledError:
return
if include_unpublished:
click.secho(
_(
"WARNING: Unpublished drivers may be installed. "
"These drivers may be unmaintained or unusable."
),
fg="yellow",
)
executor = await EnvironmentExecutor.get()
try:
await executor.install(
driver.as_requirement() if driver.project_link else Requirement("nonebot2"),
extra_args=pip_args or (),
)
except ProcessExecutionError:
click.secho(
_("Errors occurred in installing driver {driver.name}. Aborted.").format(
driver=driver
),
fg="red",
)
return
@driver.command(
context_settings={"ignore_unknown_options": True}, help=_("Update nonebot driver.")
)
@click.option(
"--include-unpublished",
is_flag=True,
default=False,
flag_value=True,
help=_("Whether to include unpublished drivers."),
)
@click.argument("name", nargs=1, default=None)
@click.argument("pip_args", nargs=-1, default=None)
@run_async
async def update(
name: str | None,
pip_args: list[str] | None,
include_unpublished: bool = False,
):
try:
driver = await find_exact_package(
_("Driver name to update:"),
name,
await list_drivers(include_unpublished=include_unpublished),
)
except CancelledError:
return
if include_unpublished:
click.secho(
_(
"WARNING: Unpublished drivers may be installed. "
"These drivers may be unmaintained or unusable."
),
fg="yellow",
)
executor = await EnvironmentExecutor.get()
try:
await executor.install(
driver.as_requirement() if driver.project_link else Requirement("nonebot2"),
extra_args=pip_args or (),
)
except ProcessExecutionError:
click.secho(
_("Errors occurred in updating driver {driver.name}. Aborted.").format(
driver=driver
),
fg="red",
)
return
@driver.command(
aliases=["remove"],
context_settings={"ignore_unknown_options": True},
help=_("Uninstall nonebot driver from current project."),
)
@click.argument("name", nargs=1, default=None)
@click.argument("pip_args", nargs=-1, default=None)
@run_async
async def uninstall(name: str | None, pip_args: list[str] | None):
try:
driver = await find_exact_package(
_("Driver name to uninstall:"),
name,
await list_drivers(
include_unpublished=True # unpublished modules are always removable
),
)
except CancelledError:
return
if driver.project_link:
executor = await EnvironmentExecutor.get()
await executor.uninstall(
driver.as_requirement(versioned=False), extra_args=pip_args or ()
)
await executor.install(Requirement("nonebot2"), extra_args=pip_args or ())