envconfig2 is a simple library to make it easier to read config settings from
the environment. It is modeled loosely after Kelsey Hightower's
[
envconfig](
https://github.com/kelseyhightower/envconfig) for Go.
>>> os.environ['DB_HOST'] = 'localhost'
>>> os.environ['DB_PORT'] = '5432'
>>> envconfig2.string('DB_HOST')
>>> envconfig2.integer('DB_PORT')
>>> os.environ['PORT'] = '80'
>>> envconfig2.integer('PORT')
>>> os.environ['DEBUG'] = 'true'
>>> envconfig2.boolean('DEBUG')
>>> os.environ['USERS'] = 'user1,user2,user3'
>>> envconfig2.list('USERS')
['user1', 'user2', 'user3']
>>> os.environ['JSON'] = '{"debug": true}'
>>> envconfig2.json('JSON')
By default envconfig2 will just verify that the given value can be parsed by urlparse.urlparse
>>> os.environ['URL'] = 'https://example.com'
>>> envconfig2.url('URL')
If you would rather get a urlparse.Parse result you can tell envconfig2 to expand the url.
>>> os.environ['URL'] = 'https://example.com'
>>> envconfig2.url('URL', expand=True)
ParseResult(scheme='https', netloc='example.com', path='', params='', query='', fragment='')
>>> envconfig2.string('API_KEY', required=True)
ValueError: required environmnet variable API_KEY not set
>>> envconfig2.string('DB_HOST', default='localhost')
Sometimes it's useful to require a prefix on each variable as well. This can
be done by setting
`prefix` on the module itself. envconfig2 will then look
for
`{prefix}_{name}` as well as
`name`.
>>> os.environ['MYAPP_DEBUG'] = 'True'
>>> envconfig2.prefix = 'MYAPP'
>>> envconfig2.boolean('DEBUG')
You can also set
`prefix_required` to only look for
`{prefix}_{name}`.
>>> os.environ['DEBUG'] = 'True'
>>> envconfig2.prefix = 'MYAPP'
>>> envconfig2.boolean('DEBUG')
>>> envconfig2.prefix_required = True
>>> envconfig2.boolean('DEBUG')
Also, if a prefix is set, a variable having the prefix will be used before the
>>> os.environ['MYAPP_USERNAME'] = 'user1'
>>> os.environ['USERNAME'] = 'user2'
>>> envconfig2.prefix = 'MYAPP'
>>> envconfig2.string('USERNAME')
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA