Skip to content

Commit

Permalink
Preliminary debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
TravisDart committed Dec 23, 2024
1 parent fcc3704 commit 51bd893
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ vulture.egg-info/
.tox/
.venv/
.vscode/
.idea/
*.egg-info/
26 changes: 23 additions & 3 deletions vulture/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand All @@ -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(
Expand Down

0 comments on commit 51bd893

Please sign in to comment.