Files
Projektarbeit-MYP/backend/utils/fix_session_usage.py

62 lines
2.0 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.11
"""
Skript zur automatischen Behebung von get_cached_session() Aufrufen
Konvertiert direkte Session-Aufrufe zu Context Manager Pattern.
Autor: MYP Team
Datum: 2025-06-09
"""
import re
import os
def fix_session_usage(file_path):
"""Behebt Session-Usage in einer Datei"""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Pattern für direkte Session-Aufrufe
patterns = [
# session = get_cached_session() -> with get_cached_session() as session:
(r'(\s+)session = get_cached_session\(\)', r'\1with get_cached_session() as session:'),
# session.close() entfernen (wird automatisch durch Context Manager gemacht)
(r'\s+session\.close\(\)\s*\n', '\n'),
# Einrückung nach with-Statement anpassen
(r'(with get_cached_session\(\) as session:\s*\n)(\s+)([^\s])',
lambda m: m.group(1) + m.group(2) + ' ' + m.group(3))
]
original_content = content
for pattern, replacement in patterns:
if callable(replacement):
content = re.sub(pattern, replacement, content, flags=re.MULTILINE)
else:
content = re.sub(pattern, replacement, content, flags=re.MULTILINE)
# Nur schreiben wenn sich etwas geändert hat
if content != original_content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"{file_path} wurde aktualisiert")
return True
else:
print(f" {file_path} benötigt keine Änderungen")
return False
def main():
"""Hauptfunktion"""
backend_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
user_mgmt_file = os.path.join(backend_dir, 'blueprints', 'user_management.py')
if os.path.exists(user_mgmt_file):
print(f"Bearbeite {user_mgmt_file}...")
fix_session_usage(user_mgmt_file)
else:
print(f"❌ Datei nicht gefunden: {user_mgmt_file}")
if __name__ == "__main__":
main()