summaryrefslogtreecommitdiff
path: root/src/webob/acceptparse.py
diff options
context:
space:
mode:
authorIra Lun <sammyrosajoe@gmail.com>2017-07-18 16:40:00 +0100
committerIra Lun <sammyrosajoe@gmail.com>2017-07-18 16:40:00 +0100
commit009374d8411f35bdb463d3e238d4cd3cb99fe5cb (patch)
treef740e2156f1c0f520d22a662f93f2a38879908e0 /src/webob/acceptparse.py
parent10a89f2714718abeaa31cdc0964239e4b679de07 (diff)
downloadwebob-009374d8411f35bdb463d3e238d4cd3cb99fe5cb.tar.gz
Add __str__ and tests for the three AcceptLanguage classes.
Accept.__str__ returns a tidied up version of the header value, and AcceptLanguageValidHeader.__str__ does the same. NilAccept.__str__ returns ''; but given that the corresponding Accept.__str__ returns a tidied up version of the header value, returning '' would be misleading when the header is not empty, but rather is not found in the request. We also need to return a different value to indicate an invalid header, which cannot be parsed and tidied up. So we instead remove the ambiguity with '<no header in request>' and '<invalid header value>'.
Diffstat (limited to 'src/webob/acceptparse.py')
-rw-r--r--src/webob/acceptparse.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/webob/acceptparse.py b/src/webob/acceptparse.py
index c8f4172..5fa8f0d 100644
--- a/src/webob/acceptparse.py
+++ b/src/webob/acceptparse.py
@@ -459,6 +459,26 @@ class AcceptLanguageValidHeader(AcceptLanguage):
self.header_value,
)
+ def __str__(self):
+ """
+ Return a tidied up version of the header value.
+
+ e.g. If the ``header_value`` is ``', \t,de;q=0.000 \t, es;q=1.000, zh,
+ jp;q=0.210 ,'``, ``str(instance)`` returns ``'de;q=0, es, zh,
+ jp;q=0.21'``.
+
+ """
+ result = []
+ for range_, qvalue in self.parsed:
+ if qvalue == 1.0:
+ item = range_
+ elif qvalue == 0.0:
+ item = '{};q=0'.format(range_)
+ else:
+ item = '{};q={}'.format(range_, qvalue)
+ result.append(item)
+ return ', '.join(result)
+
def _old_match(self, mask, item):
"""
Return whether a language tag matches a language range.
@@ -1445,6 +1465,9 @@ class AcceptLanguageNoHeader(_AcceptLanguageInvalidOrNoHeader):
def __repr__(self):
return '{}()'.format(self.__class__.__name__)
+ def __str__(self):
+ return '<no header in request>'
+
class AcceptLanguageInvalidHeader(_AcceptLanguageInvalidOrNoHeader):
"""
@@ -1479,6 +1502,9 @@ class AcceptLanguageInvalidHeader(_AcceptLanguageInvalidOrNoHeader):
self.header_value,
)
+ def __str__(self):
+ return '<invalid header value>'
+
class MIMEAccept(Accept):
"""