Skip to content

Commit

Permalink
Add wrap_text cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
fedecalendino committed Aug 28, 2022
1 parent ad1e614 commit 62c5808
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
tmp.py

.DS_Store
.idea/

Expand Down
13 changes: 13 additions & 0 deletions pysubparser/cleaners/wrap_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import Iterable, Iterator
import textwrap
from pysubparser.classes.subtitle import Subtitle


def clean(subtitles: Iterable[Subtitle], width: int = 40) -> Iterator[Subtitle]:
for subtitle in subtitles:
subtitle.lines = textwrap.wrap(
subtitle.text,
width=width,
)

yield subtitle
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sonar.organization=fedecalendino

sonar.projectName=pysub-parser
sonar.projectKey=fedecalendino_pysub-parser
sonar.projectVersion=1.6.0
sonar.projectVersion=1.7.0

sonar.language=py

Expand Down
79 changes: 46 additions & 33 deletions tests/test_cleaners.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase

from pysubparser.cleaners import ascii, brackets, formatting, lower_case
from pysubparser.cleaners import ascii, brackets, formatting, lower_case, wrap_text
from pysubparser.parser import parse


Expand All @@ -9,69 +9,82 @@ def setUp(self) -> None:
self.subtitles = parse("./tests/files/valid/cleaners.srt")

def _assert_subtitles(self, subtitles, expected):
for subtitle, text in zip(subtitles, expected):
self.assertEqual(subtitle.text, text)
for subtitle, lines in zip(subtitles, expected):
self.assertEqual(subtitle.lines, lines)

def test_no_cleaner(self):
expected = [
"Subtitle",
"- Subtitle",
"[Sound effect] Subtitle",
"<format>Subtitle</format>",
"Multi line Subtitle",
"Sübtîtlé",
["Subtitle"],
["- Subtitle"],
["[Sound effect] Subtitle"],
["<format>Subtitle</format>"],
["Multi line", "Subtitle"],
["Sübtîtlé"],
]

self._assert_subtitles(self.subtitles, expected)

def test_ascii_cleaner(self):
expected = [
"Subtitle",
"- Subtitle",
"[Sound effect] Subtitle",
"<format>Subtitle</format>",
"Multi line Subtitle",
"subtitle",
["Subtitle"],
["- Subtitle"],
["[Sound effect] Subtitle"],
["<format>Subtitle</format>"],
["Multi line", "Subtitle"],
["subtitle"],
]

clean_subtitles = ascii.clean(self.subtitles)
self._assert_subtitles(clean_subtitles, expected)

def test_lower_case_cleaner(self):
expected = [
"subtitle",
"- subtitle",
"[sound effect] subtitle",
"<format>subtitle</format>",
"multi line subtitle",
"sübtîtlé",
["subtitle"],
["- subtitle"],
["[sound effect] subtitle"],
["<format>subtitle</format>"],
["multi line", "subtitle"],
["sübtîtlé"],
]

clean_subtitles = lower_case.clean(self.subtitles)
self._assert_subtitles(clean_subtitles, expected)

def test_formatting_cleaner(self):
expected = [
"Subtitle",
"- Subtitle",
"[Sound effect] Subtitle",
"Subtitle",
"Multi line Subtitle",
"Sübtîtlé",
["Subtitle"],
["- Subtitle"],
["[Sound effect] Subtitle"],
["Subtitle"],
["Multi line", "Subtitle"],
["Sübtîtlé"],
]

clean_subtitles = formatting.clean(self.subtitles)
self._assert_subtitles(clean_subtitles, expected)

def test_brackets_cleaner(self):
expected = [
"Subtitle",
"- Subtitle",
"Subtitle",
"<format>Subtitle</format>",
"Multi line Subtitle",
"Sübtîtlé",
["Subtitle"],
["- Subtitle"],
["Subtitle"],
["<format>Subtitle</format>"],
["Multi line", "Subtitle"],
["Sübtîtlé"],
]

clean_subtitles = brackets.clean(self.subtitles)
self._assert_subtitles(clean_subtitles, expected)

def test_wrap_text_cleaner(self):
expected = [
["Subtitle"],
["- Subtitle"],
["[Sound", "effect]", "Subtitle"],
["<format>Su", "btitle</fo", "rmat>"],
["Multi line", "Subtitle"],
["Sübtîtlé"],
]

clean_subtitles = wrap_text.clean(self.subtitles, width=10)
self._assert_subtitles(clean_subtitles, expected)

0 comments on commit 62c5808

Please sign in to comment.