Working with config files in Python: ConfigParser advanced

Posted by in Python

As you could read on my last post about Python and ConfigParser, I wasn’t completly agree on how to save data, so I decided to investigate if some one has an improved class for ConfigParser.

On my first search, I found this page (http://www.decalage.info/en/python/configparser), and indeed, I got an improved class, but only for reading data (not for writting) within the comments.

I keep on searching, and I found a Python project with a very powerful class, but not able to use it for diferent projects . Basically, the class is designed specifically for the project..

The better option I found was a GitHub project: https://github.com/songqiang/configparser/blob/master/ConfigParser.py, . Again, it’s only for reading, not for writting.

so, I ended up writting my own ConfigParser advanced, that you can have the code here and on GitHub.

""" ConfigParserAdvanced,
developer by David Trillo Montero
write me at manejandodatos@gmail.com

Visit my website: http://www.manejandodatos.es """
# Versión 0.7.0 - 20140110

import ConfigParser

class ConfigParserAdvanced():
    def __init__(self, sfile, main_section = None, getdefault = ''):
        self.file = sfile
        self.reload()
        self.main_section = main_section
        self.getdefault = getdefault

    def reload(self):
        self.config = ConfigParser.SafeConfigParser()
        self.config.read(self.file)

    def defaultValue(self, value):
        self.getdefault = value

    def read(self, sfilename):
        return self.config.read(sfilename)

    def sections(self):
        """ Sections of the Config File """
        return self.config.sections()

    def main_section(self, new_section):
        """ Change Section """
        self.main_section = new_section

    def options(self, new_section):
        """ Options from a NEW SECTION, that become Main_Section """
        self.main_section = new_section
        return self.config.options(self.main_section)

    def add_section(self, new_section):
        """ Add a New section """
        self.main_section = new_section
        self.config.add_section(self.main_section)

    # GET functions
    def get(self, section, option, defval = None):
        """ Get a VALUE of an option, of the SECTION"""
        self.main_section = section
        try:
            return self.config.get(self.main_section, option)
        except:
            if defval != None:
                return defval
            else:
                return self.getdefault
    def getboolean(self, section, option):
        """ GET a BOOLEAN Value of an OPTION of the SECTION """
        self.main_section = section
        try:
            return self.config.getboolean(self.main_section, option)
        except:
            return False
    def getfloat(self, section, option):
        """ GET a BOOLEAN Value of an OPTION of the SECTION """
        self.main_section = section
        try:
            return self.config.getfloat(self.main_section, option)
        except:
            return False
    def getint(self, section, option):
        """ GET a BOOLEAN Value of an OPTION of the SECTION """
        self.main_section = section
        try:
            return self.config.getint(self.main_section, option)
        except:
            return False
    def has_option(self, section,option):
        """ Exists OPTION in SECTION """
        self.main_section = section
        try:
            return self.config.has_option(self.main_section, option)
        except:
            return False

    # get FROM the Main_SECTION
    def options2(self):
        """ Options from Main_Section """
        return self.options(self.main_section)
    def get2(self, option):
        """ Get a VALUE of an option, of the MAIN_SECTION"""
        return self.get(self, self.main_section, option)
    def getboolean2(self, option):
        """ GET a BOOLEAN Value of an OPTION of the SECTION """
        return self.getboolean(self.main_section, option)
    def getint2(self, option):
        """ GET a BOOLEAN Value of an OPTION of the SECTION """
        return self.getint(self.main_section, option)
    def getfloat2(self, option):
        """ GET a BOOLEAN Value of an OPTION of the SECTION """
        return self.getfloat(self.main_section, option)
    def has_option2(self, option):
        """ Exists OPTION in MAIN_SECTION """
        return self.has_option(self.main_section, option)
    def has_section(self, section):
        try:
            return self.config.has_section(section)
        except:
            return False

    # Write Data to self.File
    def writedata(self):
        sf = open(self.file,"w") # Necesito el fichero INI "preparado" para grabar información
        self.config.write(sf)
        sf.close()
    def set(self, section, option, value = None, bSave = True):
        if self.config.has_section(section) == False:
            self.config.add_section(section)
        self.main_section = section
        self.config.set(self.main_section, option, value)
        if bSave:
            self.writedata()

    # Write OPTION-VALUE to MAIN_SECTION
    def set2(self, option, value = None, bSave = True):
        """ Set data on the MAIN_SECTION """
        self.set(self.main_section, option, value, bSave)

Características de mi ConfigParser advanced

La principales características de mi propuesta de ConfigParser son:

  • One unique class for reading and writting
  • If I am working with one section, I have methods for not including it every time (although there are methos that allows you to include it)
  • Improve asking for sections of keys on a section
  • It checks If a section exist before adding new data. In case the section doesn’t exist, it creates it before

I guess several of you would think about the unnecesary of creating such class, but I use it, and I share with you!