Skip to content

Commit a4b9df3

Browse files
committed
✨ support requires with module val
1 parent c684699 commit a4b9df3

6 files changed

Lines changed: 57 additions & 20 deletions

File tree

‎arclet/entari/exceptions.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ def __init__(self, func, mod, pid):
55
66
Please choose one of the following solutions before import it:
77
* add {func.__module__!r} to your config file.
8+
* rename {func.__module__!r} to 'entari_plugin_{func.__module__!s}'
89
* write the comment after the import statement line: `# entari: plugin`
910
* append `load_plugin({func.__module__!r})` before the import statement.
1011
* call `requires({func.__module__!r})` in the {mod.__name__!r}.
@@ -22,6 +23,7 @@ def __init__(self, func, mod, pid):
2223
2324
Please choose one of the following solutions:
2425
[magenta] *[/] add [bright_green]{func.__module__!r}[/] to your config file.
26+
[magenta] *[/] rename [red]{func.__module__!r}[/] to [bright_green]'entari_plugin_{func.__module__!s}'[/]
2527
[magenta] *[/] write the comment after the import statement line: [white]`# entari: plugin`[/]
2628
[magenta] *[/] append [bright_yellow]`load_plugin({func.__module__!r})`[/] [u bright_white]before the import stmt[/].
2729
[magenta] *[/] call [bright_yellow]`requires({func.__module__!r})`[/] in the [blue]{mod.__name__!r}[/].

‎arclet/entari/plugin/model.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
publish,
2424
)
2525
from arclet.letoderea.breakpoint import StepOut, step_out
26+
from arclet.letoderea.core import add_task
2627
from arclet.letoderea.provider import Provider, ProviderFactory, TProviders
2728
from arclet.letoderea.publisher import Publisher, _publishers
2829
from arclet.letoderea.scope import RegisterWrapper
@@ -283,7 +284,7 @@ def enable(self):
283284
plugin_service.service_waiter.assign(serv.id)
284285
try:
285286
it(Launart).add_component(serv)
286-
t = asyncio.create_task(serv.status.wait_for("prepared"))
287+
t = add_task(serv.status.wait_for("prepared"))
287288
t.add_done_callback(tasks.discard)
288289
tasks.add(t)
289290
except ValueError:
@@ -358,15 +359,15 @@ def __post_init__(self):
358359
def _clean_service(self):
359360
manager = it(Launart)
360361

361-
def _gen(service):
362+
def _gen(service: Service):
362363
for serv in manager.components.values():
363364
if service.id in serv.required:
364365
yield from _gen(serv)
365366
yield service
366367

367368
_services = [s for serv in self._services.values() for s in _gen(serv)]
368369

369-
async def _clean(services):
370+
async def _clean(services: list[Service]):
370371
if not manager.task_group:
371372
return
372373
for serv in services:
@@ -376,11 +377,11 @@ async def _clean(services):
376377
try:
377378
tracker = manager.task_group.sideload_trackers[serv.id]
378379
manager.remove_component(serv)
379-
await asyncio.wait([tracker, asyncio.create_task(serv.status.wait_for("finished"))])
380+
await asyncio.wait([tracker, add_task(serv.status.wait_for("finished"))])
380381
except (ValueError, KeyError):
381382
pass
382383

383-
return asyncio.create_task(_clean(_services))
384+
return add_task(_clean(_services))
384385

385386
def dispose(self, *, is_cleanup: bool = False):
386387
if not is_cleanup and self.is_static:

‎arclet/entari/plugin/module.py‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,20 @@
3030
NAMESPACE_PAT = re.compile(r"entari:\s*namespace")
3131

3232

33-
def package(*names: str):
33+
def package(*names: str | ModuleType):
3434
"""手动指定特定模块作为插件的子模块"""
3535
if not (plugin := current_plugin.get(None)):
3636
raise LookupError("no plugin context found")
37-
_SUBMODULE_WAITLIST.setdefault(plugin.module.__name__, set()).update(names)
37+
_SUBMODULE_WAITLIST.setdefault(plugin.module.__name__, set()).update(
38+
[n if isinstance(n, str) else n.__name__ for n in names]
39+
)
3840

3941

