diff --git a/shepherd/config.py b/shepherd/config.py index 0e43298..17cd6c1 100644 --- a/shepherd/config.py +++ b/shepherd/config.py @@ -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