Skip to content
Snippets Groups Projects
Commit 0323005e authored by Gavin M. Roy's avatar Gavin M. Roy
Browse files

Make things work in Python 2.7 && 3

parent dbe9302e
No related branches found
No related tags found
No related merge requests found
import unittest
 
from tornado_aws import xml
from tornado_aws import txml
 
 
class TestCase(unittest.TestCase):
Loading
Loading
@@ -9,14 +9,14 @@ class TestCase(unittest.TestCase):
expectation = {'test': {'foo': 'bar', 'baz': {'qux': 'corgie'}}}
value = """<?xml version="1.0" encoding="UTF-8"?>
<test><foo>bar</foo><baz><qux>corgie</qux></baz></test>"""
self.assertDictEqual(xml.loads(value), expectation)
self.assertDictEqual(txml.loads(value), expectation)
 
def test_generic_xml_with_attributes(self):
expectation = {'test': {'foo': 'bar',
'baz': {'qux': 'corgie', '@val': '1'}}}
value = """<?xml version="1.0" encoding="UTF-8"?>
<test><foo>bar</foo><baz val="1"><qux>corgie</qux></baz></test>"""
self.assertDictEqual(xml.loads(value), expectation)
self.assertDictEqual(txml.loads(value), expectation)
 
def test_generic_xml_with_text_and_children(self):
expectation = {'test': {'foo': 'bar',
Loading
Loading
@@ -25,9 +25,9 @@ class TestCase(unittest.TestCase):
'#text': 'gorge'}}}
value = """<?xml version="1.0" encoding="UTF-8"?>
<test><foo>bar</foo><baz val="1">gorge<qux>corgie</qux></baz></test>"""
self.assertDictEqual(xml.loads(value), expectation)
self.assertDictEqual(txml.loads(value), expectation)
 
def test_invalid_xml(self):
with self.assertRaises(ValueError):
xml.loads('foo')
txml.loads('foo')
 
Loading
Loading
@@ -12,9 +12,15 @@ import json
import logging
import os
try:
from urllib import parse as _urlparse
from urllib.parse import urlparse
except ImportError: # pragma: nocover
import urlparse as _urlparse
from urlparse import urlparse
try:
from urllib.parse import quote
except ImportError: # pragma: nocover
from urllib import quote
 
from tornado import concurrent, httpclient, ioloop
 
Loading
Loading
@@ -23,7 +29,7 @@ try:
except ImportError: # pragma: nocover
curl_httpclient = None
 
from tornado_aws import config, exceptions, xml
from tornado_aws import config, exceptions, txml
 
LOGGER = logging.getLogger(__name__)
 
Loading
Loading
@@ -250,7 +256,7 @@ class AWSClient(object):
:raises: ValueError
 
"""
payload = xml.loads(content.decode('utf-8'))
payload = txml.loads(content.decode('utf-8'))
if 'Error' in payload:
return payload['Error']
elif 'Errors' in payload and 'Error' in payload['Errors']:
Loading
Loading
@@ -333,7 +339,7 @@ class AWSClient(object):
:return: str
 
"""
return _urlparse.urlparse(url).netloc
return urlparse(url).netloc
 
@staticmethod
def _quote(value):
Loading
Loading
@@ -344,7 +350,7 @@ class AWSClient(object):
:rtype: str
 
"""
return _urlparse.quote(value, safe='').replace('%7E', '~')
return quote(value, safe='').replace('%7E', '~')
 
@staticmethod
def _sign(key, msg):
Loading
Loading
Loading
Loading
@@ -5,8 +5,8 @@ XML Deserialization
Parse XML return content and return it as a dict.
 
"""
from collections import defaultdict
from xml.etree import ElementTree as etree
import collections
import xml.etree.ElementTree as ET
 
 
def loads(content):
Loading
Loading
@@ -18,8 +18,8 @@ def loads(content):
 
"""
try:
return _xml_to_dict(etree.XML(content))
except etree.ParseError as error:
return _xml_to_dict(ET.XML(content))
except ET.ParseError as error:
raise ValueError(str(error))
 
 
Loading
Loading
@@ -33,7 +33,7 @@ def _xml_to_dict(t):
d = {t.tag: {} if t.attrib else None}
children = list(t)
if children:
dd = defaultdict(list)
dd = collections.defaultdict(list)
for dc in map(_xml_to_dict, children):
for k, v in dc.items():
dd[k].append(v)
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment