Exemplo n.º 1
0
class TestConfigFile(unittest.TestCase):
    configfile = None

    def setUp(self):
        # 创建对象
        config_dir = os.path.abspath(
            os.path.join(os.path.dirname(__file__), 'files'))
        self.configfile = ConfigFile(config_dir)

    def test_is_app_exist(self):
        file_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__), 'files', 'config.json'))
        self.assertEqual(self.configfile.is_app_exist('config', 'json'),
                         file_path)
        self.assertEqual(self.configfile.is_app_exist('config', 'None'), False)

    def test_load_app(self):
        self.assertEqual(self.configfile.load_app('config'), {"file": "yaml"})
        self.assertEqual(self.configfile.load_app('None'), None)

    def test_load_out(self):
        file_path = os.path.join(os.path.dirname(__file__), 'files',
                                 'config.yaml')
        self.assertEqual(ConfigFile.load_out(file_path), {"file": "yaml"})
        file_path = os.path.join(os.path.dirname(__file__), 'files',
                                 'config.json')
        self.assertEqual(ConfigFile.load_out(file_path), {"file": "json"})

    def test_load_app_json(self):
        self.assertEqual(self.configfile.load_app_json('config'),
                         {"file": "json"})

    def test_dynamic_json(self):
        self.assertEqual(
            self.configfile.load_dynamic_json('config', callback, 3),
            {"timestamp": timestamp1})
        time.sleep(1)
        self.assertEqual(
            self.configfile.load_dynamic_json('config', callback, 3),
            {"timestamp": timestamp1})
        time.sleep(3)
        self.assertNotEqual(
            self.configfile.load_dynamic_json('config', callback, 3),
            {"timestamp": timestamp1})

        file_path = os.path.join(self.configfile.config_dir,
                                 '.'.join(['config', 'dynamic', 'json']))
        self.assertTrue(os.path.isfile(file_path))
        self.configfile.del_dynamic_json('config')
        self.assertFalse(os.path.isfile(file_path))
Exemplo n.º 2
0
# -*- coding: utf-8 -*-
# Author: Ztj

import logging
import logging.config
import os

from configfile import ConfigFile
from registry import Registry
from libs.logger import JsonLogger
"""全局设置"""
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(levelname)s - %(message)s')
"""全局变量"""
service_root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
"""初始化配置"""
GlobalConfigFile = ConfigFile(os.path.join(service_root_path, 'configs'))
"""加载配置文件"""
app_options = Registry(GlobalConfigFile.load_app('app'))
"""配置日志"""
log_options = GlobalConfigFile.load_app('log')
if log_options is not None:
    logging.setLoggerClass(JsonLogger)
    logging.config.dictConfig(log_options)

json_logger = logging.getLogger('json')