From 9def0edb736e81fa443ea9cf525c5ec8fca0d1db Mon Sep 17 00:00:00 2001 From: Denys Fedoryshchenko Date: Mon, 18 Sep 2023 13:30:50 +0300 Subject: [PATCH] base_api.py: Implement operators and document them in cli help Right now for operators we are using "magic" suffix such as __gt, __lt and so on, which is not documented and a bit non-intuitive. We can make cli tool more intuitive with this patch, where it will hide from user this magic, until we find better mechanism to handle operators as mentioned in https://github.com/kernelci/kernelci-api/issues/356 Signed-off-by: Denys Fedoryshchenko --- kernelci/cli/base_api.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/kernelci/cli/base_api.py b/kernelci/cli/base_api.py index 8a066b6dd2..5ab25b02f7 100644 --- a/kernelci/cli/base_api.py +++ b/kernelci/cli/base_api.py @@ -12,6 +12,7 @@ import abc import json +import re import kernelci.api from .base import Command, Args, catch_http_error @@ -63,12 +64,28 @@ class AttributesCommand(APICommand): { 'name': 'attributes', 'nargs': '*', - 'help': "Attributes in name=value format", + 'help': "Attributes in name=value format, where = is OPERATOR and can be one of: >, <, >=, <=, =", }, ] @classmethod def _split_attributes(cls, attributes): - return dict( - tuple(attr.split('=')) for attr in attributes - ) if attributes else {} + rval = {} + for attribute in attributes: + pattern = r'^(\w+)([!=<>]+)(\w+)$' + match = re.match(pattern, attribute) + if match: + attribute, operator, value = match.groups() + # if operator is >= then append to attribute '__gte' suffix + # and so on + switch = { + '>': '__gt', + '<': '__lt', + '>=': '__gte', + '<=': '__lte', + } + if operator in switch: + attribute += switch[operator] + rval[attribute] = value + + return rval