#!/usr/bin/python

import os
import sys
import ConfigParser

class configFile:
    """
    Simple Configurator File Parser.
    Developed for use with my WordPress (wp.py) blog manager application
    The config file must be in $HOME/.wp.conf and must have this formar:
    [BlogName]
    User=foo
    Pass=bar
    Server=http://whatever.com
    [BlogName2]
    ...
    And so on
    Available Methods:
        getUSer -> returns the User field of a section
        getPass -> returns the Pass field of a section
        getServer -> returns the Server field of a section
    """

    _confFile = ""
    _section = ""

    def __init__(self, section):
        """
        Class constructor.
        Params :
            section : a string with the section name (blog name) of the conf file
        """
        path = os.path.expanduser("~")
        self._confFile = ConfigParser.ConfigParser();
        try:
            self._confFile.read(path + "/.wp.py.conf")
        except err:
            print "Error: " + err.__str__()
        self._section = section

    def getUser(self):
        """
        returns the User field of a section
        """
        user = ""
        try:
            user = self._confFile.get(self._section, "User")
        except ConfigParser.NoSectionError:
            print "Section " + self._section + " doesn't exists in conf file"
        except err:
            print "Error: " + err.__str__()

        return user

    def getPass(self):
        """
        returns the Pass field of a section
        """
        passwd = ""
        try:
            passwd = self._confFile.get(self._section, "Pass")
        except ConfigParser.NoSectionError:
            print "Section " + self._section + " doesn't exists in conf file"
        except err:
            print "Error: " + err.__str__()

        return passwd 

    def getServer(self):
        """
        returns the Server field of a section
        """
        server = ""
        try:
            server = self._confFile.get(self._section, "Server")
        except ConfigParser.NoSectionError:
            print "Section " + self._section + " doesn't exists in conf file"
        except err:
            print "Error: " + err.__str__()

        return server

def main():
    conf = configFile(sys.argv[1])
    print conf.getUser()
    print conf.getPass()
    print conf.getServer()

if __name__ == "__main__":
    main()

