Skip to content
Snippets Groups Projects
config.py 2.93 KiB
Newer Older
  • Learn to ignore specific revisions
  • ulif's avatar
    ulif committed
    #  diceware -- passphrases to remember
    
    ulif's avatar
    ulif committed
    #  Copyright (C) 2015-2017  Uli Fouquet
    
    ulif's avatar
    ulif committed
    #
    #  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
    
    ulif's avatar
    ulif committed
    
    
    OPTIONS_DEFAULTS = dict(
        num=6,
        caps=True,
        specials=0,
        delimiter="",
        randomsource="system",
    
        verbose=0,
    
        wordlist="en_eff",
    
        dice_sides=6,
    
    ulif's avatar
    ulif committed
        )
    
    ulif's avatar
    ulif committed
    
    
    
    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"), ]
    
    def get_configparser(path_list=None):
    
    ulif's avatar
    ulif committed
        """Parse `path_list` for config values.
    
    
        If no list is given we use `valid_locations()`.
    
    
    ulif's avatar
    ulif committed
        Return a list of paths read and a config parser instance.
        """
    
        if path_list is None:
    
            path_list = valid_locations()
    
        parser = SafeParser()
    
    ulif's avatar
    ulif committed
        found = parser.read(path_list)
        return found, parser
    
    
    
    def get_config_dict(
            path_list=None, defaults_dict=OPTIONS_DEFAULTS, section="diceware"):
    
    ulif's avatar
    ulif committed
        """Get config values found in files from `path_list`.
    
        Read files in `path_list` config files and return option values from
        section `section` as regular dictonary.
    
    ulif's avatar
    ulif committed
    
        We only accept values for which a default exists in
    
        `defaults_dict`. If `defaults_dict` is ``None`` we use
        ``OPTIONS_DEFAULTS``.
    
    ulif's avatar
    ulif committed
    
        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):
    
            if isinstance(val, bool):
    
                result[key] = parser.getboolean(section, key)
    
            elif isinstance(val, int):
    
                result[key] = parser.getint(section, key)
    
                result[key] = parser.get(section, key).strip("\"'")
    
        return result