- #!/usr/bin/env python3
- import JSON #JSON 模块, 用于将像字典的字符串转换为字典
- import re #re 模块, 查找替换
- import shutil #copy 文件用
- shutil.copyfile('conf','conf.bak') #shutil.copyfile(文件 1, 文件 2): 不用打开文件, 直接用文件名进行覆盖 copy.
- #全局变量函数
- def env():
- global user_dic,insert_addr,server,weight,maxconn,insert_conf,rec,ser,wei,max #global 定义全局变量, 这样函数外也能调用该变量
- user_dic = JSON.loads(user_input) # 将用户输入类似字典字符串转换为字典
- insert_addr = list(user_dic.items())[0][0] + ' ' + list(user_dic.items())[0][1] # 将用户输入信息第一个 keys 和 values 取出来
- rec = list(user_dic.keys())[1]
- ser = list(user_dic[rec].keys())[0]
- wei = list(user_dic[rec].keys())[1]
- max = list(user_dic[rec].keys())[2]
- if rec in user_dic.keys():
- if ser in user_dic[rec].keys():
- server = user_dic[rec][ser]
- if wei in user_dic[rec].keys():
- weight = user_dic[rec][wei]
- if max in user_dic[rec].keys():
- maxconn = user_dic[rec][max]
- insert_conf = '%s %s %s %s %s %s %s' % (ser, server, server, wei, weight, max, maxconn) # 将信息格式化
- else:
- print('输入有误')
- View Code
- # 将文件加入列表函数
- def list_lines():
- global lines
- with open('conf.bak', 'r') as f:
- for line in f:
- f.seek(0) # 文件指针指定为 0 开始
- lines = f.readlines() # 将文件每行读取到列表 lines 中, 包括换行符 \ n 和缩进 \ t.
- View Code
- # 判断是否是 JSON 格式函数
- def check_json_format():
- try:
- JSON.loads(user_input,encoding='utf-8') #检测 user_input 是否是 JSON 格式, 不是则异常
- except ValueError: #异常报错, 如没有异常这条永远不走
- return False #异常报错返回 False
- return True #没有异常返回 True
- View Code
- # 判断字符串是否为数字函数
- def isnub(s):
- try:
- nb = float(s) #将字符串转换成数字成功则返回 True
- return True
- except ValueError as e:
- return False #如果出现异常则返回 False
- View Code
- # 索引值是否存在函数
- def if_index():
- try:
- lines.index(insert_index)
- return False
- except ValueError:
- return True
- View Code
- # 判断输入不能为空
- def if_else():
- if len(user_input) == 0:
- print('输入不能为空, 请重新输入!')
- return True
- else:
- print('输入有误, 请输入 json 格式字符串!')
- return False
- View Code
- # 输出文件变化
- def change():
- with open('conf', 'r') as f:
- print('修改之前:')
- for line in f:
- if insert_addr in line:
- print(line + next(f))
- with open('conf', 'r') as f:
- print('修改之后:')
- for line in f:
- if insert_addr in line:
- print(line + next(f) + next(f))
- View Code
- # 配置文件增加函数
- def increase_conf():
- global user_input,insert_index
- while True:
- user_input = input('输入要增加配置文件内容:').strip()
- if check_json_format() and isnub(user_input) == False: #如果 JSON 判断函数返回 True, 则为 JSON 格式
- env()
- list_lines()
- if insert_addr+'\n' in lines:
- insert_index = lines.index(insert_addr+'\n') # 获取 insert_addr+\n 在列表 lines 中的索引位置
- if if_index():
- lines.insert(insert_index + 2, '' + insert_conf +'\n') # 在列表中指定索引位置插入信息
- with open('conf', '+w') as conf:
- conf.write(''.join(lines)) # join 将列表内容写入文件中以''隔开, 也可以以'\n'隔开
- print('已将 %s 添加到 conf 文件中'%(insert_conf))
- change()
- return True
- else:
- print('%s 在 conf 文件中不存在'%(insert_addr))
- return False
- else:
- print('%s 在 conf 文件中不存在'%(insert_addr))
- return False
- if if_else():
- continue
- else:
- continue
- View Code
- # 配置文件删除函数
- def del_conf():
- global user_input,insert_index
- while True:
- user_input = input('请输入需要删除配置信息:').strip()
- if check_json_format() and isnub(user_input) == False: # 如果 JSON 判断函数返回 True, 则为 JSON 格式
- env()
- list_lines()
- if insert_addr + '\n' in lines:
- if ''+insert_conf+'\n' in lines:
- insert_index = lines.index(''+insert_conf+'\n')
- if if_index():
- del[lines[insert_index]]
- with open('conf', '+w') as conf:
- conf.write(''.join(lines)) # join 将列表内容写入文件中以''隔开, 也可以以'\n'隔开
- return True
- else:
- print('%s %s %s %s %s %s %s 在文件 conf 中不存在'%(ser, server, server, wei, weight, max, maxconn))
- continue
- else:
- # print('%s 不存在 conf 中'%(insert_conf))
- print('%s %s %s %s %s %s %s 在文件 conf 中不存在' % (ser, server, server, wei, weight, max, maxconn))
- continue
- else:
- print('%s 在文件 conf 中不存在'%(insert_addr))
- continue
- if if_else():
- continue
- else:
- continue
- View Code
- # 配置文件修改函数
- def modify_conf():
- global user_input
- while True:
- user_input = input('请输入需要修改的内容:').strip()
- if check_json_format() and isnub(user_input) == False: # 如果 JSON 判断函数返回 True, 则为 JSON 格式
- env()
- list_lines()
- if insert_addr+'\n' in lines:
- if ''+insert_conf+'\n' in lines:
- while True:
- user_input = input('需要修改为:').strip()
- if len(user_input) == 0:
- print('输入不能为空, 请重新输入!')
- continue
- else:
- open('conf', 'w').write(re.sub(insert_conf, user_input,open('conf.bak').read()))# 读取文件 conf.bak 中
- # insert_conf 替换为 user_input, 这里 re.sub 是查找替换, 正则表达式.
- print('已将:%s\n'
- '修改为:%s'%(insert_conf,user_input))
- return True
- if if_else():
- continue
- else:
- continue
- View Code
- # 配置文件查看函数
- def see_conf():
- while True:
- user_input = input('请输入需要查找的内容:').strip()
- if len(user_input) == 0:
- print('输入不能为空, 请重新输入')
- continue
- else:
- with open('conf','r') as f:
- for line in f:
- if user_input in line.strip():
- print(line)
- print(next(f))
- return
- else:
- print('无法查找')
- return
- View Code
- while True:
- print('1,%s 2,%s 3,%s 4,%s'%('增加','删除','修改','查看'))
- user_input = input('请选择对配置文件的动作:').strip()
- if len(user_input) == 0:
- print('输入不能为空, 请重新输入!')
- continue
- if user_input == '1' or user_input == '增加':
- increase_conf()
- break
- if user_input == '2' or user_input == '删除':
- del_conf()
- break
- if user_input == '3' or user_input == '修改':
- modify_conf()
- break
- if user_input == '4' or user_input == '查看':
- see_conf()
- break
- else:
- print('输入有误')
- continue
来源: http://www.bubuko.com/infodetail-3005263.html