Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:28

0001 ## @package CrabConfigParser
0002 # This module extends the python configparser to create crab3 config files
0003 #
0004 # This module extends the python configparser to create crab3 config files.
0005 
0006 
0007 from configparser import RawConfigParser
0008 
0009 ## The CrabConfigParser class
0010 #
0011 # This class extends the python ConfigParser class and adds functions to
0012 # output crab3 config files
0013 class CrabConfigParser(RawConfigParser):
0014 
0015     ## The constructor.
0016     def __init__(self):
0017         RawConfigParser.__init__(self)
0018         self.optionxform = str
0019     ## Write CrabConfigParser object to file
0020     # @type self: CrabConfigParser
0021     # @param self: The object pointer.
0022     # @type filename: string
0023     # @param filename The name of the output crab3 config file.
0024     def writeCrabConfig(self,filename):
0025         sections = self.sections()
0026         fixedsections = ['General','JobType','Data','Site','User','Debug']
0027         outlines = []
0028         # Add inital header for crab config file
0029         outlines.append('from WMCore.Configuration import Configuration \n')
0030         outlines.append('config = Configuration()')
0031         # we will first add the main crab3 config sections in the given order
0032         for fixedsection in fixedsections:
0033             if fixedsection in sections:
0034                 outlines.extend(self.getSectionLines(fixedsection))
0035                 sections.remove(fixedsection)
0036         # add additional sections (may be added in future crab3 versions ?)
0037         for section in sections:
0038             outlines.extend(self.getSectionLines(section))
0039         #print filename
0040         with open(filename,'w') as outfile:
0041             for line in outlines:
0042                 outfile.write(f"{line}\n")
0043     ## Helper function to retrieve crab config output lines for one section
0044     # @type self: CrabConfigParser
0045     # @param self:The object pointer.
0046     # @type section: string
0047     # @param section:The section name.
0048     # @rtype: list of strings
0049     # @return: Lines for one section in crab3 config file
0050     def getSectionLines(self,section):
0051         sectionLines = []
0052         sectionLines.append('\nconfig.section_("%s")'%section)
0053         configItems =  self.items(section)
0054         for configItem in configItems:
0055             if not isinstance(configItem[1], str):
0056                 sectionLines.append('config.%s.%s = %s'%(section,configItem[0],configItem[1]))
0057             elif "True" in configItem[1] or "False" in configItem[1]:
0058                 sectionLines.append('config.%s.%s = %s'%(section,configItem[0],configItem[1]))
0059             else:
0060                 parsed = False
0061                 if configItem[0]=="runRange" :
0062                     sectionLines.append('config.%s.%s = \'%s\''%(section,configItem[0],configItem[1]))
0063                     parsed = True
0064                 if not parsed:
0065                     try:
0066                         sectionLines.append('config.%s.%s = %d'%(section,configItem[0],int(configItem[1])))
0067                         parsed = True
0068                     except:
0069                         pass
0070                 if not parsed:
0071                     try:
0072                         sectionLines.append('config.%s.%s = %.2f'%(section,configItem[0],float(configItem[1])))
0073                         parsed = True
0074                     except:
0075                         pass
0076                 if not parsed:
0077                     if isinstance(configItem[1], list):
0078                         sectionLines.append('config.%s.%s = %s'%(section,configItem[0],str(configItem[1])))
0079                         parsed = True
0080                 if not parsed:
0081                     sectionLines.append('config.%s.%s = \'%s\''%(section,configItem[0],configItem[1]))
0082 
0083         return sectionLines