Skip to content

Commit

Permalink
Optimize Trivia list loading (#6336)
Browse files Browse the repository at this point in the history
Co-authored-by: Kreusada <67752638+Kreusada@users.noreply.github.com>
  • Loading branch information
Jackenmen and Kreusada authored Oct 29, 2024
1 parent 4396323 commit 4134881
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions redbot/cogs/trivia/trivia.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

UNIQUE_ID = 0xB3C0E453
_ = Translator("Trivia", __file__)
YAMLSafeLoader = getattr(yaml, "CSafeLoader", yaml.SafeLoader)


class InvalidListError(Exception):
Expand Down Expand Up @@ -759,7 +760,7 @@ async def _save_trivia_list(
return

buffer = io.BytesIO(await attachment.read())
trivia_dict = yaml.safe_load(buffer)
trivia_dict = yaml.load(buffer, YAMLSafeLoader)
TRIVIA_LIST_SCHEMA.validate(trivia_dict)

buffer.seek(0)
Expand Down Expand Up @@ -803,7 +804,7 @@ def get_core_lists() -> List[pathlib.Path]:
return list(core_lists_path.glob("*.yaml"))


def get_list(path: pathlib.Path) -> Dict[str, Any]:
def get_list(path: pathlib.Path, *, validate_schema: bool = True) -> Dict[str, Any]:
"""
Returns a trivia list dictionary from the given path.
Expand All @@ -814,12 +815,14 @@ def get_list(path: pathlib.Path) -> Dict[str, Any]:
"""
with path.open(encoding="utf-8") as file:
try:
trivia_dict = yaml.safe_load(file)
trivia_dict = yaml.load(file, YAMLSafeLoader)
except yaml.error.YAMLError as exc:
raise InvalidListError("YAML parsing failed.") from exc

try:
TRIVIA_LIST_SCHEMA.validate(trivia_dict)
except schema.SchemaError as exc:
raise InvalidListError("The list does not adhere to the schema.") from exc
if validate_schema:
try:
TRIVIA_LIST_SCHEMA.validate(trivia_dict)
except schema.SchemaError as exc:
raise InvalidListError("The list does not adhere to the schema.") from exc

return trivia_dict

0 comments on commit 4134881

Please # to comment.