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