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