Skip to content

Commit 117a45a

Browse files
committed
message type checks
1 parent 91b9825 commit 117a45a

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

ovos_bus_client/message.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,22 @@ def normalize(text, *args, **kwargs):
1010
return text
1111

1212

13-
class Message(_MycroftMessage):
13+
class _MessageMeta(type):
14+
""" To override isinstance checks we need to use a metaclass """
15+
def __instancecheck__(self, instance):
16+
return isinstance(instance, _MycroftMessage)
17+
18+
19+
class Message(_MycroftMessage, metaclass=_MessageMeta):
1420
"""Mycroft specific Message class."""
1521

22+
def __eq__(self, other):
23+
if not isinstance(other, _MycroftMessage):
24+
return False
25+
return other.msg_type == self.msg_type and \
26+
other.data == self.data and \
27+
other.context == self.context
28+
1629
def utterance_remainder(self):
1730
"""
1831
DEPRECATED - mycroft-core hack, used by some skills in the wild
@@ -33,3 +46,14 @@ def utterance_remainder(self):
3346
# Substitute only whole words matching the token
3447
utt = re.sub(r'\b' + token.get("key", "") + r"\b", "", utt)
3548
return normalize(utt)
49+
50+
51+
if __name__ == "__main__":
52+
m1 = _MycroftMessage("")
53+
m2 = Message("")
54+
print(m1 == m2)
55+
print(m2 == m1)
56+
print(isinstance(m1, _MycroftMessage))
57+
print(isinstance(m1, Message))
58+
print(isinstance(m2, _MycroftMessage))
59+
print(isinstance(m2, Message))

0 commit comments

Comments
 (0)