|
| 1 | +from logging import getLogger |
| 2 | +from typing import List |
| 3 | + |
| 4 | +from qtpy import QtWidgets |
| 5 | + |
| 6 | +from activity_browser import application |
| 7 | +from activity_browser.actions.base import ABAction, exception_dialogs |
| 8 | +from activity_browser.ui import widgets, threading |
| 9 | +from activity_browser.bwutils import exporters |
| 10 | + |
| 11 | +log = getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class DatabaseExportBW2Package(ABAction): |
| 15 | + """ |
| 16 | + ABAction to export database(s) to BW2Package format (.bw2package). |
| 17 | + """ |
| 18 | + |
| 19 | + # icon = icons.qicons.export_db |
| 20 | + text = "Export to .bw2package" |
| 21 | + tool_tip = "Export database(s) to BW2Package format" |
| 22 | + |
| 23 | + @classmethod |
| 24 | + @exception_dialogs |
| 25 | + def run(cls, db_names: List[str] = None): |
| 26 | + if db_names is None: |
| 27 | + import bw2data as bd |
| 28 | + dialog = widgets.ABDatabaseSelectionDialog( |
| 29 | + parent=application.main_window, |
| 30 | + databases=sorted(bd.databases), |
| 31 | + title="Select databases to export to BW2Package" |
| 32 | + ) |
| 33 | + if dialog.exec_() == QtWidgets.QDialog.Accepted: |
| 34 | + db_names = dialog.get_selected_databases() |
| 35 | + else: |
| 36 | + return |
| 37 | + |
| 38 | + # Get export directory or file from user |
| 39 | + if len(db_names) == 1: |
| 40 | + # Single database - suggest a filename |
| 41 | + suggested_name = f"{db_names[0]}.bw2package" |
| 42 | + path, _ = QtWidgets.QFileDialog.getSaveFileName( |
| 43 | + parent=application.main_window, |
| 44 | + caption=f'Export database "{db_names[0]}" to BW2Package', |
| 45 | + directory=suggested_name, |
| 46 | + filter='Brightway2 Database Package (*.bw2package);; All files (*.*)' |
| 47 | + ) |
| 48 | + else: |
| 49 | + # Multiple databases - ask for directory |
| 50 | + path = QtWidgets.QFileDialog.getExistingDirectory( |
| 51 | + parent=application.main_window, |
| 52 | + caption=f'Select directory to export {len(db_names)} databases', |
| 53 | + ) |
| 54 | + |
| 55 | + if not path: |
| 56 | + return |
| 57 | + |
| 58 | + # Show export dialog |
| 59 | + context = { |
| 60 | + "db_names": db_names, |
| 61 | + "path": path, |
| 62 | + } |
| 63 | + export_dialog = ExportBW2PackageSetup( |
| 64 | + parent=application.main_window, |
| 65 | + title="Export to BW2Package", |
| 66 | + context=context |
| 67 | + ) |
| 68 | + export_dialog.exec_() |
| 69 | + |
| 70 | + |
| 71 | +class ExportBW2PackageSetup(widgets.ABWizard): |
| 72 | + """Wizard for exporting databases to BW2Package format.""" |
| 73 | + |
| 74 | + class ExportPage(widgets.ABThreadedWizardPage): |
| 75 | + """Wizard page to export the selected database(s) to BW2Package.""" |
| 76 | + title = "Exporting Database(s)" |
| 77 | + subtitle = "Exporting database(s) to .bw2package file(s)" |
| 78 | + |
| 79 | + class Thread(threading.ABThread): |
| 80 | + """Thread to handle the export process.""" |
| 81 | + |
| 82 | + def run_safely(self, db_names: List[str], path: str): |
| 83 | + """Export the database(s) to BW2Package.""" |
| 84 | + for db_name in db_names: |
| 85 | + try: |
| 86 | + success = exporters.store_database_as_package(db_name, path) |
| 87 | + if success: |
| 88 | + log.info(f"Successfully exported database '{db_name}' to BW2Package") |
| 89 | + else: |
| 90 | + log.error(f"Failed to export database '{db_name}'") |
| 91 | + raise RuntimeError(f"Database '{db_name}' not found") |
| 92 | + except Exception as e: |
| 93 | + log.error(f"Failed to export database '{db_name}': {e}") |
| 94 | + raise |
| 95 | + |
| 96 | + def initializePage(self, context: dict): |
| 97 | + """Start the export thread.""" |
| 98 | + self.thread.start(context["db_names"], context["path"]) |
| 99 | + |
| 100 | + pages = [ExportPage] |
0 commit comments