2016-05-26 16:16:13 +00:00
|
|
|
# Copyright 2016 Mycroft AI, Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Mycroft Core.
|
|
|
|
#
|
|
|
|
# Mycroft Core is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Mycroft Core is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Mycroft Core. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2016-05-20 14:16:01 +00:00
|
|
|
import json
|
|
|
|
|
2016-05-20 22:15:53 +00:00
|
|
|
__author__ = 'seanfitz'
|
|
|
|
|
2016-05-20 14:16:01 +00:00
|
|
|
|
|
|
|
class Message(object):
|
2016-09-04 00:59:39 +00:00
|
|
|
def __init__(self, type, metadata={}, context=None):
|
|
|
|
self.type = type
|
2016-05-20 14:16:01 +00:00
|
|
|
self.metadata = metadata
|
|
|
|
self.context = context
|
|
|
|
|
|
|
|
def serialize(self):
|
|
|
|
return json.dumps({
|
2016-09-04 00:59:39 +00:00
|
|
|
'type': self.type,
|
2016-05-20 14:16:01 +00:00
|
|
|
'metadata': self.metadata,
|
|
|
|
'context': self.context
|
|
|
|
})
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def deserialize(json_str):
|
|
|
|
json_message = json.loads(json_str)
|
2016-09-04 00:59:39 +00:00
|
|
|
return Message(json_message.get('type'),
|
2016-05-20 14:16:01 +00:00
|
|
|
metadata=json_message.get('metadata'),
|
|
|
|
context=json_message.get('context'))
|
|
|
|
|
2016-09-04 00:59:39 +00:00
|
|
|
def reply(self, type, metadata, context={}):
|
2016-05-20 14:16:01 +00:00
|
|
|
if not context:
|
|
|
|
context = {}
|
|
|
|
new_context = self.context if self.context else {}
|
|
|
|
for key in context:
|
|
|
|
new_context[key] = context[key]
|
|
|
|
if 'target' in metadata:
|
|
|
|
new_context['target'] = metadata['target']
|
|
|
|
elif 'client_name' in context:
|
|
|
|
context['target'] = context['client_name']
|
2016-09-04 00:59:39 +00:00
|
|
|
return Message(type, metadata, context=new_context)
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-09-04 00:59:39 +00:00
|
|
|
def publish(self, type, metadata, context={}):
|
2016-05-20 14:16:01 +00:00
|
|
|
if not context:
|
|
|
|
context = {}
|
|
|
|
new_context = self.context.copy() if self.context else {}
|
|
|
|
for key in context:
|
|
|
|
new_context[key] = context[key]
|
|
|
|
|
|
|
|
if 'target' in new_context:
|
|
|
|
del new_context['target']
|
|
|
|
|
2016-09-04 00:59:39 +00:00
|
|
|
return Message(type, metadata, context=new_context)
|