Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 08:38:58

0001 import sys
0002 
0003 from .MsgWrapper import MsgWrapper
0004 
0005 _factoryModuleName = __name__.split(".")[-1]
0006 
0007 
0008 # base class for factory
0009 class FactoryBase:
0010     # constructor
0011     def __init__(self, vos, sourceLabels, logger, modConfig):
0012         if isinstance(vos, list):
0013             self.vos = vos
0014         else:
0015             try:
0016                 self.vos = vos.split("|")
0017             except Exception:
0018                 self.vos = [vos]
0019         if isinstance(sourceLabels, list):
0020             self.sourceLabels = sourceLabels
0021         else:
0022             try:
0023                 self.sourceLabels = sourceLabels.split("|")
0024             except Exception:
0025                 self.sourceLabels = [sourceLabels]
0026         self.modConfig = modConfig
0027         self.logger = MsgWrapper(logger, _factoryModuleName)
0028         self.implMap = {}
0029         self.className = None
0030         self.classMap = {}
0031 
0032     # initialize all modules
0033     def initializeMods(self, *args):
0034         # parse config
0035         for configStr in self.modConfig.split(","):
0036             configStr = configStr.strip()
0037             items = configStr.split(":")
0038             # check format
0039             try:
0040                 vos = items[0].split("|")
0041                 sourceLabels = items[1].split("|")
0042                 moduleName = items[2]
0043                 className = items[3]
0044                 try:
0045                     subTypes = items[4].split("|")
0046                 except Exception:
0047                     subTypes = ["any"]
0048             except Exception:
0049                 self.logger.error(f"wrong config definition : {configStr}")
0050                 continue
0051             # loop over all VOs
0052             for vo in vos:
0053                 # loop over all labels
0054                 for sourceLabel in sourceLabels:
0055                     # check vo and sourceLabel if specified
0056                     if vo not in ["", "any"] and vo not in self.vos and None not in self.vos and "any" not in self.vos:
0057                         continue
0058                     if (
0059                         sourceLabel not in ["", "any"]
0060                         and sourceLabel not in self.sourceLabels
0061                         and None not in self.sourceLabels
0062                         and "any" not in self.sourceLabels
0063                     ):
0064                         continue
0065                     # loop over all sub types
0066                     for subType in subTypes:
0067                         # import
0068                         try:
0069                             # import module
0070                             self.logger.info(f"vo={vo} label={sourceLabel} subtype={subType}")
0071                             self.logger.info(f"importing {moduleName}")
0072                             mod = __import__(moduleName)
0073                             for subModuleName in moduleName.split(".")[1:]:
0074                                 mod = getattr(mod, subModuleName)
0075                             # get class
0076                             self.logger.info(f"getting class {className}")
0077                             cls = getattr(mod, className)
0078                             # instantiate
0079                             self.logger.info("instantiating")
0080                             impl = cls(*args)
0081                             # set vo
0082                             impl.vo = vo
0083                             impl.prodSourceLabel = sourceLabel
0084                             # append
0085                             if vo not in self.implMap:
0086                                 self.implMap[vo] = {}
0087                                 self.classMap[vo] = {}
0088                             if sourceLabel not in self.implMap[vo]:
0089                                 self.implMap[vo][sourceLabel] = {}
0090                                 self.classMap[vo][sourceLabel] = {}
0091                             self.implMap[vo][sourceLabel][subType] = impl
0092                             self.classMap[vo][sourceLabel][subType] = cls
0093                             self.logger.info(f"{cls} is ready for {vo}:{sourceLabel}:{subType}")
0094                         except Exception:
0095                             errtype, errvalue = sys.exc_info()[:2]
0096                             self.logger.error(
0097                                 "failed to import {mn}.{cn} for vo={vo} label={lb} subtype={st} due to {et} {ev}".format(
0098                                     et=errtype.__name__, ev=errvalue, st=subType, vo=vo, lb=sourceLabel, cn=className, mn=moduleName
0099                                 )
0100                             )
0101                             raise ImportError(f"failed to import {moduleName}.{className}")
0102         # return
0103         return True
0104 
0105     # get implementation for vo and sourceLabel. Only work with initializeMods()
0106     def getImpl(self, vo, sourceLabel, subType="any", doRefresh=True):
0107         # check VO
0108         if vo in self.implMap:
0109             # match VO
0110             voImplMap = self.implMap[vo]
0111         elif "any" in self.implMap:
0112             # catch all
0113             voImplMap = self.implMap["any"]
0114         else:
0115             return None
0116         # check sourceLabel
0117         if sourceLabel in voImplMap:
0118             # match sourceLabel
0119             srcImplMap = voImplMap[sourceLabel]
0120         elif "any" in voImplMap:
0121             # catch all
0122             srcImplMap = voImplMap["any"]
0123         else:
0124             return None
0125         # check subType
0126         if subType in srcImplMap:
0127             # match subType
0128             tmpImpl = srcImplMap[subType]
0129             if doRefresh:
0130                 tmpImpl.refresh()
0131             return tmpImpl
0132         elif "any" in srcImplMap:
0133             # catch all
0134             tmpImpl = srcImplMap["any"]
0135             if doRefresh:
0136                 tmpImpl.refresh()
0137             return tmpImpl
0138         else:
0139             return None
0140 
0141     # instantiate implementation for vo and sourceLabel. Only work with initializeMods()
0142     def instantiateImpl(self, vo, sourceLabel, subType, *args):
0143         # check VO
0144         if vo in self.classMap:
0145             # match VO
0146             voImplMap = self.classMap[vo]
0147         elif "any" in self.classMap:
0148             # catch all
0149             voImplMap = self.classMap["any"]
0150         else:
0151             return None
0152         # check sourceLabel
0153         if sourceLabel in voImplMap:
0154             # match sourceLabel
0155             srcImplMap = voImplMap[sourceLabel]
0156         elif "any" in voImplMap:
0157             # catch all
0158             srcImplMap = voImplMap["any"]
0159         else:
0160             return None
0161         # check subType
0162         if subType in srcImplMap:
0163             # match subType
0164             impl = srcImplMap[subType](*args)
0165             impl.vo = vo
0166             impl.prodSourceLabel = sourceLabel
0167             return impl
0168         elif "any" in srcImplMap:
0169             # catch all
0170             impl = srcImplMap["any"](*args)
0171             impl.vo = vo
0172             impl.prodSourceLabel = sourceLabel
0173             return impl
0174         else:
0175             return None
0176 
0177     # get class name of impl
0178     def getClassName(self, vo=None, sourceLabel=None):
0179         impl = self.getImpl(vo, sourceLabel, doRefresh=False)
0180         if impl is None:
0181             return None
0182         return impl.__class__.__name__