mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 11:34:18 +00:00
Raise configuration errors for missing dict keys
This commit is contained in:
parent
53aa6c16b1
commit
c128935d61
3 changed files with 24 additions and 1 deletions
|
@ -8,6 +8,7 @@ from .event import Event
|
|||
from .errors import \
|
||||
CommandError, InvalidInputError, UnsupportedError, ConfigurationError, ExternalError, UserError, ProgramError
|
||||
from .keyboardkey import KeyboardKey
|
||||
from .configdict import ConfigDict
|
||||
|
||||
__all__ = [
|
||||
"CommandInterface",
|
||||
|
@ -23,4 +24,5 @@ __all__ = [
|
|||
"ProgramError",
|
||||
"Event",
|
||||
"KeyboardKey",
|
||||
"ConfigDict",
|
||||
]
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from typing import *
|
||||
import asyncio as aio
|
||||
from .errors import UnsupportedError
|
||||
from .configdict import ConfigDict
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .event import Event
|
||||
|
@ -36,7 +37,7 @@ class CommandInterface:
|
|||
A reference to a :class:`~royalnet.constellation.Constellation`."""
|
||||
|
||||
def __init__(self, config: Dict[str, Any]):
|
||||
self.config: Dict[str, Any] = config
|
||||
self.config: ConfigDict[str, Any] = ConfigDict.convert(config)
|
||||
"""The config section for the pack of the command."""
|
||||
|
||||
# Will be bound after the command/event has been created
|
||||
|
|
20
royalnet/commands/configdict.py
Normal file
20
royalnet/commands/configdict.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
from .errors import ConfigurationError
|
||||
|
||||
|
||||
class ConfigDict(dict):
|
||||
def __missing__(self, key):
|
||||
raise ConfigurationError(f"Missing config key '{key}'")
|
||||
|
||||
@classmethod
|
||||
def convert(cls, item):
|
||||
if isinstance(item, dict):
|
||||
cd = ConfigDict()
|
||||
for key in item:
|
||||
cd[key] = cls.convert(item[key])
|
||||
return cd
|
||||
elif isinstance(item, list):
|
||||
nl = []
|
||||
for obj in item:
|
||||
nl.append(cls.convert(obj))
|
||||
else:
|
||||
return item
|
Loading…
Reference in a new issue