INIConfig.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. # 功能基类,请勿添加外部逻辑功能;
  9. class CINIConfig():
  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(self.path)
  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(self.path)
  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(self.path)
  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(self.path)
  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(self.path)
  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(self.path)
  64. return self.cfgParser.sections()
  65. # 获取指定section所有items;
  66. def get_items(self, section):
  67. # 读取配置文件;
  68. self.cfgParser.read(self.path)
  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(self.path)
  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(self.path)
  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(self.path)
  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(self.path)
  100. self.cfgParser.remove_section(section)
  101. # 保存修改;
  102. self.save_config()
  103. # 是否有section存在;
  104. def has_section(self, section):
  105. # 读取配置文件;
  106. self.cfgParser.read(self.path)
  107. return self.cfgParser.has_section(section)
  108. # 某section下是否有option存在;
  109. def has_option(self, section, option):
  110. # 读取配置文件;
  111. self.cfgParser.read(self.path)
  112. return self.cfgParser.has_option(section, option)
  113. if __name__ == "__main__":
  114. # tconfig = TConfig(r'E:\SAT\SAT_API\ssat_sdk\MenuTree3\MenuTree.ini')
  115. tconfig = CINIConfig(r'E:\SAT\resource\MenuTree\NT72\DVB_nafrican\MenuTree.ini')
  116. # print tconfig.get_options('First')
  117. # print tconfig.get_sections()
  118. # print tconfig.get_items('First')
  119. value = tconfig.get_value('value', 'Setting')
  120. print value
  121. # print tconfig.get_dict(value)
  122. # print tconfig.set_value('First', 'fffff','aaaa')
  123. # tconfig.del_option('First','fffff')
  124. # tconfig.del_section('Second')
  125. # tconfig.set_value('second','setting','{"offset":10, "minPeri":0, "maxPeri":0, "minArea":0, "maxArea":0}')