Skip to content

Commit

Permalink
read support for lists in ini format
Browse files Browse the repository at this point in the history
  • Loading branch information
nihlaeth committed Feb 25, 2017
1 parent 7c11086 commit 60e64e0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
17 changes: 17 additions & 0 deletions tests/test_ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,18 @@ class NestedSection(Section):
with pytest.raises(InvalidConfigTree):
ini_validate(None, OrderedDict(
nested_section=NestedSection()))
unsupported_option = StringOption()
unsupported_option.type_ = "nonsense"
with pytest.raises(InvalidConfigTree):
ini_validate(None, OrderedDict(
unsupported_option=unsupported_option))

def test_read():
config_directory = Path(__file__).parents[0] / 'test_read'
class SectionOne(Section):
string = StringOption()
multiline_string = StringOption()
list = StringListOption()
integer = IntegerOption()
float = FloatOption()
boolean = BooleanOption()
Expand All @@ -64,6 +70,7 @@ class SectionTwo(Section):
empty_integer = IntegerOption()
empty_float = FloatOption()
empty_boolean = BooleanOption()
empty_list = StringListOption()
missing_key = StringOption()
config_tree = OrderedDict([
('section_one', SectionOne()),
Expand All @@ -74,10 +81,14 @@ class SectionTwo(Section):
assert ' '.join([
word.strip() for word in config_tree[
'section_one'].multiline_string.split('\n')]) == "some lines of text"
assert config_tree['section_one'].list[0] == "one"
assert config_tree['section_one'].list[1] == "two"
assert config_tree['section_one'].list[2] == "three"
assert config_tree['section_one'].integer == 5
assert config_tree['section_one'].float == 2.3
assert config_tree['section_one'].boolean is True
assert config_tree['section_two'].empty_string is None
assert config_tree['section_two'].empty_list is None
assert config_tree['section_two'].empty_integer is None
assert config_tree['section_two'].empty_float is None
assert config_tree['section_two'].empty_boolean is None
Expand All @@ -102,6 +113,12 @@ class InvalidBooleanSection(Section):
with pytest.raises(ValueError):
ini_read(
None, config_directory / 'data_types.cfg', config_tree)
class InvalidListSection(Section):
not_a_list = StringListOption()
config_tree = OrderedDict(section_three=InvalidListSection())
with pytest.raises(ValueError):
ini_read(
None, config_directory / 'data_types.cfg', config_tree)

def test_register_extension():
result = register_extension()
Expand Down
7 changes: 7 additions & 0 deletions tests/test_read/data_types.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ multiline_string = some
lines
of
text
list = - one
- two
- three
integer = 5
float = 2.3
boolean = True
Expand All @@ -13,8 +16,12 @@ empty_string =
empty_integer =
empty_float =
empty_boolean =
empty_list =

[section_three]
not_an_integer = 5.6
not_a_float = yes
not_a_boolean = text
not_a_list = multi
line
string
20 changes: 19 additions & 1 deletion user_config/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ def ini_validate(_, elements):
if isinstance(sub_elements[sub_element], Section):
raise InvalidConfigTree(
'nested sections are not supported for ini files')
if sub_elements[sub_element].type_ not in (
str, int, float, bool, list):
raise InvalidConfigTree(
'unsupported data type {}'.format(
sub_elements[sub_element].type_))

def ini_read(_, path, elements):
"""
Expand Down Expand Up @@ -80,11 +85,24 @@ def ini_read(_, path, elements):
try:
if keys[key].type_ == bool:
keys[key].set_value(config.getboolean(section, key))
elif keys[key].type_ == list:
value = config.get(section, key).split('\n')
if len(value) == 1 and value[0] == '':
continue
result = []
for item in value:
item = item.lstrip()
if not item.startswith("- "):
raise ValueError(
'{} is not a valid ini list'.format(
config.get(section, key)))
result.append(item[2:])
keys[key].set_value(result)
elif keys[key].type_ == str:
value = config.get(section, key)
if value != '':
keys[key].set_value(value)
else:
elif keys[key].type_ == int or keys[key].type_ == float:
keys[key].set_value(
keys[key].type_(config.get(section, key)))
except ValueError:
Expand Down

0 comments on commit 60e64e0

Please sign in to comment.