从环境变量和/或文件优化应用程序配置(+Django)
django-confenv的Python项目详细描述
django confenv目的-使django设置.py(或任何其他Python 项目配置)持久化,而实际安装是从文件完成的 使用简单的“env=value”格式或操作系统环境变量。还有,这个 工艺称为Twelve-factor methodology或{a2}方法。在
请注意,不管名称如何,您都可以在ANYPython中使用django-confenv 项目,没有激活任何与django相关的东西。在
django confenv是django-environ的大量重构版本 通过Daniele Faraglia和other authors。与该准则的不同之处在于:
- higher speed (sometimes much higher)
- smaller code
- smaller footprint
- django-related stuff moved to module
- more functionality in .env file(s) search/selection
- some improvements/speedups in ‘api’
- separated basic tests for base and django functionality
django confenv是(C)20192020 Vitaly Protsko<;me@protsko.expert>; 在GPLv3下发布。如果您需要其他许可证下的代码,请, 联系作者。在
快速使用
获取环境
# base functionality classfromconfenvimportEnv
Django项目
^{pr2}$接下来,“配置”Env本身
# Set exception to be raised in case of error# For Django it is set to ImproperlyConfigured#Env.exception = Exception# Get default configuration file name from named# environemt variable# Default: CONFENV_PROFILEEnv.defconf_key='BIGPROJECT_CONFNAME'# Set env file name to be read, instead of name,# defined in env var named in Env.defconf_key# filename + '.env' will also be checked, if# file with plain filename not found# File will be searched in directory where calling# Env class instantiation program resides if# Env.filepath is None# Default: '.env'Env.filename='default.conf'# Search for file Env.filename or file '.env' in this# path, if set.# If name in Env.filename is absolute, filepath will# not be prepended (will be ignored).# If this file path is not absolute the directory where# calling Env class instantiation program resides will# be prepended.# Default: NoneEnv.filepath='..'# Set dict to operate on# Default: os.environ#Env.data = dict()
收集文件中的所有可变值,也可由shell解析:
# # default.conf # # put settings in environment variables DEBUG=1# you can use simple variable substitution. Remember, that # this interpreter is not a shell. This works only if you # start value from '$<varname>'. In that case variable # will be substituted by value of the variable, found in # previous asignments or in Env.data dictionary. Resolving # recursion depth is not limited. CACHEDIR=$HOME/.cache
- 您可以定义
- 在项目中使用的参数的类型和默认值 直接将“scheme”定义为参数或字典。 例如,将变量DEBUG定义为布尔参数 默认“False”值,您可以使用以下任一形式:
# directly in parametersenv=Env(DEBUG=(bool,False),CACHEDIR=(str,))# or as a prepared list of kwargs:kwl={'DEBUG':(bool,False),'CACHEDIR':(str,),}env=Env(**kwl)
支持类型的完整列表,您可以在后面的文件中找到。在
# now use it in your program# WARNING:# If you dont pass the default value to converter# or miss it in Env class's parameters ("schema"),# Env will require the variable to exist in OS# environment (precisely, in Env.data dict),# otherwise Env.exception will be thrown.# this will assign True to variable debug, if# environment variable DEBUG value in# ('true', 'on', 'ok', 'y', 'yes', 'yea', '1')# Other value or variable absence will assign# False value.DEBUG=env('DEBUG')# you can directly point Env to conversion you# need to be done with value of env var by# calling corresponding method directlySERVER=env.url('SERVER','http://www.example.com')# this will assign instance of ParseResult from# urllib.parse with pre-parsed URL, for the default# value in example above it will be# ParseResult(scheme='http', netloc='www.example.com', path='', params='', query='', fragment='')## NB: all other conversions return expected type,# not an instance of side class# use substituted var, but back it with default valueCACHE=env('CACHEDIR',default='/var/cache')# complex json is also not a problemPARAMS=env.json('PARAMETERS',"{'par1':'val1', 'par2': {'def': 1, 'set': 2}, 'par3': [1, 2]}")# this will assign default dictionary to# variable PARAMS if PARAMETERS is absent in# env file or in OS environment variables
支持的类型
下面是示例中收集的所有支持的数据类型 .env文件和在程序中使用它的代码。在
# # myappconf.env # # NB: Quotes for us are optional, value counted # from character after the equal sign, except # when surrounded by quotes, in which case # they are stripped. # There is limitation: variable assignments # must be written in one line. Continuations # are not supported. # bool variable, accepting values # true on ok y yes yea 1 # as True, any other value as False. # You can use "export" keyword before variable # name and maintain one place of configuration # for application and accompanying shell scripts exportDEBUG=no # lines with "unset" keyword are silently ignored unset APP_DEBUG # str variable # Any sequence of characters. This is effectively # the same, as unicode type, look below for # declaration example(s) SERVERNAME="Our service server"# bytes variable # Sequence of any characters, that can be # decoded by .encode('utf8') method. Or you can # pass encoding directly to convertor method # with parameter: env.bytes('VAR', encoding='utf16') WELCOMEMSG=Добро пожаловать! # integer variable PORT=1234# float variable MAXAVGLOAD=5.5 # list variable LISTENIP=127.0.0.1,192.168.1.1,10.0.0.1 # tuple variable # value type in this case will be str ENDPOINTS=(start,read,calculate,write,stop)# dict variable #1 # In this case all values are strings and # here is no need to declare "schema" for # this variant. USERACL=root=admin,jdoe=operator,john=user # dict variable #2 # This dictionary variant needs declaration # to properly convert values, it can be done # in Env class instantiation parameters. COEFFICIENT=a=10.11;b=5;result=unknown # json variable # This type can be used for complex setup # of something (like menu) or for any other # kind of structured (initialization?) data. MENUEXTRA={"ExtraItem1": {"display": "&Cook Coffee", "handler": "cooker", "allow": "ALL"}}# url variable # This can be used for pointing to any kind # of resources, allowed schemes are as in # urllib. EXTLOGO=http://image.bigproject.com/biglogo.jpg
- django应用程序还有其他转换器。
- 它们只存在于Env中,从Confinv.django公司 模块。
# # djangosite.env # # Applications list # We can add applications to django's standard # list in this way. APPADD=django.contrib.humanize APPADD=$APPADD,django.contrib.syndication APPADD=$APPADD,bs3base,testapp,myapp # Database URL # This variable value can be parsed as database # configuration for django project. Env will # automatically select appropriate django driver # for database type pointed by url scheme. # Recognized schemes are: # postgres, postgresql, pgsql, postgis, mysql, # mysql2, mysql-connector, mysqlgis, mssql, oracle, # pyodbc, redshift, spatialite, sqlite, ldap MAINDB_URL=pgsql://dbuser:dbpass@db.example.com:5432/bigproject?AUTOCOMMIT=True # Cache URL # You can point django to cache resource(-s) as url # Recognized schemes are: # dbcache, dummycache, filecache, locmemcache, # memcache, pymemcache, rediscache, redis CACHE_URL=locmemcache:// MEMCACHE_URL=memcache://localhost:12345 # E-Mail URL # Django's e-mail submitting parameters # Recognized schemes are: # smtp, smtps, smtp+tls, smtp+ssl, consolemail, # filemail, memorymail, dummymail MAINMAIL=smtp+tls://senduser:accesspw@mta.example.com:587 # Search URL # This otional feature uses drivers from django-haystack # to find that needle. # Recognized schemes are: # elasticsearch, elasticsearch2, solr, whoosh, # xapian, simple SEARCHENGINE=solr://search.example.com:8983/solr/bigproject?q=*.*
- 您可以使用Env的所有功能,而无需任何类型声明。
- Env instance有用于直接变量转换的方法。 但是,我是舒尔,你想要更严格的值类型定义, 这给了我们更多的机会去寻找虫子。 此外,您始终可以使用对实例的直接调用来获取 默认情况下支持纯值。
- NB:下面是声明中命名变量类型的细微差别
并调用转换器。此列表中的类型:
str, bool, int, float, list, tuple, dict
你可以直接指向,因为它们是内置的 标识符是已知的解释器。类型
url, json, unicode, bytes
您只能通过引用他们的名字来使用,如下所示。 尽管如此,你可以引用所有类型的名称,包括 第一个列表,如果你记不清哪些是内置的。 ;—)
fromconfenvimportEnvEnv.filename='myappconf'kwl={# if you dont give default value, you can get an exception# from Env in case if variable not present in Env.data'DEBUG':(bool,False),# you can completely omit declaration of variables# with str value type - it is default#'SERVERNAME': (str, ),# or, which is equivalent, you can declare it as'SERVERNAME':('unicode',),# we'll comment out this declaration to demonstrate direct# convertor calls later#'WELCOMEMSG': ('bytes', 'Welcome !'),'PORT':(int,),'MAXAVGLOAD':(float,),# quoted type name also works'LISTENIP':('list',),# items for this tuple are of type str'ENDPOINTS':(tuple,),# default type of values str'USERACL':(dict,{'ALL':'deny'}),# default value type is str'COEFFICIENT':({'value':str,'cast':{'a':float,'b':int}},{'result':'NaN'}),'MENUEXTRA':('json',),'EXTLOGO':('url',),}env=Env(**kwl)# store values from environment in program configuration items#flagDebug=env('DEBUG')# assigned value bool FalsetextServer=env('SERVERNAME','Default service')# assigned value str 'Our service server'# in-line conversiontextWelcome=env.bytes('WELCOMEMSG','Welcome !')# assigned value str 'Добро пожаловать!'paramPort=env('PORT',default=4321)# assigned value int 1234paramNextPort=env.PORT+1# yes, all parameters are accessible as object attributeparamLoad=env('MAXAVGLOAD',default=10.0)# assigned value float 5.5paramListen=env('LISTENIP',default=['0.0.0.0'])# assigned value [ '127.0.0.1', '192.168.1.1', '10.0.0.1' ]progServices=env('ENDPOINTS',default=('start','stop'))# assigned value tuple('start', 'read', 'calculate', 'write', 'stop')paramACL=env('USERACL')# assigned value {'root': 'admin', 'jdoe': 'operator', 'john': 'user'}paramMUL=env('COEFFICIENT')# assigned value { 'a': 10.11, 'b': 5, 'result': 'unknown' }menuExtra=env('MENUEXTRA')# assigned value { 'ExtraItem1': { 'display': '&Cook Coffee', 'handler': 'cooker', 'allow': 'ALL' } }urlLogo=env('EXTLOGO')# assigned value ParseResult(scheme='http', netloc='image.bigproject.com', path='/biglogo.jpg', params='', query='', fragment='')config={}config.update(env)# you can use items() keys() and values() to access raw content of# env.datafork,vinenv:print('{}={}'.format(k,v))# access to all 'raw' values
django项目设置.pyEnv可以生成完整的配置 包括数据库、缓存、电子邮件和搜索功能的字典 将其转换为标准的django配置结构。在
# settings.py## This file can be "static", all changeable information# resides in ../myappconf.env and ../djangosite.env# get django-specific Envfromconfenv.djangoimportEnv# set path to .env's to directory, where manage.py resideEnv.filepath='..'# expecting, that you place example .env files 'myappconf'# and 'djangosite' in appropriate dirEnv.filename='myappconf'# django Env's version have place to hold variable names# to read database, cache, e-mail and search URLs default# values from# They are class members with this default values:#Env.defenv_db = 'DATABASE_URL'#Env.defenv_cache = 'CACHE_URL'#Env.defenv_email = 'EMAIL_URL'#Env.defenv_search = 'SEARCH_URL'# search for database config here by defaultEnv.defenv_db='MAINDB_URL'env=Env()# load additional configurationenv.readfile(filename='djangosite')DEBUG=env.bool('DEBUG',True)# use "untyped" variable as list to extend# django's defaultINSTALLED_APPS=[# ... standard django's list ...]+env.list('APPADD',[])# ...# databases configDATABASES={# config will be read from MAINDB_URL'default':env.db_url(),}CACHES={# this cache config will be read from CACHE_URL'default':env.cache_url(),# second cache config'quick':env.cache('MEMCACHE_URL'),}# This will require MAINMAIL key to be existent# in Env.data dictEMAIL_CONFIG=env.email_url('MAINMAIL')vars().update(EMAIL_CONFIG)# django's search extensionHAYSTACK_CONNECTIONS={'default':env.search_url('SEARCHENGINE','simple://'),}
安装
这个包可以从标准的Python包安装 来源pypi.org网站在
pip install django-confenv
学分
此代码是(c)Vitaly Protsko<;me@protsko.expert>;,位于GPLv3下。在
这项工作是基于django-environ(c)Daniele Faraglia 其中包括来自other authors的工作。在
- 项目
标签: