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

101 webconnections not being added #115

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
2 changes: 1 addition & 1 deletion canflood/_pars/WebConnections.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ url=http://preview.grid.unep.ch/geoserver/wcs?bbox=-180,-89,180,84&styles=&versi

[NPRI]
group=connections-arcgisfeatureserver\ECCC_NationalPollutantReleaseInventory_NPRI
url=https://maps-cartes.ec.gc.ca/arcgis/rest/services/NPRI_INRP/NPRI_INRP/MapServer
url=https://maps-cartes.ec.gc.ca/arcgis/rest/services/STB_DGST/NPRI/MapServer
cefect marked this conversation as resolved.
Show resolved Hide resolved

[NHSL]
group=connections-arcgisfeatureserver\NRCan_NationalHumanSettlementLayer_NHSL
Expand Down
90 changes: 62 additions & 28 deletions canflood/misc/webConnections.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#python standards
import os, logging, copy

from configparser import ConfigParser, RawConfigParser
from configparser import ConfigParser
import numpy as np


Expand Down Expand Up @@ -43,8 +43,12 @@ class WebConnectAction(QMenuAction):
icon_location = 'menu'

def launch(self):

cefect marked this conversation as resolved.
Show resolved Hide resolved
base_dir = os.path.dirname(os.path.dirname(__file__))
newSettings_fp1=os.path.join(base_dir, r'_pars\WebConnections.ini')

#=======================================================================
# defai;ts
# details
ceftanveer marked this conversation as resolved.
Show resolved Hide resolved
#=======================================================================
log = self.logger.getChild('l')

Expand All @@ -55,7 +59,7 @@ def launch(self):
kwargs = {attn:getattr(self, attn) for attn in self.inherit_fieldNames}
with WebConnect(**kwargs) as wrkr:
newCons_d = wrkr.addAll()

wrkr.read_connections(newSettings_fp1)
#=======================================================================
# wrap
#=======================================================================
Expand All @@ -80,11 +84,7 @@ def __init__(self,

**kwargs):





super().__init__(**kwargs) #initilzie teh baseclass
super().__init__(**kwargs) #initilzie the baseclass

#setup

