Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catch errors when pgservice not found. #104

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 40 additions & 1 deletion modelbaker/utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.",
)
Loading