diff --git a/ckanext/spatial/lib/csw_client.py b/ckanext/spatial/lib/csw_client.py index 0ac075e5..1791f083 100644 --- a/ckanext/spatial/lib/csw_client.py +++ b/ckanext/spatial/lib/csw_client.py @@ -10,6 +10,12 @@ log = logging.getLogger(__name__) +# Py2 vs Py3 encoding +if six.PY2: + _enc='utf-8' +else: + _enc=str + class CswError(Exception): pass @@ -178,13 +184,13 @@ def getrecordbyid(self, ids=[], esn="full", outputschema="gmd", **kw): md = csw._exml.find("/{http://www.isotc211.org/2005/gmd}MD_Metadata") mdtree = etree.ElementTree(md) try: - record["xml"] = etree.tostring(mdtree, pretty_print=True, encoding=str) + record["xml"] = etree.tostring(mdtree, pretty_print=True, encoding=_enc) except TypeError: # API incompatibilities between different flavours of elementtree try: - record["xml"] = etree.tostring(mdtree, pretty_print=True, encoding=str) + record["xml"] = etree.tostring(mdtree, pretty_print=True, encoding=_enc) except AssertionError: - record["xml"] = etree.tostring(md, pretty_print=True, encoding=str) + record["xml"] = etree.tostring(md, pretty_print=True, encoding=_enc) record["xml"] = '\n' + record["xml"] record["tree"] = mdtree diff --git a/ckanext/spatial/model/harvested_metadata.py b/ckanext/spatial/model/harvested_metadata.py index a7225185..5ed6db77 100644 --- a/ckanext/spatial/model/harvested_metadata.py +++ b/ckanext/spatial/model/harvested_metadata.py @@ -4,6 +4,12 @@ import logging log = logging.getLogger(__name__) +# Py2 vs Py3 encoding +def _encode(element): + if six.PY2: + return element.encode('utf-8') + else: + return str(element) class MappedXmlObject(object): elements = [] @@ -91,9 +97,9 @@ def get_value(self, element): value[child.name] = child.read_value(element) return value elif type(element) == etree._ElementStringResult: - value = str(element) + value = _encode(element) elif type(element) == etree._ElementUnicodeResult: - value = str(element) + value = _encode(element) else: value = self.element_tostring(element) return value