From 1eb38ddcfacba6b58764ba2728eba15beef0633d Mon Sep 17 00:00:00 2001 From: carlo cancellieri Date: Wed, 20 Oct 2021 13:04:00 +0200 Subject: [PATCH 1/2] fix encoding problem on py2.7 #256 --- ckanext/spatial/lib/csw_client.py | 13 ++++++++++--- ckanext/spatial/model/harvested_metadata.py | 11 +++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/ckanext/spatial/lib/csw_client.py b/ckanext/spatial/lib/csw_client.py index 0ac075e5..de768bd6 100644 --- a/ckanext/spatial/lib/csw_client.py +++ b/ckanext/spatial/lib/csw_client.py @@ -10,6 +10,13 @@ log = logging.getLogger(__name__) +# Py2 vs Py3 encoding +import sys +if sys.version_info[0] < 3: + _enc='utf-8' +else: + _enc=str + class CswError(Exception): pass @@ -178,13 +185,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..feeda78b 100644 --- a/ckanext/spatial/model/harvested_metadata.py +++ b/ckanext/spatial/model/harvested_metadata.py @@ -4,6 +4,13 @@ import logging log = logging.getLogger(__name__) +# Py2 vs Py3 encoding +def _encode(element): + import sys + if sys.version_info[0] < 3: + return element.encode('utf-8') + else: + return str(element) class MappedXmlObject(object): elements = [] @@ -91,9 +98,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 From c67bfaad25ee89549b08685b75e9e3b6f0958da9 Mon Sep 17 00:00:00 2001 From: carlo cancellieri Date: Thu, 28 Oct 2021 19:03:22 +0200 Subject: [PATCH 2/2] use six for python version check --- ckanext/spatial/lib/csw_client.py | 3 +-- ckanext/spatial/model/harvested_metadata.py | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/ckanext/spatial/lib/csw_client.py b/ckanext/spatial/lib/csw_client.py index de768bd6..1791f083 100644 --- a/ckanext/spatial/lib/csw_client.py +++ b/ckanext/spatial/lib/csw_client.py @@ -11,8 +11,7 @@ log = logging.getLogger(__name__) # Py2 vs Py3 encoding -import sys -if sys.version_info[0] < 3: +if six.PY2: _enc='utf-8' else: _enc=str diff --git a/ckanext/spatial/model/harvested_metadata.py b/ckanext/spatial/model/harvested_metadata.py index feeda78b..5ed6db77 100644 --- a/ckanext/spatial/model/harvested_metadata.py +++ b/ckanext/spatial/model/harvested_metadata.py @@ -5,9 +5,8 @@ log = logging.getLogger(__name__) # Py2 vs Py3 encoding -def _encode(element): - import sys - if sys.version_info[0] < 3: +def _encode(element): + if six.PY2: return element.encode('utf-8') else: return str(element)