Skip to content

Commit

Permalink
TableHandler integration: Move createResult from Criteria to Rule
Browse files Browse the repository at this point in the history
Eases creation of subclassed Results by custom WebModules.
  • Loading branch information
JulienCochuyt committed Oct 17, 2024
1 parent 842ac02 commit e379811
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions addon/globalPlugins/webAccess/ruleHandler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,12 @@ def __init__(self, criteria, context, index):
})
self.bindGestures(criteria.gestures)

def __repr__(self):
try:
return f"<{type(self).__name__} of {self.rule.name!r} at {self.startOffset, self.endOffset}>"
except Exception:
return super().__repr__()

def _get_criteria(self):
return self._criteria()

Expand Down Expand Up @@ -1399,13 +1405,11 @@ def checkContextPageType(self):
if not (found or exclude):
return False
return True

def createResult(self, node, context, index):
return SingleNodeResult(self, node, context, index)


def iterResults(self):
t = logTimeStart()
mgr = self.rule.ruleManager
rule = self.rule
mgr = rule.ruleManager
text = self.text
if not self.checkContextPageTitle():
return
Expand All @@ -1429,14 +1433,18 @@ def iterResults(self):
name = name.strip()
if not name:
continue
rule = mgr.getRule(name, layer=self.layer)
if rule is None:
log.error((
"In rule \"{rule}\".contextParent: "
"Rule not found: \"{parent}\""
).format(rule=self.name, parent=name))
parentRule = mgr.getRule(name, layer=self.layer)
if parentRule is None:
log.error(
'In rule "{rule}", alternative "{alternative}" .contextParent: '
'Rule not found: "{parent}"'
).format(
rule=rule.name,
alternative=self.name or f"#{rule.criteria.index(self)}",
parent=name,
)
return
results = rule.getResults()
results = parentRule.getResults()
if not exclude and any(r.properties.multiple for r in results):
if multipleContext is None:
multipleContext = True
Expand Down Expand Up @@ -1466,7 +1474,7 @@ def iterResults(self):
rootNodes = newRootNodes
kwargs = getSimpleSearchKwargs(self)
excludedNodes.update({
result.node for result in self.rule.ruleManager.subModules._results
result.node for result in mgr.subModules._results
})
if excludedNodes:
kwargs["exclude"] = excludedNodes
Expand Down Expand Up @@ -1497,8 +1505,8 @@ def iterResults(self):
context = textInfos.offsets.Offsets(
startOffset=root.offset,
endOffset=root.offset + root.size
) if root is not self.ruleManager.nodeManager.mainNode else None
yield self.createResult(node, context, index)
) if root is not mgr.nodeManager.mainNode else None
yield rule.createResult(self, node, context, index)
if not self.properties.multiple and not multipleContext:
return

Expand Down Expand Up @@ -1567,6 +1575,9 @@ def load(self, data):
+ ", ".join(list(data.keys()))
)

def createResult(self, criteria, node, context, index):
return SingleNodeResult(criteria, node, context, index)

def resetResults(self):
self._results = None

Expand Down

0 comments on commit e379811

Please sign in to comment.