CMS 3D CMS Logo

normFileParser.py
Go to the documentation of this file.
1 import ConfigParser,os.path
2 
3 #################################
4 #norm file format spec
5 #lines beginning with a semicolon ';' a pound sign '#' or the letters 'REM' (uppercase or lowercase) will be ignored.
6 #section uppercase
7 # [NORMDEFINITION] #section required only if first create
8 # name=HFtest #priority to commandline --name option if present
9 # comment=
10 # lumitype=
11 # istypedefault=
12 # [NORMDATA Since] # section required
13 # since=
14 # corrector=
15 # norm_occ1=
16 # norm_occ2=
17 # amodetag=
18 # egev=
19 # comment=
20 # ...
21 #################################
22 
24  def __init__(self,filename):
25  self.__parser=ConfigParser.ConfigParser()
26  self.__inifilename=filename
27  self.__defsectionname='NormDefinition'
28  self.__datasectionname='NormData'
29  def parse(self):
30  '''
31  output:
32  [ {defoption:value},[{dataoption:value}] ]
33  '''
34  if not os.path.exists(self.__inifilename) or not os.path.isfile(self.__inifilename):
35  raise ValueError(self.__inifilename+' is not a file or does not exist')
36  self.__parser.read(self.__inifilename)
37  result=[]
38  defsectionResult={}
39  datasectionResult=[]
40  sections=self.__parser.sections()
41  for section in sections:
42  thisectionresult={}
43  options=self.__parser.options(section)
44  for o in options:
45  try:
46  thisectionresult[o]=self.__parser.get(section,o)
47  except:
48  continue
49  if section==self.__defsectionname:
50  defsectionResult=thisectionresult
51  elif self.__datasectionname in section:
52  datasectionResult.append(thisectionresult)
53  return [defsectionResult,datasectionResult]
54 
55 if __name__ == "__main__":
56  s='../test/norm_HFV2.cfg'
57  parser=normFileParser(s)
58  print parser.parse()
59 
def __init__(self, filename)
norm file format spec lines beginning with a semicolon ';' a pound sign '#' or the letters 'REM' (upp...