more organize

This commit is contained in:
Jesse Bannon 2023-09-20 13:50:16 -07:00
parent 6c1ce8b820
commit 6b2cd52313
10 changed files with 146 additions and 131 deletions

View file

@ -1,4 +1,4 @@
from ytdl_sub.script.types import String
from ytdl_sub.script.types.resolvable import String
class StringFunctions:

View file

@ -6,7 +6,7 @@ from typing import Set
from ytdl_sub.script.functions import Resolvable
from ytdl_sub.script.parser import parse
from ytdl_sub.script.syntax_tree import SyntaxTree
from ytdl_sub.script.syntax_tree import Variable
from ytdl_sub.script.types.variable import Variable
from ytdl_sub.utils.exceptions import StringFormattingException

View file

@ -1,14 +1,14 @@
from typing import List
from typing import Optional
from ytdl_sub.script.syntax_tree import ArgumentType
from ytdl_sub.script.syntax_tree import Function
from ytdl_sub.script.syntax_tree import SyntaxTree
from ytdl_sub.script.syntax_tree import Variable
from ytdl_sub.script.types import Boolean
from ytdl_sub.script.types import Float
from ytdl_sub.script.types import Integer
from ytdl_sub.script.types import String
from ytdl_sub.script.types.function import ArgumentType
from ytdl_sub.script.types.function import Function
from ytdl_sub.script.types.resolvable import Boolean
from ytdl_sub.script.types.resolvable import Float
from ytdl_sub.script.types.resolvable import Integer
from ytdl_sub.script.types.resolvable import String
from ytdl_sub.script.types.variable import Variable
from ytdl_sub.utils.exceptions import StringFormattingException
from ytdl_sub.validators.string_formatter_validators import is_valid_source_variable_name

View file

@ -1,83 +1,17 @@
from abc import ABC
from abc import abstractmethod
from dataclasses import dataclass
from typing import Dict
from typing import List
from typing import Optional
from typing import Set
from typing import Union
from typing import final
from ytdl_sub.script.functions import Functions
from ytdl_sub.script.types import Boolean
from ytdl_sub.script.types import Float
from ytdl_sub.script.types import Integer
from ytdl_sub.script.types import Resolvable
from ytdl_sub.script.types import String
from ytdl_sub.script.types.function import Function
from ytdl_sub.script.types.function import VariableDependency
from ytdl_sub.script.types.resolvable import Resolvable
from ytdl_sub.script.types.resolvable import String
from ytdl_sub.script.types.variable import Variable
from ytdl_sub.utils.exceptions import StringFormattingException
@dataclass(frozen=True)
class Variable:
name: str
ArgumentType = Union[Integer, Float, String, Boolean, Variable, "Function"]
@dataclass(frozen=True)
class VariableDependency(ABC):
@property
@abstractmethod
def variables(self) -> Set[Variable]:
raise NotImplemented()
@abstractmethod
def resolve(self, resolved_variables: Dict[Variable, Resolvable]) -> str:
raise NotImplemented()
@final
def has_variable_dependency(self, resolved_variables: Dict[Variable, Resolvable]) -> bool:
"""
Returns
-------
True if variable dependency. False otherwise.
"""
return self.variables.issubset(set(resolved_variables.keys()))
@dataclass(frozen=True)
class Function(VariableDependency):
name: str
args: List[ArgumentType]
def __post_init__(self):
# TODO: Figure out resolution via introspecting args and outputs of function
try:
getattr(Functions, self.name)
except AttributeError:
raise StringFormattingException(f"Function name {self.name} does not exist")
@property
def variables(self) -> Set[Variable]:
"""
Returns
-------
All variables used within the function
"""
variables: Set[Variable] = set()
for arg in self.args:
if isinstance(arg, Variable):
variables.add(arg)
elif isinstance(arg, Function):
variables.update(arg.variables)
return variables
def resolve(self, resolved_variables: Dict[Variable, Resolvable]) -> Resolvable:
raise NotImplemented()
@dataclass(frozen=True)
class SyntaxTree(VariableDependency):
ast: List[String | Variable | Function]

View file

