| The Gaudi Framework v29r0 (ff2e7097)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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