1"""Codec for quoted-printable encoding. 2 3This codec de/encodes from bytes to bytes. 4""" 5 6import codecs 7import quopri 8from io import BytesIO 9 10def quopri_encode(input, errors='strict'): 11 assert errors == 'strict' 12 f = BytesIO(input) 13 g = BytesIO() 14 quopri.encode(f, g, quotetabs=True) 15 return (g.getvalue(), len(input)) 16 17def quopri_decode(input, errors='strict'): 18 assert errors == 'strict' 19 f = BytesIO(input) 20 g = BytesIO() 21 quopri.decode(f, g) 22 return (g.getvalue(), len(input)) 23 24class Codec(codecs.Codec): 25 def encode(self, input, errors='strict'): 26 return quopri_encode(input, errors) 27 def decode(self, input, errors='strict'): 28 return quopri_decode(input, errors) 29 30class IncrementalEncoder(codecs.IncrementalEncoder): 31 def encode(self, input, final=False): 32 return quopri_encode(input, self.errors)[0] 33 34class IncrementalDecoder(codecs.IncrementalDecoder): 35 def decode(self, input, final=False): 36 return quopri_decode(input, self.errors)[0] 37 38class StreamWriter(Codec, codecs.StreamWriter): 39 charbuffertype = bytes 40 41class StreamReader(Codec, codecs.StreamReader): 42 charbuffertype = bytes 43 44# encodings module API 45 46def getregentry(): 47 return codecs.CodecInfo( 48 name='quopri', 49 encode=quopri_encode, 50 decode=quopri_decode, 51 incrementalencoder=IncrementalEncoder, 52 incrementaldecoder=IncrementalDecoder, 53 streamwriter=StreamWriter, 54 streamreader=StreamReader, 55 _is_text_encoding=False, 56 ) 57