123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- # -*- coding:utf-8 -*-
- import string
- import os
- import sys
- import time
- from configparser import ConfigParser
- import json
- import codecs
- class TConfig:
- 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_file(codecs.open(self.path, "r", "utf-8-sig"))
- if self.cfgParser.has_option(section, option):
- return self.cfgParser.get(section, option)
- return None
- # 获取参数值;
- def get_int(self, section, option):
- # 读取配置文件;
- self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
- if self.cfgParser.has_option(section, option):
- return self.cfgParser.getint(section, option)
- return None
- # 获取参数值;
- def get_float(self, section, option):
- # 读取配置文件;
- self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
- if self.cfgParser.has_option(section, option):
- return self.cfgParser.getfloat(section, option)
- return None
- # 获取参数值;
- def get_boolean(self, section, option):
- # 读取配置文件;
- self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
- if self.cfgParser.has_option(section, option):
- return self.cfgParser.getboolean(section, option)
- return None
- # 获取键值;
- def get_options(self, section):
- # 读取配置文件;
- self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
- if self.cfgParser.has_section(section):
- return self.cfgParser.options(section)
- return None
- # 获取所有sections;
- def get_sections(self):
- # 读取配置文件;
- self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
- return self.cfgParser.sections()
- # 获取指定section所有items;
- def get_items(self, section):
- # 读取配置文件;
- self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
- if self.cfgParser.has_section(section):
- return self.cfgParser.items(section)
- return None
- # 添加section;
- def add_section(self, section):
- # 读取配置文件;
- self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
- 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_file(codecs.open(self.path, "r", "utf-8-sig"))
- 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_file(codecs.open(self.path, "r", "utf-8-sig"))
- self.cfgParser.remove_option(section, option)
- # 保存修改;
- self.save_config()
- # 删除section;
- def del_section(self, section):
- # 读取配置文件;
- self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
- self.cfgParser.remove_section(section)
- # 保存修改;
- self.save_config()
- def has_section(self, section):
- # 读取配置文件;
- self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
- return self.cfgParser.has_section(section)
- def has_option(self, section, option):
- # 读取配置文件;
- self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
- return self.cfgParser.has_option(section, option)
- if __name__ == "__main__":
- tconfig = TConfig(r'D:\SAT\resource\MenuTree\NT72\NT72\MenuTree.ini')
- value = tconfig.get_value('value', 'Setting')
- print value
|