summaryrefslogtreecommitdiff
path: root/src/webob/acceptparse.py
diff options
context:
space:
mode:
authorIra Lun <sammyrosajoe@gmail.com>2017-08-29 20:57:40 +0100
committerIra Lun <sammyrosajoe@gmail.com>2017-08-29 20:57:40 +0100
commit21e1227d6a14c0e425bcfee1a95cd31a9f1c981a (patch)
treead14a998392b9abbc57ded27593458b980c806be /src/webob/acceptparse.py
parentb4778929c532632daedc1aa1e81514868f0ca08d (diff)
downloadwebob-21e1227d6a14c0e425bcfee1a95cd31a9f1c981a.tar.gz
Add accept_charset_property, docs and tests.
Diffstat (limited to 'src/webob/acceptparse.py')
-rw-r--r--src/webob/acceptparse.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/webob/acceptparse.py b/src/webob/acceptparse.py
index db44dce..2844d4a 100644
--- a/src/webob/acceptparse.py
+++ b/src/webob/acceptparse.py
@@ -1191,6 +1191,65 @@ def create_accept_charset_header(header_value):
return AcceptCharsetInvalidHeader(header_value=header_value)
+def accept_charset_property():
+ doc = """
+ Property representing the ``Accept-Charset`` header.
+
+ (:rfc:`RFC 7231, section 5.3.3 <7231#section-5.3.3>`)
+
+ The header value in the request environ is parsed and a new object
+ representing the header is created every time we *get* the value of the
+ property. (*set* and *del* change the header value in the request
+ environ, and do not involve parsing.)
+ """
+
+ ENVIRON_KEY = 'HTTP_ACCEPT_CHARSET'
+
+ def fget(request):
+ """Get an object representing the header in the request."""
+ return create_accept_charset_header(
+ header_value=request.environ.get(ENVIRON_KEY)
+ )
+
+ def fset(request, value):
+ """
+ Set the corresponding key in the request environ.
+
+ `value` can be:
+
+ * ``None``
+ * a ``str`` header value
+ * a ``dict``, where keys are charsets and values are qvalues
+ * a ``tuple`` or ``list``, where each item is a charset ``str`` or a
+ ``tuple`` or ``list`` (charset, qvalue) pair (``str``\ s and pairs
+ can be mixed within the ``tuple`` or ``list``)
+ * an :class:`AcceptCharsetValidHeader`, :class:`AcceptCharsetNoHeader`,
+ or :class:`AcceptCharsetInvalidHeader` instance
+ * object of any other type that returns a value for ``__str__``
+ """
+ if value is None or isinstance(value, AcceptCharsetNoHeader):
+ fdel(request=request)
+ else:
+ if isinstance(
+ value, (AcceptCharsetValidHeader, AcceptCharsetInvalidHeader)
+ ):
+ header_value = value.header_value
+ else:
+ header_value = AcceptCharset._python_value_to_header_str(
+ value=value,
+ )
+ request.environ[ENVIRON_KEY] = header_value
+
+ def fdel(request):
+ """Delete the corresponding key from the request environ."""
+ try:
+ del request.environ[ENVIRON_KEY]
+ except KeyError:
+ pass
+
+ return property(fget, fset, fdel, textwrap.dedent(doc))
+
+
class AcceptEncoding(object):
"""
Represent an ``Accept-Encoding`` header.