CMS 3D CMS Logo

common.py
Go to the documentation of this file.
1 import itertools
2 
3 import FWCore.ParameterSet.Config as cms
4 import six
5 
6 def producers_by_type(process, *types):
7  "Find all EDProducers in the Process that are instances of the given C++ type."
8  return (module for module in process._Process__producers.values() if module._TypedParameterizable__type in types)
9 
10 def filters_by_type(process, *types):
11  "Find all EDFilters in the Process that are instances of the given C++ type."
12  return (filter for filter in process._Process__filters.values() if filter._TypedParameterizable__type in types)
13 
14 def analyzers_by_type(process, *types):
15  "Find all EDAnalyzers in the Process that are instances of the given C++ type."
16  return (analyzer for analyzer in process._Process__analyzers.values() if analyzer._TypedParameterizable__type in types)
17 
18 def esproducers_by_type(process, *types):
19  "Find all ESProducers in the Process that are instances of the given C++ type."
20  return (module for module in process._Process__esproducers.values() if module._TypedParameterizable__type in types)
21 
22 
23 def insert_modules_before(process, target, *modules):
24  "Add the `modules` before the `target` in any Sequence, Paths or EndPath that contains the latter."
25  for sequence in itertools.chain(
26  six.itervalues(process._Process__sequences),
27  six.itervalues(process._Process__paths),
28  six.itervalues(process._Process__endpaths)
29  ):
30  try:
31  position = sequence.index(target)
32  except ValueError:
33  continue
34  else:
35  for module in reversed(modules):
36  sequence.insert(position, module)
37 
38 
39 def insert_modules_after(process, target, *modules):
40  "Add the `modules` after the `target` in any Sequence, Paths or EndPath that contains the latter."
41  for sequence in itertools.chain(
42  six.itervalues(process._Process__sequences),
43  six.itervalues(process._Process__paths),
44  six.itervalues(process._Process__endpaths)
45  ):
46  try:
47  position = sequence.index(target)
48  except ValueError:
49  continue
50  else:
51  for module in reversed(modules):
52  sequence.insert(position+1, module)
53 
54 
55 # logic from Modifier.toModify from FWCore/ParameterSet/python/Config.py
56 def replace_with(fromObj, toObj):
57  """Replace one object with a different one of the same type.
58 
59  This function replaces the contents of `fromObj` object with those of `toObj`,
60  so all references ot it remain valid.
61  """
62 
63  if not isinstance(toObj, type(fromObj)):
64  raise TypeError('replaceWith requires both arguments to be the same type')
65 
66  if isinstance(toObj, cms._ModuleSequenceType):
67  fromObj._seq = toObj._seq
68 
69  elif isinstance(toObj, cms._Parameterizable):
70  # delete the old items, in case `toObj` is not a complete superset of `fromObj`
71  for p in fromObj.parameterNames_():
72  delattr(fromObj, p)
73  for p in toObj.parameterNames_():
74  setattr(fromObj, p, getattr(toObj, p))
75  if isinstance(toObj, cms._TypedParameterizable):
76  fromObj._TypedParameterizable__type = toObj._TypedParameterizable__type
77 
78  else:
79  raise TypeError('replaceWith does not work with "%s" objects' % str(type(fromObj)))
80 
def replace_with(fromObj, toObj)
Definition: common.py:56
def filters_by_type(process, types)
Definition: common.py:10
def insert_modules_before(process, target, modules)
Definition: common.py:23
def producers_by_type(process, types)
Definition: common.py:6
def insert_modules_after(process, target, modules)
Definition: common.py:39
#define str(s)
def analyzers_by_type(process, types)
Definition: common.py:14
def esproducers_by_type(process, types)
Definition: common.py:18