2005-05-01 | paulb | file changeset files shortlog | [project @ 2005-05-01 18:26:19 by paulb] Added a note about .pyc files. |
paulb@168 | 1 | #!/usr/bin/env python |
paulb@168 | 2 | |
paulb@168 | 3 | """ |
paulb@168 | 4 | Response helper classes. |
paulb@168 | 5 | """ |
paulb@168 | 6 | |
paulb@168 | 7 | class ConvertingStream: |
paulb@168 | 8 | |
paulb@168 | 9 | """ |
paulb@168 | 10 | A converting stream which converts Unicode text to plain strings. |
paulb@168 | 11 | """ |
paulb@168 | 12 | |
paulb@168 | 13 | def __init__(self, stream, encoding): |
paulb@168 | 14 | |
paulb@168 | 15 | "Set the actual response 'stream' and the desired output 'encoding'." |
paulb@168 | 16 | |
paulb@168 | 17 | self.stream = stream |
paulb@168 | 18 | self.encoding = encoding |
paulb@168 | 19 | |
paulb@168 | 20 | def write(self, text): |
paulb@168 | 21 | |
paulb@168 | 22 | "Write the given 'text', either a plain string or a Unicode object." |
paulb@168 | 23 | |
paulb@168 | 24 | if type(text) == type(u""): |
paulb@168 | 25 | self.stream.write(text.encode(self.encoding)) |
paulb@168 | 26 | else: |
paulb@168 | 27 | self.stream.write(text) |
paulb@168 | 28 | |
paulb@168 | 29 | # vim: tabstop=4 expandtab shiftwidth=4 |