Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Mar 18, 2024
1 parent e89e679 commit fe1b291
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions teslemetry_stream/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,27 +192,33 @@ async def listen(self):
async for event in self:
if event:
for listener, filters in self._listeners.values():
if filters and recursive_match(filters, event):
if recursive_match(filters, event):
listener(event)
LOGGER.debug("Listen has finished")


def recursive_match(dict1, dict2):
"""Recursively match dict1 with dict2."""
for key, value1 in dict1.items():
if key not in dict2:
return False
value2 = dict2[key]
if isinstance(value1, dict):
if not recursive_match(value1, value2):
return False
elif isinstance(value1, list):
if not all(
any(recursive_match(item1, item2) for item2 in value2)
for item1 in value1
):
return False
elif value1 is not None:
if value1 != value2:
if dict1 is not None:
for key, value1 in dict1.items():
if key not in dict2:
# A required key isn't present
return False
value2 = dict2[key]
if isinstance(value1, dict):
# Check the next level of the dict
if not recursive_match(value1, value2):
return False
elif isinstance(value1, list):
# Check each dict in the list
if not all(
any(recursive_match(item1, item2) for item2 in value2)
for item1 in value1
):
return False
elif value1 is not None:
# Check the value matches
if value1 != value2:
return False
# No differences found
return True

0 comments on commit fe1b291

Please sign in to comment.