Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Test on Django 5.0
  Bump actions/setup-python from 4.7.0 to 4.7.1
  Bump actions/checkout from 4.0.0 to 4.1.1
  Add Python 3.12 to the test matrix
  Make inline comments handling optional and disabled by default
  Bump actions/checkout from 3.6.0 to 4.0.0
  Change version: v0.12.0 -> v0.11.2
  Change version: v0.12.0 -> v0.11.2
  Update change log
  Revert "Add variable expansion (fix joke2k#421)"
  Bump version
  • Loading branch information
fdemmer committed Mar 17, 2024
2 parents df301b6 + a1113e4 commit 7d582be
Show file tree
Hide file tree
Showing 16 changed files with 192 additions and 44 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3.6.0
uses: actions/checkout@v4.1.1

- name: Set up Python 3.10
uses: actions/[email protected].0
uses: actions/[email protected].1
with:
python-version: '3.10'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
pip install tox tox-gh-actions setuptools
- name: Check MANIFEST.in for completeness
run: tox -e manifest
Expand Down Expand Up @@ -64,10 +64,10 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3.6.0
uses: actions/checkout@v4.1.1

- name: Set up Python 3.10
uses: actions/[email protected].0
uses: actions/[email protected].1
with:
python-version: '3.10'

Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
- '3.9'
- '3.10'
- '3.11'
- '3.12'
- 'pypy-3.7'
os: [ ubuntu-latest, macos-latest, windows-latest ]

Expand All @@ -66,19 +67,19 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3.6.0
uses: actions/checkout@v4.1.1
with:
fetch-depth: 5

