Mercurial > hg > auditok
changeset 421:5e2fe07cd12f
Add test for make_kwargs error
author | Amine Sehili <amine.sehili@gmail.com> |
---|---|
date | Fri, 18 Oct 2024 23:45:16 +0200 |
parents | f874558779b9 |
children | 912bec5a58ed |
files | auditok/cmdline.py auditok/cmdline_util.py auditok/exceptions.py tests/test_cmdline_util.py |
diffstat | 4 files changed, 63 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/auditok/cmdline.py Fri Oct 18 23:32:41 2024 +0200 +++ b/auditok/cmdline.py Fri Oct 18 23:45:16 2024 +0200 @@ -23,7 +23,7 @@ from auditok import AudioRegion, __version__ from .cmdline_util import initialize_workers, make_kwargs, make_logger -from .exceptions import AudioEncodingWarning, EndOfProcessing +from .exceptions import ArgumentError, EndOfProcessing __all__ = [] __date__ = "2015-11-23" @@ -386,8 +386,14 @@ ) args = parser.parse_args(argv) + try: + kwargs = make_kwargs(args) + except ArgumentError as exc: + print(exc, file=sys.stderr) + return 1 + logger = make_logger(args.debug, args.debug_file) - kwargs = make_kwargs(args) + stream_saver, tokenizer_worker = initialize_workers( logger=logger, **kwargs.split, **kwargs.io, **kwargs.miscellaneous )
--- a/auditok/cmdline_util.py Fri Oct 18 23:32:41 2024 +0200 +++ b/auditok/cmdline_util.py Fri Oct 18 23:45:16 2024 +0200 @@ -3,6 +3,7 @@ from collections import namedtuple from . import workers +from .exceptions import ArgumentError from .io import player_for from .util import AudioReader @@ -22,6 +23,12 @@ except (ValueError, TypeError): use_channel = args_ns.use_channel + if args_ns.join_detections is not None and args_ns.save_stream is None: + raise ArgumentError( + "using --join-detections/-j requires --save-stream/-O " + "to be specified." + ) + io_kwargs = { "input": args_ns.input, "audio_format": args_ns.input_format, @@ -87,7 +94,6 @@ if kwargs["save_stream"] is not None: if kwargs["join_detections"] is not None: - print("Using event joiner...") stream_saver = workers.AudioEventsJoinerWorker( silence_duration=kwargs["join_detections"], filename=kwargs["save_stream"],
--- a/auditok/exceptions.py Fri Oct 18 23:32:41 2024 +0200 +++ b/auditok/exceptions.py Fri Oct 18 23:45:16 2024 +0200 @@ -1,3 +1,7 @@ +class ArgumentError(Exception): + """Raised when command line arguments have invalid values.""" + + class TooSmallBlockDuration(ValueError): """Raised when block_dur results in a block_size smaller than one sample."""
--- a/tests/test_cmdline_util.py Fri Oct 18 23:32:41 2024 +0200 +++ b/tests/test_cmdline_util.py Fri Oct 18 23:45:16 2024 +0200 @@ -12,6 +12,7 @@ make_kwargs, make_logger, ) +from auditok.exceptions import ArgumentError from auditok.workers import ( AudioEventsJoinerWorker, CommandLineWorker, @@ -175,6 +176,49 @@ assert kwargs == expected +def test_make_kwargs_error(): + + args = ( + "file", + 30, + 0.01, + 16000, + 2, + 2, + 1, + "raw", + "ogg", + True, + None, + 1, + None, # save_stream + None, + 1.0, # join_detections + None, + None, + 0.2, + 10, + 0.3, + False, + False, + 55, + False, + False, + None, + True, + None, + "TIME_FORMAT", + "TIMESTAMP_FORMAT", + ) + + args_ns = _ArgsNamespace(*args) + expected_err_msg = "using --join-detections/-j requires " + expected_err_msg += "--save-stream/-O to be specified." + with pytest.raises(ArgumentError) as arg_err: + make_kwargs(args_ns) + assert str(arg_err.value) == expected_err_msg + + def test_make_logger_stderr_and_file(capsys): with TemporaryDirectory() as tmpdir: file = os.path.join(tmpdir, "file.log")