config.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. import os
  23. if sys.version_info.major > 2:
  24. from configparser import ConfigParser, ParsingError
  25. else:
  26. from ConfigParser import ConfigParser, ParsingError
  27. import copy
  28. config_dir = os.path.expanduser("~/.pwman")
  29. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  30. 'cls_timeout': '5',
  31. 'save': 'True'
  32. },
  33. 'Database': {'type': 'SQLite',
  34. 'filename': os.path.join(config_dir,
  35. "pwman.db")},
  36. 'Encryption': {'algorithm': 'AES'},
  37. 'Readline': {'history': os.path.join(config_dir,
  38. "history")}
  39. }
  40. class ConfigException(Exception):
  41. """Basic exception for config."""
  42. def __init__(self, message):
  43. self.message = message
  44. def __str__(self):
  45. return "{}: {}".format(self.__class__.__name__,
  46. self.message) # pragma: no cover
  47. class ConfigNoConfigException(ConfigException):
  48. pass
  49. _file = None
  50. _conf = dict()
  51. _defaults = dict()
  52. """
  53. Add a global wide defaults, without regarding any section!
  54. """
  55. defaults = {}
  56. class Config(object):
  57. def __init__(self, filename=None, defaults=None, **kwargs):
  58. self.filename = filename
  59. self.parser = self._load(defaults)
  60. def _load(self, defaults):
  61. try:
  62. parser = ConfigParser(defaults)
  63. with open(self.filename) as f:
  64. try:
  65. parser.read_file(f)
  66. except AttributeError:
  67. parser.readfp(f)
  68. except ParsingError as e:
  69. raise ConfigException(e)
  70. self._add_defaults(defaults, parser)
  71. return parser
  72. def _add_defaults(self, defaults, parser):
  73. for section, options in defaults.items():
  74. if not parser.has_section(section):
  75. parser.add_section(section)
  76. for key, value in options.items():
  77. if not parser.has_option(section, key):
  78. parser.set(section, key, value)
  79. def get_value(self, section, name):
  80. return self.parser.get(section, name)
  81. def set_value(self, section, name, value):
  82. self.parser.set(section, name, value)
  83. def set_conf(conf_dict):
  84. global _conf
  85. _conf = conf_dict
  86. def set_defaults(defaults):
  87. global _defaults
  88. _defaults = defaults
  89. def add_defaults(defaults):
  90. global _defaults
  91. for n in defaults.keys():
  92. if n not in _defaults:
  93. _defaults[n] = dict()
  94. for k in defaults[n].keys():
  95. _defaults[n][k] = defaults[n][k]
  96. def get_value(section, name):
  97. global _conf, _defaults
  98. try:
  99. return _conf[section][name]
  100. except KeyError:
  101. pass
  102. try:
  103. value = _defaults[section][name]
  104. set_value(section, name, value)
  105. return value
  106. except KeyError:
  107. pass
  108. return ''
  109. def set_value(section, name, value):
  110. global _conf
  111. if section not in _conf:
  112. _conf[section] = dict()
  113. _conf[section][name] = value
  114. def get_conf():
  115. """
  116. Get a copy of the config.
  117. Modifications have no effect.
  118. This function only serves for allowing applications
  119. to output the config to the user"""
  120. global _conf
  121. return copy.deepcopy(_conf)
  122. def load(filename):
  123. """Load configuration from 'filename'."""
  124. global _conf, _file
  125. _file = filename
  126. parser = ConfigParser()
  127. fp = None
  128. try:
  129. try:
  130. fp = open(filename, "r")
  131. try:
  132. parser.read_file(fp)
  133. except AttributeError:
  134. parser.readfp(fp)
  135. except ParsingError as e:
  136. raise ConfigException(e)
  137. except IOError as e:
  138. raise ConfigNoConfigException(e)
  139. finally:
  140. if (fp):
  141. fp.close()
  142. for section in parser.sections():
  143. for option in parser.options(section):
  144. set_value(section, option, parser.get(section, option))
  145. def set_config(config_dict):
  146. global _conf
  147. _conf = config_dict
  148. def save(filename=None):
  149. """Save the configuration to 'filename'."""
  150. global _conf, _file
  151. if not filename:
  152. filename = _file
  153. parser = ConfigParser()
  154. for key in _conf.keys():
  155. if not parser.has_section(key):
  156. parser.add_section(key)
  157. sectiondict = _conf[key]
  158. if isinstance(sectiondict, dict):
  159. for optionkey in sectiondict.keys():
  160. parser.set(key, optionkey, str(sectiondict[optionkey]))
  161. try:
  162. fp = open(filename, "w+")
  163. parser.write(fp)
  164. fp.close()
  165. except IOError as e:
  166. raise ConfigException(str(e))
  167. def get_pass_conf():
  168. numerics = get_value("Generator", "numerics").lower() == 'true'
  169. # TODO: allow custom leetifying through the config
  170. leetify = get_value("Generator", "leetify").lower() == 'true'
  171. special_chars = get_value("Generator", "special_chars").lower() == 'true'
  172. return numerics, leetify, special_chars