API Reference¶
Use D4-Diag programmatically in your Python code.
CodeMapAnalyzer¶
Main class for code analysis and diagram generation.
Constructor¶
Parameters:
- project_root (str): Absolute path to project root directory
Attributes:
- files (Dict[str, dict]): Per-file analysis data
- import_edges (Set[tuple]): Import relationships between files
- project_root (str): Project root path
Methods¶
build_module_map¶
Build mapping from module names to file paths.
Parameters:
- file_paths (List[str]): List of absolute file paths to analyze
Returns: None
Side effects: Populates internal _module_map for import resolution
analyze_file¶
Analyze a single Python file.
Parameters:
- file_path (str): Absolute path to Python file
Returns: None
Side effects: Updates files dict with analysis results
Raises: - Prints syntax error message if file has invalid Python syntax
generate_architecture¶
Generate architecture overview diagram.
Parameters:
- output_file (str): Path where diagram will be saved
Returns: None
Side effects: Writes Mermaid diagram to file
generate_class_diagram¶
Generate UML class diagram.
Parameters:
- output_file (str): Path where diagram will be saved
Returns: None
Side effects: Writes Mermaid diagram to file
generate_module_deps¶
Generate module dependency diagram.
Parameters:
- output_file (str): Path where diagram will be saved
Returns: None
Side effects: Writes Mermaid diagram to file
generate_all¶
Generate all three diagram types.
Parameters:
- output_dir (str): Directory where diagrams will be saved
Returns: None
Side effects:
- Creates output directory if it doesn't exist
- Writes three .mmd files to output directory
print_summary¶
Print analysis summary to stdout.
Returns: None
Side effects: Prints formatted summary to stdout
Utility Functions¶
sanitize_id¶
Sanitize a string for use as a Mermaid node ID.
Parameters:
- name (str): Raw string to sanitize
Returns: str - Sanitized ID safe for Mermaid
Example:
qlabel¶
Quote a label for safe Mermaid rendering.
Parameters:
- text (str): Text to quote
Returns: str - Quoted text safe for Mermaid labels
Example:
Complete Example¶
import os
from pathlib import Path
from d4_diag.main import CodeMapAnalyzer, find_python_files
# Setup
project_root = "/path/to/project"
output_dir = os.path.join(project_root, "docs", "diagrams")
# Find all Python files
file_paths = find_python_files(project_root)
print(f"Found {len(file_paths)} Python files")
# Create analyzer
analyzer = CodeMapAnalyzer(project_root)
# Build module map for import resolution
analyzer.build_module_map(file_paths)
# Analyze all files
for file_path in file_paths:
analyzer.analyze_file(file_path)
# Print summary
analyzer.print_summary()
# Generate diagrams
analyzer.generate_all(output_dir)
print(f"Diagrams saved to {output_dir}")
Data Structures¶
File Info¶
Each entry in analyzer.files has this structure:
{
'classes': [
{
'name': str, # Class name
'methods': [str], # List of method names
'bases': [str] # List of base class names
}
],
'functions': [str], # List of function names
'imports': [str] # List of imported modules
}
Import Edge¶
Each entry in analyzer.import_edges is a tuple:
Where paths are relative to project_root.
Next Steps¶
- CLI Reference - Command-line usage
- Examples - Real-world examples