feat(playout): add jinja2 quote filter for liquidsoap
This commit is contained in:
parent
1b027262ee
commit
3c8bf6c9eb
|
@ -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
|
||||
|
|
|
@ -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}"'
|
|
@ -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
|
Loading…
Reference in New Issue