Skip to content

Commit c33c097

Browse files
committedFeb 27, 2015
[MetaSearch]: use OWSLib for connection testing, add task to build CSW connection file
1 parent f106a2e commit c33c097

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed
 

‎python/plugins/MetaSearch/pavement.py

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
import getpass
2424
import os
2525
import shutil
26-
from urllib import urlencode
27-
from urllib2 import urlopen
2826
import xml.etree.ElementTree as etree
2927
import xmlrpclib
3028
import zipfile
3129

3230
from paver.easy import (call_task, cmdopts, error, info, options, path,
33-
pushd, sh, task, Bunch)
31+
sh, task, Bunch)
32+
33+
from owslib.csw import CatalogueServiceWeb
3434

3535
PLUGIN_NAME = 'MetaSearch'
3636
BASEDIR = os.path.abspath(os.path.dirname(__file__))
@@ -180,24 +180,46 @@ def test_default_csw_connections():
180180
relpath = 'resources%sconnections-default.xml' % os.sep
181181
csw_connections_xml = options.base.plugin / relpath
182182

183-
csws = etree.parse(csw_connections_xml)
184-
185-
for csw in csws.findall('csw'):
186-
# name = csw.attrib.get('name')
187-
data = {
188-
'service': 'CSW',
189-
'version': '2.0.2',
190-
'request': 'GetCapabilities'
191-
}
192-
values = urlencode(data)
193-
url = '%s?%s' % (csw.attrib.get('url'), values)
194-
content = urlopen(url)
195-
if content.getcode() != 200:
196-
raise ValueError('Bad HTTP status code')
197-
csw_xml = etree.fromstring(content.read())
198-
tag = '{http://www.opengis.net/cat/csw/2.0.2}Capabilities'
199-
if csw_xml.tag != tag:
200-
raise ValueError('root element should be csw:Capabilities')
183+
conns = etree.parse(csw_connections_xml)
184+
185+
for conn in conns.findall('csw'):
186+
try:
187+
csw = CatalogueServiceWeb(conn.attrib.get('url'))
188+
info('Success: %s', csw.identification.title)
189+
except Exception, err:
190+
raise ValueError('ERROR: %s', err)
191+
192+
193+
@task
194+
@cmdopts([
195+
('filename=', 'f', 'Path to file of CSW URLs'),
196+
])
197+
def generate_csw_connections_file():
198+
"""generate a CSW connections file from a flat file of CSW URLs"""
199+
200+
filename = options.get('filename', False)
201+
202+
if not filename:
203+
raise ValueError('path to file of CSW URLs required')
204+
205+
conns = etree.Element('qgsCSWConnections')
206+
conns.attrib['version'] = '1.0'
207+
208+
with open(filename) as connsfh:
209+
for line in connsfh:
210+
url = line.strip()
211+
if not url: # blank line
212+
continue
213+
try:
214+
csw = CatalogueServiceWeb(url)
215+
title = unicode(csw.identification.title)
216+
conn = etree.SubElement(conns, 'csw', name=title, url=url)
217+
#conn.attrib['name'] = title
218+
#conn.attrib['url'] = url
219+
except Exception, err:
220+
error('ERROR on CSW %s: %s', url, err)
221+
222+
info(etree.tostring(conns, encoding='utf-8'))
201223

202224

203225
def get_package_filename():

0 commit comments

Comments
 (0)
Please sign in to comment.