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.
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
import re
|
|
|
|
from ytdl_sub.script.functions import Functions
|
|
from ytdl_sub.script.utils.exceptions import InvalidFunctionName
|
|
from ytdl_sub.script.utils.exceptions import InvalidVariableName
|
|
|
|
_NAME_REGEX_VALIDATOR = re.compile(r"^[a-z][a-z0-9_]*$")
|
|
|
|
|
|
def is_valid_name(name: str) -> bool:
|
|
"""
|
|
Returns
|
|
-------
|
|
True if the name adheres to the ``snake_case`` format. False otherwise.
|
|
"""
|
|
return re.match(_NAME_REGEX_VALIDATOR, name) is not None
|
|
|
|
|
|
def validate_variable_name(variable_name: str) -> str:
|
|
"""
|
|
Raises
|
|
------
|
|
InvalidVariableName
|
|
if the variable name is invalid
|
|
"""
|
|
if not is_valid_name(variable_name):
|
|
raise InvalidVariableName(
|
|
f"Variable name '{variable_name}' is invalid:"
|
|
" Names must be lower_snake_cased and begin with a letter."
|
|
)
|
|
|
|
if Functions.is_built_in(variable_name):
|
|
raise InvalidVariableName(
|
|
f"Variable name '{variable_name}' is invalid:"
|
|
" The name is used by a built-in function and cannot be overwritten."
|
|
)
|
|
|
|
return variable_name
|
|
|
|
|
|
def validate_custom_function_name(custom_function_name: str) -> None:
|
|
"""
|
|
Raises
|
|
------
|
|
InvalidFunctionName
|
|
If the function name is invalid
|
|
InvalidVariableName
|
|
if the variable name is invalid
|
|
"""
|
|
if not is_valid_name(custom_function_name):
|
|
raise InvalidFunctionName(
|
|
f"Custom function name '%{custom_function_name}' is invalid:"
|
|
" Names must be %lower_snake_cased and begin with a letter."
|
|
)
|
|
|
|
if Functions.is_built_in(custom_function_name):
|
|
raise InvalidFunctionName(
|
|
f"Custom function name '%{custom_function_name}' is invalid:"
|
|
" The name is used by a built-in function and cannot be overwritten."
|
|
)
|