-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced.py
More file actions
67 lines (56 loc) · 2.22 KB
/
advanced.py
File metadata and controls
67 lines (56 loc) · 2.22 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Advanced Python API usage for code2schema
"""
from pathlib import Path
from code2schema import extract_project, analyze
from code2schema.analyzer.cqrs import CQRSRole
from code2schema.analyzer.graph import build_rich_graph, hub_nodes, centrality_report, layer_violations
# 1. Extract with exclusions
modules = extract_project(
Path("./backend"),
exclude=["tests/**", "migrations/**", "venv/**", ".venv/**"]
)
schema = analyze(modules)
# 2. Filter functions by role
queries = [f for f in schema.all_functions() if f.role == CQRSRole.QUERY]
commands = [f for f in schema.all_functions() if f.role == CQRSRole.COMMAND]
orchestrators = [f for f in schema.all_functions() if f.role == CQRSRole.ORCHESTRATOR]
# 3. Find high fan-out functions (potential refactoring targets)
high_fan_out = [f for f in schema.all_functions() if f.fan_out >= 10]
print(f"Functions with fan-out >= 10: {len(high_fan_out)}")
for func in high_fan_out[:10]:
print(f" - {func.name} (fan_out={func.fan_out})")
# 4. Find long functions
long_functions = [f for f in schema.all_functions() if f.lines > 100]
print(f"\nFunctions with > 100 lines: {len(long_functions)}")
# 5. Graph analysis
G = build_rich_graph(schema)
# Get hub nodes (architectural centers)
hubs = hub_nodes(G, top_n=10)
print("\nTop 10 hub nodes:")
for node, score in hubs:
print(f" - {node}: {score:.4f}")
# PageRank centrality
centrality = centrality_report(G, top_n=5)
print("\nTop 5 by PageRank:")
print(centrality)
# 6. Access workflow DAGs
print(f"\nWorkflows: {len(schema.workflows)}")
for wf in schema.workflows[:5]:
print(f" - {wf.name}: {len(wf.steps)} steps")
# 7. Access quality rules
print(f"\nQuality rules: {len(schema.rules)}")
for rule in schema.rules[:5]:
print(f" - [{rule.severity}] {rule.id}: {rule.condition}")
# 8. Inspect specific function
if schema.modules:
mod = schema.modules[0]
if mod.functions:
func = mod.functions[0]
print(f"\nExample function: {func.name}")
print(f" Module: {func.module}")
print(f" Role: {func.role.value}")
print(f" Lines: {func.lines}")
print(f" Fan-out: {func.fan_out}")
print(f" Calls: {func.calls}")
print(f" Side effects: {[s.value for s in func.side_effects]}")