需求如下:
# (可选)如果backend下所有的记录都已经被删除,那么将当前 backend test.oldboy.org 也删除掉。
思路如下:
文件处理:
1、查询、、、、判断用户输入,查询关键字,判断是否存在关键字符串,存在打印,不存在则输出
2、增加、、、、判断是否存在backend字段:
1、存在则判断是否存在要添加字段,存在无动作,不存在则添加。
2、判断是否存在要添加的记录,存在无动作,不存在则添加。
3、删除、、、、定位到删除的backend关键字
1、找到相应记录,然后删除,判断删除后此backend字段是否为空,为空则删除backend字段。
代码实现如下:
# -*- coding: utf-8 -*-
__author__ = 'weibinf'
import json #导入模块
import os #导入模块
def cat():
read = raw_input("\033[31m请输入backend: \033[0m").strip()
strr = "backend" + " " + read #取得输入字段
obj = open("ha",'r') #打开方式
i = 0
for item in obj: #取得字段是否存在
if strr.strip() == item.replace("\n",'').strip():
i += 1
if i > 0: #不为0
exitt = False
obj.seek(0) #从文件开头重新读取
while not exitt: #循环读取
line = obj.readline().strip() #去掉本行中的空格
if strr.strip() == line.replace("\n",'').strip(): #判断查询字符串和本行字符串是否相等
print "\033[34m -\033[0m" * 30
print "\033[34m %s \033[0m"%line
obj.seek(obj.tell())
while not exitt:
line = obj.readline()
if "backend" in line:#判断本行是否包含backend字段
exitt = True #包含则退出
else:
print "\033[34m %s \033[0m"%line
else:
print "\033[31m 你输入的字符串不在文本中。。。 \033[0m" #不存在打印
def edit():
read = raw_input("\033[31m请输入要新加的记录: \033[0m").strip() #获取输入字符串
read_dict = json.loads(read) #转换成字典
backend_title = read_dict['backend'] #取出key值
strr = "backend" + " " + str(backend_title) #取出字符串
strr1 = " " * 8 + "server" + " " + str(read_dict['record']['server']) + " " + str(read_dict['record']['server']) + " " + "weight" + " " + str(read_dict['record']['weight']) + " " + "maxconn" + " " + str(read_dict['record']['maxconn'])
with open("ha",'r') as obj , open("haha",'a+') as obj2: #打开两个文件
i = 0
for item in obj:
if strr.strip() == item.replace("\n",'').strip(): #判断字段是否存在
i += 1
qa = obj.tell()
if i == 0: #不存在则重新写入到文件
obj.seek(0)
while True:
line = obj.readline()
aa = obj.tell()
if qa == aa:
obj2.write(line)
obj2.write("\n")
obj2.write('%s\n'%strr)
obj2.write('%s\n'%strr1)
obj2.flush()
break
else:
obj2.write(line)
elif i == 1: #字段存在
exitt = False
obj.seek(0)
while not exitt:
line = obj.readline()
obj2.write(line)
bb = obj.tell()
if strr.strip() == line.replace("\n",'').strip(): #判断字符串和本行是否相等
obj.seek(bb)
while not exitt:
line = obj.readline()
cc = obj.tell()
if strr1.replace("\n",'').replace(" ",'').strip() != line.replace("\n",'').replace(" ",'').strip(): #字符串和本行是否相等
if "backend" in line: #本行包含backend关键字
num = obj.tell() - len(line) - 1
obj2.seek(num)
obj2.write('%s\n'%strr1)
obj2.write(line)
obj.flush()
elif qa == cc: #判断文件是否已经是结尾
obj2.write(line)
obj2.write('%s\n'%strr1)
obj.flush()
exitt = True
else: #其余结果
obj2.write(line)
obj2.flush()
os.rename("ha","ha.bak") #重新命名文件
os.rename("haha","ha")
def dele():
read = raw_input("\033[31m请输入要删除的记录: \033[0m").strip()
read_dict = json.loads(read)
backend_title = read_dict['backend']
strr = "backend" + " " + str(backend_title)
strr1 = " " * 8 + "server" + " " + str(read_dict['record']['server']) + " " + str(read_dict['record']['server']) + " " + "weight" + " " + str(read_dict['record']['weight']) + " " + "maxconn" + " " + str(read_dict['record']['maxconn'])
with open("ha",'r+') as obj , open("hadel",'a+') as obj2: #打开两个文件
i = 0
for item in obj:
if strr.strip() == item.replace("\n",'').strip(): #判断文件中是否包含关键字符串
i += 1
qa = obj.tell()
if i == 0:
print "\033[31m你输入的记录不存在。。。 \033[0m" #不存在输出
elif i == 1: #存在关键字符串
exitt = False #设置全局关键退出字段
obj.seek(0) #回到文件开头
while not exitt:
line = obj.readline()
bb = obj.tell()
if strr.strip() != line.replace("\n",'').strip(): #字符串和本行是否相等
obj2.write(line)
obj2.flush()
else:
if qa == bb : #是否是文件结尾
obj2.write(line)
exitt = True
else:
xs = line
while not exitt:
line = obj.readline()
bb = obj.tell()
if strr1.replace("\n",'').replace(" ",'').strip() != line.replace("\n",'').replace(" ",'').strip() : #本行和关键字符串不相等
if qa == bb:
obj2.write(line)
exitt = True
elif "backend" in line: #本行包含关键字
obj2.write(line)
obj2.flush()
else:
obj2.write(xs)
obj2.write(line)
obj2.flush()
else:
continue
os.rename("ha","hadel.bak")
os.rename("hadel","ha")
if __name__ == '__main__':
print """\033[31m
1、获取ha记录
2、增加ha记录
3、删除ha记录
\033[0m"""
while True:
num = raw_input("\033[31m请输入相应编号:\033[0m").strip()
if num == "1":
cat()
elif num == "2":
edit()
elif num == "3":
dele()
else:
break