mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
Optimize Trivia list loading (#6336)
Co-authored-by: Kreusada <67752638+Kreusada@users.noreply.github.com>
This commit is contained in:
parent
4396323205
commit
4134881fae
@ -29,6 +29,7 @@ __all__ = ("Trivia", "UNIQUE_ID", "InvalidListError", "get_core_lists", "get_lis
|
|||||||
|
|
||||||
UNIQUE_ID = 0xB3C0E453
|
UNIQUE_ID = 0xB3C0E453
|
||||||
_ = Translator("Trivia", __file__)
|
_ = Translator("Trivia", __file__)
|
||||||
|
YAMLSafeLoader = getattr(yaml, "CSafeLoader", yaml.SafeLoader)
|
||||||
|
|
||||||
|
|
||||||
class InvalidListError(Exception):
|
class InvalidListError(Exception):
|
||||||
@ -759,7 +760,7 @@ class Trivia(commands.Cog):
|
|||||||
return
|
return
|
||||||
|
|
||||||
buffer = io.BytesIO(await attachment.read())
|
buffer = io.BytesIO(await attachment.read())
|
||||||
trivia_dict = yaml.safe_load(buffer)
|
trivia_dict = yaml.load(buffer, YAMLSafeLoader)
|
||||||
TRIVIA_LIST_SCHEMA.validate(trivia_dict)
|
TRIVIA_LIST_SCHEMA.validate(trivia_dict)
|
||||||
|
|
||||||
buffer.seek(0)
|
buffer.seek(0)
|
||||||
@ -803,7 +804,7 @@ def get_core_lists() -> List[pathlib.Path]:
|
|||||||
return list(core_lists_path.glob("*.yaml"))
|
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.
|
Returns a trivia list dictionary from the given path.
|
||||||
|
|
||||||
@ -814,12 +815,14 @@ def get_list(path: pathlib.Path) -> Dict[str, Any]:
|
|||||||
"""
|
"""
|
||||||
with path.open(encoding="utf-8") as file:
|
with path.open(encoding="utf-8") as file:
|
||||||
try:
|
try:
|
||||||
trivia_dict = yaml.safe_load(file)
|
trivia_dict = yaml.load(file, YAMLSafeLoader)
|
||||||
except yaml.error.YAMLError as exc:
|
except yaml.error.YAMLError as exc:
|
||||||
raise InvalidListError("YAML parsing failed.") from exc
|
raise InvalidListError("YAML parsing failed.") from exc
|
||||||
|
|
||||||
try:
|
if validate_schema:
|
||||||
TRIVIA_LIST_SCHEMA.validate(trivia_dict)
|
try:
|
||||||
except schema.SchemaError as exc:
|
TRIVIA_LIST_SCHEMA.validate(trivia_dict)
|
||||||
raise InvalidListError("The list does not adhere to the schema.") from exc
|
except schema.SchemaError as exc:
|
||||||
|
raise InvalidListError("The list does not adhere to the schema.") from exc
|
||||||
|
|
||||||
return trivia_dict
|
return trivia_dict
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user