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 ea7f0eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
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 ea7f0eb

Please sign in to comment.