The Gaudi Framework  v37r1 (a7f61348)
GenerateGaudiOpts.py
Go to the documentation of this file.
1 
11 from __future__ import print_function
12 
13 from xml.etree import ElementTree
14 
15 
17  counter, cmask, invmask, sampling_period, startatevent, storeresultsat, family
18 ):
19  cmask = map(int, cmask)
20  invmask = map(int, invmask)
21  sampling_period = map(int, sampling_period)
22  startatevent = int(startatevent)
23 
24  from Configurables import ApplicationMgr, AuditorSvc, PerfMonAuditor
25 
26  app = ApplicationMgr()
27  app.AuditAlgorithms = 1
28  pfaud = PerfMonAuditor()
29  try:
30  pfaud.EVENT0 = counter[0]
31  pfaud.SP0 = sampling_period[0]
32  pfaud.INV0 = int(invmask[0])
33  pfaud.CMASK0 = int(cmask[0])
34  pfaud.EVENT1 = counter[1]
35  pfaud.SP1 = sampling_period[1]
36  pfaud.INV1 = int(invmask[1])
37  pfaud.CMASK1 = int(cmask[1])
38  pfaud.EVENT2 = counter[2]
39  pfaud.SP2 = sampling_period[2]
40  pfaud.INV2 = int(invmask[2])
41  pfaud.CMASK2 = int(cmask[2])
42  pfaud.EVENT3 = counter[3]
43  pfaud.SP3 = sampling_period[3]
44  pfaud.INV3 = int(invmask[3])
45  pfaud.CMASK3 = int(cmask[3])
46  except IndexError:
47  pass
48  pfaud.FAMILY = family
49  # for <2.5 Python use: test and true_value or false_value
50  pfaud.PREFIX = "%s_%s" % (storeresultsat, "S" if sampling_period[0] > 0 else "C")
51  pfaud.SAMPLE = int(sampling_period[0] > 0)
52  pfaud.START_AT_EVENT = startatevent
53  # pfaud.LEVEL = 5
54  AuditorSvc().Auditors.append(pfaud)
55  print(pfaud)
56 
57 
58 class XmlDictObject(dict):
59  """
60  Adds object like functionality to the standard dictionary.
61  """
62 
63  def __init__(self, initdict=None):
64  if initdict is None:
65  initdict = {}
66  dict.__init__(self, initdict)
67 
68  def __getattr__(self, item):
69  return self.__getitem__(item)
70 
71  def __setattr__(self, item, value):
72  self.__setitem__(item, value)
73 
74  def __str__(self):
75  if "_text" in self:
76  return self.__getitem__("_text")
77  else:
78  return ""
79 
80  @staticmethod
81  def Wrap(x):
82  """
83  Static method to wrap a dictionary recursively as an XmlDictObject
84  """
85 
86  if isinstance(x, dict):
87  return XmlDictObject((k, XmlDictObject.Wrap(v)) for (k, v) in x.iteritems())
88  elif isinstance(x, list):
89  return [XmlDictObject.Wrap(v) for v in x]
90  else:
91  return x
92 
93  @staticmethod
94  def _UnWrap(x):
95  if isinstance(x, dict):
96  return dict((k, XmlDictObject._UnWrap(v)) for (k, v) in x.iteritems())
97  elif isinstance(x, list):
98  return [XmlDictObject._UnWrap(v) for v in x]
99  else:
100  return x
101 
102  def UnWrap(self):
103  """
104  Recursively converts an XmlDictObject to a standard dictionary and returns the result.
105  """
106 
107  return XmlDictObject._UnWrap(self)
108 
109 
110 def _ConvertDictToXmlRecurse(parent, dictitem):
111  assert not isinstance(dictitem, list)
112 
113  if isinstance(dictitem, dict):
114  for tag, child in dictitem.iteritems():
115  if str(tag) == "_text":
116  parent.text = str(child)
117  elif isinstance(child, list):
118  # iterate through the array and convert
119  for listchild in child:
120  elem = ElementTree.Element(tag)
121  parent.append(elem)
122  _ConvertDictToXmlRecurse(elem, listchild)
123  else:
124  elem = ElementTree.Element(tag)
125  parent.append(elem)
126  _ConvertDictToXmlRecurse(elem, child)
127  else:
128  parent.text = str(dictitem)
129 
130 
131 def ConvertDictToXml(xmldict):
132  """
133  Converts a dictionary to an XML ElementTree Element
134  """
135 
136  roottag = xmldict.keys()[0]
137  root = ElementTree.Element(roottag)
138  _ConvertDictToXmlRecurse(root, xmldict[roottag])
139  return root
140 
141 
142 def _ConvertXmlToDictRecurse(node, dictclass):
143  nodedict = dictclass()
144 
145  if len(node.items()) > 0:
146  # if we have attributes, set them
147  nodedict.update(dict(node.items()))
148 
149  for child in node:
150  # recursively add the element's children
151  newitem = _ConvertXmlToDictRecurse(child, dictclass)
152  if child.tag in nodedict:
153  # found duplicate tag, force a list
154  if isinstance(nodedict[child.tag], list):
155  # append to existing list
156  nodedict[child.tag].append(newitem)
157  else:
158  # convert to list
159  nodedict[child.tag] = [nodedict[child.tag], newitem]
160  else:
161  # only one, directly set the dictionary
162  nodedict[child.tag] = newitem
163 
164  if node.text is None:
165  text = ""
166  else:
167  text = node.text.strip()
168 
169  if len(nodedict) > 0:
170  # if we have a dictionary add the text as a dictionary value (if there is any)
171  if len(text) > 0:
172  nodedict["_text"] = text
173  else:
174  # if we don't have child nodes or attributes, just set the text
175  nodedict = text
176 
177  return nodedict
178 
179 
180 def ConvertXmlToDict(root, dictclass=XmlDictObject):
181  """
182  Converts an XML file or ElementTree Element to a dictionary
183  """
184 
185  # If a string is passed in, try to open it as a file
186  if isinstance(root, str):
187  root = ElementTree.parse(root).getroot()
188  elif not isinstance(root, ElementTree.Element):
189  raise TypeError("Expected ElementTree.Element or file path string")
190 
191  return dictclass({root.tag: _ConvertXmlToDictRecurse(root, dictclass)})
GaudiProfiling.GenerateGaudiOpts.XmlDictObject.__setattr__
def __setattr__(self, item, value)
Definition: GenerateGaudiOpts.py:71
GaudiProfiling.GenerateGaudiOpts.XmlDictObject.UnWrap
def UnWrap(self)
Definition: GenerateGaudiOpts.py:102
GaudiProfiling.GenerateGaudiOpts.XmlDictObject
Definition: GenerateGaudiOpts.py:58
GaudiProfiling.GenerateGaudiOpts._ConvertDictToXmlRecurse
def _ConvertDictToXmlRecurse(parent, dictitem)
Definition: GenerateGaudiOpts.py:110
GaudiProfiling.GenerateGaudiOpts.XmlDictObject.__getattr__
def __getattr__(self, item)
Definition: GenerateGaudiOpts.py:68
Containers::map
struct GAUDI_API map
Parametrisation class for map-like implementation.
Definition: KeyedObjectManager.h:35
GaudiProfiling.GenerateGaudiOpts.ConvertDictToXml
def ConvertDictToXml(xmldict)
Definition: GenerateGaudiOpts.py:131
GaudiProfiling.GenerateGaudiOpts.XmlDictObject._UnWrap
def _UnWrap(x)
Definition: GenerateGaudiOpts.py:94
GaudiProfiling.GenerateGaudiOpts.generateOptions
def generateOptions(counter, cmask, invmask, sampling_period, startatevent, storeresultsat, family)
Definition: GenerateGaudiOpts.py:16
GaudiProfiling.GenerateGaudiOpts.XmlDictObject.__init__
def __init__(self, initdict=None)
Definition: GenerateGaudiOpts.py:63
PerfMonAuditor
Definition: PerfMonAuditor.cpp:208
AuditorSvc
Definition: AuditorSvc.h:28
GaudiProfiling.GenerateGaudiOpts._ConvertXmlToDictRecurse
def _ConvertXmlToDictRecurse(node, dictclass)
Definition: GenerateGaudiOpts.py:142
GaudiProfiling.GenerateGaudiOpts.ConvertXmlToDict
def ConvertXmlToDict(root, dictclass=XmlDictObject)
Definition: GenerateGaudiOpts.py:180
ApplicationMgr
Definition: ApplicationMgr.h:57
GaudiProfiling.GenerateGaudiOpts.XmlDictObject.__str__
def __str__(self)
Definition: GenerateGaudiOpts.py:74
GaudiProfiling.GenerateGaudiOpts.XmlDictObject.Wrap
def Wrap(x)
Definition: GenerateGaudiOpts.py:81