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