entry does not parse kwargs till called

This commit is contained in:
Jesse Bannon 2023-12-08 12:29:35 -08:00
parent fb8c65b653
commit 543845d57a
12 changed files with 21 additions and 29 deletions

View file

@ -2,7 +2,6 @@ import copy
from typing import Any
from typing import Dict
from typing import Optional
from typing import Set
from yt_dlp.utils import sanitize_filename

View file

@ -21,7 +21,6 @@ from ytdl_sub.config.preset_options import OutputOptions
from ytdl_sub.config.preset_options import TOptionsValidator
from ytdl_sub.config.preset_options import YTDLOptions
from ytdl_sub.downloaders.url.validators import MultiUrlValidator
from ytdl_sub.entries.entry import Entry
from ytdl_sub.entries.script.variable_definitions import VARIABLES
from ytdl_sub.entries.script.variable_scripts import VARIABLE_SCRIPTS
from ytdl_sub.prebuilt_presets import PREBUILT_PRESET_NAMES

View file

@ -87,7 +87,6 @@ class InfoJsonDownloader(SourcePlugin[InfoJsonDownloaderOptions]):
return Entry(
entry_dict=entry_dict,
working_directory=self.working_directory,
override_variables=self.overrides.dict_with_format_strings,
)
raise ValidationException(
@ -102,13 +101,10 @@ class InfoJsonDownloader(SourcePlugin[InfoJsonDownloaderOptions]):
entries: List[Entry] = []
for download_mapping in self._enhanced_download_archive.mapping.entry_mappings.values():
entry = self._get_entry_from_download_mapping(download_mapping)
entries.append(entry)
entry = self._get_entry_from_download_mapping(download_mapping).initialize_script(
override_variables=self.overrides.dict_with_format_strings
)
# Remove each entry from the live download archive since it will get re-added
# unless it is filtered
for entry in entries:
self._enhanced_download_archive.mapping.remove_entry(entry.uid)
prior_variables = entry.kwargs_get(YTDL_SUB_ENTRY_VARIABLES_KWARG_KEY, {})
entry.add(
@ -123,8 +119,12 @@ class InfoJsonDownloader(SourcePlugin[InfoJsonDownloaderOptions]):
),
}
)
entries.append(entry)
for entry in sorted(entries, key=lambda ent: ent.get(v.download_index)):
# Remove each entry from the live download archive since it will get re-added
# unless it is filtered
self._enhanced_download_archive.mapping.remove_entry(entry.uid)
yield entry
# If the original entry file_path is no longer maintained in the new mapping, then

View file

@ -360,7 +360,6 @@ class MultiUrlDownloader(SourcePlugin[MultiUrlValidator]):
return Entry(
download_entry_dict,
working_directory=self.working_directory,
override_variables=self.overrides.dict_with_format_strings,
)
def _iterate_child_entries(
@ -421,7 +420,6 @@ class MultiUrlDownloader(SourcePlugin[MultiUrlValidator]):
parents=parents,
entry_dicts=entry_dicts,
working_directory=self.working_directory,
override_variables=self.overrides.dict_with_format_strings,
)
return parents, orphans
@ -471,7 +469,9 @@ class MultiUrlDownloader(SourcePlugin[MultiUrlValidator]):
entry.add_kwargs(
{COLLECTION_URL: self.overrides.apply_formatter(collection_url.url)}
)
yield entry
yield entry.initialize_script(
override_variables=self.overrides.dict_with_format_strings
)
def download(self, entry: Entry) -> Optional[Entry]:
"""
@ -504,11 +504,6 @@ class MultiUrlDownloader(SourcePlugin[MultiUrlValidator]):
download_logger.info("Entry rejected by download match-filter, skipping ..")
return None
upload_date_idx = self._enhanced_download_archive.mapping.get_num_entries_with_upload_date(
upload_date_standardized=entry.get(v.upload_date_standardized)
)
download_idx = self._enhanced_download_archive.num_entries
entry.add_kwargs(
{
# Subtitles are not downloaded in metadata run, only here, so move over
@ -518,6 +513,11 @@ class MultiUrlDownloader(SourcePlugin[MultiUrlValidator]):
COMMENTS: download_entry.kwargs_get(COMMENTS),
}
)
upload_date_idx = self._enhanced_download_archive.mapping.get_num_entries_with_upload_date(
upload_date_standardized=entry.get(v.upload_date_standardized)
)
download_idx = self._enhanced_download_archive.num_entries
entry.add(
{
# Tracks number of entries downloaded

View file

@ -3,7 +3,6 @@ from datetime import datetime
from pathlib import Path
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Type
from typing import TypeVar

View file

@ -21,15 +21,15 @@ class Entry(BaseEntry, Scriptable):
Entry object to represent a single media object returned from yt-dlp.
"""
def __init__(
self, entry_dict: Dict, working_directory: str, override_variables: Dict[str, str]
):
def __init__(self, entry_dict: Dict, working_directory: str):
BaseEntry.__init__(self, entry_dict=entry_dict, working_directory=working_directory)
Scriptable.__init__(self)
def initialize_script(self, override_variables: Dict[str, str]) -> "Entry":
self.script.add({VARIABLES.entry_metadata.variable_name: f"{{{json.dumps(self._kwargs)}}}"})
self.add(override_variables)
self.update_script()
return self
def get(self, variable: Variable) -> str:
return self.script.resolve(unresolvable=self.unresolvable).get_str(variable.variable_name)

