forked from MycroftAI/mycroft-core
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
messagebus module duplicate code removal
- Loading branch information
Showing
7 changed files
with
7 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1 @@ | ||
# 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. | ||
# | ||
"""Message bus configuration loader. | ||
The message bus event handler and client use basically the same configuration. | ||
This code is re-used in both to load config values. | ||
""" | ||
from collections import namedtuple | ||
|
||
from ovos_config.config import Configuration | ||
from mycroft.util.log import LOG | ||
|
||
MessageBusConfig = namedtuple( | ||
'MessageBusConfig', | ||
['host', 'port', 'route', 'ssl'] | ||
) | ||
|
||
|
||
def load_message_bus_config(**overrides): | ||
"""Load the bits of device configuration needed to run the message bus.""" | ||
LOG.info('Loading message bus configs') | ||
config = Configuration() | ||
|
||
try: | ||
websocket_configs = config['websocket'] | ||
except KeyError as ke: | ||
LOG.error('No websocket configs found ({})'.format(repr(ke))) | ||
raise | ||
else: | ||
mb_config = MessageBusConfig( | ||
host=overrides.get('host') or websocket_configs.get('host'), | ||
port=overrides.get('port') or websocket_configs.get('port'), | ||
route=overrides.get('route') or websocket_configs.get('route'), | ||
ssl=overrides.get('ssl') or config.get('ssl') | ||
) | ||
if not all([mb_config.host, mb_config.port, mb_config.route]): | ||
error_msg = 'Missing one or more websocket configs' | ||
LOG.error(error_msg) | ||
raise ValueError(error_msg) | ||
|
||
return mb_config | ||
from ovos_bus_client.conf import load_message_bus_config, client_from_config, MessageBusConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1 @@ | ||
# Copyright 2017 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. | ||
# | ||
import re | ||
|
||
from mycroft.util.parse import normalize | ||
from ovos_bus_client.message import dig_for_message | ||
import ovos_bus_client | ||
|
||
|
||
class Message(ovos_bus_client.Message): | ||
"""Mycroft specific Message class.""" | ||
|
||
def utterance_remainder(self): | ||
""" | ||
For intents get the portion not consumed by Adapt. | ||
For example: if they say 'Turn on the family room light' and there are | ||
entity matches for "turn on" and "light", then it will leave behind | ||
" the family room " which is then normalized to "family room". | ||
Returns: | ||
str: Leftover words or None if not an utterance. | ||
""" | ||
utt = normalize(self.data.get("utterance", "")) | ||
if utt and "__tags__" in self.data: | ||
for token in self.data["__tags__"]: | ||
# Substitute only whole words matching the token | ||
utt = re.sub(r'\b' + token.get("key", "") + r"\b", "", utt) | ||
return normalize(utt) | ||
from ovos_bus_client.message import dig_for_message, Message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1 @@ | ||
# 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 ovos_config.config import Configuration | ||
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() | ||
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() | ||
from ovos_bus_client.send_func import send |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters