bless yappi

This commit is contained in:
Jesse Bannon 2023-12-08 00:31:09 -08:00
parent fb58941ff5
commit 72eeb4443f
6 changed files with 27 additions and 37 deletions

View file

@ -180,7 +180,7 @@ class Preset(_PresetShell):
return added_variables
@functools.cached_property
def _cached_script_builder(self) -> ScriptBuilder:
def _script_builder(self) -> ScriptBuilder:
# Set the formatter variables to be the overrides
script = ScriptBuilder(
Scriptable.add_sanitized_variables(self.overrides.dict_with_format_strings)
@ -194,28 +194,16 @@ class Preset(_PresetShell):
_ = script.partial_build()
return script
@property
def _script_builder(self) -> ScriptBuilder:
return copy.deepcopy(self._cached_script_builder)
@functools.cached_property
def _script_builder_with_added_variables(self) -> ScriptBuilder:
def _script(self) -> Script:
"""
Contains actualized script which should hold all Override variables
"""
return self._script_builder.add(
Scriptable.add_sanitized_variables(
{source_var: "dummy_string" for source_var in self._added_variables}
)
)
@functools.cached_property
def _cached_script(self) -> Script:
"""
Contains actualized script which should hold all Override variables
"""
return self._script_builder_with_added_variables.partial_build()
@property
def _script(self) -> Script:
return copy.deepcopy(self._cached_script)
).partial_build()
def __validate_and_get_plugins(self) -> PresetPlugins:
preset_plugins = PresetPlugins()
@ -232,8 +220,8 @@ class Preset(_PresetShell):
return preset_plugins
def __validate_added_variables(self):
script_builder = self._script_builder
self.downloader_options.validate_with_variables(script=copy.deepcopy(script_builder))
script_builder = copy.deepcopy(self._script_builder)
self.downloader_options.validate_with_variables(script=script_builder.partial_build())
script_builder.add(
Scriptable.add_sanitized_variables(
{name: "dummy_string" for name in self.downloader_options.added_source_variables()}
@ -244,7 +232,7 @@ class Preset(_PresetShell):
self.plugins.zipped(), key=lambda pl: pl[0].priority.modify_entry
):
# Validate current plugin using source + added plugin variables
plugin_options.validate_with_variables(script=copy.deepcopy(script_builder))
plugin_options.validate_with_variables(script=script_builder.partial_build())
script_builder.add(
Scriptable.add_sanitized_variables(
{

View file

@ -54,7 +54,7 @@ class OptionsValidator(Validator, ABC):
"""
return []
def validate_with_variables(self, script: ScriptBuilder) -> None:
def validate_with_variables(self, script: Script) -> None:
"""
Optional validation after init with the session's source and override variables.

View file

@ -253,21 +253,19 @@ class MultiUrlValidator(OptionsValidator):
"""
return list(self._urls.list[0].variables.keys)
def validate_with_variables(self, script: ScriptBuilder) -> None:
def validate_with_variables(self, script: Script) -> None:
"""
Ensures new variables added are not existing variables
"""
# Apply formatting to each new source variable, ensure it resolves
for collection_url in self.urls.list:
script.add(collection_url.variables.dict_with_format_strings)
resolved_script = script.partial_build()
for name, definition in collection_url.variables.dict_with_format_strings.items():
script.is_resolvable(variable_name=name, variable_definition=definition)
# Ensure at least URL is non-empty
has_non_empty_url = False
for url_validator in self.urls.list:
resolved_script.add({"tmp_var_url": url_validator.url.format_string})
has_non_empty_url |= bool(str(resolved_script.resolve().get_native("tmp_var_url")))
has_non_empty_url |= bool(str(script.is_resolvable(url_validator.url.format_string)))
if not has_non_empty_url:
raise self._validation_exception("Must contain at least one url that is non-empty")

View file

@ -212,7 +212,7 @@ class RegexOptions(OptionsDictValidator):
"""
return self._skip_if_match_fails
def validate_with_variables(self, script: ScriptBuilder) -> None:
def validate_with_variables(self, script: Script) -> None:
for key, regex_options in self.source_variable_capture_dict.items():
# Ensure each variable getting captured is a source variable
if key not in script._variables:

View file

@ -281,14 +281,13 @@ class Script:
)
unresolvable: Set[Variable] = {Variable(name) for name in (unresolvable or {})}
unresolved_filter = set(resolved.keys()).union(unresolvable)
unresolved: Dict[Variable, SyntaxTree] = {
Variable(name): ast
for name, ast in self._variables.items()
if Variable(name) not in set(resolved.keys()).union(unresolvable)
if Variable(name) not in unresolved_filter
}
tmp = 0
while unresolved:
unresolved_count: int = len(unresolved)
@ -354,17 +353,19 @@ class Script:
def is_resolvable(
self,
variable_definition: str,
variable_name: Optional[str] = None,
resolved: Optional[Dict[str, Resolvable]] = None,
unresolvable: Optional[Set[str]] = None,
) -> Resolvable:
var_name = variable_name if variable_name else "tmp_var"
try:
self.add({"tmp_var": variable_definition})
self.add({var_name: variable_definition})
return self._resolve(
resolved=resolved, unresolvable=unresolvable, output_filter={"tmp_var"}
).get("tmp_var")
resolved=resolved, unresolvable=unresolvable, output_filter={var_name}
).get(var_name)
finally:
if "tmp_var" in self._variables:
del self._variables["tmp_var"]
if var_name in self._variables:
del self._variables[var_name]
def get(self, variable_name: str) -> Resolvable:
if variable_name not in self._variables:

View file

@ -66,6 +66,8 @@ class TestPrebuiltTVShowPresets:
media_player_preset: str,
tv_show_structure_preset: str,
):
# yappi.set_clock_type("cpu") # Use set_clock_type("wall") for wall time
# yappi.start()
parent_presets = _tv_show_by_date_parent_presets(
all_in_one_preset, media_player_preset, tv_show_structure_preset
)
@ -82,6 +84,7 @@ class TestPrebuiltTVShowPresets:
},
},
)
# yappi.get_func_stats().print_all()
def test_compilation_many_urls(
self,