#!/usr/bin/env python
# -*- coding:utf-8 -*-
from nacos_fast import *
import os
import configparser
import redis
from redis.sentinel import Sentinel
BaseCnf = "report.cnf"


class config_fastapi(object):
    """
    # Config().get_content("user_information")
    """
    def __init__(self):
        if os.path.abspath(os.curdir).find('zkey_report') > -1:
            if os.path.abspath(os.curdir).find('core') > -1:
                file_path = os.path.join(os.path.abspath(os.curdir).replace('core', "conf"), BaseCnf)
            else:
                file_path = os.path.join(os.path.abspath(os.curdir), "conf", BaseCnf)
        elif os.path.abspath(os.curdir).find('core') > -1:
            file_path = os.path.join(os.path.abspath(os.curdir).replace('core', "conf"), BaseCnf)
        else:
            file_path = os.path.join(os.path.abspath(os.curdir), "zkey_report", "conf", BaseCnf)

        self.cf = configparser.ConfigParser()
        self.cf.read(file_path,encoding='utf-8')

    def get_sections(self):
        return self.cf.sections()

    def get_options(self, section):
        return self.cf.options(section)

    def get_content(self, section):
        result = {}
        for option in self.get_options(section):
            value = self.cf.get(section, option)
            result[option] = int(value) if value.isdigit() else value
        return result

    @staticmethod
    def get_redis_connect(redis_cfg):
        master = None
        try:
            if str(redis_cfg['host']).find(',') > -1:
                host_list = str(redis_cfg['host']).split(',')
                port_list = str(redis_cfg['port']).split(',')
                host_port_list = []
                for host_list_index, host_list_item in enumerate(host_list):
                    if host_list_index < len(port_list):
                        host_port_list.append((host_list_item, int(port_list[host_list_index])))
                    else:
                        host_port_list.append((host_list_item, int(port_list[0])))
                sentinel = Sentinel(host_port_list)
                master = sentinel.master_for(redis_cfg['mymaster'], socket_timeout=1000, password=redis_cfg['password'],
                                             decode_responses=False)
            else:
                master = redis.Redis(host=redis_cfg['host'], port=redis_cfg['port'], password=redis_cfg['password'],
                                     decode_responses=False)
        except Exception as ex:
            print(ex)
        finally:
            return master


nacosConf = config_fastapi().get_content("nacos")
mongodb_cfg, redis_cfg, templateFileServerUrlMap, responseCount = nacos_main(nacosConf)
redis_connect = config_fastapi().get_redis_connect(redis_cfg)
redis_cfg['redis_connect'] = redis_connect


class AppConfig:
    def __init__(self):
        self.mongodb_cfg_map = mongodb_cfg
        self.redis_cfg_map = redis_cfg
        self.templateFileServerUrlMap = templateFileServerUrlMap
        self.maxResponseCount = responseCount


# 创建一个全局实例
app_config = AppConfig()
