feat(playout): add jinja2 quote filter for liquidsoap

This commit is contained in:
jo 2023-02-20 16:00:48 +01:00 committed by Jonas L
parent 1b027262ee
commit 3c8bf6c9eb
3 changed files with 32 additions and 0 deletions

View File

@ -6,6 +6,7 @@ from libretime_shared.config import AudioFormat, IcecastOutput, SystemOutput
from ..config import Config
from .models import Info, StreamPreferences
from .utils import quote
here = Path(__file__).parent
@ -16,6 +17,7 @@ templates = Environment( # nosec
loader=templates_loader,
keep_trailing_newline=True,
)
templates.filters["quote"] = quote
# Liquidsoap has 4 hardcoded output stream set of variables, so we need to
# fill the missing stream outputs with placeholders so Liquidsoap does

View File

@ -0,0 +1,15 @@
from typing import Any
def quote(value: Any, double=False) -> str:
"""
Quote and escape strings quotes for liquidsoap.
Double will escape the quotes twice, this is usually only used for the socket
communication to liquidsoap.
"""
if not isinstance(value, str):
value = str(value)
escaper = "\\\\" if double else "\\"
escaped = value.replace('"', f'{escaper}"')
return f'"{escaped}"'

View File

@ -0,0 +1,15 @@
import pytest
from libretime_playout.liquidsoap.utils import quote
@pytest.mark.parametrize(
"value, double, expected",
[
("something", False, '"something"'),
('something"', False, '"something\\""'),
('something"', True, '"something\\\\""'),
],
)
def test_quote(value, double, expected):
assert quote(value, double) == expected