configparser.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. class ConfigParser():
  2. content = ''
  3. config_dict = {}
  4. def _parse(self):
  5. self.content = self.content.replace('\r', '')
  6. content = self.content
  7. lines = content.split('\n')
  8. for line in lines:
  9. if line.startswith('#'):
  10. continue
  11. if line.startswith(';'):
  12. continue
  13. if line.startswith('['):
  14. section = line.replace('[', '')
  15. section = section.replace(']', '')
  16. self.config_dict[section] = {}
  17. continue
  18. if line.strip() == '':
  19. continue
  20. stmt = line.split('=')
  21. key = stmt[0].strip()
  22. value = _getvalue(stmt).strip()
  23. section_dict = self.config_dict[section]
  24. section_dict[key] = value
  25. def sections(self):
  26. section_keys = self.config_dict.keys()
  27. sections = []
  28. for section_item in section_keys:
  29. sections.append(section_item)
  30. return sections
  31. def options(self, section):
  32. section_dict = self.config_dict[section]
  33. option_keys = section_dict.keys()
  34. options = []
  35. for option_item in option_keys:
  36. options.append(option_item)
  37. return options
  38. def get(self, section, option):
  39. section_dict = self.config_dict[section]
  40. return section_dict[option]
  41. def set(self, section, option, value):
  42. section_dict = self.config_dict[section]
  43. section_dict[option] = value
  44. # support config[key] = val
  45. def __setitem__(self, __key, __val):
  46. self.config_dict[__key] = __val
  47. # support val = config[key]
  48. def __getitem__(self, __key):
  49. return self.config_dict[__key]
  50. def items(self, section):
  51. section_dict = self.config_dict[section]
  52. section_keys = section_dict.keys()
  53. items = []
  54. for key in section_keys:
  55. val = section_dict[key]
  56. items.append([key, val])
  57. return items
  58. def __str__(self):
  59. content = ''
  60. section_keys = self.config_dict.keys()
  61. for section_item in section_keys:
  62. content += '[' + section_item + ']\n'
  63. section_dict = self.config_dict[section_item]
  64. section_keys = section_dict.keys()
  65. for key in section_keys:
  66. val = section_dict[key]
  67. content += key + ' = ' + val + '\n'
  68. content += '\n'
  69. return content
  70. def write(self, file_name):
  71. print('Error: write() method not implemented')
  72. raise
  73. self.content = self.__str__(self)
  74. print(self.content)
  75. def read_string(self, content):
  76. self.content = content
  77. self._parse()
  78. def read(self, file_name):
  79. print('Error: read() method not implemented')
  80. raise
  81. content = ''
  82. self.content = content
  83. self._parse()
  84. def _getvalue(stmt):
  85. index = 0
  86. val = ''
  87. for item in stmt:
  88. if index > 0:
  89. if val != '':
  90. val += ('=' + item)
  91. else:
  92. val += item
  93. index = index + 1
  94. return val