import shutil import logging import time import os from PyQt5.QtWidgets import QFileDialog from sqlalchemy.exc import SQLAlchemyError from logline_leviathan.database.database_manager import session_scope from logline_leviathan.database.database_operations import DatabaseOperations class DatabaseUtility(): def __init__(self, main_window): self.main_window = main_window self.database_operations = DatabaseOperations(self, main_window.db_init_func) def purgeDatabase(self): if self.main_window.isProcessing(): self.main_window.showProcessingWarning() return try: with session_scope() as db_session: # Close and dispose of any existing database session if db_session: db_session.close() db_session.bind.dispose() # Attempt to delete the database file with retries retries = 3 for attempt in range(retries): try: if os.path.exists('entities.db'): os.remove('entities.db') break except OSError as e: if attempt < retries - 1: time.sleep(0.1) else: raise e # Reinitialize the database self.main_window.db_init_func() self.main_window.statusLabel.setText(" Leere Datenbank initalisiert. Mit der Analyse fortfahren.") logging.debug("Database created.") yaml_data = self.database_operations.loadRegexFromYAML() self.database_operations.populate_and_update_entities_from_yaml(yaml_data) self.main_window.refreshApplicationState() self.main_window.generate_report_window.updateCheckboxes() # Add this line to update the checkboxes self.main_window.generate_wordlist_window.updateCheckboxes() except SQLAlchemyError as e: logging.error(f"Error creating database: {e}") except Exception as e: logging.error(f"General error: {e}") def importDatabase(self): if self.main_window.isProcessing(): self.main_window.showProcessingWarning() return options = QFileDialog.Options() db_file, _ = QFileDialog.getOpenFileName(self.main_window, "Select External Database", "", "Database Files (*.db);;All Files (*)", options=options) if db_file and db_file.endswith(".db"): try: shutil.copy(db_file, 'entities.db') self.main_window.current_db_path = db_file self.main_window.statusLabel.setText(" Bestehende Datenbank für diese Sitzung ausgewählt.") self.main_window.refreshApplicationState() self.main_window.generate_report_window.updateCheckboxes() # Add this line to update the checkboxes self.main_window.generate_wordlist_window.updateCheckboxes() except Exception as e: logging.error(f"Error selecting external database: {e}") self.main_window.statusLabel.setText(f" Fehler bei der Auswahl der Datenbank: {e}") else: self.main_window.statusLabel.setText(" Keine gueltige Datenbank ausgewählt.") def exportDatabase(self): if self.main_window.isProcessing(): self.main_window.showProcessingWarning() return options = QFileDialog.Options() default_filename = "entities_" + time.strftime('%Y%m%d_%H%M%S') + ".db" save_path, _ = QFileDialog.getSaveFileName(self.main_window, "Save Database File", default_filename, "Database Files (*.db);;All Files (*)", options=options) if save_path: try: shutil.copy('entities.db', save_path) self.main_window.statusLabel.setText(f" Datenbank erfolgreich exportiert nach {save_path}") except Exception as e: logging.error(f"Error exporting database: {e}") self.main_window.statusLabel.setText(f" Fehler beim Exportieren der Datenbank: {e}")