Skip to content

Commit

Permalink
Add get_class methods with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NeonDaniel committed Oct 11, 2023
1 parent 806c653 commit 0b5269d
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 17 deletions.
69 changes: 52 additions & 17 deletions ovos_plugin_manager/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,33 @@ class OVOSLangDetectionFactory:
}

@staticmethod
def create(config=None):
def get_class(config=None):
"""
Factory method to get a Language Detector class based on configuration.
Configuration contains a `language` section with
the name of a LangDetection module to be read by this method.
"language": {
"detection_module": <engine_name>
}
"""
config = config or Configuration()
if "language" in config:
config = config["language"]
lang_module = config.get("detection_module", config.get("module"))
if not lang_module:
raise ValueError("`language.detection_module` not configured")
if lang_module in OVOSLangDetectionFactory.MAPPINGS:
lang_module = OVOSLangDetectionFactory.MAPPINGS[lang_module]
return load_lang_detect_plugin(lang_module)

@staticmethod
def create(config=None) -> LanguageDetector:
"""
Factory method to create a LangDetection engine based on configuration
The configuration file `mycroft.conf` contains a `language` section with
Configuration contains a `language` section with
the name of a LangDetection module to be read by this method.
"language": {
Expand All @@ -135,12 +157,7 @@ def create(config=None):
config = config["language"]
lang_module = config.get("detection_module", config.get("module"))
try:
if not lang_module:
raise ValueError("`language.detection_module` not configured")
if lang_module in OVOSLangDetectionFactory.MAPPINGS:
lang_module = OVOSLangDetectionFactory.MAPPINGS[lang_module]

clazz = load_lang_detect_plugin(lang_module)
clazz = OVOSLangDetectionFactory.get_class(config)
if clazz is None:
raise ValueError(f"Failed to load module: {lang_module}")
LOG.info(f'Loaded the Language Detection plugin {lang_module}')
Expand Down Expand Up @@ -170,12 +187,34 @@ class OVOSLangTranslationFactory:
}

@staticmethod
def create(config=None):
def get_class(config=None):
"""
Factory method to get a Language Translator class based on configuration.
Configuration contains a `language` section with
the name of a Translation module to be read by this method.
"language": {
"translation_module": <engine_name>
}
"""
config = config or Configuration()
if "language" in config:
config = config["language"]
lang_module = config.get("translation_module", config.get("module"))
if not lang_module:
raise ValueError("`language.translation_module` not configured")
if lang_module in OVOSLangTranslationFactory.MAPPINGS:
lang_module = OVOSLangTranslationFactory.MAPPINGS[lang_module]
return load_tx_plugin(lang_module)

@staticmethod
def create(config=None) -> LanguageTranslator:
"""
Factory method to create a LangTranslation engine based on configuration
The configuration file `mycroft.conf` contains a `language` section with
the name of a LangDetection module to be read by this method.
Configuration contains a `language` section with
the name of a Translation module to be read by this method.
"language": {
"translation_module": <engine_name>
Expand All @@ -186,18 +225,14 @@ def create(config=None):
config = config["language"]
lang_module = config.get("translation_module", config.get("module"))
try:
if not lang_module:
raise ValueError("`language.translation_module` not configured")
if lang_module in OVOSLangTranslationFactory.MAPPINGS:
lang_module = OVOSLangTranslationFactory.MAPPINGS[lang_module]
clazz = load_tx_plugin(lang_module)
clazz = OVOSLangTranslationFactory.get_class(config)
if clazz is None:
raise ValueError(f"Failed to load module: {lang_module}")
LOG.info(f'Loaded the Language Translation plugin {lang_module}')
return clazz(config=get_plugin_config(config, "language",
lang_module))
except Exception:
# The Language Detection backend failed to start, fall back if appropriate.
# The Language Translation backend failed to start, fall back if appropriate.
if lang_module != _fallback_translate_plugin:
lang_module = _fallback_translate_plugin
LOG.error(f'Language Translation plugin {lang_module} '
Expand Down
61 changes: 61 additions & 0 deletions test/unittests/test_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,36 @@ def test_mappings(self):
str)
self.assertNotEqual(conf, OVOSLangDetectionFactory.MAPPINGS[conf])

@patch("ovos_plugin_manager.language.load_lang_detect_plugin")
@patch("ovos_plugin_manager.language.Configuration")
def test_get_class(self, config, load_plugin):
from ovos_plugin_manager.language import OVOSLangDetectionFactory
test_config = {"language": {
"detection_module": "libretranslate"
}}
mock_class = Mock()
config.return_value = test_config
load_plugin.return_value = mock_class

# Test mapped plugin from config
self.assertEquals(OVOSLangDetectionFactory.get_class(), mock_class)
load_plugin.assert_called_with("libretranslate_detection_plug")

# Test explicitly specified mapped plugin
conf = {"module": "google"}
self.assertEquals(OVOSLangDetectionFactory.get_class(conf), mock_class)
load_plugin.assert_called_with("googletranslate_detection_plug")

# Test unmapped plugin
conf = {"language": {"detection_module": "real-detect-plug"}}
self.assertEquals(OVOSLangDetectionFactory.get_class(conf), mock_class)
load_plugin.assert_called_with("real-detect-plug")

# Test invalid module config
conf = {"language": {}}
with self.assertRaises(ValueError):
OVOSLangDetectionFactory.get_class(conf)

@patch("ovos_plugin_manager.language.load_lang_detect_plugin")
@patch("ovos_plugin_manager.language.Configuration")
def test_create(self, config, load_plugin):
Expand Down Expand Up @@ -145,6 +175,37 @@ def test_mappings(self):
str)
self.assertNotEqual(conf, OVOSLangTranslationFactory.MAPPINGS[conf])

@patch("ovos_plugin_manager.language.load_tx_plugin")
@patch("ovos_plugin_manager.language.Configuration")
def test_get_class(self, config, load_plugin):
from ovos_plugin_manager.language import OVOSLangTranslationFactory
test_config = {"language": {
"translation_module": "libretranslate"
}}
mock_class = Mock()
config.return_value = test_config
load_plugin.return_value = mock_class

# Test mapped plugin from config
self.assertEquals(OVOSLangTranslationFactory.get_class(), mock_class)
load_plugin.assert_called_with("libretranslate_plug")

# Test explicitly specified mapped plugin
conf = {"module": "google"}
self.assertEquals(OVOSLangTranslationFactory.get_class(conf),
mock_class)
load_plugin.assert_called_with("googletranslate_plug")

# Test unmapped plugin
conf = {"language": {"translation_module": "real-detect-plug"}}
self.assertEquals(OVOSLangTranslationFactory.get_class(conf), mock_class)
load_plugin.assert_called_with("real-detect-plug")

# Test invalid module config
conf = {"language": {}}
with self.assertRaises(ValueError):
OVOSLangTranslationFactory.get_class(conf)

@patch("ovos_plugin_manager.language.load_tx_plugin")
@patch("ovos_plugin_manager.language.Configuration")
def test_create(self, config, load_plugin):
Expand Down

0 comments on commit 0b5269d

Please sign in to comment.