Skip to main content

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

MIT

Metadata

Release files for config-redis 0.2.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for config-redis 0.2.12
File Size Uploaded Provenance Actions
config-redis-0.2.12.tar.gz 9.4 kB Without provenance Details for config-redis-0.2.12.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for config-redis 0.2.12
File Interpreter ABI Platform Actions
config_redis-0.2.12-py3-none-any.whl Python 3 none any Details for config_redis-0.2.12-py3-none-any.whl

Total release size: 17.1 kB

Release files / config-redis-0.2.12.tar.gz

Download URL config-redis-0.2.12.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3c523ff3d117de6915210af636162226c13d975570d38572df797c2d9b71ae4c
BLAKE2b-256 checksum
How to use checksums
9780e72b01c9f1ffe557145ab7986a2b423e0571160395326f7d18324352e256
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / config_redis-0.2.12-py3-none-any.whl

Download URL config_redis-0.2.12-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
398c3547de9e79e2cbc2f6b51f2690ca98e7567143fd1d0353175cd74424b773
BLAKE2b-256 checksum
How to use checksums
4fa84017ccd83304ecec436e4d9a551580ed784a111842c7f9537bfdb58290d7
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for config-redis 0.2.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page