#!/usr/bin/env python3 """ Erweiterte Template-Problemanalyse für MYP Admin Panel Vollständige Identifikation aller Template-Probleme mit Kontextanalyse Autor: MYP Team - Till Tomczak Datum: 2025-06-19 """ import re import json from typing import Dict, List, Set, Tuple class AdvancedTemplateAnalyzer: """ Erweiterte Template-Problemanalyse mit detaillierter Kontextverarbeitung """ def __init__(self): self.template_path = "/mnt/c/Users/TTOMCZA.EMEA/Dev/Projektarbeit-MYP/backend/templates/admin.html" self.blueprint_path = "/mnt/c/Users/TTOMCZA.EMEA/Dev/Projektarbeit-MYP/backend/blueprints/admin_unified.py" self.app_path = "/mnt/c/Users/TTOMCZA.EMEA/Dev/Projektarbeit-MYP/backend/app.py" self.problems = [] self.corrections = [] self.all_endpoints = set() def analyze_all_endpoints(self): """ Analysiere alle verfügbaren Endpoints aus allen Quellen """ print("🔍 Vollständige Endpoint-Analyse") # 1. Admin Blueprint Endpoints with open(self.blueprint_path, 'r', encoding='utf-8') as f: blueprint_content = f.read() # Admin Blueprint Routen admin_routes = re.findall(r'@admin_blueprint\.route\(["\']([^"\']+)["\'][^)]*\)\s*@admin_required\s*def\s+([a-zA-Z_][a-zA-Z0-9_]*)', blueprint_content, re.MULTILINE | re.DOTALL) for route_path, function_name in admin_routes: self.all_endpoints.add(f"admin.{function_name}") # Admin API Blueprint Routen api_routes = re.findall(r'@admin_api_blueprint\.route\(["\']([^"\']+)["\'][^)]*\)\s*@admin_required\s*def\s+([a-zA-Z_][a-zA-Z0-9_]*)', blueprint_content, re.MULTILINE | re.DOTALL) for route_path, function_name in api_routes: self.all_endpoints.add(f"admin_api.{function_name}") # 2. App.py Endpoints (Haupt-Routen) with open(self.app_path, 'r', encoding='utf-8') as f: app_content = f.read() # Haupt-App Routen ohne Blueprint app_routes = re.findall(r'@app\.route\(["\']([^"\']+)["\'][^)]*\)\s*(?:@[a-zA-Z_][a-zA-Z0-9_]*\s*)*def\s+([a-zA-Z_][a-zA-Z0-9_]*)', app_content, re.MULTILINE | re.DOTALL) for route_path, function_name in app_routes: self.all_endpoints.add(function_name) print(f" ✓ Insgesamt {len(self.all_endpoints)} Endpoints gefunden") def analyze_template_problems(self): """ Analysiere spezifische Template-Probleme """ print("\n🔍 Template-Problem-Analyse") with open(self.template_path, 'r', encoding='utf-8') as f: template_content = f.read() # Problem 1: Fehlende Blueprint-Referenz für jobs_page if "jobs.jobs_page" in template_content: self.problems.append({ 'type': 'incorrect_blueprint_reference', 'line': 179, 'issue': "jobs.jobs_page existiert nicht als Blueprint", 'current': "url_for('jobs.jobs_page')", 'correct': "url_for('jobs_page')", 'description': "Referenz auf jobs_page ohne Blueprint-Prefix" }) # Problem 2: Überprüfe komplexe url_for Konstrukte complex_url_pattern = r"url_for\(['\"]([^'\"]+)['\"].*if.*else.*\)" complex_matches = re.findall(complex_url_pattern, template_content) for match in complex_matches: if match not in self.all_endpoints: self.problems.append({ 'type': 'complex_url_for_construct', 'issue': f"Komplexes url_for Konstrukt mit möglicherweise nicht existierendem Endpoint: {match}", 'current': match, 'description': "Komplexe bedingte url_for Referenz" }) # Problem 3: Überprüfe Parameter-basierte url_for Aufrufe param_url_pattern = r"url_for\(['\"]([^'\"]+)['\"][^)]+\)" param_matches = re.findall(param_url_pattern, template_content) for match in param_matches: if match not in self.all_endpoints: self.problems.append({ 'type': 'parametrized_url_for', 'issue': f"Parametrisierter url_for Aufruf mit möglicherweise nicht existierendem Endpoint: {match}", 'current': match, 'description': "URL-Parameter-basierte Referenz" }) print(f" ❌ {len(self.problems)} Template-Probleme identifiziert") def generate_fixes(self): """ Generiere konkrete Fixes für alle identifizierten Probleme """ print("\n🔧 Fix-Generierung") with open(self.template_path, 'r', encoding='utf-8') as f: template_content = f.read() for problem in self.problems: if problem['type'] == 'incorrect_blueprint_reference': # Fix für jobs.jobs_page → jobs_page old_string = "{{ url_for('jobs.jobs_page') if 'jobs' in url_for.__globals__ else '#' }}" new_string = "{{ url_for('jobs_page') }}" if old_string in template_content: self.corrections.append({ 'old_string': old_string, 'new_string': new_string, 'description': 'Korrigiere jobs.jobs_page → jobs_page', 'line': problem.get('line', 'unknown'), 'confidence': 'high' }) print(f" ✓ {len(self.corrections)} Korrekturen generiert") def generate_detailed_report(self): """ Generiere detaillierten Analysebericht """ return { 'analysis_summary': { 'total_endpoints': len(self.all_endpoints), 'problems_found': len(self.problems), 'corrections_available': len(self.corrections), 'analysis_scope': ['admin.html', 'admin_unified.py', 'app.py'] }, 'endpoints_discovered': sorted(list(self.all_endpoints)), 'problems_identified': self.problems, 'corrections_generated': self.corrections, 'recommendations': [ "Verwende konsistente Blueprint-Referenzen", "Vermeide komplexe bedingte url_for Konstrukte", "Überprüfe alle Parameter-basierten URL-Generierungen", "Implementiere einheitliche Namenskonventionen" ] } def main(): """ Hauptfunktion für erweiterte Template-Analyse """ print("🚀 MYP Template-Problemanalyse Tool") print("=" * 60) analyzer = AdvancedTemplateAnalyzer() # Vollständige Analyse analyzer.analyze_all_endpoints() analyzer.analyze_template_problems() analyzer.generate_fixes() # Generiere detaillierten Bericht report = analyzer.generate_detailed_report() print("\n📊 DETAILLIERTER ANALYSEBERICHT") print("=" * 60) print(f"Gesamte Endpoints: {report['analysis_summary']['total_endpoints']}") print(f"Identifizierte Probleme: {report['analysis_summary']['problems_found']}") print(f"Verfügbare Korrekturen: {report['analysis_summary']['corrections_available']}") if report['problems_identified']: print("\n❌ IDENTIFIZIERTE PROBLEME:") for i, problem in enumerate(report['problems_identified'], 1): print(f" {i}. {problem['issue']}") print(f" Typ: {problem['type']}") if 'line' in problem: print(f" Zeile: {problem['line']}") print(f" Aktuell: {problem.get('current', 'N/A')}") print(f" Korrekt: {problem.get('correct', 'N/A')}") print() if report['corrections_generated']: print("🔧 GENERIERTE KORREKTUREN:") for i, correction in enumerate(report['corrections_generated'], 1): print(f" {i}. {correction['description']}") print(f" Vertrauen: {correction['confidence']}") print(f" Zeile: {correction['line']}") print(f" Alt: {correction['old_string']}") print(f" Neu: {correction['new_string']}") print() print("💡 EMPFEHLUNGEN:") for rec in report['recommendations']: print(f" • {rec}") # Speichere erweiterten Bericht with open('/mnt/c/Users/TTOMCZA.EMEA/Dev/Projektarbeit-MYP/backend/template_problem_report.json', 'w', encoding='utf-8') as f: json.dump(report, f, indent=2, ensure_ascii=False) print(f"\n💾 Detaillierter Bericht gespeichert: template_problem_report.json") return report if __name__ == "__main__": main()