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

Update config default value handling with updated unit test #276

Merged
merged 2 commits into from
Nov 18, 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
30 changes: 21 additions & 9 deletions ovos_plugin_manager/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def get_plugin_config(config: Optional[dict] = None, section: str = None,
- module-specific configurations take priority
- section-specific configuration is appended (new keys only)
- global `lang` configuration is appended (if not already set)

If no module is specified, then the requested section configuration is
assumed to be a top-level key in the base configuration, defaulting to the
base configuration if that section is not found. If both `module` and
`section` are unspecified, then the base configuration is returned.
@param config: Base configuration to parse, defaults to `Configuration()`
@param section: Config section for the plugin (i.e. TTS, STT, language)
@param module: Module/plugin to get config for, default reads from config
Expand All @@ -27,16 +32,23 @@ def get_plugin_config(config: Optional[dict] = None, section: str = None,
if module:
module_config = dict(config.get(module) or dict())
module_config.setdefault('module', module)
for key, val in config.items():
# Configured module name is not part of that module's config
if key in ("module", "translation_module", "detection_module"):
continue
elif isinstance(val, dict):
continue
# Use section-scoped config as defaults (i.e. TTS.lang)
module_config.setdefault(key, val)
if config == Configuration():
LOG.debug(f"No `{section}` config in Configuration")
else:
# If the config section exists (i.e. `stt`), then handle any default
# values in that section (i.e. `lang`)
for key, val in config.items():
# Configured module name is not part of that module's config
if key in ("module", "translation_module", "detection_module"):
continue
elif isinstance(val, dict):
continue
# Use section-scoped config as defaults (i.e. TTS.lang)
module_config.setdefault(key, val)
config = module_config
if section not in ["hotwords", "VAD", "listener", "gui"]:
if section not in ["hotwords", "VAD", "listener", "gui", None]:
# With some exceptions, plugins will want a `lang` value. If it was not
# set in the section or module config, use the default top-level config.
config.setdefault('lang', lang)
LOG.debug(f"Loaded configuration: {config}")
return config
Expand Down
7 changes: 7 additions & 0 deletions test/unittests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,13 @@ def test_get_plugin_config(self, config):
self.assertEqual(tts_config['voice'], 'german-mbrola-5')
self.assertNotIn("ovos_tts_plugin_espeakng", tts_config)

# Test PHAL with no configuration
phal_config = get_plugin_config(config, "PHAL")
self.assertEqual(set(phal_config.keys()), config.keys())
phal_config = get_plugin_config(config, "PHAL", "test_plugin")
self.assertEqual(phal_config, {"module": "test_plugin",
"lang": config["lang"]})

self.assertEqual(_MOCK_CONFIG, start_config)

def test_get_valid_plugin_configs(self):
Expand Down
Loading