@ -1,48 +1,3 @@
from abc import ABC
from abc import abstractmethod
from dataclasses import dataclass
from typing import Generic
from typing import TypeVar
T = TypeVar("T")
NumericT = TypeVar("NumericT", bound=int | float)
@dataclass(frozen=True)
class Resolvable(ABC):
@abstractmethod
def resolve(self) -> str:
...
@dataclass(frozen=True)
class ResolvableT(Resolvable, Generic[T]):
value: T
def resolve(self) -> str:
return str(self.value)
@dataclass(frozen=True)
class Numeric(ResolvableT[NumericT], ABC, Generic[NumericT]):
pass
@dataclass(frozen=True)
class Integer(ResolvableT[int]):
pass
@dataclass(frozen=True)
class Float(ResolvableT[float]):
pass
@dataclass(frozen=True)
class Boolean(ResolvableT[bool]):
pass
@dataclass(frozen=True)
class String(ResolvableT[str]):
pass

View file

View file

@ -0,0 +1,72 @@
from abc import ABC
from abc import abstractmethod
from dataclasses import dataclass
from typing import Dict
from typing import List
from typing import Set
from typing import Union
from typing import final
from ytdl_sub.script.functions import Functions
from ytdl_sub.script.types.resolvable import Boolean
from ytdl_sub.script.types.resolvable import Float
from ytdl_sub.script.types.resolvable import Integer
from ytdl_sub.script.types.resolvable import Resolvable
from ytdl_sub.script.types.resolvable import String
from ytdl_sub.script.types.variable import Variable
from ytdl_sub.utils.exceptions import StringFormattingException
ArgumentType = Union[Integer, Float, String, Boolean, Variable, "Function"]
@dataclass(frozen=True)
class VariableDependency(ABC):
@property
@abstractmethod
def variables(self) -> Set[Variable]:
raise NotImplemented()
@abstractmethod
def resolve(self, resolved_variables: Dict[Variable, Resolvable]) -> str:
raise NotImplemented()
@final
def has_variable_dependency(self, resolved_variables: Dict[Variable, Resolvable]) -> bool:
"""
Returns
-------
True if variable dependency. False otherwise.
"""
return self.variables.issubset(set(resolved_variables.keys()))
@dataclass(frozen=True)
class Function(VariableDependency):
name: str
args: List[ArgumentType]
def __post_init__(self):
# TODO: Figure out resolution via introspecting args and outputs of function
try:
getattr(Functions, self.name)
except AttributeError:
raise StringFormattingException(f"Function name {self.name} does not exist")
@property
def variables(self) -> Set[Variable]:
"""
Returns
-------
All variables used within the function
"""
variables: Set[Variable] = set()
for arg in self.args:
if isinstance(arg, Variable):
variables.add(arg)
elif isinstance(arg, Function):
variables.update(arg.variables)
return variables
def resolve(self, resolved_variables: Dict[Variable, Resolvable]) -> Resolvable:
raise NotImplemented()

View file

@ -0,0 +1,48 @@
from abc import ABC
from abc import abstractmethod
from dataclasses import dataclass
from typing import Generic
from typing import TypeVar
T = TypeVar("T")
NumericT = TypeVar("NumericT", bound=int | float)
@dataclass(frozen=True)
class Resolvable(ABC):
@abstractmethod
def resolve(self) -> str:
...
@dataclass(frozen=True)
class ResolvableT(Resolvable, Generic[T]):
value: T
def resolve(self) -> str:
return str(self.value)
@dataclass(frozen=True)
class Numeric(ResolvableT[NumericT], ABC, Generic[NumericT]):
pass
@dataclass(frozen=True)
class Integer(ResolvableT[int]):
pass
@dataclass(frozen=True)
class Float(ResolvableT[float]):
pass
@dataclass(frozen=True)
class Boolean(ResolvableT[bool]):
pass
@dataclass(frozen=True)
class String(ResolvableT[str]):
pass

View file

@ -0,0 +1,6 @@
from dataclasses import dataclass
@dataclass(frozen=True)
class Variable:
name: str

View file

@ -1,13 +1,13 @@
import pytest
from ytdl_sub.script.parser import parse
from ytdl_sub.script.syntax_tree import Function
from ytdl_sub.script.syntax_tree import SyntaxTree
from ytdl_sub.script.syntax_tree import Variable
from ytdl_sub.script.types import Boolean
from ytdl_sub.script.types import Float
from ytdl_sub.script.types import Integer
from ytdl_sub.script.types import String
from ytdl_sub.script.types.function import Function
from ytdl_sub.script.types.resolvable import Boolean
from ytdl_sub.script.types.resolvable import Float
from ytdl_sub.script.types.resolvable import Integer
from ytdl_sub.script.types.resolvable import String
from ytdl_sub.script.types.variable import Variable
from ytdl_sub.utils.exceptions import StringFormattingException