Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-19 08:00:01

0001 from . import core_utils
0002 from .db_interface import DBInterface
0003 
0004 # logger
0005 _logger = core_utils.setup_logger("plugin_factory")
0006 
0007 
0008 # plugin factory
0009 class PluginFactory(object):
0010     # constructor
0011     def __init__(self, no_db=False):
0012         self.classMap = {}
0013         self.noDB = no_db
0014 
0015     # get plugin key
0016     def get_plugin_key(self, plugin_conf):
0017         # use module + class as key
0018         moduleName = plugin_conf["module"]
0019         className = plugin_conf["name"]
0020         pluginKey = f"{moduleName}.{className}"
0021         return pluginKey
0022 
0023     # get plugin instance
0024     def get_plugin(self, plugin_conf):
0025         # use module + class as key
0026         moduleName = plugin_conf["module"]
0027         className = plugin_conf["name"]
0028         pluginKey = f"{moduleName}.{className}"
0029         if moduleName is None or className is None:
0030             return None
0031         # get class
0032         if pluginKey not in self.classMap:
0033             tmpLog = core_utils.make_logger(_logger, method_name="get_plugin")
0034             # import module
0035             tmpLog.debug(f"importing {moduleName}")
0036             mod = __import__(moduleName)
0037             for subModuleName in moduleName.split(".")[1:]:
0038                 mod = getattr(mod, subModuleName)
0039             # get class
0040             tmpLog.debug(f"getting class {className}")
0041             cls = getattr(mod, className)
0042             # add
0043             self.classMap[pluginKey] = cls
0044         # make args
0045         args = {}
0046         for tmpKey, tmpVal in plugin_conf.items():
0047             if tmpKey in ["module", "name"]:
0048                 continue
0049             args[tmpKey] = tmpVal
0050         # add database interface
0051         if not self.noDB:
0052             args["dbInterface"] = DBInterface()
0053         # instantiate
0054         cls = self.classMap[pluginKey]
0055         impl = cls(**args)
0056         # bare instance when middleware is used
0057         if "original_config" in plugin_conf and "bareFunctions" in plugin_conf:
0058             bare_impl = self.get_plugin(plugin_conf["original_config"])
0059             impl.bare_impl = bare_impl
0060         return impl