Skip to content

Commit

Permalink
Add interpolate argument to avoid resolving proxied values.
Browse files Browse the repository at this point in the history
The argument is already documented but not implemented yet.
Fixes joke2k#415
  • Loading branch information
David-Wobrock committed Sep 26, 2022
1 parent bd1e640 commit aaaa07c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ Proxy value
===========

Values that being with a ``$`` may be interpolated. Pass ``interpolate=True`` to
``environ.Env()`` to enable this feature:
``environ.Env()`` to enable this feature (``True`` by default):

.. code-block:: python
Expand Down
9 changes: 7 additions & 2 deletions environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,11 @@ class Env:
}
CLOUDSQL = 'cloudsql'

def __init__(self, **scheme):
def __init__(self, interpolate=True, **scheme):
self.smart_cast = True
self.escape_proxy = False
self.prefix = ""
self.interpolate = interpolate
self.scheme = scheme

def __call__(self, var, cast=None, default=NOTSET, parse_default=False):
Expand Down Expand Up @@ -395,7 +396,11 @@ def get_value(self, var, cast=None, default=NOTSET, parse_default=False):
# Resolve any proxied values
prefix = b'$' if isinstance(value, bytes) else '$'
escape = rb'\$' if isinstance(value, bytes) else r'\$'
if hasattr(value, 'startswith') and value.startswith(prefix):
if (
self.interpolate
and hasattr(value, 'startswith')
and value.startswith(prefix)
):
value = value.lstrip(prefix)
value = self.get_value(value, cast=cast, default=default)

Expand Down
4 changes: 4 additions & 0 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ def test_bool_true(self, value, variable):
def test_proxied_value(self):
assert self.env('PROXIED_VAR') == 'bar'

def test_not_interpolated_proxied_value(self):
env = Env(interpolate=False)
assert env('PROXIED_VAR') == '$STR_VAR'

def test_escaped_dollar_sign(self):
self.env.escape_proxy = True
assert self.env('ESCAPED_VAR') == '$baz'
Expand Down

0 comments on commit aaaa07c

Please sign in to comment.