Spaces:
Runtime error
Runtime error
File size: 5,221 Bytes
1d95600 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
import os
import logging
import hashlib
from typing import Dict, Any, Optional
from datetime import datetime
import json
def setup_logging():
"""Setup logging configuration"""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(),
logging.FileHandler('app.log')
]
)
def ensure_directory(path: str) -> bool:
"""Ensure directory exists, create if it doesn't"""
try:
os.makedirs(path, exist_ok=True)
return True
except Exception as e:
logging.error(f"Failed to create directory {path}: {str(e)}")
return False
def generate_file_hash(content: str) -> str:
"""Generate SHA-256 hash for content"""
return hashlib.sha256(content.encode()).hexdigest()
def sanitize_filename(filename: str) -> str:
"""Sanitize filename for safe file system operations"""
import re
# Remove invalid characters
filename = re.sub(r'[<>:"/\\|?*]', '_', filename)
# Limit length
if len(filename) > 255:
name, ext = os.path.splitext(filename)
filename = name[:255-len(ext)] + ext
return filename
def format_file_size(size_bytes: int) -> str:
"""Format file size in human readable format"""
if size_bytes == 0:
return "0B"
size_names = ["B", "KB", "MB", "GB", "TB"]
import math
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return f"{s} {size_names[i]}"
def validate_environment():
"""Validate required environment variables"""
required_vars = ['OPENAI_API_KEY']
missing_vars = []
for var in required_vars:
if not os.getenv(var):
missing_vars.append(var)
if missing_vars:
logging.warning(f"Missing environment variables: {', '.join(missing_vars)}")
return False
return True
def save_json_file(data: Dict[Any, Any], filepath: str) -> bool:
"""Save data to JSON file"""
try:
ensure_directory(os.path.dirname(filepath))
with open(filepath, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
return True
except Exception as e:
logging.error(f"Failed to save JSON file {filepath}: {str(e)}")
return False
def load_json_file(filepath: str) -> Optional[Dict[Any, Any]]:
"""Load data from JSON file"""
try:
if not os.path.exists(filepath):
return None
with open(filepath, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
logging.error(f"Failed to load JSON file {filepath}: {str(e)}")
return None
def cleanup_old_files(directory: str, max_age_hours: int = 24) -> int:
"""Clean up old files in directory"""
try:
if not os.path.exists(directory):
return 0
now = datetime.now()
removed_count = 0
for filename in os.listdir(directory):
filepath = os.path.join(directory, filename)
if os.path.isfile(filepath):
file_age = now - datetime.fromtimestamp(os.path.getmtime(filepath))
if file_age.total_seconds() > max_age_hours * 3600:
try:
os.remove(filepath)
removed_count += 1
logging.info(f"Removed old file: {filepath}")
except Exception as e:
logging.error(f"Failed to remove file {filepath}: {str(e)}")
return removed_count
except Exception as e:
logging.error(f"Failed to cleanup directory {directory}: {str(e)}")
return 0
def get_system_info() -> Dict[str, Any]:
"""Get basic system information"""
import platform
import psutil
try:
return {
'platform': platform.system(),
'python_version': platform.python_version(),
'cpu_count': os.cpu_count(),
'memory_gb': round(psutil.virtual_memory().total / (1024**3), 2),
'disk_usage': {
'total_gb': round(psutil.disk_usage('/').total / (1024**3), 2),
'free_gb': round(psutil.disk_usage('/').free / (1024**3), 2)
}
}
except Exception as e:
logging.error(f"Failed to get system info: {str(e)}")
return {'error': str(e)}
def measure_execution_time(func):
"""Decorator to measure function execution time"""
import functools
import time
@functools.wraps(func)
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
execution_time = end_time - start_time
logging.info(f"{func.__name__} executed in {execution_time:.2f} seconds")
return result
return wrapper
# Initialize logging when module is imported
setup_logging()
# Validate environment on import
if not validate_environment():
logging.warning("Environment validation failed. Some features may not work properly.")
|