Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions fastargs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self):
self.entries = {}
self.content = {}


def add_section(self, section):
self.sections[section.ns] = section

Expand Down Expand Up @@ -239,3 +240,14 @@ def summary(self, target=sys.stderr):

return self

def as_dict(self):
""" Stores config in a dict, like 'section.parameter' -> value """
output = {}
for path in self.entries.keys():
try:
value = self[path]
if value is not None:
output['.'.join(path)] = self[path]
except:
pass
return output
18 changes: 18 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ def test_summary(self):
self.assertIn('3', output)
self.assertNotIn('first.sec.param3', output)

def test_as_dict(self):
Section('first_sec', 'test_sec1').params(
param1=Param(Anything(), required=True),
param2=Param(Anything(), default='3'),
param3=Param(Anything(), required=False)
)

cfg = get_current_config().collect({
'first_sec.param1': 42
})

output_dict = cfg.as_dict()
target = {'first_sec.param1': 42,
'first_sec.param2': '3'}

self.assertDictEqual(output_dict, target)


def test_conditional_arguments(self):
Section('a').params(
value=Param(int)
Expand Down