ytdl-sub/src/ytdl_sub/script/script_output.py
Jesse Bannon e92b1cd12a
[BACKEND][HUGE] Function Support in variable syntax (#838)
A complete gutting of the internals of ytdl-sub to support functions in our variable syntax, in addition to being able to access a yt-dlp entry's .info.json fields using functions. Functionally, ytdl-sub should still look and behave the same from a user-perspective.

With so many lines of code changed (+8927, -2708), no doubt there will be new issues. Please make a GH issue or reach out on Discord if your config/subscriptions break in any way/shape/form.

Details on how to use function support will come soon in the form of proper documentation in our readthedocs.
2023-12-18 16:08:15 -08:00

52 lines
1.3 KiB
Python

from dataclasses import dataclass
from typing import Any
from typing import Dict
from ytdl_sub.script.types.resolvable import Resolvable
from ytdl_sub.script.utils.exceptions import ScriptVariableNotResolved
@dataclass(frozen=True)
class ScriptOutput:
output: Dict[str, Resolvable]
def as_native(self) -> Dict[str, Any]:
"""
Returns
-------
The script output as native python types
"""
return {name: out.native for name, out in self.output.items()}
def get(self, name: str) -> Resolvable:
"""
Returns
-------
The script output's variable as a resolvable type
Raises
------
ScriptVariableNotResolved
The variable name requested did not resolve
"""
if name not in self.output:
raise ScriptVariableNotResolved(
f"Tried to access resolved variable {name}, but it has not resolved"
)
return self.output[name]
def get_native(self, name: str) -> Any:
"""
Returns
-------
The script output's variable as native python type
"""
return self.get(name).native
def get_str(self, name: str) -> str:
"""
Returns
-------
The script output's variable as a string
"""
return str(self.get(name))