Enterprise-grade code complexity analyzer supporting mutiple programming languages with standardized metrics.
Project description
Code Analyzer
Enterprise-grade code complexity analyzer supporting ** mutiple programming languages** with standardized metrics and intelligent language detection.
Key Features
- 14 Language Support: SQL (Oracle, SQL Server, MySQL, PostgreSQL, BigQuery, Snowflake, Spark SQL, Redshift, SQLite, Sybase, DB2), Python, PySpark, SAS
- Standardized Scoring: Unified 0-100 complexity scale across all languages
- Enterprise Metrics: 10-dimension analysis framework with business-friendly thresholds
- Intelligent Detection: Automatic language detection with content-based analysis
- CLI Interface: Modern Click-based command-line interface
- Extensible Architecture: Plugin-based system for easy language additions
๐ฆ Installation
pip install pyspcodeanalyzer
๐ ๏ธ Quick Start
CLI Usage
Analyze a single file:
code_analyzer analyze examples/sample.py
Analyze entire directory:
code_analyzer analyze src/ --recursive
Analyze code directly:
code_analyzer analyze-code "SELECT * FROM users WHERE active = 1" --language oracle
List supported languages:
code_analyzer languages
View metrics information:
code_analyzer metrics
Python API
from code_analyzer.core.registry import get_analyzer
# Analyze Python code
analyzer = get_analyzer("python")
result = analyzer.analyze_source("""
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
""")
print(f"Complexity Score: {result.total_score}/100")
print(f"Complexity Level: {result.classification}")
๐ Supported Languages
| Language | Extensions | Key Features |
|---|---|---|
| Python | .py |
Functions, classes, async/await, decorators, comprehensions |
| PySpark | .py |
DataFrame operations, RDD transformations, Spark SQL |
| SAS | .sas |
DATA steps, PROC procedures, macro programming |
| Oracle | .sql, .pkb, .pks |
PL/SQL packages, procedures, functions, triggers |
| SQL Server | .sql |
T-SQL stored procedures, functions, triggers, SSIS |
| MySQL | .sql |
Stored procedures, functions, triggers, events |
| PostgreSQL | .sql |
PL/pgSQL, extensions, custom types, table inheritance |
| BigQuery | .sql |
Standard SQL, UDFs, ML functions, nested data |
| Snowflake | .sql |
JavaScript UDFs, streams, tasks, time travel |
| Spark SQL | .sql |
DataFrames, Catalyst optimizer, bucketing |
| Redshift | .sql |
Stored procedures, external tables, Spectrum |
| SQLite | .sql |
Triggers, views, CTEs, JSON functions |
| Sybase | .sql, .db |
ASE procedures, functions, triggers |
| DB2 | .sql |
Stored procedures, UDFs, XML support |
๐ Complexity Scoring System
10-Dimension Analysis Framework
Each language is analyzed across these standardized dimensions:
- SQL Logic Complexity - Query complexity, joins, subqueries
- Utility Complexity - Procedures, functions, stored logic
- Data Operations - CRUD operations, data transformations
- Control Flow - Conditional logic, loops, branching
- Error Handling - Exception handling, validation
- File I/O & External Integration - External data sources, APIs
- Performance & Optimization - Indexing, partitioning, caching
- Security & Access Control - Authentication, authorization, encryption
- ODS Output Delivery - Reporting, output generation
- Execution Control - Transaction management, concurrency
Scoring Scale
- 0-30: ๐ข Simple - Basic operations, minimal complexity
- 31-60: ๐ก Medium - Moderate complexity, some advanced features
- 61-80: ๐ Complex - Advanced operations, multiple integrations
- 81-100: ๐ด Very Complex - Highly sophisticated, enterprise-grade code
๐ง Configuration
Custom Weights
Create config.yaml to customize dimension weights:
weights:
script_size_structure: 10
dependency_footprint: 10
analytics_depth: 10
sql_reporting_logic: 10
transformation_logic: 10
utility_complexity: 10
execution_control: 10
file_io_external_integration: 10
ods_output_delivery: 10
error_handling_optimization: 10
include_cyclomatic: false
classification_thresholds:
simple: 30
medium: 60
complex: 80
very_complex: 100
Environment Variables
export CODE_ANALYZER_CONFIG=/path/to/config.yaml
export CODE_ANALYZER_LOG_LEVEL=INFO
๐ CLI Commands
analyze
Analyze files or directories for code complexity.
code_analyzer analyze [PATH] [OPTIONS]
Options:
--recursive, -r Analyze directories recursively
--language, -l Force specific language detection
--output, -o Output format (text, json, csv)
--threshold, -t Minimum complexity threshold to report
--config, -c Custom configuration file path
analyze-code
Analyze code directly from command line.
code_analyzer analyze-code [CODE] --language [LANG]
Options:
--language, -l Programming language (required)
--output, -o Output format (text, json)
languages
List all supported programming languages.
code_analyzer languages
Options:
--verbose, -v Show detailed language information
metrics
Show information about complexity metrics and scoring.
code_analyzer metrics
Options:
--language, -l Show metrics for specific language
--weights, -w Display current weight configuration
version
Display version information.
code_analyzer version
๐๏ธ Architecture
The Code Analyzer follows a modular, enterprise-grade architecture:
code_analyzer/
โโโ core/ # Core analysis engine
โ โโโ analyzer_base.py # Base analyzer interface
โ โโโ registry.py # Language registry & detection
โ โโโ metrics.py # Centralized metrics system
โ โโโ exception.py # Exception hierarchy
โโโ analyzers/ # Language-specific analyzers
โ โโโ python_analyzer.py
โ โโโ oracle_analyzer.py
โ โโโ sas_analyzer.py
โ โโโ ...
โโโ config/ # Configuration management
โ โโโ loader.py # Configuration loader
โ โโโ defaults/
โ โโโ config.yaml # Default configuration
โโโ reporters/ # Output formatting[TBD]
โ โโโ __init__.py
โ
โโโ cli.py # Command-line interface
๐ Extending the Analyzer
Add support for new languages by implementing the AnalyzerBase interface:
from code_analyzer.core.analyzer_base import AnalyzerBase
from code_analyzer.core.registry import analyzer_registry
class CustomAnalyzer(AnalyzerBase):
language = "custom"
def analyze_code(self, code: str) -> AnalysisResult:
# Implement your analysis logic
pass
def _calculate_metrics(self, code: str) -> Dict[str, int]:
# Calculate complexity metrics
pass
# Register your analyzer
analyzer_registry.register_analyzer("custom", CustomAnalyzer)
๐งช Testing
Run the test suite:
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run with coverage
pytest --cov=code_analyzer --cov-report=html
# Run specific test
pytest tests/test_sas_metrics.py -v
๐ Examples
Enterprise SAS Analysis
code_analyzer analyze examples/sample.sas
๐ Code Complexity Analysis Results
============================================================
๐ Summary:
Total Files: 1
Average Score: 35.0/100
Complexity Distribution:
Medium: 1 files (100.0%)
๐ Individual Results:
----------------------------------------
File: examples/sample_sas_test.sas
Language: sas
Score: 35/100 (Medium)
Cyclomatic: 2
๐ค Contributing
We welcome contributions! Please see our Contributing Guide for details.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- Inspired by enterprise code analysis needs
- Built with modern Python packaging standards
- Designed for scalable, multi-language analysis
๐ Support
- Documentation: Read the Docs
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Made with โค๏ธ for the developer community
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyspcodeanalyzer-1.0.0.tar.gz.
File metadata
- Download URL: pyspcodeanalyzer-1.0.0.tar.gz
- Upload date:
- Size: 94.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.10.6 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c761bc021f43b133fc3f3c8d0283204a0a3841b8c8a9320a9e6e0e43a61a841c
|
|
| MD5 |
604e69d6158771e0bf5d3cc54bdba915
|
|
| BLAKE2b-256 |
e1b2dd384ea1aeafbd3c0f61e32d387b26f02e425940ffee9b46d0b6acff8268
|
File details
Details for the file pyspcodeanalyzer-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pyspcodeanalyzer-1.0.0-py3-none-any.whl
- Upload date:
- Size: 125.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.10.6 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f40f062f5b7f3a1281862ead179bd0db209154231d98ce83321a96d37677fca0
|
|
| MD5 |
2b6e62181d093d25cb973ffa87439d0e
|
|
| BLAKE2b-256 |
a348f4ba643dd463154cab20817df07b0dd280e2330c6d36f3d3ec64c27b7774
|