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

Release v0.11.2 #492

Merged
merged 5 commits into from
Sep 1, 2023
Merged
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
11 changes: 10 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ 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.2`_ - 1-September-2023
-------------------------------
Fixed
+++++
- Revert "Add variable expansion." feature
due to `#490 <https://github.com/joke2k/django-environ/issues/490>`_.


`v0.11.1`_ - 30-August-2023
---------------------------
Fixed
Expand Down Expand Up @@ -380,6 +388,7 @@ Added
- Initial release.


.. _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
.. _v0.10.0: https://github.com/joke2k/django-environ/compare/v0.9.0...v0.10.0
Expand All @@ -396,4 +405,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
22 changes: 0 additions & 22 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,6 @@ And use it with ``settings.py`` as follows:
:start-after: -code-begin-
:end-before: -overview-

Variables can contain references to another variables: ``$VAR`` or ``${VAR}``.
Referenced variables are searched in the environment and within all definitions
in the ``.env`` file. References are checked for recursion (self-reference).
Exception is thrown if any reference results in infinite loop on any level
of recursion. Variable values are substituted similar to shell parameter
expansion. Example:

.. code-block:: shell

# shell
export POSTGRES_USERNAME='user' POSTGRES_PASSWORD='SECRET'

.. code-block:: shell

# .env
POSTGRES_HOSTNAME='example.com'
POSTGRES_DB='database'
DATABASE_URL="postgres://${POSTGRES_USERNAME}:${POSTGRES_PASSWORD}@${POSTGRES_HOSTNAME}:5432/${POSTGRES_DB}"

The value of ``DATABASE_URL`` variable will become
``postgres://user:SECRET@example.com:5432/database``.

The ``.env`` file should be specific to the environment and not checked into
version control, it is best practice documenting the ``.env`` file with an example.
For example, you can also add ``.env.dist`` with a template of your variables to
Expand Down
4 changes: 2 additions & 2 deletions environ/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
from .environ import *


__copyright__ = 'Copyright (C) 2013-2022 Daniele Faraglia'
__copyright__ = 'Copyright (C) 2013-2023 Daniele Faraglia'
"""The copyright notice of the package."""

__version__ = '0.11.1'
__version__ = '0.11.2'
"""The version of the package."""

__license__ = 'MIT'
Expand Down
66 changes: 12 additions & 54 deletions environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import os
import re
import sys
import threading
import warnings
from os.path import expandvars
from urllib.parse import (
parse_qs,
ParseResult,
Expand All @@ -42,9 +40,6 @@
Openable = (str, os.PathLike)
logger = logging.getLogger(__name__)

# Variables which values should not be expanded
NOT_EXPANDED = 'DJANGO_SECRET_KEY', 'CACHE_URL'


def _cast(value):
# Safely evaluate an expression node or a string containing a Python
Expand Down Expand Up @@ -194,11 +189,7 @@ class Env:
for s in ('', 's')]
CLOUDSQL = 'cloudsql'

VAR = re.compile(r'(?<!\\)\$\{?(?P<name>[A-Z_][0-9A-Z_]*)}?',
re.IGNORECASE)

def __init__(self, **scheme):
self._local = threading.local()
self.smart_cast = True
self.escape_proxy = False
self.prefix = ""
Expand Down Expand Up @@ -352,13 +343,9 @@ def path(self, var, default=NOTSET, **kwargs):
"""
return Path(self.get_value(var, default=default), **kwargs)

def get_value(self, var, cast=None, # pylint: disable=R0913
default=NOTSET, parse_default=False, add_prefix=True):
def get_value(self, var, cast=None, default=NOTSET, parse_default=False):
"""Return value for given environment variable.

- Expand variables referenced as ``$VAR`` or ``${VAR}``.
- Detect infinite recursion in expansion (self-reference).

:param str var:
Name of variable.
:param collections.abc.Callable or None cast:
Expand All @@ -367,33 +354,15 @@ def get_value(self, var, cast=None, # pylint: disable=R0913
If var not present in environ, return this instead.
:param bool parse_default:
Force to parse default.
:param bool add_prefix:
Whether to add prefix to variable name.
:returns: Value from environment or default (if set).
:rtype: typing.IO[typing.Any]
"""
var_name = f'{self.prefix}{var}' if add_prefix else var
if not hasattr(self._local, 'vars'):
self._local.vars = set()
if var_name in self._local.vars:
error_msg = f"Environment variable '{var_name}' recursively "\
"references itself (eventually)"
raise ImproperlyConfigured(error_msg)

self._local.vars.add(var_name)
try:
return self._get_value(
var_name, cast=cast, default=default,
parse_default=parse_default)
finally:
self._local.vars.remove(var_name)

def _get_value(self, var_name, cast=None, default=NOTSET,
parse_default=False):

logger.debug(
"get '%s' casted as '%s' with default '%s'",
var_name, cast, default)
var, cast, default)

var_name = f'{self.prefix}{var}'
if var_name in self.scheme:
var_info = self.scheme[var_name]

Expand All @@ -419,37 +388,26 @@ def _get_value(self, var_name, cast=None, default=NOTSET,
value = self.ENVIRON[var_name]
except KeyError as exc:
if default is self.NOTSET:
error_msg = f'Set the {var_name} environment variable'
error_msg = f'Set the {var} environment variable'
raise ImproperlyConfigured(error_msg) from exc

value = default

# Expand variables
if isinstance(value, (bytes, str)) and var_name not in NOT_EXPANDED:
def repl(match_):
return self.get_value(
match_.group('name'), cast=cast, default=default,
parse_default=parse_default, add_prefix=False)

is_bytes = isinstance(value, bytes)
if is_bytes:
value = value.decode('utf-8')
value = self.VAR.sub(repl, value)
value = expandvars(value)
if is_bytes:
value = value.encode('utf-8')

# 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):
value = value.lstrip(prefix)
value = self.get_value(value, cast=cast, default=default)

if self.escape_proxy and hasattr(value, 'replace'):
value = value.replace(escape, prefix)

# Smart casting
if self.smart_cast and cast is None and default is not None \
and not isinstance(default, NoValue):
cast = type(default)
if self.smart_cast:
if cast is None and default is not None and \
not isinstance(default, NoValue):
cast = type(default)

value = None if default is None and value == '' else value

Expand Down
27 changes: 0 additions & 27 deletions tests/test_expansion.py

This file was deleted.

9 changes: 0 additions & 9 deletions tests/test_expansion.txt

This file was deleted.