Python-24_综合练习-01_函数_文件处理_解耦--查询功能

--------------------------------------------- 一、查询功能:---------------------------------------------

def fetch(data):
    print("\033[1;43m查询功能\033[0m")          # \033[1;43m---\033[0m  给---添加颜色,43代表颜色
    print("\033[1;43m用户数据\033[0m",data)
    backend_data="backend %s" %data
    with open("haproxy.conf","r") as read_f:
        arg=False
        res=[]
        for read_line in read_f:
            if read_line.strip()==backend_data:
                arg=True
                continue
            if arg and read_line.startswith("backend"):
                break
            if arg:
                print("\033[1;43m%s\033[0m"%read_line,end="")
                res.append(read_line)
    return res

def add():
    print("添加功能")
def change():
    print("修改功能")
def delete():
    print("删除功能")

msg="""
1:查询
2:添加
3:修改
4:删除
5:退出
"""
msg_dic={"1":fetch,"2":add,"3":change,"4":delete}

while True:
    print(msg)
    choice=input("请输入你需要的选项:").strip()
    if not choice:continue
    if choice=="5":break
    data=input("请输入您的数据:").strip()
    msg_dic[choice](data)


# 退出当前循环、退出全部循环:
arg=True
while arg:
    print("level1")
    choice=input("level1 >>>").strip()
    if choice == "quit": break
    if choice == "quit_all": arg = False
    while arg:
        print("level2")
        choice = input("level2 >>>").strip()
        if choice == "quit": break
        if choice == "quit_all": arg = False
        while arg:
            print("level3")
            choice = input("level3 >>>").strip()
            if choice == "quit": break
            if choice == "quit_all": arg = False

--------------------------------------------- 二、修改功能:---------------------------------------------

import os
def fetch(data):
    print("\033[1;43m查询功能\033[0m")  # \033[1;43m---\033[0m  给---添加颜色,43代表颜色
    print("\033[1;43m用户数据\033[0m", data)
    backend_data = "backend %s" % data
    with open("haproxy.conf", "r") as read_f:
        arg = False
        res = []
        for read_line in read_f:
            if read_line.strip() == backend_data:
                arg = True
                continue
            if arg and read_line.startswith("backend"):
                break
            if arg:
                print("\033[1;43m%s\033[0m" % read_line, end="")
                res.append(read_line)
    return res


def add():
    print("添加功能")


def change(data):
    print("修改功能")
    print("用户输入数据%s" % data)
    backend = data[0]["backend"]  # 文件当中的一条记录
    backend_data="backend %s" %backend
    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)
    res=fetch(backend)
    print("来自修改功能",res)
    if not res or old_server_record not in res:
        return "修改内容不存在"
    else:
        index=res.index(old_server_record)
        res[index]=new_server_record

    res.insert(0,"%s\n" %backend_data)
    with open("haproxy.conf","r") as read_f,\
        open("haproxy.conf_new","w") as write_f:
        arg=False
        arg_write=False
        for read_line in read_f:
            if read_line.strip()==backend_data:
                arg=True
                continue
            if arg and read_line.startswith("backend"):
                arg=False
            if not arg:
                write_f.write(read_line)
            else:
                if not arg_write:
                    for record in res:
                        write_f.write(record)
                    arg_write=True
    os.rename("haproxy.conf","haproxy.conf.bak")
    os.rename("haproxy.conf_new","haproxy.conf")
    os.remove("haproxy.conf.bak")


def delete():
    print("删除功能")


msg = """
1:查询
2:添加
3:修改
4:删除
5:退出
"""
msg_dic = {"1": fetch, "2": add, "3": change, "4": delete}

while True:
    print(msg)
    choice = input("请输入你需要的选项:").strip()
    if not choice: continue
    if choice == "5": break
    data = input("请输入您的数据:").strip()
    if choice != "1":
        data = eval(data)
    res = msg_dic[choice](data)
    print(res)

# data --->> [{"backend":"www.newmet1.top","record":{"server":"2.2.2.5","weight":"30","maxconn":"4000"}},{"backend":"www.newmet1.top","record":{"server":"2.2.2.4","weight":"20","maxconn":"3000"}}]

# 查询和修改功能,代码都涉及到文件处理的操作,可以单独定义一个函数,用来处理文件的操作!!
修改功能

------------------------------------ 二、修改功能--程序的解耦:-------------------------------------

import os
def file_handle(backend_data,res=None,type="fetch"):
    if type=="fetch":
        with open("haproxy.conf", "r") as read_f:
            tag = False
            res = []
            for read_line in read_f:
                if read_line.strip() == backend_data:
                    tag = True
                    continue
                if tag and read_line.startswith("backend"):
                    break
                if tag:
                    print("\033[1;43m%s\033[0m" % read_line, end="")
                    res.append(read_line)
        return res
    elif type == "change":
        with open("haproxy.conf", "r") as read_f, \
                open("haproxy.conf_new", "w") as write_f:
            tag = False
            tag_write = False
            for read_line in read_f:
                if read_line.strip() == backend_data:
                    tag = True
                    continue
                if tag and read_line.startswith("backend"):
                    tag = False
                if not tag:
                    write_f.write(read_line)
                else:
                    if not tag_write:
                        for record in res:
                            write_f.write(record)
                        tag_write = True
        os.rename("haproxy.conf", "haproxy.conf.bak")
        os.rename("haproxy.conf_new", "haproxy.conf")
        os.remove("haproxy.conf.bak")

def fetch(data):
    # print("\033[1;43m查询功能\033[0m")  # \033[1;43m---\033[0m  给---添加颜色,43代表颜色
    # print("\033[1;43m用户数据\033[0m", data)
    backend_data = "backend %s" % data
    return file_handle(backend_data)


def add():
    print("添加功能")


def change(data):
    print("修改功能")
    print("用户输入数据%s" % data)
    backend = data[0]["backend"]  # 文件当中的一条记录
    backend_data="backend %s" %backend
    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)
    res=fetch(backend)
    print("来自修改功能",res)
    if not res or old_server_record not in res:
        return "修改内容不存在"
    else:
        index=res.index(old_server_record)
        res[index]=new_server_record
    res.insert(0,"%s\n" %backend_data)
    file_handle(backend_data,res=res,type="change")

def delete():
    print("删除功能")


msg = """
1:查询
2:添加
3:修改
4:删除
5:退出
"""
msg_dic = {"1": fetch, "2": add, "3": change, "4": delete}

while True:
    print(msg)
    choice = input("请输入你需要的选项:").strip()
    if not choice: continue
    if choice == "5": break
    data = input("请输入您的数据:").strip()
    if choice != "1":
        data = eval(data)
    res = msg_dic[choice](data)
    print(res)

# data --->> [{"backend":"www.newmet1.top","record":{"server":"2.2.2.5","weight":"30","maxconn":"4000"}},{"backend":"www.newmet1.top","record":{"server":"2.2.2.4","weight":"20","maxconn":"3000"}}]

# 查询和修改功能,代码都涉及到文件处理的操作,可以单独定义一个函数,用来处理文件的操作!!
修改功能:程序的解耦

 

posted on 2018-11-29 13:45  NewMet  阅读(129)  评论(0编辑  收藏  举报

导航