-
Notifications
You must be signed in to change notification settings - Fork 6
/
sensor_things_browser.py
288 lines (218 loc) · 9.89 KB
/
sensor_things_browser.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*- coding: utf-8 -*-
"""SensorThingsWebView class
Description
-----------
This is QWebView customized subclass.
Libraries/Modules
-----------------
- None.
Notes
-----
- None.
Author(s)
---------
- Created by Sandro Moretti on 06/06/2022.
Dedagroup Spa.
Members
-------
"""
import json
from qgis.core import QgsApplication, QgsNetworkAccessManager
from qgis.PyQt.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl, QVariant, QEventLoop, QObject
from qgis.PyQt.QtWebKit import QWebSettings
from qgis.PyQt.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply
# pylint: disable=no-name-in-module
from qgis.PyQt.QtWebKitWidgets import QWebView, QWebPage
# plugin modules
from SensorThingsAPI import __QGIS_PLUGIN_NAME__, __PLG_DEBUG__
from SensorThingsAPI.log.logger import QgisLogger as logger
#
#-----------------------------------------------------------
class SensorThingsRequestError(Exception):
"""Base class for other exceptions"""
#
#-----------------------------------------------------------
class SensorThingsNetworkReplyPromise(QObject):
"""
Class to create an asynchronous network reply
with signal resolved, rejected to mimic
javascript promose
"""
# signals
resolved = pyqtSignal(QVariant)
rejected = pyqtSignal(str)
def __init__(self, nam: QgsNetworkAccessManager, parent: QObject=None):
"""Constructor"""
super().__init__(parent=parent)
# init
self._nam = nam or QgsNetworkAccessManager.instance()
self._options = {}
@pyqtSlot(str, QVariant)
def get(self, url, options):
"""Send a get request"""
# init
self._options = options if isinstance(options, dict) else {}
url = str(url).strip()
# request
request = QNetworkRequest(QUrl(url))
request.setPriority(QNetworkRequest.HighPriority)
request.setAttribute(QNetworkRequest.HttpPipeliningAllowedAttribute, True)
# no cache
request.setAttribute(QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.AlwaysNetwork)
request.setAttribute(QNetworkRequest.CacheSaveControlAttribute, False)
# send request
reply = self._nam.get(request)
reply.readyRead.connect(lambda: QgsApplication.processEvents( QEventLoop.ExcludeUserInputEvents ))
reply.finished.connect(self.onFinishedRequest)
# debug
if __PLG_DEBUG__:
logger.log(logger.Level.Info, "{}: {}".format(self.tr('Request url'), url))
#
QgsApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
def onFinishedRequest(self):
"""finished request slot"""
reply = None
try:
# init
reply = self._nam.sender()
if reply is None:
raise SensorThingsRequestError(self.tr('No data returned'))
# check if reply object
if not hasattr(reply, 'error'):
raise SensorThingsRequestError(self.tr('Request timeout'))
# check if error
if reply.error() != QNetworkReply.NoError:
raise SensorThingsRequestError(reply.errorString())
status_code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
if status_code != 200:
raise SensorThingsRequestError("{}: {}".format(self.tr('HTTP status code'), status_code))
# return data
if hasattr(reply, 'readAll'):
result = json.loads(str( reply.readAll().data(), 'utf-8' ))
else:
result = json.loads(str( reply.content(), 'utf-8' ))
# get source data
dataSrc = str(self._options.get('dataSrc', '')).strip()
if dataSrc and isinstance(result, dict):
result = result.get(dataSrc, result)
# return data
self.resolved.emit(result)
except Exception as ex:
self.rejected.emit(str(ex))
finally:
if reply is not None:
reply.deleteLater()
#
#-----------------------------------------------------------
class SensorThingsNetworkAccessManager(QNetworkAccessManager):
"""QNetworkAccessManager subclass.
This subclass is used to deny external resources.
"""
def __init__(self, deny_external_links, parent=None):
self._deny_external_links = deny_external_links
super().__init__(parent)
def createRequest(self, op, request, outgoingData=None):
strURL = str(request.url().toString())
if __PLG_DEBUG__:
logger.log(logger.Level.Info, "{}: {}".format(self.tr("Request url"), strURL))
if self._deny_external_links and\
not request.url().isLocalFile():
request.setUrl(QUrl('file:///web/denied.resouce'))
return super().createRequest(op, request, outgoingData)
@staticmethod
def requestExternalData(url: str, options: dict=None, nam: QgsNetworkAccessManager=None):
"""Method to request external data synchronously"""
# init
nam = nam or QgsNetworkAccessManager.instance()
url = str(url).strip()
options = options if isinstance(options, dict) else {}
# request
request = QNetworkRequest(QUrl(url))
request.setPriority(QNetworkRequest.HighPriority)
request.setAttribute(QNetworkRequest.HttpPipeliningAllowedAttribute, True)
# no cache
request.setAttribute(QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.AlwaysNetwork)
request.setAttribute(QNetworkRequest.CacheSaveControlAttribute, False)
# debug
if __PLG_DEBUG__:
logger.log(logger.Level.Info, "{}: {}".format(SensorThingsNetworkAccessManager.tr("Request url"), url))
# send request
reply = nam.blockingGet(request)
# check if error
if reply.error() != QNetworkReply.NoError:
raise SensorThingsRequestError(reply.errorString())
status_code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
if status_code != 200:
raise SensorThingsRequestError(reply.errorString())
# return data
if hasattr( reply, 'readAll' ):
result = json.loads(str( reply.readAll().data(), 'utf-8' ))
else:
result = json.loads(str( reply.content(), 'utf-8' ))
# get source data
dataSrc = str(options.get('dataSrc', '')).strip()
if dataSrc and isinstance(result, dict):
result = result.get(dataSrc, result)
# return data
return result
@staticmethod
def requestExternalDataAsync(nam: QgsNetworkAccessManager=None, parent=None):
"""Method to request external data asynchronously"""
return SensorThingsNetworkReplyPromise(nam, parent)
#
#-----------------------------------------------------------
class SensorThingsWebPage(QWebPage):
"""QWebPage subclass that opens links in system browser
This subclass is used to deny external resources.
"""
def __init__(self, deny_external_urls, parent=None):
super().__init__(parent)
self._deny_external_urls = deny_external_urls
def acceptNavigationRequest(self, frame, request, navigationType):
strURL = str(request.url().toString())
if __PLG_DEBUG__:
logger.log(logger.Level.Info, "{}: {}".format(self.tr("Webview request"), strURL))
if self._deny_external_urls and\
not request.url().isLocalFile():
return False
return super().acceptNavigationRequest(frame, request, navigationType)
#
#-----------------------------------------------------------
class SensorThingsWebView(QWebView):
"""Derived QWebView class to deny external resources"""
def __init__(self, parent=None):
# init
QWebView.__init__(self, parent=parent)
# Create widgets
nam = SensorThingsNetworkAccessManager(deny_external_links=True)
webPage = SensorThingsWebPage(deny_external_urls=True)
webPage.setNetworkAccessManager(nam)
webPage.networkAccessManager().sslErrors.connect(self._handleSslErrors)
self.setPage(webPage)
#super().connect(self.ui.webView,QtCore.SIGNAL("titleChanged (const QString&)"), self.adjustTitle)
settings = self.settings()
##settings.setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls, False)
##settings.setAttribute(QWebSettings.LocalContentCanAccessFileUrls, False)
##settings.setAttribute(QWebSettings.XSSAuditingEnabled, True)
settings.setDefaultTextEncoding('utf-8')
if __PLG_DEBUG__:
settings.setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
else:
self.setContextMenuPolicy(Qt.NoContextMenu)
def _handleSslErrors(self, reply, errors):
"""Handle SSL errors"""
logger.log(
logger.Level.Critical,
"{}: {}".format(self.tr("SSL error"), errors),
tag=__QGIS_PLUGIN_NAME__)
reply.ignoreSslErrors()
def _injectPyToJs(self, obj, js_objname='pyjsapi'):
"""Add pyapi to javascript window object"""
self.page().mainFrame().addToJavaScriptWindowObject(js_objname, obj)
def injectPyToJs(self, obj, js_objname='pyjsapi'):
"""Add pyapi to javascript window object"""
# add Python object to js
self._injectPyToJs(obj, js_objname)
# add Python object to js on document loaded
self.page().mainFrame().javaScriptWindowObjectCleared.connect(
lambda o=obj, n=js_objname, self=self: self._injectPyToJs(o, n))