config.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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) 2018 Oz N Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  20. # ============================================================================
  21. import os
  22. import platform
  23. import sys
  24. from functools import lru_cache
  25. if sys.version_info.major > 2: # pragma: no cover
  26. from configparser import (ConfigParser, ParsingError, NoOptionError,
  27. NoSectionError, MissingSectionHeaderError)
  28. else: # pragma: no cover
  29. from ConfigParser import (ConfigParser, ParsingError, NoOptionError,
  30. NoSectionError, MissingSectionHeaderError)
  31. # XDG code taken from xdg.py
  32. # https://github.com/srstevenson/xdg/blob/master/xdg.py
  33. # Copyright © 2016-2018 Scott Stevenson <scott@stevenson.io>
  34. def _getenv(variable: str, default: str) -> str:
  35. """Get an environment variable.
  36. Parameters
  37. ----------
  38. variable : str
  39. The environment variable.
  40. default : str
  41. A default value that will be returned if the environment
  42. variable is unset or empty.
  43. Returns
  44. -------
  45. str
  46. The value of the environment variable, or the default value.
  47. """
  48. return os.environ.get(variable) or default
  49. XDG_CACHE_HOME = _getenv('XDG_CACHE_HOME',
  50. os.path.expandvars(os.path.join('$HOME', '.cache')))
  51. XDG_CONFIG_DIRS = _getenv('XDG_CONFIG_DIRS', '/etc/xdg').split(':')
  52. XDG_CONFIG_HOME = _getenv('XDG_CONFIG_HOME',
  53. os.path.expandvars(os.path.join('$HOME', '.config')))
  54. XDG_DATA_DIRS = _getenv('XDG_DATA_DIRS',
  55. '/usr/local/share/:/usr/share/').split(':')
  56. XDG_DATA_HOME = _getenv('XDG_DATA_HOME',
  57. os.path.expandvars(
  58. os.path.join('$HOME', '.local', 'share')))
  59. XDG_RUNTIME_DIR = os.getenv('XDG_RUNTIME_DIR')
  60. @lru_cache(maxsize=None)
  61. def find_config_dir(appname):
  62. """
  63. Backward compatibly config dir finder
  64. If ~/.appname is not found define a new XDG compat one
  65. """
  66. config_dir = os.path.expanduser("~/.%s" % appname)
  67. if os.path.exists(config_dir):
  68. return config_dir
  69. elif platform.system() == 'Windows':
  70. return os.path.expandvars(os.path.join('$APPDATA', appname))
  71. else:
  72. return os.path.join(XDG_CONFIG_HOME, appname)
  73. config_dir = find_config_dir('pwman')
  74. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  75. 'cls_timeout': '10', 'cp_timeout': '5',
  76. 'save': 'True', 'supress_version_check': 'no',
  77. 'lock_timeout': '600'
  78. },
  79. 'Database': {
  80. 'dburi': 'sqlite://' + os.path.join(config_dir,
  81. 'pwman.db')},
  82. 'Readline': {'history': os.path.join(config_dir,
  83. 'history')},
  84. 'Crypto': {'supress_warning': 'no'},
  85. 'Updater': {'supress_version_check': 'no'}
  86. }
  87. if 'win' in sys.platform:
  88. default_config['Database']['dburi'] = default_config['Database']['dburi'].replace("\\", "/") # noqa
  89. class ConfigException(Exception):
  90. """Basic exception for config."""
  91. def __init__(self, message):
  92. self.message = message
  93. def __str__(self):
  94. return "{}: {}".format(self.__class__.__name__,
  95. self.message) # pragma: no cover
  96. class ConfigNoConfigException(ConfigException):
  97. pass
  98. class Config(object):
  99. def __init__(self, filename=None, defaults=None, **kwargs):
  100. self.filename = filename
  101. self.parser = self._load(defaults)
  102. def _load(self, defaults):
  103. defaults = defaults or default_config
  104. parser = ConfigParser()
  105. try:
  106. with open(self.filename) as f:
  107. try:
  108. try:
  109. parser.read_file(f)
  110. except AttributeError:
  111. parser.readfp(f)
  112. except (ParsingError, MissingSectionHeaderError) as e:
  113. raise ConfigException(e)
  114. except IOError:
  115. self._self_write_new_conf(self.filename, defaults, parser)
  116. self._add_defaults(defaults, parser)
  117. return parser
  118. def _self_write_new_conf(self, filename, defaults, parser):
  119. self.parser = parser
  120. self._add_defaults(defaults, parser)
  121. self.save()
  122. def _add_defaults(self, defaults, parser):
  123. for section, options in defaults.items():
  124. if not parser.has_section(section):
  125. parser.add_section(section)
  126. for key, value in options.items():
  127. if not parser.has_option(section, key):
  128. parser.set(section, key, value)
  129. def get_value(self, section, name):
  130. try:
  131. return self.parser.get(section, name)
  132. except (NoOptionError, NoSectionError): # pragma: no cover
  133. return ''
  134. def set_value(self, section, name, value):
  135. self.parser.set(section, name, value)
  136. def save(self):
  137. if "False" not in self.get_value("Global", "Save"):
  138. with open(self.filename, "w") as fp:
  139. self.parser.write(fp)
  140. def get_pass_conf(config):
  141. ascii_lowercase = config.get_value("Generator",
  142. "ascii_lowercase").lower() == 'true'
  143. ascii_uppercase = config.get_value("Generator",
  144. "ascii_uppercase").lower() == 'true'
  145. ascii_digits = config.get_value("Generator",
  146. "ascii_digits").lower() == 'true'
  147. ascii_punctuation = config.get_value("Generator",
  148. "ascii_punctuation").lower() == 'true'
  149. return ascii_lowercase, ascii_uppercase, ascii_digits, ascii_punctuation