|
| 1 | +import os |
| 2 | +from abc import abstractmethod |
| 3 | + |
| 4 | +from qgis.core import ( |
| 5 | + QgsProcessingAlgorithm, |
| 6 | + QgsProcessingOutputBoolean, |
| 7 | + QgsProcessingOutputFile, |
| 8 | + QgsProcessingOutputNumber, |
| 9 | + QgsProcessingOutputString, |
| 10 | + QgsProcessingParameterAuthConfig, |
| 11 | + QgsProcessingParameterBoolean, |
| 12 | + QgsProcessingParameterEnum, |
| 13 | + QgsProcessingParameterFile, |
| 14 | + QgsProcessingParameterNumber, |
| 15 | + QgsProcessingParameterString, |
| 16 | +) |
| 17 | +from qgis.PyQt.QtCore import QCoreApplication, QSettings |
| 18 | +from qgis.PyQt.QtGui import QIcon |
| 19 | + |
| 20 | +from ..iliwrapper.ili2dbconfig import BaseConfiguration |
| 21 | +from ..utils.db_utils import get_authconfig_map, get_service_config |
| 22 | + |
| 23 | + |
| 24 | +class Ili2dbAlgorithm(QgsProcessingAlgorithm): |
| 25 | + def __init__(self): |
| 26 | + super().__init__() |
| 27 | + |
| 28 | + def group(self): |
| 29 | + return self.tr("ili2db") |
| 30 | + |
| 31 | + def groupId(self): |
| 32 | + return "ili2db" |
| 33 | + |
| 34 | + def icon(self): |
| 35 | + return QIcon(os.path.join(os.path.dirname(__file__), "images/interlis.png")) |
| 36 | + |
| 37 | + def tr(self, string): |
| 38 | + return QCoreApplication.translate("Processing", string) |
| 39 | + |
| 40 | + def createInstance(self): |
| 41 | + return self.__class__() |
| 42 | + |
| 43 | + @abstractmethod |
| 44 | + def connection_input_params(self): |
| 45 | + return [] |
| 46 | + |
| 47 | + @abstractmethod |
| 48 | + def connection_output_params(self): |
| 49 | + return [] |
| 50 | + |
| 51 | + @abstractmethod |
| 52 | + def get_db_configuration_from_input(self, parameters, context, configuration): |
| 53 | + return False |
| 54 | + |
| 55 | + @abstractmethod |
| 56 | + def get_output_from_db_configuration(self, configuration): |
| 57 | + return {} |
| 58 | + |
| 59 | + @abstractmethod |
| 60 | + def get_settings_configuration_from_input(self, parameters, context, configuration): |
| 61 | + return |
| 62 | + |
| 63 | + def current_baseconfig(self): |
| 64 | + baseconfig = BaseConfiguration() |
| 65 | + settings = QSettings() |
| 66 | + settings.beginGroup("QgisModelBaker/ili2db") |
| 67 | + baseconfig.restore(settings) |
| 68 | + return baseconfig |
| 69 | + |
| 70 | + |
| 71 | +class Ili2pgAlgorithm(Ili2dbAlgorithm): |
| 72 | + SERVICE = "SERVICE" |
| 73 | + HOST = "HOST" |
| 74 | + DBNAME = "DBNAME" |
| 75 | + PORT = "PORT" |
| 76 | + USER = "USER" |
| 77 | + PASSWORD = "PASSWORD" |
| 78 | + SCHEMA = "SCHEMA" |
| 79 | + SSLMODE = "SSLMODE" |
| 80 | + USESUPERUSER = "USESUPERUSER" |
| 81 | + AUTHCFG = "AUTHCFG" |
| 82 | + |
| 83 | + def __init__(self): |
| 84 | + super().__init__() |
| 85 | + |
| 86 | + def connection_input_params(self): |
| 87 | + params = [] |
| 88 | + |
| 89 | + service_param = QgsProcessingParameterString( |
| 90 | + self.SERVICE, |
| 91 | + self.tr("Service"), |
| 92 | + None, |
| 93 | + optional=True, |
| 94 | + ) |
| 95 | + service_param.setHelp(self.tr("The PostgreSQL service config file.")) |
| 96 | + params.append(service_param) |
| 97 | + |
| 98 | + host_param = QgsProcessingParameterString( |
| 99 | + self.HOST, |
| 100 | + self.tr("Host"), |
| 101 | + defaultValue="localhost", |
| 102 | + optional=True, |
| 103 | + ) |
| 104 | + host_param.setHelp( |
| 105 | + self.tr("The host of the database server. By default is localhost.") |
| 106 | + ) |
| 107 | + params.append(host_param) |
| 108 | + |
| 109 | + port_param = QgsProcessingParameterNumber( |
| 110 | + self.PORT, |
| 111 | + self.tr("Port"), |
| 112 | + type=QgsProcessingParameterNumber.Type.Integer, |
| 113 | + defaultValue=5432, |
| 114 | + optional=True, |
| 115 | + ) |
| 116 | + port_param.setHelp( |
| 117 | + self.tr("The port of the database server. By default is 5432.") |
| 118 | + ) |
| 119 | + params.append(port_param) |
| 120 | + |
| 121 | + dbname_param = QgsProcessingParameterString( |
| 122 | + self.DBNAME, |
| 123 | + self.tr("Database"), |
| 124 | + defaultValue=None, |
| 125 | + optional=True, |
| 126 | + ) |
| 127 | + dbname_param.setHelp(self.tr("The database name. The database should exist.")) |
| 128 | + params.append(dbname_param) |
| 129 | + |
| 130 | + schema_param = QgsProcessingParameterString( |
| 131 | + self.SCHEMA, |
| 132 | + self.tr("Schema"), |
| 133 | + defaultValue=None, |
| 134 | + optional=True, |
| 135 | + ) |
| 136 | + schema_param.setHelp(self.tr("The database schema.")) |
| 137 | + params.append(schema_param) |
| 138 | + |
| 139 | + sslmode_param = QgsProcessingParameterEnum( |
| 140 | + self.SSLMODE, |
| 141 | + self.tr("SSL Mode"), |
| 142 | + ["disable", "allow", "prefer", "require", "verify-ca", "verify-full"], |
| 143 | + defaultValue=None, |
| 144 | + optional=True, |
| 145 | + usesStaticStrings=True, |
| 146 | + ) |
| 147 | + sslmode_param.setHelp(self.tr("The SSL mode if needed.")) |
| 148 | + params.append(sslmode_param) |
| 149 | + user_param = QgsProcessingParameterString( |
| 150 | + self.USER, |
| 151 | + self.tr("User"), |
| 152 | + defaultValue=None, |
| 153 | + optional=True, |
| 154 | + ) |
| 155 | + user_param.setHelp(self.tr("The user to access the database.")) |
| 156 | + params.append(user_param) |
| 157 | + |
| 158 | + password_param = QgsProcessingParameterString( |
| 159 | + self.PASSWORD, |
| 160 | + self.tr("Password"), |
| 161 | + defaultValue=None, |
| 162 | + optional=True, |
| 163 | + ) |
| 164 | + password_param.setHelp(self.tr("The password of the user.")) |
| 165 | + params.append(password_param) |
| 166 | + |
| 167 | + usesuperuser_param = QgsProcessingParameterBoolean( |
| 168 | + self.USESUPERUSER, |
| 169 | + self.tr("Use superuser login from the settings"), |
| 170 | + defaultValue=False, |
| 171 | + ) |
| 172 | + usesuperuser_param.setHelp( |
| 173 | + self.tr("Excecute the task with the super user login from the settings") |
| 174 | + ) |
| 175 | + params.append(usesuperuser_param) |
| 176 | + |
| 177 | + authcfg_param = QgsProcessingParameterAuthConfig( |
| 178 | + self.AUTHCFG, |
| 179 | + self.tr("Authentication"), |
| 180 | + defaultValue=None, |
| 181 | + optional=True, |
| 182 | + ) |
| 183 | + authcfg_param.setHelp( |
| 184 | + self.tr( |
| 185 | + "When choosing a QGIS Authentication you don't need user and password." |
| 186 | + ) |
| 187 | + ) |
| 188 | + params.append(authcfg_param) |
| 189 | + |
| 190 | + return params |
| 191 | + |
| 192 | + def connection_output_params(self): |
| 193 | + params = [] |
| 194 | + |
| 195 | + # outputs for pass through |
| 196 | + params.append(QgsProcessingOutputString(self.SERVICE, self.tr("Service"))) |
| 197 | + params.append(QgsProcessingOutputString(self.HOST, self.tr("Host"))) |
| 198 | + params.append(QgsProcessingOutputString(self.DBNAME, self.tr("Database"))) |
| 199 | + params.append(QgsProcessingOutputNumber(self.PORT, self.tr("Port"))) |
| 200 | + params.append(QgsProcessingOutputString(self.USER, self.tr("User"))) |
| 201 | + params.append(QgsProcessingOutputString(self.PASSWORD, self.tr("Password"))) |
| 202 | + params.append(QgsProcessingOutputString(self.SCHEMA, self.tr("Schema"))) |
| 203 | + params.append(QgsProcessingOutputString(self.SSLMODE, self.tr("SSL Mode"))) |
| 204 | + params.append( |
| 205 | + QgsProcessingOutputBoolean(self.USESUPERUSER, self.tr("Use Superuser")) |
| 206 | + ) |
| 207 | + params.append( |
| 208 | + QgsProcessingOutputString(self.AUTHCFG, self.tr("Authentication")) |
| 209 | + ) |
| 210 | + return params |
| 211 | + |
| 212 | + def get_db_configuration_from_input(self, parameters, context, configuration): |
| 213 | + """ |
| 214 | + Returns true if mandatory parameters are given |
| 215 | + """ |
| 216 | + |
| 217 | + configuration.dbservice = self.parameterAsString( |
| 218 | + parameters, self.SERVICE, context |
| 219 | + ) |
| 220 | + service_map, _ = get_service_config(configuration.dbservice) |
| 221 | + |
| 222 | + if self.parameterAsString(parameters, self.AUTHCFG, context): |
| 223 | + configuration.dbauthid = self.parameterAsString( |
| 224 | + parameters, self.AUTHCFG, context |
| 225 | + ) # needed for passthroug |
| 226 | + authconfig_map = get_authconfig_map(configuration.dbauthid) |
| 227 | + configuration.dbusr = authconfig_map.get("username") |
| 228 | + configuration.dbpwd = authconfig_map.get("password") |
| 229 | + else: |
| 230 | + configuration.dbusr = self.parameterAsString( |
| 231 | + parameters, self.USER, context |
| 232 | + ) or service_map.get("user") |
| 233 | + configuration.dbpwd = self.parameterAsString( |
| 234 | + parameters, self.PASSWORD, context |
| 235 | + ) or service_map.get("password") |
| 236 | + configuration.dbhost = self.parameterAsString( |
| 237 | + parameters, self.HOST, context |
| 238 | + ) or service_map.get("host") |
| 239 | + configuration.dbport = str( |
| 240 | + self.parameterAsInt(parameters, self.PORT, context) |
| 241 | + ) or service_map.get("port") |
| 242 | + configuration.database = self.parameterAsString( |
| 243 | + parameters, self.DBNAME, context |
| 244 | + ) or service_map.get("dbname") |
| 245 | + configuration.dbschema = self.parameterAsString( |
| 246 | + parameters, self.SCHEMA, context |
| 247 | + ) |
| 248 | + configuration.sslmode = self.parameterAsEnum(parameters, self.SSLMODE, context) |
| 249 | + configuration.db_use_super_login = self.parameterAsBoolean( |
| 250 | + parameters, self.USESUPERUSER, context |
| 251 | + ) |
| 252 | + valid = bool( |
| 253 | + configuration.dbhost and configuration.database and configuration.dbschema |
| 254 | + ) |
| 255 | + return valid |
| 256 | + |
| 257 | + def get_output_from_db_configuration(self, configuration): |
| 258 | + """ |
| 259 | + Returns an output map |
| 260 | + """ |
| 261 | + |
| 262 | + output_map = { |
| 263 | + self.SERVICE: configuration.dbservice, |
| 264 | + self.HOST: configuration.dbhost, |
| 265 | + self.DBNAME: configuration.database, |
| 266 | + self.PORT: configuration.dbport, |
| 267 | + self.USER: configuration.dbusr, |
| 268 | + self.PASSWORD: configuration.dbpwd, |
| 269 | + self.SCHEMA: configuration.dbschema, |
| 270 | + self.SSLMODE: configuration.sslmode, |
| 271 | + self.USESUPERUSER: configuration.db_use_super_login, |
| 272 | + self.AUTHCFG: configuration.dbauthid, |
| 273 | + } |
| 274 | + return output_map |
| 275 | + |
| 276 | + |
| 277 | +class Ili2gpkgAlgorithm(Ili2dbAlgorithm): |
| 278 | + |
| 279 | + DBPATH = "DBPATH" |
| 280 | + |
| 281 | + def __init__(self): |
| 282 | + super().__init__() |
| 283 | + |
| 284 | + def connection_input_params(self): |
| 285 | + params = [] |
| 286 | + |
| 287 | + dbpath_param = QgsProcessingParameterFile( |
| 288 | + self.DBPATH, |
| 289 | + self.tr("Database File Path"), |
| 290 | + defaultValue=None, |
| 291 | + optional=True, |
| 292 | + ) |
| 293 | + dbpath_param.setHelp(self.tr("The database file path (*.gpkg).")) |
| 294 | + params.append(dbpath_param) |
| 295 | + |
| 296 | + return params |
| 297 | + |
| 298 | + def connection_output_params(self): |
| 299 | + params = [] |
| 300 | + |
| 301 | + params.append( |
| 302 | + QgsProcessingOutputFile(self.DBPATH, self.tr("Database File Path")) |
| 303 | + ) |
| 304 | + |
| 305 | + return params |
| 306 | + |
| 307 | + def get_db_configuration_from_input(self, parameters, context, configuration): |
| 308 | + """ |
| 309 | + Returns true if mandatory parameters are given |
| 310 | + """ |
| 311 | + valid = False |
| 312 | + dbpath = self.parameterAsFile(parameters, self.DBPATH, context) |
| 313 | + if dbpath and dbpath.endswith(".gpkg"): |
| 314 | + configuration.dbfile = dbpath |
| 315 | + valid = True |
| 316 | + return valid |
| 317 | + |
| 318 | + def get_output_from_db_configuration(self, configuration): |
| 319 | + """ |
| 320 | + Returns an output map |
| 321 | + """ |
| 322 | + |
| 323 | + output_map = {self.DBPATH: configuration.dbfile} |
| 324 | + return output_map |
0 commit comments