Skip to content

Commit

Permalink
Node search: Slight optimization and rationalize in test
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienCochuyt committed Oct 15, 2024
1 parent 26fcd32 commit d0a5b41
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
37 changes: 11 additions & 26 deletions addon/globalPlugins/webAccess/nodeHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,24 +590,6 @@ def searchString(self, text, exclude=None, limit=None):
return result
return []

def search_eq(self, itemList, value):
if not isinstance(itemList, list):
itemList = [itemList]
for item in itemList:
if item == value:
return True
return False

def search_in(self, itemList, value):
if value is None or value == "":
return False
if not isinstance(itemList, list):
itemList = [itemList]
for item in itemList:
if item.replace("*", "") in value:
return True
return False

def searchNode(
self,
exclude=None,
Expand All @@ -632,9 +614,8 @@ def searchNode(
Additional keyword arguments names are of the form:
`test_property[#index]`
All of the criteria must be matched (logical `and`).
Values can be lists, in which case any value in the list can match
(logical `or`).
All of the keywords must be matched (logical `and`).
Values are collections, any element can match (logical `or`).
Supported tests are: `eq`, `notEq`, `in` and `notIn`.
Properties `text` and `prevText` are mutually exclusive, are only valid
Expand All @@ -650,7 +631,7 @@ def searchNode(
_count += 1
found = True
# Copy kwargs dict to get ready for Python 3:
for key, allowedValues in list(kwargs.copy().items()):
for key, searchedValues in list(kwargs.copy().items()):
if "_" not in key:
log.warning("Unexpected argument: {arg}".format(arg=key))
continue
Expand All @@ -676,18 +657,22 @@ def searchNode(
candidateValues = (candidateValue,)
for candidateValue in candidateValues:
if test == "eq":
if self.search_eq(allowedValues, candidateValue):
if candidateValue in searchedValues:
del kwargs[key]
break
elif test == "in":
if self.search_in(allowedValues, candidateValue):
if candidateValue and any(
True for searchedValue in searchedValues if searchedValue in candidateValue
):
del kwargs[key]
break
elif test == "notEq":
if self.search_eq(allowedValues, candidateValue):
if candidateValue in searchedValues:
return []
elif test == "notIn":
if self.search_in(allowedValues, candidateValue):
if candidateValue and any(
True for searchedValue in searchedValues if searchedValue in candidateValue
):
return []
else: # no break
if test in ("eq", "in"):
Expand Down
11 changes: 6 additions & 5 deletions addon/globalPlugins/webAccess/ruleHandler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1436,13 +1436,14 @@ def getSimpleSearchKwargs(criteria, raiseOnUnsupported=False):
if not expr:
continue
if expr[0] == "!":
if "*" in expr:
notIn.append(expr[1:].strip())
expr = expr[1:].strip()
if "*" in (expr[0], expr[-1]):
notIn.append(expr.strip("*").strip())
else:
notEq.append(expr[1:].strip())
notEq.append(expr)
else:
if "*" in expr:
in_.append(expr)
if "*" in (expr[0], expr[-1]):
in_.append(expr.strip("*").strip())
else:
eq.append(expr)
for test, values in (
Expand Down

0 comments on commit d0a5b41

Please sign in to comment.