config.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. import sys
  22. if sys.version_info.major > 2:
  23. from configparser import ConfigParser, ParsingError
  24. else:
  25. from ConfigParser import ConfigParser, ParsingError
  26. import copy
  27. class ConfigException(Exception):
  28. """Basic exception for config."""
  29. def __init__(self, message):
  30. self.message = message
  31. def __str__(self):
  32. return "{}: {}".format(self.__class__.__name__,
  33. self.message) # pragma: no cover
  34. class ConfigNoConfigException(ConfigException):
  35. pass
  36. _file = None
  37. _conf = dict()
  38. _defaults = dict()
  39. def set_conf(conf_dict):
  40. global _conf
  41. _conf = conf_dict
  42. def set_defaults(defaults):
  43. global _defaults
  44. _defaults = defaults
  45. def add_defaults(defaults):
  46. global _defaults
  47. for n in defaults.keys():
  48. if n not in _defaults:
  49. _defaults[n] = dict()
  50. for k in defaults[n].keys():
  51. _defaults[n][k] = defaults[n][k]
  52. def get_value(section, name):
  53. global _conf, _defaults
  54. try:
  55. return _conf[section][name]
  56. except KeyError:
  57. pass
  58. try:
  59. value = _defaults[section][name]
  60. set_value(section, name, value)
  61. return value
  62. except KeyError:
  63. pass
  64. return ''
  65. def set_value(section, name, value):
  66. global _conf
  67. if section not in _conf:
  68. _conf[section] = dict()
  69. _conf[section][name] = value
  70. def get_conf():
  71. """
  72. Get a copy of the config.
  73. Modifications have no effect.
  74. This function only serves for allowing applications
  75. to output the config to the user"""
  76. global _conf
  77. return copy.deepcopy(_conf)
  78. def load(filename):
  79. """Load configuration from 'filename'."""
  80. global _conf, _file
  81. _file = filename
  82. parser = ConfigParser()
  83. fp = None
  84. try:
  85. try:
  86. fp = open(filename, "r")
  87. parser.readfp(fp)
  88. except ParsingError as e:
  89. raise ConfigException(e)
  90. except IOError as e:
  91. raise ConfigNoConfigException(e)
  92. finally:
  93. if (fp):
  94. fp.close()
  95. for section in parser.sections():
  96. for option in parser.options(section):
  97. set_value(section, option, parser.get(section, option))
  98. def set_config(config_dict):
  99. global _conf
  100. _conf = config_dict
  101. def save(filename=None):
  102. """Save the configuration to 'filename'."""
  103. global _conf, _file
  104. if not filename:
  105. filename = _file
  106. parser = ConfigParser()
  107. for key in _conf.keys():
  108. if not parser.has_section(key):
  109. parser.add_section(key)
  110. sectiondict = _conf[key]
  111. if isinstance(sectiondict, dict):
  112. for optionkey in sectiondict.keys():
  113. parser.set(key, optionkey, sectiondict[optionkey])
  114. try:
  115. fp = file(filename, "w+")
  116. parser.write(fp)
  117. fp.close()
  118. except IOError as e:
  119. raise ConfigException(str(e))
  120. def get_pass_conf():
  121. numerics = get_value("Generator", "numerics").lower() == 'true'
  122. # TODO: allow custom leetifying through the config
  123. leetify = get_value("Generator", "leetify").lower() == 'true'
  124. special_chars = get_value("Generator", "special_chars").lower() == 'true'
  125. return numerics, leetify, special_chars