summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Options
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2014-03-05 13:37:02 -0500
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2014-03-05 13:37:41 -0500
commit560a262b731b53bc99a42eef8e17b16cc9630107 (patch)
tree67ccfa9fe5b1835456de51df38b25093bb9b7518 /src/lib/Bcfg2/Options
parentd432491c12c8923b1f1c110e8422e6590fd1c24a (diff)
downloadbcfg2-560a262b731b53bc99a42eef8e17b16cc9630107.tar.gz
bcfg2-560a262b731b53bc99a42eef8e17b16cc9630107.tar.bz2
bcfg2-560a262b731b53bc99a42eef8e17b16cc9630107.zip
Options: finalize ConfigFileActions
This ensures that /etc/bcfg2-web.conf gets read, even if the --web-config for [reporting].config options are not given
Diffstat (limited to 'src/lib/Bcfg2/Options')
-rw-r--r--src/lib/Bcfg2/Options/Actions.py42
1 files changed, 27 insertions, 15 deletions
diff --git a/src/lib/Bcfg2/Options/Actions.py b/src/lib/Bcfg2/Options/Actions.py
index 7b85f7c4c..0d65c584d 100644
--- a/src/lib/Bcfg2/Options/Actions.py
+++ b/src/lib/Bcfg2/Options/Actions.py
@@ -7,7 +7,27 @@ from Bcfg2.Options.Parser import get_parser
__all__ = ["ConfigFileAction", "ComponentAction", "PluginsAction"]
-class ComponentAction(argparse.Action):
+class FinalizableAction(argparse.Action):
+ """ A FinalizableAction requires some additional action to be taken
+ when storing the value, and as a result must be finalized if the
+ default value is used."""
+
+ def __init__(self, *args, **kwargs):
+ argparse.Action.__init__(self, *args, **kwargs)
+ self._final = False
+
+ def finalize(self, parser, namespace):
+ """ Finalize a default value by calling the action callable. """
+ if not self._final:
+ self.__call__(parser, namespace, getattr(namespace, self.dest,
+ self.default))
+
+ def __call__(self, parser, namespace, values, option_string=None):
+ setattr(namespace, self.dest, values)
+ self._final = True
+
+
+class ComponentAction(FinalizableAction):
""" ComponentAction automatically imports classes and modules
based on the value of the option, and automatically collects
options from the loaded classes and modules. It cannot be used by
@@ -84,8 +104,7 @@ class ComponentAction(argparse.Action):
if self.mapping:
if 'choices' not in kwargs:
kwargs['choices'] = self.mapping.keys()
- self._final = False
- argparse.Action.__init__(self, *args, **kwargs)
+ FinalizableAction.__init__(self, *args, **kwargs)
def _import(self, module, name):
""" Import the given name from the given module, handling
@@ -127,14 +146,6 @@ class ComponentAction(argparse.Action):
print("Could not load component %s" % name)
return cls
- def finalize(self, parser, namespace):
- """ Finalize a default value by loading the components given
- 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, getattr(namespace, self.dest,
- self.default))
-
def __call__(self, parser, namespace, values, option_string=None):
if values is None:
result = None
@@ -147,18 +158,19 @@ class ComponentAction(argparse.Action):
result.append(cls)
else:
result = self._load_component(values)
- self._final = True
- setattr(namespace, self.dest, result)
+ FinalizableAction.__call__(self, parser, namespace, result,
+ option_string=option_string)
-class ConfigFileAction(argparse.Action):
+class ConfigFileAction(FinalizableAction):
""" ConfigFileAction automatically loads and parses a
supplementary config file (e.g., ``bcfg2-web.conf`` or
``bcfg2-lint.conf``). """
def __call__(self, parser, namespace, values, option_string=None):
get_parser().add_config_file(self.dest, values)
- setattr(namespace, self.dest, values)
+ FinalizableAction.__call__(self, parser, namespace, values,
+ option_string=option_string)
class PluginsAction(ComponentAction):