Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base_api.py: Implement operators and document them in cli help #2109

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 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,42 @@ 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', '__ne'
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',
'!=': '__ne',
}
if operator in switch:
attribute += switch[operator]
elif operator != '=':
raise ValueError(f"Invalid operator {operator}")
if attribute in ret:
raise ValueError(f"Attribute {attribute} already exists")
ret[attribute] = value
else:
raise ValueError(f"Invalid attribute {attribute}")

return ret
Loading