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