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