From d609e79fb5717b6b17c9cf674e173a8ab20a80e2 Mon Sep 17 00:00:00 2001 From: Jiangge Zhang Date: Thu, 7 Jul 2016 16:50:37 +0800 Subject: [PATCH 1/3] Add "smart" format support and test it. --- envcfg/smart/__init__.py | 16 ++++++++++++++++ tests/test_smart.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 envcfg/smart/__init__.py create mode 100644 tests/test_smart.py 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') From 6a91687d8a0212d14aac752116358b3fc82c505e Mon Sep 17 00:00:00 2001 From: Jiangge Zhang Date: Thu, 7 Jul 2016 16:52:20 +0800 Subject: [PATCH 2/3] We should be compatible with Python 3.5 --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From fc658c6db6a93e504335d697b09fd8215749fd00 Mon Sep 17 00:00:00 2001 From: Jiangge Zhang Date: Thu, 7 Jul 2016 16:53:27 +0800 Subject: [PATCH 3/3] Add a table to show available formats. --- README.rst | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) 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 --------