From 27d2556ce9a1b9aa92d50bc26f7d8759e836ad40 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Mon, 10 Feb 2014 10:50:37 -0500 Subject: Options: Finalize actual value, not default value This also fixes some extraneous calls in the option parsing loop. --- src/lib/Bcfg2/Options/Actions.py | 3 ++- src/lib/Bcfg2/Options/Options.py | 32 ++++++++++++++++++-------------- src/lib/Bcfg2/Options/Parser.py | 3 +-- 3 files changed, 21 insertions(+), 17 deletions(-) (limited to 'src/lib/Bcfg2/Options') diff --git a/src/lib/Bcfg2/Options/Actions.py b/src/lib/Bcfg2/Options/Actions.py index 3ebe75b5f..7b85f7c4c 100644 --- a/src/lib/Bcfg2/Options/Actions.py +++ b/src/lib/Bcfg2/Options/Actions.py @@ -132,7 +132,8 @@ class ComponentAction(argparse.Action): in it. This lets a default be specified with a list of strings instead of a list of classes. """ if not self._final: - self.__call__(parser, namespace, self.default) + self.__call__(parser, namespace, getattr(namespace, self.dest, + self.default)) def __call__(self, parser, namespace, values, option_string=None): if values is None: diff --git a/src/lib/Bcfg2/Options/Options.py b/src/lib/Bcfg2/Options/Options.py index 81bd7f7d8..136511f41 100644 --- a/src/lib/Bcfg2/Options/Options.py +++ b/src/lib/Bcfg2/Options/Options.py @@ -169,7 +169,10 @@ class Option(object): the appropriate default value in the appropriate format.""" for parser, action in self.actions.items(): if hasattr(action, "finalize"): - _debug("Finalizing %s for %s" % (self, parser)) + if parser: + _debug("Finalizing %s for %s" % (self, parser)) + else: + _debug("Finalizing %s" % self) action.finalize(parser, namespace) def from_config(self, cfp): @@ -184,7 +187,6 @@ class Option(object): """ if not self.cf: return None - _debug("Setting %s from config file(s)" % self) if '*' in self.cf[1]: if cfp.has_section(self.cf[0]): # build a list of known options in this section, and @@ -194,23 +196,25 @@ class Option(object): exclude.update(o.cf[1] for o in parser.option_list if o.cf and o.cf[0] == self.cf[0]) - return dict([(o, cfp.get(self.cf[0], o)) - for o in fnmatch.filter(cfp.options(self.cf[0]), - self.cf[1]) - if o not in exclude]) + rv = dict([(o, cfp.get(self.cf[0], o)) + for o in fnmatch.filter(cfp.options(self.cf[0]), + self.cf[1]) + if o not in exclude]) else: - return dict() + rv = dict() else: + if self.type: + rtype = self.type + else: + rtype = lambda x: x try: - val = cfp.getboolean(*self.cf) + rv = rtype(cfp.getboolean(*self.cf)) except ValueError: - val = cfp.get(*self.cf) + rv = rtype(cfp.get(*self.cf)) except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): - return None - if self.type: - return self.type(val) - else: - return val + rv = None + _debug("Setting %s from config file(s): %s" % (self, rv)) + return rv def default_from_config(self, cfp): """ Set the default value of this option from the config file diff --git a/src/lib/Bcfg2/Options/Parser.py b/src/lib/Bcfg2/Options/Parser.py index cb71d7491..d855b6232 100644 --- a/src/lib/Bcfg2/Options/Parser.py +++ b/src/lib/Bcfg2/Options/Parser.py @@ -261,8 +261,7 @@ class Parser(argparse.ArgumentParser): self._set_defaults() self.parse_known_args(args=self.argv, namespace=self.namespace) self._parse_config_options() - self._parse_config_options() - self._finalize() + self._finalize() # phase 4: fix up macros _debug("Option parsing phase 4: Fix up macros") -- cgit v1.2.3-1-g7c22