Rename Message type arg to msg_type
Name change to remove collision with the builin type.pull/2285/head
parent
e2d5b92bfe
commit
3b2c7267e7
|
@ -102,7 +102,7 @@ class MessageBusClient:
|
|||
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):
|
||||
|
@ -118,7 +118,7 @@ class MessageBusClient:
|
|||
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.
|
||||
|
@ -126,7 +126,7 @@ class MessageBusClient:
|
|||
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
|
||||
|
@ -138,7 +138,7 @@ class MessageBusClient:
|
|||
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
|
||||
|
@ -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))
|
||||
|
|
|
@ -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 {}
|
||||
|
||||
|
@ -51,11 +49,9 @@ class Message:
|
|||
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):
|
||||
|
@ -78,7 +74,7 @@ class Message:
|
|||
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
|
||||
|
@ -91,7 +87,7 @@ class Message:
|
|||
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
|
||||
|
||||
|
@ -108,7 +104,7 @@ class Message:
|
|||
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
|
||||
|
@ -122,18 +118,18 @@ class Message:
|
|||
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
|
||||
|
||||
|
@ -148,7 +144,7 @@ class Message:
|
|||
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):
|
||||
"""
|
||||
|
|
|
@ -42,7 +42,8 @@ class MessageBusEventHandler(WebSocketHandler):
|
|||
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)
|
||||
|
|
|
@ -170,7 +170,7 @@ class InterceptEmitter(object):
|
|||
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)
|
||||
|
@ -467,13 +467,13 @@ class SkillTest(object):
|
|||
"""
|
||||
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
|
||||
|
|
|
@ -29,30 +29,30 @@ class TestThreadedEventEmitter:
|
|||
self.count += 1
|
||||
|
||||
def test_on(self):
|
||||
self.emitter.on(self.msg.type, self.example_event)
|
||||
self.emitter.emit(self.msg.type, self.msg)
|
||||
self.emitter.emit(self.msg.type, self.msg)
|
||||
self.emitter.on(self.msg.msg_type, self.example_event)
|
||||
self.emitter.emit(self.msg.msg_type, self.msg)
|
||||
self.emitter.emit(self.msg.msg_type, self.msg)
|
||||
sleep(0.1)
|
||||
assert self.count == 2
|
||||
|
||||
def test_once(self):
|
||||
self.emitter.once(self.msg.type, self.example_event)
|
||||
self.emitter.emit(self.msg.type, self.msg)
|
||||
self.emitter.emit(self.msg.type, self.msg)
|
||||
self.emitter.once(self.msg.msg_type, self.example_event)
|
||||
self.emitter.emit(self.msg.msg_type, self.msg)
|
||||
self.emitter.emit(self.msg.msg_type, self.msg)
|
||||
sleep(0.1)
|
||||
assert self.count == 1
|
||||
|
||||
def test_remove_listener_on(self):
|
||||
self.emitter.on(self.msg.type, self.example_event)
|
||||
self.emitter.remove_listener(self.msg.type, self.example_event)
|
||||
self.emitter.emit(self.msg.type)
|
||||
self.emitter.on(self.msg.msg_type, self.example_event)
|
||||
self.emitter.remove_listener(self.msg.msg_type, self.example_event)
|
||||
self.emitter.emit(self.msg.msg_type)
|
||||
sleep(0.1)
|
||||
assert self.count == 0
|
||||
|
||||
def test_remove_all_listeners(self):
|
||||
self.emitter.on(self.msg.type, self.example_event)
|
||||
self.emitter.once(self.msg.type, self.example_event)
|
||||
self.emitter.remove_all_listeners(self.msg.type)
|
||||
self.emitter.emit(self.msg.type)
|
||||
self.emitter.on(self.msg.msg_type, self.example_event)
|
||||
self.emitter.once(self.msg.msg_type, self.example_event)
|
||||
self.emitter.remove_all_listeners(self.msg.msg_type)
|
||||
self.emitter.emit(self.msg.msg_type)
|
||||
sleep(0.1)
|
||||
assert self.count == 0
|
||||
|
|
|
@ -82,7 +82,7 @@ class MessageBusMock:
|
|||
self.event_handlers = []
|
||||
|
||||
def emit(self, message):
|
||||
self.message_types.append(message.type)
|
||||
self.message_types.append(message.msg_type)
|
||||
self.message_data.append(message.data)
|
||||
|
||||
def on(self, event, _):
|
||||
|
|
|
@ -95,6 +95,6 @@ class TestEventScheduler(unittest.TestCase):
|
|||
es.schedule_event('test', time.time(), None)
|
||||
|
||||
es.check_state()
|
||||
self.assertEquals(emitter.emit.call_args[0][0].type, 'test')
|
||||
self.assertEquals(emitter.emit.call_args[0][0].msg_type, 'test')
|
||||
self.assertEquals(emitter.emit.call_args[0][0].data, {})
|
||||
es.shutdown()
|
||||
|
|
|
@ -22,7 +22,7 @@ class MockEmitter(object):
|
|||
self.reset()
|
||||
|
||||
def emit(self, message):
|
||||
self.types.append(message.type)
|
||||
self.types.append(message.msg_type)
|
||||
self.results.append(message.data)
|
||||
|
||||
def get_types(self):
|
||||
|
|
|
@ -8,7 +8,7 @@ class MockEmitter:
|
|||
self.reset()
|
||||
|
||||
def emit(self, message):
|
||||
self.types.append(message.type)
|
||||
self.types.append(message.msg_type)
|
||||
self.results.append(message.data)
|
||||
|
||||
def get_types(self):
|
||||
|
|
|
@ -40,7 +40,7 @@ class MockEmitter(object):
|
|||
self.reset()
|
||||
|
||||
def emit(self, message):
|
||||
self.types.append(message.type)
|
||||
self.types.append(message.msg_type)
|
||||
self.results.append(message.data)
|
||||
|
||||
def get_types(self):
|
||||
|
@ -108,8 +108,8 @@ class MycroftSkillTest(unittest.TestCase):
|
|||
self.assertEqual(sorted(load_regex(path, 'A')), sorted(result_list))
|
||||
|
||||
def check_emitter(self, result_list):
|
||||
for type in self.emitter.get_types():
|
||||
self.assertEqual(type, 'register_vocab')
|
||||
for msg_type in self.emitter.get_types():
|
||||
self.assertEqual(msg_type, 'register_vocab')
|
||||
self.assertEqual(sorted(self.emitter.get_results(),
|
||||
key=lambda d: sorted(d.items())),
|
||||
sorted(result_list, key=lambda d: sorted(d.items())))
|
||||
|
@ -205,15 +205,15 @@ class MycroftSkillTest(unittest.TestCase):
|
|||
self.emitter.reset()
|
||||
|
||||
def check_register_intent(self, result_list):
|
||||
for type in self.emitter.get_types():
|
||||
self.assertEqual(type, 'register_intent')
|
||||
for msg_type in self.emitter.get_types():
|
||||
self.assertEqual(msg_type, 'register_intent')
|
||||
self.assertEqual(sorted(self.emitter.get_results()),
|
||||
sorted(result_list))
|
||||
self.emitter.reset()
|
||||
|
||||
def check_register_vocabulary(self, result_list):
|
||||
for type in self.emitter.get_types():
|
||||
self.assertEqual(type, 'register_vocab')
|
||||
for msg_type in self.emitter.get_types():
|
||||
self.assertEqual(msg_type, 'register_vocab')
|
||||
self.assertEqual(sorted(self.emitter.get_results()),
|
||||
sorted(result_list))
|
||||
self.emitter.reset()
|
||||
|
@ -381,8 +381,8 @@ class MycroftSkillTest(unittest.TestCase):
|
|||
|
||||
def test_set_context(self):
|
||||
def check_set_context(result_list):
|
||||
for type in self.emitter.get_types():
|
||||
self.assertEqual(type, 'add_context')
|
||||
for msg_type in self.emitter.get_types():
|
||||
self.assertEqual(msg_type, 'add_context')
|
||||
self.assertEqual(sorted(self.emitter.get_results()),
|
||||
sorted(result_list))
|
||||
self.emitter.reset()
|
||||
|
|
Loading…
Reference in New Issue