#!/usr/bin/python 
from configparser import ConfigParser
import json
  
  
def config(filename='database.ini', section='postgresql'): 
    # create a parser 
    parser = ConfigParser() 
    # read config file 
    parser.read(filename) 
  
    # get section, default to postgresql 
    db = {} 
    if parser.has_section(section): 
        params = parser.items(section) 
        for param in params: 
            db[param[0]] = param[1] 
    else: 
        raise Exception('Section {0} not found in the {1} file'.format(section, filename)) 
  
    return db

def sites_config(filename='database.ini', section='manage'): 
    # create a parser 
    parser = ConfigParser() 
    # read config file 
    parser.read(filename) 
  
    # get section, default to postgresql 
    instance_config = {}
    first_setup = False
    if parser.has_section(section): 
        params = parser.items(section)
        print(params)
        for param in params: 
            instance_config[param[0]] = param[1].split(',')
    else: 
        raise Exception('Section {0} not found in the {1} file'.format(section, filename)) 
    
    instance_config['first_setup'] = parser.getboolean('manage', 'first_setup')
    instance_config['signup_enabled'] = parser.getboolean('manage', 'signup_enabled')


    print(instance_config)
    return instance_config

def setFirstSetupDone():
    config = ConfigParser()
    config.read('database.ini')
    config.set('manage', 'first_setup', 'False')
    with open('database.ini', 'w') as configFile:
        config.write(configFile)

def write_new_site(site_name):

    old_value = [site for site in sites_config()['sites'] if site != ""]
    print(old_value)

    old_value.append(site_name)
    old_value = set(old_value)

    config = ConfigParser()
    config.read('database.ini')
    config.set('manage', 'sites', ','.join(old_value))

    with open('database.ini', 'w') as configFile:
        config.write(configFile)

def delete_site(site_name):
    old_value = sites_config()['sites']
    old_value.remove(site_name)

    config = ConfigParser()
    config.read('database.ini')
    config.set('manage', 'sites', ','.join(old_value))

    with open('database.ini', 'w') as configFile:
        config.write(configFile)

