Skip to content

Commit

Permalink
Rename Message type arg to msg_type
Browse files Browse the repository at this point in the history
Name change to remove collision with the builin type.
  • Loading branch information
forslund committed Sep 2, 2019
1 parent e2d5b92 commit 3a54730
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
10 changes: 5 additions & 5 deletions mycroft/messagebus/client/client.py
Expand Up @@ -102,7 +102,7 @@ def on_error(self, error):
def on_message(self, message):
parsed_message = Message.deserialize(message)
self.emitter.emit('message', message)
self.emitter.emit(parsed_message.type, parsed_message)
self.emitter.emit(parsed_message.msg_type, parsed_message)

def emit(self, message):
if not self.connected_event.wait(10):
Expand All @@ -118,15 +118,15 @@ def emit(self, message):
self.client.send(json.dumps(message.__dict__))
except WebSocketConnectionClosedException:
LOG.warning('Could not send {} message because connection '
'has been closed'.format(message.type))
'has been closed'.format(message.msg_type))

def wait_for_response(self, message, reply_type=None, timeout=None):
"""Send a message and wait for a response.
Args:
message (Message): message to send
reply_type (str): the message type of the expected reply.
Defaults to "<message.type>.response".
Defaults to "<message.msg_type>.response".
timeout: seconds to wait before timeout, defaults to 3
Returns:
The received message or None if the response timed out
Expand All @@ -138,7 +138,7 @@ def handler(message):
response.append(message)

# Setup response handler
self.once(reply_type or message.type + '.response', handler)
self.once(reply_type or message.msg_type + '.response', handler)
# Send request
self.emit(message)
# Wait for response
Expand Down Expand Up @@ -214,7 +214,7 @@ def echo():
message_bus_client = MessageBusClient()

def repeat_utterance(message):
message.type = 'speak'
message.msg_type = 'speak'
message_bus_client.emit(message)

message_bus_client.on('message', create_echo_function(None))
Expand Down
32 changes: 14 additions & 18 deletions mycroft/messagebus/message.py
Expand Up @@ -25,20 +25,18 @@ class Message:
between processes of Mycroft.
Attributes:
type (str): type of data sent within the message.
msg_type (str): type of data sent within the message.
data (dict): data sent within the message
context: info about the message not part of data such as source,
destination or domain.
"""
# TODO: For 119.08, change the name of the "type" argument to msg_type
# The name "type" shadows a Python built-in name
def __init__(self, type, data=None, context=None):
def __init__(self, msg_type, data=None, context=None):
"""Used to construct a message object
Message objects will be used to send information back and fourth
bettween processes of mycroft service, voice, skill and cli
"""
self.type = type
self.msg_type = msg_type
self.data = data or {}
self.context = context or {}

Expand All @@ -51,11 +49,9 @@ def serialize(self):
Returns:
str: a json string representation of the message.
"""
return json.dumps(dict(
type=self.type,
data=self.data,
context=self.context
))
return json.dumps({'type': self.msg_type,
'data': self.data,
'context': self.context})

@staticmethod
def deserialize(value):
Expand All @@ -78,7 +74,7 @@ def deserialize(value):
obj.get('data') or {},
obj.get('context') or {})

def reply(self, type, data=None, context=None):
def reply(self, msg_type, data=None, context=None):
"""Construct a reply message for a given message
This will take the same parameters as a message object but use
Expand All @@ -91,7 +87,7 @@ def reply(self, type, data=None, context=None):
new context generated.
Args:
type (str): type of message
msg_type (str): type of message
data (dict): data for message
context: intended context for new message
Expand All @@ -108,7 +104,7 @@ def reply(self, type, data=None, context=None):
new_context['target'] = data['target']
elif 'client_name' in context:
context['target'] = context['client_name']
return Message(type, data, context=new_context)
return Message(msg_type, data, context=new_context)

def response(self, data=None, context=None):
"""Construct a response message for the message
Expand All @@ -122,18 +118,18 @@ def response(self, data=None, context=None):
Returns
(Message) message with the type modified to match default response
"""
response_message = self.reply(self.type, data or {}, context)
response_message.type += '.response'
response_message = self.reply(self.msg_type, data or {}, context)
response_message.msg_type += '.response'
return response_message

def publish(self, type, data, context=None):
def publish(self, msg_type, data, context=None):
"""
Copy the original context and add passed in context. Delete
any target in the new context. Return a new message object with
passed in data and new context. Type remains unchanged.
Args:
type (str): type of message
msg_type (str): type of message
data (dict): date to send with message
context: context added to existing context
Expand All @@ -148,7 +144,7 @@ def publish(self, type, data, context=None):
if 'target' in new_context:
del new_context['target']

return Message(type, data, context=new_context)
return Message(msg_type, data, context=new_context)

def utterance_remainder(self):
"""
Expand Down
3 changes: 2 additions & 1 deletion mycroft/messagebus/service/event_handler.py
Expand Up @@ -42,7 +42,8 @@ def on_message(self, message):
return

try:
self.emitter.emit(deserialized_message.type, deserialized_message)
self.emitter.emit(deserialized_message.msg_type,
deserialized_message)
except Exception as e:
LOG.exception(e)
traceback.print_exc(file=sys.stdout)
Expand Down
10 changes: 5 additions & 5 deletions test/integrationtests/skills/skill_tester.py
Expand Up @@ -170,7 +170,7 @@ def on(self, event, f):
self.emitter.on(event, f)

def emit(self, event, *args, **kwargs):
event_name = event.type
event_name = event.msg_type
if self.q:
self.q.put(event)
self.emitter.emit(event_name, event, *args, **kwargs)
Expand Down Expand Up @@ -467,13 +467,13 @@ def check_queue(self, q, evaluation_rule):
"""
try:
event = q.get(timeout=1)
if ':' in event.type:
event.data['__type__'] = event.type.split(':')[1]
if ':' in event.msg_type:
event.data['__type__'] = event.msg_type.split(':')[1]
else:
event.data['__type__'] = event.type
event.data['__type__'] = event.msg_type

evaluation_rule.evaluate(event.data)
if event.type == 'mycroft.skill.handler.complete':
if event.msg_type == 'mycroft.skill.handler.complete':
return True
except Empty:
pass
Expand Down

0 comments on commit 3a54730

Please sign in to comment.