Skip to content

Commit 8948a6d

Browse files
committed
✨ allow load plugin config in dirs by $files
1 parent 436fcf4 commit 8948a6d

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

Diff for: arclet/entari/config.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,36 @@ def __post_init__(self):
2929
self.__class__.instance = self
3030
self.reload()
3131

32+
@staticmethod
33+
def _load_plugin(path: Path):
34+
if path.suffix.startswith(".json"):
35+
with path.open("r", encoding="utf-8") as f:
36+
return json.load(f)
37+
if path.suffix in (".yaml", ".yml"):
38+
try:
39+
import yaml
40+
except ImportError:
41+
raise RuntimeError("yaml is not installed")
42+
43+
with path.open("r", encoding="utf-8") as f:
44+
return yaml.safe_load(f)
45+
raise NotImplementedError(f"unsupported plugin config file format: {path!s}")
46+
3247
def reload(self):
3348
self.updater(self)
49+
plugin_files: list[str] = self.plugin.pop("$files", []) # type: ignore
50+
for file in plugin_files:
51+
path = Path(file)
52+
if not path.exists():
53+
raise FileNotFoundError(file)
54+
if path.is_dir():
55+
for _path in path.iterdir():
56+
if not _path.is_file():
57+
continue
58+
self.plugin[_path.stem] = self._load_plugin(_path)
59+
else:
60+
self.plugin[path.stem] = self._load_plugin(path)
61+
3462
self.plugin.setdefault(".commands", {})
3563
self.prelude_plugin = self.plugin.pop("$prelude", []) # type: ignore
3664
disabled = []
@@ -91,7 +119,7 @@ def _updater(self: EntariConfig):
91119
self.plugin = data.get("plugins", {})
92120

93121
return cls(_path, _updater)
94-
raise NotImplementedError(f"unsupported config file format: {_path.suffix}")
122+
raise NotImplementedError(f"unsupported config file format: {_path!s}")
95123

96124

97125
load_config = EntariConfig.load

0 commit comments

Comments
 (0)