-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtask.py
More file actions
84 lines (70 loc) · 2.51 KB
/
task.py
File metadata and controls
84 lines (70 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
"""
GEOE3 Plugin
Author: Your Name
Copyright: 2024, Your Organization
License: GPL-3.0-only
This file is part of the GEOE3 QGIS Plugin. It is available under the terms of the GNU General Public License v3.0 only.
See the LICENSE file in the project root for more information.
"""
import os
from qgis.PyQt.QtCore import pyqtSignal
from qgis.core import Qgis, QgsMessageLog, QgsTask
class GeoE3Task(QgsTask):
"""
Custom task for running GEOE3 plugin operations as a background job.
"""
finished = pyqtSignal(bool)
error = pyqtSignal()
def __init__(self, description: str, node: dict):
"""🏗️ Initialize the instance.
Args:
description (str): Description of the task.
node (dict): Node containing task data.
"""
super().__init__(description)
self.node = node
def run(self) -> bool:
"""
Executes the task. This is the main work method that performs the background operation.
Returns:
bool: True if task completed successfully, False otherwise.
"""
try:
output_path = self.node["output_path"]
if self.node.get("processed", False) and os.path.exists(output_path):
QgsMessageLog.logMessage(
f"{self.node['name']} already processed",
"GeoE3",
Qgis.Info,
)
self.finished.emit(True)
return True
# Simulate processing
self.process_node()
self.node["processed"] = True
QgsMessageLog.logMessage(f"Processed {self.node['name']}", "GeoE3", Qgis.Info)
self.finished.emit(True)
return True
except Exception as e:
QgsMessageLog.logMessage(
f"Task failed for {self.node['name']}: {str(e)}",
"GeoE3",
Qgis.Critical,
)
self.error.emit()
return False
def process_node(self) -> None:
"""
Simulates the processing of the node.
"""
output_path = self.node["output_path"]
os.makedirs(os.path.dirname(output_path), exist_ok=True)
with open(output_path, "w") as f:
f.write(f"Processed output for {self.node['name']}")
def cancel(self) -> None:
"""
Handles task cancellation.
"""
QgsMessageLog.logMessage(f"{self.node['name']} task was cancelled", "GEOE3", Qgis.Info)
super().cancel()