Roshan r February 2016

How to skip a section in config file using configparser

I have written a code where parameters are taken from a config file. My first parameter in the config is for setting the debug level.

    config = ConfigParser.RawConfigParser()
    config.read('config.cfg')
    log_level = config.get('Logger','log_level' )

There are other sections in the config which gives the server IP and password to scan each sections.

Main Code:

for section in config.sections():
    components = dict() #start with empty dictionary for each section
    env.user = config.get(section, 'server.user_name')
    env.password = config.get(section, 'server.password')
    host = config.get(section, 'server.ip')

From my config

[Logger]
#Possible values for logging are INFO, DEBUG and ERROR
log_level = DEBUG

[server1]
server.user_name = root
server.password = password
server.ip = 172.19.41.21
[server2]
server.user_name = root
server.password = password
server.ip = 172.19.41.21

Now my code says to check each section to retrieve the username and password. Since the first section doesn't contain these values, it's failing. How can i check each section for username and password and if its not there just go to the next section. I tried by checking for NONE and go to the next section. But that code is ugly and it's failing. Something like this:

if env.user=='':
        next

Can someone help me to proceed further?

Answers


tjohnson February 2016

Add this code to the beginning of your for loop:

if not config.has_option(section, 'server.user_name'):
    continue


user3100115 February 2016

Since only the first section doesn't contain these values you can use the iter function.

sections = iter(config.sections())
next(sections)
for section in sections:
    # something(section)

Or as @tjohnson mentioned:

for section in config.sections()[1:]:
    # something(section)


Cilyan February 2016

Another way to do is to just catch the exception.

for section in config.sections():
    components = dict() #start with empty dictionary for each section
    try:
        env.user = config.get(section, 'server.user_name')
        env.password = config.get(section, 'server.password')
        host = config.get(section, 'server.ip')
    except ConfigParser.NoOptionError as e:
        continue # At least one required option is missing in the section, skip

The advantage is that if any option is missing, the section will be ignored.

However, if you need to be atomic (for example, if it is a problem to set env.user if the section is finally ignored due to server.ip not present), you might want something like this.

for section in config.sections():
    components = dict() #start with empty dictionary for each section
    try:
        tmp_user = config.get(section, 'server.user_name')
        tmp_password = config.get(section, 'server.password')
        tmp_host = config.get(section, 'server.ip')
    except ConfigParser.NoOptionError as e:
        continue # At least one required option is missing in the section, skip
    else:
        env.user = tmp_user
        env.password = tmp_password
        host = tmp_host

In that case, maybe it is easier to use has_option 3 times.

Post Status

Asked in February 2016
Viewed 3,808 times
Voted 6
Answered 3 times

Search




Leave an answer