Mon, Aug 20, 2018 at 01:27:48PM CEST, olichtne(a)redhat.com wrote:
From: Ondrej Lichtner <olichtne(a)redhat.com>
RecipeParam class is a Param class that points (by name) to a different
Param object.
This can be used to parametrize the Requirements of a Recipe using it's
parameters, e.g. driver value.
Signed-off-by: Ondrej Lichtner <olichtne(a)redhat.com>
---
lnst/Controller/Recipe.py | 11 +++++---
lnst/Controller/Requirements.py | 47 ++++++++++++++++++++++++++++++++-
lnst/Controller/__init__.py | 2 +-
3 files changed, 55 insertions(+), 5 deletions(-)
diff --git a/lnst/Controller/Recipe.py b/lnst/Controller/Recipe.py
index ddd8016..080dd46 100644
--- a/lnst/Controller/Recipe.py
+++ b/lnst/Controller/Recipe.py
@@ -87,9 +87,7 @@ class BaseRecipe(object):
self.params = Parameters()
for attr in dir(self):
val = getattr(self, attr)
- if isinstance(val, HostReq):
- setattr(self.req, attr, copy.deepcopy(val))
- elif isinstance(val, Param):
+ if isinstance(val, Param):
if attr in kwargs:
param_val = kwargs.pop(attr)
param_val = val.type_check(param_val)
@@ -103,6 +101,13 @@ class BaseRecipe(object):
raise RecipeError("Parameter {} is mandatory"
.format(attr))
+ for attr in dir(self):
+ val = getattr(self, attr)
+ if isinstance(val, HostReq):
+ new_val = copy.deepcopy(val)
+ new_val.reinit_with_params(self.params)
+ setattr(self.req, attr, new_val)
+
if len(kwargs):
for key in kwargs.keys():
raise RecipeError("Unknown parameter {}".format(key))
diff --git a/lnst/Controller/Requirements.py b/lnst/Controller/Requirements.py
index 1d2ea6f..1337df0 100644
--- a/lnst/Controller/Requirements.py
+++ b/lnst/Controller/Requirements.py
@@ -20,12 +20,18 @@ __author__ = """
olichtne(a)redhat.com (Ondrej Lichtner)
"""
+import copy
from lnst.Common.LnstError import LnstError
-from lnst.Common.Parameters import Parameters
+from lnst.Common.Parameters import Parameters, Param
class RequirementError(LnstError):
pass
+class RecipeParam(Param):
+ def __init__(self, name, mandatory=False, **kwargs):
+ self.name = name
+ super(RecipeParam, self).__init__(mandatory, **kwargs)
+
class HostReq(object):
"""Specifies a Slave machine requirement
@@ -48,6 +54,27 @@ class HostReq(object):
raise RequirementError("'params' is a reserved
keyword.")
setattr(self.params, name, val)
+ def reinit_with_params(self, recipe_params):
+ for name, val in self.params:
+ if isinstance(val, RecipeParam):
+ if val.name in recipe_params:
+ new_val = getattr(recipe_params, val.name)
+ setattr(self.params, name, new_val)
+ else:
+ try:
+ new_val = copy.deepcopy(val.default)
+ setattr(self.params, name, new_val)
+ except AttributeError:
+ if val.mandatory:
+ raise RequirementError(
+ "Recipe parameter {} is mandatory for Recipe
Requirements parameter {}"
+ .format(val.name, name))
+ else:
+ delattr(self.params, name)
+
+ for name, dev_req in self:
+ dev_req.reinit_with_params(recipe_params)
+
This looks the same as the code below. Could we have a GenericReq class
that would have the reinit_with_params() and DeviceReq/HostReq would
inherit it?
> def __iter__(self):
> for x in dir(self):
> val = getattr(self, x)
>@@ -86,6 +113,24 @@ class DeviceReq(object):
> raise RequirementError("'params' is a reserved
keyword.")
> setattr(self.params, name, val)
>
>+ def reinit_with_params(self, recipe_params):
>+ for name, val in self.params:
>+ if isinstance(val, RecipeParam):
>+ if val.name in recipe_params:
>+ new_val = getattr(recipe_params, val.name)
>+ setattr(self.params, name, new_val)
>+ else:
>+ try:
>+ new_val = copy.deepcopy(val.default)
>+ setattr(self.params, name, new_val)
>+ except AttributeError:
>+ if val.mandatory:
>+ raise RequirementError(
>+ "Recipe parameter {} is mandatory for Recipe
Requirements parameter {}"
>+ .format(val.name, name))
>+ else:
>+ delattr(self.params, name)
>+
> def _to_dict(self):
> res = {'network': self.label,
> 'params': self.params._to_dict()}
>diff --git a/lnst/Controller/__init__.py b/lnst/Controller/__init__.py
>index 2dbb60f..106bb91 100644
>--- a/lnst/Controller/__init__.py
>+++ b/lnst/Controller/__init__.py
>@@ -1,4 +1,4 @@
> from lnst.Controller.Controller import Controller
> from lnst.Controller.Recipe import BaseRecipe
>-from lnst.Controller.Requirements import HostReq, DeviceReq
>+from lnst.Controller.Requirements import HostReq, DeviceReq, RecipeParam
> from lnst.Controller.NetNamespace import NetNamespace
>--
>2.17.0
>_______________________________________________
>LNST-developers mailing list -- lnst-developers(a)lists.fedorahosted.org
>To unsubscribe send an email to lnst-developers-leave(a)lists.fedorahosted.org
>Fedora Code of Conduct:
https://getfedora.org/code-of-conduct.html
>List Guidelines:
https://fedoraproject.org/wiki/Mailing_list_guidelines
>List Archives:
https://lists.fedoraproject.org/archives/list/lnst-developers@lists.fedor...