Gaudi Framework, version v22r0

Home   Generated: 9 Feb 2011

GenerateGaudiOpts.py

Go to the documentation of this file.
00001 import os, sys, time
00002 from xml.etree import ElementTree
00003 
00004 def generateOptions(counter, cmask, invmask, sampling_period, startatevent, storeresultsat, family):
00005     cmask = map(int, cmask)
00006     invmask = map(int, invmask)
00007     sampling_period = map(int, sampling_period)
00008     startatevent = int(startatevent)
00009 
00010     from Configurables import ApplicationMgr, AuditorSvc, PerfMonAuditor
00011     app = ApplicationMgr()
00012     app.AuditAlgorithms = 1
00013     pfaud = PerfMonAuditor()
00014     try:
00015         pfaud.EVENT0 = counter[0]
00016         pfaud.SP0 = sampling_period[0]
00017         pfaud.INV0 = int(invmask[0])
00018         pfaud.CMASK0 = int(cmask[0])
00019         pfaud.EVENT1 = counter[1]
00020         pfaud.SP1 = sampling_period[1]
00021         pfaud.INV1 = int(invmask[1])
00022         pfaud.CMASK1 = int(cmask[1])
00023         pfaud.EVENT2 = counter[2]
00024         pfaud.SP2 = sampling_period[2]
00025         pfaud.INV2 = int(invmask[2])
00026         pfaud.CMASK2 = int(cmask[2])
00027         pfaud.EVENT3 = counter[3]
00028         pfaud.SP3 = sampling_period[3]
00029         pfaud.INV3 = int(invmask[3])
00030         pfaud.CMASK3 = int(cmask[3])
00031     except IndexError:
00032         pass
00033     pfaud.FAMILY = family
00034     pfaud.PREFIX = "%s_%s" % (storeresultsat, "S" if sampling_period[0] > 0 else "C") # for <2.5 Python use: test and true_value or false_value
00035     pfaud.SAMPLE = int(sampling_period[0] > 0)
00036     pfaud.START_AT_EVENT = startatevent
00037     #pfaud.LEVEL = 5
00038     AuditorSvc().Auditors.append(pfaud)
00039     print pfaud
00040 
00041 
00042 class XmlDictObject(dict):
00043     """
00044     Adds object like functionality to the standard dictionary.
00045     """
00046 
00047     def __init__(self, initdict=None):
00048         if initdict is None:
00049             initdict = {}
00050         dict.__init__(self, initdict)
00051     
00052     def __getattr__(self, item):
00053         return self.__getitem__(item)
00054     
00055     def __setattr__(self, item, value):
00056         self.__setitem__(item, value)
00057     
00058     def __str__(self):
00059         if self.has_key('_text'):
00060             return self.__getitem__('_text')
00061         else:
00062             return ''
00063 
00064     @staticmethod
00065     def Wrap(x):
00066         """
00067         Static method to wrap a dictionary recursively as an XmlDictObject
00068         """
00069 
00070         if isinstance(x, dict):
00071             return XmlDictObject((k, XmlDictObject.Wrap(v)) for (k, v) in x.iteritems())
00072         elif isinstance(x, list):
00073             return [XmlDictObject.Wrap(v) for v in x]
00074         else:
00075             return x
00076 
00077     @staticmethod
00078     def _UnWrap(x):
00079         if isinstance(x, dict):
00080             return dict((k, XmlDictObject._UnWrap(v)) for (k, v) in x.iteritems())
00081         elif isinstance(x, list):
00082             return [XmlDictObject._UnWrap(v) for v in x]
00083         else:
00084             return x
00085         
00086     def UnWrap(self):
00087         """
00088         Recursively converts an XmlDictObject to a standard dictionary and returns the result.
00089         """
00090 
00091         return XmlDictObject._UnWrap(self)
00092 
00093 def _ConvertDictToXmlRecurse(parent, dictitem):
00094     assert type(dictitem) is not type([])
00095 
00096     if isinstance(dictitem, dict):
00097         for (tag, child) in dictitem.iteritems():
00098             if str(tag) == '_text':
00099                 parent.text = str(child)
00100             elif type(child) is type([]):
00101                 # iterate through the array and convert
00102                 for listchild in child:
00103                     elem = ElementTree.Element(tag)
00104                     parent.append(elem)
00105                     _ConvertDictToXmlRecurse(elem, listchild)
00106             else:                
00107                 elem = ElementTree.Element(tag)
00108                 parent.append(elem)
00109                 _ConvertDictToXmlRecurse(elem, child)
00110     else:
00111         parent.text = str(dictitem)
00112     
00113 def ConvertDictToXml(xmldict):
00114     """
00115     Converts a dictionary to an XML ElementTree Element 
00116     """
00117 
00118     roottag = xmldict.keys()[0]
00119     root = ElementTree.Element(roottag)
00120     _ConvertDictToXmlRecurse(root, xmldict[roottag])
00121     return root
00122 
00123 def _ConvertXmlToDictRecurse(node, dictclass):
00124     nodedict = dictclass()
00125     
00126     if len(node.items()) > 0:
00127         # if we have attributes, set them
00128         nodedict.update(dict(node.items()))
00129     
00130     for child in node:
00131         # recursively add the element's children
00132         newitem = _ConvertXmlToDictRecurse(child, dictclass)
00133         if nodedict.has_key(child.tag):
00134             # found duplicate tag, force a list
00135             if type(nodedict[child.tag]) is type([]):
00136                 # append to existing list
00137                 nodedict[child.tag].append(newitem)
00138             else:
00139                 # convert to list
00140                 nodedict[child.tag] = [nodedict[child.tag], newitem]
00141         else:
00142             # only one, directly set the dictionary
00143             nodedict[child.tag] = newitem
00144 
00145     if node.text is None: 
00146         text = ''
00147     else: 
00148         text = node.text.strip()
00149     
00150     if len(nodedict) > 0:            
00151         # if we have a dictionary add the text as a dictionary value (if there is any)
00152         if len(text) > 0:
00153             nodedict['_text'] = text
00154     else:
00155         # if we don't have child nodes or attributes, just set the text
00156         nodedict = text
00157         
00158     return nodedict
00159         
00160 def ConvertXmlToDict(root, dictclass=XmlDictObject):
00161     """
00162     Converts an XML file or ElementTree Element to a dictionary
00163     """
00164 
00165     # If a string is passed in, try to open it as a file
00166     if type(root) == type(''):
00167         root = ElementTree.parse(root).getroot()
00168     elif not isinstance(root, ElementTree.Element):
00169         raise TypeError, 'Expected ElementTree.Element or file path string'
00170 
00171     return dictclass({root.tag: _ConvertXmlToDictRecurse(root, dictclass)})
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Wed Feb 9 16:24:58 2011 for Gaudi Framework, version v22r0 by Doxygen version 1.6.2 written by Dimitri van Heesch, © 1997-2004