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


# import ConfigParser


# class MyConfigParser(ConfigParser.ConfigParser):
#     def __init__(self, defaults=None):
#         ConfigParser.ConfigParser.__init__(self, defaults=defaults)
#
#     def optionxform(self, optionstr):
#         return optionstr

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

    # 保存修改;
    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)

    def getParentWaitTime(self, parent):
        optionName = parent + "WaitTime"
        try:
            waitTime = float(self.get_value("waitTime", optionName))
        except Exception:
            waitTime = 1.0
            print "获取%s界面的等待界面时间失败,使用默认值1.0s" % str(parent)
        return waitTime

    def getThresholdDict(self, option):
        section = "Threshold"
        thresholdDict = {}
        value = self.get_value(section, option)
        print "value:", value, type(value)
        if not value:
            return thresholdDict
        value_str = str(value)
        print "value_str:", value_str, type(value_str)
        value_dict = eval(value_str)
        print "value_dict:", value_dict, type(value_dict)
        return value_dict


if __name__ == "__main__":
    # tconfig = TConfig(r'E:\SAT\SAT_API\ssat_sdk\MenuTree3\MenuTree.ini')
    tconfig = TConfig(r'E:\SAT\resource\MenuTree\NT72\DVB_nafrican\MenuTree.ini')
    # print tconfig.get_options('First')
    # print tconfig.get_sections()
    # print tconfig.get_items('First')
    value = tconfig.get_value('value', 'Setting')
    print value
    # print tconfig.get_dict(value)
    # print tconfig.set_value('First', 'fffff','aaaa')
    # tconfig.del_option('First','fffff')
    # tconfig.del_section('Second')
    # tconfig.set_value('second','setting','{"offset":10, "minPeri":0, "maxPeri":0, "minArea":0, "maxArea":0}')