Projektarbeit-MYP/backend/scripts/quick_unicode_fix.py
2025-06-05 01:34:10 +02:00

148 lines
4.5 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
Quick Unicode Fix für MYP Backend
=================================
Behebt Unicode-Encoding-Probleme in logging_config.py
für Windows CP1252-Umgebungen.
Verwendung: python quick_unicode_fix.py
"""
import os
import re
from pathlib import Path
def fix_unicode_in_file(file_path: Path):
"""Ersetzt Unicode-Emojis durch ASCII-Text in einer Datei"""
print(f"🔧 Bearbeite: {file_path}")
# Emoji-Ersetzungen definieren
replacements = {
'\\u2705': '[OK]', # ✅ -> [OK]
'\\u274c': '[ERROR]', # ❌ -> [ERROR]
'\\u26a0': '[WARN]', # ⚠️ -> [WARN]
'\\u2139': '[INFO]', # -> [INFO]
'': '[OK]',
'': '[ERROR]',
'⚠️': '[WARN]',
'': '[INFO]',
# Weitere häufige Emojis
'🚀': '[START]',
'🔄': '[RESTART]',
'📋': '[LIST]',
'🎉': '[SUCCESS]',
'💥': '[CRASH]',
'🔍': '[SEARCH]',
'📊': '[STATS]',
'🛑': '[STOP]',
'⏭️': '[SKIP]',
'🚨': '[ALERT]',
}
try:
# Datei lesen
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
original_content = content
changes_made = 0
# Ersetzungen durchführen
for emoji, replacement in replacements.items():
if emoji in content:
content = content.replace(emoji, replacement)
changes_made += 1
print(f" ✓ Ersetzt: {emoji} -> {replacement}")
# UTF-8 Encoding-Zwang hinzufügen (falls nicht vorhanden)
encoding_fix = """
# UTF-8 Encoding für Windows-Kompatibilität
import sys
if hasattr(sys.stdout, 'reconfigure'):
try:
sys.stdout.reconfigure(encoding='utf-8')
except:
pass
"""
if "sys.stdout.reconfigure" not in content:
# Füge nach den Imports ein
import_pattern = r'(import\s+.*?\n)(\n*)(class|def|\w+\s*=)'
if re.search(import_pattern, content, re.MULTILINE):
content = re.sub(
import_pattern,
r'\1' + encoding_fix + r'\n\3',
content,
count=1,
flags=re.MULTILINE
)
changes_made += 1
print(f" ✓ UTF-8 Encoding-Fix hinzugefügt")
# Datei schreiben (nur wenn Änderungen)
if changes_made > 0:
# Backup erstellen
backup_path = file_path.with_suffix(f'{file_path.suffix}.backup')
with open(backup_path, 'w', encoding='utf-8') as f:
f.write(original_content)
print(f" 📂 Backup erstellt: {backup_path}")
# Geänderte Datei schreiben
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"{changes_made} Änderungen gespeichert")
else:
print(f" Keine Änderungen erforderlich")
return changes_made
except Exception as e:
print(f" ❌ Fehler bei {file_path}: {e}")
return 0
def main():
"""Hauptfunktion - führt Unicode-Fix durch"""
print("🚀 MYP Backend Unicode-Fix")
print("=" * 50)
backend_dir = Path(__file__).parent
files_to_fix = [
backend_dir / "utils" / "logging_config.py",
backend_dir / "app_cleaned.py",
backend_dir / "app.py",
]
total_changes = 0
fixed_files = 0
for file_path in files_to_fix:
if file_path.exists():
changes = fix_unicode_in_file(file_path)
total_changes += changes
if changes > 0:
fixed_files += 1
else:
print(f"⚠️ Datei nicht gefunden: {file_path}")
print("\n" + "=" * 50)
print("📊 ZUSAMMENFASSUNG")
print("=" * 50)
print(f"✅ Bearbeitete Dateien: {len([f for f in files_to_fix if f.exists()])}")
print(f"🔧 Dateien mit Änderungen: {fixed_files}")
print(f"📝 Gesamte Änderungen: {total_changes}")
if total_changes > 0:
print(f"\n🎉 Unicode-Fix erfolgreich angewendet!")
print(f"💡 Teste jetzt: python -c \"import app_cleaned\"")
else:
print(f"\n Keine Unicode-Probleme gefunden.")
print("\n🔄 Für Systemweite UTF-8-Unterstützung:")
print(" set PYTHONIOENCODING=utf-8")
if __name__ == "__main__":
main()