import os

def fetch(data):
    # print('\033[1;43m这是查询功能\033[0m')
    # print('\033[1;43m用户数据是\033[0m',data)
    backend_data='backend %s'%data
    with open('website.conf','r',encoding='utf-8') as read_f:
        tag = False
        ret = []
        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(read_line,end='')
                ret.append(read_line)
        return ret

def add():
    pass

def change(data):
    print('这是修改功能')
    backend= data[0]['backend']
    backend_data='backend %s'%backend
    old_sever_record='%s sever %s weight %s\n'%(' '*8,data[0]['record']['sever'],
                                                 data[0]['record']['weight'])
    new_sever_record='%s sever %s weight %s\n'%(' '*8,data[1]['record']['sever'],
                                               data[1]['record']['weight'])

    res=fetch(backend)
    print('change函数',res)

    # if not res or old_sever_record not in res:
    #     return '你要修改的记录不存在'
    # else:
    index=res.index(old_sever_record)
    res[index]=new_sever_record
    res.insert(0,'%s\n'%backend_data)
    with open('website.conf','r') as read_f,\
        open('website_new.conf','w') as write_f:
        tag=False
        has_wirte=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 has_wirte:
                    for record in res:
                        write_f.write(record)


def delete():
    pass

if __name__=='__main__':
    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)


# [{'backend':'www.newboy2.org','record':{'sever':'1.1.1.8','weight':80}},{'backend':'www.newboy2.org','record':{'sever':'1.0.0.8','weight':100}}]

 

posted on 2019-04-04 16:58  Manuel  阅读(130)  评论(0编辑  收藏  举报