Skip to content

Commit

Permalink
base_api.py: Implement operators and document them in cli help
Browse files Browse the repository at this point in the history
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 kernelci/kernelci-api#356

Signed-off-by: Denys Fedoryshchenko <[email protected]>
  • Loading branch information
nuclearcat committed Sep 18, 2023
1 parent 77c16d9 commit 895ad07
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions kernelci/cli/base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import abc
import json
import re

import kernelci.api
from .base import Command, Args, catch_http_error
Expand Down Expand Up @@ -63,12 +64,39 @@ 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 {}
""" Split attributes into a dictionary.
At moment we use small hack, if operator matches one of: >, <, >=, <=
then we append to attribute '__gt', '__lt', '__gte', '__lte' suffix
accordingly to API documentation.
"""
ret = {}
if not attributes:
return ret
for attribute in attributes:
pattern = r'^([\S]+)([!=<>]+)([\S]+)$'
match = re.match(pattern, attribute)
if match:
attribute, operator, value = match.groups()
switch = {
'>': '__gt',
'<': '__lt',
'>=': '__gte',
'<=': '__lte',
}
if operator in switch:
attribute += switch[operator]
elif operator != '=':
raise ValueError(f"Invalid operator {operator}")
ret[attribute] = value
else:
raise ValueError(f"Invalid attribute {attribute}")

return ret

0 comments on commit 895ad07

Please sign in to comment.