Config - added template generation

master
Tom Wilson 6 years ago
parent a53d63dbfa
commit 98a71f1da0

@ -142,7 +142,30 @@ class DictDef(_ConfigDefinition):
except InvalidConfigError as e:
e.args = ("Key: " + key,) + e.args
raise
def get_template(self, include_optional=False):
"""
Return a config dict with the minimum structure required for this ConfigDefinition.
Default values will be included, though not all required fields will necessarily have
defaults that successfully validate.
Args:
include_optional: If set true, will include *all* config fields, not just the
required ones
Returns:
Dict containing the structure that should be passed back in (with values) to comply
with this ConfigDefinition.
"""
template = {}
for key, confdef in self.def_dict.items():
if confdef.optional and (not include_optional):
continue
if hasattr(confdef,"get_template"):
template[key]=confdef.get_template(include_optional)
else:
template[key]=confdef.default
return template
class _ListDefMixin():
@ -156,6 +179,12 @@ class _ListDefMixin():
e.args = ("List index: " + str(index),) + e.args
raise
def get_template(self, include_optional=False):
if hasattr(super(),"get_template"):
return [super().get_template(include_optional)]
else:
return [self.default]
@freezedryable
class BoolListDef(_ListDefMixin, BoolDef):
pass

Loading…
Cancel
Save