From c6c5b1125f2627faa6a29cebfecd7b84e96dcff9 Mon Sep 17 00:00:00 2001 From: jo Date: Wed, 1 Mar 2023 17:59:27 +0100 Subject: [PATCH] refactor(playout): generate entrypoint to string --- .../liquidsoap/entrypoint.py | 18 ++++------ playout/libretime_playout/liquidsoap/main.py | 16 +++++---- playout/tests/liquidsoap/entrypoint_test.py | 34 +++++++++---------- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/playout/libretime_playout/liquidsoap/entrypoint.py b/playout/libretime_playout/liquidsoap/entrypoint.py index 4d8c43454..e7c3b300a 100644 --- a/playout/libretime_playout/liquidsoap/entrypoint.py +++ b/playout/libretime_playout/liquidsoap/entrypoint.py @@ -18,13 +18,12 @@ templates.filters["quote"] = quote def generate_entrypoint( - entrypoint_filepath: Path, log_filepath: Optional[Path], config: Config, preferences: StreamPreferences, info: Info, version: Tuple[int, int, int], -): +) -> str: paths = {} paths["auth_filepath"] = here / "liquidsoap_auth.py" paths["lib_filepath"] = here / f"{version[0]}.{version[1]}/ls_script.liq" @@ -32,13 +31,10 @@ def generate_entrypoint( if log_filepath is not None: paths["log_filepath"] = log_filepath.resolve() - entrypoint_filepath.write_text( - templates.get_template("entrypoint.liq.j2").render( - config=config.copy(), - preferences=preferences, - info=info, - paths=paths, - version=version, - ), - encoding="utf-8", + return templates.get_template("entrypoint.liq.j2").render( + config=config.copy(), + preferences=preferences, + info=info, + paths=paths, + version=version, ) diff --git a/playout/libretime_playout/liquidsoap/main.py b/playout/libretime_playout/liquidsoap/main.py index 9fc20bd0f..6679af29e 100644 --- a/playout/libretime_playout/liquidsoap/main.py +++ b/playout/libretime_playout/liquidsoap/main.py @@ -40,13 +40,15 @@ def cli(log_level: str, log_filepath: Optional[Path], config_filepath: Optional[ preferences = StreamPreferences(**api_client.get_stream_preferences().json()) entrypoint_filepath = Path.cwd() / "radio.liq" - generate_entrypoint( - entrypoint_filepath, - log_filepath, - config, - preferences, - info, - version, + entrypoint_filepath.write_text( + generate_entrypoint( + log_filepath, + config, + preferences, + info, + version, + ), + encoding="utf-8", ) exec_args = [ diff --git a/playout/tests/liquidsoap/entrypoint_test.py b/playout/tests/liquidsoap/entrypoint_test.py index 704502481..516e5292d 100644 --- a/playout/tests/liquidsoap/entrypoint_test.py +++ b/playout/tests/liquidsoap/entrypoint_test.py @@ -21,15 +21,12 @@ from .fixtures import TEST_STREAM_CONFIGS "stream_config", TEST_STREAM_CONFIGS, ) -def test_generate_entrypoint(tmp_path: Path, stream_config: Config, version, snapshot): - entrypoint_filepath = tmp_path / "radio.liq" - +def test_generate_entrypoint(stream_config: Config, version, snapshot): with mock.patch( "libretime_playout.liquidsoap.entrypoint.here", Path("/fake"), ): - generate_entrypoint( - entrypoint_filepath, + found = generate_entrypoint( log_filepath=Path("/var/log/radio.log"), config=stream_config, preferences=StreamPreferences( @@ -43,7 +40,6 @@ def test_generate_entrypoint(tmp_path: Path, stream_config: Config, version, sna version=version, ) - found = entrypoint_filepath.read_text(encoding="utf-8") assert found == snapshot @@ -59,19 +55,21 @@ def test_liquidsoap_syntax(tmp_path: Path, stream_config): entrypoint_filepath = tmp_path / "radio.liq" log_filepath = tmp_path / "radio.log" - generate_entrypoint( - entrypoint_filepath, - log_filepath=log_filepath, - config=stream_config, - preferences=StreamPreferences( - input_fade_transition=0.0, - message_format=0, - message_offline="LibreTime - offline", + entrypoint_filepath.write_text( + generate_entrypoint( + log_filepath=log_filepath, + config=stream_config, + preferences=StreamPreferences( + input_fade_transition=0.0, + message_format=0, + message_offline="LibreTime - offline", + ), + info=Info( + station_name="LibreTime", + ), + version=get_liquidsoap_version(), ), - info=Info( - station_name="LibreTime", - ), - version=get_liquidsoap_version(), + encoding="utf-8", ) check_call(["liquidsoap", "--check", str(entrypoint_filepath)])