Skip to content

Commit

Permalink
Refactor to consolidate common logic (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeonDaniel authored May 26, 2023
1 parent af054b4 commit 534119a
Show file tree
Hide file tree
Showing 25 changed files with 1,381 additions and 953 deletions.
42 changes: 28 additions & 14 deletions ovos_plugin_manager/audio.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
from ovos_plugin_manager.utils import PluginConfigTypes, load_plugin, find_plugins, PluginTypes
from ovos_plugin_manager.utils import PluginConfigTypes, find_plugins, PluginTypes
from ovos_utils.log import LOG
from ovos_utils.messagebus import get_mycroft_bus
from ovos_config import Configuration


def find_audio_service_plugins() -> dict:
"""
Find all installed plugins
@return: dict plugin names to entrypoints
"""
return find_plugins(PluginTypes.AUDIO)


def get_audio_service_configs() -> dict:
"""
Get valid plugin configurations by plugin name
@return: dict plugin names to list of dict configurations
"""
from ovos_plugin_manager.utils.config import load_configs_for_plugin_type
return load_configs_for_plugin_type(PluginTypes.AUDIO)


def get_audio_service_module_configs(module_name: str) -> dict:
"""
Get valid configuration for the specified plugin
@param module_name: plugin to get configuration for
@return: dict configuration (if provided)
"""
from ovos_plugin_manager.utils.config import load_plugin_configs
return load_plugin_configs(module_name, PluginConfigTypes.AUDIO)


def setup_audio_service(service_module, config=None, bus=None):
"""Run the appropriate setup function and return created service objects.
Expand Down Expand Up @@ -31,19 +58,6 @@ def setup_audio_service(service_module, config=None, bus=None):
return None


def find_audio_service_plugins():
return find_plugins(PluginTypes.AUDIO)


def get_audio_service_configs():
return {plug: get_audio_service_module_configs(plug)
for plug in find_audio_service_plugins()}


def get_audio_service_module_configs(module_name):
return load_plugin(module_name + ".config", PluginConfigTypes.AUDIO)


def load_audio_service_plugins(config=None, bus=None):
"""Load installed audioservice plugins.
Expand Down
30 changes: 22 additions & 8 deletions ovos_plugin_manager/audio2ipa.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
from typing import Optional
from ovos_plugin_manager.utils import normalize_lang, load_plugin, find_plugins, PluginTypes, PluginConfigTypes
from ovos_plugin_manager.templates.audio2ipa import Audio2IPA
from ovos_utils.log import LOG


def find_audio2ipa_plugins():
def find_audio2ipa_plugins() -> dict:
"""
Find all installed plugins
@return: dict plugin names to entrypoints
"""
return find_plugins(PluginTypes.AUDIO2IPA)


def load_audio2ipa_plugin(module_name):
def load_audio2ipa_plugin(module_name: str) -> type(Audio2IPA):
"""
Get an uninstantiated class for the requested module_name
@param module_name: Plugin entrypoint name to load
@return: Uninstantiated class
"""
return load_plugin(module_name, PluginTypes.AUDIO2IPA)


class OVOSAudio2IPAFactory:
def get_audio2ipa_config(config: Optional[dict] = None) -> dict:
"""
Get relevant configuration for factory methods
@param config: global Configuration OR plugin class-specific configuration
@return: plugin class-specific configuration
"""
from ovos_plugin_manager.utils.config import get_plugin_config
return get_plugin_config(config, "audio2ipa")


class OVOSAudio2IPAFactory:
@staticmethod
def get_class(config=None):
"""Factory method to get a Audio2IPA engine class based on configuration.
Expand Down Expand Up @@ -49,8 +68,3 @@ def create(config=None):
LOG.debug('The selected Audio2IPA plugin could not be loaded.')
raise
return audio2ipa


def get_audio2ipa_config(config=None):
from ovos_plugin_manager.utils.config import get_plugin_config
return get_plugin_config(config, "audio2ipa")
39 changes: 27 additions & 12 deletions ovos_plugin_manager/audio_transformers.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
from ovos_plugin_manager.utils import load_plugin, find_plugins, PluginTypes, PluginConfigTypes
from ovos_plugin_manager.utils import load_plugin, find_plugins, PluginTypes, \
PluginConfigTypes
from ovos_plugin_manager.templates.transformers import AudioTransformer


def find_audio_transformer_plugins():
def find_audio_transformer_plugins() -> dict:
"""
Find all installed plugins
@return: dict plugin names to entrypoints
"""
return find_plugins(PluginTypes.AUDIO_TRANSFORMER)


def get_audio_transformer_configs():
return {plug: get_audio_transformer_module_configs(plug)
for plug in find_audio_transformer_plugins()}


def get_audio_transformer_module_configs(module_name):
return load_plugin(module_name + ".config", PluginConfigTypes.AUDIO_TRANSFORMER)


def load_audio_transformer_plugin(module_name):
def load_audio_transformer_plugin(module_name: str) -> type(AudioTransformer):
"""Wrapper function for loading audio_transformer plugin.
Arguments:
Expand All @@ -24,3 +21,21 @@ def load_audio_transformer_plugin(module_name):
"""
return load_plugin(module_name, PluginTypes.AUDIO_TRANSFORMER)


def get_audio_transformer_configs() -> dict:
"""
Get valid plugin configurations by plugin name
@return: dict plugin names to list of dict configurations
"""
from ovos_plugin_manager.utils.config import load_configs_for_plugin_type
return load_configs_for_plugin_type(PluginTypes.AUDIO_TRANSFORMER)


def get_audio_transformer_module_configs(module_name: str):
"""
Get valid configuration for the specified plugin
@param module_name: plugin to get configuration for
@return: dict configuration (if provided) (TODO: Validate return type)
"""
from ovos_plugin_manager.utils.config import load_plugin_configs
return load_plugin_configs(module_name, PluginConfigTypes.AUDIO_TRANSFORMER)
125 changes: 69 additions & 56 deletions ovos_plugin_manager/coreference.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,83 @@
from ovos_plugin_manager.utils import normalize_lang, load_plugin, find_plugins, PluginTypes, PluginConfigTypes
from typing import Optional

from ovos_plugin_manager.utils import normalize_lang, load_plugin, \
find_plugins, PluginTypes, PluginConfigTypes
from ovos_config import Configuration
from ovos_utils.log import LOG
from ovos_plugin_manager.templates.coreference import CoreferenceSolverEngine, replace_coreferences
from ovos_plugin_manager.templates.coreference import CoreferenceSolverEngine, \
replace_coreferences


def find_coref_plugins():
def find_coref_plugins() -> dict:
"""
Find all installed plugins
@return: dict plugin names to entrypoints
"""
return find_plugins(PluginTypes.COREFERENCE_SOLVER)


def get_coref_configs():
return {plug: get_coref_module_configs(plug)
for plug in find_coref_plugins()}


def get_coref_module_configs(module_name):
cfgs = load_plugin(module_name + ".config", PluginConfigTypes.COREFERENCE_SOLVER) or {}
return {normalize_lang(lang): v for lang, v in cfgs.items()}


def get_coref_lang_configs(lang, include_dialects=False):
lang = normalize_lang(lang)
configs = {}
for plug in find_coref_plugins():
configs[plug] = []
confs = get_coref_module_configs(plug)
if include_dialects:
lang = lang.split("-")[0]
for l in confs:
if l.startswith(lang):
configs[plug] += confs[l]
elif lang in confs:
configs[plug] += confs[lang]
elif f"{lang}-{lang}" in confs:
configs[plug] += confs[f"{lang}-{lang}"]
return {k: v for k, v in configs.items() if v}


def get_coref_supported_langs():
configs = {}
for plug in find_coref_plugins():
confs = get_coref_module_configs(plug)
for lang, cfgs in confs.items():
if confs:
if lang not in configs:
configs[lang] = []
configs[lang].append(plug)
return configs


def load_coref_plugin(module_name):
"""Wrapper function for loading coref plugin.
Arguments:
module_name (str): coref module name from config
Returns:
class: CoreferenceSolver plugin class
def load_coref_plugin(module_name: str) -> type(CoreferenceSolverEngine):
"""
Get an uninstantiated class for the requested module_name
@param module_name: Plugin entrypoint name to load
@return: Uninstantiated class
"""
return load_plugin(module_name, PluginTypes.COREFERENCE_SOLVER)


def get_coref_configs() -> dict:
"""
Get valid plugin configurations by plugin name
@return: dict plugin names to list of dict configurations
"""
from ovos_plugin_manager.utils.config import load_configs_for_plugin_type
return load_configs_for_plugin_type(PluginTypes.COREFERENCE_SOLVER)


def get_coref_module_configs(module_name: str) -> dict:
"""
Get valid configuration for the specified plugin
@param module_name: plugin to get configuration for
@return: dict configuration (if provided)
"""
from ovos_plugin_manager.utils.config import load_plugin_configs
return load_plugin_configs(module_name,
PluginConfigTypes.COREFERENCE_SOLVER, True)


def get_coref_lang_configs(lang: str, include_dialects: bool = False) -> dict:
"""
Get a dict of plugin names to list valid configurations for the requested
lang.
@param lang: Language to get configurations for
@param include_dialects: consider configurations in different locales
@return: dict {`plugin_name`: [`valid_configs`]}
"""
from ovos_plugin_manager.utils.config import get_plugin_language_configs
return get_plugin_language_configs(PluginTypes.COREFERENCE_SOLVER, lang,
include_dialects)


def get_coref_supported_langs() -> dict:
"""
Return a dict of plugin names to list supported languages
@return: dict plugin names to list supported languages
"""
from ovos_plugin_manager.utils.config import get_plugin_supported_languages
return get_plugin_supported_languages(PluginTypes.COREFERENCE_SOLVER)


def get_coref_config(config: Optional[dict] = None) -> dict:
"""
Get relevant configuration for factory methods
@param config: global Configuration OR plugin class-specific configuration
@return: plugin class-specific configuration
"""
from ovos_plugin_manager.utils.config import get_plugin_config
config = config or Configuration()
return get_plugin_config(config, "coref")


class OVOSCoreferenceSolverFactory:
""" replicates the base mycroft class, but uses only OPM enabled plugins"""
MAPPINGS = {
Expand Down Expand Up @@ -105,9 +124,3 @@ def create(config=None):
LOG.exception(f'CoreferenceSolver plugin {plugin} '
f'could not be loaded!')
raise


def get_coref_config(config=None):
from ovos_plugin_manager.utils.config import get_plugin_config
config = config or Configuration()
return get_plugin_config(config, "coref")
Loading

0 comments on commit 534119a

Please sign in to comment.