Skip to content

Commit

Permalink
lazily imported dotenv file
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Oct 11, 2018
1 parent d847e35 commit defe2af
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions masonite/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import os
from pathlib import Path

from dotenv import load_dotenv


class LoadEnvironment:
"""This class is used for loading the environment from .env and .env.* files."""
Expand All @@ -17,12 +15,15 @@ def __init__(self, env=None, override=True, only=None):
override {bool} -- Whether or not the environment variables found should overwrite existing ones. (default: {False})
only {string} -- If this is set then it will only load that environment. (default: {None})
"""
from dotenv import load_dotenv
self.env = load_dotenv

if only:
self._load_environment(only, override=override)
return

env_path = str(Path('.') / '.env')
load_dotenv(env_path, override=override)
self.env(env_path, override=override)

if os.environ.get('APP_ENV'):
self._load_environment(os.environ.get(
Expand All @@ -40,7 +41,7 @@ def _load_environment(self, env, override=False):
override {bool} -- Whether the environment file should overwrite existing environment keys. (default: {False})
"""
env_path = str(Path('.') / '.env.{}'.format(env))
load_dotenv(dotenv_path=env_path, override=override)
self.env(dotenv_path=env_path, override=override)


def env(value, default=None):
Expand Down

0 comments on commit defe2af

Please sign in to comment.