diff options
author | Ian Bicking <ian@ianbicking.org> | 2007-08-10 23:19:35 +0000 |
---|---|---|
committer | Ian Bicking <ian@ianbicking.org> | 2007-08-10 23:19:35 +0000 |
commit | ffcb5d33eef6eb16ea8750575a96e2a766236903 (patch) | |
tree | 54e1ddea272c60a7034144f2be5a6d5fbdede6a7 | |
parent | 9f491dfbe78884d06bce09f6945c08056b73c569 (diff) | |
download | paste-git-ffcb5d33eef6eb16ea8750575a96e2a766236903.tar.gz |
Port WSGIResponse.content_type and .charset from WebOb
-rw-r--r-- | docs/news.txt | 4 | ||||
-rw-r--r-- | paste/wsgiwrappers.py | 74 | ||||
-rw-r--r-- | tests/test_wsgiwrappers.py | 2 |
3 files changed, 80 insertions, 0 deletions
diff --git a/docs/news.txt b/docs/news.txt index 4f99e54..76eed30 100644 --- a/docs/news.txt +++ b/docs/news.txt @@ -17,6 +17,10 @@ svn trunk * Try to encode any unicode input to ``paste.auth.auth_tkt`` +* ``paste.wsgiwrappers.WSGIResponse`` now has a ``.content_type`` + attribute (that does not include parameters), and a ``.charset`` + attribute (that gets the charset parameter). + 1.4 --- diff --git a/paste/wsgiwrappers.py b/paste/wsgiwrappers.py index 99a57fc..a4b68de 100644 --- a/paste/wsgiwrappers.py +++ b/paste/wsgiwrappers.py @@ -458,6 +458,80 @@ class WSGIResponse(object): 'is an iterator)' % self.__class__.__name__ return sum([len(chunk) for chunk in self._iter]) + ######################################## + ## Content-type and charset + + def charset__get(self): + """ + Get/set the charset (in the Content-Type) + """ + header = self.headers.get('content-type') + if not header: + return None + match = _CHARSET_RE.search(header) + if match: + return match.group(1) + return None + + def charset__set(self, charset): + if charset is None: + del self.charset + return + try: + header = self.headers.pop('content-type') + except KeyError: + raise AttributeError( + "You cannot set the charset when on content-type is defined") + match = _CHARSET_RE.search(header) + if match: + header = header[:match.start()] + header[match.end():] + header += '; charset=%s' % charset + self.headers['content-type'] = header + + def charset__del(self): + try: + header = self.headers.pop('content-type') + except KeyError: + # Don't need to remove anything + return + match = _CHARSET_RE.search(header) + if match: + header = header[:match.start()] + header[match.end():] + self.headers['content-type'] = header + + charset = property(charset__get, charset__set, charset__del, doc=charset__get.__doc__) + + def content_type__get(self): + """ + Get/set the Content-Type header (or None), *without* the + charset or any parameters. + + If you include parameters (or ``;`` at all) when setting the + content_type, any existing parameters will be deleted; + otherwise they will be preserved. + """ + header = self.headers.get('content-type') + if not header: + return None + return header.split(';', 1)[0] + + def content_type__set(self, value): + if ';' not in value: + header = self.headers.get('content-type', '') + if ';' in header: + params = header.split(';', 1)[1] + value += ';' + params + self.headers['content-type'] = value + + def content_type__del(self): + try: + del self.headers['content-type'] + except KeyError: + pass + + content_type = property(content_type__get, content_type__set, + content_type__del, doc=content_type__get.__doc__) + ## @@ I'd love to remove this, but paste.httpexceptions.get_exception ## doesn't seem to work... # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html diff --git a/tests/test_wsgiwrappers.py b/tests/test_wsgiwrappers.py index 4a98ca3..b552f86 100644 --- a/tests/test_wsgiwrappers.py +++ b/tests/test_wsgiwrappers.py @@ -99,6 +99,8 @@ def test_wsgirequest_charset_fileupload(): def test_wsgiresponse_charset(): response = WSGIResponse(mimetype='text/html; charset=UTF-8') + assert response.content_type == 'text/html' + assert response.charset == 'UTF-8' response.write(u'test') response.write(u'test2') response.write('test3') |