no kwargs in regex

This commit is contained in:
Jesse Bannon 2023-12-12 12:21:30 -08:00
parent 96cbb45dde
commit 38960a205d
2 changed files with 22 additions and 18 deletions

View file

@ -18,7 +18,6 @@ def _(key: str, backend: bool = False) -> str:
CHAPTERS = _("chapters", backend=True) CHAPTERS = _("chapters", backend=True)
YTDL_SUB_CUSTOM_CHAPTERS = _("ytdl_sub_custom_chapters", backend=True) YTDL_SUB_CUSTOM_CHAPTERS = _("ytdl_sub_custom_chapters", backend=True)
YTDL_SUB_REGEX_SOURCE_VARS = _("ytdl_sub_regex_source_vars", backend=True)
SPONSORBLOCK_CHAPTERS = _("sponsorblock_chapters", backend=True) SPONSORBLOCK_CHAPTERS = _("sponsorblock_chapters", backend=True)
SPLIT_BY_CHAPTERS_PARENT_ENTRY = _("split_by_chapters_parent_entry", backend=True) SPLIT_BY_CHAPTERS_PARENT_ENTRY = _("split_by_chapters_parent_entry", backend=True)
COMMENTS = _("comments", backend=True) COMMENTS = _("comments", backend=True)

View file

@ -1,15 +1,16 @@
from collections import defaultdict
from typing import Any from typing import Any
from typing import Dict from typing import Dict
from typing import List from typing import List
from typing import Optional from typing import Optional
from typing import Set from typing import Set
from ytdl_sub.config.overrides import Overrides
from ytdl_sub.config.plugin import Plugin from ytdl_sub.config.plugin import Plugin
from ytdl_sub.config.plugin import PluginPriority from ytdl_sub.config.plugin import PluginPriority
from ytdl_sub.config.preset_options import OptionsDictValidator from ytdl_sub.config.preset_options import OptionsDictValidator
from ytdl_sub.config.preset_options import PluginOperation from ytdl_sub.config.preset_options import PluginOperation
from ytdl_sub.entries.entry import Entry from ytdl_sub.entries.entry import Entry
from ytdl_sub.entries.variables.kwargs import YTDL_SUB_REGEX_SOURCE_VARS
from ytdl_sub.script.parser import parse from ytdl_sub.script.parser import parse
from ytdl_sub.script.utils.exceptions import ScriptVariableNotResolved from ytdl_sub.script.utils.exceptions import ScriptVariableNotResolved
from ytdl_sub.utils.exceptions import RegexNoMatchException from ytdl_sub.utils.exceptions import RegexNoMatchException
@ -21,6 +22,7 @@ from ytdl_sub.validators.string_formatter_validators import ListFormatterValidat
from ytdl_sub.validators.string_formatter_validators import StringFormatterValidator from ytdl_sub.validators.string_formatter_validators import StringFormatterValidator
from ytdl_sub.validators.validators import BoolValidator from ytdl_sub.validators.validators import BoolValidator
from ytdl_sub.validators.validators import DictValidator from ytdl_sub.validators.validators import DictValidator
from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadArchive
logger = Logger.get(name="regex") logger = Logger.get(name="regex")
@ -219,8 +221,9 @@ class RegexOptions(OptionsDictValidator):
""" """
return self._from.variable_capture_dict return self._from.variable_capture_dict
def _is_evaluatable_at_metadata_time( @classmethod
self, unresolved_variables: Set[str], input_variable_name: str, regex_options: VariableRegex def _can_evaluate_at_metadata_time(
cls, unresolved_variables: Set[str], input_variable_name: str, regex_options: VariableRegex
) -> bool: ) -> bool:
if input_variable_name in unresolved_variables: if input_variable_name in unresolved_variables:
return False return False
@ -244,7 +247,7 @@ class RegexOptions(OptionsDictValidator):
} }
for input_variable_name, regex_options in self.source_variable_capture_dict.items(): for input_variable_name, regex_options in self.source_variable_capture_dict.items():
key = PluginOperation.MODIFY_ENTRY key = PluginOperation.MODIFY_ENTRY
if self._is_evaluatable_at_metadata_time( if self._can_evaluate_at_metadata_time(
unresolved_variables=unresolved_variables, unresolved_variables=unresolved_variables,
input_variable_name=input_variable_name, input_variable_name=input_variable_name,
regex_options=regex_options, regex_options=regex_options,
@ -262,16 +265,19 @@ class RegexPlugin(Plugin[RegexOptions]):
modify_entry=PluginPriority.MODIFY_ENTRY_AFTER_SPLIT + 0, modify_entry=PluginPriority.MODIFY_ENTRY_AFTER_SPLIT + 0,
) )
@classmethod def __init__(
def _add_processed_regex_variable_name(cls, entry: Entry, source_var: str) -> None: self,
if not entry.kwargs_contains(YTDL_SUB_REGEX_SOURCE_VARS): options: RegexOptions,
entry.add_kwargs({YTDL_SUB_REGEX_SOURCE_VARS: []}) overrides: Overrides,
enhanced_download_archive: EnhancedDownloadArchive,
entry.kwargs(YTDL_SUB_REGEX_SOURCE_VARS).append(source_var) ):
super().__init__(
@classmethod options=options,
def _contains_processed_regex_variable(cls, entry: Entry, variable_name: str) -> bool: overrides=overrides,
return variable_name in entry.kwargs_get(YTDL_SUB_REGEX_SOURCE_VARS, []) enhanced_download_archive=enhanced_download_archive,
)
# Lookup of entry id to processed regex variables
self._processed_regex_vars: Dict[str, Set[str]] = defaultdict(set)
def _try_skip_entry(self, entry: Entry, variable_name: str) -> None: def _try_skip_entry(self, entry: Entry, variable_name: str) -> None:
# Skip the entry if toggled # Skip the entry if toggled
@ -323,7 +329,7 @@ class RegexPlugin(Plugin[RegexOptions]):
# Record which regex source variables are processed, to # Record which regex source variables are processed, to
# process as many variables as possible in the metadata stage, then the rest # process as many variables as possible in the metadata stage, then the rest
# after the media file has been downloaded. # after the media file has been downloaded.
if self._contains_processed_regex_variable(entry, variable_name): if variable_name in self._processed_regex_vars[entry.ytdl_uid()]:
continue continue
# If it's the metadata stage, and it can't be processed, skip until post-metadata # If it's the metadata stage, and it can't be processed, skip until post-metadata
@ -332,8 +338,7 @@ class RegexPlugin(Plugin[RegexOptions]):
): ):
continue continue
self._add_processed_regex_variable_name(entry, variable_name) self._processed_regex_vars[entry.ytdl_uid()].add(variable_name)
regex_input_str = str(entry.script.get(variable_name)) regex_input_str = str(entry.script.get(variable_name))
if ( if (