Skip to content

Commit

Permalink
Merge pull request #66 from willkg/fast-setup
Browse files Browse the repository at this point in the history
add fast setup
  • Loading branch information
willkg committed Apr 23, 2018
2 parents 72667ab + e4c5d1d commit 07ffdfd
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
32 changes: 26 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,28 @@ Most other libraries I looked at had one or more of the following issues:
Quick start
===========

Example
-------
Fast start example
------------------

You have an app and want it to look for configuration in an ``.env`` file then
the environment. You can do this::

from everett.manager import ConfigManager

config = ConfigManager.basic_config()


Then you can use it like this::

debug_mode = config('debug', parser=bool)


When you outgrow that or need different variations of it, you can change
that to creating a ``ConfigManager`` from scratch.


More control example
--------------------

We have an app and want to pull configuration from an INI file stored in
a place specified by ``MYAPP_INI`` in the environment, ``~/.myapp.ini``,
Expand Down Expand Up @@ -223,16 +243,16 @@ Components support subclassing, mixins and all that, too.
Install
=======

From PyPI
---------
Install from PyPI
-----------------

Run::

$ pip install everett


For hacking
-----------
Install for hacking
-------------------

Run::

Expand Down
44 changes: 43 additions & 1 deletion everett/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def get_key_from_envs(envs, key, namespace=None):
"""Return the value of a key from the given dict respecting namespaces.
Data can also be a list of data dicts.
"""
# if it barks like a dict, make it a list
# have to use `get` since dicts and lists
Expand Down Expand Up @@ -799,6 +800,47 @@ def __init__(self, environments, doc='', with_override=True):
self.envs = environments
self.doc = doc

@classmethod
def basic_config(cls, env_file='.env'):
"""Returns a basic ConfigManager
This sets up a ConfigManager that will look for configuration in
this order:
1. environment
2. specified ``env_file`` defaulting to ``.env``
This is for a fast one-line opinionated setup.
Example::
from everett.manager import ConfigManager
config = ConfigManager.basic_config()
This is shorthand for::
config = ConfigManager(
environments=[
ConfigOSEnv(),
ConfigEnvFileEnv(['.env'])
]
)
:arg env_file: the name of the env file to use
:returns: a :py:class:`everett.manager.ConfigManager`
"""
return cls(
environments=[
ConfigOSEnv(),
ConfigEnvFileEnv([env_file])
]
)

@classmethod
def from_dict(cls, dict_config):
"""Creates a ConfigManager with specified configuration as a Python dict
Expand All @@ -818,7 +860,7 @@ def from_dict(cls, dict_config):
.. versionadded:: 0.3
"""
return ConfigManager([ConfigDictEnv(dict_config)])
return cls([ConfigDictEnv(dict_config)])

def __call__(self, key, namespace=None, default=NO_VALUE,
alternate_keys=NO_VALUE, doc='', parser=str, raise_error=True,
Expand Down
2 changes: 1 addition & 1 deletion tests/data/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# dude
# this is a comment
LOGLEVEL=walter
DEBUG=True
YOURE_NOT_A=golfer
Expand Down
17 changes: 16 additions & 1 deletion tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
ConfigEnvFileEnv,
ConfigIniEnv,
ConfigManager,
ConfigObjEnv,
ConfigOSEnv,
ConfigObjEnv,
ListOf,
config_override,
get_key_from_envs,
Expand Down Expand Up @@ -464,6 +464,21 @@ def test_config_from_dict():
assert config('FOO', raise_error=False) == 'bar'


def test_basic_config(datadir):
os.environ['EVERETT_BASIC_CONFIG_TEST'] = 'foo'
env_filename = os.path.join(datadir, '.env')
config = ConfigManager.basic_config(env_filename)

# This doesn't exist in either the environment or the env file
assert config('FOO', raise_error=False) is NO_VALUE

# This exists in the environment
assert config('EVERETT_BASIC_CONFIG_TEST') == 'foo'

# This exists in the env file
assert config('LOGLEVEL') == 'walter'


def test_config_manager_doc():
config = ConfigManager(
[
Expand Down

0 comments on commit 07ffdfd

Please sign in to comment.