From 67364b29e59f4cadce281f118e68c8006e0545ab Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Sun, 9 Jul 2023 15:52:24 -0700 Subject: [PATCH] functions WIP --- src/ytdl_sub/script/__init__.py | 0 src/ytdl_sub/script/functions.py | 31 +++++++++++++ src/ytdl_sub/script/parser.py | 75 ++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 src/ytdl_sub/script/__init__.py create mode 100644 src/ytdl_sub/script/functions.py create mode 100644 src/ytdl_sub/script/parser.py diff --git a/src/ytdl_sub/script/__init__.py b/src/ytdl_sub/script/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/ytdl_sub/script/functions.py b/src/ytdl_sub/script/functions.py new file mode 100644 index 00000000..c4095fba --- /dev/null +++ b/src/ytdl_sub/script/functions.py @@ -0,0 +1,31 @@ + + +class Functions: + + @staticmethod + def lower(string: str) -> str: + """ + Returns + ------- + Lower-cased string + """ + return string.lower() + + @staticmethod + def upper(string: str) -> str: + """ + Returns + ------- + Upper-cased string + """ + return string.upper() + + @staticmethod + def capitalize(string: str) -> str: + """ + Returns + ------- + Capitalized string + """ + return string.capitalize() + \ No newline at end of file diff --git a/src/ytdl_sub/script/parser.py b/src/ytdl_sub/script/parser.py new file mode 100644 index 00000000..c61dce0a --- /dev/null +++ b/src/ytdl_sub/script/parser.py @@ -0,0 +1,75 @@ +from dataclasses import dataclass +from queue import LifoQueue +from typing import Optional, List + +from ytdl_sub.validators.string_formatter_validators import is_valid_source_variable_name + +@dataclass +class Variable: + name: str + +@dataclass +class Function: + name: str + args: List[str] + + +class Parser: + + def __init__(self, text: str): + self._text = text + self._pos = 0 + self._stack: LifoQueue[Variable | Function] = LifoQueue() + + def read(self) -> Optional[str]: + try: + ch = self._text[self._pos] + except IndexError: + return None + + self._pos += 1 + return ch + + def parse_variable(self) -> Variable: + var_name = "" + while ch := self.read(): + if ch == "}": + break + var_name += ch + + _ = is_valid_source_variable_name(var_name, raise_exception=True) + return Variable(var_name) + + def parse_function(self) -> Function: + parenthesis_counter = 0 + func_name = "" + func_args = "" + + while ch := self.read(): + if ch not in ['(', ')']: + if parenthesis_counter > 0: + func_args += ch + else: + func_name += ch + elif ch == '(': + parenthesis_counter += 1 + elif ch == ')': + parenthesis_counter -= 1 + if parenthesis_counter == 0: + break + + + + + + + + def parse(self): + while True: + ch = self.read() + if ch == '{': + self.parse_variable() + if ch == '%': + self.parse_function() + +