Executes a SPARQL query using RDFlib, with or without credentials and tries GET and POST query methods and uses proxy settings.
- 398 def executeQuery(triplestoreurl, query,triplestoreconf=None):
-
-
400 SPARQLUtils.exception =
None
-
401 QgsMessageLog.logMessage(str(triplestoreurl), MESSAGE_CATEGORY, Qgis.Info)
-
402 if triplestoreurl[
"type"]==
"endpoint":
-
-
404 proxyEnabled = s.value(
"proxy/proxyEnabled")
-
405 proxyType = s.value(
"proxy/proxyType")
-
406 proxyHost = s.value(
"proxy/proxyHost")
-
407 proxyPort = s.value(
"proxy/proxyPort")
-
408 proxyUser = s.value(
"proxy/proxyUser")
-
409 proxyPassword = s.value(
"proxy/proxyPassword")
-
410 if proxyHost !=
None and proxyHost !=
"" and proxyPort !=
None and proxyPort !=
"":
-
411 QgsMessageLog.logMessage(
'Proxy? ' + str(proxyHost), MESSAGE_CATEGORY, Qgis.Info)
-
412 proxy = urllib.request.ProxyHandler({
'http': proxyHost})
-
413 opener = urllib.request.build_opener(proxy)
-
414 urllib.request.install_opener(opener)
-
415 QgsMessageLog.logMessage(
'Started task "{}"'.format(query.replace(
"<",
"").replace(
">",
"")), MESSAGE_CATEGORY, Qgis.Info)
-
416 sparql = SPARQLWrapper(triplestoreurl[
"url"])
-
417 if triplestoreconf!=
None and "auth" in triplestoreconf
and "userCredential" in triplestoreconf[
"auth"] \
-
418 and triplestoreconf[
"auth"][
"userCredential"]!=
"" \
-
419 and "userPassword" in triplestoreconf[
"auth"] \
-
420 and triplestoreconf[
"auth"][
"userPassword"] !=
None:
-
-
422 if "method" in triplestoreconf[
"auth"]
and triplestoreconf[
"auth"][
"method"]
in SPARQLUtils.authmethods:
-
423 sparql.setHTTPAuth(SPARQLUtils.authmethods[triplestoreconf[
"auth"][
"method"]])
-
-
425 sparql.setHTTPAuth(BASIC)
-
426 sparql.setCredentials(triplestoreconf[
"auth"][
"userCredential"], triplestoreconf[
"auth"][
"userPassword"])
-
427 sparql.setQuery(query)
-
428 sparql.setMethod(GET)
-
429 sparql.setReturnFormat(JSON)
-
-
-
-
433 results = sparql.queryAndConvert()
-
434 QgsMessageLog.logMessage(
"Result: QUERY FINISHED WITH RESULTS", MESSAGE_CATEGORY, Qgis.Info)
-
-
436 if isinstance(results,dict)
and "status_code" in results:
-
-
-
439 except Exception
as e:
-
-
441 sparql = SPARQLWrapper(triplestoreurl[
"url"],agent=
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11")
-
442 sparql.setQuery(query)
-
443 if triplestoreconf !=
None and "auth" in triplestoreconf
and "userCredential" in triplestoreconf[
"auth"] \
-
444 and triplestoreconf[
"auth"][
"userCredential"] !=
"" \
-
445 and "userPassword" in triplestoreconf[
"auth"] \
-
446 and triplestoreconf[
"auth"][
"userPassword"] !=
None:
-
-
-
-
450 if "method" in triplestoreconf[
"auth"]
and triplestoreconf[
"auth"][
-
451 "method"]
in SPARQLUtils.authmethods:
-
452 sparql.setHTTPAuth(SPARQLUtils.authmethods[triplestoreconf[
"auth"][
"method"]])
-
-
454 sparql.setHTTPAuth(BASIC)
-
455 sparql.setCredentials(triplestoreconf[
"auth"][
"userCredential"],
-
456 triplestoreconf[
"auth"][
"userPassword"])
-
457 sparql.setMethod(POST)
-
458 sparql.setReturnFormat(JSON)
-
459 results = sparql.queryAndConvert()
-
460 QgsMessageLog.logMessage(
"Result: QUERY FINISHED WITH RESULTS", MESSAGE_CATEGORY, Qgis.Info)
-
-
462 if isinstance(results,dict)
and "status_code" in results:
-
463 SPARQLUtils.exception = str(results)
-
-
-
466 QgsMessageLog.logMessage(
"Exception: " + str(e), MESSAGE_CATEGORY, Qgis.Info)
-
467 SPARQLUtils.exception=str(e)
-
468 if "OntopUnsupportedInputQueryException: The expression Exists" in str(e):
-
469 return "Exists error"
-
-
-
472 graph=triplestoreurl[
"instance"]
-
473 QgsMessageLog.logMessage(
"Graph: " + str(triplestoreurl), MESSAGE_CATEGORY, Qgis.Info)
-
474 QgsMessageLog.logMessage(
"Query: " + str(query).replace(
"<",
"").replace(
">",
""), MESSAGE_CATEGORY, Qgis.Info)
-
-
476 if "CONSTRUCT" in str(query):
-
477 results = graph.query(query)
-
-
-
-
-
-
483 results=json.loads(graph.query(query).serialize(format=
"json"))
-
484 QgsMessageLog.logMessage(
"Result: " + str(results)+
" triples", MESSAGE_CATEGORY, Qgis.Info)
-
-
-
+
400 def executeQuery(triplestoreurl, query,triplestoreconf=None):
+
+
402 SPARQLUtils.exception =
None
+
403 QgsMessageLog.logMessage(str(triplestoreurl), MESSAGE_CATEGORY, Qgis.Info)
+
404 if triplestoreurl[
"type"]==
"endpoint":
+
+
406 proxyEnabled = s.value(
"proxy/proxyEnabled")
+
407 proxyType = s.value(
"proxy/proxyType")
+
408 proxyHost = s.value(
"proxy/proxyHost")
+
409 proxyPort = s.value(
"proxy/proxyPort")
+
410 proxyUser = s.value(
"proxy/proxyUser")
+
411 proxyPassword = s.value(
"proxy/proxyPassword")
+
412 if proxyHost !=
None and proxyHost !=
"" and proxyPort !=
None and proxyPort !=
"":
+
413 QgsMessageLog.logMessage(
'Proxy? ' + str(proxyHost), MESSAGE_CATEGORY, Qgis.Info)
+
414 proxy = urllib.request.ProxyHandler({
'http': proxyHost})
+
415 opener = urllib.request.build_opener(proxy)
+
416 urllib.request.install_opener(opener)
+
417 QgsMessageLog.logMessage(
'Started task "{}"'.format(query.replace(
"<",
"").replace(
">",
"")), MESSAGE_CATEGORY, Qgis.Info)
+
418 sparql = SPARQLWrapper(triplestoreurl[
"url"])
+
419 if triplestoreconf!=
None and "auth" in triplestoreconf
and "userCredential" in triplestoreconf[
"auth"] \
+
420 and triplestoreconf[
"auth"][
"userCredential"]!=
"" \
+
421 and "userPassword" in triplestoreconf[
"auth"] \
+
422 and triplestoreconf[
"auth"][
"userPassword"] !=
None:
+
+
424 if "method" in triplestoreconf[
"auth"]
and triplestoreconf[
"auth"][
"method"]
in SPARQLUtils.authmethods:
+
425 sparql.setHTTPAuth(SPARQLUtils.authmethods[triplestoreconf[
"auth"][
"method"]])
+
+
427 sparql.setHTTPAuth(BASIC)
+
428 sparql.setCredentials(triplestoreconf[
"auth"][
"userCredential"], triplestoreconf[
"auth"][
"userPassword"])
+
429 sparql.setQuery(query)
+
430 sparql.setMethod(GET)
+
431 sparql.setReturnFormat(JSON)
+
+
+
+
435 results = sparql.queryAndConvert()
+
436 QgsMessageLog.logMessage(
"Result: QUERY FINISHED WITH RESULTS", MESSAGE_CATEGORY, Qgis.Info)
+
+
438 if isinstance(results,dict)
and "status_code" in results:
+
+
+
441 except Exception
as e:
+
+
443 sparql = SPARQLWrapper(triplestoreurl[
"url"],agent=
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11")
+
444 sparql.setQuery(query)
+
445 if triplestoreconf !=
None and "auth" in triplestoreconf
and "userCredential" in triplestoreconf[
"auth"] \
+
446 and triplestoreconf[
"auth"][
"userCredential"] !=
"" \
+
447 and "userPassword" in triplestoreconf[
"auth"] \
+
448 and triplestoreconf[
"auth"][
"userPassword"] !=
None:
+
+
+
+
452 if "method" in triplestoreconf[
"auth"]
and triplestoreconf[
"auth"][
+
453 "method"]
in SPARQLUtils.authmethods:
+
454 sparql.setHTTPAuth(SPARQLUtils.authmethods[triplestoreconf[
"auth"][
"method"]])
+
+
456 sparql.setHTTPAuth(BASIC)
+
457 sparql.setCredentials(triplestoreconf[
"auth"][
"userCredential"],
+
458 triplestoreconf[
"auth"][
"userPassword"])
+
459 sparql.setMethod(POST)
+
460 sparql.setReturnFormat(JSON)
+
461 results = sparql.queryAndConvert()
+
462 QgsMessageLog.logMessage(
"Result: QUERY FINISHED WITH RESULTS", MESSAGE_CATEGORY, Qgis.Info)
+
+
464 if isinstance(results,dict)
and "status_code" in results:
+
465 SPARQLUtils.exception = str(results)
+
+
+
468 QgsMessageLog.logMessage(
"Exception: " + str(e), MESSAGE_CATEGORY, Qgis.Info)
+
469 SPARQLUtils.exception=str(e)
+
470 if "OntopUnsupportedInputQueryException: The expression Exists" in str(e):
+
471 return "Exists error"
+
+
+
474 graph=triplestoreurl[
"instance"]
+
475 QgsMessageLog.logMessage(
"Graph: " + str(triplestoreurl), MESSAGE_CATEGORY, Qgis.Info)
+
476 QgsMessageLog.logMessage(
"Query: " + str(query).replace(
"<",
"").replace(
">",
""), MESSAGE_CATEGORY, Qgis.Info)
+
+
478 if "CONSTRUCT" in str(query):
+
479 results = graph.query(query)
+
+
+
+
+
+
485 results=json.loads(graph.query(query).serialize(format=
"json"))
+
+
+
+
@@ -555,25 +555,25 @@