Rename "stringify", improve docs

This commit is contained in:
Massimiliano Culpo 2023-08-14 10:17:54 +02:00 committed by Todd Gamblin
parent 1db73eb1f2
commit d07d5410f3

View File

@ -591,20 +591,24 @@ class NodeArgument(NamedTuple):
pkg: str pkg: str
def stringify(sym): def intermediate_repr(sym):
"""Stringify symbols from clingo models. """Returns an intermediate representation of clingo models for Spack's spec builder.
This will turn a ``clingo.Symbol`` into a string, or a sequence of ``clingo.Symbol`` Currently, transforms symbols from clingo models either to strings or to NodeArgument objects.
objects into a tuple of strings.
Returns:
This will turn a ``clingo.Symbol`` into a string or NodeArgument, or a sequence of
``clingo.Symbol`` objects into a tuple of those objects.
""" """
# TODO: simplify this when we no longer have to support older clingo versions. # TODO: simplify this when we no longer have to support older clingo versions.
if isinstance(sym, (list, tuple)): if isinstance(sym, (list, tuple)):
return tuple(stringify(a) for a in sym) return tuple(intermediate_repr(a) for a in sym)
try: try:
if sym.name == "node": if sym.name == "node":
return NodeArgument(id=stringify(sym.arguments[0]), pkg=stringify(sym.arguments[1])) return NodeArgument(
id=intermediate_repr(sym.arguments[0]), pkg=intermediate_repr(sym.arguments[1])
)
except RuntimeError: except RuntimeError:
# This happens when using clingo w/ CFFI and trying to access ".name" for symbols # This happens when using clingo w/ CFFI and trying to access ".name" for symbols
# that are not functions # that are not functions
@ -623,10 +627,10 @@ def stringify(sym):
def extract_args(model, predicate_name): def extract_args(model, predicate_name):
"""Extract the arguments to predicates with the provided name from a model. """Extract the arguments to predicates with the provided name from a model.
Pull out all the predicates with name ``predicate_name`` from the model, and return Pull out all the predicates with name ``predicate_name`` from the model, and
their stringified arguments as tuples. return their intermediate representation.
""" """
return [stringify(sym.arguments) for sym in model if sym.name == predicate_name] return [intermediate_repr(sym.arguments) for sym in model if sym.name == predicate_name]
class ErrorHandler: class ErrorHandler:
@ -874,7 +878,8 @@ def on_model(model):
for sym in best_model: for sym in best_model:
if sym.name not in ("attr", "error", "opt_criterion"): if sym.name not in ("attr", "error", "opt_criterion"):
tty.debug( tty.debug(
"UNKNOWN SYMBOL: %s(%s)" % (sym.name, ", ".join(stringify(sym.arguments))) "UNKNOWN SYMBOL: %s(%s)"
% (sym.name, ", ".join(intermediate_repr(sym.arguments)))
) )
elif cores: elif cores: