'Dynamically create CSV files for each section of a config.ini file

I am working on automating status reports using Python and the Jira API with a config.ini file.

My goal is to have a config file where users can supply a jql statement, more so just specifying a filter in Jira that shows the tickets that should be in the report. This along with a column set. The different column sets is to represent different sets of fields.

Example config file:

Here is the code I have so far. I basically have it right now breaking the report into sections based on the column set; general, risk, etc. I think it would be better to have the script create a new csv for each section it identifies in the configuration file; Section 1, Section 2, etc. One of the problems I am running into with how I have it currently is if both sections have the same column set, the first section gets overwritten.

for sectionName in userConfig_file.sections(): ##sectionName = Section 1 or Section 2
    optionName = userConfig_file.options(sectionName)
    #for optionName in userConfig_file.options(sectionName): ## optionName = columnSet or jql
    #    valueName = userConfig_file.get(sectionName, optionName)
    #valueName = userConfig_file.get(sectionName,optionName)
    if (optionName[0].__eq__("columnset")):
        #print(userConfig_file.get(sectionName, optionName[0]))
        #print(userConfig_file.get(sectionName, optionName[2]))
        if (userConfig_file.get(sectionName,optionName[0]).__eq__("general")):
            for i in generalFields:
                generalHeaders.append(generalFields[i])
                with open(generalSection,'w') as f:
                    csvwriter = csv.writer(f)
                    csvwriter.writerow(generalHeaders)
            for results in jira.search_issues(userConfig_file.get(sectionName, optionName[2])):
                generalSectionData = []
                with open(generalSection, 'a') as f:
                    csvwriter = csv.writer(f)
                    generalSectionData.append(results)  # issue key
                    generalSectionData.append(results.fields.status)
                    generalSectionData.append(results.fields.created)
                    generalSectionData.append(results.fields.updated)
                    generalSectionData.append(results.fields.priority)
                    generalSectionData.append(results.fields.summary)
                    generalSectionData.append(results.fields.issuetype)
                    csvwriter.writerow(generalSectionData)
        elif (userConfig_file.get(sectionName,optionName[0]).__eq__("risk")):
            for i in riskFields:
                riskHeaders.append(riskFields[i])
                with open(riskSection, 'w') as f:
                    csvwriter = csv.writer(f)
                    csvwriter.writerow(riskHeaders)
            for results in jira.search_issues(userConfig_file.get(sectionName, optionName[2])):
                riskSectionData = []
                with open(riskSection, 'a') as f:
                    csvwriter = csv.writer(f)
                    riskSectionData.append(results)  # issue key
                    riskSectionData.append(results.fields.status)
                    riskSectionData.append(results.fields.created)
                    riskSectionData.append(results.fields.updated)
                    riskSectionData.append(results.fields.priority)
                    riskSectionData.append(results.fields.summary)
                    riskSectionData.append(results.fields.issuetype)
                    csvwriter.writerow(riskSectionData)```

I wanted to get some ideas on how to have the script dynamically create separate csv files for each section. A user should be able to specify x amount of sections in the config file, example: 6 sections should create 6 csv files instead of being based by the columnset provided in the config.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source