183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh"""Codec for quoted-printable encoding. 283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew HsiehLike base64 and rot13, this returns Python strings, not Unicode. 483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh""" 583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehimport codecs, quopri 783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehtry: 883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh from cStringIO import StringIO 983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehexcept ImportError: 1083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh from StringIO import StringIO 1183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 1283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehdef quopri_encode(input, errors='strict'): 1383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh """Encode the input, returning a tuple (output object, length consumed). 1483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 1583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh errors defines the error handling to apply. It defaults to 1683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 'strict' handling which is the only currently supported 1783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh error handling for this codec. 1883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 1983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh """ 2083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh assert errors == 'strict' 2183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh # using str() because of cStringIO's Unicode undesired Unicode behavior. 2283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh f = StringIO(str(input)) 2383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh g = StringIO() 2483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh quopri.encode(f, g, 1) 2583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh output = g.getvalue() 2683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh return (output, len(input)) 2783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 2883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehdef quopri_decode(input, errors='strict'): 2983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh """Decode the input, returning a tuple (output object, length consumed). 3083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 3183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh errors defines the error handling to apply. It defaults to 3283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 'strict' handling which is the only currently supported 3383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh error handling for this codec. 3483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 3583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh """ 3683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh assert errors == 'strict' 3783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh f = StringIO(str(input)) 3883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh g = StringIO() 3983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh quopri.decode(f, g) 4083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh output = g.getvalue() 4183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh return (output, len(input)) 4283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 4383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass Codec(codecs.Codec): 4483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 4583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def encode(self, input,errors='strict'): 4683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh return quopri_encode(input,errors) 4783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def decode(self, input,errors='strict'): 4883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh return quopri_decode(input,errors) 4983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 5083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass IncrementalEncoder(codecs.IncrementalEncoder): 5183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def encode(self, input, final=False): 5283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh return quopri_encode(input, self.errors)[0] 5383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 5483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass IncrementalDecoder(codecs.IncrementalDecoder): 5583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def decode(self, input, final=False): 5683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh return quopri_decode(input, self.errors)[0] 5783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 5883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass StreamWriter(Codec, codecs.StreamWriter): 5983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh pass 6083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 6183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass StreamReader(Codec,codecs.StreamReader): 6283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh pass 6383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 6483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh# encodings module API 6583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 6683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehdef getregentry(): 6783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh return codecs.CodecInfo( 6883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh name='quopri', 6983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh encode=quopri_encode, 7083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh decode=quopri_decode, 7183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh incrementalencoder=IncrementalEncoder, 7283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh incrementaldecoder=IncrementalDecoder, 7383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh streamwriter=StreamWriter, 7483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh streamreader=StreamReader, 7583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh ) 76