diff options
| -rw-r--r-- | tests/test_descriptors.py | 4 | ||||
| -rw-r--r-- | webob/__init__.py | 27 | ||||
| -rw-r--r-- | webob/acceptparse.py | 7 | ||||
| -rw-r--r-- | webob/descriptors.py | 15 | ||||
| -rw-r--r-- | webob/etag.py | 6 | ||||
| -rw-r--r-- | webob/util.py | 39 |
6 files changed, 42 insertions, 56 deletions
diff --git a/tests/test_descriptors.py b/tests/test_descriptors.py index b0dc969..d83cd18 100644 --- a/tests/test_descriptors.py +++ b/tests/test_descriptors.py @@ -110,9 +110,9 @@ def test_upath_property_fset(): def test_header_getter_doc(): from webob.descriptors import header_getter - desc = header_getter('AHEADER', '14.3') + desc = header_getter('X-Header', '14.3') assert 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3' in desc.__doc__ - assert '``AHEADER`` header' in desc.__doc__ + assert '``X-Header`` header' in desc.__doc__ def test_header_getter_fget(): from webob.descriptors import header_getter diff --git a/webob/__init__.py b/webob/__init__.py index abcd9d7..ec6b7cb 100644 --- a/webob/__init__.py +++ b/webob/__init__.py @@ -1,7 +1,7 @@ -import cgi from webob.datetime_utils import * from webob.request import * from webob.response import * +from webob.util import html_escape __all__ = [ 'Request', 'Response', @@ -9,31 +9,6 @@ __all__ = [ 'html_escape' ] -def html_escape(s): - """HTML-escape a string or object - - This converts any non-string objects passed into it to strings - (actually, using ``unicode()``). All values returned are - non-unicode strings (using ``&#num;`` entities for all non-ASCII - characters). - - None is treated specially, and returns the empty string. - """ - if s is None: - return '' - if hasattr(s, '__html__'): - return s.__html__() - if not isinstance(s, basestring): - if hasattr(s, '__unicode__'): - s = unicode(s) - else: - s = str(s) - s = cgi.escape(s, True) - if isinstance(s, unicode): - s = s.encode('ascii', 'xmlcharrefreplace') - return s - - BaseRequest.ResponseClass = Response Response.RequestClass = Request diff --git a/webob/acceptparse.py b/webob/acceptparse.py index cb25076..c74c254 100644 --- a/webob/acceptparse.py +++ b/webob/acceptparse.py @@ -10,7 +10,7 @@ exists, but this ignores them. """ import re, warnings -from webob.util import rfc_reference +from webob.util import header_docstring from webob.headers import _trans_name as header_to_key part_re = re.compile( @@ -349,10 +349,7 @@ def accept_property(header, rfc_section, AcceptClass=Accept, NilClass=NilAccept ): key = header_to_key(header) - doc = "Gets and sets and deletes the ``%s`` header %s." % ( - header, - rfc_reference(header, rfc_section) - ) + doc = header_docstring(header, rfc_section) #doc += " Converts it as a %s." % convert_name def fget(req): value = req.environ.get(key) diff --git a/webob/descriptors.py b/webob/descriptors.py index 065b1fe..791cfbf 100644 --- a/webob/descriptors.py +++ b/webob/descriptors.py @@ -5,9 +5,7 @@ from datetime import datetime, date from webob.byterange import Range, ContentRange from webob.etag import IfRange, NoIfRange from webob.datetime_utils import parse_date, serialize_date -from webob.util import rfc_reference -from webob.headers import _trans_key - +from webob.util import header_docstring CHARSET_RE = re.compile(r';\s*charset=([^;]*)', re.I) QUOTES_RE = re.compile('"(.*)"') @@ -18,11 +16,7 @@ _not_given = object() def environ_getter(key, default=_not_given, rfc_section=None): if rfc_section: - header = _trans_key(key) - doc = "Gets and sets the ``%s`` header %s." % ( - header, - rfc_reference(header, rfc_section) - ) + doc = header_docstring(key, rfc_section) else: doc = "Gets and sets the ``%s`` key in the environment." % key if default is _not_given: @@ -54,10 +48,7 @@ def upath_property(key): def header_getter(header, rfc_section): - doc = "Gets and sets and deletes the ``%s`` header %s." % ( - header, - rfc_reference(header, rfc_section) - ) + doc = header_docstring(header, rfc_section) key = header.lower() def fget(r): diff --git a/webob/etag.py b/webob/etag.py index b0274fe..b87eb34 100644 --- a/webob/etag.py +++ b/webob/etag.py @@ -5,15 +5,13 @@ Also If-Range parsing """ from webob.datetime_utils import * -from webob.util import rfc_reference +from webob.util import header_docstring __all__ = ['AnyETag', 'NoETag', 'ETagMatcher', 'IfRange', 'NoIfRange', 'etag_property'] def etag_property(key, default, rfc_section): - header = key[5:].title().replace('_', '-') - doc = "Gets and sets the ``%s`` header." % header - doc += rfc_reference(key, rfc_section) + doc = header_docstring(key, rfc_section) doc += " Converts it as a Etag." def fget(req): value = req.environ.get(key) diff --git a/webob/util.py b/webob/util.py index d796398..00b03f6 100644 --- a/webob/util.py +++ b/webob/util.py @@ -1,11 +1,36 @@ -def rfc_reference(header, section): - if not section: +import cgi +from webob.headers import _trans_key + +def html_escape(s): + """HTML-escape a string or object + + This converts any non-string objects passed into it to strings + (actually, using ``unicode()``). All values returned are + non-unicode strings (using ``&#num;`` entities for all non-ASCII + characters). + + None is treated specially, and returns the empty string. + """ + if s is None: return '' - major_section = section.split('.')[0] - link = 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec%s.html#sec%s' % (major_section, section) - #if header.startswith('HTTP_'): - # header = header[5:].title().replace('_', '-') - return "(`HTTP spec section %s <%s>`_)" % (section, link) + if hasattr(s, '__html__'): + return s.__html__() + if not isinstance(s, basestring): + if hasattr(s, '__unicode__'): + s = unicode(s) + else: + s = str(s) + s = cgi.escape(s, True) + if isinstance(s, unicode): + s = s.encode('ascii', 'xmlcharrefreplace') + return s +def header_docstring(header, rfc_section): + if header.isupper(): + header = _trans_key(header) + major_section = rfc_section.split('.')[0] + link = 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec%s.html#sec%s' % (major_section, rfc_section) + return "Gets and sets the ``%s`` header (`HTTP spec section %s <%s>`_)." \ + % (header, rfc_section, link) status_reasons = { # Status Codes |
