# -*- coding:utf-8 -*- import string import os import sys import time from configparser import ConfigParser import json # 功能基类,请勿添加外部逻辑功能; class CINIConfig(): def __init__(self, ini_path): self.path = ini_path self.cfgParser = ConfigParser() # 保存修改; def save_config(self): with open(self.path, 'wb') as configfile: self.cfgParser.write(configfile) # 字符转字典; def get_dict(self, value): return json.loads(value) def get_value_dict(self, section, option): value = self.get_value(section, option) if value is None: return {} return json.loads(value) # 获取参数值;默认返回字符串 def get_value(self, section, option): # 读取配置文件; self.cfgParser.read(self.path) if self.cfgParser.has_option(section, option): return self.cfgParser.get(section, option) return None # 获取参数值; def get_int(self, section, option): # 读取配置文件; self.cfgParser.read(self.path) if self.cfgParser.has_option(section, option): return self.cfgParser.getint(section, option) return None # 获取参数值; def get_float(self, section, option): # 读取配置文件; self.cfgParser.read(self.path) if self.cfgParser.has_option(section, option): return self.cfgParser.getfloat(section, option) return None # 获取参数值; def get_boolean(self, section, option): # 读取配置文件; self.cfgParser.read(self.path) if self.cfgParser.has_option(section, option): return self.cfgParser.getboolean(section, option) return None # 获取键值; def get_options(self, section): # 读取配置文件; self.cfgParser.read(self.path) if self.cfgParser.has_section(section): return self.cfgParser.options(section) return None # 获取所有sections; def get_sections(self): # 读取配置文件; self.cfgParser.read(self.path) return self.cfgParser.sections() # 获取指定section所有items; def get_items(self, section): # 读取配置文件; self.cfgParser.read(self.path) if self.cfgParser.has_section(section): return self.cfgParser.items(section) return None # 添加section; def add_section(self, section): # 读取配置文件; self.cfgParser.read(self.path) if self.cfgParser.has_section(section) is False: self.cfgParser.add_section(section) # 保存修改; self.save_config() # 设置值; def set_value(self, section, option, value): # 读取配置文件; self.cfgParser.read(self.path) if self.cfgParser.has_section(section) is False: self.cfgParser.add_section(section) self.cfgParser.set(section, option, value) # 保存修改; self.save_config() # 删除指定option; def del_option(self, section, option): # 读取配置文件; self.cfgParser.read(self.path) self.cfgParser.remove_option(section, option) # 保存修改; self.save_config() # 删除section; def del_section(self, section): # 读取配置文件; self.cfgParser.read(self.path) self.cfgParser.remove_section(section) # 保存修改; self.save_config() # 是否有section存在; def has_section(self, section): # 读取配置文件; self.cfgParser.read(self.path) return self.cfgParser.has_section(section) # 某section下是否有option存在; def has_option(self, section, option): # 读取配置文件; self.cfgParser.read(self.path) return self.cfgParser.has_option(section, option) if __name__ == "__main__": # tconfig = TConfig(r'E:\SAT\SAT_API\ssat_sdk\MenuTree3\MenuTree.ini') tconfig = CINIConfig(r'E:\SAT\resource\MenuTree\NT72\DVB_nafrican\MenuTree.ini') # print tconfig.get_options('First') # print tconfig.get_sections() # print tconfig.get_items('First') value = tconfig.get_value('value', 'Setting') print value # print tconfig.get_dict(value) # print tconfig.set_value('First', 'fffff','aaaa') # tconfig.del_option('First','fffff') # tconfig.del_section('Second') # tconfig.set_value('second','setting','{"offset":10, "minPeri":0, "maxPeri":0, "minArea":0, "maxArea":0}')