File indexing completed on 2025-03-13 02:31:58
0001 from .SequenceTypes import *
0002 from .Modules import OutputModule, EDProducer, EDFilter, EDAnalyzer, Service, ESProducer, ESSource, _Module
0003 from .Mixins import _Labelable
0004
0005
0006 class ScheduleTaskValidator(object):
0007 def __init__(self):
0008 pass
0009 def enter(self,visitee):
0010 if visitee.isLeaf():
0011 if isinstance(visitee, _Labelable):
0012 if not visitee.hasLabel_():
0013 raise ValueError("A task associated with the Schedule contains a module of type '"+visitee.type_()+"'\nwhich has no assigned label.")
0014 elif isinstance(visitee, Service):
0015 if not visitee._inProcess:
0016 raise ValueError("A task associated with the Schedule contains a service of type '"+visitee.type_()+"'\nwhich is not attached to the process.")
0017 def leave(self,visitee):
0018 pass
0019
0020
0021 class PathValidator(object):
0022 def __init__(self):
0023 self.__label = ''
0024 def setLabel(self,label:str):
0025 self.__label = "'"+label+"' "
0026 def enter(self,visitee):
0027 if isinstance(visitee,OutputModule):
0028 raise ValueError("Path "+self.__label+"cannot contain an OutputModule, '"+visitee.type_()+"', with label '"+visitee.label_()+"'")
0029 if visitee.isLeaf():
0030 if isinstance(visitee, _Labelable):
0031 if not visitee.hasLabel_():
0032 raise ValueError("Path "+self.__label+"contains a module of type '"+visitee.type_()+"' which has no assigned label.")
0033 elif isinstance(visitee, Service):
0034 if not visitee._inProcess:
0035 raise ValueError("Path "+self.__label+"contains a service of type '"+visitee.type_()+"' which is not attached to the process.\n")
0036 def leave(self,visitee):
0037 pass
0038
0039
0040 class EndPathValidator(object):
0041 _presetFilters = ["TriggerResultsFilter", "HLTPrescaler"]
0042 def __init__(self):
0043 self.filtersOnEndpaths = []
0044 self.__label = ''
0045 self._levelInTasks = 0
0046 def setLabel(self,label:str):
0047 self.__label = "'"+label+"' "
0048 def enter(self,visitee):
0049 if visitee.isLeaf():
0050 if isinstance(visitee, _Labelable):
0051 if not visitee.hasLabel_():
0052 raise ValueError("EndPath "+self.__label+"contains a module of type '"+visitee.type_()+"' which has\nno assigned label.")
0053 elif isinstance(visitee, Service):
0054 if not visitee._inProcess:
0055 raise ValueError("EndPath "+self.__label+"contains a service of type '"+visitee.type_()+"' which is not attached to the process.\n")
0056 if isinstance(visitee, Task):
0057 self._levelInTasks += 1
0058 if self._levelInTasks > 0:
0059 return
0060 if isinstance(visitee,EDFilter):
0061 if (visitee.type_() in self._presetFilters):
0062 if (visitee.type_() not in self.filtersOnEndpaths):
0063 self.filtersOnEndpaths.append(visitee.type_())
0064 def leave(self,visitee):
0065 if self._levelInTasks > 0:
0066 if isinstance(visitee, Task):
0067 self._levelInTasks -= 1
0068
0069 class NodeVisitor(object):
0070 """Form sets of all modules, ESProducers, ESSources and Services in visited objects. Can be used
0071 to visit Paths, EndPaths, Sequences or Tasks. Includes in sets objects on sub-Sequences and sub-Tasks"""
0072 def __init__(self):
0073 self.modules = set()
0074 self.esProducers = set()
0075 self.esSources = set()
0076 self.services = set()
0077 def enter(self,visitee):
0078 if visitee.isLeaf():
0079 if isinstance(visitee, _Module):
0080 self.modules.add(visitee)
0081 elif isinstance(visitee, ESProducer):
0082 self.esProducers.add(visitee)
0083 elif isinstance(visitee, ESSource):
0084 self.esSources.add(visitee)
0085 elif isinstance(visitee, Service):
0086 self.services.add(visitee)
0087 def leave(self,visitee):
0088 pass
0089
0090 class CompositeVisitor(object):
0091 """ Combines 3 different visitor classes in 1 so we only have to visit all the paths and endpaths once"""
0092 def __init__(self, validator, node, decorated, optional=None):
0093 self._validator = validator
0094 self._node = node
0095 self._decorated = decorated
0096 self._optional = optional
0097 def enter(self, visitee):
0098 self._validator.enter(visitee)
0099 self._node.enter(visitee)
0100 self._decorated.enter(visitee)
0101 if self._optional:
0102 self._optional.enter(visitee)
0103 def leave(self, visitee):
0104 self._validator.leave(visitee)
0105
0106
0107 self._decorated.leave(visitee)
0108 if self._optional:
0109 self._optional.leave(visitee)
0110
0111 class ModuleNamesFromGlobalsVisitor(object):
0112 """Fill a list with the names of Event module types in a sequence. The names are determined
0113 by using globals() to lookup the variable names assigned to the modules. This
0114 allows the determination of the labels before the modules have been attached to a Process."""
0115 def __init__(self,globals_,l):
0116 self._moduleToName = { v[1]:v[0] for v in globals_.items() if isinstance(v[1],_Module) }
0117 self._names =l
0118 def enter(self,node):
0119 if isinstance(node,_Module):
0120 self._names.append(self._moduleToName[node])
0121 def leave(self,node):
0122 return
0123
0124 if __name__=="__main__":
0125 import unittest
0126 class TestModuleCommand(unittest.TestCase):
0127 def setUp(self):
0128 """Nothing to do """
0129 pass
0130 def testValidators(self):
0131 producer = EDProducer("Producer")
0132 analyzer = EDAnalyzer("Analyzer")
0133 output = OutputModule("Out")
0134 filter = EDFilter("Filter")
0135 unlabeled = EDAnalyzer("UnLabeled")
0136 producer.setLabel("producer")
0137 analyzer.setLabel("analyzer")
0138 output.setLabel("output")
0139 filter.setLabel("filter")
0140 s1 = Sequence(analyzer*producer)
0141 s2 = Sequence(output+filter)
0142 p1 = Path(s1)
0143 p2 = Path(s1*s2)
0144 p3 = Path(s1+unlabeled)
0145 ep1 = EndPath(producer+output+analyzer)
0146 ep2 = EndPath(filter+output)
0147 ep3 = EndPath(s2)
0148 ep4 = EndPath(unlabeled)
0149 pathValidator = PathValidator()
0150 endpathValidator = EndPathValidator()
0151 p1.visit(pathValidator)
0152 self.assertRaises(ValueError, p2.visit, pathValidator)
0153 self.assertRaises(ValueError, p3.visit, pathValidator)
0154 ep1.visit(endpathValidator)
0155 ep2.visit(endpathValidator)
0156 ep3.visit(endpathValidator)
0157 self.assertRaises(ValueError, ep4.visit, endpathValidator)
0158
0159 unittest.main()
0160