From 2e67031ffb981ae1a47043cd48d50470eb6dc0b6 Mon Sep 17 00:00:00 2001 From: Andy Smith Date: Mon, 25 Oct 2010 19:21:09 +0900 Subject: [PATCH] Duplicate the two trivial escaping functions remaining from tornado's code and remove the dependency. --- nova/objectstore/handler.py | 12 ++++++------ nova/utils.py | 25 +++++++++++++++++++++++++ tools/pip-requires | 1 - 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/nova/objectstore/handler.py b/nova/objectstore/handler.py index 074eea601c64..b26906001437 100644 --- a/nova/objectstore/handler.py +++ b/nova/objectstore/handler.py @@ -44,7 +44,6 @@ import multiprocessing import os import urllib -from tornado import escape from twisted.application import internet from twisted.application import service from twisted.web import error @@ -55,6 +54,7 @@ from twisted.web import static from nova import context from nova import exception from nova import flags +from nova import utils from nova.auth import manager from nova.objectstore import bucket from nova.objectstore import image @@ -70,10 +70,10 @@ def render_xml(request, value): name = value.keys()[0] request.write('\n') - request.write('<' + escape.utf8(name) + + request.write('<' + utils.utf8(name) + ' xmlns="http://doc.s3.amazonaws.com/2006-03-01">') _render_parts(value.values()[0], request.write) - request.write('') + request.write('') request.finish() @@ -87,7 +87,7 @@ def finish(request, content=None): def _render_parts(value, write_cb): """Helper method to render different Python objects to XML""" if isinstance(value, basestring): - write_cb(escape.xhtml_escape(value)) + write_cb(utils.xhtml_escape(value)) elif isinstance(value, int) or isinstance(value, long): write_cb(str(value)) elif isinstance(value, datetime.datetime): @@ -97,9 +97,9 @@ def _render_parts(value, write_cb): if not isinstance(subvalue, list): subvalue = [subvalue] for subsubvalue in subvalue: - write_cb('<' + escape.utf8(name) + '>') + write_cb('<' + utils.utf8(name) + '>') _render_parts(subsubvalue, write_cb) - write_cb('') + write_cb('') else: raise Exception("Unknown S3 value type %r", value) diff --git a/nova/utils.py b/nova/utils.py index 7683fc9f458a..e58302c11bd3 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -28,6 +28,7 @@ import random import subprocess import socket import sys +from xml.sax import saxutils from twisted.internet.threads import deferToThread @@ -212,3 +213,27 @@ def deferredToThread(f): def g(*args, **kwargs): return deferToThread(f, *args, **kwargs) return g + + +def xhtml_escape(value): + """Escapes a string so it is valid within XML or XHTML. + + Code is directly from the utf8 function in + http://github.com/facebook/tornado/blob/master/tornado/escape.py + + """ + return saxutils.escape(value, {'"': """}) + + +def utf8(value): + """Try to turn a string into utf-8 if possible. + + Code is directly from the utf8 function in + http://github.com/facebook/tornado/blob/master/tornado/escape.py + + """ + if isinstance(value, unicode): + return value.encode("utf-8") + assert isinstance(value, str) + return value + diff --git a/tools/pip-requires b/tools/pip-requires index c76fad86f530..548073326560 100644 --- a/tools/pip-requires +++ b/tools/pip-requires @@ -13,7 +13,6 @@ python-daemon==1.5.5 python-gflags==1.3 redis==2.0.0 routes==1.12.3 -tornado==1.0 WebOb==0.9.8 wsgiref==0.1.2 zope.interface==3.6.1