Skip to content

Commit 91b9825

Browse files
committed
move more code from core
1 parent 240929a commit 91b9825

File tree

7 files changed

+148
-10
lines changed

7 files changed

+148
-10
lines changed

ovos_bus_client/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from .client.client import MessageBusClient
15-
from .message import Message
16-
from .send_func import send
17-
from .conf import client_from_config
14+
from ovos_bus_client.client.client import MessageBusClient
15+
from ovos_bus_client.message import Message
16+
from ovos_bus_client.send_func import send
17+
from ovos_bus_client.session import Session, SessionManager, UtteranceState
18+
from ovos_bus_client.conf import client_from_config
1819

1920
"""
2021
Mycroft Messagebus Client.

ovos_bus_client/client/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from ovos_bus_client.util import create_echo_function
33
from ovos_bus_client.client.collector import MessageCollector
44
from ovos_bus_client.client.waiter import MessageWaiter
5-
5+
from ovos_bus_client.conf import load_message_bus_config
66
from mycroft_bus_client.client.client import MessageBusClient as _MycroftBusClient, MessageBusClientConf
77
from ovos_bus_client.session import Session, SessionManager
88

ovos_bus_client/conf.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,71 @@
1-
from mycroft_bus_client.conf import *
1+
"""Message bus configuration loader.
2+
3+
The message bus event handler and client use basically the same configuration.
4+
This code is re-used in both to load config values.
5+
"""
6+
import json
7+
8+
from mycroft.util.log import LOG
9+
from ovos_config.config import Configuration
10+
from mycroft_bus_client.client.client import MessageBusClientConf
11+
12+
# mycroft-core had this duplicated with both names...
13+
MessageBusConfig = MessageBusClientConf
14+
15+
16+
def load_message_bus_config(**overrides):
17+
"""Load the bits of device configuration needed to run the message bus."""
18+
LOG.info('Loading message bus configs')
19+
config = Configuration()
20+
21+
try:
22+
websocket_configs = config['websocket']
23+
except KeyError as ke:
24+
LOG.error('No websocket configs found ({})'.format(repr(ke)))
25+
raise
26+
else:
27+
mb_config = MessageBusConfig(
28+
host=overrides.get('host') or websocket_configs.get('host'),
29+
port=overrides.get('port') or websocket_configs.get('port'),
30+
route=overrides.get('route') or websocket_configs.get('route'),
31+
ssl=overrides.get('ssl') or config.get('ssl')
32+
)
33+
if not all([mb_config.host, mb_config.port, mb_config.route]):
34+
error_msg = 'Missing one or more websocket configs'
35+
LOG.error(error_msg)
36+
raise ValueError(error_msg)
37+
38+
return mb_config
39+
40+
41+
def client_from_config(subconf='core', file_path='/etc/mycroft/bus.conf'):
42+
"""Load messagebus configuration from file.
43+
44+
The config is a basic json file with a number of "sub configurations"
45+
46+
Ex:
47+
{
48+
"core": {
49+
"route": "/core",
50+
"port": "8181"
51+
}
52+
"gui": {
53+
"route": "/gui",
54+
"port": "8811"
55+
}
56+
}
57+
58+
Arguments:
59+
subconf: configuration to choose from the file, defaults to "core"
60+
if omitted.
61+
file_path: path to the config file, defaults to /etc/mycroft/bus.conf
62+
if omitted.
63+
Returns:
64+
MessageBusClient instance based on the selected config.
65+
"""
66+
from ovos_bus_client.client import MessageBusClient
67+
68+
with open(file_path) as f:
69+
conf = json.load(f)
70+
71+
return MessageBusClient(**conf[subconf])

ovos_bus_client/message.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
1-
from mycroft_bus_client.message import *
1+
import re
2+
from mycroft_bus_client.message import Message as _MycroftMessage, dig_for_message, CollectionMessage
3+
from ovos_utils.log import LOG
4+
5+
try:
6+
from lingua_franca.parse import normalize
7+
except ImportError:
8+
# optional LF import
9+
def normalize(text, *args, **kwargs):
10+
return text
11+
12+
13+
class Message(_MycroftMessage):
14+
"""Mycroft specific Message class."""
15+
16+
def utterance_remainder(self):
17+
"""
18+
DEPRECATED - mycroft-core hack, used by some skills in the wild
19+
20+
For intents get the portion not consumed by Adapt.
21+
22+
For example: if they say 'Turn on the family room light' and there are
23+
entity matches for "turn on" and "light", then it will leave behind
24+
" the family room " which is then normalized to "family room".
25+
26+
Returns:
27+
str: Leftover words or None if not an utterance.
28+
"""
29+
LOG.warning("Message.utterance_remainder has been deprecated!")
30+
utt = normalize(self.data.get("utterance", ""))
31+
if utt and "__tags__" in self.data:
32+
for token in self.data["__tags__"]:
33+
# Substitute only whole words matching the token
34+
utt = re.sub(r'\b' + token.get("key", "") + r"\b", "", utt)
35+
return normalize(utt)

ovos_bus_client/send_func.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
1-
from mycroft_bus_client.send_func import *
1+
from websocket import create_connection
2+
3+
from ovos_config.config import Configuration
4+
from ovos_bus_client.client import MessageBusClient
5+
from ovos_bus_client.message import Message
6+
7+
8+
def send(message_to_send, data_to_send=None):
9+
"""Send a single message over the websocket.
10+
11+
Args:
12+
message_to_send (str): Message to send
13+
data_to_send (dict): data structure to go along with the
14+
message, defaults to empty dict.
15+
"""
16+
data_to_send = data_to_send or {}
17+
18+
# Calculate the standard Mycroft messagebus websocket address
19+
config = Configuration()
20+
config = config.get("websocket")
21+
url = MessageBusClient.build_url(
22+
config.get("host"),
23+
config.get("port"),
24+
config.get("route"),
25+
config.get("ssl")
26+
)
27+
28+
# Send the provided message/data
29+
ws = create_connection(url)
30+
packet = Message(message_to_send, data_to_send).serialize()
31+
ws.send(packet)
32+
ws.close()

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
mycroft-messagebus-client
1+
mycroft-messagebus-client
2+
ovos-config
3+
ovos-utils

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def required(requirements_file):
1616

1717
setup(
1818
name='ovos-bus-client',
19-
version='0.0.1',
19+
version='0.0.2',
2020
packages=['ovos_bus_client',
2121
'ovos_bus_client.client',
2222
'ovos_bus_client.util'],

0 commit comments

Comments
 (0)