forked from ernoma/assesclimateimpact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ykr_tool_tasks.py
42 lines (38 loc) · 1.31 KB
/
ykr_tool_tasks.py
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
from qgis.core import (QgsTask, QgsMessageLog, Qgis)
from .createdbconnection import createDbConnection
class QueryTask(QgsTask):
def __init__(self, connParams, queries):
super().__init__('Suoritetaan laskentaa', QgsTask.CanCancel)
self.exception = None
self.conn = None
self.queries = queries
try:
self.conn = createDbConnection(connParams)
self.cur = self.conn.cursor()
except Exception as e:
self.exception = e
def run(self):
if self.exception:
return False
i = 0
for query in self.queries:
self.setProgress(i / len(self.queries) * 100)
i += 1
if self.isCanceled():
self.exception = 'Laskenta keskeytetty'
return False
try:
self.cur.execute(query)
except Exception as e:
self.exception = e
self.conn.rollback()
self.conn.close()
return False
self.conn.commit()
self.conn.close()
return True
def finished(self, result):
if not result:
QgsMessageLog.logMessage('Laskentavirhe: ' + str(self.exception), 'YKRTool', Qgis.Critical)
# raise self.exception
self.cancel()