-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.py
More file actions
34 lines (33 loc) · 1.54 KB
/
Encoder.py
File metadata and controls
34 lines (33 loc) · 1.54 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
# 在进入编译之前,先检查文件编码格式,如果不是utf-8,则转换为utf-8编码格式并覆盖源文件
import os
from chardet import detect
import ColorPrint
def encodeProject(dir:str, verbose: bool) -> bool:
if verbose:
ColorPrint.print_verbose_hint("开始转换工程的编码格式")
L = []
for root, dirs, files in os.walk(dir):
#获得所有bkscr文件
for file in files:
if os.path.splitext(file)[1] == ".bkscr" or os.path.splitext(file)[1] == ".bkpsr":
L.append(os.path.join(root, file))
if len(L) > 0:
for path in L:
# print(path)
#修改编码格式
with open(path, 'rb+') as fp:
old_content = fp.read()
encoding = detect(old_content)['encoding']
if verbose:
ColorPrint.print_verbose_hint("检测到编码 -> " + encoding)
if encoding == "UTF-8-SIG":
content = old_content.decode(encoding).encode('utf8')
fp.seek(0)
fp.write(content)
if verbose:
ColorPrint.print_compile(old_content.decode("utf-8-sig"),content.decode("utf-8"))
elif encoding == "utf-8" or encoding == "ascii": pass
else:
ColorPrint.print_verbose_hint("暂时不支持转换的编码格式,或者你需要手动修改转换器代码并添加新的编码格式转换支持")
return False
return True