Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
Merge fc658c6 into e2ab604
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyseek committed Jul 7, 2016
2 parents e2ab604 + fc658c6 commit 720cafd
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
41 changes: 40 additions & 1 deletion README.rst
Expand Up @@ -26,9 +26,48 @@ Installation
::

$ pip install python-envcfg
$ pip freeze > requirements.txt # http://nvie.com/posts/pin-your-packages/


Supported Formats
-----------------

- ``import envcfg.raw.foo as config``:
Import each ``FOO_*`` environment variable as string.
- ``import envcfg.json.foo as config``:
Import each ``FOO_*`` environment variable as JSON body.
- ``import envcfg.smart.foo as config``:
Try to import each ``FOO_*`` environment variable as JSON body, if fail then import it as string.

There is an example table:

+----------------------+---------------------------+-----------------------+
| Environment Variable | Python Import Statement | Python Variable Value |
+======================+===========================+=======================+
| ``FOO_NAME=foo`` | ``envcfg.raw.foo.NAME`` | ``'foo'`` |
+----------------------+---------------------------+-----------------------+
| ``FOO_NAME="foo"`` | ``envcfg.raw.foo.NAME`` | ``'"foo"'`` |
+----------------------+---------------------------+-----------------------+
| ``FOO_NUM1=42`` | ``envcfg.raw.foo.NUM1`` | ``'42'`` |
+----------------------+---------------------------+-----------------------+
| ``FOO_NUM1="42"`` | ``envcfg.raw.foo.NUM1`` | ``'"42"'`` |
+----------------------+---------------------------+-----------------------+
| ``FOO_NAME=foo`` | ``envcfg.json.foo.NAME`` | *ImportError* |
+----------------------+---------------------------+-----------------------+
| ``FOO_NAME="foo"`` | ``envcfg.json.foo.NAME`` | ``'foo'`` |
+----------------------+---------------------------+-----------------------+
| ``FOO_NUM1=42`` | ``envcfg.json.foo.NUM1`` | ``42`` |
+----------------------+---------------------------+-----------------------+
| ``FOO_NUM1="42"`` | ``envcfg.json.foo.NUM1`` | ``'42'`` |
+----------------------+---------------------------+-----------------------+
| ``FOO_NAME=foo`` | ``envcfg.smart.foo.NAME`` | ``'foo'`` |
+----------------------+---------------------------+-----------------------+
| ``FOO_NAME="foo"`` | ``envcfg.smart.foo.NAME`` | ``'foo'`` |
+----------------------+---------------------------+-----------------------+
| ``FOO_NUM1=42`` | ``envcfg.smart.foo.NUM1`` | ``42`` |
+----------------------+---------------------------+-----------------------+
| ``FOO_NUM1="42"`` | ``envcfg.smart.foo.NUM1`` | ``'42'`` |
+----------------------+---------------------------+-----------------------+

Examples
--------

Expand Down
16 changes: 16 additions & 0 deletions envcfg/smart/__init__.py
@@ -0,0 +1,16 @@
from __future__ import absolute_import

from .._hook import import_hook


@import_hook(__name__)
def value_processor(name, raw_name, raw_value):
import json
try:
return json.loads(raw_value)
except ValueError:
return raw_value


del import_hook
del value_processor
37 changes: 37 additions & 0 deletions tests/test_smart.py
@@ -0,0 +1,37 @@
from pytest import raises


def test_success(environ):
environ['SUCHDOGE_BOOLEAN'] = 'true'
environ['SUCHDOGE_INTEGER'] = '42'
environ['SUCHDOGE_REAL'] = '42.42'
environ['SUCHDOGE_STRING'] = '"42"'
environ['SUCHDOGE_DICT'] = '{"value": 42}'
environ['SUCHDOGE_RAW_STR'] = 'foo'

from envcfg.smart.suchdoge import (
BOOLEAN,
INTEGER,
REAL,
STRING,
DICT,
RAW_STR,
)
assert BOOLEAN is True
assert INTEGER == 42
assert REAL == 42.42
assert STRING == '42'
assert DICT == {'value': 42}
assert RAW_STR == 'foo'


def test_failed(environ):
with raises(ImportError) as einfo:
import envcfg.smart._private_module # noqa
assert einfo.value.args[0].startswith(
'No module named envcfg.smart._private_module')

with raises(ImportError) as einfo:
import envcfg.smart.INVALID_NAME # noqa
assert einfo.value.args[0].startswith(
'No module named envcfg.smart.INVALID_NAME')
2 changes: 1 addition & 1 deletion tox.ini
@@ -1,5 +1,5 @@
[tox]
envlist = py27,py33,py34,pypy
envlist = py27,py33,py34,py35,pypy
[testenv]
deps =
pytest
Expand Down

0 comments on commit 720cafd

Please sign in to comment.