Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
## @package CrabConfigParser
# This module extends the python configparser to create crab3 config files
#
# This module extends the python configparser to create crab3 config files.


from configparser import RawConfigParser

## The CrabConfigParser class
#
# This class extends the python ConfigParser class and adds functions to
# output crab3 config files
class CrabConfigParser(RawConfigParser):

    ## The constructor.
    def __init__(self):
        RawConfigParser.__init__(self)
        self.optionxform = str
    ## Write CrabConfigParser object to file
    # @type self: CrabConfigParser
    # @param self: The object pointer.
    # @type filename: string
    # @param filename The name of the output crab3 config file.
    def writeCrabConfig(self,filename):
        sections = self.sections()
        fixedsections = ['General','JobType','Data','Site','User','Debug']
        outlines = []
        # Add inital header for crab config file
        outlines.append('from WMCore.Configuration import Configuration \n')
        outlines.append('config = Configuration()')
        # we will first add the main crab3 config sections in the given order
        for fixedsection in fixedsections:
            if fixedsection in sections:
                outlines.extend(self.getSectionLines(fixedsection))
                sections.remove(fixedsection)
        # add additional sections (may be added in future crab3 versions ?)
        for section in sections:
            outlines.extend(self.getSectionLines(section))
        #print filename
        with open(filename,'w') as outfile:
            for line in outlines:
                outfile.write(f"{line}\n")
    ## Helper function to retrieve crab config output lines for one section
    # @type self: CrabConfigParser
    # @param self:The object pointer.
    # @type section: string
    # @param section:The section name.
    # @rtype: list of strings
    # @return: Lines for one section in crab3 config file
    def getSectionLines(self,section):
        sectionLines = []
        sectionLines.append('\nconfig.section_("%s")'%section)
        configItems =  self.items(section)
        for configItem in configItems:
            if not isinstance(configItem[1], str):
                sectionLines.append('config.%s.%s = %s'%(section,configItem[0],configItem[1]))
            elif "True" in configItem[1] or "False" in configItem[1]:
                sectionLines.append('config.%s.%s = %s'%(section,configItem[0],configItem[1]))
            else:
                parsed = False
                if configItem[0]=="runRange" :
                    sectionLines.append('config.%s.%s = \'%s\''%(section,configItem[0],configItem[1]))
                    parsed = True
                if not parsed:
                    try:
                        sectionLines.append('config.%s.%s = %d'%(section,configItem[0],int(configItem[1])))
                        parsed = True
                    except:
                        pass
                if not parsed:
                    try:
                        sectionLines.append('config.%s.%s = %.2f'%(section,configItem[0],float(configItem[1])))
                        parsed = True
                    except:
                        pass
                if not parsed:
                    if isinstance(configItem[1], list):
                        sectionLines.append('config.%s.%s = %s'%(section,configItem[0],str(configItem[1])))
                        parsed = True
                if not parsed:
                    sectionLines.append('config.%s.%s = \'%s\''%(section,configItem[0],configItem[1]))

        return sectionLines