Skip to content

Commit f8d3450

Browse files
authored
Update config default value handling with updated unit test (#276)
* Update `config` default value handling with updated unit test * Update unit tests Update docstring to more completely describe behavior
1 parent 9149751 commit f8d3450

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

ovos_plugin_manager/utils/config.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def get_plugin_config(config: Optional[dict] = None, section: str = None,
1414
- module-specific configurations take priority
1515
- section-specific configuration is appended (new keys only)
1616
- global `lang` configuration is appended (if not already set)
17+
18+
If no module is specified, then the requested section configuration is
19+
assumed to be a top-level key in the base configuration, defaulting to the
20+
base configuration if that section is not found. If both `module` and
21+
`section` are unspecified, then the base configuration is returned.
1722
@param config: Base configuration to parse, defaults to `Configuration()`
1823
@param section: Config section for the plugin (i.e. TTS, STT, language)
1924
@param module: Module/plugin to get config for, default reads from config
@@ -27,16 +32,23 @@ def get_plugin_config(config: Optional[dict] = None, section: str = None,
2732
if module:
2833
module_config = dict(config.get(module) or dict())
2934
module_config.setdefault('module', module)
30-
for key, val in config.items():
31-
# Configured module name is not part of that module's config
32-
if key in ("module", "translation_module", "detection_module"):
33-
continue
34-
elif isinstance(val, dict):
35-
continue
36-
# Use section-scoped config as defaults (i.e. TTS.lang)
37-
module_config.setdefault(key, val)
35+
if config == Configuration():
36+
LOG.debug(f"No `{section}` config in Configuration")
37+
else:
38+
# If the config section exists (i.e. `stt`), then handle any default
39+
# values in that section (i.e. `lang`)
40+
for key, val in config.items():
41+
# Configured module name is not part of that module's config
42+
if key in ("module", "translation_module", "detection_module"):
43+
continue
44+
elif isinstance(val, dict):
45+
continue
46+
# Use section-scoped config as defaults (i.e. TTS.lang)
47+
module_config.setdefault(key, val)
3848
config = module_config
39-
if section not in ["hotwords", "VAD", "listener", "gui"]:
49+
if section not in ["hotwords", "VAD", "listener", "gui", None]:
50+
# With some exceptions, plugins will want a `lang` value. If it was not
51+
# set in the section or module config, use the default top-level config.
4052
config.setdefault('lang', lang)
4153
LOG.debug(f"Loaded configuration: {config}")
4254
return config

test/unittests/test_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,13 @@ def test_get_plugin_config(self, config):
649649
self.assertEqual(tts_config['voice'], 'german-mbrola-5')
650650
self.assertNotIn("ovos_tts_plugin_espeakng", tts_config)
651651

652+
# Test PHAL with no configuration
653+
phal_config = get_plugin_config(config, "PHAL")
654+
self.assertEqual(set(phal_config.keys()), config.keys())
655+
phal_config = get_plugin_config(config, "PHAL", "test_plugin")
656+
self.assertEqual(phal_config, {"module": "test_plugin",
657+
"lang": config["lang"]})
658+
652659
self.assertEqual(_MOCK_CONFIG, start_config)
653660

654661
def test_get_valid_plugin_configs(self):

0 commit comments

Comments
 (0)