Skip to content

Commit

Permalink
fix: remove dead code (#141)
Browse files Browse the repository at this point in the history
* fix: remove dead code

* Update client.py

* Update collector.py

* Update waiter.py

* Delete ovos_bus_client/util/utils.py

* Delete test/unittests/test_compat.py

* Update requirements.txt

* Update message.py

* Update message.py

* Update __init__.py

* Update test_util.py

* Update test_message.py

* Update test_client.py

* Update scheduler.py

* Update scheduler.py

* Update scheduler.py

* Update scheduler.py

* Update test_event_scheduler.py
  • Loading branch information
JarbasAl authored Nov 21, 2024
1 parent 8ca8a6e commit 68147ad
Show file tree
Hide file tree
Showing 13 changed files with 13 additions and 256 deletions.
31 changes: 1 addition & 30 deletions ovos_bus_client/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,8 @@
from ovos_bus_client.message import Message, CollectionMessage, GUIMessage
from ovos_bus_client.session import SessionManager, Session

try:
from mycroft_bus_client import MessageBusClient as _MessageBusClientBase
except ImportError:
# TODO - code in the wild does isinstance checks
# this conditional subclassing should be removed ASAP, it is only here for the migration period
# mycroft_bus_client is abandonware until further notice from MycroftAI

class _MessageBusClientBase:
pass


class MessageBusClient(_MessageBusClientBase):
class MessageBusClient:
"""The Mycroft Messagebus Client
The Messagebus client connects to the Mycroft messagebus service
Expand Down Expand Up @@ -435,22 +425,3 @@ def on_message(self, *args):

parsed_message = GUIMessage.deserialize(message)
self.emitter.emit(parsed_message.msg_type, parsed_message)


@deprecated("No direct replacement", "0.1.0")
def echo():
"""
Echo function repeating all input from a user.
"""

from ovos_bus_client.util import create_echo_function
# TODO: Deprecate in 0.1.0
message_bus_client = MessageBusClient()

def repeat_utterance(message):
message.msg_type = 'speak'
message_bus_client.emit(message)

message_bus_client.on('message', create_echo_function(None))
message_bus_client.on('recognizer_loop:utterance', repeat_utterance)
message_bus_client.run_forever()
12 changes: 1 addition & 11 deletions ovos_bus_client/client/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,8 @@
from uuid import uuid4
import time

try:
from mycroft_bus_client.client.collector import MessageCollector as _MessageCollectorBase
except ImportError:
# TODO - code in the wild does isinstance checks
# this conditional subclassing should be removed ASAP, it is only here for the migration period
# mycroft_bus_client is abandonware until further notice from MycroftAI

class _MessageCollectorBase:
pass


class MessageCollector(_MessageCollectorBase):
class MessageCollector:
"""Collect multiple response.
This class encapsulates the logic for collecting messages from
Expand Down
11 changes: 0 additions & 11 deletions ovos_bus_client/client/waiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

from threading import Event
from typing import List, Union

try:
from mycroft_bus_client.client.waiter import MessageWaiter as _MessageWaiterBase
except ImportError:
# TODO - code in the wild does isinstance checks
# this conditional subclassing should be removed ASAP, it is only here for the migration period
# mycroft_bus_client is abandonware until further notice from MycroftAI

class _MessageWaiterBase:
pass


class MessageWaiter:
"""Wait for a single message.
Expand Down
73 changes: 7 additions & 66 deletions ovos_bus_client/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,8 @@
from ovos_utils.security import encrypt, decrypt
from ovos_config.config import Configuration

try:
from lingua_franca.parse import normalize
except ImportError:
# optional LF import
def normalize(text, *args, **kwargs):
return text

try:
from mycroft_bus_client.message import Message as _MsgBase, \
CollectionMessage as _CollectionMsgBase

except ImportError:

# TODO - code in the wild does isinstance checks
# this conditional subclassing should be removed ASAP, it is only here for the migration period
# mycroft_bus_client is abandonware until further notice from MycroftAI

class _MsgBase:
pass


class _CollectionMsgBase(_MsgBase):
pass


class _MessageMeta(type):
""" To override isinstance checks we need to use a metaclass """

def __instancecheck__(self, instance):
try:
from mycroft_bus_client.message import Message as _MycroftMessage
return isinstance(instance, _MycroftMessage) or \
super().__instancecheck__(instance)
except:
return super().__instancecheck__(instance)


class Message(_MsgBase, metaclass=_MessageMeta):
class Message:
"""Holds and manipulates data sent over the websocket
Message objects will be used to send information back and forth
Expand Down Expand Up @@ -165,7 +129,7 @@ def _json_load(value):
return obj

@staticmethod
def deserialize(value: str) -> _MsgBase:
def deserialize(value: str) -> 'Message':
"""
This takes a string and constructs a message object.
Expand All @@ -186,7 +150,7 @@ def deserialize(value: str) -> _MsgBase:
obj.get('data') or {},
obj.get('context') or {})

def forward(self, msg_type: str, data: dict = None) -> _MsgBase:
def forward(self, msg_type: str, data: dict = None) -> 'Message':
"""
Keep context and forward message
Expand All @@ -204,8 +168,7 @@ def forward(self, msg_type: str, data: dict = None) -> _MsgBase:
data = data or {}
return Message(msg_type, data, context=self.context)

def reply(self, msg_type: str, data: dict = None,
context: dict = None) -> _MsgBase:
def reply(self, msg_type: str, data: dict = None, context: dict = None) -> 'Message':
"""
Construct a reply message for a given message
Expand Down Expand Up @@ -240,7 +203,7 @@ def reply(self, msg_type: str, data: dict = None,
new_context['source'] = s
return Message(msg_type, data, context=new_context)

def response(self, data: dict = None, context: dict = None) -> _MsgBase:
def response(self, data: dict = None, context: dict = None) -> 'Message':
"""
Construct a response message for the message
Expand All @@ -255,8 +218,7 @@ def response(self, data: dict = None, context: dict = None) -> _MsgBase:
"""
return self.reply(self.msg_type + '.response', data, context)

def publish(self, msg_type: str, data: dict,
context: dict = None) -> _MsgBase:
def publish(self, msg_type: str, data: dict, context: dict = None) -> 'Message':
"""
Copy the original context and add passed in context. Delete
any target in the new context. Return a new message object with
Expand All @@ -280,27 +242,6 @@ def publish(self, msg_type: str, data: dict,

return Message(msg_type, data, context=new_context)

@deprecated("This method is deprecated with no replacement", "0.1.0")
def utterance_remainder(self):
"""
DEPRECATED - mycroft-core hack, used by some skills in the wild
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)


def encrypt_as_dict(key: str, data: str, nonce=None) -> dict:
ciphertext, tag, nonce = encrypt(key, data, nonce=nonce)
Expand Down Expand Up @@ -340,7 +281,7 @@ def dig_for_message(max_records: int = 10) -> Optional[Message]:
return None


class CollectionMessage(Message, _CollectionMsgBase):
class CollectionMessage(Message):
"""Extension of the Message class for use with collect handlers.
The class provides the convenience methods success and failure to report
Expand Down
1 change: 1 addition & 0 deletions ovos_bus_client/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from ovos_bus_client.session import SessionManager
from ovos_bus_client.util.scheduler import EventScheduler


_DEFAULT_WS_CONFIG = {"host": "0.0.0.0",
"port": 8181,
"route": "/core",
Expand Down
16 changes: 2 additions & 14 deletions ovos_bus_client/util/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@

from ovos_config.config import Configuration
from ovos_config.locations import get_xdg_data_save_path, get_xdg_config_save_path
from ovos_utils.log import LOG, log_deprecation
from ovos_utils.events import EventContainer as _EventContainer
from ovos_utils.events import EventSchedulerInterface as _SchedulerInterface
from ovos_utils.log import LOG
from ovos_utils.events import EventContainer, EventSchedulerInterface
from ovos_bus_client.message import Message


Expand Down Expand Up @@ -349,14 +348,3 @@ def shutdown(self):
self.store()
raise e


class EventContainer(_EventContainer):
def __init__(self, *args, **kwargs):
log_deprecation("Import from `ovos_utils.events`", "0.1.0")
_EventContainer.__init__(self, *args, **kwargs)


class EventSchedulerInterface(_SchedulerInterface):
def __init__(self, *args, **kwargs):
log_deprecation("Import from `ovos_utils.events`", "0.1.0")
_SchedulerInterface.__init__(self, *args, **kwargs)
55 changes: 0 additions & 55 deletions ovos_bus_client/util/utils.py

This file was deleted.

1 change: 0 additions & 1 deletion test/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pytest
pytest-cov
mycroft-messagebus-client
14 changes: 0 additions & 14 deletions test/unittests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,7 @@
WS_CONF = {"websocket": {"host": "testhost", "port": 1337, "route": "/core", "ssl": False}}


class TestClient(unittest.TestCase):
def test_echo(self):
from ovos_bus_client.client.client import echo

# TODO

def test_inheritance(self):
from mycroft_bus_client.client import MessageBusClient as _Client

self.assertTrue(issubclass(MessageBusClient, _Client))


class TestMessageBusClient(unittest.TestCase):
from ovos_bus_client.client.client import MessageBusClient

client = MessageBusClient()

def test_build_url(self):
Expand Down
15 changes: 0 additions & 15 deletions test/unittests/test_compat.py

This file was deleted.

23 changes: 1 addition & 22 deletions test/unittests/test_event_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

from unittest.mock import MagicMock, patch
from ovos_utils.messagebus import FakeBus
from ovos_bus_client.util.scheduler import EventScheduler, EventSchedulerInterface
from ovos_bus_client.util.scheduler import EventScheduler


# TODO - move to ovos-bus-client
class TestEventScheduler(unittest.TestCase):
@patch('threading.Thread')
@patch('json.load')
Expand Down Expand Up @@ -102,23 +101,3 @@ def test_send_event(self, mock_open, mock_dump, mock_load, mock_thread):
self.assertEqual(emitter.emit.call_args[0][0].data, {})
es.shutdown()


class TestEventSchedulerInterface(unittest.TestCase):
def test_shutdown(self):
def f(message):
print('TEST FUNC')

bus = ExecutorEventEmitter()

es = EventSchedulerInterface('tester')
es.set_bus(FakeBus())
es.set_id('id')

# Schedule a repeating event
es.schedule_repeating_event(f, None, 10, name='f')
self.assertTrue(len(es.bus.ee._events['id:f']) == 1)

es.shutdown()
# Check that the reference to the function has been removed from the
# bus emitter
self.assertTrue(len(bus._events['id:f']) == 0)
Loading

0 comments on commit 68147ad

Please sign in to comment.