mirror of
https://github.com/jazzband/django-configurations.git
synced 2026-03-16 22:20:27 +00:00
refactoring of DotConfiguration
This commit is contained in:
parent
f5c8b49842
commit
27eb748d68
6 changed files with 63 additions and 41 deletions
|
|
@ -1,9 +1,9 @@
|
|||
# flake8: noqa
|
||||
from .base import Settings, Configuration
|
||||
from .base import Settings, Configuration, DotConfiguration
|
||||
from .decorators import pristinemethod
|
||||
|
||||
__version__ = '0.8'
|
||||
__all__ = ['Configuration', 'pristinemethod', 'Settings']
|
||||
__all__ = ['Configuration', 'DotConfiguration', 'pristinemethod', 'Settings']
|
||||
|
||||
|
||||
def load_ipython_extension(ipython):
|
||||
|
|
|
|||
|
|
@ -41,39 +41,6 @@ class ConfigurationBase(type):
|
|||
return "<Configuration '{0}.{1}'>".format(self.__module__,
|
||||
self.__name__)
|
||||
|
||||
@staticmethod
|
||||
def read_env(env_file='.env', **overrides):
|
||||
"""
|
||||
Pulled from Honcho code with minor updates, reads local default
|
||||
environment variables from a .env file located in the project root
|
||||
directory.
|
||||
|
||||
http://www.wellfireinteractive.com/blog/easier-12-factor-django/
|
||||
https://gist.github.com/bennylope/2999704
|
||||
"""
|
||||
import re
|
||||
import os
|
||||
|
||||
with open(env_file) as f:
|
||||
content = f.read()
|
||||
|
||||
for line in content.splitlines():
|
||||
m1 = re.match(r'\A([A-Za-z_0-9]+)=(.*)\Z', line)
|
||||
if not m1:
|
||||
continue
|
||||
key, val = m1.group(1), m1.group(2)
|
||||
m2 = re.match(r"\A'(.*)'\Z", val)
|
||||
if m2:
|
||||
val = m2.group(1)
|
||||
m3 = re.match(r'\A"(.*)"\Z', val)
|
||||
if m3:
|
||||
val = re.sub(r'\\(.)', r'\1', m3.group(1))
|
||||
os.environ.setdefault(key, val)
|
||||
|
||||
# set defaults
|
||||
for key, value in overrides.items():
|
||||
os.environ.setdefault(key, value)
|
||||
|
||||
|
||||
class Configuration(six.with_metaclass(ConfigurationBase)):
|
||||
"""
|
||||
|
|
@ -114,6 +81,53 @@ class Configuration(six.with_metaclass(ConfigurationBase)):
|
|||
setup_value(cls, name, value)
|
||||
|
||||
|
||||
class DotConfiguration(Configuration):
|
||||
|
||||
DOT_ENV = None
|
||||
DOT_ENV_LOADED = False
|
||||
|
||||
@classmethod
|
||||
def pre_setup(cls):
|
||||
Configuration.pre_setup()
|
||||
if not cls.DOT_ENV_LOADED and cls.DOT_ENV:
|
||||
cls.read_env(cls.DOT_ENV)
|
||||
|
||||
@classmethod
|
||||
def read_env(cls, env_file='.env', **overrides):
|
||||
"""
|
||||
Pulled from Honcho code with minor updates, reads local default
|
||||
environment variables from a .env file located in the project root
|
||||
or provided directory.
|
||||
|
||||
http://www.wellfireinteractive.com/blog/easier-12-factor-django/
|
||||
https://gist.github.com/bennylope/2999704
|
||||
"""
|
||||
import re
|
||||
import os
|
||||
|
||||
with open(env_file) as f:
|
||||
content = f.read()
|
||||
|
||||
for line in content.splitlines():
|
||||
m1 = re.match(r'\A([A-Za-z_0-9]+)=(.*)\Z', line)
|
||||
if not m1:
|
||||
continue
|
||||
key, val = m1.group(1), m1.group(2)
|
||||
m2 = re.match(r"\A'(.*)'\Z", val)
|
||||
if m2:
|
||||
val = m2.group(1)
|
||||
m3 = re.match(r'\A"(.*)"\Z', val)
|
||||
if m3:
|
||||
val = re.sub(r'\\(.)', r'\1', m3.group(1))
|
||||
os.environ.setdefault(key, val)
|
||||
|
||||
# set defaults
|
||||
for key, value in overrides.items():
|
||||
os.environ.setdefault(key, value)
|
||||
|
||||
cls.DOT_ENV_LOADED = True
|
||||
|
||||
|
||||
class Settings(Configuration):
|
||||
|
||||
@classmethod
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from configurations import Configuration, values
|
||||
|
||||
|
||||
class Base(Configuration):
|
||||
class Base(DotConfiguration):
|
||||
# Django settings for test_project project.
|
||||
|
||||
DEBUG = values.BooleanValue(True, environ=True)
|
||||
|
|
|
|||
6
tests/settings/dot_env.py
Normal file
6
tests/settings/dot_env.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from configurations import DotConfiguration
|
||||
|
||||
|
||||
class DotEnvConfiguration(DotConfiguration):
|
||||
|
||||
DOT_ENV = 'test_project/.env'
|
||||
|
|
@ -64,7 +64,6 @@ class Test(Configuration):
|
|||
@classmethod
|
||||
def pre_setup(cls):
|
||||
cls.PRE_SETUP_TEST_SETTING = 6
|
||||
cls.read_env('test_project/.env')
|
||||
|
||||
@classmethod
|
||||
def post_setup(cls):
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
import os
|
||||
from django.test import TestCase
|
||||
from configurations.values import BooleanValue
|
||||
from mock import patch
|
||||
|
||||
class EnvValueTests(TestCase):
|
||||
|
||||
class DotEnvLoadingTests(TestCase):
|
||||
|
||||
@patch.dict(os.environ, clear=True,
|
||||
DJANGO_CONFIGURATION='DotEnvConfiguration',
|
||||
DJANGO_SETTINGS_MODULE='tests.settings.dot_env')
|
||||
def test_env_loaded(self):
|
||||
check_value = BooleanValue(False)
|
||||
self.assertEqual(check_value.setup('ENV_LOADED'), True)
|
||||
from tests.settings import dot_env
|
||||
self.assertTrue(dot_env.DOT_ENV_LOADED)
|
||||
Loading…
Reference in a new issue