configi是一个简单的配置数据包装器。它提供了一个到多个后端的一致接口,目前通过任意http json加载、redis k/v存储和s3json文件加载。
configi的Python项目详细描述
configi是一个简单的配置数据包装器。它提供了一个到多个后端的一致接口,目前通过任意http json加载、redis k/v存储和s3json文件加载。
使用configi
代码本身是有文档记录的,但这里有几个方法:
s3配置源
fromconfigiimportDynamicConfig,S3ConfigSourceimportboto# Boto not included, but if you want to use S3ConfigSource# you probably have it already. I hope.s3=boto.connect_s3()bucket=s3.get_bucket("my-config-repository")config_file_key=bucket.get_key("my-config-file-key")source=S3ConfigSource(config_file_key)config=DynamicConfig(source,expiry=(60*4),# Every four hours reloadquiet_mode=True,# Errors will `print` instead of throwing Exceptions.namespace_dicts=True,# Turn dictionaries into dot-accessible namespaces.defaults={'some_key':'a_value'}# Defaults dict is used to return values, if a key is unset remotely.)printconfig.foo>Noneconfig.foo='bar'printconfig.foo>barprintconfig.some_key>a_valueconfig.some_key='banana'printconfig.some_key>banana
redis配置源
fromconfigiimportDynamicConfig,RedisConfigSourceimportredis# Redis not included, but if you want to use# RedisConfigSource, then you'll need it.r=redis.StrictRedis()# Get a Redis connection. Yours will have more parameters.source=RedisConfigSource(r,prefix='MY_AWESOME_CONFIGI_CONFIG_SETUP:')# Default prefix is 'CONFIGI:'config=DynamicConfig(source,expiry=(60*4),# Every four hours reloadquiet_mode=True,# Errors will `print` instead of throwing Exceptions.namespace_dicts=True,# Turn dictionaries into dot-accessible namespaces.defaults={'some_key':'a_value'}# Defaults dict is used to return values, if a key is unset remotely.)printconfig.foo>Noneconfig.foo='bar'printconfig.foo>barprintconfig.some_key# Even though defaults were passed, any remotely-set value overrides.>orangutan
json文件配置源
fromconfigiimportDynamicConfig,JSONConfigSourcesource=JSONConfigSource("https://some-url.com/my-config-file.json")config=DynamicConfig(source,expiry=(60*4),# Every four hours reloadquiet_mode=True,# Errors will `print` instead of throwing Exceptions.namespace_dicts=True,# Turn dictionaries into dot-accessible namespaces.defaults={'some_key':'a_value'}# Defaults dict is used to return values, if a key is unset remotely.)printconfig.foo>Noneconfig.foo='bar'>DynamicConfigError:Couldnotsetkeyfoo
注意事项
一些配置源更适合于只读配置。也就是说,基于json文件的任意配置是严格只读的。基于s3的配置是读/写的,但对于高写场景来说不是很理想。redis,如果一个redis商店既安全又对你可用,可能是你最好的选择。