From 66122d583ea68e8ffdab0a4afcd34bbe7f241d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Carrillo?= Date: Tue, 26 Nov 2024 00:01:03 -0500 Subject: [PATCH] [ili2db] Add export_metaconfig handy method to Ili2dbUtils; parse parameters from an exported metaConfig .ini file in ilitoppingmaker --- modelbaker/ilitoppingmaker/ili2dbsettings.py | 15 +++++++ modelbaker/utils/ili2db_utils.py | 45 +++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/modelbaker/ilitoppingmaker/ili2dbsettings.py b/modelbaker/ilitoppingmaker/ili2dbsettings.py index 9b4ea87..8146e8d 100644 --- a/modelbaker/ilitoppingmaker/ili2dbsettings.py +++ b/modelbaker/ilitoppingmaker/ili2dbsettings.py @@ -16,6 +16,8 @@ * * ***************************************************************************/ """ +import configparser +from pathlib import Path class Ili2dbSettings(dict): @@ -105,3 +107,16 @@ def parse_parameters_from_db(self, db_connector): self.parameters["createTidCol"] = True return True + + def parse_parameters_from_ini_file(self, ini_file: str) -> bool: + p = Path(ini_file) + if p.exists(): + config = configparser.ConfigParser() + config.read(ini_file) + if not "CONFIGURATION" in config or not "ch.ehi.ili2db" in config: + return False + + self.parameters = dict(config["ch.ehi.ili2db"]) + return True + + return False diff --git a/modelbaker/utils/ili2db_utils.py b/modelbaker/utils/ili2db_utils.py index b8410d4..786386a 100644 --- a/modelbaker/utils/ili2db_utils.py +++ b/modelbaker/utils/ili2db_utils.py @@ -18,8 +18,12 @@ """ from qgis.PyQt.QtCore import QObject, Qt, pyqtSignal -from ..iliwrapper import ilideleter -from ..iliwrapper.ili2dbconfig import DeleteConfiguration, Ili2DbCommandConfiguration +from ..iliwrapper import ilideleter, ilimetaconfigexporter +from ..iliwrapper.ili2dbconfig import ( + DeleteConfiguration, + ExportMetaConfigConfiguration, + Ili2DbCommandConfiguration, +) from ..iliwrapper.ili2dbutils import JavaNotFoundError from ..utils.qt_utils import OverrideCursor @@ -108,6 +112,43 @@ def delete_dataset( return res, msg + def export_metaconfig( + self, ini_file: str, configuration: Ili2DbCommandConfiguration = None + ): + """ + :param ini_file: Output file + :param configuration: Base Ili2DbCommandConfiguration object + :return: Tuple with boolean result and optional message + """ + metaconfig_exporter = ilimetaconfigexporter.MetaConfigExporter() + metaconfig_exporter.tool = configuration.tool + metaconfig_exporter.configuration = ExportMetaConfigConfiguration(configuration) + metaconfig_exporter.configuration.metaconfigoutputfile = ini_file + + with OverrideCursor(Qt.WaitCursor): + self._connect_ili_executable_signals(metaconfig_exporter) + self._log = "" + + res = True + msg = self.tr("MetaConfig successfully exported to '{}'!").format(ini_file) + try: + if ( + metaconfig_exporter.run() + != ilimetaconfigexporter.MetaConfigExporter.SUCCESS + ): + msg = self.tr( + "An error occurred when exporting the metaconfig from the DB to '{}' (check the QGIS log panel)." + ).format(ini_file) + res = False + self.log_on_error.emit(self._log) + except JavaNotFoundError as e: + msg = e.error_string + res = False + + self._disconnect_ili_executable_signals(metaconfig_exporter) + + return res, msg + def _connect_ili_executable_signals(self, ili_executable): ili_executable.process_started.connect(self.process_started) ili_executable.stderr.connect(self.stderr)