Skip to content

Commit

Permalink
config get_str_list
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-B committed Jan 18, 2021
1 parent da2ea3e commit dd1ef30
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions spinn_utilities/configs/camel_case_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ def get_str(self, section, option):
return None
return value

def get_str_list(self, section, option, token=","):
""" Get the string value of an option split into a list
:param str section: What section to get the option from.
:param str option: What option to read.
:param token: The token to split the string into a list
:return: The list (possibly empty) of the option values
:rtype: list(str)
"""
value = self.get(section, option)
if value == self._none_marker:
return []
if len(value.strip()) == 0:
return []
as_list = value.split(token)
return list(map(lambda x: x.strip(), as_list))

def get_int(self, section, option):
""" Get the integer value of an option.
Expand Down
17 changes: 17 additions & 0 deletions unittests/test_conf_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,20 @@ def parseAbc(parser):
config_parsers=[("Abc", parseAbc)])
assert config.options("Abc") == ["ghi"]
assert config.getfloat("Abc", "ghi") == 3.75


def test_str_list(tmpdir):
with tmpdir.as_cwd():
f = tmpdir.join(CFGFILE)
f.write("[abc]\n"
"as_list=bacon, is,so ,cool \n"
"as_none=None\n"
"as_empty=\n"
"fluff=more\n")
config = conf_loader.load_config(CFGFILE, [CFGPATH])
assert config.get_str_list("abc", "as_list") == \
["bacon", "is", "so", "cool"]
assert config.get_str_list("abc", "as_none") == []
assert config.get_str_list("abc", "as_empty") == []
assert config.get_str_list("abc", "fluff") == ["more"]

0 comments on commit dd1ef30

Please sign in to comment.