[MetaSearch]: use OWSLib for connection testing, add task to build CSW connection file

This commit is contained in:
Tom Kralidis 2015-02-27 07:09:26 -05:00
parent f106a2ef99
commit c33c097706

View File

@ -23,14 +23,14 @@ from ConfigParser import ConfigParser
import getpass import getpass
import os import os
import shutil import shutil
from urllib import urlencode
from urllib2 import urlopen
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
import xmlrpclib import xmlrpclib
import zipfile import zipfile
from paver.easy import (call_task, cmdopts, error, info, options, path, from paver.easy import (call_task, cmdopts, error, info, options, path,
pushd, sh, task, Bunch) sh, task, Bunch)
from owslib.csw import CatalogueServiceWeb
PLUGIN_NAME = 'MetaSearch' PLUGIN_NAME = 'MetaSearch'
BASEDIR = os.path.abspath(os.path.dirname(__file__)) BASEDIR = os.path.abspath(os.path.dirname(__file__))
@ -180,24 +180,46 @@ def test_default_csw_connections():
relpath = 'resources%sconnections-default.xml' % os.sep relpath = 'resources%sconnections-default.xml' % os.sep
csw_connections_xml = options.base.plugin / relpath csw_connections_xml = options.base.plugin / relpath
csws = etree.parse(csw_connections_xml) conns = etree.parse(csw_connections_xml)
for csw in csws.findall('csw'): for conn in conns.findall('csw'):
# name = csw.attrib.get('name') try:
data = { csw = CatalogueServiceWeb(conn.attrib.get('url'))
'service': 'CSW', info('Success: %s', csw.identification.title)
'version': '2.0.2', except Exception, err:
'request': 'GetCapabilities' raise ValueError('ERROR: %s', err)
}
values = urlencode(data)
url = '%s?%s' % (csw.attrib.get('url'), values) @task
content = urlopen(url) @cmdopts([
if content.getcode() != 200: ('filename=', 'f', 'Path to file of CSW URLs'),
raise ValueError('Bad HTTP status code') ])
csw_xml = etree.fromstring(content.read()) def generate_csw_connections_file():
tag = '{http://www.opengis.net/cat/csw/2.0.2}Capabilities' """generate a CSW connections file from a flat file of CSW URLs"""
if csw_xml.tag != tag:
raise ValueError('root element should be csw:Capabilities') filename = options.get('filename', False)
if not filename:
raise ValueError('path to file of CSW URLs required')
conns = etree.Element('qgsCSWConnections')
conns.attrib['version'] = '1.0'
with open(filename) as connsfh:
for line in connsfh:
url = line.strip()
if not url: # blank line
continue
try:
csw = CatalogueServiceWeb(url)
title = unicode(csw.identification.title)
conn = etree.SubElement(conns, 'csw', name=title, url=url)
#conn.attrib['name'] = title
#conn.attrib['url'] = url
except Exception, err:
error('ERROR on CSW %s: %s', url, err)
info(etree.tostring(conns, encoding='utf-8'))
def get_package_filename(): def get_package_filename():