Mon, Feb 22, 2021 at 10:02:05PM CET, pgagne(a)redhat.com wrote:
From: Perry Gagne <pgagne(a)redhat.com>
Added parameter for representing an option that can only
be one of a select group of values.
Signed-off-by: Perry Gagne <pgagne(a)redhat.com>
---
lnst/Common/Parameters.py | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/lnst/Common/Parameters.py b/lnst/Common/Parameters.py
index f73fd3a..8a7c2eb 100644
--- a/lnst/Common/Parameters.py
+++ b/lnst/Common/Parameters.py
@@ -153,6 +153,36 @@ class ListParam(Param):
.format(item, str(e)))
return value
+
+class ChoiceParam(Param):
+ """Choice Param
+ This parameter is used for sitiuation where a param can have one of
+ a specified set of valid values. For example:
+
+ >>> flow_type = ChoiceParam(type=StrParam, choices=set('tcp_rr',
'udp_rr', 'tcp_crr'))
+
+ The type check will fail if the specified value does not pass both the specified
+ subtype `type_check` or is not one of the specified choices.
+ """
+ def __init__(self, type=None, choices=set(), **kwargs):
+ self._type = type() if type is not None else None
+ self._choices = choices
+ super().__init__(**kwargs)
+
+ def type_check(self, value):
+ if self._type is not None:
+ try:
+ v = self._type.type_check(value)
+ except ParamError as e:
+ raise e
What is the message a user gets when incorrect type of the parameter is
provided?
+ else:
+ value = v
+
+ if value not in self._choices:
+ raise ParamError(f"Value '{value}' not one of
{self._choices}")
+ return value
+
+
class Parameters(object):
def __init__(self):
self._attrs = {}
--
2.26.2
_______________________________________________
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://docs.fedoraproject.org/en-US/project/code-of-conduct/
List Guidelines:
https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives:
https://lists.fedorahosted.org/archives/list/lnst-developers@lists.fedora...
Do not reply to spam on the list, report it:
https://pagure.io/fedora-infrastructure