From 51bd8938882a16245696e515dd40fb0cf298de08 Mon Sep 17 00:00:00 2001 From: Travis Dart Date: Mon, 23 Dec 2024 12:21:46 -0500 Subject: [PATCH] Preliminary debugging --- .gitignore | 2 ++ vulture/core.py | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 0ec272f6..388d8e83 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ vulture.egg-info/ .tox/ .venv/ .vscode/ +.idea/ +*.egg-info/ diff --git a/vulture/core.py b/vulture/core.py index cc301b71..f219ca92 100644 --- a/vulture/core.py +++ b/vulture/core.py @@ -193,7 +193,7 @@ class Vulture(ast.NodeVisitor): """Find dead code.""" def __init__( - self, verbose=False, ignore_names=None, ignore_decorators=None + self, verbose=False, ignore_names=None, ignore_decorators=None, vertex_callback=None, edge_callback=None ): self.verbose = verbose @@ -214,6 +214,9 @@ def get_list(typ): self.ignore_names = ignore_names or [] self.ignore_decorators = ignore_decorators or [] + self.vertex_callback = vertex_callback + self.edge_callback = edge_callback + self.filename = Path() self.code = [] self.exit_code = ExitCode.NoDeadCode @@ -497,6 +500,9 @@ def visit_BinOp(self, node): self.used_names |= set(re.findall(r"%\((\w+)\)", node.left.value)) def visit_Call(self, node): + # Outgoing to another function: + print("node.func.id:", node.func.id) + # Count getattr/hasattr(x, "some_attr", ...) as usage of some_attr. if isinstance(node.func, ast.Name) and ( (node.func.id == "getattr" and 2 <= len(node.args) <= 3) @@ -632,10 +638,14 @@ def visit(self, node): method = "visit_" + node.__class__.__name__ visitor = getattr(self, method, None) + + lineno = getattr(node, "lineno", 1) + line = self.code[lineno - 1] if self.code else "" if self.verbose: - lineno = getattr(node, "lineno", 1) - line = self.code[lineno - 1] if self.code else "" self._log(lineno, ast.dump(node), line) + + print("method:", method, getattr(node, "name", None), lineno, line) + if visitor: visitor(node) @@ -663,6 +673,14 @@ def generic_visit(self, node): self.visit(value) +def vertex_callback(*args, **kwargs): + print("vertex_callback", args, kwargs) + + +def edge_callback(*args, **kwargs): + print("edge_callback", args, kwargs) + + def main(): try: config = make_config() @@ -674,6 +692,8 @@ def main(): verbose=config["verbose"], ignore_names=config["ignore_names"], ignore_decorators=config["ignore_decorators"], + vertex_callback=vertex_callback, + edge_callback=edge_callback, ) vulture.scavenge(config["paths"], exclude=config["exclude"]) sys.exit(