forked from AstrBotDevs/AstrBot_Plugins_Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins_map.py
More file actions
34 lines (24 loc) · 1.12 KB
/
plugins_map.py
File metadata and controls
34 lines (24 loc) · 1.12 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
from __future__ import annotations
import json
from pathlib import Path
def validate_plugins_map(data: object, *, source_name: str) -> dict[str, dict]:
if not isinstance(data, dict):
raise ValueError("plugins.json must contain a JSON object")
for name, payload in data.items():
if not isinstance(name, str):
raise ValueError(
f"plugins.json on the {source_name} has a non-string key: {name!r}"
)
if not isinstance(payload, dict):
raise ValueError(
f"plugins.json entry {name!r} on the {source_name} must be a JSON object"
)
return data
def load_plugins_map_text(text: str, *, source_name: str) -> dict[str, dict]:
try:
data = json.loads(text)
except json.JSONDecodeError as exc:
raise ValueError(f"plugins.json is invalid on the {source_name}: {exc}") from exc
return validate_plugins_map(data, source_name=source_name)
def load_plugins_map_file(path: Path, *, source_name: str) -> dict[str, dict]:
return load_plugins_map_text(path.read_text(encoding="utf-8"), source_name=source_name)