dissolve-python 0.3.0

A tool to dissolve deprecated calls in Python codebases
Documentation
#!/usr/bin/env python3
"""
Main consumer script demonstrating usage of deprecated functions.

This script will generate deprecation warnings when run, showing
how the dissolve library helps users identify deprecated function usage.

Run with: python consumer/main.py
"""

import asyncio
import sys
import os

# Add the library to the path so we can import it
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

# Import deprecated functions from the library
from library.utils import old_add, old_multiply, legacy_divide, square, cube, old_format_string
from library.async_ops import old_fetch_data, get_json, legacy_upload, old_batch_fetch
from library.models import OldDataProcessor, LegacyUser, SimpleProcessor
from library.processors import ProcessingEngine, DataValidator
from library.config import (
    OLD_API_URL, OLD_TIMEOUT, LEGACY_RETRIES, AppConfig, FeatureFlags, DevConfig
)
from library.containers import LegacyContainer, SmartList

# Also import some new functions for comparison
from library.utils import add, multiply, divide
from library.models import DataProcessor, User


def demonstrate_deprecated_functions():
    """Demonstrate usage of deprecated utility functions."""
    print("=== Deprecated Function Usage Examples ===")
    
    # Basic deprecated functions
    result1 = old_add(10, 5)
    print(f"old_add(10, 5) = {result1}")
    
    result2 = old_multiply(4, 7)  
    print(f"old_multiply(4, 7) = {result2}")
    
    result3 = legacy_divide(20, 4)
    print(f"legacy_divide(20, 4) = {result3}")
    
    # Functions with parameter transformations
    result4 = square(8)
    print(f"square(8) = {result4}")
    
    result5 = cube(3)
    print(f"cube(3) = {result5}")
    
    # Complex parameter transformation
    result6 = old_format_string("Hello, {}! Welcome to {}", "Alice", "our app", uppercase=True)
    print(f"old_format_string result: {result6}")


def demonstrate_deprecated_classes():
    """Demonstrate usage of deprecated classes and methods."""
    print("\n=== Deprecated Class Usage Examples ===")
    
    # Deprecated class instantiation
    old_processor = OldDataProcessor("database://localhost", cache_size=25)
    processed_data = old_processor.process("sample data")
    print(f"OldDataProcessor result: {processed_data}")
    
    info = old_processor.get_info()
    print(f"Processor info: {info}")
    
    # Deprecated methods on classes
    legacy_user = LegacyUser("john_doe", "john@example.com")
    name = legacy_user.get_name()  # Deprecated method
    print(f"Legacy user name: {name}")
    
    legacy_user.set_profile_data({"age": 30, "city": "New York"})  # Deprecated method
    
    # Deprecated properties
    enabled_status = legacy_user.is_enabled  # Deprecated property
    print(f"User enabled status: {enabled_status}")
    
    # Another deprecated class
    simple_proc = SimpleProcessor("data_source")
    simple_result = simple_proc.process_item("test item")
    print(f"SimpleProcessor result: {simple_result}")


def demonstrate_class_and_static_methods():
    """Demonstrate deprecated class and static methods."""
    print("\n=== Deprecated Class/Static Method Examples ===")
    
    # Deprecated class methods
    old_engine = ProcessingEngine.old_create_engine(fast=True)
    print(f"Old engine type: {old_engine.engine_type}")
    
    legacy_engine = ProcessingEngine.legacy_fast_processor()
    print(f"Legacy engine type: {legacy_engine.engine_type}")
    
    # Deprecated static methods
    is_valid = ProcessingEngine.old_validate_data(["test", "data"])
    print(f"Data validation result: {is_valid}")
    
    clean_text = ProcessingEngine.legacy_clean_text("  HELLO WORLD  ")
    print(f"Cleaned text: '{clean_text}'")
    
    # Complex transformation
    batch_result = ProcessingEngine.old_process_batch(
        ["  Item1  ", "  ITEM2  "], 
        uppercase=True, 
        trim=False
    )
    print(f"Batch processing result: {batch_result}")
    
    # Deprecated validation methods
    email_valid = DataValidator.check_email("test@example.com")
    print(f"Email validation: {email_valid}")
    
    phone_valid = DataValidator.validate_phone_number("555-123-4567")
    print(f"Phone validation: {phone_valid}")
    
    strict_validator = DataValidator.create_strict_validator()
    print(f"Strict validator: {strict_validator}")


