Commits

Ulysses Vilela committed 21073d1

Use the contents of a .env file to set the DJANGO_ENV for a project

  • Participants
  • Parent commits 697a1fb

Comments (0)

Files changed (1)

inoa/settings/settings_loader.py

 # -*- coding: utf-8 -*-
 
-def load(_globals, _locals, _file, SETTINGS_ROOT=None, ENV_VAR_NAME=None):
+
+def load(_globals, _locals, _file, SETTINGS_ROOT=None, ENV_VAR_NAME=None, ENV_FILE_NAME=None):
     """
     Loads the settings in the SETTINGS_ROOT directory, in alphabetical order.
     
     The files must use the format NN-<description>-<type>.py:
         NN: 2 digit number for ordering
         description: description +_+
-        type: if the env var given by ENV_VAR_NAME is set to <type>, the file will be loaded.
+        type: reads the env var ENV_VAR_NAME or the contents of ENV_FILE_NAME if the env var is not defined and loads
+              the files with that <type>.
               'all' files will always be loaded. 
     
     To use, make this your Django settings module (through the DJANGO_SETTINGS_MODULE environment variable)
                    Defaults to 'settings'. This folder does not have to be a Python package.
     ENV_VAR_NAME: the environment variable which will be used to distinguish between execution environments.
                   Defaults to 'DJANGO_ENV'.
-    
     """
-    
+
     import os
     import glob
-    
+
     SETTINGS_ROOT = SETTINGS_ROOT or 'settings'
     ENV_VAR_NAME = ENV_VAR_NAME or 'DJANGO_ENV'
-    
-    env = os.environ[ENV_VAR_NAME]
-    
+    ENV_FILE_NAME = ENV_FILE_NAME or '.env'
+
+    env = os.environ.get(ENV_VAR_NAME, None)
+    if not env:
+        f = open(ENV_FILE_NAME, 'r')
+        env = f.read().rstrip('\n')
+
     settings_all = glob.glob(os.path.join(os.path.dirname(_file), SETTINGS_ROOT, '*-all.py'))
     settings_env = glob.glob(os.path.join(os.path.dirname(_file), SETTINGS_ROOT, '*-%s.py' % env))
     s = settings_all + settings_env
     s.sort()
-    
+
     for f in s:
         execfile(os.path.abspath(f))
-    
+
     _locals.update(locals())
     _globals.update(globals())