From 867fc3b7f7c6a53cba4249b1c1485c6c092e6b3f Mon Sep 17 00:00:00 2001 From: Jack Danger Date: Mon, 2 Oct 2017 13:35:53 -0700 Subject: [PATCH 1/2] Removing dicttoxml dependency --- moto/redshift/responses.py | 23 +++++++++++++++++++++-- setup.py | 1 - 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/moto/redshift/responses.py b/moto/redshift/responses.py index 52ca908e8..58983310f 100644 --- a/moto/redshift/responses.py +++ b/moto/redshift/responses.py @@ -2,7 +2,8 @@ from __future__ import unicode_literals import json -import dicttoxml +import xmltodict + from jinja2 import Template from six import iteritems @@ -26,6 +27,24 @@ def convert_json_error_to_xml(json_error): return template.render(code=code, message=message) +def itemize(data): + """ + The xmltodict.unparse requires we modify the shape of the input dictionary slightly. Instead of a dict of the form: + {'key': ['value1', 'value2']} + We must provide: + {'key': {'item': ['value1', 'value2']}} + """ + if isinstance(data, dict): + ret = {} + for key in data: + ret[key] = itemize(data[key]) + return ret + elif isinstance(data, list): + return {'item': [itemize(value) for value in data]} + else: + return data + + class RedshiftResponse(BaseResponse): @property @@ -36,7 +55,7 @@ class RedshiftResponse(BaseResponse): if self.request_json: return json.dumps(response) else: - xml = dicttoxml.dicttoxml(response, attr_type=False, root=False) + xml = xmltodict.unparse(itemize(response), full_document=False) return xml.decode("utf-8") def call_action(self): diff --git a/setup.py b/setup.py index 378119925..0770c098f 100755 --- a/setup.py +++ b/setup.py @@ -13,7 +13,6 @@ install_requires = [ "cryptography>=2.0.0", "requests>=2.5", "xmltodict", - "dicttoxml", "six>1.9", "werkzeug", "pyaml", From 95a4bd5a7b4d73de8efcc2834b5543a3baef26cc Mon Sep 17 00:00:00 2001 From: Jack Danger Date: Mon, 2 Oct 2017 15:25:02 -0700 Subject: [PATCH 2/2] supporting python 3 --- moto/redshift/responses.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/moto/redshift/responses.py b/moto/redshift/responses.py index 58983310f..a320f9cae 100644 --- a/moto/redshift/responses.py +++ b/moto/redshift/responses.py @@ -56,7 +56,9 @@ class RedshiftResponse(BaseResponse): return json.dumps(response) else: xml = xmltodict.unparse(itemize(response), full_document=False) - return xml.decode("utf-8") + if hasattr(xml, 'decode'): + xml = xml.decode('utf-8') + return xml def call_action(self): status, headers, body = super(RedshiftResponse, self).call_action()