#!/usr/bin/env python
#-*- coding: UTF-8 -*-
#Ticloud web version 2.0
#author:WangRui

import ConfigParser
import logging


class ConfigManager(object):

    _config_dict = None

    @staticmethod
    def create(filename):
        parse_file = ParseIniFile(filename)
        parse_file.init()
        parse_file.getvalue()
        parse_file.close()
        ConfigManager._config_dict = parse_file.ini_dict

    @staticmethod
    def getvalue(arr, args):
        try:
            return ConfigManager._config_dict[arr][args]
        except AttributeError:
            logging.error("from ConfigManager._config_dict get config attribute error")
        return None


class ParseIniFile(object):
    """
         解析ini配置文件
    """
    def __init__(self, filename):
        self.filename = filename
        self.cfg = None
        self.read_handle = None
        self.ini_dict = {}

    def init(self):
        self.cfg = ConfigParser.ConfigParser()
        try:
            with open(self.filename, "r") as self.read_handle:
                self.cfg.readfp(self.read_handle)
        except IOError:
            logging.error("parse ini file error")

    def close(self):
        if self.read_handle is not None:
            self.read_handle.close()

    def getvalue(self):
        if self.read_handle:
            for sect in self.cfg.sections():
                temp_dict = dict()
                temp_dict["info"] = ''
                for opt in self.cfg.options(sect):
                    temp_dict[opt] = self.cfg.get(sect, opt)
                    info = "\n" + opt + "=" + self.cfg.get(sect, opt)
                    temp_dict["info"] += info
                self.ini_dict[sect] = temp_dict

    def all_options(self, sect):
        List = []
        for opt in self.cfg.options(sect):
            Dict = {}
            Dict["opt"] = opt
            Dict["value"] = self.cfg.get(sect, opt)
            List.append(Dict)
        return List

    def get_value_now(self, sect, opt):
        return self.cfg.get(sect, opt)

    def write(self, data):
        for k in self.ini_dict[data]:
            if not cmp(k, "info"):
                continue
            self.cfg.set(data, k, self.ini_dict[data][k])
        self.cfg.write(open(self.filename, "w"))

    def delsection(self, name):
        e = ''
        self.cfg = ConfigParser.ConfigParser()
        try:
            self.cfg.read(self.filename)
            self.cfg.remove_section(name)
            self.cfg.write(open(self.filename, "w"))
        except ConfigParser.ParsingError, e:
            print e
        return e


class ParseConfigFile(object):

    def __init__(self, filename):
        self.filename = filename
        self.cfg = None
        self.read_handle = None
        self.ini_dict = {}

    def init(self):
        self.cfg = ConfigParser.ConfigParser()
        try:
            with open(self.filename, "r") as self.read_handle:
                self.cfg.readfp(self.read_handle)
        except IOError:
            logging.error("parse ini file error")

    def close(self):
        if self.read_handle is not None:
            self.read_handle.close()

    def getvalue(self):
        if self.read_handle:
            for sect in self.cfg.sections():
                temp_dict = dict()
                temp_dict["info"] = ''
                for opt in self.cfg.options(sect):
                    temp_dict[opt] = self.cfg.get(sect, opt)
                    info = "\n" + opt + "=" + self.cfg.get(sect, opt)
                    temp_dict["info"] += info
                self.ini_dict[sect] = temp_dict

    def write(self, data):
        for k in self.ini_dict[data]:
            if not cmp(k, "info"):
                continue
            self.cfg.set(data, k, self.ini_dict[data][k])
        self.cfg.write(open(self.filename, "w"))

    def delsection(self, name):
        e = ''
        self.cfg = ConfigParser.ConfigParser()
        try:
            self.cfg.read(self.filename)
            self.cfg.remove_section(name)
            self.cfg.write(open(self.filename, "w"))
        except ConfigParser.ParsingError, e:
            print e
        return e