mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-22 02:37:57 -05:00
[Downloader] Add schema validation to info.json file processing (#3533)
* schema v1 * set hidden to True for shared libs * fix test data * add warning about invalid top-level structure * don't show full traceback for JSONDecodeError
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Optional, Tuple, Dict, Any
|
||||
from typing import Any, Dict, Tuple
|
||||
|
||||
from .info_schemas import REPO_SCHEMA, update_mixin
|
||||
from .log import log
|
||||
|
||||
|
||||
class RepoJSONMixin:
|
||||
@@ -9,35 +12,36 @@ class RepoJSONMixin:
|
||||
def __init__(self, repo_folder: Path):
|
||||
self._repo_folder = repo_folder
|
||||
|
||||
self.author: Tuple[str, ...] = ()
|
||||
self.install_msg: Optional[str] = None
|
||||
self.short: Optional[str] = None
|
||||
self.description: Optional[str] = None
|
||||
self.author: Tuple[str, ...]
|
||||
self.install_msg: str
|
||||
self.short: str
|
||||
self.description: str
|
||||
|
||||
self._info_file = repo_folder / self.INFO_FILE_NAME
|
||||
if self._info_file.exists():
|
||||
self._read_info_file()
|
||||
self._info: Dict[str, Any]
|
||||
|
||||
self._info: Dict[str, Any] = {}
|
||||
self._read_info_file()
|
||||
|
||||
def _read_info_file(self) -> None:
|
||||
if not (self._info_file.exists() or self._info_file.is_file()):
|
||||
return
|
||||
|
||||
try:
|
||||
with self._info_file.open(encoding="utf-8") as f:
|
||||
info = json.load(f)
|
||||
except json.JSONDecodeError:
|
||||
return
|
||||
if self._info_file.exists():
|
||||
try:
|
||||
with self._info_file.open(encoding="utf-8") as f:
|
||||
info = json.load(f)
|
||||
except json.JSONDecodeError as e:
|
||||
log.error(
|
||||
"Invalid JSON information file at path: %s\nError: %s", self._info_file, str(e)
|
||||
)
|
||||
info = {}
|
||||
else:
|
||||
self._info = info
|
||||
info = {}
|
||||
if not isinstance(info, dict):
|
||||
log.warning(
|
||||
"Invalid top-level structure (expected dict, got %s)"
|
||||
" in JSON information file at path: %s",
|
||||
type(info).__name__,
|
||||
self._info_file,
|
||||
)
|
||||
info = {}
|
||||
self._info = info
|
||||
|
||||
try:
|
||||
author = tuple(info.get("author", []))
|
||||
except ValueError:
|
||||
author = ()
|
||||
self.author = author
|
||||
|
||||
self.install_msg = info.get("install_msg")
|
||||
self.short = info.get("short")
|
||||
self.description = info.get("description")
|
||||
update_mixin(self, REPO_SCHEMA)
|
||||
|
||||
Reference in New Issue
Block a user