maybe more optimized
This commit is contained in:
parent
2203267025
commit
a0ccefbb03
5 changed files with 36 additions and 24 deletions
|
|
@ -2,6 +2,7 @@ import copy
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from typing import Set
|
||||||
|
|
||||||
from yt_dlp.utils import sanitize_filename
|
from yt_dlp.utils import sanitize_filename
|
||||||
|
|
||||||
|
|
@ -9,6 +10,7 @@ from ytdl_sub.entries.entry import Entry
|
||||||
from ytdl_sub.entries.script.variable_definitions import VARIABLES
|
from ytdl_sub.entries.script.variable_definitions import VARIABLES
|
||||||
from ytdl_sub.entries.variables.override_variables import SUBSCRIPTION_NAME
|
from ytdl_sub.entries.variables.override_variables import SUBSCRIPTION_NAME
|
||||||
from ytdl_sub.script.parser import parse
|
from ytdl_sub.script.parser import parse
|
||||||
|
from ytdl_sub.script.script import Script
|
||||||
from ytdl_sub.utils.scriptable import Scriptable
|
from ytdl_sub.utils.scriptable import Scriptable
|
||||||
from ytdl_sub.validators.string_formatter_validators import DictFormatterValidator
|
from ytdl_sub.validators.string_formatter_validators import DictFormatterValidator
|
||||||
from ytdl_sub.validators.string_formatter_validators import StringFormatterValidator
|
from ytdl_sub.validators.string_formatter_validators import StringFormatterValidator
|
||||||
|
|
@ -116,14 +118,15 @@ class Overrides(DictFormatterValidator, Scriptable):
|
||||||
-------
|
-------
|
||||||
The format_string after .format has been called
|
The format_string after .format has been called
|
||||||
"""
|
"""
|
||||||
|
script: Script = self.script
|
||||||
|
unresolvable: Set[str] = self.unresolvable
|
||||||
if entry:
|
if entry:
|
||||||
script = entry.script
|
script = entry.script
|
||||||
unresolvable = entry.unresolvable
|
unresolvable = entry.unresolvable
|
||||||
else:
|
|
||||||
script = self.script
|
|
||||||
unresolvable = self.unresolvable
|
|
||||||
|
|
||||||
if function_overrides:
|
return str(
|
||||||
script = copy.deepcopy(script).add(function_overrides)
|
script.resolve_once(
|
||||||
|
dict({"tmp_var": formatter.format_string}, **(function_overrides or {})),
|
||||||
return str(script.resolve_once(formatter.format_string, unresolvable=unresolvable))
|
unresolvable=unresolvable,
|
||||||
|
)["tmp_var"]
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -259,7 +259,7 @@ class Preset(_PresetShell):
|
||||||
) -> None:
|
) -> None:
|
||||||
try:
|
try:
|
||||||
self._script.resolve_once(
|
self._script.resolve_once(
|
||||||
formatter_validator.format_string,
|
{"tmp_var": formatter_validator.format_string},
|
||||||
unresolvable=self._get_unresolvable_variables(formatter_validator),
|
unresolvable=self._get_unresolvable_variables(formatter_validator),
|
||||||
)
|
)
|
||||||
except VariableDoesNotExist as exc:
|
except VariableDoesNotExist as exc:
|
||||||
|
|
|
||||||
|
|
@ -259,13 +259,17 @@ class MultiUrlValidator(OptionsValidator):
|
||||||
"""
|
"""
|
||||||
# Apply formatting to each new source variable, ensure it resolves
|
# Apply formatting to each new source variable, ensure it resolves
|
||||||
for collection_url in self.urls.list:
|
for collection_url in self.urls.list:
|
||||||
for name, definition in collection_url.variables.dict_with_format_strings.items():
|
script.resolve_once(collection_url.variables.dict_with_format_strings)
|
||||||
script.resolve_once(variable_name=name, variable_definition=definition)
|
|
||||||
|
|
||||||
# Ensure at least URL is non-empty
|
# Ensure at least URL is non-empty
|
||||||
has_non_empty_url = False
|
has_non_empty_url = False
|
||||||
for url_validator in self.urls.list:
|
url_variables = {
|
||||||
has_non_empty_url |= bool(str(script.resolve_once(url_validator.url.format_string)))
|
f"tmp_var_url_{idx}": url_validator.url.format_string
|
||||||
|
for idx, url_validator in enumerate(self.urls.list)
|
||||||
|
}
|
||||||
|
output = script.resolve_once(url_variables)
|
||||||
|
for out in output:
|
||||||
|
has_non_empty_url |= bool(str(out))
|
||||||
|
|
||||||
if not has_non_empty_url:
|
if not has_non_empty_url:
|
||||||
raise self._validation_exception("Must contain at least one url that is non-empty")
|
raise self._validation_exception("Must contain at least one url that is non-empty")
|
||||||
|
|
|
||||||
|
|
@ -331,6 +331,10 @@ class Script:
|
||||||
self._update_internally(resolved_variables=resolved_variables)
|
self._update_internally(resolved_variables=resolved_variables)
|
||||||
|
|
||||||
if output_filter:
|
if output_filter:
|
||||||
|
for name in output_filter:
|
||||||
|
if name not in resolved_variables:
|
||||||
|
raise ValueError(f"Specified {name} to resolve, but it did not")
|
||||||
|
|
||||||
return ScriptOutput(
|
return ScriptOutput(
|
||||||
{
|
{
|
||||||
name: resolvable
|
name: resolvable
|
||||||
|
|
@ -369,20 +373,21 @@ class Script:
|
||||||
|
|
||||||
def resolve_once(
|
def resolve_once(
|
||||||
self,
|
self,
|
||||||
variable_definition: str,
|
variable_definitions: Dict[str, str],
|
||||||
variable_name: Optional[str] = None,
|
|
||||||
resolved: Optional[Dict[str, Resolvable]] = None,
|
resolved: Optional[Dict[str, Resolvable]] = None,
|
||||||
unresolvable: Optional[Set[str]] = None,
|
unresolvable: Optional[Set[str]] = None,
|
||||||
) -> Resolvable:
|
) -> Dict[str, Resolvable]:
|
||||||
var_name = variable_name if variable_name else "tmp_var"
|
|
||||||
try:
|
try:
|
||||||
self.add({var_name: variable_definition})
|
self.add(variable_definitions)
|
||||||
return self._resolve(
|
return self._resolve(
|
||||||
pre_resolved=resolved, unresolvable=unresolvable, output_filter={var_name}
|
pre_resolved=resolved,
|
||||||
).get(var_name)
|
unresolvable=unresolvable,
|
||||||
|
output_filter=set(list(variable_definitions.keys())),
|
||||||
|
).output
|
||||||
finally:
|
finally:
|
||||||
if var_name in self._variables:
|
for name in variable_definitions.keys():
|
||||||
del self._variables[var_name]
|
if name in self._variables:
|
||||||
|
del self._variables[name]
|
||||||
|
|
||||||
def get(self, variable_name: str) -> Resolvable:
|
def get(self, variable_name: str) -> Resolvable:
|
||||||
if variable_name not in self._variables:
|
if variable_name not in self._variables:
|
||||||
|
|
|
||||||
|
|
@ -153,6 +153,9 @@ class TestPrebuiltTVShowPresets:
|
||||||
is_youtube_channel: bool,
|
is_youtube_channel: bool,
|
||||||
is_many_urls: bool,
|
is_many_urls: bool,
|
||||||
):
|
):
|
||||||
|
# yappi.set_clock_type("wall") # Use set_clock_type("wall") for wall time
|
||||||
|
# yappi.start()
|
||||||
|
|
||||||
expected_summary_name = "unit/{}/{}/is_yt_{}{}".format(
|
expected_summary_name = "unit/{}/{}/is_yt_{}{}".format(
|
||||||
media_player_preset,
|
media_player_preset,
|
||||||
tv_show_structure_preset,
|
tv_show_structure_preset,
|
||||||
|
|
@ -180,9 +183,6 @@ class TestPrebuiltTVShowPresets:
|
||||||
preset_dict=preset_dict,
|
preset_dict=preset_dict,
|
||||||
)
|
)
|
||||||
|
|
||||||
# yappi.set_clock_type("wall") # Use set_clock_type("wall") for wall time
|
|
||||||
# yappi.start()
|
|
||||||
|
|
||||||
with mock_download_collection_entries(
|
with mock_download_collection_entries(
|
||||||
is_youtube_channel=is_youtube_channel, num_urls=2 if is_many_urls else 1
|
is_youtube_channel=is_youtube_channel, num_urls=2 if is_many_urls else 1
|
||||||
):
|
):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue