Python练习--在线修改配置文件

#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# author:stealth
import json,os

def fetch(backend):
    backend_title = 'backend %s' %backend
    record_list = []
    with open('ha.conf','r',encoding='utf-8') as obj:
        flag = False
        for line in obj:
            line = line.strip()
            if line == backend_title:
                flag = True
                continue
            if flag and line.startswith('backend'):
                flag = False
                break
            if flag and line:
                record_list.append(line)

    return record_list

def add(dict_info):
    backend = dict_info.get('backend')
    record_list = fetch(backend)
    backend_title = 'backend %s' %backend
    current_record = 'server %s %s weight %s maxconn %s' %(dict_info['record']['server'], dict_info['record']['server'], dict_info['record']['weight'], dict_info['record']['maxconn'])
    if not record_list:
        record_list.append(backend_title)
        record_list.append(current_record)
        with open('ha.conf','r') as read_f,open('ha.conf.new','w') as write_f:
            for line in read_f:
                write_f.write(line)
            for i in record_list:
                if i.startswith('backend'):
                    write_f.write(i+'\n')
                else:
                    write_f.write('%s%s\n' %(8*' ',i))
    else:
        record_list.insert(0,backend_title)
        if current_record not in record_list:
            record_list.append(current_record)
        with open('ha.conf', 'r') as read_f, open('ha.conf.new', 'w') as write_f:
            flag = False
            has_write = False
            for line in read_f:
               line_strip = line.strip()
               if line_strip == backend_title:
                    flag = True
                    continue
               if flag and line_strip.startswith('backend'):
                    flag = False
               if not flag:
                    write_f.write(line)
               else:
                    if not has_write:
                        for i in record_list:
                            if i.startswith('backend'):
                                write_f.write(i+'\n')
                            else:
                                write_f.write('%s%s\n' %(8*' ',i))
                    has_write = True

def remove(dict_info):
    backend = dict_info.get('backend')
    record_list = fetch(backend)
    backend_title = 'backend %s' %backend
    current_record = 'server %s %s weight %s maxconn %s' % (dict_info['record']['server'], dict_info['record']['server'], dict_info['record']['weight'], dict_info['record']['maxconn'])
    if not record_list:
        return
    else:
        if current_record not in record_list:
            return
        else:
            del record_list[record_list.index(current_record)]
            if len(record_list) > 0:
                record_list.insert(0,backend_title)
        with open('ha.conf') as read_f,open('ha.conf.new','w') as write_f:
            flag = False
            has_write = False
            for line in read_f:
                line_strip = line.strip()
                if line_strip == backend_title:
                    flag = True
                    continue
                if flag and line_strip.startswith('backend'):
                    flag = False
                if not flag:
                    write_f.write(line)
                else:
                    if not has_write:
                        for i in record_list:
                            if i.startswith('backend'):
                                write_f.write(i+'\n')
                            else:
                                write_f.write('%s%s\n' %(8*' ',i))
                        has_write = True

if __name__ == '__main__':
    print('1、获取;2、添加;3、删除')
    num = input('请输入序号:')
    if num == '1':
        data = input('请输入内容:')
        print(fetch(data))

    elif num == '2':
        data = input('请输入内容:')
        dict_data = json.loads(data)
        add(dict_data)
    elif num == '3':
        data = input('请输入内容:')
        dict_data = json.loads(data)
        remove(dict_data)
    else:
        print('您输入的有误!')

 

你可能感兴趣的:(python练习)