- name: Set up Python ${{ matrix.python }}
uses: actions/[email protected].0
uses: actions/[email protected].1
with:
python-version: ${{ matrix.python }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install tox tox-gh-actions
python -m pip install tox tox-gh-actions setuptools
- name: Setuptools self-test
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3.6.0
uses: actions/checkout@v4.1.1

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/cs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3.6.0
uses: actions/checkout@v4.1.1

- name: Set up Python 3.10
uses: actions/[email protected].0
uses: actions/[email protected].1
with:
python-version: '3.10'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
pip install tox tox-gh-actions setuptools
- name: Lint with tox
run: tox -e lint
6 changes: 3 additions & 3 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3.6.0
uses: actions/checkout@v4.1.1

- name: Set up Python 3.10
uses: actions/[email protected].0
uses: actions/[email protected].1
with:
python-version: '3.10'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
pip install tox tox-gh-actions setuptools
- name: Check external links in the package documentation
run: tox -e linkcheck
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is part of the django-environ.
#
# Copyright (c) 2021, Serghei Iakovlev <[email protected]>
# Copyright (c) 2021-2023, Serghei Iakovlev <[email protected]>
# Copyright (c) 2013-2021, Daniele Faraglia <[email protected]>
#
# For the full copyright and license information, please view
Expand Down
20 changes: 16 additions & 4 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ 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
-----------------------------
Changed
+++++++
- Formally support Python 3.12.
- Disabled inline comments handling by default due to potential side effects.
While the feature itself is useful, the project's philosophy dictates that
it should not be enabled by default for all users
`#499 <https://github.com/joke2k/django-environ/issues/499>`_.


`v0.11.2`_ - 1-September-2023
-------------------------------
-----------------------------
Fixed
+++++
- Revert "Add variable expansion." feature
Expand All @@ -31,7 +42,7 @@ Added
`#463 <https://github.com/joke2k/django-environ/pull/463>`_.
- Added variable expansion
`#468 <https://github.com/joke2k/django-environ/pull/468>`_.
- Added capability to handle comments after #, after quoted values,
- Added capability to handle comments after ``#``, after quoted values,
like ``KEY= 'part1 # part2' # comment``
`#475 <https://github.com/joke2k/django-environ/pull/475>`_.
- Added support for ``interpolate`` parameter
Expand Down Expand Up @@ -388,7 +399,8 @@ Added
- Initial release.


.. _v0.11.2: https://github.com/joke2k/django-environ/compare/v0.11.1...v0.11.2
==== BASE ====
==== BASE ====
.. _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 @@ -405,4 +417,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
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2021, Serghei Iakovlev <[email protected]>
Copyright (c) 2021-2023, Serghei Iakovlev <[email protected]>
Copyright (c) 2013-2021, Daniele Faraglia <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ the code on `GitHub <https://github.com/joke2k/django-environ>`_,
and the latest release on `PyPI <https://pypi.org/project/django-environ/>`_.

It’s rigorously tested on Python 3.6+, and officially supports
Django 1.11, 2.2, 3.0, 3.1, 3.2, 4.0, 4.1 and 4.2.
Django 1.11, 2.2, 3.0, 3.1, 3.2, 4.0, 4.1, 4.2, and 5.0.

If you'd like to contribute to ``django-environ`` you're most welcome!

Expand Down
65 changes: 65 additions & 0 deletions docs/tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,71 @@
Tips
====

Handling Inline Comments in .env Files
======================================

``django-environ`` provides an optional feature to parse inline comments in ``.env``
files. This is controlled by the ``parse_comments`` parameter in the ``read_env``
method.

Modes
-----

- **Enabled (``parse_comments=True``)**: Inline comments starting with ``#`` will be ignored.
- **Disabled (``parse_comments=False``)**: The entire line, including comments, will be read as the value.
- **Default**: The behavior is the same as when ``parse_comments=False``.

Side Effects
------------

While this feature can be useful for adding context to your ``.env`` files,
it can introduce unexpected behavior. For example, if your value includes
a ``#`` symbol, it will be truncated when ``parse_comments=True``.

Why Disabled by Default?
------------------------

In line with the project's philosophy of being explicit and avoiding unexpected behavior,
this feature is disabled by default. If you understand the implications and find the feature
useful, you can enable it explicitly.

Example
-------

Here is an example demonstrating the different modes of handling inline comments.

**.env file contents**:

.. code-block:: shell
# .env file contents
BOOL_TRUE_WITH_COMMENT=True # This is a comment
STR_WITH_HASH=foo#bar # This is also a comment
**Python code**:

.. code-block:: python
import environ
# Using parse_comments=True
env = environ.Env()
env.read_env(parse_comments=True)
print(env('BOOL_TRUE_WITH_COMMENT')) # Output: True
print(env('STR_WITH_HASH')) # Output: foo
# Using parse_comments=False
env = environ.Env()
env.read_env(parse_comments=False)
print(env('BOOL_TRUE_WITH_COMMENT')) # Output: True # This is a comment
print(env('STR_WITH_HASH')) # Output: foo#bar # This is also a comment
# Using default behavior
env = environ.Env()
env.read_env()
print(env('BOOL_TRUE_WITH_COMMENT')) # Output: True # This is a comment
print(env('STR_WITH_HASH')) # Output: foo#bar # This is also a comment
Docker-style file based variables
=================================
Expand Down
2 changes: 1 addition & 1 deletion environ/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
__copyright__ = 'Copyright (C) 2013-2023 Daniele Faraglia'
"""The copyright notice of the package."""

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

__license__ = 'MIT'
Expand Down
46 changes: 33 additions & 13 deletions environ/environ.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is part of the django-environ.
#
# Copyright (c) 2021-2022, Serghei Iakovlev <[email protected]>
# Copyright (c) 2021-2023, Serghei Iakovlev <[email protected]>
# Copyright (c) 2013-2021, Daniele Faraglia <[email protected]>
#
# For the full copyright and license information, please view
Expand Down Expand Up @@ -862,8 +862,8 @@ def search_url_config(cls, url, engine=None):
return config

@classmethod
def read_env(cls, env_file=None, overwrite=False, encoding='utf8',
**overrides):
def read_env(cls, env_file=None, overwrite=False, parse_comments=False,
encoding='utf8', **overrides):
r"""Read a .env file into os.environ.
If not given a path to a dotenv path, does filthy magic stack
Expand All @@ -883,6 +883,8 @@ def read_env(cls, env_file=None, overwrite=False, encoding='utf8',
the Django settings module from the Django project root.
:param overwrite: ``overwrite=True`` will force an overwrite of
existing environment variables.
:param parse_comments: Determines whether to recognize and ignore
inline comments in the .env file. Default is False.
:param encoding: The encoding to use when reading the environment file.
:param \**overrides: Any additional keyword arguments provided directly
to read_env will be added to the environment. If the key matches an
Expand Down Expand Up @@ -927,22 +929,40 @@ def _keep_escaped_format_characters(match):
for line in content.splitlines():
m1 = re.match(r'\A(?:export )?([A-Za-z_0-9]+)=(.*)\Z', line)
if m1:

# Example:
#
# line: KEY_499=abc#def
# key: KEY_499
# val: abc#def
key, val = m1.group(1), m1.group(2)
# Look for value in quotes, ignore post-# comments
# (outside quotes)
m2 = re.match(r"\A\s*'(?<!\\)(.*)'\s*(#.*\s*)?\Z", val)
if m2:
val = m2.group(1)

if not parse_comments:
# Default behavior
#
# Look for value in single quotes
m2 = re.match(r"\A'(.*)'\Z", val)
if m2:
val = m2.group(1)
else:
# For no quotes, find value, ignore comments
# after the first #
m2a = re.match(r"\A(.*?)(#.*\s*)?\Z", val)
if m2a:
val = m2a.group(1)
# Ignore post-# comments (outside quotes).
# Something like ['val' # comment] becomes ['val'].
m2 = re.match(r"\A\s*'(?<!\\)(.*)'\s*(#.*\s*)?\Z", val)
if m2:
val = m2.group(1)
else:
# For no quotes, find value, ignore comments
# after the first #
m2a = re.match(r"\A(.*?)(#.*\s*)?\Z", val)
if m2a:
val = m2a.group(1)

# Look for value in double quotes
m3 = re.match(r'\A"(.*)"\Z', val)
if m3:
val = re.sub(r'\\(.)', _keep_escaped_format_characters,
m3.group(1))

overrides[key] = str(val)
elif not line or line.startswith('#'):
# ignore warnings for empty line-breaks or comments
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def get_version_string():
'Framework :: Django :: 4.0',
'Framework :: Django :: 4.1',
'Framework :: Django :: 4.2',
'Framework :: Django :: 5.0',

'Operating System :: OS Independent',

Expand All @@ -159,6 +160,7 @@ def get_version_string():
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',

Expand Down
Loading

0 comments on commit 7d582be

Please sign in to comment.