1.函数解耦,fetch()和change()函数中均用到文件处理的操作,所以定义file_handler()做统一处理,防止fetch和change中都夹杂文件处理的内容,增强代码的可读性和维护性
#!user/bin/env python # -*- coding:utf-8 -*- import os def file_handler(backend_data,res=None,type='fetch'): #位置参数在左,默认参数在右 # print(backend_data) if type == 'fetch': with open('haproxy.conf','r') as read_f: li = [] tag = False for read_line in read_f: # print(read_line) if read_line.strip() == backend_data: tag = True continue if tag and read_line.startswith('backend'): #找到backend 并且 下一行以backend开头(加上tag防止在目标行之前先找到backend) break if tag: print('\033[1;45m%s\033[0m' %read_line,end='') li.append(read_line) return li elif type == 'change': with open('haproxy.conf', 'r') as \ read_f, open('haproxy_new.conf', 'w') as write_f: tag = False has_writen_tag = False for read_line in read_f: if read_line.strip() == backend_data: tag = True # write_f.write(read_line) continue if tag and read_line.startswith('backend'): tag = False if not tag: write_f.write(read_line) else: if not has_writen_tag: for record in res: write_f.write(record) has_writen_tag = True os.rename('haproxy.conf', 'haproxy.conf.bak') # 对haproxy.conf文件重新命名为haproxy.conf.bak os.rename('haproxy_new.conf', 'haproxy.conf') # 对haproxy_new.conf重新命名为haproxy.conf os.remove('haproxy.conf.bak') # 移除.bak文件 def fetch(data): # print('\033[1;43m这是查询功能\033[0m') # print('\033[1;43m用户数据是\033[0m',data) backend_data = 'backend %s' %data return file_handler(backend_data) def add(): pass def change(data): # print('这是修改功能') print('用户输入的数据是',data) backend = data[0]['backend'] #文件中的一条记录 查找1 www.oldboy1.org backend_data = 'backend %s'%backend #backend www.oldboy1.org # server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000 # server 2.2.2.5 2.2.2.5 weight 30 maxconn 4000 # [{'backend':'www.oldboy1.org','record':{'server':'2.2.2.4','weight':20,'maxconn':3000}},{'backend':'www.oldboy1.org','record':{'server':'2.2.2.5','weight':30,'maxconn':4000}}] old_server_record = '%sserver %s %s weight %s maxconn %s\n'%(' '*8,data[0]['record']['server'], data[0]['record']['server'], data[0]['record']['weight'], data[0]['record']['maxconn']) new_server_record = '%sserver %s %s weight %s maxconn %s\n'%(' '*8,data[1]['record']['server'], data[1]['record']['server'], data[1]['record']['weight'], data[1]['record']['maxconn']) print('用户想要修改的记录是',old_server_record) #查找2{'server':'2.2.2.4','weight':20,'maxconn':3000} res = fetch(backend) #fetch('www.oldboy1.org') # print('来自查找函数',res) print('来自修改函数--->',res) if not res or old_server_record not in res: print('修改记录不存在') else: index = res.index(old_server_record) res[index] = new_server_record res.insert(0,'%s\n' %backend_data) file_handler(backend_data,res=res,type='change') def delete(): pass if __name__ == '__main__': msg = ''' 1:查询 2:添加 3.修改 4.删除 5.退出 ''' msg_dic = { '1':fetch, '2':add, '3':change, '4':delete, '5':exit } bool = True while bool: print(msg) choice = input('please input your item:') if not choice:continue if choice == '5':break data = input('请输入你的数据:') if data == '': continue if choice != '1': data = eval(data) res = msg_dic[choice](data) print(res)