This repository was archived by the owner on Feb 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.py
More file actions
executable file
·58 lines (48 loc) · 1.5 KB
/
functions.py
File metadata and controls
executable file
·58 lines (48 loc) · 1.5 KB
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
import re
def encode_string(str):
res = b""
str = b''.join(str).decode('utf-8')
for chr in str:
if not (chr.isascii() and chr.isprintable()):
chr = b"\x01" + chr.encode("utf-16be").hex().upper().encode("ISO-8859-4")
else:
chr = chr.encode("ISO-8859-4")
res += chr
return res
def decode_string(str):
res = []
for word in str:
if is_encoded(word):
word = decode_word(word)
else:
word = word.decode('ISO-8859-4')
res.append(word)
return "".join(res)
def decode_word(word):
return bytes.fromhex(word.replace(b"\x01", b"").decode("ISO-8859-4")).decode('utf-16be')
def is_encoded(word):
return word[0] == 1
def parse_line(line):
return re.findall(b"\\x01.{4}|\w+|[^\w\r\n]", line)
def parse_file(file_path):
res = []
with open(file_path, "rb") as file:
lines = file.readlines()
for line in lines:
res.append({
"name": line.split(b'\t')[0],
"text": parse_line(line.split(b'\t')[1]),
})
return res
def decode_file(file_path):
content = parse_file(file_path)
res = ""
for line in content:
res += f"{line['name'].decode('ISO-8859-4')}\t{decode_string(line['text'])}\r\n"
return res
def encode_file(file_path):
content = parse_file(file_path)
res = b""
for line in content:
res += line['name'] + b"\t" + encode_string(line['text']) + b"\r\n"
return res[:-2] # cut last newline