# -*- coding:utf-8 -*-
import string
import os
import sys
import time
from configparser import ConfigParser
import json


class TConfig:
    def __init__(self, ini_path):
        self.path = ini_path
        self.cfgParser = ConfigParser()

    # 保存修改;
    def save_config(self):
        with open(self.path, 'wb') as configfile:
            self.cfgParser.write(configfile)

    # 字符转字典;
    def get_dict(self, value):
        return json.loads(value)

    def get_value_dict(self, section, option):
        value = self.get_value(section, option)
        if value is None:
            return {}
        return json.loads(value)

    # 获取参数值;默认返回字符串
    def get_value(self, section, option):
        # 读取配置文件;
        self.cfgParser.read(self.path)
        if self.cfgParser.has_option(section, option):
            return self.cfgParser.get(section, option)
        return None

    # 获取参数值;
    def get_int(self, section, option):
        # 读取配置文件;
        self.cfgParser.read(self.path)
        if self.cfgParser.has_option(section, option):
            return self.cfgParser.getint(section, option)
        return None

    # 获取参数值;
    def get_float(self, section, option):
        # 读取配置文件;
        self.cfgParser.read(self.path)
        if self.cfgParser.has_option(section, option):
            return self.cfgParser.getfloat(section, option)
        return None

    # 获取参数值;
    def get_boolean(self, section, option):
        # 读取配置文件;
        self.cfgParser.read(self.path)
        if self.cfgParser.has_option(section, option):
            return self.cfgParser.getboolean(section, option)
        return None

    # 获取键值;
    def get_options(self, section):
        # 读取配置文件;
        self.cfgParser.read(self.path)
        if self.cfgParser.has_section(section):
            return self.cfgParser.options(section)
        return None

    # 获取所有sections;
    def get_sections(self):
        # 读取配置文件;
        self.cfgParser.read(self.path)
        return self.cfgParser.sections()

    # 获取指定section所有items;
    def get_items(self, section):
        # 读取配置文件;
        self.cfgParser.read(self.path)
        if self.cfgParser.has_section(section):
            return self.cfgParser.items(section)
        return None

    # 添加section;
    def add_section(self, section):
        # 读取配置文件;
        self.cfgParser.read(self.path)
        if self.cfgParser.has_section(section) is False:
            self.cfgParser.add_section(section)
        # 保存修改;
        self.save_config()

    # 设置值;
    def set_value(self, section, option, value):
        # 读取配置文件;
        self.cfgParser.read(self.path)
        if self.cfgParser.has_section(section) is False:
            self.cfgParser.add_section(section)
        self.cfgParser.set(section, option, value)
        # 保存修改;
        self.save_config()

    # 删除指定option;
    def del_option(self, section, option):
        # 读取配置文件;
        self.cfgParser.read(self.path)
        self.cfgParser.remove_option(section, option)
        # 保存修改;
        self.save_config()

    # 删除section;
    def del_section(self, section):
        # 读取配置文件;
        self.cfgParser.read(self.path)
        self.cfgParser.remove_section(section)
        # 保存修改;
        self.save_config()

    def has_section(self, section):
        # 读取配置文件;
        self.cfgParser.read(self.path)
        return self.cfgParser.has_section(section)

    def has_option(self, section, option):
        # 读取配置文件;
        self.cfgParser.read(self.path)
        return self.cfgParser.has_option(section, option)


if __name__ == "__main__":
    tconfig = TConfig(r'E:\SAT\resource\MenuTree\NT72\DVB_nafrican\MenuTree.ini')
    value = tconfig.get_value('value', 'Setting')
    print value