diff --git a/bin/audio-offset-finder b/audio_offset_finder/cli.py old mode 100755 new mode 100644 similarity index 96% rename from bin/audio-offset-finder rename to audio_offset_finder/cli.py index e7953e7..8379fd9 --- a/bin/audio-offset-finder +++ b/audio_offset_finder/cli.py @@ -1,8 +1,6 @@ -#!/usr/bin/python3 - # audio-offset-finder # -# Copyright (c) 2014-22 British Broadcasting Corporation +# Copyright (c) 2014-23 British Broadcasting Corporation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from audio_offset_finder.audio_offset_finder import find_offset_between_files +from .audio_offset_finder import find_offset_between_files import argparse import sys @@ -117,5 +115,9 @@ def plot_results(args, results): pyplot.show() -if __name__ == "__main__": +def run(): sys.exit(main(sys.argv[1:])) + + +if __name__ == "__main__": + run() diff --git a/setup.cfg b/setup.cfg index fd2f9e5..5ea6387 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,4 +27,4 @@ dev = [options.entry_points] console_scripts = - executable-name = audio_offset_finder.audio_offset_finder:main + audio-offset-finder = audio_offset_finder.cli:run diff --git a/tests/tool_test.py b/tests/tool_test.py index afd1ba5..3b8d75b 100644 --- a/tests/tool_test.py +++ b/tests/tool_test.py @@ -1,6 +1,6 @@ # audio-offset-finder # -# Copyright (c) 2014-22 British Broadcasting Corporation +# Copyright (c) 2014-23 British Broadcasting Corporation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -18,41 +18,18 @@ import numpy as np import os import types -from importlib.machinery import ModuleSpec, SourceFileLoader -from importlib.util import spec_from_loader, module_from_spec +from audio_offset_finder.cli import main, reorder_correlations from unittest.mock import patch from io import StringIO import tempfile -# Function to import code from a file -def import_from_source(name: str, file_path: str) -> types.ModuleType: - loader: SourceFileLoader = SourceFileLoader(name, file_path) - spec: ModuleSpec = spec_from_loader(loader.name, loader) - module: types.ModuleType = module_from_spec(spec) - loader.exec_module(module) - return module - - -script_path: str = os.path.abspath( - os.path.join( - os.path.dirname(os.path.abspath(__file__)), - "..", - "bin", - "audio-offset-finder", - ) -) - - -tool: types.ModuleType = import_from_source("audio-offset-finder", script_path) - - def test_reorder_correlations(): input_array1 = np.array([0, 1, 2, 3]) - np.testing.assert_array_equal(tool.reorder_correlations(input_array1), np.array([2, 3, 0, 1])) + np.testing.assert_array_equal(reorder_correlations(input_array1), np.array([2, 3, 0, 1])) input_array2 = np.array([0, 1, 2, 3, 4]) - np.testing.assert_array_equal(tool.reorder_correlations(input_array2), np.array([2, 3, 4, 0, 1])) + np.testing.assert_array_equal(reorder_correlations(input_array2), np.array([2, 3, 4, 0, 1])) def test_tool(): @@ -62,7 +39,7 @@ def test_tool(): "--find-offset-of tests/audio/timbl_2.mp3 --within tests/audio/timbl_1.mp3 --resolution 160 " "--trim 35 --save-plot " ) + plot_file_path with patch("sys.stdout", new=StringIO()) as fakeStdout: - tool.main(args1.split()) + main(args1.split()) output = fakeStdout.getvalue().strip() assert output, "audio_offset_finder did not produce any output" assert "Offset: 12.26" in output @@ -72,13 +49,13 @@ def test_tool(): args2 = "--find-offset-of tests/audio/timbl_2.mp3" with pytest.raises(SystemExit) as error: - tool.main(args2.split()) + main(args2.split()) assert error.type == SystemExit assert error.value.code > 0, "missing 'within' file" args3 = "--within tests/audio/timbl_1.mp3" with pytest.raises(SystemExit) as error: - tool.main(args3.split()) + main(args3.split()) assert error.type == SystemExit assert error.value.code > 0, "missing 'offset-of' file" @@ -88,7 +65,7 @@ def test_json(): args = "--find-offset-of tests/audio/timbl_2.mp3 --within tests/audio/timbl_1.mp3 --resolution 160 " "--trim 35 --json" with patch("sys.stdout", new=StringIO()) as fakeStdout: - tool.main(args.split()) + main(args.split()) output = fakeStdout.getvalue().strip() json_array = json.loads(output) assert len(json_array) == 2