Skip to content

Commit

Permalink
Merge pull request #1 from secretescapes/make-proxying-optional
Browse files Browse the repository at this point in the history
Make proxying optional
  • Loading branch information
Neil Lyons authored Sep 22, 2017
2 parents 628ed38 + 8f8a58d commit 423a076
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
21 changes: 21 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,27 @@ This has the effect of four slashes being present for an absolute
file path: sqlite:////full/path/to/your/database/file.sqlite.


Interpolate environment variables
---------------------------------

Values that being with a ``$`` will be interpolated. Pass ``interpolate=False`` to ``environ.Env()`` to disable this feature.

.. code-block:: bash
PROXIED_VAR=$STR_VAR
STR_VAR=bar
.. code-block:: python
env = environ.Env()
env.str('PROXIED_VAR')
>>> 'bar'
env = environ.Env(interpolate=False)
env.str('PROXIED_VAR')
>>> '$STR_VAR'
Tests
=====

Expand Down
5 changes: 3 additions & 2 deletions environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ class Env(object):
"simple": "haystack.backends.simple_backend.SimpleEngine",
}

def __init__(self, **scheme):
def __init__(self, interpolate=True, **scheme):
self.interpolate = interpolate
self.scheme = scheme

def __call__(self, var, cast=None, default=NOTSET, parse_default=False):
Expand Down Expand Up @@ -277,7 +278,7 @@ def get_value(self, var, cast=None, default=NOTSET, parse_default=False):
value = default

# Resolve any proxied values
if hasattr(value, 'startswith') and value.startswith('$'):
if self.interpolate and hasattr(value, 'startswith') and value.startswith('$'):
value = value.lstrip('$')
value = self.get_value(value, cast=cast, default=default)

Expand Down
4 changes: 4 additions & 0 deletions environ/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ def test_bool_false(self):
def test_proxied_value(self):
self.assertTypeAndValue(str, 'bar', self.env('PROXIED_VAR'))

def test_proxied_value_interpolate_false(self):
env = Env(interpolate=False)
self.assertTypeAndValue(str, '$STR_VAR', env('PROXIED_VAR'))

def test_int_list(self):
self.assertTypeAndValue(list, [42, 33], self.env('INT_LIST', cast=[int]))
self.assertTypeAndValue(list, [42, 33], self.env.list('INT_LIST', int))
Expand Down

0 comments on commit 423a076

Please sign in to comment.