summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander.sulfrian@fu-berlin.de>2016-07-12 00:24:23 +0200
committerAlexander Sulfrian <alexander.sulfrian@fu-berlin.de>2017-03-21 17:26:08 +0100
commit257ba8d92eda8be0a347f3d68b174a2354782578 (patch)
treebc09b71997aa338354de39879e4b83ad1c5352ad
parentfb7d6c605c46aa6373e8b3cf121527acf011b980 (diff)
downloadbcfg2-257ba8d92eda8be0a347f3d68b174a2354782578.tar.gz
bcfg2-257ba8d92eda8be0a347f3d68b174a2354782578.tar.bz2
bcfg2-257ba8d92eda8be0a347f3d68b174a2354782578.zip
Server/Plugin: Support functions and values for OnDemandDict
Now you can also specify simple values for the OnDemandDict.
-rw-r--r--src/lib/Bcfg2/Server/Plugin/helpers.py13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/lib/Bcfg2/Server/Plugin/helpers.py b/src/lib/Bcfg2/Server/Plugin/helpers.py
index 17363a675..9ef4a2527 100644
--- a/src/lib/Bcfg2/Server/Plugin/helpers.py
+++ b/src/lib/Bcfg2/Server/Plugin/helpers.py
@@ -1707,13 +1707,14 @@ class OnDemandDict(MutableMapping):
:func:`Bcfg2.Server.Plugins.Packages.Packages.get_additional_data`;
see the docstring for that function for details on why.
- Unlike a dict, you should not specify values for the righthand
- side of this mapping, but functions that get values. E.g.:
+ Unlike a dict, you can specify values or functions for the
+ righthand side of this mapping. If you specify a function, it will
+ be evaluated, when you access the value for the first time. E.g.:
.. code-block:: python
d = OnDemandDict(foo=load_foo,
- bar=lambda: "bar");
+ bar="bar")
"""
def __init__(self, **getters):
@@ -1722,7 +1723,11 @@ class OnDemandDict(MutableMapping):
def __getitem__(self, key):
if key not in self._values:
- self._values[key] = self._getters[key]()
+ if callable(self._getters[key]):
+ self._values[key] = self._getters[key]()
+ else:
+ self._values[key] = self._getters[key]
+
return self._values[key]
def __setitem__(self, key, getter):