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