diff --git a/diceware/config.py b/diceware/config.py index be9e968b727fa1722cc78f50940e78db0d3c31da..6f1ebd87f268ca997f71aebf7f7c045c71a10d9e 100644 --- a/diceware/config.py +++ b/diceware/config.py @@ -46,5 +46,15 @@ def valid_locations(): return result +def get_configparser(path_list): + """Parse `path_list` for config values. + + Return a list of paths read and a config parser instance. + """ + parser = configparser.SafeConfigParser() + found = parser.read(path_list) + return found, parser + + class DicewareConfigParser(configparser.SafeConfigParser): pass diff --git a/tests/test_config.py b/tests/test_config.py index 593543376cb6841979934f84debfa114a0d9e83b..7c6c8e39da02fc07c9e035f9847fb9bab5acb3dc 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,4 +1,6 @@ -from diceware.config import OPTIONS_DEFAULTS, valid_locations +from diceware.config import ( + OPTIONS_DEFAULTS, valid_locations, get_configparser, + ) class TestConfigModule(object): @@ -16,3 +18,15 @@ class TestConfigModule(object): assert valid_locations() == [ str(new_home / ".diceware.ini") ] + + def test_get_configparser(self, tmpdir): + # we can parse simple configs + conf_path = tmpdir / "sample.ini" + conf_path.write("\n".join(["[diceware]", "num=1", ""])) + found, config = get_configparser([str(conf_path), ]) + assert found == [str(conf_path)] + + def test_get_configparser_empty_list(self): + # we cope with empty config file lists + found, config = get_configparser([]) + assert found == []