paulb@168 | 1 | #!/usr/bin/env python |
paulb@168 | 2 | |
paulb@168 | 3 | """ |
paulb@168 | 4 | Response helper classes. |
paulb@403 | 5 | |
paulb@403 | 6 | Copyright (C) 2004, 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@489 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@168 | 21 | """ |
paulb@168 | 22 | |
paulb@168 | 23 | class ConvertingStream: |
paulb@168 | 24 | |
paulb@168 | 25 | """ |
paulb@168 | 26 | A converting stream which converts Unicode text to plain strings. |
paulb@168 | 27 | """ |
paulb@168 | 28 | |
paulb@168 | 29 | def __init__(self, stream, encoding): |
paulb@168 | 30 | |
paulb@168 | 31 | "Set the actual response 'stream' and the desired output 'encoding'." |
paulb@168 | 32 | |
paulb@168 | 33 | self.stream = stream |
paulb@168 | 34 | self.encoding = encoding |
paulb@168 | 35 | |
paulb@168 | 36 | def write(self, text): |
paulb@168 | 37 | |
paulb@168 | 38 | "Write the given 'text', either a plain string or a Unicode object." |
paulb@168 | 39 | |
paulb@168 | 40 | if type(text) == type(u""): |
paulb@168 | 41 | self.stream.write(text.encode(self.encoding)) |
paulb@168 | 42 | else: |
paulb@168 | 43 | self.stream.write(text) |
paulb@168 | 44 | |
paulb@168 | 45 | # vim: tabstop=4 expandtab shiftwidth=4 |