Move send function away from __main__ file
This resolves the Runtime Warning "'mycroft.messagebus.send' found in sys.modules after import of package 'mycroft.messagebus', but prior to execution of 'mycroft.messagebus.send'; this may result in unpredictable behaviour"pull/2371/head
parent
0811df3831
commit
39a38bc641
|
@ -13,5 +13,5 @@
|
|||
# limitations under the License.
|
||||
from .client.client import MessageBusClient
|
||||
from .message import Message
|
||||
from .send import send
|
||||
from .send_func import send
|
||||
from .service.event_handler import MessageBusEventHandler
|
||||
|
|
|
@ -15,13 +15,7 @@
|
|||
import sys
|
||||
import json
|
||||
|
||||
from websocket import create_connection
|
||||
|
||||
from mycroft.configuration import Configuration
|
||||
from mycroft.configuration.locations import (DEFAULT_CONFIG, SYSTEM_CONFIG,
|
||||
USER_CONFIG)
|
||||
from mycroft.messagebus.client import MessageBusClient
|
||||
from mycroft.messagebus.message import Message
|
||||
from .send_func import send
|
||||
|
||||
|
||||
def main():
|
||||
|
@ -57,36 +51,6 @@ def main():
|
|||
send(message_to_send, data_to_send)
|
||||
|
||||
|
||||
def send(message_to_send, data_to_send=None):
|
||||
"""Send a single message over the websocket.
|
||||
|
||||
Args:
|
||||
message_to_send (str): Message to send
|
||||
data_to_send (dict): data structure to go along with the
|
||||
message, defaults to empty dict.
|
||||
"""
|
||||
data_to_send = data_to_send or {}
|
||||
|
||||
# Calculate the standard Mycroft messagebus websocket address
|
||||
config = Configuration.get([DEFAULT_CONFIG,
|
||||
SYSTEM_CONFIG,
|
||||
USER_CONFIG],
|
||||
cache=False)
|
||||
config = config.get("websocket")
|
||||
url = MessageBusClient.build_url(
|
||||
config.get("host"),
|
||||
config.get("port"),
|
||||
config.get("route"),
|
||||
config.get("ssl")
|
||||
)
|
||||
|
||||
# Send the provided message/data
|
||||
ws = create_connection(url)
|
||||
packet = Message(message_to_send, data_to_send).serialize()
|
||||
ws.send(packet)
|
||||
ws.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
# Copyright 2019 Mycroft AI Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
from websocket import create_connection
|
||||
|
||||
from mycroft.configuration import Configuration
|
||||
from mycroft.configuration.locations import (DEFAULT_CONFIG, SYSTEM_CONFIG,
|
||||
USER_CONFIG)
|
||||
from mycroft.messagebus.client import MessageBusClient
|
||||
from mycroft.messagebus.message import Message
|
||||
|
||||
|
||||
def send(message_to_send, data_to_send=None):
|
||||
"""Send a single message over the websocket.
|
||||
|
||||
Args:
|
||||
message_to_send (str): Message to send
|
||||
data_to_send (dict): data structure to go along with the
|
||||
message, defaults to empty dict.
|
||||
"""
|
||||
data_to_send = data_to_send or {}
|
||||
|
||||
# Calculate the standard Mycroft messagebus websocket address
|
||||
config = Configuration.get([DEFAULT_CONFIG,
|
||||
SYSTEM_CONFIG,
|
||||
USER_CONFIG],
|
||||
cache=False)
|
||||
config = config.get("websocket")
|
||||
url = MessageBusClient.build_url(
|
||||
config.get("host"),
|
||||
config.get("port"),
|
||||
config.get("route"),
|
||||
config.get("ssl")
|
||||
)
|
||||
|
||||
# Send the provided message/data
|
||||
ws = create_connection(url)
|
||||
packet = Message(message_to_send, data_to_send).serialize()
|
||||
ws.send(packet)
|
||||
ws.close()
|
Loading…
Reference in New Issue