def _validate_no_extra_args(self, plugin_cls, plugin_args: dict, args: dict): for name in args: if not name in plugin_args: message = "Plugin {0} doesn't have an argument called '{1}'".format( plugin_cls.__name__, name) raise InvalidPluginReferenceError(message)
def get_plugin(self, name: str, args: dict = None): args = args or dict() if not self.has_plugin(name): raise InvalidPluginReferenceError("Cannot find a plugin with name '{0}'".format(name)) plugin_cls = self.plugins[name] self._validate_args(plugin_cls, args) return plugin_cls(**args)
def _validate_args(self, plugin_cls, args: dict): """ Validate the given args are valid for the plugin """ plugin_args = self.get_args_for(plugin_cls) self._validate_no_extra_args(plugin_cls, plugin_args, args) for arg in plugin_args.values(): if args.get(arg.name) is None: if arg.required: message = "Missing a required argument {0} for plugin {1}".format(arg.name, plugin_cls.__name__) raise InvalidPluginReferenceError(message) args[arg.name] = arg.default