annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/encodings/rot_13.py @ 68:5028fdace37b

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