[BACKEND] Optimize script initialization
This commit is contained in:
parent
2dbe426ced
commit
01a650fa36
3 changed files with 36 additions and 17 deletions
|
|
@ -133,6 +133,8 @@ class Overrides(DictFormatterValidator, Scriptable):
|
||||||
"""
|
"""
|
||||||
Initialize the override script with override variables + any unresolved variables
|
Initialize the override script with override variables + any unresolved variables
|
||||||
"""
|
"""
|
||||||
|
self.initialize_base_script()
|
||||||
|
|
||||||
self.script.add(
|
self.script.add(
|
||||||
ScriptUtils.add_sanitized_variables(
|
ScriptUtils.add_sanitized_variables(
|
||||||
{SubscriptionVariables.subscription_name(): subscription_name}
|
{SubscriptionVariables.subscription_name(): subscription_name}
|
||||||
|
|
|
||||||
|
|
@ -44,12 +44,6 @@ class Entry(BaseEntry, Scriptable):
|
||||||
BaseEntry.__init__(self, entry_dict=entry_dict, working_directory=working_directory)
|
BaseEntry.__init__(self, entry_dict=entry_dict, working_directory=working_directory)
|
||||||
Scriptable.__init__(self)
|
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":
|
def initialize_script(self, other: Optional[Scriptable] = None) -> "Entry":
|
||||||
"""
|
"""
|
||||||
Initializes the entry script using the Overrides script, then adding
|
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
|
# Overrides contains added variables that are unresolvable, add them here
|
||||||
if other:
|
if other:
|
||||||
self.script = copy.deepcopy(other.script)
|
self._script = copy.deepcopy(other.script)
|
||||||
self.unresolvable = copy.deepcopy(other.unresolvable)
|
self._unresolvable = copy.deepcopy(other.unresolvable)
|
||||||
|
else:
|
||||||
|
self.initialize_base_script()
|
||||||
|
|
||||||
self._add_entry_kwargs_to_script()
|
self._add_entry_kwargs_to_script()
|
||||||
return self
|
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:
|
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.
|
Gets a variable of an expected type. Will error if it does not exist or is not resolved.
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import copy
|
||||||
from abc import ABC
|
from abc import ABC
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
from typing import Optional
|
||||||
from typing import Set
|
from typing import Set
|
||||||
|
|
||||||
from ytdl_sub.entries.script.function_scripts import CUSTOM_FUNCTION_SCRIPTS
|
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.exceptions import StringFormattingException
|
||||||
from ytdl_sub.utils.script import ScriptUtils
|
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):
|
class Scriptable(ABC):
|
||||||
"""
|
"""
|
||||||
Shared class between Entry and Overrides to manage their underlying Script.
|
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):
|
def __init__(self):
|
||||||
self.script = copy.deepcopy(Scriptable._BASE_SCRIPT)
|
self._script: Optional[Script] = None
|
||||||
self.unresolvable: Set[str] = copy.deepcopy(UNRESOLVED_VARIABLES)
|
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:
|
def update_script(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
@ -45,7 +60,7 @@ class Scriptable(ABC):
|
||||||
for var, definition in values.items()
|
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(
|
self.script.add(
|
||||||
ScriptUtils.add_sanitized_variables(
|
ScriptUtils.add_sanitized_variables(
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue