def _custom_yaml_constructor(loader: Any, node: Any, clstype: Any) -> Any: """YAML custom constructor. Necessary to create an engine and retry handler.""" args = loader.construct_mapping(node, deep=True) if 'type' not in args: raise ValueError( "You have to specify a 'type' when instantiating a engine with !engine" ) clazz_name = args.pop('type') return load_plugin(clazz_name, clstype, **args)
def test_load_plugin(): plugin = load_plugin("pnp.plugins.pull.simple.Repeat", Pull, name='pytest', repeat="Hello World", wait=1) assert plugin is not None assert isinstance(plugin, Repeat) assert str( plugin) == "Repeat(interval=1.0, name='pytest', repeat='Hello World')"
def _mk_pull(task_config: Box, **extra: Any) -> PullModel: """Make a pull out of a task configuration.""" name = '{}_pull'.format(task_config[Schemas.task_name]) pull_args = task_config[Schemas.task_pull_name][Schemas.plugin_args_name] plugin = task_config[Schemas.task_pull_name][Schemas.plugin_name] args = {'name': name, **extra, **pull_args} return PullModel(instance=cast( Pull, load_plugin( plugin_path=plugin, plugin_type=Pull, instantiate=True, **args)))
def test_load_plugin_with_invalid_plugins(): with pytest.raises(TypeError): load_plugin(object(), object) with pytest.raises(TypeError): load_plugin(None, object) with pytest.raises(TypeError): load_plugin(1, object)
def _mk_udf(udf_config: Box) -> UDFModel: if not isinstance(udf_config, Box): udf_config = Box(udf_config) udf_type = cast( Union[str, type], 'callable' if not hasattr(udf_config, Schemas.plugin_args_name) else UserDefinedFunction) instantiate = hasattr(udf_config, Schemas.plugin_args_name) kwargs = udf_config.get(Schemas.plugin_args_name) or {} fun = load_plugin(plugin_path=udf_config[Schemas.plugin_name], plugin_type=udf_type, instantiate=instantiate, **{ 'name': udf_config[Schemas.udf_name], **kwargs }) return UDFModel(name=udf_config[Schemas.udf_name], callable=cast(UserDefinedFunction, fun))
def _many(pushlist: List[Box], prefix: str) -> Iterable[PushModel]: for i, push in enumerate(pushlist): push_name = '{prefix}_{i}'.format(i=i, prefix=prefix) args = { 'name': push_name, **extra, **push[Schemas.plugin_args_name] } unwrap = getattr(push, Schemas.push_unwrap_name, False) yield PushModel(instance=cast( Push, load_plugin(plugin_path=push[Schemas.plugin_name], plugin_type=Push, instantiate=True, **args)), selector=push[Schemas.push_selector_name], unwrap=unwrap, deps=list( _many(push[Schemas.push_deps_name], push_name)))
def test_load_plugin_with_wrong_base_type(): with pytest.raises(PluginTypeError) as e: load_plugin("pnp.plugins.pull.simple.Repeat", str) assert "The plugin is requested to inherit from '<class 'str'>', but it does not." in str( e)
def test_load_plugin_with_unknown_class(): with pytest.raises(ClassNotFoundError): load_plugin("pnp.plugins.pull.simple.Unknown", object)
def test_load_plugin_with_unknown_package(): with pytest.raises(NamespaceNotFoundError): load_plugin("unknown_namespace.Repeat", object)
def test_load_plugin_wo_required_args(): with pytest.raises(InvocationError): load_plugin("pnp.plugins.pull.simple.Repeat", object)