Get proj config args from redis
Project description
config-redis
config-redis是一个python库,用于从redis获取项目配置,并且可以在项目中隐藏您的配置详细信息,以免配置信息泄露。
Installation
1.使用python包管理工具 pip 进行安装.
pip install config-redis
2.本项目需要使用redis存储配置
3.从命令行设置环境变量或写入配置文件
export CONF_FOR_REDISCONF="{'host': '172.0.0.1', 'port': 6379, 'db': 0, 'password': 'your_password', 'decode_responses': True}" :$CONF_FOR_REDISCONF
Usage
1.setup your config, e.g: your_proj/setting.py
from configredis import SetConfig, Tools, SetRedis
con = SetConfig.ConfigArgs()
# env 关键字是default, default是全局配置,可以放置env共用的配置
SetConfig.defaultconfig(
c_env='default',
disk_name='TenD'
)
# env 关键字是dev
SetConfig.devconfig(
c_env='dev',
celery_broker="amqp://user:password@172.0.0.1:5672//",
r_flow={'host': Tools.host_ip('httpbin.org'), 'port': 7000, 'db': 0, "cluster": True},
r_flow1={'host': 'httpbin.org', 'port': 7000, 'db': 0, "cluster": True}
)
# env 关键字是pro
SetConfig.proconfig(
c_env='pro',
celery_broker="amqp://user:password@172.0.0.1:5672//",
disk_name="TenB"
)
# env 关键字是custom_env
SetConfig.envconfig(
'custom_env',
c_env='custom_env'
)
config = SetConfig.configs('bin.org') # 如果明文配置env,会以明文配置优先,如果配置文件删除明文配置,则会查找redis最新配置,SetConfig.configs会自动把明文配置持久化到redis, replace_domain参数可以自动解析相关域名IP,适合局域网静态IP地址,如果是动态IP,请关闭。
if __name__ == '__main__':
print(config) # 获得当前env的配置
2.运行上一步SetConfig.configs会自动会自动把明文配置持久化到redis,此时删除所有明文配置文件SetConfig.defaultconfig/SetConfig.devconfig/SetConfig.proconfig/SetConfig.envconfig,项目会自动从redis拉取最新的配置。
from configredis import SetConfig, Tools, SetRedis
con = SetConfig.ConfigArgs()
config = SetConfig.configs('bin.org')
if __name__ == '__main__':
print(config) # 获得当前env的配置
# print(SetConfig.getconfs()) # 查看redis中项目目前所有版本配置
# SetConfig.print_config() # 罗列项目所有版本的配置信息,并根据index打印相关配置,方便复制到配置文件, 顺序从新到旧
# print(con['custom_env']) # 根据env查看redis储存的最新配置
# SetConfig.upsert_field_to_redis('custom_env', disk_name='TenC') # 明文配置删除的情况下,给redis中的最新配置增加字段,不要跟明文配置同时使用,会导致redis版本错乱 # noqa
# print(Tools.lookup_redis_proj_config()) # 显示redis存储的最新配置
# print(Tools.lookup_proj_config(replace_domain='bin.org')) # 显示当前所有env配置,明文优先于redis配置
# print(SetRedis().getkeys()) # 查看redis所有的项目名称
# SetConfig.delconfs() # 罗列项目所有版本的配置信息,并根据index删除相关配置, 顺序从旧到新
3.在项目中导入config获得对应的配置
from tests.setting import config
def test_env():
return config['celery_broker']
if __name__ == '__main__':
print(test_env())
4.使用命令行根据关键字获得配置运行项目。
python sample.py pro/dev/custom_env # default 是全局配置,可以放置env共用的配置
Contributing
使用前请做适当的测试,以确定跟您的项目完全兼容。
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
config-redis-0.2.12.tar.gz
(9.4 kB
view details)
Built Distribution
File details
Details for the file config-redis-0.2.12.tar.gz
.
File metadata
- Download URL: config-redis-0.2.12.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c523ff3d117de6915210af636162226c13d975570d38572df797c2d9b71ae4c |
|
MD5 | 501c47d07313a31e3b276b72c767047e |
|
BLAKE2b-256 | 9780e72b01c9f1ffe557145ab7986a2b423e0571160395326f7d18324352e256 |
File details
Details for the file config_redis-0.2.12-py3-none-any.whl
.
File metadata
- Download URL: config_redis-0.2.12-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 398c3547de9e79e2cbc2f6b51f2690ca98e7567143fd1d0353175cd74424b773 |
|
MD5 | b781d9b996472b880a2e48233fe4df32 |
|
BLAKE2b-256 | 4fa84017ccd83304ecec436e4d9a551580ed784a111842c7f9537bfdb58290d7 |