configparser.py 3.2 KB

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