"""Package containing all modules used by agents.Use the helper functions get_module_typeto load module classes from this package."""fromtypingimportUnion,Dict,Iterable,Listfromagentlib.utilsimportplugin_importfromagentlib.utils.fuzzy_matchingimportfuzzy_match# Global modules:_MODULE_TYPES=plugin_import.SaveUpdateDict()_LOADED_CORE_MODULES=Falsedef_import_modules():""" Import the module information, but only if it has not been loaded yet. """global_MODULE_TYPESifnot_LOADED_CORE_MODULES:_load_core_modules()# Only return copy, never allow changes in the private objectreturn_MODULE_TYPES.copy()def_load_core_modules()->None:global_LOADED_CORE_MODULES,_MODULE_TYPES# Communicatorimportagentlib.modules.communicatorascommunicator_MODULE_TYPES.update(communicator.MODULE_TYPES)# Controllerimportagentlib.modules.controllerascontroller_MODULE_TYPES.update(controller.MODULE_TYPES)# Utilsimportagentlib.modules.utilsasutils_MODULE_TYPES.update(utils.MODULE_TYPES)# Simulatorimportagentlib.modules.simulationassimulation_MODULE_TYPES.update(simulation.MODULE_TYPES)_LOADED_CORE_MODULES=True
[docs]defget_module_type(module_type)->Union[Dict,Iterable]:""" Return and load the given module type Args: module_type str: The string identifier to load the module. Returns: module BaseModuleType: The module specified by the given module_type """# Check if it's a pluginif"."inmodule_type:plugin_import.load_plugin(name=module_type.split(".")[0],loaded_classes=_MODULE_TYPES,plugin_types_name="MODULE_TYPES",)# Load the core and plugin modulesmodule_types=_import_modules()ifmodule_typeinmodule_types:returnmodule_types[module_type].import_class()# Raise error if still herematches=fuzzy_match(target=module_type,choices=module_types.keys())msg=(f"Given module_type '{module_type}' is neither in the AgentLib nor in "f"installed plugins. ")ifmatches:msg+=f"Did you mean one of these? {', '.join(matches)}"# Extract names onlyraiseModuleNotFoundError(msg)
[docs]defget_all_module_types(plugins:List[str]=None):""" Returns all available module types Args: plugins List[str]: A list of strings being the plugins to consider in the search. Returns: dict: Module types, with the key as the types name and the value being the ModuleImport instance """forplugininplugins:plugin_import.load_plugin(name=plugin,loaded_classes=_MODULE_TYPES,plugin_types_name="MODULE_TYPES")module_types=_import_modules()returnmodule_types