config.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. #============================================================================
  17. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  18. #============================================================================
  19. from ConfigParser import ConfigParser,ParsingError
  20. import copy
  21. class ConfigException(Exception):
  22. """Basic exception for config."""
  23. def __init__(self, message):
  24. self.message = message
  25. def __str__(self):
  26. return "%s: %s" % (self.__class__.__name__, self.message)
  27. class ConfigNoConfigException(ConfigException):
  28. pass
  29. _file = None
  30. _conf = dict()
  31. _defaults = dict()
  32. def set_defaults(defaults):
  33. global _defaults
  34. _defaults = defaults
  35. def add_defaults(defaults):
  36. global _defaults
  37. for n in defaults.keys():
  38. if not _defaults.has_key(n):
  39. _defaults[n] = dict()
  40. for k in defaults[n].keys():
  41. _defaults[n][k] = defaults[n][k]
  42. def get_value(section, name):
  43. global _conf, _defaults
  44. try:
  45. return _conf[section][name]
  46. except KeyError, e:
  47. pass
  48. try:
  49. value = _defaults[section][name]
  50. set_value(section, name, value)
  51. return value
  52. except KeyError, e:
  53. pass
  54. return ''
  55. def set_value(section, name, value):
  56. global _conf
  57. if not _conf.has_key(section):
  58. _conf[section] = dict()
  59. _conf[section][name] = value
  60. def get_conf():
  61. """Get a copy of the config. Modifications have no effect. This function only serves for allowing applications to output the config to the user"""
  62. global _conf
  63. return copy.deepcopy(_conf)
  64. def load(filename):
  65. """Load configuration from 'filename'."""
  66. global _conf, _file
  67. _file = filename
  68. parser = ConfigParser()
  69. fp = None
  70. try:
  71. try:
  72. fp = file(filename, "r")
  73. res = parser.readfp(fp)
  74. except ParsingError,e:
  75. raise ConfigException(e)
  76. except IOError, e:
  77. raise ConfigNoConfigException(e)
  78. finally:
  79. if (fp):
  80. fp.close()
  81. for section in parser.sections():
  82. for option in parser.options(section):
  83. set_value(section, option, parser.get(section, option))
  84. def save(filename=None):
  85. """Save the configuration to 'filename'."""
  86. global _conf, _file
  87. if filename == None:
  88. filename = _file
  89. parser = ConfigParser()
  90. for key in _conf.keys():
  91. if not parser.has_section(key):
  92. parser.add_section(key)
  93. sectiondict = _conf[key]
  94. if (type(sectiondict) == dict):
  95. for optionkey in sectiondict.keys():
  96. parser.set(key, optionkey, sectiondict[optionkey])
  97. try:
  98. fp = file(filename, "w+")
  99. parser.write(fp)
  100. fp.close()
  101. except IOError, e:
  102. raise ConfigException(str(e))