Skip to content

Commit

Permalink
SDP: use regex for reply check (resend protocol)
Browse files Browse the repository at this point in the history
  • Loading branch information
onkelandy committed Dec 23, 2024
1 parent 880e1a8 commit 1db4a56
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
7 changes: 6 additions & 1 deletion lib/model/sdp/protocol.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import threading
import queue
import json
import re


#############################################################################################################################################################################################################################################
Expand Down Expand Up @@ -530,7 +531,11 @@ def check_reply(self, command, value):
for c in compare:
self.logger.debug(f'Comparing expected reply {c} ({type(c)}) with value {value} ({type(value)}).')
# check if expected value equals received value or both are None (only happens with lists in reply_pattern)
if c is None or type(c)(value) == c:
if isinstance(c, re.Pattern):
cond = re.search(c, str(value))
else:
cond = type(c)(value) == c
if c is None or cond:
# remove command from _sending dict
self._sending.pop(command)
self._sending_retries.pop(command)
Expand Down
10 changes: 5 additions & 5 deletions lib/model/smartdeviceplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ def send_command(self, command, value=None, return_result=False, **kwargs):
:return: True if send was successful, False otherwise
:rtype: bool
"""
def is_regex(pattern):
def captures_value(pattern):
try:
re.compile(pattern)
# Check for non-empty unescaped parentheses indicating capture groups
Expand Down Expand Up @@ -787,14 +787,14 @@ def is_regex(pattern):
if reply_pattern is None or value is None:
resend_info = {'command': resend_command, 'returnvalue': None, 'read_cmd': read_cmd}
# if reply_pattern has no lookup or capture group, put it in resend_info as expected reply
elif not isinstance(reply_pattern, list) and not is_regex(reply_pattern):
resend_info = {'command': resend_command, 'returnvalue': reply_pattern, 'read_cmd': read_cmd}
elif not isinstance(reply_pattern, list) and not captures_value(reply_pattern):
resend_info = {'command': resend_command, 'returnvalue': re.compile(reply_pattern), 'read_cmd': read_cmd}
# if reply_pattern is list, check if one of the entries has capture group
elif isinstance(reply_pattern, list):
return_list = []
for r in reply_pattern:
if not is_regex(r):
return_list.append(r)
if not captures_value(r):
return_list.append(re.compile(r))
elif value not in return_list:
return_list.append(value)
reply_pattern = None if None in return_list else return_list
Expand Down

0 comments on commit 1db4a56

Please sign in to comment.