Skip to content

Commit

Permalink
feat(dlplus): ODR-PadEnc output (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
hairmare authored Jan 17, 2022
1 parent 848d303 commit a419cc3
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/lint-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:

- run: pip install -r requirements-dev.txt

- run: pytest --pylint --cov=nowplaypadgen --cov-fail-under=100 tests
- run: pytest --cov-fail-under=100
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,32 @@ Later on you might want to generate DL+ that deletes an item tag.

```

Finally, you can render it as an [ODR-PadEnc](https://github.com/opendigitalradio/ODR-PadEnc) style string.

```python
>>> from nowplaypadgen.dlplus import *
>>> message = DLPlusMessage()
>>> message.add_dlp_object(DLPlusObject("ITEM.TITLE", "Radio Bern"))
>>> message.add_dlp_object(DLPlusObject("STATIONNAME.SHORT", "RaBe"))
>>> message.add_dlp_object(DLPlusObject("STATIONNAME.LONG", "Radio Bern RaBe"))
>>> message.build("{o[STATIONNAME.LONG]}")
>>> from nowplaypadgen.renderer.odr import ODRPadEncRenderer
>>> renderer = ODRPadEncRenderer(message)

```

This will generate the following ODR-PadEnc style DLS string when rendered with `print(renderer)`:

```
##### parameters { #####
DL_PLUS=1
DL_PLUS_TAG=1 0 10
DL_PLUS_TAG=31 11 4
DL_PLUS_TAG=32 0 15
##### parameters } #####
Radio Bern RaBe
```

## Release Management

The CI/CD setup uses semantic commit messages following the [conventional commits standard](https://www.conventionalcommits.org/en/v1.0.0/).
Expand Down
79 changes: 79 additions & 0 deletions nowplaypadgen/renderer/odr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""Convert to ODR-padenc style string prepresentation of Dynamic Label Plus strings.
This module generates a string/file that may be used with odr-padenc. odr-padenc
expects ether a simple DLS string or a string prefixed with a DLS header that
specifies the dynamic segments to be encoded into the mux.
More documentation on the odr-padenc format is available from <TODO>.
"""

from nowplaypadgen.dlplus import DLPlusMessage


class ODRPadEncRenderer:
"""Manage ODR-padenc format."""

def __init__(self, message: DLPlusMessage):
"""Create :class:`ODRPadEncRenderer` instance."""
self._message = message
super().__init__()

@property
def message(self):
"""Return messsage.
:returns: `DLPlusMessage`
"""
return self._message

def __str__(self):
"""Render :class:`ODRPadEncRenderer` as a odr-padenc style string.
@TODO ensure this matches what odr-padenc expects.
Simple non labeled strings are output as is.
>>> from nowplaypadgen.dlplus import DLPlusMessage, DLPlusObject
>>> message = DLPlusMessage()
>>> message.build("I am a message!")
>>> odr = ODRPadEncRenderer(message)
>>> str(odr)
'I am a message!'
If you add labels they are added in a odr-padenc style parameters frontmatter.
>>> message.add_dlp_object(DLPlusObject("STATIONNAME.LONG", "Radio RaBe"))
>>> message.add_dlp_object(DLPlusObject("STATIONNAME.SHORT", "RaBe"))
>>> message.build("{o[STATIONNAME.LONG]}")
>>> string = str(odr)
>>> "##### parameters { #####" in string
True
>>> "DL_PLUS=1" in string
True
>>> "DL_PLUS_TAG=31 6 4" in string
True
>>> "DL_PLUS_TAG=32 0 10" in string
True
>>> "##### parameters } #####" in string
True
>>> "Radio RaBe" in string
True
:returns: String DLPlusMessage formatted for input to odr-dabenc.
"""
dlp_tags = ""
if self.message.get_dlp_tags():
dlp_tags = "".join(
(
"##### parameters { #####\n",
"DL_PLUS=1\n",
"".join(
{
f"DL_PLUS_TAG={tag.code} {tag.start} {tag.length}\n"
for (_, tag) in self.message.get_dlp_tags().items()
}
),
"##### parameters } #####\n",
)
)
return f"{dlp_tags}{self.message.message}"
24 changes: 24 additions & 0 deletions tests/test_render_odr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from nowplaypadgen.dlplus import DLPlusMessage, DLPlusObject
from nowplaypadgen.renderer.odr import ODRPadEncRenderer


def test_render_odr():
"""Test the rendering of a DL Pluss message."""

message = DLPlusMessage()
message.add_dlp_object(DLPlusObject("ITEM.TITLE", "Radio Bern"))
message.add_dlp_object(DLPlusObject("STATIONNAME.SHORT", "RaBe"))
message.add_dlp_object(DLPlusObject("STATIONNAME.LONG", "Radio Bern RaBe"))
message.build("{o[STATIONNAME.LONG]}")

renderer = ODRPadEncRenderer(message)
output = str(renderer)

assert output.split("\n")[0] == "##### parameters { #####"
assert output.split("\n")[1] == "DL_PLUS=1"
assert output.split("\n")[5] == "##### parameters } #####"
assert output.split("\n")[6] == "Radio Bern RaBe"

assert "DL_PLUS_TAG=1 0 10" in output
assert "DL_PLUS_TAG=31 11 4" in output
assert "DL_PLUS_TAG=32 0 15" in output

0 comments on commit a419cc3

Please sign in to comment.