]>
Commit | Line | Data |
---|---|---|
3257aa99 DM |
1 | #!/usr/bin/env python\r |
2 | """ Python Character Mapping Codec for ROT13.\r | |
3 | \r | |
4 | See http://ucsub.colorado.edu/~kominek/rot13/ for details.\r | |
5 | \r | |
6 | Written by Marc-Andre Lemburg (mal@lemburg.com).\r | |
7 | \r | |
8 | """#"\r | |
9 | \r | |
10 | import codecs\r | |
11 | \r | |
12 | ### Codec APIs\r | |
13 | \r | |
14 | class Codec(codecs.Codec):\r | |
15 | \r | |
16 | def encode(self,input,errors='strict'):\r | |
17 | return codecs.charmap_encode(input,errors,encoding_map)\r | |
18 | \r | |
19 | def decode(self,input,errors='strict'):\r | |
20 | return codecs.charmap_decode(input,errors,decoding_map)\r | |
21 | \r | |
22 | class IncrementalEncoder(codecs.IncrementalEncoder):\r | |
23 | def encode(self, input, final=False):\r | |
24 | return codecs.charmap_encode(input,self.errors,encoding_map)[0]\r | |
25 | \r | |
26 | class IncrementalDecoder(codecs.IncrementalDecoder):\r | |
27 | def decode(self, input, final=False):\r | |
28 | return codecs.charmap_decode(input,self.errors,decoding_map)[0]\r | |
29 | \r | |
30 | class StreamWriter(Codec,codecs.StreamWriter):\r | |
31 | pass\r | |
32 | \r | |
33 | class StreamReader(Codec,codecs.StreamReader):\r | |
34 | pass\r | |
35 | \r | |
36 | ### encodings module API\r | |
37 | \r | |
38 | def getregentry():\r | |
39 | return codecs.CodecInfo(\r | |
40 | name='rot-13',\r | |
41 | encode=Codec().encode,\r | |
42 | decode=Codec().decode,\r | |
43 | incrementalencoder=IncrementalEncoder,\r | |
44 | incrementaldecoder=IncrementalDecoder,\r | |
45 | streamwriter=StreamWriter,\r | |
46 | streamreader=StreamReader,\r | |
47 | )\r | |
48 | \r | |
49 | ### Decoding Map\r | |
50 | \r | |
51 | decoding_map = codecs.make_identity_dict(range(256))\r | |
52 | decoding_map.update({\r | |
53 | 0x0041: 0x004e,\r | |
54 | 0x0042: 0x004f,\r | |
55 | 0x0043: 0x0050,\r | |
56 | 0x0044: 0x0051,\r | |
57 | 0x0045: 0x0052,\r | |
58 | 0x0046: 0x0053,\r | |
59 | 0x0047: 0x0054,\r | |
60 | 0x0048: 0x0055,\r | |
61 | 0x0049: 0x0056,\r | |
62 | 0x004a: 0x0057,\r | |
63 | 0x004b: 0x0058,\r | |
64 | 0x004c: 0x0059,\r | |
65 | 0x004d: 0x005a,\r | |
66 | 0x004e: 0x0041,\r | |
67 | 0x004f: 0x0042,\r | |
68 | 0x0050: 0x0043,\r | |
69 | 0x0051: 0x0044,\r | |
70 | 0x0052: 0x0045,\r | |
71 | 0x0053: 0x0046,\r | |
72 | 0x0054: 0x0047,\r | |
73 | 0x0055: 0x0048,\r | |
74 | 0x0056: 0x0049,\r | |
75 | 0x0057: 0x004a,\r | |
76 | 0x0058: 0x004b,\r | |
77 | 0x0059: 0x004c,\r | |
78 | 0x005a: 0x004d,\r | |
79 | 0x0061: 0x006e,\r | |
80 | 0x0062: 0x006f,\r | |
81 | 0x0063: 0x0070,\r | |
82 | 0x0064: 0x0071,\r | |
83 | 0x0065: 0x0072,\r | |
84 | 0x0066: 0x0073,\r | |
85 | 0x0067: 0x0074,\r | |
86 | 0x0068: 0x0075,\r | |
87 | 0x0069: 0x0076,\r | |
88 | 0x006a: 0x0077,\r | |
89 | 0x006b: 0x0078,\r | |
90 | 0x006c: 0x0079,\r | |
91 | 0x006d: 0x007a,\r | |
92 | 0x006e: 0x0061,\r | |
93 | 0x006f: 0x0062,\r | |
94 | 0x0070: 0x0063,\r | |
95 | 0x0071: 0x0064,\r | |
96 | 0x0072: 0x0065,\r | |
97 | 0x0073: 0x0066,\r | |
98 | 0x0074: 0x0067,\r | |
99 | 0x0075: 0x0068,\r | |
100 | 0x0076: 0x0069,\r | |
101 | 0x0077: 0x006a,\r | |
102 | 0x0078: 0x006b,\r | |
103 | 0x0079: 0x006c,\r | |
104 | 0x007a: 0x006d,\r | |
105 | })\r | |
106 | \r | |
107 | ### Encoding Map\r | |
108 | \r | |
109 | encoding_map = codecs.make_encoding_map(decoding_map)\r | |
110 | \r | |
111 | ### Filter API\r | |
112 | \r | |
113 | def rot13(infile, outfile):\r | |
114 | outfile.write(infile.read().encode('rot-13'))\r | |
115 | \r | |
116 | if __name__ == '__main__':\r | |
117 | import sys\r | |
118 | rot13(sys.stdin, sys.stdout)\r |