diff --git a/README.rst b/README.rst index 40c97ad..f4c5aac 100644 --- a/README.rst +++ b/README.rst @@ -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 -------- diff --git a/envcfg/smart/__init__.py b/envcfg/smart/__init__.py new file mode 100644 index 0000000..5aae86f --- /dev/null +++ b/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 diff --git a/tests/test_smart.py b/tests/test_smart.py new file mode 100644 index 0000000..b715a1d --- /dev/null +++ b/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') diff --git a/tox.ini b/tox.ini index ce727ce..89f42a9 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py27,py33,py34,pypy +envlist = py27,py33,py34,py35,pypy [testenv] deps = pytest