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