function main_v12
Main entry point function for the Contract Validity Analyzer application that orchestrates configuration loading, logging setup, FileCloud connection, and contract analysis execution.
/tf/active/vicechatdev/contract_validity_analyzer/main.py
87 - 186
complex
Purpose
This function serves as the primary entry point for a command-line application that analyzes contracts from FileCloud storage. It handles argument parsing, configuration management, logging initialization, optional dry-run mode for document discovery, and executes the full contract analysis pipeline with concurrent processing support. The function manages the complete lifecycle from initialization through execution to summary reporting, including error handling and graceful shutdown.
Source Code
def main():
"""Main entry point."""
args = parse_arguments()
try:
# Load configuration
config = Config(args.config)
# Override configuration with command line arguments
if args.path:
config.set('filecloud', {**config.get_section('filecloud'), 'base_path': args.path})
if args.verbose:
config.set('logging', {**config.get_section('logging'), 'level': 'DEBUG'})
if args.extensions:
extensions = [ext.strip() for ext in args.extensions.split(',')]
config.set('document_processing', {**config.get_section('document_processing'), 'supported_extensions': extensions})
# Set up logging
log_dir = "logs"
if args.output_dir:
log_dir = os.path.join(args.output_dir, "logs")
os.makedirs(log_dir, exist_ok=True)
setup_logging(config.get_section('logging'), log_dir)
logger = get_logger(__name__)
logger.info("Starting Contract Validity Analyzer")
logger.info(f"Configuration: {config.config_path}")
logger.info(f"FileCloud path: {config.get_section('filecloud').get('base_path')}")
logger.info(f"Concurrent threads: {args.concurrent}")
# Create output directory if specified
if args.output_dir:
output_dir = os.path.join(args.output_dir, "output")
os.makedirs(output_dir, exist_ok=True)
config.set('output_dir', output_dir)
# Dry run mode
if args.dry_run:
logger.info("DRY RUN MODE - Discovering documents without processing")
from utils.filecloud_client import FileCloudClient
# Connect to FileCloud and list documents
fc_client = FileCloudClient(config.get_section('filecloud'))
if not fc_client.connect():
logger.error("Failed to connect to FileCloud")
return 1
documents = fc_client.search_documents()
fc_client.disconnect()
if documents:
logger.info(f"Found {len(documents)} documents to analyze:")
for doc in documents:
logger.info(f" - {doc['filename']} ({doc['size']} bytes)")
else:
logger.warning("No documents found")
return 0
# Initialize and run analyzer
analyzer = ContractAnalyzer(config.config)
# Set up analysis parameters
analysis_kwargs = {'max_concurrent': args.concurrent}
if args.max_files:
analysis_kwargs['max_files'] = args.max_files
results = analyzer.analyze_contracts(**analysis_kwargs)
# Print summary
stats = analyzer.get_summary_stats()
if stats:
logger.info("=" * 50)
logger.info("ANALYSIS SUMMARY")
logger.info("=" * 50)
for key, value in stats.items():
logger.info(f"{key.replace('_', ' ').title()}: {value}")
logger.info("=" * 50)
# Print LLM usage stats
llm_stats = analyzer.llm_client.get_usage_stats()
if llm_stats.get('total_tokens', 0) > 0:
logger.info("LLM Usage Statistics:")
logger.info(f" Total tokens: {llm_stats['total_tokens']:,}")
logger.info(f" Prompt tokens: {llm_stats['total_prompt_tokens']:,}")
logger.info(f" Completion tokens: {llm_stats['total_completion_tokens']:,}")
logger.info("Analysis complete!")
return 0
except KeyboardInterrupt:
logger.info("Analysis interrupted by user")
return 1
except Exception as e:
logger.error(f"Fatal error: {e}")
return 1
Return Value
Returns an integer exit code: 0 for successful completion, 1 for errors (including KeyboardInterrupt, connection failures, or fatal exceptions). This follows standard Unix convention for process exit codes.
Dependencies
ossysargparsepathlib
Required Imports
import os
import sys
import argparse
from pathlib import Path
from config.config import Config
from core.analyzer import ContractAnalyzer
from utils.logging_utils import setup_logging
from utils.logging_utils import get_logger
Conditional/Optional Imports
These imports are only needed under specific conditions:
from utils.filecloud_client import FileCloudClient
Condition: only when dry_run mode is enabled (args.dry_run is True)
OptionalUsage Example
# This function is designed to be called as the main entry point of the application
# Typically invoked from a script like:
if __name__ == '__main__':
import sys
sys.exit(main())
# Command line usage examples:
# Basic run:
# python script.py --config config.yaml
# Dry run to discover documents:
# python script.py --config config.yaml --dry-run
# With custom settings:
# python script.py --config config.yaml --path /contracts --verbose --concurrent 5 --max-files 100
# With custom extensions:
# python script.py --config config.yaml --extensions pdf,docx,txt --output-dir ./results
Best Practices
- This function expects parse_arguments() to be defined elsewhere in the module and return a properly structured args object
- Ensure the configuration file exists and contains all required sections (filecloud, logging, document_processing) before calling
- The function creates directories automatically but requires write permissions in the working directory
- Use --dry-run flag first to verify FileCloud connectivity and document discovery before running full analysis
- Monitor the logs directory for detailed execution logs, especially when debugging issues
- The function handles KeyboardInterrupt gracefully, allowing users to stop long-running analyses
- LLM usage statistics are only displayed if tokens were actually consumed during analysis
- Command-line arguments override configuration file settings, allowing flexible runtime customization
- The concurrent parameter controls parallelism; adjust based on system resources and API rate limits
- Exit codes follow Unix conventions: check return value for automation/scripting purposes
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function main_v7 86.4% similar
-
function main_v32 78.2% similar
-
function main 77.0% similar
-
function main_v69 72.3% similar
-
class ContractAnalyzer 71.5% similar