config.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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': '10', 'cp_timeout': '5',
  32. 'save': 'True'
  33. },
  34. 'Database': {'type': 'SQLite',
  35. 'filename': os.path.join(config_dir,
  36. "pwman.db")},
  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. class Config(object):
  50. def __init__(self, filename=None, defaults=None, **kwargs):
  51. self.filename = filename
  52. self.parser = self._load(defaults)
  53. def _load(self, defaults):
  54. parser = ConfigParser()
  55. try:
  56. with open(self.filename) as f:
  57. try:
  58. parser.read_file(f)
  59. except AttributeError:
  60. parser.readfp(f)
  61. except ParsingError as e: # pragma: no cover
  62. raise ConfigException(e)
  63. except IOError:
  64. self._add_defaults(defaults, parser)
  65. self.save(os.path.join(config_dir, 'config'), parser)
  66. self._add_defaults(defaults, parser)
  67. return parser
  68. def _add_defaults(self, defaults, parser):
  69. for section, options in defaults.items():
  70. if not parser.has_section(section):
  71. parser.add_section(section)
  72. for key, value in options.items():
  73. if not parser.has_option(section, key):
  74. parser.set(section, key, value)
  75. def get_value(self, section, name):
  76. try:
  77. return self.parser.get(section, name)
  78. except (NoOptionError, NoSectionError): # pragma: no cover
  79. return ''
  80. def set_value(self, section, name, value):
  81. self.parser.set(section, name, value)
  82. def save(self, filename, parser=None): # pragma: no cover
  83. with open(filename, "w") as fp:
  84. if parser:
  85. parser.write(fp)
  86. else:
  87. self.parser.write(fp)
  88. def get_pass_conf(config): # pragma: no cover
  89. ascii_lowercase = config.get_value("Generator",
  90. "ascii_lowercase").lower() == 'true'
  91. ascii_uppercase = config.get_value("Generator",
  92. "ascii_uppercase").lower() == 'true'
  93. ascii_digits = config.get_value("Generator",
  94. "ascii_digits").lower() == 'true'
  95. ascii_punctuation = config.get_value("Generator",
  96. "ascii_punctuation").lower() == 'true'
  97. return ascii_lowercase, ascii_uppercase, ascii_digits, ascii_punctuation