This repository was archived by the owner on Dec 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviscii_codec.py
91 lines (63 loc) · 2.15 KB
/
viscii_codec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import codecs
from functools import lru_cache
# Codec APIs
class Codec(codecs.Codec):
def encode(self, input, errors="strict"):
return codecs.charmap_encode(input, errors, encoding_table)
def decode(self, input, errors="strict"):
return codecs.charmap_decode(input, errors, decoding_table)
class IncrementalEncoder(codecs.IncrementalEncoder):
def encode(self, input, final=False):
return codecs.charmap_encode(input, self.errors, encoding_table)[0]
class IncrementalDecoder(codecs.IncrementalDecoder):
def decode(self, input, final=False):
return codecs.charmap_decode(input, self.errors, decoding_table)[0]
class StreamWriter(Codec, codecs.StreamWriter):
pass
class StreamReader(Codec, codecs.StreamReader):
pass
# encodings module API
@lru_cache()
def getregentry():
return codecs.CodecInfo(
name="viscii",
encode=Codec().encode,
decode=Codec().decode,
incrementalencoder=IncrementalEncoder,
incrementaldecoder=IncrementalDecoder,
streamreader=StreamReader,
streamwriter=StreamWriter,
)
# Decoding Table
decoding_table = (
"\x00\x01Ẳ\x03\x04ẴẪ\x07\x08\t\n\x0b\x0c\r\x0e\x0f"
"\x10\x11\x12\x13Ỷ\x15\x16\x17\x18Ỹ\x1a\x1b\x1c\x1dỴ\x1f"
" !\"#$%&'()*+,-./"
"0123456789:;<=>?"
"@ABCDEFGHIJKLMNO"
"PQRSTUVWXYZ[\\]^_"
"`abcdefghijklmno"
"pqrstuvwxyz{|}~\x7f"
"ẠẮẰẶẤẦẨẬẼẸẾỀỂỄỆỐ"
"ỒỔỖỘỢỚỜỞỊỎỌỈỦŨỤỲ"
"Õắằặấầẩậẽẹếềểễệố"
"ồổỗỠƠộờởịỰỨỪỬơớƯ"
"ÀÁÂÃẢĂẳẵÈÉÊẺÌÍĨỳ"
"ĐứÒÓÔạỷừửÙÚỹỵÝỡư"
"àáâãảăữẫèéêẻìíĩỉ"
"đựòóôõỏọụùúũủýợỮ"
)
# Encoding table
encoding_table = codecs.charmap_build(decoding_table)
def search_function(encoding):
entry = getregentry()
if entry.name == encoding:
return entry
return None
def register():
codecs.register(search_function)
def unregister():
try:
codecs.unregister(search_function)
except AttributeError:
pass