View file

@ -283,7 +283,6 @@ class EntryParent(BaseEntry):
parents: List["EntryParent"],
entry_dicts: List[Dict],
working_directory: str,
override_variables: Dict[str, str],
) -> List[Entry]:
"""
Reads all entries that do not have any parents
@ -296,7 +295,6 @@ class EntryParent(BaseEntry):
Entry(
entry_dict=entry_dict,
working_directory=working_directory,
override_variables=override_variables,
)
for entry_dict in entry_dicts
if cls.is_entry(entry_dict) and not _in_any_parents(entry_dict)

View file

@ -147,7 +147,7 @@ ENTRY_DERIVED_VARIABLES: Dict[Variable, str] = {
v.creator_sanitized: sanitized(v.creator),
v.download_index_padded6: pad_int(v.download_index, 6),
v.upload_date_index_padded: pad_int(v.upload_date_index, 2),
v.upload_date_index_reversed: f"{{%sub(100, {v.download_index.variable_name})}}",
v.upload_date_index_reversed: f"{{%sub(100, {v.upload_date_index.variable_name})}}",
v.upload_date_index_reversed_padded: pad_int(v.upload_date_index_reversed, 2),
}

View file

@ -12,7 +12,6 @@ from ytdl_sub.entries.entry import Entry
from ytdl_sub.entries.script.variable_scripts import VARIABLE_SCRIPTS
from ytdl_sub.entries.variables.kwargs import YTDL_SUB_REGEX_SOURCE_VARS
from ytdl_sub.script.script import Script
from ytdl_sub.script.script import ScriptBuilder
from ytdl_sub.utils.exceptions import RegexNoMatchException
from ytdl_sub.utils.exceptions import StringFormattingVariableNotFoundException
from ytdl_sub.utils.logger import Logger

View file

@ -5,7 +5,6 @@ from typing import Any
from typing import Dict
from typing import Set
from ytdl_sub.entries.script.variable_scripts import ENTRY_INJECTED_VARIABLES
from ytdl_sub.entries.script.variable_scripts import UNRESOLVED_VARIABLES
from ytdl_sub.entries.script.variable_scripts import VARIABLE_SCRIPTS
from ytdl_sub.script.script import Script
@ -46,4 +45,4 @@ class Scriptable(ABC):
)
self.unresolvable -= set(list(values.keys()))
self.script.resolve(unresolvable=self.unresolvable, update=True)
self.update_script()

View file

@ -653,7 +653,6 @@ class EnhancedDownloadArchive:
parent_entry = Entry(
entry_dict=entry.kwargs(SPLIT_BY_CHAPTERS_PARENT_ENTRY),
working_directory=entry.working_directory(),
override_variables={},
)
self.mapping.add_entry(parent_entry, entry_file_path=output_file_name)
elif entry:

View file

@ -174,7 +174,7 @@ def mock_entry_kwargs(
@pytest.fixture
def mock_entry(mock_entry_kwargs):
return Entry(entry_dict=mock_entry_kwargs, working_directory=".", override_variables={})
return Entry(entry_dict=mock_entry_kwargs, working_directory=".")
@pytest.fixture