Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix broken support to py2.7 #256 #257

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions ckanext/spatial/lib/csw_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

log = logging.getLogger(__name__)

# Py2 vs Py3 encoding
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use six for the python version checking? eg

import six

if six.PY3:
    # ...
else:
    # ...

or even

_encoding = 'utf-8' if six.PY3 else str

But looking at the documentation it looks like the most portable solution would be to pass
record["xml"] = etree.tostring(mdtree, pretty_print=True, encoding='utf-8') regardless of the Python version. Does this work as expected on Python 2.7 (and 3)?


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was 'unicode' and have been change into 'str' by:
e063522#diff-db75a495bf76416216c3b4fe8cc1038bc53d22a19cb0990cbd823b17c2d67364R104

Now I can't be sure about py3 (don't have a working environment) but you could be right.
To avoid causing problem I proposed encoding='utf-8' for py2 and 'as it is' (futurized) for py3.

Can someone confirm about py3?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems ok to me.
There are tests now, so such change could be made, and we see how the CI tests go :)

if six.PY2:
_enc='utf-8'
else:
_enc=str

class CswError(Exception):
pass

Expand Down Expand Up @@ -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"] = '<?xml version="1.0" encoding="UTF-8"?>\n' + record["xml"]
record["tree"] = mdtree
Expand Down
10 changes: 8 additions & 2 deletions ckanext/spatial/model/harvested_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
Expand Down