Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 08:39:14

0001 #!/usr/bin/env python
0002 # Licensed under the Apache License, Version 2.0 (the "License");
0003 # you may not use this file except in compliance with the License.
0004 # You may obtain a copy of the License at
0005 # http://www.apache.org/licenses/LICENSE-2.0
0006 #
0007 # Authors:
0008 # - Wen Guan, wen.guan@cern.ch, 2018
0009 # - Paul Nilsson, paul.nilsson@cern.ch, 2021
0010 
0011 
0012 import logging
0013 logger = logging.getLogger(__name__)
0014 
0015 """
0016 A factory to manage plugins
0017 """
0018 
0019 
0020 class PluginFactory(object):
0021 
0022     def __init__(self, *args, **kwargs):
0023         self.classMap = {}
0024 
0025     def get_plugin(self, confs):
0026         """
0027         Load plugin class
0028 
0029         :param confs: a dict of configurations.
0030         """
0031 
0032         class_name = confs['class']
0033         if class_name is None:
0034             logger.error("[class] is not defined in confs: %s", confs)
0035             return None
0036 
0037         if class_name not in self.classMap:
0038             logger.info("Trying to import %s", class_name)
0039             components = class_name.split('.')
0040             mod = __import__('.'.join(components[:-1]))
0041             for comp in components[1:]:
0042                 mod = getattr(mod, comp)
0043             self.classMap[class_name] = mod
0044 
0045         args = {}
0046         for key in confs:
0047             if key in ['class']:
0048                 continue
0049             args[key] = confs[key]
0050 
0051         cls = self.classMap[class_name]
0052         logger.info("Importing %s with args: %s", cls, args)
0053         impl = cls(**args)
0054 
0055         return impl