simplifiy
This commit is contained in:
parent
f8a2f03130
commit
0477d29f44
2 changed files with 26 additions and 29 deletions
|
|
@ -182,13 +182,13 @@ class Script:
|
||||||
|
|
||||||
def resolve(
|
def resolve(
|
||||||
self,
|
self,
|
||||||
pre_resolved_variables: Optional[Dict[str, Resolvable]] = None,
|
resolved: Optional[Dict[str, Resolvable]] = None,
|
||||||
unresolvable: Optional[Set[str]] = None,
|
unresolvable: Optional[Set[str]] = None,
|
||||||
) -> Dict[str, Resolvable]:
|
) -> Dict[str, Resolvable]:
|
||||||
"""
|
"""
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
pre_resolved_variables
|
resolved
|
||||||
Optional variables that have been resolved elsewhere and could be used in this script
|
Optional variables that have been resolved elsewhere and could be used in this script
|
||||||
unresolvable
|
unresolvable
|
||||||
Variables that cannot be resolved, forcing any variable that depends on it to not be
|
Variables that cannot be resolved, forcing any variable that depends on it to not be
|
||||||
|
|
@ -198,39 +198,38 @@ class Script:
|
||||||
-------
|
-------
|
||||||
Dict of resolved values
|
Dict of resolved values
|
||||||
"""
|
"""
|
||||||
variables: Dict[Variable, SyntaxTree] = {
|
resolved: Dict[Variable, Resolvable] = {
|
||||||
Variable(name): ast for name, ast in self._variables.items()
|
Variable(name): value for name, value in (resolved or {}).items()
|
||||||
|
}
|
||||||
|
unresolvable: Set[Variable] = {Variable(name) for name in (unresolvable or {})}
|
||||||
|
unresolved: Dict[Variable, SyntaxTree] = {
|
||||||
|
Variable(name): ast
|
||||||
|
for name, ast in self._variables.items()
|
||||||
|
if Variable(name) not in set(resolved.keys()).union(unresolvable)
|
||||||
}
|
}
|
||||||
|
|
||||||
unresolvable_variables: Set[Variable] = set(Variable(var) for var in (unresolvable or {}))
|
while unresolved:
|
||||||
resolved_variables: Dict[Variable, Resolvable] = {
|
unresolved_count: int = len(unresolved)
|
||||||
Variable(name): value for name, value in (pre_resolved_variables or {}).items()
|
|
||||||
}
|
|
||||||
unresolved_variables: Set[Variable] = (
|
|
||||||
set(variables.keys()) - set(resolved_variables.keys()) - unresolvable_variables
|
|
||||||
)
|
|
||||||
|
|
||||||
while unresolved_variables:
|
for variable, definition in copy.deepcopy(unresolved).items():
|
||||||
unresolved_count: int = len(unresolved_variables)
|
|
||||||
|
|
||||||
for variable in copy.deepcopy(unresolved_variables):
|
|
||||||
# If the variable's variable dependencies contain an unresolvable variable,
|
# If the variable's variable dependencies contain an unresolvable variable,
|
||||||
# declare it as unresolvable and continue
|
# declare it as unresolvable and continue
|
||||||
if variables[variable].contains(unresolvable_variables):
|
if definition.contains(unresolvable):
|
||||||
unresolvable_variables.add(variable)
|
unresolvable.add(variable)
|
||||||
unresolved_variables.remove(variable)
|
del unresolved[variable]
|
||||||
|
|
||||||
# Otherwise, resolve it
|
# Otherwise, if it has dependencies that are all resolved, then
|
||||||
elif not variables[variable].is_subset_of(variables=resolved_variables.keys()):
|
# resolve the definition
|
||||||
resolved_variables[variable] = variables[variable].resolve(
|
elif not definition.is_subset_of(variables=resolved.keys()):
|
||||||
resolved_variables=resolved_variables,
|
resolved[variable] = unresolved[variable].resolve(
|
||||||
|
resolved_variables=resolved,
|
||||||
custom_functions=self._functions,
|
custom_functions=self._functions,
|
||||||
)
|
)
|
||||||
unresolved_variables.remove(variable)
|
del unresolved[variable]
|
||||||
|
|
||||||
if len(unresolved_variables) == unresolved_count:
|
if len(unresolved) == unresolved_count:
|
||||||
# Implies a cycle within the variables. Should never reach
|
# Implies a cycle within the variables. Should never reach
|
||||||
# since cycles are detected in __init__
|
# since cycles are detected in __init__
|
||||||
raise UNREACHABLE
|
raise UNREACHABLE
|
||||||
|
|
||||||
return {variable.name: resolvable for variable, resolvable in resolved_variables.items()}
|
return {variable.name: resolvable for variable, resolvable in resolved.items()}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ class TestScript:
|
||||||
"bb": "b",
|
"bb": "b",
|
||||||
"cc": "{%custom_func(aa, bb)}",
|
"cc": "{%custom_func(aa, bb)}",
|
||||||
}
|
}
|
||||||
).resolve(pre_resolved_variables={"bb": String("bb_override")}) == {
|
).resolve(resolved={"bb": String("bb_override")}) == {
|
||||||
"aa": String("a"),
|
"aa": String("a"),
|
||||||
"bb": String("bb_override"),
|
"bb": String("bb_override"),
|
||||||
"cc": String('return ["a", "bb_override"]'),
|
"cc": String('return ["a", "bb_override"]'),
|
||||||
|
|
@ -41,9 +41,7 @@ class TestScript:
|
||||||
assert overrides == {"override": String("hi")}
|
assert overrides == {"override": String("hi")}
|
||||||
|
|
||||||
entry_map = ResolvedMap({String("title"): String("the title")})
|
entry_map = ResolvedMap({String("title"): String("the title")})
|
||||||
entry_output = script.resolve(
|
entry_output = script.resolve(resolved=dict(overrides, **{"entry": entry_map}))
|
||||||
pre_resolved_variables=dict(overrides, **{"entry": entry_map})
|
|
||||||
)
|
|
||||||
assert entry_output == {
|
assert entry_output == {
|
||||||
"override": String("hi"),
|
"override": String("hi"),
|
||||||
"entry": entry_map,
|
"entry": entry_map,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue