Skip to main content

Enterprise-grade code complexity analyzer supporting mutiple programming languages with standardized metrics.

Project description

Code Analyzer

PyPI version Python Support License: MIT

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:

  1. SQL Logic Complexity - Query complexity, joins, subqueries
  2. Utility Complexity - Procedures, functions, stored logic
  3. Data Operations - CRUD operations, data transformations
  4. Control Flow - Conditional logic, loops, branching
  5. Error Handling - Exception handling, validation
  6. File I/O & External Integration - External data sources, APIs
  7. Performance & Optimization - Indexing, partitioning, caching
  8. Security & Access Control - Authentication, authorization, encryption
  9. ODS Output Delivery - Reporting, output generation
  10. 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.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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


Made with โค๏ธ for the developer community

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyspcodeanalyzer-1.0.0.tar.gz (94.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyspcodeanalyzer-1.0.0-py3-none-any.whl (125.0 kB view details)

Uploaded Python 3

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

Hashes for pyspcodeanalyzer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c761bc021f43b133fc3f3c8d0283204a0a3841b8c8a9320a9e6e0e43a61a841c
MD5 604e69d6158771e0bf5d3cc54bdba915
BLAKE2b-256 e1b2dd384ea1aeafbd3c0f61e32d387b26f02e425940ffee9b46d0b6acff8268

See more details on using hashes here.

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

Hashes for pyspcodeanalyzer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f40f062f5b7f3a1281862ead179bd0db209154231d98ce83321a96d37677fca0
MD5 2b6e62181d093d25cb973ffa87439d0e
BLAKE2b-256 a348f4ba643dd463154cab20817df07b0dd280e2330c6d36f3d3ec64c27b7774

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page