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