diff --git a/ovos_plugin_manager/microphone.py b/ovos_plugin_manager/microphone.py index 9e4c86c..d6bc213 100644 --- a/ovos_plugin_manager/microphone.py +++ b/ovos_plugin_manager/microphone.py @@ -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: @@ -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 @@ -45,7 +48,7 @@ def get_class(config=None): "module": } """ - config = get_microphone_config(config) + config = config or Configuration().get("listener", {}).get("microphone", {}) microphone_module = config.get("module") return load_microphone_plugin(microphone_module) @@ -60,19 +63,12 @@ def create(cls, config=None): "module": } """ - 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) diff --git a/test/unittests/test_microphone.py b/test/unittests/test_microphone.py index cd7114d..e2cfddc 100644 --- a/test/unittests/test_microphone.py +++ b/test/unittests/test_microphone.py @@ -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): @@ -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 @@ -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) @@ -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') @@ -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'})