The Code Analyzer API provides comprehensive static code analysis capabilities for the Aurelis platform, enabling automated detection of issues, quality metrics calculation, and security vulnerability scanning.
The Code Analyzer is responsible for:
Main analyzer class providing comprehensive code analysis capabilities.
from aurelis.analysis import CodeAnalyzer
analyzer = CodeAnalyzer()
analyze_file(file_path: Path) -> AnalysisResult
Performs comprehensive analysis of a single file.
Parameters:
file_path
(Path): Path to the file to analyzeReturns:
AnalysisResult
: Complete analysis results including issues, metrics, and suggestionsExample:
from pathlib import Path
from aurelis.analysis import CodeAnalyzer
analyzer = CodeAnalyzer()
result = await analyzer.analyze_file(Path("src/main.py"))
print(f"Found {len(result.issues)} issues")
for issue in result.issues:
print(f"{issue.severity}: {issue.message} (line {issue.location.line_number})")
analyze_directory(directory_path: Path, recursive: bool = True) -> List[AnalysisResult]
Analyzes all files in a directory.
Parameters:
directory_path
(Path): Directory to analyzerecursive
(bool): Whether to analyze subdirectories recursivelyReturns:
List[AnalysisResult]
: Analysis results for each fileanalyze_code_string(content: str, language: str) -> AnalysisResult
Analyzes code from a string.
Parameters:
content
(str): Code content to analyzelanguage
(str): Programming language identifierReturns:
AnalysisResult
: Analysis resultsget_metrics(file_path: Path) -> Dict[str, Any]
Calculates detailed code metrics for a file.
Parameters:
file_path
(Path): File to analyzeReturns:
Dict[str, Any]
: Comprehensive metrics including complexity, maintainability, and quality scoresExample:
metrics = await analyzer.get_metrics(Path("src/complex_module.py"))
print(f"Cyclomatic complexity: {metrics['cyclomatic_complexity']}")
print(f"Lines of code: {metrics['lines_of_code']}")
print(f"Maintainability index: {metrics['maintainability_index']}")
is_ready() -> bool
Checks if the analyzer is ready for use.
Returns:
bool
: True if ready, False otherwiseDefines the types of analysis that can be performed:
SYNTAX
: Syntax error detectionLOGIC
: Logic error and bug detectionPERFORMANCE
: Performance analysis and optimization suggestionsSECURITY
: Security vulnerability scanningSTYLE
: Code style and convention checkingINFO
: Informational messagesWARNING
: Potential issues that should be reviewedERROR
: Definite errors that need fixingCRITICAL
: Severe issues that must be addressed immediatelyContains the complete results of code analysis.
@dataclass
class AnalysisResult:
file_path: Path
analysis_types: List[AnalysisType]
issues: List[CodeIssue]
metrics: Dict[str, Union[int, float, str]]
suggestions: List[str]
confidence: float
processing_time: float
timestamp: datetime
Represents a specific issue found during analysis.
@dataclass
class CodeIssue:
id: str
type: AnalysisType
severity: ErrorSeverity
message: str
description: str
location: CodeLocation
suggested_fix: Optional[str]
rule_id: Optional[str]
confidence: float
Represents a location in source code.
@dataclass
class CodeLocation:
file_path: Path
line_number: int
column_number: int
end_line_number: Optional[int]
end_column_number: Optional[int]
The analyzer can be configured through the main Aurelis configuration:
analysis:
enabled_types:
- syntax
- logic
- performance
- security
- style
max_file_size: 1048576 # 1MB
timeout: 30 # seconds
parallel_processing: true
max_workers: 4
Currently supported languages:
import asyncio
from pathlib import Path
from aurelis.analysis import CodeAnalyzer
async def analyze_project():
analyzer = CodeAnalyzer()
# Analyze a specific file
result = await analyzer.analyze_file(Path("src/main.py"))
# Print summary
print(f"Analysis completed for {result.file_path}")
print(f"Issues found: {len(result.issues)}")
print(f"Processing time: {result.processing_time:.2f}s")
# Show issues by severity
for severity in ['CRITICAL', 'ERROR', 'WARNING', 'INFO']:
issues = [i for i in result.issues if i.severity.value == severity]
if issues:
print(f"\n{severity} ({len(issues)}):")
for issue in issues:
print(f" Line {issue.location.line_number}: {issue.message}")
asyncio.run(analyze_project())
async def analyze_entire_project():
analyzer = CodeAnalyzer()
project_root = Path("./src")
results = await analyzer.analyze_directory(project_root)
# Aggregate statistics
total_issues = sum(len(r.issues) for r in results)
critical_issues = sum(
len([i for i in r.issues if i.severity == ErrorSeverity.CRITICAL])
for r in results
)
print(f"Analyzed {len(results)} files")
print(f"Total issues: {total_issues}")
print(f"Critical issues: {critical_issues}")
# Files with most issues
results.sort(key=lambda r: len(r.issues), reverse=True)
print("\nFiles with most issues:")
for result in results[:5]:
print(f" {result.file_path}: {len(result.issues)} issues")
from aurelis.analysis import CodeAnalyzer
from aurelis.core.types import AnalysisType, ErrorSeverity
async def custom_security_scan():
analyzer = CodeAnalyzer()
# Focus on security analysis
results = await analyzer.analyze_directory(
Path("./src"),
analysis_types=[AnalysisType.SECURITY]
)
security_issues = []
for result in results:
for issue in result.issues:
if issue.type == AnalysisType.SECURITY:
security_issues.append((result.file_path, issue))
# Generate security report
print("Security Analysis Report")
print("=" * 50)
for file_path, issue in security_issues:
print(f"File: {file_path}")
print(f"Issue: {issue.message}")
print(f"Severity: {issue.severity.value}")
print(f"Location: Line {issue.location.line_number}")
if issue.suggested_fix:
print(f"Suggested fix: {issue.suggested_fix}")
print("-" * 30)
The analyzer implements comprehensive error handling:
try:
result = await analyzer.analyze_file(file_path)
except FileNotFoundError:
print(f"File not found: {file_path}")
except PermissionError:
print(f"Permission denied: {file_path}")
except UnsupportedLanguageError:
print(f"Language not supported for: {file_path}")
except AnalysisTimeoutError:
print(f"Analysis timed out for: {file_path}")
except Exception as e:
print(f"Unexpected error during analysis: {e}")
Analysis takes too long:
Out of memory errors:
False positives:
Language not supported:
For additional support, see the main Aurelis documentation or contact the development team.