IT技术

2018-11-26 python 配置模块

2018-11-26  本文已影响2人  昨天今天下雨天1
# -*- coding: utf-8 -*-
import os
from utils.reader import YamlReader

BASE_PATH = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0]
CONFIG_FILE = os.path.join(BASE_PATH, 'config', 'config.yml')
# DATA_PATH = os.path.join(BASE_PATH, 'data')
LOG_PATH = os.path.join(BASE_PATH, 'log')
REPORT_PATH = os.path.join(BASE_PATH, 'report')


class Config(object):
    def __init__(self, config=CONFIG_FILE):
        self.config = YamlReader(config).data

    def get(self, element, index=0):
        """用YamlReader读取返回的是一个list,第一项是默认的节。如果有多个节,可以传入index来获取。"""
        return self.config[index].get(element)

    def get_db_conf(self):
        db_conf = self.get('database')
        return '{engine}://{user}:{pwd}@{host}:{port}/{db}?charset={charset}'.format(**db_conf)


conf = Config()  # default conf, parse config.yml


上一篇下一篇

猜你喜欢

热点阅读