|
23 | 23 | import getpass
|
24 | 24 | import os
|
25 | 25 | import shutil
|
26 |
| -from urllib import urlencode |
27 |
| -from urllib2 import urlopen |
28 | 26 | import xml.etree.ElementTree as etree
|
29 | 27 | import xmlrpclib
|
30 | 28 | import zipfile
|
31 | 29 |
|
32 | 30 | 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 |
34 | 34 |
|
35 | 35 | PLUGIN_NAME = 'MetaSearch'
|
36 | 36 | BASEDIR = os.path.abspath(os.path.dirname(__file__))
|
@@ -180,24 +180,46 @@ def test_default_csw_connections():
|
180 | 180 | relpath = 'resources%sconnections-default.xml' % os.sep
|
181 | 181 | csw_connections_xml = options.base.plugin / relpath
|
182 | 182 |
|
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')) |
201 | 223 |
|
202 | 224 |
|
203 | 225 | def get_package_filename():
|
|
0 commit comments