geopython / OWSLib

OWSLib is a Python package for client programming with Open Geospatial Consortium (OGC) web service (hence OWS) interface standards, and their related content models.
https://owslib.readthedocs.io
BSD 3-Clause "New" or "Revised" License
391 stars 277 forks source link

WFS crashes when <ows:OperationsMetaData> contains a comment tag #182

Closed naavis closed 10 years ago

naavis commented 10 years ago

When creating a WebFeatureService 2.0.0 object with the Finnish Meteorological Institute's WFS API the library crashes with the following stack trace:

Traceback (most recent call last):
  File "C:\Users\Samuli\Desktop\wfs_test.py", line 6, in <module>
    wfs = WebFeatureService(wfs_base_url, version='2.0.0')
  File "C:\Python27\lib\site-packages\owslib\wfs.py", line 33, in WebFeatureService
    return wfs200.WebFeatureService_2_0_0(url,  version, xml, parse_remote_metadata)
  File "C:\Python27\lib\site-packages\owslib\feature\wfs200.py", line 78, in __init__
    self._buildMetadata(parse_remote_metadata)
  File "C:\Python27\lib\site-packages\owslib\feature\wfs200.py", line 106, in _buildMetadata
    self.operations.append(OperationsMetadata(elem))
  File "C:\Python27\lib\site-packages\owslib\ows.py", line 165, in __init__
    self.name = elem.attrib['name']
KeyError: 'name'

By stepping through the code I noticed the crash happened when it hit the following comment tag in the <ows:OperationsMetadata> part of the GetCapabilities response:

<!--

      *****************************************************

-->

Maybe there is a check for comment tags missing somewhere? Since the API is not open for all, I unfortunately cannot paste the whole response here.

tomkralidis commented 10 years ago

@naavis thanks. Can you provide a GetCapabilities response (feel free to send to me offline)? The error is due to a missing element.

naavis commented 10 years ago

The element is missing because the tag is a comment tag. I think comment tags should be completely ignored. I removed all comments from the response and it is parsed correctly. I will send you the response offline.

tomkralidis commented 10 years ago

@naavis thanks. Confirmed. The question is why is this getting picked up by the XML parser to begin with.

Having said this, a safe enough workaround would be:

diff --git a/owslib/ows.py b/owslib/ows.py
index cb5ade5..b1bfd80 100644
--- a/owslib/ows.py
+++ b/owslib/ows.py
@@ -162,6 +162,8 @@ class Constraint(object):
 class OperationsMetadata(object):
     """Initialize an OWS OperationMetadata construct"""
     def __init__(self, elem, namespace=DEFAULT_OWS_NAMESPACE):
+        if 'name' not in elem.attrib:  # this is not a valid element
+            return
         self.name = elem.attrib['name']
         self.formatOptions = ['text/xml']
         parameters = []

does this work for you? If yes, PR is welcome if possible.

naavis commented 10 years ago

@tomkralidis Thanks, the fix seems to work. I will do a pull request.