jpayne@69: """Codec for quoted-printable encoding. jpayne@69: jpayne@69: This codec de/encodes from bytes to bytes. jpayne@69: """ jpayne@69: jpayne@69: import codecs jpayne@69: import quopri jpayne@69: from io import BytesIO jpayne@69: jpayne@69: def quopri_encode(input, errors='strict'): jpayne@69: assert errors == 'strict' jpayne@69: f = BytesIO(input) jpayne@69: g = BytesIO() jpayne@69: quopri.encode(f, g, quotetabs=True) jpayne@69: return (g.getvalue(), len(input)) jpayne@69: jpayne@69: def quopri_decode(input, errors='strict'): jpayne@69: assert errors == 'strict' jpayne@69: f = BytesIO(input) jpayne@69: g = BytesIO() jpayne@69: quopri.decode(f, g) jpayne@69: return (g.getvalue(), len(input)) jpayne@69: jpayne@69: class Codec(codecs.Codec): jpayne@69: def encode(self, input, errors='strict'): jpayne@69: return quopri_encode(input, errors) jpayne@69: def decode(self, input, errors='strict'): jpayne@69: return quopri_decode(input, errors) jpayne@69: jpayne@69: class IncrementalEncoder(codecs.IncrementalEncoder): jpayne@69: def encode(self, input, final=False): jpayne@69: return quopri_encode(input, self.errors)[0] jpayne@69: jpayne@69: class IncrementalDecoder(codecs.IncrementalDecoder): jpayne@69: def decode(self, input, final=False): jpayne@69: return quopri_decode(input, self.errors)[0] jpayne@69: jpayne@69: class StreamWriter(Codec, codecs.StreamWriter): jpayne@69: charbuffertype = bytes jpayne@69: jpayne@69: class StreamReader(Codec, codecs.StreamReader): jpayne@69: charbuffertype = bytes jpayne@69: jpayne@69: # encodings module API jpayne@69: jpayne@69: def getregentry(): jpayne@69: return codecs.CodecInfo( jpayne@69: name='quopri', jpayne@69: encode=quopri_encode, jpayne@69: decode=quopri_decode, jpayne@69: incrementalencoder=IncrementalEncoder, jpayne@69: incrementaldecoder=IncrementalDecoder, jpayne@69: streamwriter=StreamWriter, jpayne@69: streamreader=StreamReader, jpayne@69: _is_text_encoding=False, jpayne@69: )