From dbaa23a4066426cce93e3d2a290927f1df167410 Mon Sep 17 00:00:00 2001 From: David Wobrock Date: Mon, 26 Sep 2022 15:19:47 +0200 Subject: [PATCH] Add interpolate argument to avoid resolving proxied values. The argument is already documented but not implemented yet. Fixes #415 --- CHANGELOG.rst | 7 +++++++ docs/tips.rst | 2 +- environ/environ.py | 5 +++-- tests/test_env.py | 4 ++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 57c4c8dd..a17a8f27 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is inspired by `Keep a Changelog `_ and this project adheres to `Semantic Versioning `_. +`v0.x.y`_ - Unreleased +------------------------------- +Added ++++++ +- Added support for ``interpolate`` parameter + `#415 `_. + `v0.11.2`_ - 1-September-2023 ------------------------------- Fixed diff --git a/docs/tips.rst b/docs/tips.rst index 20b8c3e1..ab59f691 100644 --- a/docs/tips.rst +++ b/docs/tips.rst @@ -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 diff --git a/environ/environ.py b/environ/environ.py index f74884be..2101b018 100644 --- a/environ/environ.py +++ b/environ/environ.py @@ -189,10 +189,11 @@ class Env: for s in ('', 's')] 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): @@ -396,7 +397,7 @@ 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) diff --git a/tests/test_env.py b/tests/test_env.py index daecb8f5..982e6c74 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -134,6 +134,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_dollar_sign(self): assert self.env('DOLLAR_VAR') == 'SOME_VALUE$S3CR3TK3Y@HELLO'