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