From 06df5604d0f8ea580cd3e9ec8cec155dd35ce2ff Mon Sep 17 00:00:00 2001 From: getzze Date: Mon, 7 Oct 2024 23:12:28 +0100 Subject: [PATCH] rename forced -> foreign_only --- subliminal/cli.py | 4 ++-- subliminal/core.py | 10 ++++---- subliminal/score.py | 2 +- subliminal/subtitle.py | 53 +++++++++++++++++++++++------------------- tests/test_score.py | 4 +--- tests/test_subtitle.py | 36 ++++++++++++++-------------- 6 files changed, 56 insertions(+), 53 deletions(-) diff --git a/subliminal/cli.py b/subliminal/cli.py index d76dbdce..4abacc70 100644 --- a/subliminal/cli.py +++ b/subliminal/cli.py @@ -191,11 +191,11 @@ def plural(quantity: int, name: str, *, bold: bool = True, **kwargs: Any) -> str refiners_config = OptionGroup('Refiners configuration') hearing_impaired_group = MutuallyExclusiveOptionGroup( 'Hearing impaired subtitles', - help='Require or avoid hearing impaired subtitles. Set to empty if no preference (default).', + help='Require or avoid hearing impaired subtitles. If no preference (default), do not use any flag.', ) foreign_only_group = MutuallyExclusiveOptionGroup( 'Foreign only subtitles', - help='Require or avoid foreign-only/forced subtitles. Set to empty if no preference (default).', + help='Require or avoid foreign-only/forced subtitles. If no preference (default), do not use any flag.', ) diff --git a/subliminal/core.py b/subliminal/core.py index 2c611842..225f430b 100644 --- a/subliminal/core.py +++ b/subliminal/core.py @@ -252,9 +252,9 @@ def download_best_subtitles( subtitles = [s for s in subtitles if s.id not in ignore_subtitles] # filter by hearing impaired and foreign only - lt = LanguageType.from_flags(hearing_impaired=hearing_impaired, forced=foreign_only) - if lt != LanguageType.UNKNOWN: - subtitles = [s for s in subtitles if s.language_type == lt] + language_type = LanguageType.from_flags(hearing_impaired=hearing_impaired, foreign_only=foreign_only) + if language_type != LanguageType.UNKNOWN: + subtitles = [s for s in subtitles if s.language_type == language_type] # sort subtitles by score scored_subtitles = sorted( @@ -418,7 +418,7 @@ def parse_subtitle_filename(subtitle_filename: str, video_filename: str) -> Subt except (ValueError, LanguageReverseError): logger.exception('Cannot parse language code %r', language_code) - # TODO: extract the hearing_impaired or forced attribute + # TODO: extract the hearing_impaired or foreign_only attribute return Subtitle(language, subtitle_id=subtitle_filename) @@ -871,7 +871,7 @@ def save_subtitles( :param str directory: path to directory where to save the subtitles, default is next to the video. :param str encoding: encoding in which to save the subtitles, default is to keep original encoding. :param (str | None) extension: the subtitle extension, default is to match to the subtitle format. - :param bool language_type_suffix: add a suffix 'hi' or 'forced' if needed. Default to False. + :param bool language_type_suffix: add a suffix 'hi' or 'fo' if needed. Default to False. :param str language_format: format of the language suffix. Default to 'alpha2'. :return: the saved subtitles :rtype: list of :class:`~subliminal.subtitle.Subtitle` diff --git a/subliminal/score.py b/subliminal/score.py index f11055dc..624e9010 100644 --- a/subliminal/score.py +++ b/subliminal/score.py @@ -141,7 +141,7 @@ def match_hearing_impaired(subtitle: Subtitle, *, hearing_impaired: bool | None ) -def compute_score(subtitle: Subtitle, video: Video) -> int: +def compute_score(subtitle: Subtitle, video: Video, **kwargs: Any) -> int: """Compute the score of the `subtitle` against the `video`. :func:`compute_score` uses the :meth:`Subtitle.get_matches ` method and diff --git a/subliminal/subtitle.py b/subliminal/subtitle.py index d72fed25..7fefc1cd 100644 --- a/subliminal/subtitle.py +++ b/subliminal/subtitle.py @@ -49,21 +49,22 @@ class LanguageType(Enum): """Subtitle language types.""" UNKNOWN = 'unknown' - FORCED = 'forced' + FOREIGN_ONLY = 'foreign_only' NORMAL = 'normal' HEARING_IMPAIRED = 'hearing_impaired' @classmethod - def from_flags(cls, *, hearing_impaired: bool | None = None, forced: bool | None = None) -> LanguageType: + def from_flags(cls, *, hearing_impaired: bool | None = None, foreign_only: bool | None = None) -> LanguageType: """Convert to LanguageType from flags.""" language_type = cls.UNKNOWN + # hearing_impaired takes precedence over foreign_only if both are True if hearing_impaired: language_type = cls.HEARING_IMPAIRED - elif forced: - language_type = cls.FORCED - # if hearing_impaired or forced is specified to be False + elif foreign_only: + language_type = cls.FOREIGN_ONLY + # if hearing_impaired or foreign_only is specified to be False # then for sure the subtitle is normal. - elif hearing_impaired is False or forced is False: + elif hearing_impaired is False or foreign_only is False: language_type = cls.NORMAL return language_type @@ -76,9 +77,9 @@ def is_hearing_impaired(self) -> bool | None: return None return False - def is_forced(self) -> bool | None: - """Flag for forced.""" - if self == LanguageType.FORCED: + def is_foreign_only(self) -> bool | None: + """Flag for foreign only.""" + if self == LanguageType.FOREIGN_ONLY: return True if self == LanguageType.UNKNOWN: return None @@ -91,6 +92,7 @@ class Subtitle: :param language: language of the subtitle. :type language: :class:`~babelfish.language.Language` :param (bool | None) hearing_impaired: whether or not the subtitle is hearing impaired (None if unknown). + :param (bool | None) foreign_only: whether or not the subtitle is foreign only / forced (None if unknown). :param page_link: URL of the web page from which the subtitle can be downloaded. :type page_link: str :param encoding: Text encoding of the subtitle. @@ -146,7 +148,7 @@ def __init__( subtitle_id: str = '', *, hearing_impaired: bool | None = None, - forced: bool | None = None, + foreign_only: bool | None = None, page_link: str | None = None, encoding: str | None = None, subtitle_format: str | None = None, @@ -167,7 +169,7 @@ def __init__( self.fps = fps self.embedded = embedded - self.language_type = LanguageType.from_flags(hearing_impaired=hearing_impaired, forced=forced) + self.language_type = LanguageType.from_flags(hearing_impaired=hearing_impaired, foreign_only=foreign_only) self.encoding = None # validate the encoding if encoding: @@ -192,9 +194,9 @@ def hearing_impaired(self) -> bool | None: return self.language_type.is_hearing_impaired() @property - def forced(self) -> bool | None: - """Whether the subtitle is a forced subtitle.""" - return self.language_type.is_forced() + def foreign_only(self) -> bool | None: + """Whether the subtitle is a foreign only / forced subtitle.""" + return self.language_type.is_foreign_only() @property def content(self) -> bytes | None: @@ -379,7 +381,7 @@ def get_path( :type video: :class:`~subliminal.video.Video` :param bool single: save a single subtitle, default is to save one subtitle per language. :param (str | None) extension: the subtitle extension, default is to match to the subtitle format. - :param bool language_type_suffix: add a suffix 'hi' or 'forced' if needed. Default to False. + :param bool language_type_suffix: add a suffix 'hi' or 'fo' if needed. Default to False. :param str language_format: format of the language suffix. Default to 'alpha2'. :return: path of the subtitle. :rtype: str @@ -426,7 +428,7 @@ def __init__( language: Language, *, hearing_impaired: bool | None = None, - forced: bool | None = None, + foreign_only: bool | None = None, encoding: str | None = None, subtitle_format: str | None = None, ) -> None: @@ -436,7 +438,7 @@ def __init__( language, subtitle_id, hearing_impaired=hearing_impaired, - forced=forced, + foreign_only=foreign_only, encoding=encoding, subtitle_format=subtitle_format, embedded=True, @@ -448,8 +450,8 @@ def info(self) -> str: extra = '' if self.language_type == LanguageType.HEARING_IMPAIRED: extra = ' [hi]' - elif self.language_type == LanguageType.FORCED: - extra = ' [forced]' + elif self.language_type == LanguageType.FOREIGN_ONLY: + extra = ' [fo]' return f'{self.id}{extra}' @@ -491,9 +493,12 @@ def get_subtitle_suffix( :param language: language of the subtitle to put in the path. :type language: :class:`~babelfish.language.Language` - :param str language_format: format of the language suffix. Default to 'alpha2'. - :param LanguageType language_type: the language type of the subtitle (hearing impaired or forced). - :param bool language_type_suffix: add a suffix 'hi' or 'forced' if needed. Default to False. + :param str language_format: format of the language suffix. + Default to 'alpha2'. + :param LanguageType language_type: the language type of the subtitle + (hearing impaired or foreign only). + :param bool language_type_suffix: add a suffix 'hi' or 'fo' if needed. + Default to False. :return: suffix to the subtitle name. :rtype: str @@ -525,8 +530,8 @@ def get_subtitle_suffix( if language_type_suffix: if language_type == LanguageType.HEARING_IMPAIRED: language_type_part = '.hi' - elif language_type == LanguageType.FORCED: - language_type_part = '.forced' + elif language_type == LanguageType.FOREIGN_ONLY: + language_type_part = '.fo' return language_type_part + language_part diff --git a/tests/test_score.py b/tests/test_score.py index 5b9f4b5a..25276541 100644 --- a/tests/test_score.py +++ b/tests/test_score.py @@ -141,6 +141,4 @@ def test_compute_score_hash_hearing_impaired(movies): filename='', encoding='utf-8', ) - assert compute_score(subtitle, video, hearing_impaired=True) == ( - movie_scores['hash'] + movie_scores['hearing_impaired'] - ) + assert compute_score(subtitle, video, hearing_impaired=True) == movie_scores['hash'] diff --git a/tests/test_subtitle.py b/tests/test_subtitle.py index 1c42028e..123c63fa 100644 --- a/tests/test_subtitle.py +++ b/tests/test_subtitle.py @@ -18,26 +18,26 @@ @pytest.mark.parametrize('hearing_impaired', [None, True, False]) -@pytest.mark.parametrize('forced', [None, True, False]) -def test_languague_type(hearing_impaired: bool | None, forced: bool | None) -> None: - language_type = LanguageType.from_flags(hearing_impaired=hearing_impaired, forced=forced) +@pytest.mark.parametrize('foreign_only', [None, True, False]) +def test_languague_type(hearing_impaired: bool | None, foreign_only: bool | None) -> None: + language_type = LanguageType.from_flags(hearing_impaired=hearing_impaired, foreign_only=foreign_only) if hearing_impaired is True: assert language_type == LanguageType.HEARING_IMPAIRED assert language_type.is_hearing_impaired() is True - assert language_type.is_forced() is False - elif forced is True: - assert language_type == LanguageType.FORCED + assert language_type.is_foreign_only() is False + elif foreign_only is True: + assert language_type == LanguageType.FOREIGN_ONLY assert language_type.is_hearing_impaired() is False - assert language_type.is_forced() is True - elif hearing_impaired is False or forced is False: + assert language_type.is_foreign_only() is True + elif hearing_impaired is False or foreign_only is False: assert language_type == LanguageType.NORMAL assert language_type.is_hearing_impaired() is False - assert language_type.is_forced() is False + assert language_type.is_foreign_only() is False else: assert language_type == LanguageType.UNKNOWN assert language_type.is_hearing_impaired() is None - assert language_type.is_forced() is None + assert language_type.is_foreign_only() is None def test_subtitle_text() -> None: @@ -175,14 +175,14 @@ def test_get_subtitle_path_hearing_impaired(movies): assert get_subtitle_path(video.name, suffix) == os.path.splitext(video.name)[0] + '.hi.de-CH-Latn.srt' -def test_get_subtitle_path_forced(movies): +def test_get_subtitle_path_foreign_only(movies): video = movies['man_of_steel'] suffix = get_subtitle_suffix( Language('srp', None, 'Cyrl'), - language_type=LanguageType.FORCED, + language_type=LanguageType.FOREIGN_ONLY, language_type_suffix=True, ) - assert get_subtitle_path(video.name, suffix) == os.path.splitext(video.name)[0] + '.forced.sr-Cyrl.srt' + assert get_subtitle_path(video.name, suffix) == os.path.splitext(video.name)[0] + '.fo.sr-Cyrl.srt' def test_get_subtitle_path_alpha3(movies): @@ -243,12 +243,12 @@ def test_subtitle_invalid_encoding(): def test_subtitle_guess_encoding_utf8(): subtitle = Subtitle( language=Language('zho'), - forced=False, + foreign_only=False, page_link=None, encoding=None, ) subtitle.content = b'Something here' - assert subtitle.forced is False + assert subtitle.foreign_only is False assert subtitle.guess_encoding() == 'utf-8' assert subtitle.text == 'Something here' @@ -285,7 +285,7 @@ def test_subtitle_info(monkeypatch) -> None: subtitle = Subtitle( Language('eng'), 'xv34e', - forced=True, + foreign_only=True, ) text = '1\n00:00:20,000 --> 00:00:24,400\nIn response to your honored\n\n' monkeypatch.setattr(Subtitle, 'text', text) @@ -306,10 +306,10 @@ def test_embedded_subtitle_info_hearing_impaired(monkeypatch) -> None: assert isinstance(subtitle.info, str) -def test_embedded_subtitle_info_forced(monkeypatch) -> None: +def test_embedded_subtitle_info_foreign_only(monkeypatch) -> None: subtitle = EmbeddedSubtitle( Language('fra'), - forced=True, + foreign_only=True, ) text = '1\n00:00:20,000 --> 00:00:24,400\nEn réponse à votre honorée du tant\n\n' monkeypatch.setattr(Subtitle, 'text', text)