Skip to content

Commit

Permalink
entari new generate dev config
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Dec 17, 2024
1 parent b165730 commit b461019
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
43 changes: 38 additions & 5 deletions arclet/entari/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

alc = Alconna(
"entari",
Subcommand("new", help_text="新建一个 Entari 配置文件"),
Subcommand("new", Option("--dev", help_text="是否生成开发用配置文件"), help_text="新建一个 Entari 配置文件"),
Subcommand("run", help_text="运行 Entari"),
Option("-c|--config", Args["path/", str], help_text="指定配置文件路径"),
meta=CommandMeta(
Expand All @@ -18,7 +18,7 @@
)


JSON_TEMPLATE = """\
JSON_BASIC_TEMPLATE = """\
{
"basic": {
"network": [
Expand All @@ -33,6 +33,9 @@
"log_level": "info",
"prefix": ["/"]
},
"""

JSON_PLUGIN_COMMON_TEMPLATE = """\
"plugins": {
"~record_message": {},
"::echo": {},
Expand All @@ -41,8 +44,23 @@
}
"""

JSON_PLUGIN_DEV_TEMPLATE = """\
"plugins": {
"$prelude": [
"::auto_reload"
],
"~record_message": {},
"::echo": {},
"::inspect": {},
"::auto_reload": {
"watch_config": true
}
}
}
"""


YAML_TEMPLATE = """\
YAML_BASIC_TEMPLATE = """\
basic:
network:
- type: websocket
Expand All @@ -52,10 +70,24 @@
ignore_self_message: true
log_level: "info"
prefix: ["/"]
"""

YAML_PLUGIN_COMMON_TEMPLATE = """\
plugins:
.record_message: {}
::echo: {}
::inspect: {}
"""

YAML_PLUGIN_DEV_TEMPLATE = """\
plugins:
$prelude:
- ::auto_reload
.record_message: {}
::echo: {}
::inspect: {}
::auto_reload:
watch_config: true
"""


Expand All @@ -65,6 +97,7 @@ def main():
print(alc.get_help())
return
if res.find("new"):
is_dev = res.find("new.dev")
if (path := res.query[str]("config.path", None)) is None:
if find_spec("yaml"):
_path = Path.cwd() / "entari.yml"
Expand All @@ -77,12 +110,12 @@ def main():
return
if _path.suffix.startswith(".json"):
with _path.open("w", encoding="utf-8") as f:
f.write(JSON_TEMPLATE)
f.write(JSON_BASIC_TEMPLATE + (JSON_PLUGIN_DEV_TEMPLATE if is_dev else JSON_PLUGIN_COMMON_TEMPLATE))
print(f"Config file created at {_path}")
return
if _path.suffix in (".yaml", ".yml"):
with _path.open("w", encoding="utf-8") as f:
f.write(YAML_TEMPLATE)
f.write(YAML_BASIC_TEMPLATE + (YAML_PLUGIN_DEV_TEMPLATE if is_dev else YAML_PLUGIN_COMMON_TEMPLATE))
print(f"Config file created at {_path}")
return
print(f"Unsupported file extension: {_path.suffix}")
Expand Down
5 changes: 4 additions & 1 deletion arclet/entari/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ def declare_static():


def find_plugin(name: str) -> Plugin | None:
return plugin_service.plugins.get(name)
if name in plugin_service.plugins:
return plugin_service.plugins[name]
if f"entari_plugin_{name}" in plugin_service.plugins:
return plugin_service.plugins[f"entari_plugin_{name}"]


def find_plugin_by_file(file: str) -> Plugin | None:
Expand Down

0 comments on commit b461019

Please sign in to comment.