From 0df6eafeaf7a407e5f300e43b9e42d4a91b33cb7 Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Sun, 14 Apr 2019 22:11:07 -0700 Subject: [PATCH] Add function for parsing a list from a config option --- rbackup/config.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/rbackup/config.py b/rbackup/config.py index d6c4be6..e3f769c 100644 --- a/rbackup/config.py +++ b/rbackup/config.py @@ -4,6 +4,7 @@ :synopsis: Functions for handling config files. """ import configparser +import json import logging import re from contextlib import contextmanager @@ -39,6 +40,27 @@ def get_files_by_suffix(suffix): yield from CONFIG_DIR.glob(f"*{suffix}") +def load_list_from_option(parser, *, section="", option="", fallback=list()): + """Using a combination of ``ConfigParser`` and JSON, load a + list from a configuration file option. + + :param parser: the parsed config file + :type parser: ``ConfigParser`` object + :param section: the section of the config file to load + :type section: str + :param option: the option value inside the specified section + :type option: str + :returns: the list parsed by JSON + :param fallback: the fallback value to return if the option is empty + :type fallback: list + :rtype: list + """ + try: + return json.loads(parser[section][option]) + except (json.decoder.JSONDecodeError, KeyError): + return fallback + + def merge_files(files): """Parse, filter, and sort through config files to create a single --files-from argument. @@ -90,8 +112,8 @@ def merge_exclude_files(): def parse_configfile(): - """Parse a config file given its path and return - a ConfigParser object. + """Parse the main backup config file and return + a ``configparser.ConfigParser`` object. :returns: object used to parse config file :rtype: ConfigParser object