Back to index

python3.2  3.2.2
quopri_codec.py
Go to the documentation of this file.
00001 """Codec for quoted-printable encoding.
00002 
00003 This codec de/encodes from bytes to bytes and is therefore usable with
00004 bytes.transform() and bytes.untransform().
00005 """
00006 
00007 import codecs
00008 import quopri
00009 from io import BytesIO
00010 
00011 def quopri_encode(input, errors='strict'):
00012     assert errors == 'strict'
00013     f = BytesIO(input)
00014     g = BytesIO()
00015     quopri.encode(f, g, 1)
00016     return (g.getvalue(), len(input))
00017 
00018 def quopri_decode(input, errors='strict'):
00019     assert errors == 'strict'
00020     f = BytesIO(input)
00021     g = BytesIO()
00022     quopri.decode(f, g)
00023     return (g.getvalue(), len(input))
00024 
00025 class Codec(codecs.Codec):
00026     def encode(self, input, errors='strict'):
00027         return quopri_encode(input, errors)
00028     def decode(self, input, errors='strict'):
00029         return quopri_decode(input, errors)
00030 
00031 class IncrementalEncoder(codecs.IncrementalEncoder):
00032     def encode(self, input, final=False):
00033         return quopri_encode(input, self.errors)[0]
00034 
00035 class IncrementalDecoder(codecs.IncrementalDecoder):
00036     def decode(self, input, final=False):
00037         return quopri_decode(input, self.errors)[0]
00038 
00039 class StreamWriter(Codec, codecs.StreamWriter):
00040     charbuffertype = bytes
00041 
00042 class StreamReader(Codec, codecs.StreamReader):
00043     charbuffertype = bytes
00044 
00045 # encodings module API
00046 
00047 def getregentry():
00048     return codecs.CodecInfo(
00049         name='quopri',
00050         encode=quopri_encode,
00051         decode=quopri_decode,
00052         incrementalencoder=IncrementalEncoder,
00053         incrementaldecoder=IncrementalDecoder,
00054         streamwriter=StreamWriter,
00055         streamreader=StreamReader,
00056     )