diff --git a/royalnet/commands/__init__.py b/royalnet/commands/__init__.py index ca5f5785..ab5b49f0 100644 --- a/royalnet/commands/__init__.py +++ b/royalnet/commands/__init__.py @@ -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", ] diff --git a/royalnet/commands/commandinterface.py b/royalnet/commands/commandinterface.py index f61c8446..eb966e00 100644 --- a/royalnet/commands/commandinterface.py +++ b/royalnet/commands/commandinterface.py @@ -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 diff --git a/royalnet/commands/configdict.py b/royalnet/commands/configdict.py new file mode 100644 index 00000000..02c339d5 --- /dev/null +++ b/royalnet/commands/configdict.py @@ -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