Posted by: sureshamrita | August 28, 2011

Extending the python configparser

The existing configparser module of python, does not provide the following operations.

  • If you are reading a directory/file name from a config file, it does not check if the directory/file exist
  • It does not even check if the config file itself is existing or not in its read() method.
  • It does not provide any specific methods to read and process multiple valued options, say comma separated. In such a situation the user would like to get the items as a list after removing the leading and trailing white space etc.
My guess is that such refined processing is left to the user of the configparser module. The following code defines a new class AmritaConfigParser which is derived from the SafeConfigParser class which is defined in the configparser module. The AmritaConfigParser class redefines/adds the following methods to remedy the above mentioned shortcomings.
  1. read(): It checks if the file is present and if it is present calls the base class read(). If the file is not present, raises an exception.
  2. getFile(section,option,check = True,dir='False') : Reads a dir or file name. If the check option is True, the existence of the read file is checked and an exception raised if it does not exist. If the dir option is made true, the read value is appended with '/' if it does not exist already. For windows the character appended would be '\'.
  3. getFileList(self,section,option,sep = ',' , check = True): It is same as getFile() method but separates the string at the sep character and removes the leading/trailing space and returns the list. Had python permitted function overloading, these two methods could have been made into one. Or is there any other tricks to combine these two methods into one?
The code is given below:
#!/usr/bin/python3.1
from configparser import  SafeConfigParser
import os

class AmritaConfigParser(SafeConfigParser):
    def __init__(self):
        super().__init__()

    def read(self,file):
        if not os.path.exists(file):
            raise OSError(file + ' does not exist')
        return super().read(file)

    def getFile(self,section,option,check = True,dir='False'):
        """gets the filename, checks if it is present (if check
        boolean is True)  or else gives an error
        message, and appends a / at the end if it does not exist already
        in case the retrieved file is a directory """

        file = super().get(section,option)
        if check:
            if not os.path.exists(file):
                raise OSError(file + ' does not exist')

        if dir:
            if not file.endswith(os.sep):
                file = file + os.sep

        return file

    def getFileList(self,section,option,sep = ',',check = True):
        """reads a string which contain 'sep' separated files.
        removes the trailing space between them
        checks for their presence if the check bool variable is True
        Finally returns a list """
        fileList = super().get(section,option)
        if fileList:
            fileList = fileList.split(',')
            fileList = [x.strip() for x in fileList]

            if check:
                for f in fileList:
                    if not os.path.exists(f):
                        raise IOError(f + ': File cannot be openend')
        return fileList
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: