-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathmapper.py
More file actions
51 lines (44 loc) · 1.91 KB
/
mapper.py
File metadata and controls
51 lines (44 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import ast
import os
import argparse
from typing import Dict, List
class RepoAnalyzer(ast.NodeVisitor):
def __init__(self):
self.stats = {"classes": [], "functions": []}
def visit_ClassDef(self, node: ast.ClassDef):
self.stats["classes"].append(node.name)
self.generic_visit(node)
def visit_FunctionDef(self, node: ast.FunctionDef):
# Ignore private methods/functions
if not node.name.startswith('_'):
self.stats["functions"].append(node.name)
self.generic_visit(node)
def analyze_file(filepath: str) -> Dict[str, List[str]]:
with open(filepath, "r", encoding="utf-8") as f:
try:
tree = ast.parse(f.read())
analyzer = RepoAnalyzer()
analyzer.visit(tree)
return analyzer.stats
except (SyntaxError, UnicodeDecodeError):
return {"classes": [], "functions": []}
def run_mapper(target_dir: str):
print(f"# Project Map: {os.path.abspath(target_dir)}\n")
for root, _, files in os.walk(target_dir):
for file in files:
if file.endswith(".py"):
path = os.path.join(root, file)
rel_path = os.path.relpath(path, target_dir)
data = analyze_file(path)
if data["classes"] or data["functions"]:
print(f"### `{rel_path}`")
if data["classes"]:
print(f"- **Classes**: {', '.join(data['classes'])}")
if data["functions"]:
print(f"- **Public Methods**: {', '.join(data['functions'])}")
print()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate a Markdown map of a Python repository.")
parser.add_argument("path", nargs="?", default=".", help="Directory to analyze (default: current)")
args = parser.parse_args()
run_mapper(args.path)