| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 | 
							
- 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
 
-     
 
-     def get_sections(self):
 
-         
 
-         self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
 
-         return self.cfgParser.sections()
 
-     
 
-     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
 
-     
 
-     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()
 
-     
 
-     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()
 
-     
 
-     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
 
 
  |