Configuration.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ##
  2. # This is NOT the config, to get to that please look in config.py.example
  3. ##
  4. ##
  5. # This file is part of MegBot.
  6. #
  7. # MegBot is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # MegBot is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with MegBot. If not, see <http://www.gnu.org/licenses/>.
  19. ##
  20. import sys
  21. import os
  22. import json
  23. from Libraries.store import Store
  24. from types import *
  25. class Configuration(Store):
  26. FILE = "config.json"
  27. def __init__(self):
  28. self.fpath = self.FILE
  29. self.mockConfigItem = ConfigItem(self, {})
  30. self._type = DictType
  31. if not os.path.isfile(self.fpath):
  32. print "[ErrorLine] You need to move config.json.example to %s" % self.fpath
  33. sys.exit()
  34. self._sfile = open(self.fpath)
  35. data = self._sfile.read()
  36. data = json.loads(data)
  37. self._sfile.close()
  38. self.data = data
  39. for item in self.data:
  40. if type(item) in [DictType, ListType]:
  41. # make it a ConfigItem
  42. self.data[item] = ConfigItem(self, self.data[item])
  43. self._sfile = None #holder
  44. def __setitem__(self, key, value):
  45. if type(value) != type(self.mockConfigItem):
  46. value = ConfigItem(self, value)
  47. super(Configuration, self).__setitem__(key, value)
  48. # because we're not setting much we really should be
  49. # saving it back to the config.
  50. self.save(True)
  51. def save(self, pretty=False):
  52. saveable = {}
  53. for item in self.data.keys():
  54. if type(self.data[item]) == type(self.mockConfigItem):
  55. saveable[item] = self.data[item].raw()
  56. else:
  57. saveable[item] = self.data[item]
  58. super(Configuration, self).save(pretty, data=saveable)
  59. class ConfigItem(Store):
  60. def __init__(self, parent, item):
  61. self.data = item
  62. self.parent = parent
  63. for item in self.data:
  64. if type(item) in [ListType, DictType]:
  65. # make it a ConfigItem
  66. self.data[item] = ConfigItem(self, self.data[item])
  67. def __setitem__(self, key, value):
  68. self.data[key] = value
  69. self.parent.save(True)
  70. def raw(self):
  71. if type(self.data) == ListType:
  72. saveable = []
  73. for key in self.data:
  74. if type(key) == type(self.parent.mockConfigItem):
  75. saveable.append(key.__dict__)
  76. else:
  77. saveable.append(key)
  78. else:
  79. saveable = {}
  80. for key in self.data.keys():
  81. if type(self.data[key]) == type(self.parent.mockConfigItem):
  82. saveable[key] = self.data[key].raw()
  83. else:
  84. saveable[key] = self.data[key]
  85. return saveable
  86. configuration = Configuration()