Duplicate the two trivial escaping functions remaining from tornado's code and remove the dependency.

This commit is contained in:
Andy Smith 2010-10-25 19:21:09 +09:00
parent 81e8c5256c
commit 2e67031ffb
3 changed files with 31 additions and 7 deletions

View File

@ -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('<?xml version="1.0" encoding="UTF-8"?>\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('</' + escape.utf8(name) + '>')
request.write('</' + utils.utf8(name) + '>')
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('</' + escape.utf8(name) + '>')
write_cb('</' + utils.utf8(name) + '>')
else:
raise Exception("Unknown S3 value type %r", value)

View File

@ -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, {'"': "&quot;"})
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

View File

@ -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