TConfig.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 ConfigParser
  9. # class MyConfigParser(ConfigParser.ConfigParser):
  10. # def __init__(self, defaults=None):
  11. # ConfigParser.ConfigParser.__init__(self, defaults=defaults)
  12. #
  13. # def optionxform(self, optionstr):
  14. # return optionstr
  15. class TConfig:
  16. def __init__(self, ini_path):
  17. self.path = ini_path
  18. self.cfgParser = ConfigParser()
  19. # self.cfgParser = MyConfigParser()
  20. # self.cfgParser.read(ini_path)
  21. # 保存修改;
  22. def save_config(self):
  23. with open(self.path, 'wb') as configfile:
  24. self.cfgParser.write(configfile)
  25. # 字符转字典;
  26. def get_dict(self, value):
  27. return json.loads(value)
  28. def get_value_dict(self, section, option):
  29. value = self.get_value(section, option)
  30. if value is None:
  31. return {}
  32. return json.loads(value)
  33. # 获取参数值;默认返回字符串
  34. def get_value(self, section, option):
  35. # 读取配置文件;
  36. self.cfgParser.read(self.path)
  37. if self.cfgParser.has_option(section, option):
  38. return self.cfgParser.get(section, option)
  39. return None
  40. # 获取参数值;
  41. def get_int(self, section, option):
  42. # 读取配置文件;
  43. self.cfgParser.read(self.path)
  44. if self.cfgParser.has_option(section, option):
  45. return self.cfgParser.getint(section, option)
  46. return None
  47. # 获取参数值;
  48. def get_float(self, section, option):
  49. # 读取配置文件;
  50. self.cfgParser.read(self.path)
  51. if self.cfgParser.has_option(section, option):
  52. return self.cfgParser.getfloat(section, option)
  53. return None
  54. # 获取参数值;
  55. def get_boolean(self, section, option):
  56. # 读取配置文件;
  57. self.cfgParser.read(self.path)
  58. if self.cfgParser.has_option(section, option):
  59. return self.cfgParser.getboolean(section, option)
  60. return None
  61. # 获取键值;
  62. def get_options(self, section):
  63. # 读取配置文件;
  64. self.cfgParser.read(self.path)
  65. if self.cfgParser.has_section(section):
  66. return self.cfgParser.options(section)
  67. return None
  68. # 获取所有sections;
  69. def get_sections(self):
  70. # 读取配置文件;
  71. self.cfgParser.read(self.path)
  72. return self.cfgParser.sections()
  73. # 获取指定section所有items;
  74. def get_items(self, section):
  75. # 读取配置文件;
  76. self.cfgParser.read(self.path)
  77. if self.cfgParser.has_section(section):
  78. return self.cfgParser.items(section)
  79. return None
  80. # 添加section;
  81. def add_section(self, section):
  82. # 读取配置文件;
  83. self.cfgParser.read(self.path)
  84. if self.cfgParser.has_section(section) is False:
  85. self.cfgParser.add_section(section)
  86. # 保存修改;
  87. self.save_config()
  88. # 设置值;
  89. def set_value(self, section, option, value):
  90. # 读取配置文件;
  91. self.cfgParser.read(self.path)
  92. if self.cfgParser.has_section(section) is False:
  93. self.cfgParser.add_section(section)
  94. self.cfgParser.set(section, option, value)
  95. # 保存修改;
  96. self.save_config()
  97. # 删除指定option;
  98. def del_option(self, section, option):
  99. # 读取配置文件;
  100. self.cfgParser.read(self.path)
  101. self.cfgParser.remove_option(section, option)
  102. # 保存修改;
  103. self.save_config()
  104. # 删除section;
  105. def del_section(self, section):
  106. # 读取配置文件;
  107. self.cfgParser.read(self.path)
  108. self.cfgParser.remove_section(section)
  109. # 保存修改;
  110. self.save_config()
  111. def has_section(self, section):
  112. # 读取配置文件;
  113. self.cfgParser.read(self.path)
  114. return self.cfgParser.has_section(section)
  115. def has_option(self, section, option):
  116. # 读取配置文件;
  117. self.cfgParser.read(self.path)
  118. return self.cfgParser.has_option(section, option)
  119. def getParentWaitTime(self, parent):
  120. optionName = parent + "WaitTime"
  121. try:
  122. waitTime = float(self.get_value("waitTime", optionName))
  123. except Exception:
  124. waitTime = 1.0
  125. print "获取%s界面的等待界面时间失败,使用默认值1.0s" % str(parent)
  126. return waitTime
  127. def getThresholdDict(self, option):
  128. section = "Threshold"
  129. thresholdDict = {}
  130. value = self.get_value(section, option)
  131. print "value:", value, type(value)
  132. if not value:
  133. return thresholdDict
  134. value_str = str(value)
  135. print "value_str:", value_str, type(value_str)
  136. value_dict = eval(value_str)
  137. print "value_dict:", value_dict, type(value_dict)
  138. return value_dict
  139. if __name__ == "__main__":
  140. # tconfig = TConfig(r'E:\SAT\SAT_API\ssat_sdk\MenuTree3\MenuTree.ini')
  141. tconfig = TConfig(r'E:\SAT\resource\MenuTree\NT72\DVB_nafrican\MenuTree.ini')
  142. # print tconfig.get_options('First')
  143. # print tconfig.get_sections()
  144. # print tconfig.get_items('First')
  145. value = tconfig.get_value('value', 'Setting')
  146. print value
  147. # print tconfig.get_dict(value)
  148. # print tconfig.set_value('First', 'fffff','aaaa')
  149. # tconfig.del_option('First','fffff')
  150. # tconfig.del_section('Second')
  151. # tconfig.set_value('second','setting','{"offset":10, "minPeri":0, "maxPeri":0, "minArea":0, "maxArea":0}')