def demonstrate_deprecated_attributes():
    """Demonstrate usage of deprecated configuration attributes."""
    print("\n=== Deprecated Attribute Usage Examples ===")
    
    # Module-level deprecated attributes
    api_url = OLD_API_URL
    timeout = OLD_TIMEOUT
    retries = LEGACY_RETRIES
    
    print(f"Using deprecated config - API URL: {api_url}")
    print(f"Using deprecated config - Timeout: {timeout}")
    print(f"Using deprecated config - Retries: {retries}")
    
    # Class attribute deprecation
    old_endpoint = AppConfig.OLD_ENDPOINT
    legacy_size = AppConfig.LEGACY_PAGE_SIZE
    log_setting = AppConfig.OLD_LOG_SETTING
    
    print(f"App config - Old endpoint: {old_endpoint}")
    print(f"App config - Legacy page size: {legacy_size}")
    print(f"App config - Log setting: {log_setting}")
    
    # Feature flags
    ui_flag = FeatureFlags.OLD_UI_FLAG
    beta_flag = FeatureFlags.LEGACY_BETA
    
    print(f"Feature flags - UI: {ui_flag}, Beta: {beta_flag}")
    
    # Environment config
    debug_mode = DevConfig.OLD_DEBUG_MODE
    dev_api = DevConfig.LEGACY_DEV_API
    
    print(f"Dev config - Debug: {debug_mode}, API: {dev_api}")


def demonstrate_container_methods():
    """Demonstrate deprecated container methods."""
    print("\n=== Deprecated Container Method Examples ===")
    
    # Legacy container with deprecated methods
    container = LegacyContainer(["apple", "banana", "cherry"])
    
    # Deprecated magic method alternatives
    size = container.size()  # Deprecated, should use len()
    string_rep = container.to_str()  # Deprecated, should use str()
    
    print(f"Container size (deprecated method): {size}")
    print(f"Container string (deprecated method): {string_rep}")
    
    # More deprecated methods
    item = container.get_item(0)  # Deprecated, should use container[0]
    container.set_item(1, "blueberry")  # Deprecated, should use container[1] = ...
    contains = container.contains("apple")  # Deprecated, should use 'in'
    
    print(f"Got item (deprecated): {item}")
    print(f"Contains apple (deprecated): {contains}")
    
    # SmartList deprecated methods
    smart_list = SmartList([1, 2, 3, 4, 5])
    
    length = smart_list.length()  # Deprecated, should use len()
    count = smart_list.get_count()  # Deprecated, should use len()
    empty = smart_list.is_empty()  # Deprecated, should use not smart_list
    has_item = smart_list.has_item(3)  # Deprecated, should use 3 in smart_list
    
    print(f"SmartList length (deprecated): {length}")
    print(f"SmartList count (deprecated): {count}")
    print(f"SmartList is empty (deprecated): {empty}")
    print(f"SmartList has item 3 (deprecated): {has_item}")


async def demonstrate_async_deprecation():
    """Demonstrate deprecated async functions."""
    print("\n=== Deprecated Async Function Examples ===")
    
    # Deprecated async functions
    data1 = await old_fetch_data("https://api.example.com/users")
    print(f"Old fetch result: {data1}")
    
    json_data = await get_json("users/123")
    print(f"Get JSON result: {json_data}")
    
    upload_result = await legacy_upload("file.txt", "https://upload.example.com")
    print(f"Legacy upload result: {upload_result}")
    
    # Batch operations with deprecated function
    urls = ["https://api.example.com/data/1", "https://api.example.com/data/2"]
    batch_results = await old_batch_fetch(urls, delay=0.5)
    print(f"Batch fetch results: {len(batch_results)} items")


def demonstrate_comparisons():
    """Show comparison between old and new API usage."""
    print("\n=== Comparison: Old vs New API ===")
    
    # Old API (deprecated)
    old_result = old_add(15, 25)
    print(f"OLD: old_add(15, 25) = {old_result}")
    
    # New API (recommended)
    new_result = add(15, 25)
    print(f"NEW: add(15, 25) = {new_result}")
    
    # Old class (deprecated)
    old_proc = OldDataProcessor("data.db", cache_size=10)
    print(f"OLD: OldDataProcessor created with cache_size=10")
    
    # New class (recommended)  
    new_proc = DataProcessor("data.db", batch_size=20, enable_caching=True)
    print(f"NEW: DataProcessor created with batch_size=20")


def main():
    """Main function demonstrating all deprecated usage patterns."""
    print("Dissolve Library - Consumer Example")
    print("===================================")
    print("This script demonstrates usage of deprecated functions that will")
    print("generate deprecation warnings. Use 'dissolve migrate' to automatically")
    print("update this code to use the new APIs.\n")
    
    try:
        # Demonstrate different types of deprecated usage
        demonstrate_deprecated_functions()
        demonstrate_deprecated_classes()
        demonstrate_class_and_static_methods()
        demonstrate_deprecated_attributes()
        demonstrate_container_methods()
        
        # Run async examples
        print("\n=== Running Async Examples ===")
        asyncio.run(demonstrate_async_deprecation())
        
        # Show comparisons
        demonstrate_comparisons()
        
        print("\n=== Summary ===")
        print("All examples completed! You should have seen multiple deprecation warnings.")
        print("To migrate this code automatically, run:")
        print("  dissolve migrate --write consumer/")
        print("\nTo see what would be changed without writing files:")
        print("  dissolve migrate consumer/")
        print("\nTo check if migration is needed:")
        print("  dissolve migrate --check consumer/")
        
    except Exception as e:
        print(f"Error running examples: {e}")
        import traceback
        traceback.print_exc()


if __name__ == "__main__":
    main()