From 018e2b3e86221a7700c8dac074d957141f0e9d59 Mon Sep 17 00:00:00 2001 From: Nics Date: Wed, 14 Jun 2017 17:29:09 +0200 Subject: [PATCH 1/3] Add parsing for python list Not tested (obviously), hope the unit tests are good enough. --- confiture/__init__.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/confiture/__init__.py b/confiture/__init__.py index 9131a5c..c75a50d 100644 --- a/confiture/__init__.py +++ b/confiture/__init__.py @@ -11,11 +11,12 @@ def __init__(self, message): class Confiture(object): - def __init__(self, tpl_file=None): + def __init__(self, tpl_file=None, list_sep="|"): self._tpl_file = tpl_file self.__tpl = None self.__parse() self.config = None + self.list_sep = list_sep @property def tpl_file(self): @@ -43,13 +44,26 @@ def __check_required_fields(self, tpl, config): field = tpl[section] if isinstance(field, dict): self.__check_required_fields(field, config[section]) - + + def list_parse(self, config): + """ + Parse recursively the config file searching for a string describing a list + and turning it into a python list + """ + if not isinstance(config, dict): + return config.split(self.list_sep) + else: + for k in config: + config[k] = self.list_parse(config[k]) + return config + def check(self, config_path): if self.__tpl is None: raise ConfigFileError("You must load a template file first -- aborting") try: with open(config_path, 'r') as ymlfile: self.config = yaml.safe_load(ymlfile) + self.config = self.list_parse(self.config) except (IOError, yaml.error.YAMLError): raise ConfigFileError("File \"{0}\" not found -- aborting".format(config_path)) self.__check_required_fields(self.__tpl, self.config) From 4f819c86a965e4b6524ea640d29e26be03e87412 Mon Sep 17 00:00:00 2001 From: Nics Date: Wed, 14 Jun 2017 17:37:26 +0200 Subject: [PATCH 2/3] Create __init__.py --- confiture/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/confiture/__init__.py b/confiture/__init__.py index c75a50d..45c8aa0 100644 --- a/confiture/__init__.py +++ b/confiture/__init__.py @@ -51,7 +51,7 @@ def list_parse(self, config): and turning it into a python list """ if not isinstance(config, dict): - return config.split(self.list_sep) + return str(config).split(self.list_sep) else: for k in config: config[k] = self.list_parse(config[k]) From dfc449e6e6f7a2b7127f99601a2d97d2809ad787 Mon Sep 17 00:00:00 2001 From: Nics Date: Thu, 15 Jun 2017 11:16:14 +0200 Subject: [PATCH 3/3] Yeah, I know, there was a bug --- confiture/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/confiture/__init__.py b/confiture/__init__.py index 45c8aa0..72a5809 100644 --- a/confiture/__init__.py +++ b/confiture/__init__.py @@ -51,7 +51,10 @@ def list_parse(self, config): and turning it into a python list """ if not isinstance(config, dict): - return str(config).split(self.list_sep) + if self.list_sep in str(config): + return str(config).split(self.list_sep) + else: + return config else: for k in config: config[k] = self.list_parse(config[k])