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

Remove "filthy magic stack backtracking" in favor of BASE_DIR #252

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

birdcar
Copy link
Contributor

@birdcar birdcar commented Mar 19, 2020

The read_env documentation currently states:

If not given a path to a dotenv path, does filthy magic stack backtracking to find manage.py and then find the dotenv.

Based on #88, it appears that this behavior is rather fragile.

To improve this, I think the package should document that read_env expects you to provide a path. If one is not provided, it will attempt to use the BASE_DIR constant from the django settings module. If an ImportError is encountered while it attempts to do this, read_env will assume there's no .env file to be found, log an info message to that effect, and continue on.

fixes: #88

@sergeyklay
Copy link
Collaborator

PR was closed by mistake. Reopened it.

The read_env documentation currently states:

> If not given a path to a dotenv path, does filthy magic stack
> backtracking to find manage.py and then find the dotenv.

Based on joke2k#88, it appears that this behavior is
rather fragile.

To improve this, I think the package should document that read_env
expects you to provide a path. If one is not provided, it will
attempt to use the BASE_DIR constant from the django settings
module. If an ImportError is encountered while it attempts to do this,
read_env will assume there's no .env file to be found, log an info
message to that effect, and continue on.

fixes: joke2k#88
The django.conf.settings module could be successfully imported but the
BASE_DIR constant could not be defined, which would raise a NameError.

This small edit ensures that we're catching both potential failures.
@birdcar birdcar force-pushed the @nickcannariato-refactor-blank-read-env-import-and-docs branch from 279be8a to 05de258 Compare September 5, 2021 23:01
@sergeyklay sergeyklay self-assigned this Sep 6, 2021
@sergeyklay sergeyklay added the enhancement New feature or request label Sep 6, 2021
@coveralls
Copy link

coveralls commented Sep 6, 2021

Coverage Status

Coverage decreased (-0.2%) to 89.732% when pulling 05de258 on birdcar:@nickcannariato-refactor-blank-read-env-import-and-docs into d43ffd5 on joke2k:develop.

Copy link
Collaborator

@sergeyklay sergeyklay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some linting issues. Could you please take a look

@@ -18,15 +18,9 @@
import sys
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'sys' imported but unused

urlparse,
urlunparse,
)
from urllib.parse import ParseResult, parse_qs, unquote_plus, urlparse, urlunparse
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imported names are in the wrong order

Suggested change
from urllib.parse import ParseResult, parse_qs, unquote_plus, urlparse, urlunparse
from urllib.parse import parse_qs, ParseResult, unquote_plus, urlparse, urlunparse

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this line too long (82 > 79 characters)


from .compat import DJANGO_POSTGRES, ImproperlyConfigured, json, REDIS_DRIVER
from .compat import DJANGO_POSTGRES, REDIS_DRIVER, ImproperlyConfigured, json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imported names are in the wrong order

Suggested change
from .compat import DJANGO_POSTGRES, REDIS_DRIVER, ImproperlyConfigured, json
from .compat import DJANGO_POSTGRES, ImproperlyConfigured, json, REDIS_DRIVER

(True, True)
>>> root('dev', 'not_existing_dir', required=True)
Traceback (most recent call last):
environ.environ.ImproperlyConfigured: Create required path: /home/not_existing_dir
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E501 line too long (90 > 79 characters)

>>> public = root.path('public')
>>> public, public.root, public('styles')
(<Path:/home/public>, '/home/public', '/home/public/styles')
>>> assets, scripts = public.path('assets'), public.path('assets', 'scripts')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (85 > 79 characters)

>>> assets.root, scripts.root
('/home/public/assets', '/home/public/assets/scripts')
>>> assets + 'styles', str(assets + 'styles'), ~assets
(<Path:/home/public/assets/styles>, '/home/public/assets/styles', <Path:/home/public>)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (94 > 79 characters)

try:
from django.conf import settings
env_file = os.path.join(settings.BASE_DIR, '.env')
except (ImportError, NameError):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about AttributeError?

@sergeyklay sergeyklay deleted the branch joke2k:develop October 19, 2021 21:45
@sergeyklay sergeyklay closed this Oct 19, 2021
@sergeyklay sergeyklay reopened this Oct 20, 2021
@jayvdb
Copy link

jayvdb commented Apr 13, 2022

@sergeyklay is it possible to change the merge target of this PR to be main ?

@sergeyklay
Copy link
Collaborator

sergeyklay commented Jun 13, 2022

@jayvdb Usually we merge changes to the develop branch. Then, before a release, we merge the develop branch into the main and make a release from the main branch.

@jayvdb
Copy link

jayvdb commented Jun 13, 2022

No worries. At the time I wrote that, there was no develop branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve documentation for read_env()
4 participants