Skip to content

Commit

Permalink
feat: use qgs settings instead of config yml (#249)
Browse files Browse the repository at this point in the history
Co-authored-by: Jakob Schnell <[email protected]>
  • Loading branch information
merydian and koebi authored Aug 30, 2024
1 parent 0131012 commit e6f17f8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ RELEASING:
14. Create new release in GitHub with tag version and release title of `vX.X.X`
-->

## Unreleased

### Changed
- Use QgsSettings instead of config.yml file to avoid deletion of providers on update ([#108](https://github.com/GIScience/orstools-qgis-plugin/issues/108))

## [1.8.4] - 2024-07-29

### Fixed
Expand Down
22 changes: 22 additions & 0 deletions ORStools/ORStoolsPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def __init__(self, iface: QgisInterface) -> None:
except TypeError:
pass

self.add_default_provider_to_settings()

def initGui(self) -> None:
"""Create the menu entries and toolbar icons inside the QGIS GUI."""

Expand All @@ -83,3 +85,23 @@ def unload(self) -> None:
"""remove menu entry and toolbar icons"""
QgsApplication.processingRegistry().removeProvider(self.provider)
self.dialog.unload()

def add_default_provider_to_settings(self):
s = QgsSettings()
settings = s.value("ORStools/config")
if not settings:
def_settings = {
"providers": [
{
"ENV_VARS": {
"ORS_QUOTA": "X-Ratelimit-Limit",
"ORS_REMAINING": "X-Ratelimit-Remaining",
},
"base_url": "https://api.openrouteservice.org",
"key": "",
"name": "openrouteservice",
"timeout": 60,
}
]
}
s.setValue("ORStools/config", def_settings)
8 changes: 0 additions & 8 deletions ORStools/config.yml

This file was deleted.

14 changes: 6 additions & 8 deletions ORStools/utils/configmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@

import os

import yaml

from ORStools import CONFIG_PATH
from qgis.core import QgsSettings


def read_config() -> dict:
Expand All @@ -41,10 +39,10 @@ def read_config() -> dict:
:returns: Parsed settings dictionary.
:rtype: dict
"""
with open(CONFIG_PATH) as f:
doc = yaml.safe_load(f)
s = QgsSettings()
config = s.value("ORStools/config")

return doc
return config


def write_config(new_config: dict) -> None:
Expand All @@ -54,8 +52,8 @@ def write_config(new_config: dict) -> None:
:param new_config: new provider settings after altering in dialog.
:type new_config: dict
"""
with open(CONFIG_PATH, "w") as f:
yaml.safe_dump(new_config, f)
s = QgsSettings()
s.setValue("ORStools/config", new_config)


def write_env_var(key: str, value: str) -> None:
Expand Down
22 changes: 12 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import os
import yaml

from qgis.core import QgsSettings

from ORStools.ORStoolsPlugin import ORStools
from ORStools.utils.configmanager import read_config
from tests.utils.utilities import get_qgis_app

with open("ORStools/config.yml", "r+") as file:
data = yaml.safe_load(file)
QGISAPP, CANVAS, IFACE, PARENT = get_qgis_app()

ORStools(IFACE).add_default_provider_to_settings()
s = QgsSettings()
data = s.value("ORStools/config")

def pytest_sessionstart(session):
"""
Expand All @@ -14,8 +19,7 @@ def pytest_sessionstart(session):
"""
if data["providers"][0]["key"] == "":
data["providers"][0]["key"] = os.environ.get("ORS_API_KEY")
with open("ORStools/config.yml", "w") as file:
yaml.dump(data, file)
s.setValue("ORStools/config", data)
else:
raise ValueError("API key is not empty.")

Expand All @@ -25,10 +29,8 @@ def pytest_sessionfinish(session, exitstatus):
Called after whole test run finished, right before
returning the exit status to the system.
"""
with open("ORStools/config.yml", "w") as file:
if not data["providers"][0]["key"] == "":
data['providers'][0]['key'] = '' # fmt: skip
yaml.dump(data, file)

if not data["providers"][0]["key"] == "":
data['providers'][0]['key'] = '' # fmt: skip
s.setValue("ORStools/config", data)
config = read_config()
assert config["providers"][0]["key"] == '' # fmt: skip
2 changes: 1 addition & 1 deletion tests/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ def test_ORStoolsDialog(self):
dlg.line_tool.canvasDoubleClickEvent(map_dclick)

self.assertTrue(dlg.isVisible())
self.assertAlmostEqual(
self.assertEqual(
dlg.routing_fromline_list.item(0).text(), "Point 0: -0.187575, 56.516620"
)

0 comments on commit e6f17f8

Please sign in to comment.