Skip to content

Commit 1bb21b1

Browse files
committed
Converting between sqlite and functional_sqlite backends
1 parent bfcbeaa commit 1bb21b1

4 files changed

Lines changed: 51 additions & 25 deletions

File tree

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
import copy
2+
13
from qtpy import QtWidgets
24

5+
import bw2data as bd
6+
import bw_functional as bf
7+
38
from activity_browser import application
49
from activity_browser.actions.base import ABAction, exception_dialogs
5-
from activity_browser.mod import bw2data as bd
610
from activity_browser.ui.icons import qicons
711
from activity_browser.ui.threading import ABThread
812

13+
from .database_new import NewDatabaseDialog
14+
915

1016
class DatabaseDuplicate(ABAction):
1117
"""
@@ -23,28 +29,34 @@ class DatabaseDuplicate(ABAction):
2329
@exception_dialogs
2430
def run(db_name: str):
2531
assert db_name in bd.databases
32+
backend = bd.databases[db_name].get("backend", "undefined")
2633

27-
new_name, ok = QtWidgets.QInputDialog.getText(
28-
application.main_window,
29-
f"Copy {db_name}",
30-
"Name of new database:" + " " * 25,
31-
)
32-
if not new_name or not ok:
34+
if backend not in ["sqlite", "functional_sqlite"]:
35+
QtWidgets.QMessageBox.information(
36+
application.main_window,
37+
"Not possible",
38+
f"Unsupported database backend {backend}",
39+
)
3340
return
3441

35-
if new_name in bd.databases:
42+
name, backend, ok = NewDatabaseDialog.get_new_database_data(window_title="Duplicate database", backend=backend)
43+
44+
if not name or not ok:
45+
return
46+
47+
if name in bd.databases:
3648
QtWidgets.QMessageBox.information(
3749
application.main_window,
3850
"Not possible",
3951
"A database with this name already exists.",
4052
)
4153
return
4254

43-
DuplicateDatabaseDialog(db_name, new_name, application.main_window)
55+
DuplicateDatabaseDialog(db_name, name, backend, application.main_window)
4456

4557

4658
class DuplicateDatabaseDialog(QtWidgets.QProgressDialog):
47-
def __init__(self, from_db: str, to_db: str, parent=None):
59+
def __init__(self, from_db: str, to_db: str, backend: str, parent=None):
4860
super().__init__(parent=parent)
4961
self.setWindowTitle("Duplicating database")
5062
self.setLabelText(
@@ -53,12 +65,12 @@ def __init__(self, from_db: str, to_db: str, parent=None):
5365
self.setModal(True)
5466
self.setRange(0, 0)
5567

56-
self.dup_thread = DuplicateDatabaseThread(from_db, to_db, self)
68+
self.dup_thread = DuplicateDatabaseThread(application)
5769
self.dup_thread.finished.connect(self.thread_finished)
5870

5971
self.show()
6072

61-
self.dup_thread.start()
73+
self.dup_thread.start(from_db, to_db, backend)
6274

6375
def thread_finished(self) -> None:
6476
self.dup_thread.exit(0)
@@ -67,11 +79,24 @@ def thread_finished(self) -> None:
6779

6880

6981
class DuplicateDatabaseThread(ABThread):
70-
def __init__(self, from_db, to_db, parent=None):
71-
super().__init__(parent=parent)
72-
self.copy_from = from_db
73-
self.copy_to = to_db
7482

75-
def run_safely(self):
76-
database = bd.Database(self.copy_from)
77-
database.copy(self.copy_to)
83+
def run_safely(self, copy_from, copy_to, backend):
84+
database = bd.Database(copy_from)
85+
86+
data = database.load()
87+
data = database.relabel_data(data, copy_from, copy_to)
88+
89+
new_database = bd.Database(copy_to, backend=backend)
90+
91+
metadata = copy.copy(database.metadata)
92+
metadata["format"] = f"Copied from '{copy_from}'"
93+
metadata["backend"] = backend
94+
new_database.register(**metadata)
95+
96+
if database.backend == "sqlite" and backend == "functional_sqlite":
97+
data = bf.convert_sqlite_to_functional_sqlite(data)
98+
elif database.backend == "functional_sqlite" and backend == "sqlite":
99+
data = bf.convert_functional_sqlite_to_sqlite(data)
100+
101+
new_database.write(data, searchable=metadata.get("searchable"))
102+
return new_database

activity_browser/actions/database/database_new.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ class NewDatabaseDialog(QtWidgets.QDialog):
5858
A dialog for creating a new database.
5959
"""
6060

61-
def __init__(self, parent=None):
61+
def __init__(self, window_title="New Database", backend="functional_sqlite", parent=None):
6262
super().__init__(parent)
63-
self.setWindowTitle("New Database")
63+
self.setWindowTitle(window_title)
6464
self.setModal(True)
6565

6666
self.name_input = QtWidgets.QLineEdit(self)
@@ -69,6 +69,7 @@ def __init__(self, parent=None):
6969

7070
self.backend_dropdown = QtWidgets.QComboBox(self)
7171
self.backend_dropdown.addItems(["functional_sqlite", "sqlite"])
72+
self.backend_dropdown.setCurrentText(backend)
7273

7374
self.create_button = QtWidgets.QPushButton("Create", self)
7475
self.create_button.setDisabled(True)
@@ -77,7 +78,7 @@ def __init__(self, parent=None):
7778
self.build_layout()
7879

7980
@classmethod
80-
def get_new_database_data(cls) -> tuple[str, str, bool]:
81+
def get_new_database_data(cls, window_title="New Database", backend="functional_sqlite") -> tuple[str, str, bool]:
8182
"""
8283
Opens a dialog to collect data for creating a new database.
8384
@@ -87,7 +88,7 @@ def get_new_database_data(cls) -> tuple[str, str, bool]:
8788
- The selected backend type (str).
8889
- A boolean indicating whether the dialog was accepted (True) or canceled (False).
8990
"""
90-
dialog = cls(application.main_window)
91+
dialog = cls(window_title, backend, application.main_window)
9192
result = dialog.exec_()
9293

9394
return dialog.name_input.text(), dialog.backend_dropdown.currentText(), result == QtWidgets.QDialog.Accepted

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ dependencies = [
4242
"bw_simapro_csv >=0.2.6",
4343
"ecoinvent_interface",
4444
"matrix_utils>=0.5",
45-
"bw-functional==0b82",
45+
"bw-functional==0b84",
4646
"networkx",
4747
"numpy>=1.23.5,<2",
4848
"pandas>=2.2.1",

recipe/meta.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ requirements:
2727
- bw_graph_tools >=0.5
2828
- bw_processing >=1.0
2929
- bw_simapro_csv >=0.2.6
30-
- bw_functional=0.b.82
30+
- bw_functional=0.b.84
3131
- ecoinvent_interface
3232
- matrix_utils >=0.5
3333
- numpy >=1.23.5, <2

0 commit comments

Comments
 (0)