| 1 | """Codec for quoted-printable encoding.
|
|---|
| 2 |
|
|---|
| 3 | Like base64 and rot13, this returns Python strings, not Unicode.
|
|---|
| 4 | """
|
|---|
| 5 |
|
|---|
| 6 | import codecs, quopri
|
|---|
| 7 | try:
|
|---|
| 8 | from cStringIO import StringIO
|
|---|
| 9 | except ImportError:
|
|---|
| 10 | from StringIO import StringIO
|
|---|
| 11 |
|
|---|
| 12 | def quopri_encode(input, errors='strict'):
|
|---|
| 13 | """Encode the input, returning a tuple (output object, length consumed).
|
|---|
| 14 |
|
|---|
| 15 | errors defines the error handling to apply. It defaults to
|
|---|
| 16 | 'strict' handling which is the only currently supported
|
|---|
| 17 | error handling for this codec.
|
|---|
| 18 |
|
|---|
| 19 | """
|
|---|
| 20 | assert errors == 'strict'
|
|---|
| 21 | f = StringIO(input)
|
|---|
| 22 | g = StringIO()
|
|---|
| 23 | quopri.encode(f, g, 1)
|
|---|
| 24 | output = g.getvalue()
|
|---|
| 25 | return (output, len(input))
|
|---|
| 26 |
|
|---|
| 27 | def quopri_decode(input, errors='strict'):
|
|---|
| 28 | """Decode the input, returning a tuple (output object, length consumed).
|
|---|
| 29 |
|
|---|
| 30 | errors defines the error handling to apply. It defaults to
|
|---|
| 31 | 'strict' handling which is the only currently supported
|
|---|
| 32 | error handling for this codec.
|
|---|
| 33 |
|
|---|
| 34 | """
|
|---|
| 35 | assert errors == 'strict'
|
|---|
| 36 | f = StringIO(input)
|
|---|
| 37 | g = StringIO()
|
|---|
| 38 | quopri.decode(f, g)
|
|---|
| 39 | output = g.getvalue()
|
|---|
| 40 | return (output, len(input))
|
|---|
| 41 |
|
|---|
| 42 | class Codec(codecs.Codec):
|
|---|
| 43 |
|
|---|
| 44 | def encode(self, input,errors='strict'):
|
|---|
| 45 | return quopri_encode(input,errors)
|
|---|
| 46 | def decode(self, input,errors='strict'):
|
|---|
| 47 | return quopri_decode(input,errors)
|
|---|
| 48 |
|
|---|
| 49 | class IncrementalEncoder(codecs.IncrementalEncoder):
|
|---|
| 50 | def encode(self, input, final=False):
|
|---|
| 51 | return quopri_encode(input, self.errors)[0]
|
|---|
| 52 |
|
|---|
| 53 | class IncrementalDecoder(codecs.IncrementalDecoder):
|
|---|
| 54 | def decode(self, input, final=False):
|
|---|
| 55 | return quopri_decode(input, self.errors)[0]
|
|---|
| 56 |
|
|---|
| 57 | class StreamWriter(Codec, codecs.StreamWriter):
|
|---|
| 58 | pass
|
|---|
| 59 |
|
|---|
| 60 | class StreamReader(Codec,codecs.StreamReader):
|
|---|
| 61 | pass
|
|---|
| 62 |
|
|---|
| 63 | # encodings module API
|
|---|
| 64 |
|
|---|
| 65 | def getregentry():
|
|---|
| 66 | return codecs.CodecInfo(
|
|---|
| 67 | name='quopri',
|
|---|
| 68 | encode=quopri_encode,
|
|---|
| 69 | decode=quopri_decode,
|
|---|
| 70 | incrementalencoder=IncrementalEncoder,
|
|---|
| 71 | incrementaldecoder=IncrementalDecoder,
|
|---|
| 72 | streamwriter=StreamWriter,
|
|---|
| 73 | streamreader=StreamReader,
|
|---|
| 74 | )
|
|---|