config.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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) 2012 Oz Nahum <nahumoz@gmail.com>
  18. #============================================================================
  19. #============================================================================
  20. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  21. #============================================================================
  22. from ConfigParser import ConfigParser,ParsingError
  23. import copy
  24. class ConfigException(Exception):
  25. """Basic exception for config."""
  26. def __init__(self, message):
  27. self.message = message
  28. def __str__(self):
  29. return "%s: %s" % (self.__class__.__name__, self.message)
  30. class ConfigNoConfigException(ConfigException):
  31. pass
  32. _file = None
  33. _conf = dict()
  34. _defaults = dict()
  35. def set_defaults(defaults):
  36. global _defaults
  37. _defaults = defaults
  38. def add_defaults(defaults):
  39. global _defaults
  40. for n in defaults.keys():
  41. if not _defaults.has_key(n):
  42. _defaults[n] = dict()
  43. for k in defaults[n].keys():
  44. _defaults[n][k] = defaults[n][k]
  45. def get_value(section, name):
  46. global _conf, _defaults
  47. try:
  48. return _conf[section][name]
  49. except KeyError, e:
  50. pass
  51. try:
  52. value = _defaults[section][name]
  53. set_value(section, name, value)
  54. return value
  55. except KeyError, e:
  56. pass
  57. return ''
  58. def set_value(section, name, value):
  59. global _conf
  60. if not _conf.has_key(section):
  61. _conf[section] = dict()
  62. _conf[section][name] = value
  63. def get_conf():
  64. """Get a copy of the config. Modifications have no effect. This function only serves for allowing applications to output the config to the user"""
  65. global _conf
  66. return copy.deepcopy(_conf)
  67. def load(filename):
  68. """Load configuration from 'filename'."""
  69. global _conf, _file
  70. _file = filename
  71. parser = ConfigParser()
  72. fp = None
  73. try:
  74. try:
  75. fp = file(filename, "r")
  76. res = parser.readfp(fp)
  77. except ParsingError,e:
  78. raise ConfigException(e)
  79. except IOError, e:
  80. raise ConfigNoConfigException(e)
  81. finally:
  82. if (fp):
  83. fp.close()
  84. for section in parser.sections():
  85. for option in parser.options(section):
  86. set_value(section, option, parser.get(section, option))
  87. def save(filename=None):
  88. """Save the configuration to 'filename'."""
  89. global _conf, _file
  90. if filename == None:
  91. filename = _file
  92. parser = ConfigParser()
  93. for key in _conf.keys():
  94. if not parser.has_section(key):
  95. parser.add_section(key)
  96. sectiondict = _conf[key]
  97. if (type(sectiondict) == dict):
  98. for optionkey in sectiondict.keys():
  99. parser.set(key, optionkey, sectiondict[optionkey])
  100. try:
  101. fp = file(filename, "w+")
  102. parser.write(fp)
  103. fp.close()
  104. except IOError, e:
  105. raise ConfigException(str(e))