Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:missing import #279

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions ovos_plugin_manager/microphone.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from ovos_plugin_manager.utils import PluginTypes
from ovos_config import Configuration
from ovos_utils.log import LOG, deprecated

from ovos_plugin_manager.templates.microphone import Microphone
from ovos_utils.log import LOG
from ovos_plugin_manager.utils import PluginTypes


def find_microphone_plugins() -> dict:
Expand All @@ -22,6 +24,7 @@ def load_microphone_plugin(module_name: str) -> type(Microphone):
return load_plugin(module_name, PluginTypes.MIC)


@deprecated("get_microphone_config is deprecated, use Configuration() directly", "1.0.0")
def get_microphone_config(config=None):
"""
Get relevant configuration for factory methods
Expand All @@ -45,7 +48,7 @@ def get_class(config=None):
"module": <engine_name>
}
"""
config = get_microphone_config(config)
config = config or Configuration().get("listener", {}).get("microphone", {})
microphone_module = config.get("module")
return load_microphone_plugin(microphone_module)

Expand All @@ -60,19 +63,12 @@ def create(cls, config=None):
"module": <engine_name>
}
"""
config = config or Configuration()
if "microphone" in config:
config = config["microphone"]
microphone_config = get_microphone_config(config)
microphone_module = microphone_config.get('module')
config = config or Configuration().get("listener", {}).get("microphone", {})
microphone_module = config.get('module')
microphone_config = config.get(microphone_module, {})
fallback = microphone_config.get("fallback_module")
try:
clazz = OVOSMicrophoneFactory.get_class(microphone_config)
# Note that configuration is expanded for this class of plugins
# since they are dataclasses and don't have the same init signature
# as other plugin types
microphone_config.pop('lang')
microphone_config.pop('module')
clazz = OVOSMicrophoneFactory.get_class(config)
if fallback:
microphone_config.pop('fallback_module')
microphone = clazz(**microphone_config)
Expand Down
39 changes: 5 additions & 34 deletions test/unittests/test_microphone.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,6 @@ def test_load_plugin(self, load_plugin):
load_microphone_plugin("test_mod")
load_plugin.assert_called_once_with("test_mod", self.PLUGIN_TYPE)

@patch("ovos_plugin_manager.utils.config.get_plugin_config")
def test_get_config(self, get_config):
from ovos_plugin_manager.microphone import get_microphone_config
get_microphone_config(self.TEST_CONFIG)
get_config.assert_called_once_with(self.TEST_CONFIG,
self.CONFIG_SECTION)


class TestMicrophoneFactory(unittest.TestCase):
def test_create_microphone(self):
Expand All @@ -154,11 +147,9 @@ def _copy_args(*args):
mock_get_class = Mock(side_effect=_copy_args)
OVOSMicrophoneFactory.get_class = mock_get_class

OVOSMicrophoneFactory.create(config=_TEST_CONFIG)
OVOSMicrophoneFactory.create(config=_TEST_CONFIG['microphone'])
mock_get_class.assert_called_once()
self.assertEqual(call_args, ({**_TEST_CONFIG['microphone']['dummy'],
**{"module": "dummy",
"lang": "en-US"}},))
self.assertEqual(call_args, ({**_TEST_CONFIG['microphone']},))
mock_class.assert_called_once_with(**_TEST_CONFIG['microphone']['dummy'])
OVOSMicrophoneFactory.get_class = real_get_class

Expand All @@ -171,7 +162,7 @@ def test_create_microphone_fallback(self):

def _copy_args(*args):
nonlocal call_args, bad_call_args
if args[0]["module"] == "bad":
if args[0].get("module", "") == "bad":
bad_call_args = deepcopy(args)
return None
call_args = deepcopy(args)
Expand All @@ -180,7 +171,7 @@ def _copy_args(*args):
mock_get_class = Mock(side_effect=_copy_args)
OVOSMicrophoneFactory.get_class = mock_get_class

OVOSMicrophoneFactory.create(config=_FALLBACK_CONFIG)
OVOSMicrophoneFactory.create(config=_FALLBACK_CONFIG['microphone'])
mock_get_class.assert_called()
self.assertEqual(call_args[0]["module"], 'dummy')
self.assertEqual(bad_call_args[0]["module"], 'bad')
Expand All @@ -193,27 +184,7 @@ def test_get_class(self, load_plugin):
load_plugin.return_value = mock
from ovos_plugin_manager.microphone import OVOSMicrophoneFactory
# Test valid module
module = OVOSMicrophoneFactory.get_class(_TEST_CONFIG)
module = OVOSMicrophoneFactory.get_class(_TEST_CONFIG['microphone'])
load_plugin.assert_called_once_with("dummy",
PluginTypes.MIC)
self.assertEqual(mock, module)

def test_get_microphone_config(self):
from ovos_plugin_manager.microphone import get_microphone_config
config = copy(_TEST_CONFIG)
dummy_config = get_microphone_config(config)
self.assertEqual(dummy_config, {**_TEST_CONFIG['microphone']['dummy'],
**{'module': 'dummy',
'lang': 'en-US'}})
config = copy(_TEST_CONFIG)
config['microphone']['module'] = 'ovos-microphone-plugin-alsa'
alsa_config = get_microphone_config(config)
self.assertEqual(alsa_config,
{**_TEST_CONFIG['microphone']
['ovos-microphone-plugin-alsa'],
**{'module': 'ovos-microphone-plugin-alsa',
'lang': 'en-US'}})
config = copy(_TEST_CONFIG)
config['microphone']['module'] = 'fake'
fake_config = get_microphone_config(config)
self.assertEqual(fake_config, {'module': 'fake', 'lang': 'en-US'})
Loading