CMS 3D CMS Logo

SequenceVisitors.py
Go to the documentation of this file.
1 from __future__ import absolute_import
2 from .SequenceTypes import *
3 from .Modules import OutputModule, EDProducer, EDFilter, EDAnalyzer, Service, ESProducer, ESSource, _Module
4 from .Mixins import _Labelable
5 import six
6 
7 # Use this on Tasks in the Schedule
9  def __init__(self):
10  pass
11  def enter(self,visitee):
12  if visitee.isLeaf():
13  if isinstance(visitee, _Labelable):
14  if not visitee.hasLabel_():
15  raise ValueError("A task associated with the Schedule contains a module of type '"+visitee.type_()+"'\nwhich has no assigned label.")
16  elif isinstance(visitee, Service):
17  if not visitee._inProcess:
18  raise ValueError("A task associated with the Schedule contains a service of type '"+visitee.type_()+"'\nwhich is not attached to the process.")
19  def leave(self,visitee):
20  pass
21 
22 # Use this on Paths
23 class PathValidator(object):
24  def __init__(self):
25  self.__label = ''
26  def setLabel(self,label):
27  self.__label = "'"+label+"' "
28  def enter(self,visitee):
29  if isinstance(visitee,OutputModule):
30  raise ValueError("Path "+self.__label+"cannot contain an OutputModule, '"+visitee.type_()+"', with label '"+visitee.label_()+"'")
31  if visitee.isLeaf():
32  if isinstance(visitee, _Labelable):
33  if not visitee.hasLabel_():
34  raise ValueError("Path "+self.__label+"contains a module of type '"+visitee.type_()+"' which has no assigned label.")
35  elif isinstance(visitee, Service):
36  if not visitee._inProcess:
37  raise ValueError("Path "+self.__label+"contains a service of type '"+visitee.type_()+"' which is not attached to the process.\n")
38  def leave(self,visitee):
39  pass
40 
41 # Use this on EndPaths
42 class EndPathValidator(object):
43  _presetFilters = ["TriggerResultsFilter", "HLTPrescaler"]
44  def __init__(self):
46  self.__label = ''
47  self._levelInTasks = 0
48  def setLabel(self,label):
49  self.__label = "'"+label+"' "
50  def enter(self,visitee):
51  if visitee.isLeaf():
52  if isinstance(visitee, _Labelable):
53  if not visitee.hasLabel_():
54  raise ValueError("EndPath "+self.__label+"contains a module of type '"+visitee.type_()+"' which has\nno assigned label.")
55  elif isinstance(visitee, Service):
56  if not visitee._inProcess:
57  raise ValueError("EndPath "+self.__label+"contains a service of type '"+visitee.type_()+"' which is not attached to the process.\n")
58  if isinstance(visitee, Task):
59  self._levelInTasks += 1
60  if self._levelInTasks > 0:
61  return
62  if isinstance(visitee,EDFilter):
63  if (visitee.type_() in self._presetFilters):
64  if (visitee.type_() not in self.filtersOnEndpaths):
65  self.filtersOnEndpaths.append(visitee.type_())
66  def leave(self,visitee):
67  if self._levelInTasks > 0:
68  if isinstance(visitee, Task):
69  self._levelInTasks -= 1
70 
72  """Form sets of all modules, ESProducers, ESSources and Services in visited objects. Can be used
73  to visit Paths, EndPaths, Sequences or Tasks. Includes in sets objects on sub-Sequences and sub-Tasks"""
74  def __init__(self):
75  self.modules = set()
76  self.esProducers = set()
77  self.esSources = set()
78  self.services = set()
79  def enter(self,visitee):
80  if visitee.isLeaf():
81  if isinstance(visitee, _Module):
82  self.modules.add(visitee)
83  elif isinstance(visitee, ESProducer):
84  self.esProducers.add(visitee)
85  elif isinstance(visitee, ESSource):
86  self.esSources.add(visitee)
87  elif isinstance(visitee, Service):
88  self.services.add(visitee)
89  def leave(self,visitee):
90  pass
91 
92 class CompositeVisitor(object):
93  """ Combines 3 different visitor classes in 1 so we only have to visit all the paths and endpaths once"""
94  def __init__(self, validator, node, decorated):
95  self._validator = validator
96  self._node = node
97  self._decorated = decorated
98  def enter(self, visitee):
99  self._validator.enter(visitee)
100  self._node.enter(visitee)
101  self._decorated.enter(visitee)
102  def leave(self, visitee):
103  self._validator.leave(visitee)
104  # The node visitor leave function does nothing
105  #self._node.leave(visitee)
106  self._decorated.leave(visitee)
107 
109  """Fill a list with the names of Event module types in a sequence. The names are determined
110  by using globals() to lookup the variable names assigned to the modules. This
111  allows the determination of the labels before the modules have been attached to a Process."""
112  def __init__(self,globals_,l):
113  self._moduleToName = { v[1]:v[0] for v in six.iteritems(globals_) if isinstance(v[1],_Module) }
114  self._names =l
115  def enter(self,node):
116  if isinstance(node,_Module):
117  self._names.append(self._moduleToName[node])
118  def leave(self,node):
119  return
120 
121 if __name__=="__main__":
122  import unittest
123  class TestModuleCommand(unittest.TestCase):
124  def setUp(self):
125  """Nothing to do """
126  pass
127  def testValidators(self):
128  producer = EDProducer("Producer")
129  analyzer = EDAnalyzer("Analyzer")
130  output = OutputModule("Out")
131  filter = EDFilter("Filter")
132  unlabeled = EDAnalyzer("UnLabeled")
133  producer.setLabel("producer")
134  analyzer.setLabel("analyzer")
135  output.setLabel("output")
136  filter.setLabel("filter")
137  s1 = Sequence(analyzer*producer)
138  s2 = Sequence(output+filter)
139  p1 = Path(s1)
140  p2 = Path(s1*s2)
141  p3 = Path(s1+unlabeled)
142  ep1 = EndPath(producer+output+analyzer)
143  ep2 = EndPath(filter+output)
144  ep3 = EndPath(s2)
145  ep4 = EndPath(unlabeled)
146  pathValidator = PathValidator()
147  endpathValidator = EndPathValidator()
148  p1.visit(pathValidator)
149  self.assertRaises(ValueError, p2.visit, pathValidator)
150  self.assertRaises(ValueError, p3.visit, pathValidator)
151  ep1.visit(endpathValidator)
152  ep2.visit(endpathValidator)
153  ep3.visit(endpathValidator)
154  self.assertRaises(ValueError, ep4.visit, endpathValidator)
155 
156  unittest.main()
157 
def __init__(self, validator, node, decorated)