rd: debugging helper formatting

This commit is contained in:
Willi Ballenthin
2025-01-16 12:53:42 +00:00
committed by Willi Ballenthin
parent 6eb55d2f39
commit 8329abd3c8
2 changed files with 39 additions and 0 deletions

View File

@@ -105,6 +105,25 @@ class Result:
def __nonzero__(self):
return self.success
def __str__(self):
# as this object isn't user facing, this formatting is just to help with debugging
lines = []
def rec(m: "Result", indent: int):
if isinstance(m.statement, capa.engine.Statement):
line = (" " * indent) + str(m.statement.name) + " " + str(m.success)
else:
line = (" " * indent) + str(m.statement) + " " + str(m.success) + " " + str(m.locations)
lines.append(line)
for child in m.children:
rec(child, indent + 1)
rec(self, 0)
return "\n".join(lines)
class Feature(abc.ABC): # noqa: B024
# this is an abstract class, since we don't want anyone to instantiate it directly,

View File

@@ -474,6 +474,26 @@ class Match(FrozenModel):
children=children,
)
def __str__(self):
# as this object isn't user facing, this formatting is just to help with debugging
lines = []
def rec(m: "Match", indent: int):
if isinstance(m.node, StatementNode):
line = (" " * indent) + str(m.node.statement.type) + " " + str(m.success)
elif isinstance(m.node, FeatureNode):
line = (" " * indent) + str(m.node.feature) + " " + str(m.success) + " " + str(m.locations)
else:
raise ValueError("unexpected node type")
lines.append(line)
for child in m.children:
rec(child, indent + 1)
rec(self, 0)
return "\n".join(lines)
def parse_parts_id(s: str):
id_ = ""