Enjoy the good life everyday!
关闭
欢迎来PyGo个人空间 ^_^
Python模块之config配置解析 | PyGo²

Python模块之config配置解析

Python Python模块系列
Python模块系列

Python项目常用的模块代码。

版本

名称 版本
Python 3

描述

用来解析config.yaml配置文件,添加默认值。

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- coding: utf-8 -*-

"""
------------------------------------------------

describe:
解析配置

base_info:
__author__ = "PyGo"
__time__ = "2022/9/14"
__version__ = "v.1.0.0"
__mail__ = "gaoming971366@163.com"
__project__ = "quality-inspect"

usage:

design:

reference urls:

python version:
python3


Enjoy the good time everyday!!!
Life is short, I use python.

------------------------------------------------
"""

# ------------------------------------------------------------
# usage: /usr/bin/python config.py
# ------------------------------------------------------------
import os
import sys
import yaml
import inspect
import logging


# logging.basicConfig()
logger = logging.getLogger(__name__)


# get current folder, solve is or not frozen of the script
def _get_cur_folder():
if getattr(sys, "frozen", False):
return os.path.dirname(os.path.abspath(__file__))
else:
cur_folder = os.path.dirname(inspect.getfile(inspect.currentframe()))
return os.path.abspath(cur_folder)


# get global projects by mode
def _get_global_projects(mode='dev'):
if mode not in ['dev', 'prod']:
return None
return os.path.join(os.path.join(os.path.join(_get_cur_folder(), 'etc'), mode), 'projects.json')


# get current run config by mode
def _get_config(mode='dev'):
if mode not in ['dev', 'prod']:
return None
return os.path.join(os.path.join(os.path.join(_get_cur_folder(), 'etc'), mode), 'config.yaml')


# default log dir
def __get_log_dir():
return os.path.join(_get_cur_folder(), 'log')


# default log dir
def __get_result_dir():
return os.path.join(_get_cur_folder(), 'result')


"""
default config
"""
# SERVER
NAME = 'quality-inspect'
VERSION = 'v1.0.0'
DEBUG = True
SECRET_KEY = 'BelivemeIcanfly'

"""
enrty: initializate config
"""
mode = os.environ.get('mode') or 'dev'
_config_file = _get_config(mode)
if not os.path.exists(_config_file):
logger.critical('====== config file is not exist, exit ======')
sys.exit(1)

with open(_config_file, 'r', encoding='UTF-8') as f:
_config_info = yaml.safe_load(f)
if not _config_info:
logger.critical('====== config file is unavail, exit ======')
sys.exit(1)

# SERVER
NAME = _config_info['SERVER']['NAME'] or NAME
VERSION = _config_info['SERVER']['VERSION'] or VERSION
DEBUG = _config_info['SERVER']['DEBUG']
DEBUG = False if not DEBUG else DEBUG
SECRET_KEY = _config_info['SERVER']['SECRET_KEY'] or SECRET_KEY

系列

Python模块之command系统命令
Python模块之excel模块
Python模块之logger日志
Python模块之utils公共方法
Python模块之watcher打点
Python模块之config配置解析
Python模块之dtalk钉钉消息
Python模块之企业微信

更多模块请参考文章TAG进行查看。

Python模块系列,持续更新中。。。。。。
  • 本文作者:mingliang.gao【一个爱老婆Python程序猿。。。。。。】
  • 本文链接: http://pygo2.top/articles/51787/
  • 版权声明: 本博客所有文章欢迎转载,转载请注明出处!
觉得有帮助 请偶坐个公交车
0%