Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-04-08 00:35:18

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