[node-patches] Change in ovirt-node[master]: config: Allow updating defaults using kwargs
fabiand at fedoraproject.org
fabiand at fedoraproject.org
Mon Jan 28 17:34:52 UTC 2013
Fabian Deutsch has uploaded a new change for review.
Change subject: config: Allow updating defaults using kwargs
......................................................................
config: Allow updating defaults using kwargs
Previously all arguments for th eupdate() method had to be given, now
update() also takes kwargs which allows updating of entries (and not
setting all of them).
Change-Id: I4b7b7b14e18be90de9bbc930bfed6731ef56b5fb
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M scripts/tui/src/ovirt/node/config/defaults.py
1 file changed, 42 insertions(+), 17 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/80/11480/1
diff --git a/scripts/tui/src/ovirt/node/config/defaults.py b/scripts/tui/src/ovirt/node/config/defaults.py
index 774594c..00a1a71 100644
--- a/scripts/tui/src/ovirt/node/config/defaults.py
+++ b/scripts/tui/src/ovirt/node/config/defaults.py
@@ -190,6 +190,19 @@
tx = self.transaction()
tx()
+ def _args_to_keys_mapping(self, keys_to_args=False):
+ """Map the named arguments of th eupdate() method to the CFG keys
+
+ Returns:
+ A dict mapping an argname to it's cfg key (or vice versa)
+ """
+ func = self.update.wrapped_func
+ varnames = func.func_code.co_varnames[1:]
+ assert len(varnames) == len(self.keys)
+ mapping = zip(self.keys, varnames) if keys_to_args else zip(varnames,
+ self.keys)
+ return dict(mapping)
+
def retrieve(self):
"""Returns the config keys of the current component
@@ -198,16 +211,14 @@
arg corresponds to the named arguments of the subclass's
configure() method.
"""
- func = self.update.wrapped_func
- varnames = func.func_code.co_varnames[1:]
- values = ()
+ keys_to_args = self._args_to_keys_mapping(keys_to_args=True)
cfg = self.defaults.get_dict()
+ model = {}
for key in self.keys:
value = cfg[key] if key in cfg else self.none_value
- values += (value,)
- assert len(varnames) == len(values)
- cfg = dict(zip(varnames, values))
- return cfg
+ model[keys_to_args[key]] = value
+ assert len(keys_to_args) == len(model)
+ return model
def clear(self):
"""Remove the configuration for this item
@@ -245,11 +256,20 @@
{'OVIRT_A': 1, 'OVIRT_B': 2, 'OVIRT_C': 'c3'}
"""
def wrapper(self, *args, **kwargs):
- assert kwargs == {}, "kwargs are not allowed for these functions"
- if len(self.keys) != len(args):
- raise Exception("There are not enough arguments given for " +
- "%s of %s" % (func, self))
- new_cfg = dict(zip(self.keys, args))
+ if kwargs:
+ # if kwargs are given it is interpreted as an update
+ # so existing values which are not given in the kwargs are kept
+ arg_to_key = self._args_to_keys_mapping()
+ update_kwargs = self.retrieve()
+ update_kwargs.update({k: v for k, v in kwargs.items()
+ if k in update_kwargs.keys()})
+ kwargs = update_kwargs
+ new_cfg = {arg_to_key[k]: v for k, v in update_kwargs.items()}
+ else:
+ if len(self.keys) != len(args):
+ raise Exception("There are not enough arguments given " +
+ "for %s of %s" % (func, self))
+ new_cfg = dict(zip(self.keys, args))
custom_cfg = func(self, *args, **kwargs) or {}
assert type(custom_cfg) is dict, "%s must return a dict" % func
new_cfg.update(custom_cfg)
@@ -1150,11 +1170,16 @@
>>> fn = "/tmp/cfg_dummy"
>>> cfgfile = ConfigFile(fn, SimpleProvider)
- >>> n = NFSv4(cfgfile)
- >>> domain = "foo.example"
- >>> n.update(domain)
- >>> n.retrieve().items()
- [('domain', 'foo.example')]
+ >>> n = Storage(cfgfile)
+ >>> kwargs = {"init": "/dev/sda", "root_install": "/dev/sdb"}
+ >>> n.update(**kwargs)
+ >>> data = n.retrieve().items()
+ >>> data[0:3]
+ [('efi_size', None), ('overcommit', None), ('root_install', '/dev/sdb')]
+ >>> data[3:6]
+ [('config_size', None), ('data_size', None), ('init', '/dev/sda')]
+ >>> data[6:9]
+ [('logging_size', None), ('swap_size', None), ('root_size', None)]
"""
# FIXME this key is new!
keys = ("OVIRT_INIT",
--
To view, visit http://gerrit.ovirt.org/11480
To unsubscribe, visit http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I4b7b7b14e18be90de9bbc930bfed6731ef56b5fb
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Fabian Deutsch <fabiand at fedoraproject.org>
More information about the node-patches
mailing list