From fc40a40ae0b4bb884f50443187ec2852b1766553 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Wed, 4 Sep 2013 22:11:42 +0200 Subject: [PATCH] implement configurable default values for parameters Signed-off-by: Steven Armstrong --- cdist/core/cdist_type.py | 17 +++++++++++++++++ cdist/emulator.py | 6 ++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cdist/core/cdist_type.py b/cdist/core/cdist_type.py index b6ba4f00..46e126f9 100644 --- a/cdist/core/cdist_type.py +++ b/cdist/core/cdist_type.py @@ -62,6 +62,7 @@ class CdistType(object): self.__optional_parameters = None self.__optional_multiple_parameters = None self.__boolean_parameters = None + self.__parameter_defaults = None @classmethod def list_types(cls, base_path): @@ -190,3 +191,19 @@ class CdistType(object): finally: self.__boolean_parameters = parameters return self.__boolean_parameters + + @property + def parameter_defaults(self): + if not self.__parameter_defaults: + defaults = {} + try: + defaults_dir = os.path.join(self.absolute_path, "parameter", "default") + for name in os.listdir(defaults_dir): + with open(os.path.join(defaults_dir, name)) as fd: + defaults[name] = fd.read().strip() + except EnvironmentError: + # error ignored + pass + finally: + self.__parameter_defaults = defaults + return self.__parameter_defaults diff --git a/cdist/emulator.py b/cdist/emulator.py index f1f4b622..b1cd8f2d 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -96,10 +96,12 @@ class Emulator(object): parser.add_argument(argument, dest=parameter, action='append', required=True) for parameter in self.cdist_type.optional_parameters: argument = "--" + parameter - parser.add_argument(argument, dest=parameter, action='store', required=False) + parser.add_argument(argument, dest=parameter, action='store', required=False, + default=self.cdist_type.parameter_defaults.get(parameter, None)) for parameter in self.cdist_type.optional_multiple_parameters: argument = "--" + parameter - parser.add_argument(argument, dest=parameter, action='append', required=False) + parser.add_argument(argument, dest=parameter, action='append', required=False, + default=self.cdist_type.parameter_defaults.get(parameter, None)) for parameter in self.cdist_type.boolean_parameters: argument = "--" + parameter parser.add_argument(argument, dest=parameter, action='store_const', const='')