From 01a650fa36db6e410167a32f869939072a681956 Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Thu, 4 Jan 2024 15:38:58 -0800 Subject: [PATCH] [BACKEND] Optimize script initialization --- src/ytdl_sub/config/overrides.py | 2 ++ src/ytdl_sub/entries/entry.py | 18 +++++++++-------- src/ytdl_sub/utils/scriptable.py | 33 +++++++++++++++++++++++--------- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/ytdl_sub/config/overrides.py b/src/ytdl_sub/config/overrides.py index f68ce727..f46ea5a7 100644 --- a/src/ytdl_sub/config/overrides.py +++ b/src/ytdl_sub/config/overrides.py @@ -133,6 +133,8 @@ class Overrides(DictFormatterValidator, Scriptable): """ Initialize the override script with override variables + any unresolved variables """ + self.initialize_base_script() + self.script.add( ScriptUtils.add_sanitized_variables( {SubscriptionVariables.subscription_name(): subscription_name} diff --git a/src/ytdl_sub/entries/entry.py b/src/ytdl_sub/entries/entry.py index 1d684c5b..831bda6c 100644 --- a/src/ytdl_sub/entries/entry.py +++ b/src/ytdl_sub/entries/entry.py @@ -44,12 +44,6 @@ class Entry(BaseEntry, Scriptable): BaseEntry.__init__(self, entry_dict=entry_dict, working_directory=working_directory) Scriptable.__init__(self) - def _add_entry_kwargs_to_script(self) -> None: - # Add entry metadata, but avoid the `.add()` helper since it also adds sanitized - self.unresolvable.remove(v.entry_metadata.variable_name) - self.script.add({v.entry_metadata.variable_name: ScriptUtils.to_script(self._kwargs)}) - self.update_script() - def initialize_script(self, other: Optional[Scriptable] = None) -> "Entry": """ Initializes the entry script using the Overrides script, then adding @@ -57,12 +51,20 @@ class Entry(BaseEntry, Scriptable): """ # Overrides contains added variables that are unresolvable, add them here if other: - self.script = copy.deepcopy(other.script) - self.unresolvable = copy.deepcopy(other.unresolvable) + self._script = copy.deepcopy(other.script) + self._unresolvable = copy.deepcopy(other.unresolvable) + else: + self.initialize_base_script() self._add_entry_kwargs_to_script() return self + def _add_entry_kwargs_to_script(self) -> None: + # Add entry metadata, but avoid the `.add()` helper since it also adds sanitized + self.unresolvable.remove(v.entry_metadata.variable_name) + self.script.add({v.entry_metadata.variable_name: ScriptUtils.to_script(self._kwargs)}) + self.update_script() + def get(self, variable: Variable, expected_type: Type[TypeT]) -> TypeT: """ Gets a variable of an expected type. Will error if it does not exist or is not resolved. diff --git a/src/ytdl_sub/utils/scriptable.py b/src/ytdl_sub/utils/scriptable.py index aba9d1fc..a6becdcc 100644 --- a/src/ytdl_sub/utils/scriptable.py +++ b/src/ytdl_sub/utils/scriptable.py @@ -2,6 +2,7 @@ import copy from abc import ABC from typing import Any from typing import Dict +from typing import Optional from typing import Set from ytdl_sub.entries.script.function_scripts import CUSTOM_FUNCTION_SCRIPTS @@ -13,21 +14,35 @@ from ytdl_sub.script.utils.exceptions import RuntimeException from ytdl_sub.utils.exceptions import StringFormattingException from ytdl_sub.utils.script import ScriptUtils +_BASE_SCRIPT: Script = Script( + ScriptUtils.add_sanitized_variables( + dict(copy.deepcopy(VARIABLE_SCRIPTS), **copy.deepcopy(CUSTOM_FUNCTION_SCRIPTS)) + ) +) + class Scriptable(ABC): """ Shared class between Entry and Overrides to manage their underlying Script. """ - _BASE_SCRIPT: Script = Script( - ScriptUtils.add_sanitized_variables( - dict(copy.deepcopy(VARIABLE_SCRIPTS), **copy.deepcopy(CUSTOM_FUNCTION_SCRIPTS)) - ) - ) - def __init__(self): - self.script = copy.deepcopy(Scriptable._BASE_SCRIPT) - self.unresolvable: Set[str] = copy.deepcopy(UNRESOLVED_VARIABLES) + self._script: Optional[Script] = None + self._unresolvable: Optional[Set[str]] = None + + def initialize_base_script(self): + self._script = copy.deepcopy(_BASE_SCRIPT) + self._unresolvable = copy.deepcopy(UNRESOLVED_VARIABLES) + + @property + def script(self) -> Script: + assert self._script is not None, "Not initialized" + return self._script + + @property + def unresolvable(self) -> Set[str]: + assert self._unresolvable is not None, "Not initialized" + return self._unresolvable def update_script(self) -> None: """ @@ -45,7 +60,7 @@ class Scriptable(ABC): for var, definition in values.items() } - self.unresolvable -= set(list(values_as_str.keys())) + self._unresolvable -= set(list(values_as_str.keys())) self.script.add( ScriptUtils.add_sanitized_variables( {