From a4920c03c3e46adee2dd11d596f25e39c1a662db Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Sat, 9 Dec 2023 01:18:40 -0800 Subject: [PATCH] getting into real world --- .../info_json/info_json_downloader.py | 5 ++++- src/ytdl_sub/downloaders/url/downloader.py | 3 ++- src/ytdl_sub/entries/entry.py | 21 ++++++++++++++++++- src/ytdl_sub/script/script.py | 6 ++++-- src/ytdl_sub/utils/scriptable.py | 6 +++--- tests/unit/entries/conftest.py | 3 ++- tests/unit/entries/test_entry.py | 4 ++-- 7 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/ytdl_sub/downloaders/info_json/info_json_downloader.py b/src/ytdl_sub/downloaders/info_json/info_json_downloader.py index 0198c4ae..756d34d3 100644 --- a/src/ytdl_sub/downloaders/info_json/info_json_downloader.py +++ b/src/ytdl_sub/downloaders/info_json/info_json_downloader.py @@ -109,7 +109,10 @@ class InfoJsonDownloader(SourcePlugin[InfoJsonDownloaderOptions]): prior_variables = entry.kwargs(YTDL_SUB_ENTRY_VARIABLES_KWARG_KEY) del entry._kwargs[YTDL_SUB_ENTRY_VARIABLES_KWARG_KEY] - entry.initialize_script(override_variables=self.overrides.dict_with_format_strings).add( + entry.initialize_script( + override_variables=self.overrides.dict_with_format_strings, + unresolvable=self.overrides.unresolvable, + ).add( { inj.variable_name: prior_variables.get( inj.variable_name, diff --git a/src/ytdl_sub/downloaders/url/downloader.py b/src/ytdl_sub/downloaders/url/downloader.py index 395df11c..36c2a240 100644 --- a/src/ytdl_sub/downloaders/url/downloader.py +++ b/src/ytdl_sub/downloaders/url/downloader.py @@ -460,7 +460,8 @@ class MultiUrlDownloader(SourcePlugin[MultiUrlValidator]): url_validator=collection_url, parents=parents, orphans=orphan_entries ): entry.initialize_script( - override_variables=self.overrides.dict_with_format_strings + override_variables=self.overrides.dict_with_format_strings, + unresolvable=self.overrides.unresolvable, ).add( { v.ytdl_sub_input_url.variable_name: self.overrides.apply_formatter( diff --git a/src/ytdl_sub/entries/entry.py b/src/ytdl_sub/entries/entry.py index f477c5f7..e7e5c13f 100644 --- a/src/ytdl_sub/entries/entry.py +++ b/src/ytdl_sub/entries/entry.py @@ -4,6 +4,7 @@ import os from pathlib import Path from typing import Dict from typing import Optional +from typing import Set from typing import Type from typing import TypeVar from typing import final @@ -29,8 +30,26 @@ class Entry(BaseEntry, Scriptable): BaseEntry.__init__(self, entry_dict=entry_dict, working_directory=working_directory) Scriptable.__init__(self) - def initialize_script(self, override_variables: Dict[str, str]) -> "Entry": + def initialize_script( + self, override_variables: Dict[str, str], unresolvable: Set[str] + ) -> "Entry": + # TODO: CLEAN THIS SHIT UP + # Overrides contains added variables that are unresolvable, add them here + self.unresolvable |= unresolvable + + # Remove the entry variable + self.unresolvable.remove(VARIABLES.entry_metadata.variable_name) + + # Add entry metadata, but avoid the `.add()` helper since it also adds sanitized self.script.add({VARIABLES.entry_metadata.variable_name: f"{{{json.dumps(self._kwargs)}}}"}) + self.script.add( + { + unresolved: f"{{%throw('Variable {unresolved} has not been resolved yet')}}" + for unresolved in self.unresolvable + } + ) + + # use .add here to get sanitized self.add(override_variables) self.update_script() return self diff --git a/src/ytdl_sub/script/script.py b/src/ytdl_sub/script/script.py index d0c9a0d8..42e995c1 100644 --- a/src/ytdl_sub/script/script.py +++ b/src/ytdl_sub/script/script.py @@ -354,14 +354,16 @@ class Script: pre_resolved=resolved, unresolvable=unresolvable, update=update, output_filter=None ) - def add(self, variables: Dict[str, str]) -> "Script": + def add(self, variables: Dict[str, str], unresolvable: Optional[Set[str]] = None) -> "Script": added_variables_to_validate: Set[str] = set() for variable_name, variable_definition in variables.items(): self._variables[variable_name] = parse( text=variable_definition, name=variable_name, custom_function_names=set(self._functions.keys()), - variable_names=set(self._variables.keys()).union(variables.keys()), + variable_names=set(self._variables.keys()) + .union(variables.keys()) + .union(unresolvable or set()), ) if self._variables[variable_name].maybe_resolvable is None: diff --git a/src/ytdl_sub/utils/scriptable.py b/src/ytdl_sub/utils/scriptable.py index 137a3b8a..afaefb29 100644 --- a/src/ytdl_sub/utils/scriptable.py +++ b/src/ytdl_sub/utils/scriptable.py @@ -19,11 +19,11 @@ class Scriptable(ABC): self.script.resolve(unresolvable=self.unresolvable, update=True) def add(self, values: Dict[str, Any]) -> None: + self.unresolvable -= set(list(values.keys())) self.script.add( ScriptUtils.add_sanitized_variables( {name: ScriptUtils.to_script(value) for name, value in values.items()} - ) + ), + unresolvable=self.unresolvable, ) - - self.unresolvable -= set(list(values.keys())) self.update_script() diff --git a/tests/unit/entries/conftest.py b/tests/unit/entries/conftest.py index 2fe03bde..15371142 100644 --- a/tests/unit/entries/conftest.py +++ b/tests/unit/entries/conftest.py @@ -179,5 +179,6 @@ def mock_entry_kwargs( @pytest.fixture def mock_entry(mock_entry_kwargs): return Entry(entry_dict=mock_entry_kwargs, working_directory=".").initialize_script( - override_variables={} + override_variables={}, + unresolvable=set(), ) diff --git a/tests/unit/entries/test_entry.py b/tests/unit/entries/test_entry.py index 7edfcdd4..b1311df4 100644 --- a/tests/unit/entries/test_entry.py +++ b/tests/unit/entries/test_entry.py @@ -38,7 +38,7 @@ class TestEntry(object): mock_entry_kwargs["upload_date"] = upload_date entry = Entry(entry_dict=mock_entry_kwargs, working_directory=".").initialize_script( - override_variables={} + override_variables={}, unresolvable=set() ) assert entry.get_int(v.upload_year_truncated_reversed) == year_rev assert entry.get_int(v.upload_month_reversed) == month_rev @@ -58,7 +58,7 @@ class TestEntry(object): ): mock_entry_kwargs["upload_date"] = upload_date entry = Entry(entry_dict=mock_entry_kwargs, working_directory=".").initialize_script( - override_variables={} + override_variables={}, unresolvable=set() ) assert entry.get_int(v.upload_day_of_year) == day_year