config.py 3.6 KB

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