Skip to content

Commit

Permalink
Merge pull request #104 from opengisch/fix_service_file
Browse files Browse the repository at this point in the history
catch errors when pgservice not found.
  • Loading branch information
signedav authored Jul 11, 2024
2 parents 76360ad + ee1927f commit 3559d7c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
38 changes: 13 additions & 25 deletions modelbaker/db_factory/pg_command_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)]

Expand Down
35 changes: 34 additions & 1 deletion modelbaker/utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ 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)
service_map, _ = get_service_config(configuration.dbservice)
if layer_source.authConfigId():
configuration.dbauthid = layer_source.authConfigId()
authconfig_map = get_authconfig_map(configuration.dbauthid)
Expand Down Expand Up @@ -152,3 +152,36 @@ 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 names of the available services in the PostgreSQL connection service file.
"""
try:
return pgserviceparser.service_names(), None
except pgserviceparser.ServiceFileNotFound as sfe:
return (
[],
f"Services cannot be retrieved, since no service file {str(sfe)} available.",
)


def get_service_config(servicename: str):
"""
Provides the service configuration of a given service from the PostgreSQL connection service file.
"""
if not servicename:
return {}, f"No servicename given."
try:
return pgserviceparser.service_config(servicename), None
except pgserviceparser.ServiceFileNotFound as sfe:
return (
{},
f"The service {servicename} cannot be found, since no service file {str(sfe)} available.",
)
except pgserviceparser.ServiceNotFound:
return (
{},
f"The service {servicename} cannot be found in the service file.",
)

0 comments on commit 3559d7c

Please sign in to comment.