第三天作业
import json #查询函数 def fetch(backend): backend_title = "backend %s" %backend record_list = [] with open("data.txt", "r") as obj: flag = False for line in obj: line = line.strip() if line == backend_title: flag = True continue if flag and line.startswith("backend"): #匹配到第二个backend开头的字符串则退出循环 flag = False break if flag and line: #把第一个backend开头的字符串后的不为空的数据存入列表 record_list.append(line) return record_list #添加函数 def add(dict_info): backend = dict_info.get("backend") record_list = fetch(backend) backend_title = "backend %s" % backend current_record = "server %s %s weight %d maxconn %d" %(dict_info["record"]["server"], dict_info["record"]["server"], dict_info["record"]["weight"], dict_info["record"]["maxconn"]) if not record_list: #如果列表为空(配置表中没有这个数据),则添加到列表中 record_list.append(backend_title) record_list.append(current_record) with open("data,txt", "r") as read_file, open("new_data.txt", "w") as write_file: flag = False for line in read_file: write_file.write(line) for i in record_list: if i.startswith("backend"): write_file.write(i+"\n") else: write_file.write("%s%s\n" %(8*" ", i)) else: record_list.insert(0, backend_title) if current_record not in record_list: record_list.append(current_record) with open("data.txt", "r") as read_file, open("new_data.txt", "w") as write_file: flag = False has_write = False for line in read_file: line = line.strip() if line == backend_title: flag = True continue if flag and line.startswith("backend"): flag = False if not flag: write_file.write(line+"\n") else: if not has_write: for i in record_list: if i.startswith("backend"): write_file.write(i+"\n") else: write_file.write("%s%s\n" %(8*" ", i)) has_write = True #删除函数 def remove(dict_info): backend = dict_info.get("backend") record_list = fetch(backend) backend_title = "backend %s" % backend current_record = "server %s %s weight %d maxconn %d" % ( dict_info["record"]["server"], dict_info["record"]["server"], dict_info["record"]["weight"], dict_info["record"]["maxconn"]) if not record_list: return else: if current_record not in record_list: return else: del record_list[record_list.index(current_record)] if len(record_list) > 0: record_list.insert(0, backend_title) with open("data.txt", "r") as read_file, open("new_data.txt", "w") as write_file: flag = False has_write = False for line in read_file: line = line.strip() if line == backend_title: flag = True continue if flag and line.startswith("backend"): flag = False if not flag: write_file.write(line + "\n") else: if not has_write: for i in record_list: if i.startswith("backend"): write_file.write(i + "\n") else: write_file.write("%s%s\n" % (8 * " ", i)) has_write = True #主函数 if __name__ == "__main__": flag = True print("1:查询, 2:添加, 3:删除") while flag: #确保输入为:1、2、3 num = input("请输入序号:") if num in ["1", "2", "3"]: flag = False data = input("请输入内容:") if num == "1": #输入1调用查询函数 list_data = fetch(data) for i in list_data: print(i) else: #输入为2、3调用添加和删除函数 dict_data = json.loads(data) #把输入的数据转换为字典 if num == "2": #调用添加函数 add(dict_data) else: #调用删除函数 remove(dict_data)