Newer
Older
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""config -- diceware configuration
`diceware` is configurable via commandline, configuration files and
direct API calls.
"""
try:
from configparser import ConfigParser as SafeParser # Python 3.x
except ImportError: # pragma: no cover
from ConfigParser import SafeConfigParser as SafeParser # Python 2.x
OPTIONS_DEFAULTS = dict(
num=6,
caps=True,
specials=0,
delimiter="",
randomsource="system",
def valid_locations():
"""The list of valid paths we look up for config files.
"""
user_home = os.path.expanduser("~")
result = []
if user_home != "~":
result = [os.path.join(user_home, ".diceware.ini"), ]
If no list is given we use `valid_locations()`.
Return a list of paths read and a config parser instance.
"""
path_list = valid_locations()
found = parser.read(path_list)
return found, parser
def get_config_dict(
path_list=None, defaults_dict=OPTIONS_DEFAULTS, section="diceware"):
Read files in `path_list` config files and return option values from
section `section` as regular dictonary.
`defaults_dict`. If `defaults_dict` is ``None`` we use
``OPTIONS_DEFAULTS``.
Values are interpolated to have same value type as same-named values
from `defaults_dict` if they are integers or boolean.
String/text values are stripped from preceding/trailing quotes
(single and double).
result = dict(defaults_dict)
found, parser = get_configparser(path_list)
for key, val in defaults_dict.items():
if not parser.has_option(section, key):
result[key] = parser.getboolean(section, key)
result[key] = parser.getint(section, key)
result[key] = parser.get(section, key).strip("\"'")