替换老cmd,输出新conf文件:
要求:
1. 输入三个文件 a2e /a2r/ q2e
对 "cmd" : { }中内容进行操作
"lrm" : {
"grp" : 2,
"dev" : 201,
"grp_cfg" : "../../group/group_reg.cfg",
"cmd" : {
"a2r_b_bank_checkin_req" : {"type" : 129, "grps":[23, 16]},
"reg_flow_nty" : {"type" : 129, " grps " : [ 4 , 16 ] },
"a2r_wm_ch_trader_pwd_man_req" : {
"type" : 133,
"grps":[2]
},
},
"line_cfg" : {
"client" : []
}
},
2.cmd名如果存在于csv.txt的第一列,则替换为第三列。不存在则整行不需要输出。
3.如果替换后的cmd名重复,则把grps数组内的组号合并,不能重复。
如:
"qte_inst_info_nty":{"type" : 129,"grps":[15, 20]},
"qte_inst_info_nty": {"type":129,"grps":[8,10,4,5,20,]},
合并后
"qte_inst_info_nty": {"type":129,"grps":[8,10,4,5,20,15]},
4.来源哪个文件则在第一行加上注释
如:
#qte
"qte_reg_seat_capital_frz_req": {"type":129,"grps":[11,20,]}
#a2r
"a2r_b_bank_checkin_req" : {"type" : 129, "grps":[23,16]},
5. “cmd”:{}内容应满足json格式
6.替换“cmd”:{}后输出完整的新conf文件
文件内容类似:
{
"read_thread_num" : 1,
"write_thread_num" : 1,
"gen_core" : 1,
"log" : {
"name" : "./log/smlt_gems",
"level" : 0
},
"pool" : {
"pool_type" : 6,
"listen_port" : 3021,
"backlog" : 500,
"auth_ip" : ["127.0.0.1", {"0.0.0.0" : "255.255.255.255"}],
"max_fds" : 1024,
"works_num" : 1,
"poll_timeout" : 500
},
"web_pool" : {
"pool_type" : 6,
"listen_ports":{
10054:1
},
"backlog" : 500,
"auth_ip" : ["127.0.0.1", {"0.0.0.0" : "255.255.255.255"}],
"max_fds" : 1024,
"works_num" : 1,
"poll_timeout" : 500,
"read_thread_num" : 1,
"write_thread_num" : 1
},
"protocol" : {
"read_timeout" : 1000,
"write_timeout" : 1000,
"connect_timeout" : 1000,
"type" : 2
},
"web_protocol" : {
"read_timeout" : 1000,
"write_timeout" : 1000,
"connect_timeout" : 1000,
"type" : 5
},
"lrm" : {
"grp" : 2,
"dev" : 201,
"grp_cfg" : "../../group/group_reg.cfg",
"cmd" : {
"a2e_szse_add_cut_qry_req": {"type" : 1, "grps" : [8], "etf_record" : 0 },
"a2e_sse_biz_over_req": { "type" : 1, "grps" : [7], "etf_record" : 0 }
},
"line_cfg" : {
"client" : []
}
},
"plugin" : {
"plugin_dir" : "./",
"plugin_config" : {}
},
"py_path": "../../py/lib",
"auto_reply" : "./conf/auto_reply.cfg"
}
写的代码如下:
import re
import json
import copy
def read_cmd_from_file(file_name_list):
'''
从文件列表中读取cmd
:param file_name_list: 文件列表
:return:
'''
cmd_list = []
for file in file_name_list[:]:
with open(file, "r") as f:
data = f.read()
cmd_data = re.search('"cmd" : (\{[\s\S]*\}),[\s\S]*\"line_cfg\"', data).group(1)
if re.search("\},[\s\S]*?\},", cmd_data):
cmd_data = re.sub("(?<=}),(?=[\s\t\n]*\})", "", cmd_data)
if re.search("\[.*?\]", cmd_data):
cmd_data = re.sub("\[", "\"[", cmd_data)
cmd_data = re.sub("\]", "]\"", cmd_data)
if re.search(",(?=\})", cmd_data):
cmd_data = re.sub(",(?=\})", "", cmd_data)
if "q2e" in file:
cmd_json = cmd_remove_duplicate(cmd_data)
else:
cmd_json = json.loads(cmd_data)
for key in cmd_json:
if "grps" in cmd_json[key]:
cmd_json[key]["grps"] = eval(cmd_json[key]["grps"])
if " grps " in cmd_json[key]:
cmd_json[key]["grps"] = eval(cmd_json[key].pop(" grps "))
cmd_list.append({file: cmd_json})
return cmd_list
def cmd_remove_duplicate(cmd_string):
'''
将cmd的json字符串中的key值去重后,返回json
:param cmd_string: 字符串
:return: json
'''
tmp_list = []
for line in cmd_string.split("\n"):
if re.search("\".*\":", line.strip()):
key = re.search("\"(.*?)\"", line).group(1)
value = re.search("({.*?})", line).group(1)
tmp_list.append((key, value))
res_dict = {}
for item in tmp_list:
sub_dict = json.loads(item[1])
if "grps" in sub_dict:
sub_dict["grps"] = eval(sub_dict["grps"])
if item[0] not in res_dict:
res_dict[item[0]] = sub_dict
else:
res_dict[item[0]]["grps"] = list(set(res_dict[item[0]]["grps"] + sub_dict["grps"]))
return res_dict
def switch_cmd(cmd_list, file_name):
'''
对cmd进行转换
:param cmd_list: cmd列表 [{"文件名": {cmd}}]
:param file_name: 文件名,存放替换的cmd名称
:return:
'''
with open(file_name, "r") as f:
data = f.readlines()
res_list = copy.deepcopy(cmd_list)
for index in range(len(cmd_list)):
key = list(cmd_list[index].keys())[0]
cmd_dict = cmd_list[index][key]
for cmd_dict_key in cmd_dict:
for item in data:
if item.split(',')[0] == cmd_dict_key:
res_list[index][key][item.split(',')[2].strip()] = res_list[index][key].pop(cmd_dict_key)
return res_list
def merge(json_list):
'''
合并cmd
:param json_list: [{"文件名": {}}, {}, {}]
:return:
'''
res = {}
for item in json_list:
for file_key, json_value in item.items():
for key, value in json_value.items():
if key not in res:
res[key] = value
else:
tmp = copy.deepcopy(res[key])
res[key] = dict(res[key], **value)
if 'grps' in tmp and 'grps' in value:
res[key]['grps'] = list(set(tmp['grps'] + value['grps']))
return res
def read_rest_content(file):
'''
读取剩下的内容
:param file: 文件名
:return: list
'''
res = []
index = 0
with open(file, 'r') as f:
count = -1
while True:
line = f.readline()
count += 1
if "cmd" in line:
while True:
line = f.readline()
if "line_cfg" in line:
res.append(line)
index = count
break
else:
res.append(line)
if not line:
break
return res, index
def run(file_name_list):
rest_content, insert_index = read_rest_content(file_name_list[0])
source_list = read_cmd_from_file(file_name_list)
switch_list = switch_cmd(source_list, "csv.txt")
res_dict = merge(switch_list)
a2e_dict = switch_list[0]
a2r_dict = switch_list[1]
q2e_dict = switch_list[2]
big_dict = dict(a2e_dict, **a2r_dict, **q2e_dict)
tmp_write_list = re.findall("(\'.*?\})", str(res_dict), 0)
tmp_list = []
for tmp in tmp_write_list:
key = re.search("\'(.*?)\':", tmp).group(1)
notes = []
for sub_key, sub_value in big_dict.items():
if key in sub_value:
notes.append(sub_key[:-4])
tmp_list.append((tmp, notes))
cmd_write_list = ["\t\t\"cmd\" : {\n"]
for index in range(len(tmp_list)):
tmp_string = re.sub("'", "\"", tmp_list[index][0])
if "\t\t\t# " + " ".join(tmp_list[index][1]) + "\n" != "\t\t\t# " + " ".join(tmp_list[index-1][1]) + "\n":
cmd_write_list.append("\t\t\t# " + " ".join(tmp_list[index][1]) + "\n")
cmd_write_list.append("\t\t\t" + tmp_string + "\n")
cmd_write_list.append("\t\t},\n")
with open("res.txt", 'w') as f:
f.writelines(rest_content[:insert_index])
f.writelines(cmd_write_list)
f.writelines(rest_content[insert_index:])
if __name__ == "__main__":
run(["a2e.txt", "a2r.txt", "q2e.txt"])