~blamar/+junk/openstack-api-arrrg

« back to all changes in this revision

Viewing changes to vendor/tornado/tornado/escape.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# Copyright 2009 Facebook
 
4
#
 
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
 
6
# not use this file except in compliance with the License. You may obtain
 
7
# a copy of the License at
 
8
#
 
9
#     http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
# Unless required by applicable law or agreed to in writing, software
 
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
14
# License for the specific language governing permissions and limitations
 
15
# under the License.
 
16
 
 
17
"""Escaping/unescaping methods for HTML, JSON, URLs, and others."""
 
18
 
 
19
import htmlentitydefs
 
20
import re
 
21
import xml.sax.saxutils
 
22
import urllib
 
23
 
 
24
try:
 
25
    import json
 
26
    assert hasattr(json, "loads") and hasattr(json, "dumps")
 
27
    _json_decode = lambda s: json.loads(s)
 
28
    _json_encode = lambda v: json.dumps(v)
 
29
except:
 
30
    try:
 
31
        import simplejson
 
32
        _json_decode = lambda s: simplejson.loads(_unicode(s))
 
33
        _json_encode = lambda v: simplejson.dumps(v)
 
34
    except ImportError:
 
35
        try:
 
36
            # For Google AppEngine
 
37
            from django.utils import simplejson
 
38
            _json_decode = lambda s: simplejson.loads(_unicode(s))
 
39
            _json_encode = lambda v: simplejson.dumps(v)
 
40
        except ImportError:
 
41
            raise Exception("A JSON parser is required, e.g., simplejson at "
 
42
                            "http://pypi.python.org/pypi/simplejson/")
 
43
 
 
44
 
 
45
def xhtml_escape(value):
 
46
    """Escapes a string so it is valid within XML or XHTML."""
 
47
    return utf8(xml.sax.saxutils.escape(value, {'"': """}))
 
48
 
 
49
 
 
50
def xhtml_unescape(value):
 
51
    """Un-escapes an XML-escaped string."""
 
52
    return re.sub(r"&(#?)(\w+?);", _convert_entity, _unicode(value))
 
53
 
 
54
 
 
55
def json_encode(value):
 
56
    """JSON-encodes the given Python object."""
 
57
    return _json_encode(value)
 
58
 
 
59
 
 
60
def json_decode(value):
 
61
    """Returns Python objects for the given JSON string."""
 
62
    return _json_decode(value)
 
63
 
 
64
 
 
65
def squeeze(value):
 
66
    """Replace all sequences of whitespace chars with a single space."""
 
67
    return re.sub(r"[\x00-\x20]+", " ", value).strip()
 
68
 
 
69
 
 
70
def url_escape(value):
 
71
    """Returns a valid URL-encoded version of the given value."""
 
72
    return urllib.quote_plus(utf8(value))
 
73
 
 
74
 
 
75
def url_unescape(value):
 
76
    """Decodes the given value from a URL."""
 
77
    return _unicode(urllib.unquote_plus(value))
 
78
 
 
79
 
 
80
def utf8(value):
 
81
    if isinstance(value, unicode):
 
82
        return value.encode("utf-8")
 
83
    assert isinstance(value, str)
 
84
    return value
 
85
 
 
86
 
 
87
def _unicode(value):
 
88
    if isinstance(value, str):
 
89
        return value.decode("utf-8")
 
90
    assert isinstance(value, unicode)
 
91
    return value
 
92
 
 
93
 
 
94
def _convert_entity(m):
 
95
    if m.group(1) == "#":
 
96
        try:
 
97
            return unichr(int(m.group(2)))
 
98
        except ValueError:
 
99
            return "&#%s;" % m.group(2)
 
100
    try:
 
101
        return _HTML_UNICODE_MAP[m.group(2)]
 
102
    except KeyError:
 
103
        return "&%s;" % m.group(2)
 
104
 
 
105
 
 
106
def _build_unicode_map():
 
107
    unicode_map = {}
 
108
    for name, value in htmlentitydefs.name2codepoint.iteritems():
 
109
        unicode_map[name] = unichr(value)
 
110
    return unicode_map
 
111
 
 
112
_HTML_UNICODE_MAP = _build_unicode_map()