iterable arguments

This commit is contained in:
Jesse Bannon 2023-11-21 18:47:53 -08:00
parent 0fffc79cfb
commit d3e127783e
6 changed files with 37 additions and 42 deletions

View file

@ -27,12 +27,8 @@ class UnresolvedArray(Array, VariableDependency, FutureResolvable):
value: List[ArgumentType] value: List[ArgumentType]
@property @property
def variables(self) -> Set[Variable]: def _iterable_arguments(self) -> List[ArgumentType]:
return self._variables(*self.value) return self.value
@property
def function_arguments(self) -> Set[FunctionArgument]:
return self._function_arguments(*self.value)
def resolve( def resolve(
self, self,

View file

@ -40,12 +40,8 @@ from ytdl_sub.utils.exceptions import StringFormattingException
@dataclass(frozen=True) @dataclass(frozen=True)
class Function(FunctionType, VariableDependency, ABC): class Function(FunctionType, VariableDependency, ABC):
@property @property
def variables(self) -> Set[Variable]: def _iterable_arguments(self) -> List[ArgumentType]:
return self._variables(*self.args) return self.args
@property
def function_arguments(self) -> Set[FunctionArgument]:
return self._function_arguments(*self.args)
@classmethod @classmethod
def from_name_and_args(cls, name: str, args: List[ArgumentType]) -> "Function": def from_name_and_args(cls, name: str, args: List[ArgumentType]) -> "Function":

View file

@ -30,17 +30,9 @@ class UnresolvedMap(Map, VariableDependency, FutureResolvable):
value: Dict[ArgumentType, ArgumentType] value: Dict[ArgumentType, ArgumentType]
@property @property
def _as_flat_list(self) -> List[ArgumentType]: def _iterable_arguments(self) -> List[ArgumentType]:
return list(itertools.chain(*self.value.items())) return list(itertools.chain(*self.value.items()))
@property
def variables(self) -> Set[Variable]:
return self._variables(*self._as_flat_list)
@property
def function_arguments(self) -> Set[FunctionArgument]:
return self._function_arguments(*self._as_flat_list)
def resolve( def resolve(
self, self,
resolved_variables: Dict[Variable, Resolvable], resolved_variables: Dict[Variable, Resolvable],

View file

@ -121,6 +121,11 @@ class String(ResolvableT[str], Hashable, ArgumentType):
pass pass
@dataclass(frozen=True)
class NamedCustomFunction(ArgumentType, ABC):
name: str
@dataclass(frozen=True) @dataclass(frozen=True)
class FunctionType(ArgumentType, ABC): class FunctionType(ArgumentType, ABC):
name: str name: str

View file

@ -2,12 +2,10 @@ from dataclasses import dataclass
from typing import Dict from typing import Dict
from typing import List from typing import List
from typing import Optional from typing import Optional
from typing import Set
from ytdl_sub.script.types.resolvable import ArgumentType from ytdl_sub.script.types.resolvable import ArgumentType
from ytdl_sub.script.types.resolvable import Resolvable from ytdl_sub.script.types.resolvable import Resolvable
from ytdl_sub.script.types.resolvable import String from ytdl_sub.script.types.resolvable import String
from ytdl_sub.script.types.variable import FunctionArgument
from ytdl_sub.script.types.variable import Variable from ytdl_sub.script.types.variable import Variable
from ytdl_sub.script.types.variable_dependency import VariableDependency from ytdl_sub.script.types.variable_dependency import VariableDependency
from ytdl_sub.utils.exceptions import StringFormattingException from ytdl_sub.utils.exceptions import StringFormattingException
@ -18,12 +16,8 @@ class SyntaxTree(VariableDependency):
ast: List[ArgumentType] ast: List[ArgumentType]
@property @property
def variables(self) -> Set[Variable]: def _iterable_arguments(self) -> List[ArgumentType]:
return self._variables(*self.ast) return self.ast
@property
def function_arguments(self) -> Set[FunctionArgument]:
return self._function_arguments(*self.ast)
def resolve( def resolve(
self, self,

View file

@ -2,10 +2,12 @@ from abc import ABC
from abc import abstractmethod from abc import abstractmethod
from dataclasses import dataclass from dataclasses import dataclass
from typing import Dict from typing import Dict
from typing import List
from typing import Set from typing import Set
from typing import final from typing import final
from ytdl_sub.script.types.resolvable import ArgumentType from ytdl_sub.script.types.resolvable import ArgumentType
from ytdl_sub.script.types.resolvable import NamedCustomFunction
from ytdl_sub.script.types.resolvable import Resolvable from ytdl_sub.script.types.resolvable import Resolvable
from ytdl_sub.script.types.variable import FunctionArgument from ytdl_sub.script.types.variable import FunctionArgument
from ytdl_sub.script.types.variable import Variable from ytdl_sub.script.types.variable import Variable
@ -15,10 +17,16 @@ from ytdl_sub.utils.exceptions import StringFormattingException
@dataclass(frozen=True) @dataclass(frozen=True)
class VariableDependency(ABC): class VariableDependency(ABC):
@classmethod @property
def _variables(cls, *args: ArgumentType) -> Set[Variable]: @abstractmethod
def _iterable_arguments(self) -> List[ArgumentType]:
pass
@final
@property
def variables(self) -> Set[Variable]:
output: Set[Variable] = set() output: Set[Variable] = set()
for arg in args: for arg in self._iterable_arguments:
if isinstance(arg, Variable): if isinstance(arg, Variable):
output.add(arg) output.add(arg)
elif isinstance(arg, VariableDependency): elif isinstance(arg, VariableDependency):
@ -26,10 +34,11 @@ class VariableDependency(ABC):
return output return output
@classmethod @final
def _function_arguments(cls, *args: ArgumentType) -> Set[FunctionArgument]: @property
def function_arguments(self) -> Set[FunctionArgument]:
output: Set[FunctionArgument] = set() output: Set[FunctionArgument] = set()
for arg in args: for arg in self._iterable_arguments:
if isinstance(arg, FunctionArgument): if isinstance(arg, FunctionArgument):
output.add(arg) output.add(arg)
elif isinstance(arg, VariableDependency): elif isinstance(arg, VariableDependency):
@ -37,15 +46,18 @@ class VariableDependency(ABC):
return output return output
@final
@property @property
@abstractmethod def custom_functions(self) -> Set[NamedCustomFunction]:
def variables(self) -> Set[Variable]: output: Set[NamedCustomFunction] = set()
pass for arg in self._iterable_arguments:
if isinstance(arg, NamedCustomFunction):
# Custom funcs aren't hashable, so recreate just the base-class portion
output.add(NamedCustomFunction(name=arg.name))
elif isinstance(arg, VariableDependency):
output.update(arg.custom_functions)
@property return output
@abstractmethod
def function_arguments(self) -> Set[FunctionArgument]:
pass
@abstractmethod @abstractmethod
def resolve( def resolve(