Skip to content

Commit 33cbc96

Browse files
committed
fix: update providers on update of plugin
1 parent e316c72 commit 33cbc96

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

ORStools/ORStoolsPlugin.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def __init__(self, iface: QgisInterface) -> None:
5252
"""
5353
self.dialog = ORStoolsDialog.ORStoolsDialogMain(iface)
5454
self.provider = provider.ORStoolsProvider()
55+
self.settings_keys = ["ENV_VARS", "base_url", "key", "name", "endpoints", "profiles"]
5556

5657
# initialize plugin directory
5758
self.plugin_dir = os.path.dirname(__file__)
@@ -74,10 +75,7 @@ def __init__(self, iface: QgisInterface) -> None:
7475
except TypeError:
7576
pass
7677

77-
try:
78-
configmanager.read_config()["providers"]
79-
except (TypeError, KeyError):
80-
self.add_default_provider_to_settings()
78+
self.update_settings()
8179

8280
def initGui(self) -> None:
8381
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
@@ -90,24 +88,21 @@ def unload(self) -> None:
9088
QgsApplication.processingRegistry().removeProvider(self.provider)
9189
self.dialog.unload()
9290

93-
def add_default_provider_to_settings(self):
94-
s = QgsSettings()
95-
settings = s.value("ORStools/config")
91+
def update_settings(self):
92+
settings = configmanager.read_config()
9693

97-
settings_keys = ["ENV_VARS", "base_url", "key", "name", "endpoints", "profiles"]
94+
if settings is not None and settings != {}:
95+
endpoints = settings.get("endpoints", ENDPOINTS)
96+
profiles = settings.get("profiles", PROFILES)
9897

99-
# Add any new settings here for backwards compatibility
100-
if settings:
10198
changed = False
10299
for i, prov in enumerate(settings["providers"]):
103-
if any([i not in prov for i in settings_keys]):
100+
if any([key not in prov for key in self.settings_keys]):
104101
changed = True
105-
# Add here, like the endpoints
106-
prov["endpoints"] = ENDPOINTS
107-
settings["providers"][i] = prov
108-
prov["profiles"] = PROFILES
102+
prov["endpoints"] = endpoints
103+
prov["profiles"] = profiles
109104
settings["providers"][i] = prov
110105
if changed:
111-
s.setValue("ORStools/config", settings)
106+
configmanager.write_config(settings)
112107
else:
113-
s.setValue("ORStools/config", DEFAULT_SETTINGS)
108+
configmanager.write_config(DEFAULT_SETTINGS)

tests/conftest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import os
22

3-
from qgis.core import QgsSettings
43

54
from ORStools.ORStoolsPlugin import ORStools
65
from tests.utils.utilities import get_qgis_app
6+
from ORStools.utils import configmanager
77

88

99
def pytest_sessionstart(session):
@@ -13,13 +13,13 @@ def pytest_sessionstart(session):
1313
"""
1414
QGISAPP, CANVAS, IFACE, PARENT = get_qgis_app()
1515

16-
ORStools(IFACE).add_default_provider_to_settings()
17-
s = QgsSettings()
18-
data = s.value("ORStools/config")
16+
ORStools(IFACE)
17+
18+
data = configmanager.read_config()
1919

2020
if not os.environ.get("ORS_API_KEY"):
2121
raise ValueError(
2222
"No API key found in environment variables. Please set ORS_API_KEY environment variable to run tests."
2323
)
2424
data["providers"][0]["key"] = os.environ.get("ORS_API_KEY")
25-
s.setValue("ORStools/config", data)
25+
configmanager.write_config(data)

tests/test_gui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,4 +872,4 @@ def test_load_layer_exception_handling(self):
872872
dialog_main.dlg.load_vertices_from_layer("ok")
873873

874874
# Should not crash and list should be empty
875-
self.assertEqual(dialog_main.dlg.routing_fromline_list.count(), 0)
875+
self.assertEqual(dialog_main.dlg.routing_fromline_list.count(), 0)

0 commit comments

Comments
 (0)