#coding:utf-8
import ConfigParser
import os
import threading

from PyQt4.QtCore import QFileInfo

from common import commondata


configMutex = threading.Lock()

class ConfigFileParser():
    _instance = 0
    def __init__(self):       
        self.conf = ConfigParser.ConfigParser()
        configDir = QFileInfo(commondata.CONFIG_DIR)
        if (not configDir.isDir()):
            os.system("mkdir -p " + commondata.CONFIG_DIR)


    def getValue(self, section, option):
        """获取指定的section， 指定的option的值"""
        value = ""
        configMutex.acquire()
        
        self.conf.read(commondata.CONFIG_PATH)
        
        if self.conf.has_section(section):
            if self.conf.has_option(section, option):
                value = self.conf.get(section, option)
        configMutex.release()
        return value

    def addSection(self, new_section, option=None, value=None):
        """写入指定section, 增加新option的值"""
        ret = False
        configMutex.acquire()
        self.conf.read(commondata.CONFIG_PATH)
        if not self.conf.has_section(new_section):
            self.conf.add_section(new_section)
            #判断键值和数据是否有效
            if option and value:
                self.conf.set(new_section, option, value)
            self.conf.write(open(commondata.CONFIG_PATH,"w"))
            
            ret = True
        configMutex.release()
        return ret
    
    def addOption(self, section, option, value):
        """在指定的section, 增加新option的值"""
        ret = False
        configMutex.acquire()
        self.conf.read(commondata.CONFIG_PATH)
        if self.conf.has_section(section):
            if not self.conf.has_option(section, option):
                self.conf.set(section, option, value)
                self.conf.write(open(commondata.CONFIG_PATH,"w"))
                ret = True
        configMutex.release()
        return ret
            
    def has_section(self, section):
        """是否存在指定的section"""
        ret = False
        configMutex.acquire()
        self.conf.read(commondata.CONFIG_PATH)
        if self.conf.has_section(section):
            ret = True
        configMutex.release()
        return ret
    
    def has_option(self, section, option):
        """是否存在指定的option"""
        ret = False
        configMutex.acquire()
        self.conf.read(commondata.CONFIG_PATH)
        if self.conf.has_section(section):
            if self.conf.has_option(section, option):
                ret = True
        configMutex.release()
        return ret
        
    def options(self, section):
        """返回所有的option"""
        configMutex.acquire()
        self.conf.read(commondata.CONFIG_PATH)
        ret =self.conf.options(section);
        configMutex.release()
        return ret
    
    def sections(self):
        """返回所有的section"""
        configMutex.acquire()
        self.conf.read(commondata.CONFIG_PATH)
        ret = self.conf.sections()
        configMutex.release()
        return ret

        
    def changeSectionValue(self, section, option, value):
        """更新指定section, option的值"""  
        ret = False
        configMutex.acquire()
        self.conf.read(commondata.CONFIG_PATH)
        
        if self.conf.has_section(section):
            if self.conf.has_option(section, option):
                self.conf.set(section, option, value)
                self.conf.write(open(commondata.CONFIG_PATH,"w"))
                ret = True
        configMutex.release()
        return ret
    
    def remove_section(self, section):
        """删除指定的section"""
        ret = False
        configMutex.acquire()
        self.conf.read(commondata.CONFIG_PATH)
        if self.conf.has_section(section):
            self.conf.remove_section(section)
            self.conf.write(open(commondata.CONFIG_PATH,"w"))
            ret = True
        configMutex.release()
        return ret
    
    def remove_option(self, section, option):
        """删除指定的option"""
        ret = False
        configMutex.acquire()
        self.conf.read(commondata.CONFIG_PATH)
        if self.conf.has_section(section):
            if self.conf.has_option(section, option):
                self.conf.remove_option(section, option)
                self.conf.write(open(commondata.CONFIG_PATH,"w"))
                ret = True
        configMutex.release()  
        return ret
    
    def addOrUpdateValue(self, section, option, value):
        section = str(section)
        option = str(option)
        value = str(value)
        if self.has_section(section):
            if self.has_option(section, option):
                self.changeSectionValue(section, option, value)
            else:
                self.addOption(section, option, value)
        else:
            self.addSection(section, option, value)
        






        