From 547f8c428df856d97bf9d258e723e39a7609b635 Mon Sep 17 00:00:00 2001 From: Vitiko <59455966+vitiko98@users.noreply.github.com> Date: Sat, 27 May 2023 09:38:55 -0400 Subject: Added feature to treat couples of languages as equal when searching for subtitles * Add 'Language-equals' support This feature will treat couples of languages as equal for list-subtitles operations. It's optional; its methods won't do anything if an empy list is set. See more info at docstrings from 'subliminal_patch.core'. For example, let's say I only want to have "Spanish (es.srt)" subtitles and I don't care about the differences between Spain and LATAM spanish. This feature will allow me to always get European Spanish even from LATAM Spanish providers like Argenteam and Subdivx. Example for config.ini: language_equals = ['spa-MX:spa'] (Which means all Latam Spanish subtitles from every provider will be converted to European Spanish) * Add PT and ZH language tests * Add HI and Forced parsing for language pairs Format example: ["en@HI:en", "es-MX@forced:es-MX"] * Update languages.py * Update API definition to reflect the previous change * Add language equals table to the UI (test only) * Add global language selector and get language from code3 utilities * Add unit tests for language equal feature * Add encode function to language equal feature * Add CRUD methods to the language equals panel * Add equals description * Add parsing support for alpha3 custom languages * no log: add more tests * Add forced and hi support to the language equal target --------- Co-authored-by: morpheus65535 Co-authored-by: LASER-Yi --- tests/bazarr/app/test_get_providers.py | 45 ------------- tests/bazarr/conftest.py | 1 + tests/bazarr/test_app_get_providers.py | 115 +++++++++++++++++++++++++++++++++ tests/bazarr/test_subtitles_pool.py | 10 +++ tests/subliminal_patch/test_core.py | 96 +++++++++++++++++++++++++++ 5 files changed, 222 insertions(+), 45 deletions(-) delete mode 100644 tests/bazarr/app/test_get_providers.py create mode 100644 tests/bazarr/test_app_get_providers.py create mode 100644 tests/bazarr/test_subtitles_pool.py (limited to 'tests') diff --git a/tests/bazarr/app/test_get_providers.py b/tests/bazarr/app/test_get_providers.py deleted file mode 100644 index caafa17d9..000000000 --- a/tests/bazarr/app/test_get_providers.py +++ /dev/null @@ -1,45 +0,0 @@ -import pytest - -import inspect - -from bazarr.app import get_providers - - -def test_get_providers_auth(): - for val in get_providers.get_providers_auth().values(): - assert isinstance(val, dict) - - -def test_get_providers_auth_with_provider_registry(): - """Make sure all providers will be properly initialized with bazarr - configs""" - from subliminal_patch.extensions import provider_registry - - auths = get_providers.get_providers_auth() - for key, val in auths.items(): - provider = provider_registry[key] - sign = inspect.signature(provider.__init__) - for sub_key in val.keys(): - if sub_key not in sign.parameters: - raise ValueError(f"'{sub_key}' parameter not present in {provider}") - - assert sign.parameters[sub_key] is not None - - -def test_get_providers_auth_embeddedsubtitles(): - item = get_providers.get_providers_auth()["embeddedsubtitles"] - assert isinstance(item["included_codecs"], list) - assert isinstance(item["hi_fallback"], bool) - assert isinstance(item["cache_dir"], str) - assert isinstance(item["ffprobe_path"], str) - assert isinstance(item["ffmpeg_path"], str) - assert isinstance(item["timeout"], str) - assert isinstance(item["unknown_as_english"], bool) - - -def test_get_providers_auth_karagarga(): - item = get_providers.get_providers_auth()["karagarga"] - assert item["username"] is not None - assert item["password"] is not None - assert item["f_username"] is not None - assert item["f_password"] is not None diff --git a/tests/bazarr/conftest.py b/tests/bazarr/conftest.py index 865b92767..d4bc23974 100644 --- a/tests/bazarr/conftest.py +++ b/tests/bazarr/conftest.py @@ -3,5 +3,6 @@ import logging os.environ["NO_CLI"] = "true" os.environ["SZ_USER_AGENT"] = "test" +os.environ["BAZARR_VERSION"] = "test" # fixme logging.getLogger("rebulk").setLevel(logging.WARNING) diff --git a/tests/bazarr/test_app_get_providers.py b/tests/bazarr/test_app_get_providers.py new file mode 100644 index 000000000..e6a31a39f --- /dev/null +++ b/tests/bazarr/test_app_get_providers.py @@ -0,0 +1,115 @@ +import inspect + +import pytest +from subliminal_patch.core import Language + +from bazarr.app import get_providers + + +def test_get_providers_auth(): + for val in get_providers.get_providers_auth().values(): + assert isinstance(val, dict) + + +def test_get_providers_auth_with_provider_registry(): + """Make sure all providers will be properly initialized with bazarr + configs""" + from subliminal_patch.extensions import provider_registry + + auths = get_providers.get_providers_auth() + for key, val in auths.items(): + provider = provider_registry[key] + sign = inspect.signature(provider.__init__) + for sub_key in val.keys(): + if sub_key not in sign.parameters: + raise ValueError(f"'{sub_key}' parameter not present in {provider}") + + assert sign.parameters[sub_key] is not None + + +def test_get_providers_auth_embeddedsubtitles(): + item = get_providers.get_providers_auth()["embeddedsubtitles"] + assert isinstance(item["included_codecs"], list) + assert isinstance(item["hi_fallback"], bool) + assert isinstance(item["cache_dir"], str) + assert isinstance(item["ffprobe_path"], str) + assert isinstance(item["ffmpeg_path"], str) + assert isinstance(item["timeout"], str) + assert isinstance(item["unknown_as_english"], bool) + + +def test_get_providers_auth_karagarga(): + item = get_providers.get_providers_auth()["karagarga"] + assert item["username"] is not None + assert item["password"] is not None + assert item["f_username"] is not None + assert item["f_password"] is not None + + +def test_get_language_equals_default_settings(): + assert isinstance(get_providers.get_language_equals(), list) + + +def test_get_language_equals_injected_settings_invalid(): + config = get_providers.settings + config.set("general", "language_equals", '["invalid"]') + assert not get_providers.get_language_equals(config) + + +def test_get_language_equals_injected_settings_valid(): + config = get_providers.settings + config.set("general", "language_equals", '["spa:spa-MX"]') + + result = get_providers.get_language_equals(config) + assert result == [(Language("spa"), Language("spa", "MX"))] + + +@pytest.mark.parametrize( + "config_value,expected", + [ + ('["spa:spl"]', (Language("spa"), Language("spa", "MX"))), + ('["por:pob"]', (Language("por"), Language("por", "BR"))), + ('["zho:zht"]', (Language("zho"), Language("zho", "TW"))), + ], +) +def test_get_language_equals_injected_settings_custom_lang_alpha3( + config_value, expected +): + config = get_providers.settings + + config.set("general", "language_equals", config_value) + + result = get_providers.get_language_equals(config) + assert result == [expected] + + +def test_get_language_equals_injected_settings_multiple(): + config = get_providers.settings + + config.set( + "general", + "language_equals", + "['eng@hi:eng', 'spa:spl', 'spa@hi:spl', 'spl@hi:spl']", + ) + + result = get_providers.get_language_equals(config) + assert len(result) == 4 + + +def test_get_language_equals_injected_settings_valid_multiple(): + config = get_providers.settings + config.set("general", "language_equals", '["spa:spa-MX", "spa-MX:spa"]') + + result = get_providers.get_language_equals(config) + assert result == [ + (Language("spa"), Language("spa", "MX")), + (Language("spa", "MX"), Language("spa")), + ] + + +def test_get_language_equals_injected_settings_hi(): + config = get_providers.settings + config.set("general", "language_equals", '["eng@hi:eng"]') + + result = get_providers.get_language_equals(config) + assert result == [(Language("eng", hi=True), Language("eng"))] diff --git a/tests/bazarr/test_subtitles_pool.py b/tests/bazarr/test_subtitles_pool.py new file mode 100644 index 000000000..862c6d493 --- /dev/null +++ b/tests/bazarr/test_subtitles_pool.py @@ -0,0 +1,10 @@ +from bazarr.subtitles import pool + + +def test_init_pool(): + assert pool._init_pool("movie") + + +def test_pool_update(): + pool_ = pool._init_pool("movie") + assert pool._pool_update(pool_, "movie") diff --git a/tests/subliminal_patch/test_core.py b/tests/subliminal_patch/test_core.py index d6481ee16..fadf4e493 100644 --- a/tests/subliminal_patch/test_core.py +++ b/tests/subliminal_patch/test_core.py @@ -70,3 +70,99 @@ def test_pool_update_discarded_providers_2(pool_instance): # Provider should not disappear from discarded providers assert pool_instance.discarded_providers == {"argenteam"} + + +def test_language_equals_init(): + assert core._LanguageEquals([(core.Language("spa"), core.Language("spa", "MX"))]) + + +def test_language_equals_init_invalid(): + with pytest.raises(ValueError): + assert core._LanguageEquals([(core.Language("spa", "MX"),)]) + + +def test_language_equals_init_empty_list_gracefully(): + assert core._LanguageEquals([]) == [] + + +@pytest.mark.parametrize( + "langs", + [ + [(core.Language("spa"), core.Language("spa", "MX"))], + [(core.Language("por"), core.Language("por", "BR"))], + [(core.Language("zho"), core.Language("zho", "TW"))], + ], +) +def test_language_equals_check_set(langs): + equals = core._LanguageEquals(langs) + lang_set = {langs[0]} + assert equals.check_set(lang_set) == set(langs) + + +def test_language_equals_check_set_do_nothing(): + equals = core._LanguageEquals([(core.Language("eng"), core.Language("spa"))]) + lang_set = {core.Language("spa")} + assert equals.check_set(lang_set) == {core.Language("spa")} + + +def test_language_equals_check_set_do_nothing_w_forced(): + equals = core._LanguageEquals( + [(core.Language("spa", forced=True), core.Language("spa", "MX"))] + ) + lang_set = {core.Language("spa")} + assert equals.check_set(lang_set) == {core.Language("spa")} + + +@pytest.fixture +def language_equals_pool_intance(): + equals = [(core.Language("spa"), core.Language("spa", "MX"))] + yield core.SZProviderPool({"subdivx"}, language_equals=equals) + + +def test_language_equals_pool_intance_list_subtitles( + language_equals_pool_intance, movies +): + subs = language_equals_pool_intance.list_subtitles( + movies["dune"], {core.Language("spa")} + ) + assert subs + assert all(sub.language == core.Language("spa", "MX") for sub in subs) + + +def test_language_equals_pool_intance_list_subtitles_reversed(movies): + equals = [(core.Language("spa", "MX"), core.Language("spa"))] + language_equals_pool_intance = core.SZProviderPool( + {"subdivx"}, language_equals=equals + ) + subs = language_equals_pool_intance.list_subtitles( + movies["dune"], {core.Language("spa")} + ) + assert subs + assert all(sub.language == core.Language("spa") for sub in subs) + + +def test_language_equals_pool_intance_list_subtitles_empty_lang_equals(movies): + language_equals_pool_intance = core.SZProviderPool( + {"subdivx"}, language_equals=None + ) + subs = language_equals_pool_intance.list_subtitles( + movies["dune"], {core.Language("spa")} + ) + assert subs + assert not all(sub.language == core.Language("spa", "MX") for sub in subs) + + +def test_language_equals_pool_intance_list_subtitles_return_nothing(movies): + equals = [ + (core.Language("spa", "MX"), core.Language("eng")), + (core.Language("spa"), core.Language("eng")), + ] + language_equals_pool_intance = core.SZProviderPool( + {"subdivx"}, language_equals=equals + ) + subs = language_equals_pool_intance.list_subtitles( + movies["dune"], {core.Language("spa")} + ) + assert not language_equals_pool_intance.download_best_subtitles( + subs, movies["dune"], {core.Language("spa")} + ) -- cgit v1.2.3