iterable arguments
This commit is contained in:
parent
0fffc79cfb
commit
d3e127783e
6 changed files with 37 additions and 42 deletions
|
|
@ -27,12 +27,8 @@ class UnresolvedArray(Array, VariableDependency, FutureResolvable):
|
|||
value: List[ArgumentType]
|
||||
|
||||
@property
|
||||
def variables(self) -> Set[Variable]:
|
||||
return self._variables(*self.value)
|
||||
|
||||
@property
|
||||
def function_arguments(self) -> Set[FunctionArgument]:
|
||||
return self._function_arguments(*self.value)
|
||||
def _iterable_arguments(self) -> List[ArgumentType]:
|
||||
return self.value
|
||||
|
||||
def resolve(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -40,12 +40,8 @@ from ytdl_sub.utils.exceptions import StringFormattingException
|
|||
@dataclass(frozen=True)
|
||||
class Function(FunctionType, VariableDependency, ABC):
|
||||
@property
|
||||
def variables(self) -> Set[Variable]:
|
||||
return self._variables(*self.args)
|
||||
|
||||
@property
|
||||
def function_arguments(self) -> Set[FunctionArgument]:
|
||||
return self._function_arguments(*self.args)
|
||||
def _iterable_arguments(self) -> List[ArgumentType]:
|
||||
return self.args
|
||||
|
||||
@classmethod
|
||||
def from_name_and_args(cls, name: str, args: List[ArgumentType]) -> "Function":
|
||||
|
|
|
|||
|
|
@ -30,17 +30,9 @@ class UnresolvedMap(Map, VariableDependency, FutureResolvable):
|
|||
value: Dict[ArgumentType, ArgumentType]
|
||||
|
||||
@property
|
||||
def _as_flat_list(self) -> List[ArgumentType]:
|
||||
def _iterable_arguments(self) -> List[ArgumentType]:
|
||||
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(
|
||||
self,
|
||||
resolved_variables: Dict[Variable, Resolvable],
|
||||
|
|
|
|||
|
|
@ -121,6 +121,11 @@ class String(ResolvableT[str], Hashable, ArgumentType):
|
|||
pass
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class NamedCustomFunction(ArgumentType, ABC):
|
||||
name: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class FunctionType(ArgumentType, ABC):
|
||||
name: str
|
||||
|
|
|
|||
|
|
@ -2,12 +2,10 @@ from dataclasses import dataclass
|
|||
from typing import Dict
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
from typing import Set
|
||||
|
||||
from ytdl_sub.script.types.resolvable import ArgumentType
|
||||
from ytdl_sub.script.types.resolvable import Resolvable
|
||||
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_dependency import VariableDependency
|
||||
from ytdl_sub.utils.exceptions import StringFormattingException
|
||||
|
|
@ -18,12 +16,8 @@ class SyntaxTree(VariableDependency):
|
|||
ast: List[ArgumentType]
|
||||
|
||||
@property
|
||||
def variables(self) -> Set[Variable]:
|
||||
return self._variables(*self.ast)
|
||||
|
||||
@property
|
||||
def function_arguments(self) -> Set[FunctionArgument]:
|
||||
return self._function_arguments(*self.ast)
|
||||
def _iterable_arguments(self) -> List[ArgumentType]:
|
||||
return self.ast
|
||||
|
||||
def resolve(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -2,10 +2,12 @@ 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 final
|
||||
|
||||
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.variable import FunctionArgument
|
||||
from ytdl_sub.script.types.variable import Variable
|
||||
|
|
@ -15,10 +17,16 @@ from ytdl_sub.utils.exceptions import StringFormattingException
|
|||
|
||||
@dataclass(frozen=True)
|
||||
class VariableDependency(ABC):
|
||||
@classmethod
|
||||
def _variables(cls, *args: ArgumentType) -> Set[Variable]:
|
||||
@property
|
||||
@abstractmethod
|
||||
def _iterable_arguments(self) -> List[ArgumentType]:
|
||||
pass
|
||||
|
||||
@final
|
||||
@property
|
||||
def variables(self) -> Set[Variable]:
|
||||
output: Set[Variable] = set()
|
||||
for arg in args:
|
||||
for arg in self._iterable_arguments:
|
||||
if isinstance(arg, Variable):
|
||||
output.add(arg)
|
||||
elif isinstance(arg, VariableDependency):
|
||||
|
|
@ -26,10 +34,11 @@ class VariableDependency(ABC):
|
|||
|
||||
return output
|
||||
|
||||
@classmethod
|
||||
def _function_arguments(cls, *args: ArgumentType) -> Set[FunctionArgument]:
|
||||
@final
|
||||
@property
|
||||
def function_arguments(self) -> Set[FunctionArgument]:
|
||||
output: Set[FunctionArgument] = set()
|
||||
for arg in args:
|
||||
for arg in self._iterable_arguments:
|
||||
if isinstance(arg, FunctionArgument):
|
||||
output.add(arg)
|
||||
elif isinstance(arg, VariableDependency):
|
||||
|
|
@ -37,15 +46,18 @@ class VariableDependency(ABC):
|
|||
|
||||
return output
|
||||
|
||||
@final
|
||||
@property
|
||||
@abstractmethod
|
||||
def variables(self) -> Set[Variable]:
|
||||
pass
|
||||
def custom_functions(self) -> Set[NamedCustomFunction]:
|
||||
output: Set[NamedCustomFunction] = set()
|
||||
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
|
||||
@abstractmethod
|
||||
def function_arguments(self) -> Set[FunctionArgument]:
|
||||
pass
|
||||
return output
|
||||
|
||||
@abstractmethod
|
||||
def resolve(
|
||||
|
|
|
|||
Loading…
Reference in a new issue