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