Skip to content

Commit

Permalink
Use multiple tags to work well with config file options.
Browse files Browse the repository at this point in the history
  • Loading branch information
getzze committed Oct 29, 2024
1 parent 6cf6ce9 commit 3f32be1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
1 change: 1 addition & 0 deletions docs/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ provider = ["addic7ed", "opensubtitlescom", "opensubtitles"]
refiner = ["metadata", "hash", "omdb"]
ignore_refiner = ["tmdb"]
language = ["fr", "en", "pt-br"]
foreign_only = false
encoding = "utf-8"
min_score = 50
archives = true
Expand Down
76 changes: 50 additions & 26 deletions subliminal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import tomli
from babelfish import Error as BabelfishError # type: ignore[import-untyped]
from babelfish import Language
from click_option_group import MutuallyExclusiveOptionGroup, OptionGroup
from click_option_group import OptionGroup
from dogpile.cache.backends.file import AbstractFileLock
from dogpile.util.readwrite_lock import ReadWriteMutex
from platformdirs import PlatformDirs
Expand Down Expand Up @@ -141,6 +141,12 @@ def configure(ctx: click.Context, param: click.Parameter | None, filename: str |

# make download options
download_dict = toml_dict.setdefault('download', {})
# handle language types
for lt in ('hearing_impaired', 'foreign_only'):
# if an option was defined in the config file, make it a tuple, the expected type
if lt in download_dict and (isinstance(download_dict[lt], bool) or download_dict[lt] is None):
download_dict[lt] = (download_dict[lt],)

# remove the provider and refiner lists to select, extend and ignore
provider_lists = {
'select': download_dict.pop('provider', []),
Expand Down Expand Up @@ -189,14 +195,6 @@ def plural(quantity: int, name: str, *, bold: bool = True, **kwargs: Any) -> str

providers_config = OptionGroup('Providers configuration')
refiners_config = OptionGroup('Refiners configuration')
hearing_impaired_group = MutuallyExclusiveOptionGroup(
'Hearing impaired subtitles',
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. If no preference (default), do not use any flag.',
)


@click.group(
Expand Down Expand Up @@ -418,10 +416,42 @@ def cache(ctx: click.Context, clear_subliminal: bool) -> None:
),
)
@click.option('-f', '--force', is_flag=True, default=False, help='Force download even if a subtitle already exist.')
@hearing_impaired_group.option('-hi', '--hearing-impaired', is_flag=True, default=False)
@hearing_impaired_group.option('-HI', '--no-hearing-impaired', is_flag=True, default=False)
@foreign_only_group.option('-fo', '--foreign-only', is_flag=True, default=False)
@foreign_only_group.option('-FO', '--no-foreign-only', is_flag=True, default=False)
@click.option(
'-fo',
'--foreign-only',
'foreign_only',
is_flag=True,
flag_value=True,
multiple=True,
help='Prefer foreign-only subtitles.',
)
@click.option(
'-FO',
'--no-foreign-only',
'foreign_only',
is_flag=True,
flag_value=False,
multiple=True,
help='Disfavor foreign-only subtitles.',
)
@click.option(
'-hi',
'--hearing-impaired',
'hearing_impaired',
is_flag=True,
flag_value=True,
multiple=True,
help='Prefer hearing-impaired subtitles.',
)
@click.option(
'-HI',
'--no-hearing-impaired',
'hearing_impaired',
is_flag=True,
flag_value=False,
multiple=True,
help='Disfavor hearing-impaired subtitles.',
)
@click.option(
'-m',
'--min-score',
Expand All @@ -433,7 +463,7 @@ def cache(ctx: click.Context, clear_subliminal: bool) -> None:
'--language-type-suffix',
is_flag=True,
default=False,
help='Add a suffix to the saved subtitle name to indicate a hearing impaired or foreign part subtitle.',
help='Add a suffix to the saved subtitle name to indicate a hearing impaired or foreign only subtitle.',
)
@click.option(
'--language-format',
Expand Down Expand Up @@ -478,10 +508,8 @@ def download(
original_encoding: bool,
single: bool,
force: bool,
hearing_impaired: bool,
no_hearing_impaired: bool,
foreign_only: bool,
no_foreign_only: bool,
hearing_impaired: tuple[bool | None, ...],
foreign_only: tuple[bool | None, ...],
min_score: int,
language_type_suffix: bool,
language_format: str,
Expand Down Expand Up @@ -511,15 +539,11 @@ def download(

# language_type
hearing_impaired_flag: bool | None = None
if hearing_impaired:
hearing_impaired_flag = True
elif no_hearing_impaired:
hearing_impaired_flag = False
if len(hearing_impaired) > 0:
hearing_impaired_flag = hearing_impaired[-1]
foreign_only_flag: bool | None = None
if foreign_only:
foreign_only_flag = True
elif no_foreign_only:
foreign_only_flag = False
if len(foreign_only) > 0:
foreign_only_flag = foreign_only[-1]

debug = obj.get('debug', False)
if debug:
Expand Down
1 change: 1 addition & 0 deletions subliminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ def download_best_subtitles(
# sort by hearing impaired and foreign only
language_type = LanguageType.from_flags(hearing_impaired=hearing_impaired, foreign_only=foreign_only)
if language_type != LanguageType.UNKNOWN:
logger.info('Sort subtitles by %s types first', language_type.value)
subtitles = sorted(
subtitles,
key=lambda s: s.language_type == language_type,
Expand Down

0 comments on commit 3f32be1

Please sign in to comment.