From ff9b76477da30d76186e6000c3b8081a2da71fa2 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 28 Apr 2023 11:04:33 +0200 Subject: [PATCH] Fixed arguments naming. --- platypush/message/event/__init__.py | 48 ++++++++++++------- platypush/message/event/assistant/__init__.py | 8 ++-- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/platypush/message/event/__init__.py b/platypush/message/event/__init__.py index b1dfbc5eda..221487a70d 100644 --- a/platypush/message/event/__init__.py +++ b/platypush/message/event/__init__.py @@ -157,47 +157,59 @@ class Event(Message): return True + # pylint: disable=too-many-branches,too-many-return-statements def _matches_condition( self, condition: dict, - args: dict, + event_args: dict, result: "EventMatchResult", match_scores: list, ) -> bool: - for attr, value in condition.items(): - if attr not in args: + for attr, condition_value in condition.items(): + if attr not in event_args: return False - if isinstance(args[attr], str): - if self._is_relational_filter(value): - if not self._relational_filter_matches(value, args[attr]): + event_value = event_args[attr] + if isinstance(event_value, str): + if self._is_relational_filter(condition_value): + if not self._relational_filter_matches( + condition_value, event_value + ): return False else: self._matches_argument( - argname=attr, condition_value=value, args=args, result=result + argname=attr, + condition_value=condition_value, + event_args=event_args, + result=result, ) if result.is_match: match_scores.append(result.score) else: return False - elif isinstance(value, dict): - if self._is_relational_filter(value): - if not self._relational_filter_matches(value, args[attr]): + elif isinstance(condition_value, dict): + if self._is_relational_filter(condition_value): + if not self._relational_filter_matches( + condition_value, event_value + ): return False else: - if not isinstance(args[attr], dict): + if not isinstance(event_value, dict): return False if not self._matches_condition( - condition=value, - args=args[attr], + condition=condition_value, + event_args=event_value, result=result, match_scores=match_scores, ): return False - elif args[attr] != value: - return False + else: + if event_value != condition_value: + return False + + match_scores.append(2.0) return True @@ -215,7 +227,7 @@ class Event(Message): if not self._matches_condition( condition=condition.args, - args=self.args, + event_args=self.args, result=result, match_scores=match_scores, ): @@ -228,7 +240,7 @@ class Event(Message): return result def _matches_argument( - self, argname, condition_value, args, result: "EventMatchResult" + self, argname, condition_value, event_args, result: "EventMatchResult" ): """ Returns an EventMatchResult if the event argument [argname] matches @@ -236,7 +248,7 @@ class Event(Message): """ # Simple equality match by default. It can be overridden by the derived classes. - result.is_match = args.get(argname) == condition_value + result.is_match = event_args.get(argname) == condition_value if result.is_match: result.score += 2 else: diff --git a/platypush/message/event/assistant/__init__.py b/platypush/message/event/assistant/__init__.py index 98266c7f46..2047c1a62d 100644 --- a/platypush/message/event/assistant/__init__.py +++ b/platypush/message/event/assistant/__init__.py @@ -108,14 +108,14 @@ class SpeechRecognizedEvent(AssistantEvent): return result @override - def _matches_argument(self, argname, condition_value, args, result): + def _matches_argument(self, argname, condition_value, event_args, result): """ Overrides the default `_matches_argument` method to allow partial phrase matches and text extraction. Example:: - args = { + event_args = { 'phrase': 'Hey dude turn on the living room lights' } @@ -127,13 +127,13 @@ class SpeechRecognizedEvent(AssistantEvent): """ - if args.get(argname) == condition_value: + if event_args.get(argname) == condition_value: # In case of an exact match, return immediately result.is_match = True result.score = sys.maxsize return result - event_tokens = re.split(r'\s+', args.get(argname, '').strip().lower()) + event_tokens = re.split(r'\s+', event_args.get(argname, '').strip().lower()) condition_tokens = re.split(r'\s+', condition_value.strip().lower()) while event_tokens and condition_tokens: