diff options
author | Jaime van Kessel <nallath@gmail.com> | 2021-08-20 16:40:50 +0300 |
---|---|---|
committer | Jaime van Kessel <nallath@gmail.com> | 2021-08-20 16:40:50 +0300 |
commit | e0e8223c5df223069243a2192e1290e6da14e6bc (patch) | |
tree | 4c4fc02f9981a6e5ac09a7bff6ff61c99d8e4881 /cura/Settings | |
parent | 27f0fe8bb949a1be641ba7fee1f6dc74a61b7023 (diff) |
Move DB handling into specialised classes
This keeps CuraContainerRegistry a lot cleaner, especially when more container types are
added
Cura-6096
Diffstat (limited to 'cura/Settings')
-rw-r--r-- | cura/Settings/CuraContainerRegistry.py | 107 | ||||
-rw-r--r-- | cura/Settings/DatabaseHandlers/IntentDatabaseHandler.py | 35 | ||||
-rw-r--r-- | cura/Settings/DatabaseHandlers/QualityDatabaseHandler.py | 44 | ||||
-rw-r--r-- | cura/Settings/DatabaseHandlers/VariantDatabaseHandler.py | 29 | ||||
-rw-r--r-- | cura/Settings/DatabaseHandlers/__init__.py | 0 |
5 files changed, 116 insertions, 99 deletions
diff --git a/cura/Settings/CuraContainerRegistry.py b/cura/Settings/CuraContainerRegistry.py index fc50b975df..1b880d037d 100644 --- a/cura/Settings/CuraContainerRegistry.py +++ b/cura/Settings/CuraContainerRegistry.py @@ -10,6 +10,7 @@ from PyQt5.QtWidgets import QMessageBox from UM.Decorators import override from UM.Settings.ContainerFormatError import ContainerFormatError +from UM.Settings.DatabaseContainerMetadataController import DatabaseMetadataContainerController from UM.Settings.Interfaces import ContainerInterface from UM.Settings.ContainerRegistry import ContainerRegistry from UM.Settings.ContainerStack import ContainerStack @@ -32,6 +33,10 @@ from cura.Machines.ContainerTree import ContainerTree from cura.ReaderWriters.ProfileReader import NoProfileException, ProfileReader from UM.i18n import i18nCatalog +from .DatabaseHandlers.IntentDatabaseHandler import IntentDatabaseHandler +from .DatabaseHandlers.QualityDatabaseHandler import QualityDatabaseHandler +from .DatabaseHandlers.VariantDatabaseHandler import VariantDatabaseHandler + catalog = i18nCatalog("cura") @@ -44,106 +49,10 @@ class CuraContainerRegistry(ContainerRegistry): # is added, we check to see if an extruder stack needs to be added. self.containerAdded.connect(self._onContainerAdded) - self._prepare_for_database_handlers["variant"] = self._prepareVariantForDatabase - self._prepare_for_database_handlers["quality"] = self._prepareQualityForDatabase - self._prepare_for_database_handlers["intent"] = self._prepareIntentForDatabase - - self._get_from_database_handlers["variant"] = self._getVariantFromDatabase - self._get_from_database_handlers["quality"] = self._getQualityFromDatabase - self._get_from_database_handlers["intent"] = self._getIntentFromDatabase - - self._insert_into_database_queries["variant"] = "INSERT INTO variants (id, name, hardware_type, definition, version, setting_version) VALUES (?, ?, ?, ?, ?, ?)" - self._insert_into_database_queries["quality"] = "INSERT INTO qualities (id, name, quality_type, material, variant, global_quality, definition, version, setting_version) VALUES (?, ?, ? ,?, ?, ?, ?, ?, ?)" - self._insert_into_database_queries["intent"] = "INSERT INTO intents (id, name, quality_type, intent_category, variant, definition, material, version, setting_version) VALUES (?, ?, ? ,?, ?, ?, ?, ?, ?)" - - def _getQualityFromDatabase(self, container_id): - connection = self._getDatabaseConnection() - result = connection.cursor().execute("SELECT * FROM qualities where id = ?", (container_id,)) - data = result.fetchone() - return {"id": data[0], "name": data[1], "quality_type": data[2], "material": data[3], "variant": data[4], "global_quality": data[5], "definition": data[6], "container_type": InstanceContainer, "version": data[7], "setting_version": data[8], "type": "quality"} - - def _getVariantFromDatabase(self, container_id): - connection = self._getDatabaseConnection() - result = connection.cursor().execute("SELECT * FROM variants where id = ?", (container_id,)) - data = result.fetchone() - return {"id": data[0], "name": data[1], "hardware_type": data[2], "definition": data[3], "container_type": InstanceContainer, "version": data[4], "setting_version": data[5], "type": "variant"} - - def _getIntentFromDatabase(self, container_id): - connection = self._getDatabaseConnection() - result = connection.cursor().execute("SELECT * FROM intents where id = ?", (container_id,)) - data = result.fetchone() - return {"id": data[0], "name": data[1], "quality_type": data[2], "intent_category": data[3], "variant": data[4], "definition": data[5], "container_type": InstanceContainer, "material": data[6], "version": data[7], "setting_version": data[8], "type": "intent"} - - def _prepareVariantForDatabase(self, metadata): - return metadata["id"], metadata["name"], metadata["hardware_type"], metadata["definition"], metadata["version"], metadata["setting_version"] - - def _prepareQualityForDatabase(self, metadata): - global_quality = False - if "global_quality" in metadata: - global_quality = metadata["global_quality"] - material = "" - if "material" in metadata: - material = metadata["material"] - variant = "" - if "variant" in metadata: - variant = metadata["variant"] - - return metadata["id"], metadata["name"], metadata["quality_type"], material, variant, global_quality, metadata["definition"], metadata["version"], metadata["setting_version"] - - def _prepareIntentForDatabase(self, metadata) -> None: - return metadata["id"], metadata["name"], metadata["quality_type"], metadata["intent_category"], metadata["variant"], metadata["definition"], metadata["material"], metadata["version"], metadata["setting_version"] - connection = self._getDatabaseConnection() - - connection.cursor().execute( - "INSERT INTO intents (id, name, quality_type, intent_category, variant, definition) VALUES (?, ?, ? ,?, ?, ?)", - ()) - + self._database_handlers["variant"] = VariantDatabaseHandler() + self._database_handlers["quality"] = QualityDatabaseHandler() + self._database_handlers["intent"] = IntentDatabaseHandler() - @override(ContainerRegistry) - def _createDatabaseFile(self, db_path: str) -> None: - connection = super()._createDatabaseFile(db_path) - cursor = connection.cursor() - cursor.execute(""" - CREATE TABLE qualities - ( - id text, - name text, - quality_type text, - material text, - variant text, - global_quality bool, - definition text, - version text, - setting_version text - ); - CREATE UNIQUE INDEX idx_qualities_id on qualities (id); - - CREATE TABLE variants - ( - id text, - name text, - hardware_type text, - definition text, - version text, - setting_version text - ); - CREATE UNIQUE INDEX idx_variants_id on variants (id); - - CREATE TABLE intents - ( - id text, - name text, - quality_type text, - intent_category text, - variant text, - definition text, - material text, - version text, - setting_version text - ); - CREATE UNIQUE INDEX idx_intents_id on intents (id); - """) - return connection @override(ContainerRegistry) def addContainer(self, container: ContainerInterface) -> bool: diff --git a/cura/Settings/DatabaseHandlers/IntentDatabaseHandler.py b/cura/Settings/DatabaseHandlers/IntentDatabaseHandler.py new file mode 100644 index 0000000000..79a28e7fb8 --- /dev/null +++ b/cura/Settings/DatabaseHandlers/IntentDatabaseHandler.py @@ -0,0 +1,35 @@ +from UM.Settings.DatabaseContainerMetadataController import DatabaseMetadataContainerController +from UM.Settings.InstanceContainer import InstanceContainer + + +class IntentDatabaseHandler(DatabaseMetadataContainerController): + def __init__(self) -> None: + super().__init__( + insert_query="INSERT INTO intents (id, name, quality_type, intent_category, variant, definition, material, version, setting_version) VALUES (?, ?, ? ,?, ?, ?, ?, ?, ?)", + update_query="", + select_query = "SELECT * FROM intents where id = ?", + table_query="""CREATE TABLE intents + ( + id text, + name text, + quality_type text, + intent_category text, + variant text, + definition text, + material text, + version text, + setting_version text + ); + CREATE UNIQUE INDEX idx_intents_id on intents (id);""" + ) + + def _convertRawDataToMetadata(self, data): + return {"id": data[0], "name": data[1], "quality_type": data[2], "intent_category": data[3], "variant": data[4], "definition": data[5], "container_type": InstanceContainer, "material": data[6], "version": data[7], "setting_version": data[8], "type": "intent"} + + + def _convertMetadataToInsertBatch(self, metadata): + return metadata["id"], metadata["name"], metadata["quality_type"], metadata["intent_category"], metadata[ + "variant"], metadata["definition"], metadata["material"], metadata["version"], metadata["setting_version"] + + + diff --git a/cura/Settings/DatabaseHandlers/QualityDatabaseHandler.py b/cura/Settings/DatabaseHandlers/QualityDatabaseHandler.py new file mode 100644 index 0000000000..fcc21fe5b9 --- /dev/null +++ b/cura/Settings/DatabaseHandlers/QualityDatabaseHandler.py @@ -0,0 +1,44 @@ +from UM.Settings.DatabaseContainerMetadataController import DatabaseMetadataContainerController +from UM.Settings.InstanceContainer import InstanceContainer + + +class QualityDatabaseHandler(DatabaseMetadataContainerController): + def __init__(self) -> None: + super().__init__( + insert_query = "INSERT INTO qualities (id, name, quality_type, material, variant, global_quality, definition, version, setting_version) VALUES (?, ?, ? ,?, ?, ?, ?, ?, ?)", + update_query = "", + select_query = "SELECT * FROM qualities where id = ?", + table_query = """CREATE TABLE qualities + ( + id text, + name text, + quality_type text, + material text, + variant text, + global_quality bool, + definition text, + version text, + setting_version text + ); + CREATE UNIQUE INDEX idx_qualities_id on qualities (id);""" + ) + + def _convertRawDataToMetadata(self, data): + return {"id": data[0], "name": data[1], "quality_type": data[2], "material": data[3], "variant": data[4], + "global_quality": data[5], "definition": data[6], "container_type": InstanceContainer, + "version": data[7], "setting_version": data[8], "type": "quality"} + + def _convertMetadataToInsertBatch(self, metadata): + global_quality = False + if "global_quality" in metadata: + global_quality = metadata["global_quality"] + material = "" + if "material" in metadata: + material = metadata["material"] + variant = "" + if "variant" in metadata: + variant = metadata["variant"] + + return metadata["id"], metadata["name"], metadata["quality_type"], material, variant, global_quality, metadata["definition"], metadata["version"], metadata["setting_version"] + + diff --git a/cura/Settings/DatabaseHandlers/VariantDatabaseHandler.py b/cura/Settings/DatabaseHandlers/VariantDatabaseHandler.py new file mode 100644 index 0000000000..37bf40d25c --- /dev/null +++ b/cura/Settings/DatabaseHandlers/VariantDatabaseHandler.py @@ -0,0 +1,29 @@ +from UM.Settings.DatabaseContainerMetadataController import DatabaseMetadataContainerController +from UM.Settings.InstanceContainer import InstanceContainer + + +class VariantDatabaseHandler(DatabaseMetadataContainerController): + def __init__(self) -> None: + super().__init__( + insert_query= "INSERT INTO variants (id, name, hardware_type, definition, version, setting_version) VALUES (?, ?, ?, ?, ?, ?)", + update_query= "", + select_query= "SELECT * FROM variants where id = ?", + table_query="""CREATE TABLE variants + ( + id text, + name text, + hardware_type text, + definition text, + version text, + setting_version text + ); + CREATE UNIQUE INDEX idx_variants_id on variants (id);""" + ) + + def _convertRawDataToMetadata(self, data): + return {"id": data[0], "name": data[1], "hardware_type": data[2], "definition": data[3], "container_type": InstanceContainer, "version": data[4], "setting_version": data[5], "type": "variant"} + + + def _convertMetadataToInsertBatch(self, metadata): + return metadata["id"], metadata["name"], metadata["hardware_type"], metadata["definition"], metadata["version"], \ + metadata["setting_version"] diff --git a/cura/Settings/DatabaseHandlers/__init__.py b/cura/Settings/DatabaseHandlers/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/cura/Settings/DatabaseHandlers/__init__.py |