Skip to content

Commit

Permalink
clean print statements
Browse files Browse the repository at this point in the history
  • Loading branch information
warrieka committed Apr 5, 2021
1 parent 2f38599 commit 9725f24
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
25 changes: 8 additions & 17 deletions dataCatalog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from qgis.PyQt.QtCore import (Qt, QSettings, QTranslator, QCoreApplication, QUrl,
from qgis.PyQt.QtCore import (Qt, QCoreApplication, QUrl,
QSortFilterProxyModel, QRegExp, QStringListModel)
from qgis.PyQt.QtGui import QCursor, QStandardItemModel, QStandardItem
from qgis.PyQt.QtWidgets import (QApplication, QDialog, QSizePolicy, QCompleter, QInputDialog,
Expand All @@ -21,15 +21,6 @@ def __init__(self, iface):
QDialog.__init__(self, None)
self.setWindowFlags( self.windowFlags() & ~Qt.WindowContextHelpButtonHint )
self.iface = iface

# initialize locale
locale = QSettings().value("locale/userLocale", "nl")
locale = locale[0:2]
localePath = os.path.join(os.path.dirname(__file__), 'i18n', '{}.qm'.format(locale))
if os.path.exists(localePath):
self.translator = QTranslator()
self.translator.load(localePath)

self.initGui()

def initGui(self):
Expand Down Expand Up @@ -58,7 +49,6 @@ def initGui(self):
#datamodel
self.model = QStandardItemModel(self)
self.proxyModel = QSortFilterProxyModel(self)
self.proxyModel.setSourceModel(self.model)
self.ui.resultView.setModel(self.proxyModel )

#completer
Expand Down Expand Up @@ -101,6 +91,8 @@ def setModel(self, records):
wcsName = QStandardItem( rec['wcs'][0] ) #10
wmtsName = QStandardItem( rec['wmts'][0] ) #11
self.model.appendRow([title,wms,downloadLinks,id,abstract,wfs,wcs,wmts,wmsName,wfsName,wcsName,wmtsName])

self.proxyModel.setSourceModel(self.model)

#overwrite
def show(self):
Expand Down Expand Up @@ -165,7 +157,6 @@ def resultViewClicked(self):
def onZoekClicked(self):
"""Called when user clicked zoekBtn"""
self.zoek = self.ui.zoekTxt.currentText()
self.model.clear()
self.search()

def modelFilterCbxIndexChanged(self):
Expand Down Expand Up @@ -203,18 +194,18 @@ def search(self):
else: dataType=''
inspiretheme= self.ui.INSPIREthemaCbx.currentText()
inspireServiceType= self.ui.INSPIREserviceCbx.currentText()
searchResult = metadata.MDdata(
self.md.searchAll( self.zoek, orgName, dataType, inspiretheme, inspireServiceType) )
searchResult = self.md.searchAll( self.zoek, orgName, dataType, inspiretheme, inspireServiceType)
else:
searchResult = metadata.MDdata( self.md.searchAll( self.zoek ) )
searchResult = self.md.searchAll( self.zoek )
QApplication.restoreOverrideCursor()

self.ui.countLbl.setText( "Aantal gevonden: %s" % searchResult.count )
self.ui.descriptionText.setText('')
self.setModel(searchResult.records)
if searchResult.count == 0:
self.bar.pushMessage( QCoreApplication.translate("datacatalog", "Waarschuwing "),
QCoreApplication.translate("datacatalog", "Er zijn geen resultaten gevonden voor deze zoekopdracht"), duration=10)
QCoreApplication.translate("datacatalog",
"Er zijn geen resultaten gevonden voor deze zoekopdracht"), duration=10)

def addWMS(self):
"""Add WMS from current record to map"""
Expand Down
46 changes: 27 additions & 19 deletions metadataParser.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import json, sys, json
from urllib.parse import urlencode, unquote
from urllib.parse import urlencode, unquote, urlparse, parse_qsl
import xml.etree.ElementTree as ET
from io import StringIO
from .webUtil import getUrlData
from .webUtil import getUrlData, metaError

CSW_URL = "http://www.nationaalgeoregister.nl/geonetwork/srv/dut/inspire"
CSW_URL = "https://www.nationaalgeoregister.nl/geonetwork/srv/dut/inspire"


class MDdata(object):
count = 0
records = []
def __init__(self, metadataXML):
"""Parse a CSW-metadataXML
:param metadataXML: a CSW-metadata XMLdocument
"""
if not metadataXML: return
self.count = int( metadataXML.attrib["numberOfRecordsMatched"] )
self.records = []
self.count = 0

mds = metadataXML.findall("{http://www.opengis.net/cat/csw/2.0.2}Record")
for md in mds:
Expand Down Expand Up @@ -47,23 +47,28 @@ def __init__(self, metadataXML):
record['wms'] = self._findWXS( md, "OGC:WMS" )
record['wfs'] = self._findWXS( md, "OGC:WFS" )
record['wcs'] = self._findWXS( md, "OGC:WCS" )
record['wmts'] = self._findWXS( md , "OGC:WMTS")
record['wmts'] = self._findWXS( md, "OGC:WMTS")
record['download'] = self._findDownloads( md )
self.records.append(record)

def _findWXS(self , node, protocol= None ):
links = [n for n in node.findall("{http://purl.org/dc/elements/1.1/}URI")
def _findWXS(self, node, protocol= None ):
links = [n for n in node.findall("{http://purl.org/dc/elements/1.1/}URI")
if "protocol" in n.attrib and n.attrib["protocol"] == protocol]
links += [ n for n in node.findall("{http://purl.org/dc/elements/1.1/}URI")
if n.text
and 'SERVICE' in dict( parse_qsl( urlparse( n.text.upper() ).query ))
and dict(parse_qsl(urlparse( n.text.upper() ).query))['SERVICE'] in protocol ]

if len(links) > 0:
name = links[0].attrib["name"] if "name" in links[0].attrib else links[0].text
return (name, links[0].text)
return ("","")

def _findDownloads(self , node):
def _findDownloads(self, node):
links = [n for n in node.findall("{http://purl.org/dc/elements/1.1/}URI")
if "protocol" in n.attrib and "DOWNLOAD" in n.attrib["protocol"].upper() ]
atoms = [n.text for n in node.findall("{http://purl.org/dc/elements/1.1/}URI")
if "protocol" in n.attrib and "atom" in n.attrib["protocol"].lower() ]
if "protocol" in n.attrib and "ATOM" in n.attrib["protocol"].upper() ]

if len(links) == 0 and len(atoms) == 0:
return []
Expand All @@ -76,11 +81,15 @@ def _findDownloads(self , node):

try:
resp= getUrlData(atom)
except:
results = []
root = ET.fromstring(resp)
except ET.ParseError:
print( "WARNING: Geen correcte xml: {} ".format(atom) )
return []
except metaError as me:
print( "WARNING: http-fout {} -> geeft {}".format(atom, me.message) )
return []

results = []
root = ET.fromstring(resp) #parse(response).getroot() #
entries = root.findall( ".//{http://www.w3.org/2005/Atom}entry" )
for entry in entries:
dl = entry.find( "{http://www.w3.org/2005/Atom}link")
Expand Down Expand Up @@ -182,7 +191,7 @@ def list_organisations(self):
organisations.sort()
return organisations

def search(self, q="", start=1, step=100, orgName='', dataType='', inspiretheme='', inspireServiceType=''):
def _search(self, q="", start=1, step=100, orgName='', dataType='', inspiretheme='', inspireServiceType=''):
"""Search the csw with the following parameters:
:param q: free text to seach for
Expand All @@ -209,23 +218,22 @@ def searchAll(self, q="", orgName='', dataType='', inspiretheme='', inspireServi
:param inspireServiceType: filter on inspire serviceType
:return: a composite XMLdocument with the results
"""

start= 1
step= 100
result = self.search(q, start, step, orgName, dataType, inspiretheme, inspireServiceType)
result = self._search(q, start, step, orgName, dataType, inspiretheme, inspireServiceType)
searchResult = result.find(".//{http://www.opengis.net/cat/csw/2.0.2}SearchResults")
if not searchResult:
return
count = int( searchResult.attrib["numberOfRecordsMatched"] )
start += step
while (start) <= count:
result = self.search(q, start, step, orgName, dataType,inspiretheme, inspireServiceType)
result = self._search(q, start, step, orgName, dataType,inspiretheme, inspireServiceType)
mds= result.findall(".//{http://www.opengis.net/cat/csw/2.0.2}Record")
for md in mds: searchResult.append( md )
start += step

return searchResult

mdata = MDdata( searchResult )
return mdata


def getWmsLayerNames(url):
Expand Down
1 change: 0 additions & 1 deletion webUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def getUrlData(url, data=None, returnBytes=False):
:return: the response as a string
"""
bnr = QgsBlockingNetworkRequest()
print(url)
if not data:
respcode = bnr.get(QNetworkRequest( QUrl(url) ) )
else:
Expand Down

0 comments on commit 9725f24

Please sign in to comment.