Skip to content

Commit

Permalink
WIP set background tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
hypsug0 committed Mar 18, 2024
1 parent 4b28ed2 commit 1e5b224
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
46 changes: 46 additions & 0 deletions plugin_qgis_lpo/commons/tasking_refresh_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from functools import partial

Check warning on line 1 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L1

Added line #L1 was not covered by tests

from qgis.core import (

Check warning on line 3 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L3

Added line #L3 was not covered by tests
QgsApplication,
QgsMessageLog,
QgsProcessingAlgRunnerTask,
QgsProcessingContext,
QgsProcessingFeedback,
QgsProject,
QgsTaskManager,
)

from ..toolbelt.log_handler import PlgLogger

Check warning on line 13 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L13

Added line #L13 was not covered by tests

MESSAGE_CATEGORY = "AlgRunnerTask"

Check warning on line 15 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L15

Added line #L15 was not covered by tests

logger = PlgLogger().log

Check warning on line 17 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L17

Added line #L17 was not covered by tests


def task_finished(_context, successful, _results):

Check warning on line 20 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L20

Added line #L20 was not covered by tests
if not successful:
logger.log(

Check warning on line 22 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L22

Added line #L22 was not covered by tests
message=str("Task finished unsucessfully"),
log_level=2,
push=True,
duration=2,
)
else:
logger.log(

Check warning on line 29 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L29

Added line #L29 was not covered by tests
message=str("Task finished successfully"),
log_level=0,
push=True,
duration=2,
)


def bg_task(algorithm: str, params: dict):
alg = QgsApplication.processingRegistry().algorithmById(algorithm)

Check warning on line 38 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L37-L38

Added lines #L37 - L38 were not covered by tests

context = QgsProcessingContext()
feedback = QgsProcessingFeedback()

Check warning on line 41 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L40-L41

Added lines #L40 - L41 were not covered by tests

task = QgsProcessingAlgRunnerTask(alg, params, context, feedback)

Check warning on line 43 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L43

Added line #L43 was not covered by tests

task.executed.connect(partial(task_finished, context))
return task

Check warning on line 46 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L45-L46

Added lines #L45 - L46 were not covered by tests
20 changes: 16 additions & 4 deletions plugin_qgis_lpo/plugin_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
__title__,
__uri_homepage__,
)
from plugin_qgis_lpo.commons.tasking_refresh_data import bg_task

Check warning on line 35 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L35

Added line #L35 was not covered by tests
from plugin_qgis_lpo.gui.dlg_settings import PlgOptionsFactory
from plugin_qgis_lpo.gui.menu_tools import MenuTools
from plugin_qgis_lpo.processing.provider import QgisLpoProvider
Expand Down Expand Up @@ -63,6 +64,7 @@ def __init__(self, iface: QgisInterface):
self.iface: QgisInterface = iface
self.tools_menu: Optional[QMenu] = None
self.main_menu: Optional[QMenu] = None
self.tm = QgsApplication.taskManager()

Check warning on line 67 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L67

Added line #L67 was not covered by tests
# translation
# initialize the locale
self.locale: str = QgsSettings().value("locale/userLocale", QLocale().name())[
Expand Down Expand Up @@ -169,8 +171,14 @@ def initGui(self):
"Attention",
"La carte par espèces est accessible via la barre d'outils d'Extensions",
)

self.populateSettings()
# -- Post UI initialization
self.log(

Check warning on line 175 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L175

Added line #L175 was not covered by tests
message="Plugin QGIS LpO loaded",
log_level=0,
push=True,
duration=60,
)
self.iface.initializationCompleted.connect(self.populateSettings)

Check warning on line 181 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L181

Added line #L181 was not covered by tests

def populateSettings(self):
try:
Expand All @@ -184,8 +192,10 @@ def populateSettings(self):
push=True,
duration=60,
)
processing.run(
"plugin_qgis_lpo:RefreshData", {"DATABASE": "geonature_lpo"}
self.tm.addTask(

Check warning on line 195 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L195

Added line #L195 was not covered by tests
bg_task(
"plugin_qgis_lpo:RefreshData", {"DATABASE": "geonature_lpo"}
)
)
self.log(
message=f"Loading GeoNature terminated",
Expand All @@ -202,6 +212,7 @@ def populateSettings(self):
raise QgsProcessingException(
f"Could not retrieve connection details : {str(exc)}"
) from exc # processing.run("plugin_qgis_lpo:RefreshData", {"DATABASE": "geonature_lpo"})
return

Check warning on line 215 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L215

Added line #L215 was not covered by tests

def runEspeces(self): # noqa N802
connection_name = get_connection_name()
Expand Down Expand Up @@ -256,6 +267,7 @@ def run(self):
:raises Exception: if there is no item in the feed
"""
try:
self.populateSettings()

Check warning on line 270 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L270

Added line #L270 was not covered by tests
self.log(
message=self.tr("Everything ran OK."),
log_level=3,
Expand Down

0 comments on commit 1e5b224

Please sign in to comment.