40-
def requires(*names: str):
42+
def requires(*names: str | ModuleType):
4143
"""手动指定哪些模块是插件"""
42-
_ENSURE_IS_PLUGIN.update(name.replace("::", "arclet.entari.builtins.") for name in names)
44+
_ENSURE_IS_PLUGIN.update(
45+
n.replace("::", "arclet.entari.builtins.") if isinstance(n, str) else n.__name__ for n in names
46+
)
4347

4448

4549
def _ensure_plugin(names: list[str], sub: bool, pid: str, pname: str, prefix=""):
@@ -129,6 +133,12 @@ def visit_Call(self, node):
129133
for arg in node.args:
130134
if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
131135
names.append(arg.value)
136+
elif isinstance(arg, ast.Name):
137+
names.append(arg.id)
138+
elif isinstance(arg, ast.Attribute):
139+
value = arg.value
140+
if isinstance(value, ast.Name):
141+
names.append(f"{value.id}.{arg.attr}")
132142
if names:
133143
_ensure_plugin(names, False, self.pid, self.pname)
134144
elif isinstance(node.func, ast.Name) and node.func.id == "package":

‎example.schema.json‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,6 @@
353353
"description": "Echo the content; no configuration required",
354354
"additionalProperties": true
355355
},
356-
"example_plugin": {
357-
"type": "object",
358-
"description": "D:\\Projects\\Entari\\example_plugin.py; no configuration required",
359-
"additionalProperties": true
360-
},
361356
".record_message": {
362357
"type": "object",
363358
"title": "RecordConfig",
@@ -423,6 +418,16 @@
423418
"additionalProperties": false,
424419
"description": "example_reusable"
425420
},
421+
"example_plugin2": {
422+
"type": "object",
423+
"description": "D:\\Projects\\Entari\\example_plugins\\example_plugin2\\__init__.py; no configuration required",
424+
"additionalProperties": true
425+
},
426+
"example_plugin": {
427+
"type": "object",
428+
"description": "D:\\Projects\\Entari\\example_plugin.py; no configuration required",
429+
"additionalProperties": true
430+
},
426431
"example_plugin6": {
427432
"type": "object",
428433
"title": "Config",
@@ -499,6 +504,24 @@
499504
"description": "Data store interface with local directories"
500505
}
501506
}
507+
},
508+
"adapters": {
509+
"type": "array",
510+
"description": "Adapter configurations",
511+
"items": {
512+
"type": "object",
513+
"description": "Adapter configuration",
514+
"properties": {
515+
"$path": {
516+
"type": "string",
517+
"description": "Adapter Module Path"
518+
}
519+
},
520+
"required": [
521+
"$path"
522+
],
523+
"additionalProperties": true
524+
}
502525
}
503526
},
504527
"additionalProperties": false,

‎example.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ basic:
77
path: "satori"
88
ignore_self_message: true
99
log:
10-
level: "trace"
10+
level: "debug"
1111
rich_error: false
1212
external_dirs:
1313
- example_plugins
@@ -17,16 +17,16 @@ plugins:
1717
::auto_reload:
1818
watch_dirs: ["."]
1919
::echo: {}
20-
example_plugin: {}
2120
.record_message: {}
2221
.scheduler: {}
22+
?example_plugin: {}
2323
example_reusable@1:
2424
input: hello
2525
output: world!
2626
example_reusable@2:
2727
input: foo
2828
output: bar!
29-
?example_plugin2: {}
29+
example_plugin2: {}
3030
?example_plugin3: {}
3131
?example_plugin4: {}
3232
?example_plugin5: {}

‎example_plugins/example_plugin2/__init__.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
from arclet.entari.event.lifespan import Startup
33
from . import command
44

5+
import example_plugin
56
from example_plugin import kept_data
67

78
metadata(__file__)
8-
requires("example_plugin")
9+
requires(example_plugin)
910

1011

1112
@listen(Startup)
12-
@inject({"id": "entari.plugin.auto_reload/watcher", "stage": "blocking"})
13+
@inject({"id": "entari.plugin.auto_reload/watcher", "stage": "blocking"}) # type: ignore
1314
async def startup():
14-
print(111111111111111111111111111111111111111111)
15+
print(kept_data)
1516

1617

1718
# from example_reusable import conf # entari: plugin

0 commit comments

Comments
 (0)