Expand Down Expand Up @@ -143,7 +143,60 @@ def retrieve_fromFile(self, #pull parameters from CanFlood's file

return newCons_d

def read_connections(self, newSettings_fp = None):
cefect marked this conversation as resolved.
Show resolved Hide resolved
config = ConfigParser()
config.read(newSettings_fp)
for section in config.sections():
name = section
url = config.get(section, 'url', fallback="")
group = config.get(section, 'group', fallback="")

if "connections-arcgisfeatureserver" in group:
self.add_arcgis_rest_connection(name, url)
elif "connections-wcs" in group:
self.add_wcs_connection(name, url)
elif "connections-wms" in group:
self.add_wms_connection(name, url)
else:
raise ValueError(f"Unknown connection type for '{name}': {url}. Group '{group}' is not recognized.")

def add_arcgis_rest_connection(self, name, url):
cefect marked this conversation as resolved.
Show resolved Hide resolved
settings = QgsSettings(self.qini_fp, QSettings.IniFormat)
base_key = f"connections/arcgisfeatureserver/items/{name}"
settings.setValue(f"{base_key}/url", url)
settings.setValue(f"{base_key}/username", "")
settings.setValue(f"{base_key}/password", "")
settings.setValue(f"{base_key}/authcfg", "")
settings.setValue(f"{base_key}/http-header", "@Variant(\0\0\0\b\0\0\0\0)")
settings.setValue("connections/arcgisfeatureserver/selected", name)

settings.sync()

def add_wcs_connection(self, name, url):
cefect marked this conversation as resolved.
Show resolved Hide resolved
"""Adds a Web Coverage Service (WCS) connection to QGIS."""
settings = QgsSettings(self.qini_fp, QSettings.IniFormat)
base_key = f"connections/ows/items/wcs/connections/items/{name}"

settings.setValue(f"{base_key}/url", url)
settings.setValue(f"{base_key}/username", "")
settings.setValue(f"{base_key}/password", "")
settings.setValue(f"{base_key}/authcfg", "")

settings.sync()


def add_wms_connection(self, name, url):
cefect marked this conversation as resolved.
Show resolved Hide resolved
"""Adds a WMS connection to QGIS settings."""
settings = QgsSettings(self.qini_fp, QSettings.IniFormat)
base_key = f"connections/ows/items/wms/connections/items/{name}"

settings.setValue(f"{base_key}/url", url)
settings.setValue(f"{base_key}/username", "")
settings.setValue(f"{base_key}/password", "")
settings.setValue(f"{base_key}/authcfg", "")

settings.sync()

def addAll(self, #add all connections
qini_fp = None, #users settings path
newCons_d = None, #connections to load
Expand All @@ -154,15 +207,13 @@ def addAll(self, #add all connections
log = self.logger.getChild('addAll')
if newCons_d is None: newCons_d = self.newCons_d
if qini_fp is None: qini_fp = self.qini_fp

log.debug('addAll on %i'%len(newCons_d))

#=======================================================================
# initilize settings
#=======================================================================
assert os.path.exists(qini_fp), 'bad settings filepath: %s'%qini_fp
usets = QgsSettings(qini_fp, QSettings.IniFormat)

usets = QSettings(qini_fp, QSettings.IniFormat)
#navigate to group1
"""all connectins are in the qgis group"""
usets.beginGroup('qgis')
Expand Down Expand Up @@ -329,21 +380,4 @@ def checkSettings(self,


return result, msg







if __name__ =="__main__":


wrkr = WebConnect(
newSettings_fp = r'C:\LS\03_TOOLS\CanFlood\_git\canflood\_pars\WebConnections1.ini',
qini_fp = r'C:\Users\cefect\AppData\Roaming\QGIS\QGIS3\profiles\dev\QGIS\QGIS3.ini') #setup worker


wrkr.addAll() #add everything


2 changes: 1 addition & 1 deletion canflood/model/modcom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,7 @@ def _par_hndl_chk(self,
"""

if logger is None: logger=self.logger
if absolute_fp= None: absolute_fp=self.aboslute_fp
if absolute_fp is None: absolute_fp=self.aboslute_fp
log = logger.getChild('par_hndl_chk')

#=======================================================================
Expand Down
88 changes: 88 additions & 0 deletions tests2/Misct/test_webconnections.py
ceftanveer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import pytest
import configparser
import os
from misc.webConnections import WebConnect
from PyQt5.QtCore import QSettings

@pytest.fixture(scope="function")
def web_connect(tmpdir):
"""Fixture to provide an instance of WebConnect with a temporary QGIS3.ini file."""
qini_fp = tmpdir.join("QGIS3.ini")
qini_fp.write("[connections]\n")

web_connect = WebConnect(qini_fp=str(qini_fp))

settings = QSettings(str(qini_fp), QSettings.IniFormat)
settings.clear()
settings.sync()

yield web_connect

@pytest.fixture(scope="function")
def config_file(tmpdir, request):
"""Fixture to create a temporary config file with the specified content."""
test_cases = {
"valid_wms_connection": {
"config_content": {
"AutomaticallyExtractedBuildings": {
"group": "connections-wms\\NRCan_AutomaticallyExtractedBuildings",
"url": "https://maps.geogratis.gc.ca/wms/automatic_extraction_building_footprint_en?request=getcapabilities&service=wms&layers=automatic_extraction_building_footprint_en&version=1.3.0&legend_format=image/png&feature_info_type=text/html"
}
},
"expected_key": "connections/ows/items/wms/connections/items/AutomaticallyExtractedBuildings/url"
},
"valid_wcs_connection": {
"config_content": {
"GAR15": {
"group": "connections-wcs\\UNISDR_GAR15_GlobalRiskAssessment",
"url": "http://preview.grid.unep.ch/geoserver/wcs?bbox=-180,-89,180,84&styles=&version=1.0.0&coverage=GAR2015:flood_hazard_1000_yrp&width=640&height=309&crs=EPSG:4326"
}
},
"expected_key": "connections/ows/items/wcs/connections/items/GAR15/url"
},
"valid_arcgis_connection": {
"config_content": {
"NPRI": {
"group": "connections-arcgisfeatureserver\\ECCC_NationalPollutantReleaseInventory_NPRI",
"url": "https://maps-cartes.ec.gc.ca/arcgis/rest/services/STB_DGST/NPRI/MapServer"
}
},
"expected_key": "connections/arcgisfeatureserver/items/NPRI/url"
}
}

config = configparser.ConfigParser()
config.read_dict(test_cases[request.param]["config_content"])
config_file_path = tmpdir.join("test_config.ini")
with open(config_file_path, 'w') as configfile:
config.write(configfile)

return str(config_file_path), test_cases[request.param]["expected_key"]

@pytest.mark.parametrize(
"config_file, expected_key",
[
("valid_wms_connection", "connections/ows/items/wms/connections/items/AutomaticallyExtractedBuildings/url"),
("valid_wcs_connection", "connections/ows/items/wcs/connections/items/GAR15/url"),
("valid_arcgis_connection", "connections/arcgisfeatureserver/items/NPRI/url")
],
indirect=["config_file"]
)
def test_read_connections(web_connect, config_file, expected_key):
"""Test the read_connections function and check expected QGIS3.ini structure."""
config_path, expected_key = config_file
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use explicit parameters. i.e., I expect to see 'config_path' in the pytest parameters if this is the parameter you are using. i.e., don't use a nested dictionary like this.

Also, what is going on with 'expected_key' here? It looks like you define it twice?

Also, why not just use the uri info in .\canflood\_pars\WebConnections.ini? That would save you having to write out all these url strings AND provide a test on the .ini file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored the code and used explicit parameters. and removed the redundancy of expected_key.

@cefect just tried to keep the test separate from the script. Do you suggest using already present WebConnections.ini ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean up config_content and config_path. make sure all tests are passing or explain w hy not


# Run the connection update
web_connect.read_connections(config_path)

# Force QSettings to write changes immediately
settings = QSettings(web_connect.qini_fp, QSettings.IniFormat)
settings.sync() # Ensure settings are written to disk

qini_fp = web_connect.qini_fp
with open(qini_fp, "r") as f:
ini_content = f.read()
ceftanveer marked this conversation as resolved.
Show resolved Hide resolved

all_keys = settings.allKeys()

assert expected_key in all_keys, f"Expected key '{expected_key}' was not found in the updated QGIS3.ini file"