config.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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: # pragma: no cover
  24. from configparser import (ConfigParser, ParsingError, NoOptionError,
  25. NoSectionError)
  26. else: # pragma: no cover
  27. from ConfigParser import (ConfigParser, ParsingError, NoOptionError,
  28. NoSectionError)
  29. config_dir = os.path.expanduser("~/.pwman")
  30. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  31. 'cls_timeout': '5',
  32. 'save': 'True'
  33. },
  34. 'Database': {'type': 'SQLite',
  35. 'filename': os.path.join(config_dir,
  36. "pwman.db")},
  37. 'Encryption': {'algorithm': 'AES'},
  38. 'Readline': {'history': os.path.join(config_dir,
  39. "history")}
  40. }
  41. class ConfigException(Exception):
  42. """Basic exception for config."""
  43. def __init__(self, message):
  44. self.message = message
  45. def __str__(self):
  46. return "{}: {}".format(self.__class__.__name__,
  47. self.message) # pragma: no cover
  48. class ConfigNoConfigException(ConfigException):
  49. pass
  50. class Config(object):
  51. def __init__(self, filename=None, defaults=None, **kwargs):
  52. self.filename = filename
  53. self.parser = self._load(defaults)
  54. def _load(self, defaults):
  55. try:
  56. parser = ConfigParser(defaults)
  57. with open(self.filename) as f:
  58. try:
  59. parser.read_file(f)
  60. except AttributeError:
  61. parser.readfp(f)
  62. except ParsingError as e: # pragma: no cover
  63. raise ConfigException(e)
  64. self._add_defaults(defaults, parser)
  65. return parser
  66. def _add_defaults(self, defaults, parser):
  67. for section, options in defaults.items():
  68. if not parser.has_section(section):
  69. parser.add_section(section)
  70. for key, value in options.items():
  71. if not parser.has_option(section, key):
  72. parser.set(section, key, value)
  73. def get_value(self, section, name):
  74. try:
  75. return self.parser.get(section, name)
  76. except (NoOptionError, NoSectionError): # pragma: no cover
  77. return ''
  78. def set_value(self, section, name, value):
  79. self.parser.set(section, name, value)
  80. def save(self, filename):
  81. with open(filename, "w+") as fp:
  82. self.parser.write(fp)
  83. def get_pass_conf(config):
  84. numerics = config.get_value("Generator", "numerics").lower() == 'true'
  85. # TODO: allow custom leetifying through the config
  86. leetify = config.get_value("Generator", "leetify").lower() == 'true'
  87. special_chars = config.get_value("Generator",
  88. "special_chars").lower() == 'true'
  89. return numerics, leetify, special_chars