Skip to content

Commit 7d6b26a

Browse files
authored
Merge pull request #616 from cehbrecht/fix-lxml-parser
Fix lxml default parser
2 parents 7112197 + 6896931 commit 7d6b26a

17 files changed

Lines changed: 95 additions & 43 deletions

pywps/app/WPSRequest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import logging
77
import lxml
8-
import lxml.etree
8+
from pywps import xml_util as etree
99
from werkzeug.exceptions import MethodNotAllowed
1010
from pywps import get_ElementMakerForVersion
1111
import base64
@@ -85,7 +85,7 @@ def _post_request(self):
8585
' Maximum request size allowed: {} megabytes'.format(maxsize / 1024 / 1024))
8686

8787
try:
88-
doc = lxml.etree.fromstring(self.http_request.get_data())
88+
doc = etree.fromstring(self.http_request.get_data())
8989
except Exception as e:
9090
raise NoApplicableCode(e.msg)
9191

@@ -571,7 +571,7 @@ def _get_dataelement_value(value_el):
571571
"""
572572

573573
if isinstance(value_el, lxml.etree._Element):
574-
return lxml.etree.tostring(value_el, encoding=str)
574+
return etree.tostring(value_el, encoding=str)
575575
else:
576576
return value_el
577577

pywps/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from werkzeug.wrappers import Response
1616
from werkzeug.exceptions import HTTPException
17-
from werkzeug.utils import escape
17+
from markupsafe import escape
1818

1919
import logging
2020

pywps/inout/inputs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
##################################################################
55

66
import re
7-
import lxml.etree as etree
7+
from pywps import xml_util as etree
88

99
from pywps.app.Common import Metadata
1010
from pywps.exceptions import InvalidParameterValue

pywps/inout/outputs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
WPS Output classes
77
"""
88

9-
import lxml.etree as etree
9+
from pywps import xml_util as etree
1010
import os
1111
import re
1212
from pywps.app.Common import Metadata

pywps/tests.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import tempfile
66
from pathlib import Path
77

8-
import lxml.etree
8+
import lxml
9+
from pywps import xml_util as etree
910
import requests
1011
from werkzeug.test import Client
11-
from werkzeug.wrappers import BaseResponse
12+
from werkzeug.wrappers import Response
1213
from pywps import __version__
1314
from pywps import Process
1415
from pywps.inout import LiteralInput, LiteralOutput, ComplexInput, ComplexOutput, BoundingBoxInput, BoundingBoxOutput
@@ -93,17 +94,17 @@ class WpsClient(Client):
9394

9495
def post_xml(self, *args, **kwargs):
9596
doc = kwargs.pop('doc')
96-
data = lxml.etree.tostring(doc, pretty_print=True)
97+
data = etree.tostring(doc, pretty_print=True)
9798
kwargs['data'] = data
9899
return self.post(*args, **kwargs)
99100

100101

101-
class WpsTestResponse(BaseResponse):
102+
class WpsTestResponse(Response):
102103

103104
def __init__(self, *args):
104105
super(WpsTestResponse, self).__init__(*args)
105106
if re.match(r'text/xml(;\s*charset=.*)?', self.headers.get('Content-Type')):
106-
self.xml = lxml.etree.fromstring(self.get_data())
107+
self.xml = etree.fromstring(self.get_data())
107108

108109
def xpath(self, path):
109110
version = self.xml.attrib["version"]

pywps/validator/complexvalidator.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
from pywps.validator.mode import MODE
1313
from pywps.inout.formats import FORMATS
14+
from lxml.etree import XMLSchema
15+
from pywps import xml_util as etree
1416
from urllib.request import urlopen
1517
import mimetypes
1618
import os
@@ -61,13 +63,10 @@ def validategml(data_input, mode):
6163
passed = False
6264

6365
if mode >= MODE.VERYSTRICT:
64-
65-
from lxml import etree
66-
6766
try:
6867
schema_url = data_input.data_format.schema
6968
gmlschema_doc = etree.parse(urlopen(schema_url))
70-
gmlschema = etree.XMLSchema(gmlschema_doc)
69+
gmlschema = XMLSchema(gmlschema_doc)
7170
passed = gmlschema.validate(etree.parse(data_input.stream))
7271
except Exception as e:
7372
LOGGER.warning(e)
@@ -118,13 +117,10 @@ def validategpx(data_input, mode):
118117
passed = False
119118

120119
if mode >= MODE.VERYSTRICT:
121-
122-
from lxml import etree
123-
124120
try:
125121
schema_url = data_input.data_format.schema
126122
gpxschema_doc = etree.parse(urlopen(schema_url))
127-
gpxschema = etree.XMLSchema(gpxschema_doc)
123+
gpxschema = XMLSchema(gpxschema_doc)
128124
passed = gpxschema.validate(etree.parse(data_input.stream))
129125
except Exception as e:
130126
LOGGER.warning(e)
@@ -164,15 +160,13 @@ def validatexml(data_input, mode):
164160
passed = data_input.data_format.mime_type in {mtype, FORMATS.GML.mime_type}
165161

166162
if mode >= MODE.STRICT:
167-
from lxml import etree
168-
169163
# TODO: Raise the actual validation exception to make it easier to spot the error.
170164
# xml = etree.parse(data_input.file)
171165
# schema.assertValid(xml)
172166
try:
173167
fn = os.path.join(_get_schemas_home(), data_input.data_format.schema)
174168
schema_doc = etree.parse(fn)
175-
schema = etree.XMLSchema(schema_doc)
169+
schema = XMLSchema(schema_doc)
176170
passed = schema.validate(etree.parse(data_input.file))
177171
except Exception as e:
178172
LOGGER.warning(e)

pywps/xml_util.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from lxml import etree as _etree
2+
3+
4+
PARSER = _etree.XMLParser(
5+
resolve_entities=False,
6+
)
7+
8+
tostring = _etree.tostring
9+
10+
11+
def fromstring(text):
12+
return _etree.fromstring(text, parser=PARSER)
13+
14+
15+
def parse(source):
16+
return _etree.parse(source, parser=PARSER)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ python-dateutil
66
requests
77
SQLAlchemy
88
werkzeug
9+
MarkupSafe
910
humanize

tests/test_capabilities.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
##################################################################
55

66
import unittest
7-
import lxml
8-
import lxml.etree
97
from pywps import configuration
108
from pywps.app import Process, Service
119
from pywps.app.Common import Metadata
1210
from pywps import get_ElementMakerForVersion
13-
from pywps.tests import assert_pywps_version, client_for, assert_wps_version
11+
from pywps.tests import client_for, assert_wps_version
1412

1513
WPS, OWS = get_ElementMakerForVersion("1.0.0")
1614

tests/test_complexdata_io.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import unittest
44
import os
5-
from pywps import get_ElementMakerForVersion, E
5+
from pywps import get_ElementMakerForVersion
66
from pywps.app.basic import get_xpath_ns
77
from pywps import Service, Process, ComplexInput, ComplexOutput, FORMATS
88
from pywps.tests import client_for, assert_response_success
99
from owslib.wps import WPSExecution, ComplexDataInput
10-
from lxml import etree
10+
from pywps import xml_util as etree
1111

1212
VERSION = "1.0.0"
1313
WPS, OWS = get_ElementMakerForVersion(VERSION)

0 commit comments

Comments
 (0)