From b6833442afaa02e466daee92f0a2a57d72bf6687 Mon Sep 17 00:00:00 2001 From: signedav Date: Thu, 11 Jul 2024 09:53:46 +0200 Subject: [PATCH 1/3] catch errors when pgservice not found. Fixes https://github.com/opengisch/QgisModelBaker/issues/944 --- .../db_factory/pg_command_config_manager.py | 38 ++++++----------- modelbaker/utils/db_utils.py | 41 ++++++++++++++++++- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/modelbaker/db_factory/pg_command_config_manager.py b/modelbaker/db_factory/pg_command_config_manager.py index 660fd39..99d61dc 100644 --- a/modelbaker/db_factory/pg_command_config_manager.py +++ b/modelbaker/db_factory/pg_command_config_manager.py @@ -20,7 +20,7 @@ from qgis.PyQt.QtCore import QSettings from ..iliwrapper.ili2dbconfig import Ili2DbCommandConfiguration -from ..libs import pgserviceparser +from ..utils import db_utils from .db_command_config_manager import DbCommandConfigManager @@ -63,50 +63,38 @@ def get_uri(self, su: bool = False, qgis: bool = False) -> str: uri += ["service='{}'".format(self.configuration.dbservice)] # only set the params when they are not available in the service - if not pgserviceparser.service_config(self.configuration.dbservice).get( - "sslmode", None - ): + service_config, _ = db_utils.get_service_config(self.configuration.dbservice) + if not service_config or not service_config.get("sslmode", None): if self.configuration.sslmode: uri += ["sslmode='{}'".format(self.configuration.sslmode)] - if not pgserviceparser.service_config(self.configuration.dbservice).get( - "host", None - ): + if not service_config or not service_config.get("host", None): uri += ["host={}".format(self.configuration.dbhost)] - if not pgserviceparser.service_config(self.configuration.dbservice).get( - "port", None - ): + if not service_config or not service_config.get("port", None): if self.configuration.dbport: uri += ["port={}".format(self.configuration.dbport)] - if not pgserviceparser.service_config(self.configuration.dbservice).get( - "dbname", None - ): + if not service_config or not service_config.get("dbname", None): uri += ["dbname='{}'".format(self.configuration.database)] # only provide authcfg to the uri when it's needed for QGIS specific things if ( qgis and self.configuration.dbauthid - and not ( - pgserviceparser.service_config(self.configuration.dbservice).get( - "user", None - ) - and pgserviceparser.service_config(self.configuration.dbservice).get( - "password", None + and ( + not service_config + or not ( + service_config.get("user", None) + and service_config.get("password", None) ) ) ): uri += ["authcfg={}".format(self.configuration.dbauthid)] else: - if not pgserviceparser.service_config(self.configuration.dbservice).get( - "user", None - ): + if not service_config or not service_config.get("user", None): uri += ["user={}".format(self.configuration.dbusr)] - if not pgserviceparser.service_config(self.configuration.dbservice).get( - "password", None - ): + if not service_config or not service_config.get("password", None): if self.configuration.dbpwd: uri += ["password={}".format(self.configuration.dbpwd)] diff --git a/modelbaker/utils/db_utils.py b/modelbaker/utils/db_utils.py index 8251774..6290576 100644 --- a/modelbaker/utils/db_utils.py +++ b/modelbaker/utils/db_utils.py @@ -74,7 +74,8 @@ def get_configuration_from_sourceprovider(provider, configuration): layer_source = QgsDataSourceUri(provider.dataSourceUri()) mode = DbIliMode.pg configuration.dbservice = layer_source.service() - service_map = pgserviceparser.service_config(configuration.dbservice) + print("here") + service_map, _ = get_service_config(configuration.dbservice) if layer_source.authConfigId(): configuration.dbauthid = layer_source.authConfigId() authconfig_map = get_authconfig_map(configuration.dbauthid) @@ -152,3 +153,41 @@ def db_ili_version(configuration): db_connector = get_db_connector(configuration) if db_connector: return db_connector.ili_version() + + +def get_service_names(): + """ + Provides the available service_names if + """ + try: + return pgserviceparser.service_names(), None + except pgserviceparser.ServiceFileNotFound as sfe: + return ( + [], + f"The last used service {servicename} cannot be found, since no service file {str(sfe)} available anymore.", + ) + except pgserviceparser.ServiceNotFound: + return ( + [], + f"The last used service {servicename} cannot be found in the service file.", + ) + + +def get_service_config(servicename): + """ + Provides the available service_names if + """ + if not servicename: + return {}, f"No servicename given." + try: + return pgserviceparser.service_config(servicename), None + except pgserviceparser.ServiceFileNotFound as sfe: + return ( + {}, + f"The last used service {servicename} cannot be found, since no service file {str(sfe)} available anymore.", + ) + except pgserviceparser.ServiceNotFound: + return ( + {}, + f"The last used service {servicename} cannot be found in the service file.", + ) From fd08de8b5822b751f9a8484bff371094d9996820 Mon Sep 17 00:00:00 2001 From: signedav Date: Thu, 11 Jul 2024 21:32:25 +0200 Subject: [PATCH 2/3] Comments etc. --- modelbaker/utils/db_utils.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/modelbaker/utils/db_utils.py b/modelbaker/utils/db_utils.py index 6290576..6fe3a24 100644 --- a/modelbaker/utils/db_utils.py +++ b/modelbaker/utils/db_utils.py @@ -74,7 +74,6 @@ def get_configuration_from_sourceprovider(provider, configuration): layer_source = QgsDataSourceUri(provider.dataSourceUri()) mode = DbIliMode.pg configuration.dbservice = layer_source.service() - print("here") service_map, _ = get_service_config(configuration.dbservice) if layer_source.authConfigId(): configuration.dbauthid = layer_source.authConfigId() @@ -157,25 +156,20 @@ def db_ili_version(configuration): def get_service_names(): """ - Provides the available service_names if + Provides the names of the available services in the PostgreSQL connection service file. """ try: return pgserviceparser.service_names(), None except pgserviceparser.ServiceFileNotFound as sfe: return ( [], - f"The last used service {servicename} cannot be found, since no service file {str(sfe)} available anymore.", - ) - except pgserviceparser.ServiceNotFound: - return ( - [], - f"The last used service {servicename} cannot be found in the service file.", + f"Services cannot be retrieved, since no service file {str(sfe)} available.", ) -def get_service_config(servicename): +def get_service_config(service_name: str): """ - Provides the available service_names if + Provides the service configuration of a given service from the PostgreSQL connection service file. """ if not servicename: return {}, f"No servicename given." @@ -184,10 +178,10 @@ def get_service_config(servicename): except pgserviceparser.ServiceFileNotFound as sfe: return ( {}, - f"The last used service {servicename} cannot be found, since no service file {str(sfe)} available anymore.", + f"The service {servicename} cannot be found, since no service file {str(sfe)} available.", ) except pgserviceparser.ServiceNotFound: return ( {}, - f"The last used service {servicename} cannot be found in the service file.", + f"The service {servicename} cannot be found in the service file.", ) From ee1927f65cea732c01bff86cd4397ea34edf004f Mon Sep 17 00:00:00 2001 From: signedav Date: Thu, 11 Jul 2024 21:54:52 +0200 Subject: [PATCH 3/3] typo --- modelbaker/utils/db_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modelbaker/utils/db_utils.py b/modelbaker/utils/db_utils.py index 6fe3a24..aba8eee 100644 --- a/modelbaker/utils/db_utils.py +++ b/modelbaker/utils/db_utils.py @@ -167,7 +167,7 @@ def get_service_names(): ) -def get_service_config(service_name: str): +def get_service_config(servicename: str): """ Provides the service configuration of a given service from the PostgreSQL connection service file. """