TConfig.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # -*- coding:utf-8 -*-
  2. import string
  3. import os
  4. import sys
  5. import time
  6. from configparser import ConfigParser
  7. import json
  8. import codecs
  9. class TConfig:
  10. def __init__(self, ini_path):
  11. self.path = ini_path
  12. self.cfgParser = ConfigParser()
  13. # 保存修改;
  14. def save_config(self):
  15. with open(self.path, 'wb') as configfile:
  16. self.cfgParser.write(configfile)
  17. # 字符转字典;
  18. def get_dict(self, value):
  19. return json.loads(value)
  20. def get_value_dict(self, section, option):
  21. value = self.get_value(section, option)
  22. if value is None:
  23. return {}
  24. return json.loads(value)
  25. # 获取参数值;默认返回字符串
  26. def get_value(self, section, option):
  27. # 读取配置文件;
  28. self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
  29. if self.cfgParser.has_option(section, option):
  30. return self.cfgParser.get(section, option)
  31. return None
  32. # 获取参数值;
  33. def get_int(self, section, option):
  34. # 读取配置文件;
  35. self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
  36. if self.cfgParser.has_option(section, option):
  37. return self.cfgParser.getint(section, option)
  38. return None
  39. # 获取参数值;
  40. def get_float(self, section, option):
  41. # 读取配置文件;
  42. self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
  43. if self.cfgParser.has_option(section, option):
  44. return self.cfgParser.getfloat(section, option)
  45. return None
  46. # 获取参数值;
  47. def get_boolean(self, section, option):
  48. # 读取配置文件;
  49. self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
  50. if self.cfgParser.has_option(section, option):
  51. return self.cfgParser.getboolean(section, option)
  52. return None
  53. # 获取键值;
  54. def get_options(self, section):
  55. # 读取配置文件;
  56. self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
  57. if self.cfgParser.has_section(section):
  58. return self.cfgParser.options(section)
  59. return None
  60. # 获取所有sections;
  61. def get_sections(self):
  62. # 读取配置文件;
  63. self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
  64. return self.cfgParser.sections()
  65. # 获取指定section所有items;
  66. def get_items(self, section):
  67. # 读取配置文件;
  68. self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
  69. if self.cfgParser.has_section(section):
  70. return self.cfgParser.items(section)
  71. return None
  72. # 添加section;
  73. def add_section(self, section):
  74. # 读取配置文件;
  75. self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
  76. if self.cfgParser.has_section(section) is False:
  77. self.cfgParser.add_section(section)
  78. # 保存修改;
  79. self.save_config()
  80. # 设置值;
  81. def set_value(self, section, option, value):
  82. # 读取配置文件;
  83. self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
  84. if self.cfgParser.has_section(section) is False:
  85. self.cfgParser.add_section(section)
  86. self.cfgParser.set(section, option, value)
  87. # 保存修改;
  88. self.save_config()
  89. # 删除指定option;
  90. def del_option(self, section, option):
  91. # 读取配置文件;
  92. self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
  93. self.cfgParser.remove_option(section, option)
  94. # 保存修改;
  95. self.save_config()
  96. # 删除section;
  97. def del_section(self, section):
  98. # 读取配置文件;
  99. self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
  100. self.cfgParser.remove_section(section)
  101. # 保存修改;
  102. self.save_config()
  103. def has_section(self, section):
  104. # 读取配置文件;
  105. self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
  106. return self.cfgParser.has_section(section)
  107. def has_option(self, section, option):
  108. # 读取配置文件;
  109. self.cfgParser.read_file(codecs.open(self.path, "r", "utf-8-sig"))
  110. return self.cfgParser.has_option(section, option)
  111. if __name__ == "__main__":
  112. tconfig = TConfig(r'D:\SAT\resource\MenuTree\NT72\NT72\MenuTree.ini')
  113. value = tconfig.get_value('value', 'Setting')
  114. print value