Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:34:07

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 *
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(ConfigParser):
0014 
0015     ## The constructor.
0016     def __init__(self):
0017         ConfigParser.__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,'wb') as outfile:
0041             outfile.write('\n'.join(outlines) + '\n')
0042     ## Helper function to retrieve crab config output lines for one section
0043     # @type self: CrabConfigParser
0044     # @param self:The object pointer.
0045     # @type section: string
0046     # @param section:The section name.
0047     # @rtype: list of strings
0048     # @return: Lines for one section in crab3 config file
0049     def getSectionLines(self,section):
0050         sectionLines = []
0051         sectionLines.append('\nconfig.section_("%s")'%section)
0052         configItems =  self.items(section)
0053         for configItem in configItems:
0054             if not isinstance(configItem[1], str):
0055                 sectionLines.append('config.%s.%s = %s'%(section,configItem[0],configItem[1]))
0056             elif "True" in configItem[1] or "False" in configItem[1]:
0057                 sectionLines.append('config.%s.%s = %s'%(section,configItem[0],configItem[1]))
0058             else:
0059                 parsed = False
0060                 if configItem[0]=="runRange" :
0061                     sectionLines.append('config.%s.%s = \'%s\''%(section,configItem[0],configItem[1]))
0062                     parsed = True
0063                 if not parsed:
0064                     try:
0065                         sectionLines.append('config.%s.%s = %d'%(section,configItem[0],int(configItem[1])))
0066                         parsed = True
0067                     except:
0068                         pass
0069                 if not parsed:
0070                     try:
0071                         sectionLines.append('config.%s.%s = %.2f'%(section,configItem[0],float(configItem[1])))
0072                         parsed = True
0073                     except:
0074                         pass
0075                 if not parsed:
0076                     if isinstance(configItem[1], list):
0077                         sectionLines.append('config.%s.%s = %s'%(section,configItem[0],str(configItem[1])))
0078                         parsed = True
0079                 if not parsed:
0080                     sectionLines.append('config.%s.%s = \'%s\''%(section,configItem[0],configItem[1]))
0081 
0082         return sectionLines