Skip to content

Commit

Permalink
✨ allow load plugin config in dirs by $files
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Dec 28, 2024
1 parent 436fcf4 commit 8948a6d
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion arclet/entari/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,36 @@ def __post_init__(self):
self.__class__.instance = self
self.reload()

@staticmethod
def _load_plugin(path: Path):
if path.suffix.startswith(".json"):
with path.open("r", encoding="utf-8") as f:
return json.load(f)
if path.suffix in (".yaml", ".yml"):
try:
import yaml
except ImportError:
raise RuntimeError("yaml is not installed")

with path.open("r", encoding="utf-8") as f:
return yaml.safe_load(f)
raise NotImplementedError(f"unsupported plugin config file format: {path!s}")

def reload(self):
self.updater(self)
plugin_files: list[str] = self.plugin.pop("$files", []) # type: ignore
for file in plugin_files:
path = Path(file)
if not path.exists():
raise FileNotFoundError(file)
if path.is_dir():
for _path in path.iterdir():
if not _path.is_file():
continue
self.plugin[_path.stem] = self._load_plugin(_path)
else:
self.plugin[path.stem] = self._load_plugin(path)

self.plugin.setdefault(".commands", {})
self.prelude_plugin = self.plugin.pop("$prelude", []) # type: ignore
disabled = []
Expand Down Expand Up @@ -91,7 +119,7 @@ def _updater(self: EntariConfig):
self.plugin = data.get("plugins", {})

return cls(_path, _updater)
raise NotImplementedError(f"unsupported config file format: {_path.suffix}")
raise NotImplementedError(f"unsupported config file format: {_path!s}")


load_config = EntariConfig.load

0 comments on commit 8948a6d

Please sign in to comment.