diff --git a/hexsample/pipeline.py b/hexsample/pipeline.py index 98f7a98..7c588aa 100644 --- a/hexsample/pipeline.py +++ b/hexsample/pipeline.py @@ -35,7 +35,7 @@ from hxsim import HXSIM_ARGPARSER, hxsim as _hxsim -def required_args(parser : ArgumentParser) -> list: +def required_arguments(parser : ArgumentParser) -> list: """Return a list of the positional arguments for a given parser. This is useful to retrieve all the default values from an ArgumentParser @@ -49,7 +49,7 @@ def required_args(parser : ArgumentParser) -> list: """ return [action.dest for action in parser._actions if action.required] -def default_args(parser : ArgumentParser) -> dict: +def default_arguments(parser : ArgumentParser) -> dict: """Return the default arguments for a given ArgumentParser object. If the parser has no positional arguments, this is simply achieved via a @@ -68,13 +68,22 @@ def default_args(parser : ArgumentParser) -> dict: --------- parser : ArgumentParser The argument parser object for a given application. + + Returns + ------- + args, kwargs : list, dict + The list of positional arguments and a dictionary of all the other arguments. """ - args = required_args(parser) + # Positional arguments. + args = required_arguments(parser) + # All parser arguments. kwargs = vars(parser.parse_args(args)) + # Strip the positional arguments from the complete list. [kwargs.pop(key) for key in args] - return kwargs + # And return the two sets separately. + return args, kwargs -def update_args(parser : ArgumentParser, **kwargs) -> dict: +def update_arguments(parser : ArgumentParser, **kwargs) -> dict: """Retrieve the default option from an ArgumentParser object and update specific keys based on arbitrary keyword arguments. @@ -86,16 +95,22 @@ def update_args(parser : ArgumentParser, **kwargs) -> dict: kwargs : dict Additional keyword arguments. """ - args = default_args(parser) - args.update(kwargs) - return args + # Retrieve the default arguments. + _args, _kwargs = default_arguments(parser) + # Loop over the kwargs passed to the function to make sure that all of them + # are recognized by the parser. + for key in kwargs: + if key not in _args and key not in _kwargs: + raise RuntimeError(f'Unknown parameter {key} passed to a pipeline component') + _kwargs.update(kwargs) + return _kwargs def hxrecon(**kwargs): """Application wrapper. """ - return _hxrecon(**update_args(HXRECON_ARGPARSER, **kwargs)) + return _hxrecon(**update_arguments(HXRECON_ARGPARSER, **kwargs)) def hxsim(**kwargs): """Application wrapper. """ - return _hxsim(**update_args(HXSIM_ARGPARSER, **kwargs)) + return _hxsim(**update_arguments(HXSIM_ARGPARSER, **kwargs)) diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py index b34448d..61f69d8 100644 --- a/tests/test_pipeline.py +++ b/tests/test_pipeline.py @@ -25,11 +25,19 @@ def test_parsers(): """Test the relevant ArgumentParser objects. """ - assert pipeline.required_args(pipeline.HXSIM_ARGPARSER) == [] - assert pipeline.required_args(pipeline.HXRECON_ARGPARSER) == ['infile'] - assert 'infile' not in pipeline.update_args(pipeline.HXRECON_ARGPARSER) - assert 'infile' in pipeline.update_args(pipeline.HXRECON_ARGPARSER, infile='test_file') - print(pipeline.update_args(pipeline.HXSIM_ARGPARSER)) + assert pipeline.required_arguments(pipeline.HXSIM_ARGPARSER) == [] + assert pipeline.required_arguments(pipeline.HXRECON_ARGPARSER) == ['infile'] + assert 'infile' not in pipeline.update_arguments(pipeline.HXRECON_ARGPARSER) + assert 'infile' in pipeline.update_arguments(pipeline.HXRECON_ARGPARSER, infile='test_file') + print(pipeline.update_arguments(pipeline.HXSIM_ARGPARSER)) + +def test_wrong_args(): + """Make sure that, when supplied with wrong parameters, the pipeline applications + are raising a RuntimeError. + """ + with pytest.raises(RuntimeError) as excinfo: + pipeline.hxsim(numevents=100, bogusparam='howdy') + print(excinfo.value) def test_pipeline(): """Test generating and reconstructing files.