CMS 3D CMS Logo

crabConfigParser.py
Go to the documentation of this file.
1 
5 
6 
7 from configparser import RawConfigParser
8 
9 
13 class CrabConfigParser(RawConfigParser):
14 
15 
16  def __init__(self):
17  RawConfigParser.__init__(self)
18  self.optionxform = str
19 
24  def writeCrabConfig(self,filename):
25  sections = self.sections()
26  fixedsections = ['General','JobType','Data','Site','User','Debug']
27  outlines = []
28  # Add inital header for crab config file
29  outlines.append('from WMCore.Configuration import Configuration \n')
30  outlines.append('config = Configuration()')
31  # we will first add the main crab3 config sections in the given order
32  for fixedsection in fixedsections:
33  if fixedsection in sections:
34  outlines.extend(self.getSectionLines(fixedsection))
35  sections.remove(fixedsection)
36  # add additional sections (may be added in future crab3 versions ?)
37  for section in sections:
38  outlines.extend(self.getSectionLines(section))
39  #print filename
40  with open(filename,'w') as outfile:
41  for line in outlines:
42  outfile.write(f"{line}\n")
43 
50  def getSectionLines(self,section):
51  sectionLines = []
52  sectionLines.append('\nconfig.section_("%s")'%section)
53  configItems = self.items(section)
54  for configItem in configItems:
55  if not isinstance(configItem[1], str):
56  sectionLines.append('config.%s.%s = %s'%(section,configItem[0],configItem[1]))
57  elif "True" in configItem[1] or "False" in configItem[1]:
58  sectionLines.append('config.%s.%s = %s'%(section,configItem[0],configItem[1]))
59  else:
60  parsed = False
61  if configItem[0]=="runRange" :
62  sectionLines.append('config.%s.%s = \'%s\''%(section,configItem[0],configItem[1]))
63  parsed = True
64  if not parsed:
65  try:
66  sectionLines.append('config.%s.%s = %d'%(section,configItem[0],int(configItem[1])))
67  parsed = True
68  except:
69  pass
70  if not parsed:
71  try:
72  sectionLines.append('config.%s.%s = %.2f'%(section,configItem[0],float(configItem[1])))
73  parsed = True
74  except:
75  pass
76  if not parsed:
77  if isinstance(configItem[1], list):
78  sectionLines.append('config.%s.%s = %s'%(section,configItem[0],str(configItem[1])))
79  parsed = True
80  if not parsed:
81  sectionLines.append('config.%s.%s = \'%s\''%(section,configItem[0],configItem[1]))
82 
83  return sectionLines
def writeCrabConfig(self, filename)
Write CrabConfigParser object to file self: CrabConfigParser.
def getSectionLines(self, section)
Helper function to retrieve crab config output lines for one section self: CrabConfigParser.
def __init__(self)
The constructor.
The CrabConfigParser class.
#define str(s)