Skip to content

Commit

Permalink
Fix lcd arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
palemieux committed Dec 28, 2023
1 parent d9a6323 commit e08a997
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/main/python/ttconv/filters/doc/lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import logging
import typing
from dataclasses import dataclass, field
from numbers import Number

from ttconv.config import ModuleConfiguration
from ttconv.filters.document_filter import DocumentFilter
Expand All @@ -55,12 +56,21 @@ def _apply_bg_color(element: ContentElement, bg_color: ColorType):
for child in element:
_apply_bg_color(child, bg_color)

def _safe_area_decoder(s):
def _safe_area_decoder(s: Number) -> int:
safe_area = int(s)
if 30 < safe_area < 0:
raise ValueError("Safe area must be an integer between 0 and 30")
return safe_area

def _color_decoder(s: typing.Optional[ColorType]) -> typing.Optional[ColorType]:
if s is None:
return None

if not isinstance(s, str):
raise ValueError("Color specification must be a string")

return ttconv.utils.parse_color(s)

@dataclass
class LCDDocFilterConfig(ModuleConfiguration):
"""Configuration class for the Least common denominator (LCD) filter"""
Expand All @@ -70,16 +80,16 @@ def name(cls):
return "lcd"

# specifies the safe area as an integer percentage
safe_area: typing.Optional[int] = field(default=10, metadata={"decoder": int})
safe_area: typing.Optional[int] = field(default=10, metadata={"decoder": _safe_area_decoder})

# preserve text alignment the text color
preserve_text_align: typing.Optional[bool] = field(default=False, metadata={"decoder": bool})

# overrides the text color
color: typing.Optional[ColorType] = field(default=None, metadata={"decoder": ttconv.utils.parse_color})
color: typing.Optional[ColorType] = field(default=None, metadata={"decoder": _color_decoder})

# overrides the background color
bg_color: typing.Optional[ColorType] = field(default=None, metadata={"decoder": ttconv.utils.parse_color})
bg_color: typing.Optional[ColorType] = field(default=None, metadata={"decoder": _color_decoder})

class LCDDocFilter(DocumentFilter):
"""Merges regions and removes all text formatting with the exception of color
Expand Down
4 changes: 4 additions & 0 deletions src/test/python/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ def test_default_config_parsing(self):
for exp_config in expected_configurations:
self.assertTrue(exp_config in module_configurations)

def test_empty_config_parsing(self):

config_dict = json.loads('{}')


if __name__ == '__main__':
unittest.main()
7 changes: 7 additions & 0 deletions src/test/python/test_tt.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,12 @@ def test_lcd_filter(self):
'--config', '{"lcd": {"bg_color": "blue", "safe_area": 0, "color": "red", "preserve_text_align": true}}'
])

tt.main(['convert',
'-i', in_path,
'-o', out_path,
'--filter', 'lcd',
'--config', '{"lcd": {"bg_color":"red"}}'
])

if __name__ == '__main__':
unittest.main()

0 comments on commit e08a997

Please sign in to comment.