import configparser
import click
import os

import pprint as pretty

CONFIGURATION_APPLICATION_PATH = click.get_app_dir('malabar')
CONFIGURATION_FILE_NAME        = os.path.join(CONFIGURATION_APPLICATION_PATH,
                                              'configuration.ini')
DEFAULT_GROUP                  = 'otec'

RSS_DATABASE_FILE_PATH         = os.path.join(CONFIGURATION_APPLICATION_PATH,
                                              'rss.db')
OTEC_TEMPLATE_RSS_URI          = "http://cdafactory.pqa-collab.fr.alcatel-lucent.com/rss.php"

class OtecCfg:
        def __init__(self, sfile, _interpolation):
                self.configparser = configparser.ConfigParser(interpolation=_interpolation)
                self.configparser.read(sfile)

        def getDefaultSettings(self):
                return self.getSettings(DEFAULT_GROUP)

        def getSettings(self, group):
                return self.configparser[group]


def get_configuration(config_file_name, interpolation=None):
        return OtecCfg(config_file_name, interpolation)


def create_default_config_file():
        genCFG = configparser.ConfigParser(allow_no_value=True,
                                           interpolation=None)
        genCFG[DEFAULT_GROUP] = {'; Warning autogenerated file, Must be reviewed': 'WARNING',
                                 'rss-uri': OTEC_TEMPLATE_RSS_URI,
                                 'host': 'bull6-05.fr.alcatel-lucent.com',
                                 '; This should be a comment! ;-)':'Ornot',
                                 'user': 'root',
                                 'password': 'letacla',
                                 'ovf-directory': '/tmp',
                                 'Compression': 'yes'}
        genCFG['SystemUnderTest'] = {}
        if not os.path.exists(CONFIGURATION_APPLICATION_PATH):
                os.makedirs(CONFIGURATION_APPLICATION_PATH)
        with open(CONFIGURATION_FILE_NAME, 'w') as configfile:
                        genCFG.write(configfile)
