Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interpolate option #495

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ All notable changes to this project will be documented in this file.
The format is inspired by `Keep a Changelog <https://keepachangelog.com/en/1.0.0/>`_
and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0.html>`_.

`v0.11.3`_ - 0-Undefined-2023
`v0.12.0`_ - 0-Undefined-2023
-----------------------------
Added
+++++
- Added support for ``interpolate`` parameter
`#495 <https://github.com/joke2k/django-environ/pull/495>`_.
This implies a breaking change on the ``interpolate`` parameter.
Before this change, ``Env(interpolate=True)`` and ``Env(interpolate=False)`` had the same
behaviour. After the change, ``Env(interpolate=False)`` won't substitute values anymore
that start with ``$``.

Changed
+++++++
- Disabled inline comments handling by default due to potential side effects.
Expand All @@ -15,7 +24,6 @@ Changed
`#499 <https://github.com/joke2k/django-environ/issues/499>`_.



`v0.11.2`_ - 1-September-2023
-----------------------------
Fixed
Expand Down Expand Up @@ -399,7 +407,7 @@ Added
- Initial release.


.. _v0.11.3: https://github.com/joke2k/django-environ/compare/v0.11.2...v0.11.3
.. _v0.12.0: https://github.com/joke2k/django-environ/compare/v0.11.2...v0.12.0
.. _v0.11.2: https://github.com/joke2k/django-environ/compare/v0.11.1...v0.11.2
.. _v0.11.1: https://github.com/joke2k/django-environ/compare/v0.11.0...v0.11.1
.. _v0.11.0: https://github.com/joke2k/django-environ/compare/v0.10.0...v0.11.0
Expand All @@ -417,4 +425,4 @@ Added
.. _v0.4.1: https://github.com/joke2k/django-environ/compare/v0.4...v0.4.1
.. _v0.4: https://github.com/joke2k/django-environ/compare/v0.3.1...v0.4
.. _v0.3.1: https://github.com/joke2k/django-environ/compare/v0.3...v0.3.1
.. _v0.3: https://github.com/joke2k/django-environ/compare/v0.2.1...v0.3
.. _v0.3: https://github.com/joke2k/django-environ/compare/v0.2.1...v0.3
4 changes: 2 additions & 2 deletions docs/tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ The following example demonstrates the above:
Proxy value
===========

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

.. code-block:: python

Expand Down
6 changes: 4 additions & 2 deletions environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -396,7 +397,8 @@ 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
1 change: 1 addition & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def generate_data(cls):
BOOL_FALSE_STRING_LIKE_BOOL='False',
BOOL_FALSE_BOOL=False,
PROXIED_VAR='$STR_VAR',
DOLLAR_VAR_1='SOME_VALUE$S3CR3TK3Y@HELLO',
ESCAPED_VAR=r'\$baz',
INT_LIST='42,33',
INT_TUPLE='(42,33)',
Expand Down
11 changes: 11 additions & 0 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ 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_1') == 'SOME_VALUE$S3CR3TK3Y@HELLO'

def test_dollar_sign_not_interpolated(self):
env = Env(interpolate=False)
assert env('DOLLAR_VAR_1') == 'SOME_VALUE$S3CR3TK3Y@HELLO'

def test_escaped_dollar_sign(self):
self.env.escape_proxy = True
assert self.env('ESCAPED_VAR') == '$baz'
Expand Down
3 changes: 3 additions & 0 deletions tests/test_env.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ FLOAT_STRANGE_VAR1=123,420,333.3
FLOAT_STRANGE_VAR2=123.420.333,3
FLOAT_NEGATIVE_VAR=-1.0
PROXIED_VAR=$STR_VAR
DOLLAR_VAR_1=SOME_VALUE$S3CR3TK3Y@HELLO
DOLLAR_VAR_2='SOME_VALUE$S3CR3TK3Y@HELLO'
DOLLAR_VAR_3="SOME_VALUE$S3CR3TK3Y@HELLO"
ESCAPED_VAR=\$baz
EMPTY_LIST=
EMPTY_INT_VAR=
Expand Down