Python, 原创

python读取yaml配置 包含其他文件include 包含配置项拼装join

python读取多个配置文件

配置项字段拼装成新配置项 代码如下:

# 域名配置
WebDomain: &domain 0.0.0.0
WebPort: &port 8080
WebUrl: !join ["http://", *domain, ":", *port]

include其他文件配置如下:

# 引入通用配置
Config: !include config.yaml

文件目录:
config
— development.yaml
— production.yaml
— config.yaml
helpers
— config_helper.py
— logger_helper.py
— __init__.py
main.py
需安装插件yaml, 建议使用清华大学镜像(pip install -i https://pypi.tuna.tsinghua.edu.cn/simple yaml)

下面看下具体配置文件: development.yaml, production.yaml, config.yaml

# development.yaml
Environment: development

# 域名配置
WebDomain: &domain 0.0.0.0
WebPort: &port 8080
WebUrl: !join ["http://", *domain, ":", *port]

# 数据库配置
Databases:
  FcsWrite:
    Dialect: mysql
    Hostname: localhost:3306
    UserName: db_user
    Password: "&db-pass"
    Database: dbname
    DbDriver: pymysql
    CharSet: utf8
    DbCollat: utf8_general_ci
    DbDebug: True
  FcsRead:
    Dialect: mysql
    Hostname: localhost:3306
    UserName: db_user
    Password: "&db-pass"
    Database: dbname
    DbDriver: pymysql
    CharSet: utf8
    DbCollat: utf8_general_ci
    DbDebug: True

# LOG
Log:
  LogDriver: file # file, aliyun
  LogFileExtension: log
  LogPath: logs/
  # log level
  #    'CRITICAL': CRITICAL,
  #    'FATAL': FATAL,
  #    'ERROR': ERROR,
  #    'WARN': WARNING,
  #    'WARNING': WARNING,
  #    'INFO': INFO,
  #    'DEBUG': DEBUG,
  #    'NOTSET': NOTSET,
  LogLevel: DEBUG
  LogFileLevel: WARNING
  LogAliyun:
    EndPoint: cn-shanghai.log.aliyuncs.com  # 创建Project所属区域匹配的Endpoint
    Project: fc-test-service
    LogStore: fc-test-qa
    AccessKeyId: LTAIPJkrTFYHOcaC
    AccessKeySecret: Di8T1ODTfS2TdYsuoEK3SaBQYYQk7g

# 引入通用配置
Config: !include config.yaml

config.yaml为 通用各个环境的配置,参考如下

# config.yaml

Origins: ["http://localhost:3000"]
Timezone: Asia/Shanghai

下面开始python读取配置: helpers/config_helper.py

#!/usr/bin/python
# -*- coding:utf-8 -*-

import yaml
import os


# 配置中的 include参数,引入其他文件
def yaml_include(loader, node):
    file_name = os.path.join(os.path.dirname(loader.name), node.value)
    with open(file_name, 'rb') as inputfile:
        return yaml.load(inputfile, Loader=yaml.FullLoader)


# 配置项连接组成新配置项
def yaml_join(loader, node):
    seq = loader.construct_sequence(node)
    return ''.join([str(i) for i in seq])


# MyProjectEnv 为系统变量,用于配置当前运行环境如设置系统变量MyProjectEnv为production,没有配置则认为视development环境
def get_config(*args):
    """
    获取配置信息,调用环境变量 MyProjectEnv,如果设置了环境值,则调用,否则使用开发环境,可通过设置环境变量让程序调用正确的配置文件
    可变参数,获取配置中的子配置信息
    :return: dict{}
    """
    environment = "development"
    if 'MyProjectEnv' in os.environ:
        environment = os.environ['MyProjectEnv']
    current_file_path = __file__
    config_path = os.path.abspath(os.path.join(current_file_path, os.pardir, os.pardir, 'config'))
    yaml.add_constructor("!include", yaml_include)
    yaml.add_constructor("!join", yaml_join)
    with open(os.path.join(config_path, environment+'.yaml'), 'rb') as f:
        config = yaml.load(f, Loader=yaml.FullLoader)
        for arg in args:
            if arg not in config:
                print(arg, "is not in config ", config)
                return None
            config = config[arg]
        return config

如调用日志配置路径,在main.py可以这样调用

from helpers import config_helper

# 获取所有配置
# config = config_helper.get_config()

# 获取时区
# config_db = config_helper.get_config("Config", "Timezone")

# 获取日志配置信息
log_config = config_helper.get_config("Log")

(1622)

Related Post