Commit 3cb3d82a authored by wanli's avatar wanli

🧪 test: 修复读文件编码格式错误问题

parent 99d0ccd3
#!/usr/bin/env bash
###
# @Author: your name
# @Date: 2021-07-15 09:33:39
# @LastEditTime: 2021-07-21 14:25:13
# @LastEditors: your name
# @Description: In User Settings Edit
# @FilePath: \evm-store\backend\start.sh
###
Cur_Dir=$(pwd)
source venv/bin/activate
nohup ${Cur_Dir}/venv/bin/python3 ${Cur_Dir}/start.py > running.log 2>&1 &
pip install -i https://mirrors.aliyun.com/pypi/simple -r requirements.txt --trusted-host
\ No newline at end of file
......@@ -8,6 +8,7 @@ import traceback
import uuid
import time
import zipfile
import chardet
from pathlib import Path
from datetime import datetime
from flask import Blueprint, request, json
......@@ -31,6 +32,13 @@ api = Blueprint("api", __name__, url_prefix="/api/v1/%s" % config['NAME'])
logger.info("/api/v1/%s" % config['NAME'])
# 获取文件编码类型
def get_encoding(file):
# 二进制方式读取,获取字节数据,检测类型
with open(file, 'rb') as f:
data = f.read()
return chardet.detect(data)['encoding']
def stopApp():
fpath = os.sep.join([os.getcwd(), "restart.json"])
with open(fpath, "w+") as f:
......@@ -398,7 +406,8 @@ def parse_header_files():
config_file = "typeconfig.json"
typeconfig = {}
if os.path.exists(config_file):
with open(config_file, "r", encoding="utf-8") as f:
encode_type = get_encoding(config_file)
with open(config_file, "r", encoding=encode_type) as f:
typeconfig = json.loads(f.read())
if len(typeconfig.keys()) > 0:
......@@ -411,7 +420,7 @@ def parse_header_files():
for file in files:
result = parse_header_file(file.resolve().as_posix(), undefined_type, error_tips)
if result != None:
target = target_path.joinpath(".json".format(file.name))
target = target_path.joinpath("{}.json".format(file.name))
with open(target.resolve().as_posix(), "w+", encoding="utf-8") as f:
f.write(json.dumps(result))
result_list.append(result)
......@@ -451,8 +460,10 @@ def process_parse():
# 加载系统内置类型映射文件
config_file = "typeconfig.json"
typeconfig = {}
encode_type = "utf-8"
if os.path.exists(config_file):
with open(config_file, "r", encoding="utf-8") as f:
encode_type = get_encoding(config_file)
with open(config_file, "r", encoding=encode_type) as f:
typeconfig = json.loads(f.read())
if not typeconfig:
typeconfig = {}
......@@ -462,7 +473,7 @@ def process_parse():
if len(typeconfig.keys()) > 0:
conf = update_mark_type(typeconfig)
with open(config_file, "w", encoding="utf-8") as f:
with open(config_file, "w", encoding=encode_type) as f:
f.write(json.dumps(conf))
try:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment